You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

313 lines
9.4 KiB

10 years ago
10 years ago
12 years ago
13 years ago
  1. <?php
  2. /**
  3. * Copyright (c) 2012 Lukas Reschke <lukas@statuscode.ch>
  4. * This file is licensed under the Affero General Public License version 3 or
  5. * later.
  6. * See the COPYING-README file.
  7. */
  8. namespace Test;
  9. use OC_Util;
  10. /**
  11. * Class UtilTest
  12. *
  13. * @package Test
  14. * @group DB
  15. */
  16. class UtilTest extends \Test\TestCase {
  17. public function testGetVersion() {
  18. $version = \OCP\Util::getVersion();
  19. $this->assertTrue(is_array($version));
  20. foreach ($version as $num) {
  21. $this->assertTrue(is_int($num));
  22. }
  23. }
  24. public function testGetVersionString() {
  25. $version = \OC_Util::getVersionString();
  26. $this->assertTrue(is_string($version));
  27. }
  28. public function testGetEditionString() {
  29. $edition = \OC_Util::getEditionString();
  30. $this->assertTrue(is_string($edition));
  31. }
  32. public function testSanitizeHTML() {
  33. $badArray = [
  34. 'While it is unusual to pass an array',
  35. 'this function actually <blink>supports</blink> it.',
  36. 'And therefore there needs to be a <script>alert("Unit"+\'test\')</script> for it!',
  37. [
  38. 'And It Even May <strong>Nest</strong>',
  39. ],
  40. ];
  41. $goodArray = [
  42. 'While it is unusual to pass an array',
  43. 'this function actually &lt;blink&gt;supports&lt;/blink&gt; it.',
  44. 'And therefore there needs to be a &lt;script&gt;alert(&quot;Unit&quot;+&#039;test&#039;)&lt;/script&gt; for it!',
  45. [
  46. 'And It Even May &lt;strong&gt;Nest&lt;/strong&gt;'
  47. ],
  48. ];
  49. $result = OC_Util::sanitizeHTML($badArray);
  50. $this->assertEquals($goodArray, $result);
  51. $badString = '<img onload="alert(1)" />';
  52. $result = OC_Util::sanitizeHTML($badString);
  53. $this->assertEquals('&lt;img onload=&quot;alert(1)&quot; /&gt;', $result);
  54. $badString = "<script>alert('Hacked!');</script>";
  55. $result = OC_Util::sanitizeHTML($badString);
  56. $this->assertEquals('&lt;script&gt;alert(&#039;Hacked!&#039;);&lt;/script&gt;', $result);
  57. $goodString = 'This is a good string without HTML.';
  58. $result = OC_Util::sanitizeHTML($goodString);
  59. $this->assertEquals('This is a good string without HTML.', $result);
  60. }
  61. public function testEncodePath() {
  62. $component = '/§#@test%&^ä/-child';
  63. $result = OC_Util::encodePath($component);
  64. $this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result);
  65. }
  66. public function testIsNonUTF8Locale() {
  67. // OC_Util::isNonUTF8Locale() assumes escapeshellcmd('§') returns '' with non-UTF-8 locale.
  68. $locale = setlocale(LC_CTYPE, 0);
  69. setlocale(LC_CTYPE, 'C');
  70. $this->assertEquals('', escapeshellcmd('§'));
  71. $this->assertEquals('\'\'', escapeshellarg('§'));
  72. setlocale(LC_CTYPE, 'C.UTF-8');
  73. $this->assertEquals('§', escapeshellcmd('§'));
  74. $this->assertEquals('\'§\'', escapeshellarg('§'));
  75. setlocale(LC_CTYPE, $locale);
  76. }
  77. public function testFileInfoLoaded() {
  78. $expected = function_exists('finfo_open');
  79. $this->assertEquals($expected, \OC_Util::fileInfoLoaded());
  80. }
  81. public function testGetDefaultEmailAddress() {
  82. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  83. $this->assertEquals('no-reply@localhost', $email);
  84. }
  85. public function testGetDefaultEmailAddressFromConfig() {
  86. $config = \OC::$server->getConfig();
  87. $config->setSystemValue('mail_domain', 'example.com');
  88. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  89. $this->assertEquals('no-reply@example.com', $email);
  90. $config->deleteSystemValue('mail_domain');
  91. }
  92. public function testGetConfiguredEmailAddressFromConfig() {
  93. $config = \OC::$server->getConfig();
  94. $config->setSystemValue('mail_domain', 'example.com');
  95. $config->setSystemValue('mail_from_address', 'owncloud');
  96. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  97. $this->assertEquals('owncloud@example.com', $email);
  98. $config->deleteSystemValue('mail_domain');
  99. $config->deleteSystemValue('mail_from_address');
  100. }
  101. public function testGetInstanceIdGeneratesValidId() {
  102. \OC::$server->getConfig()->deleteSystemValue('instanceid');
  103. $instanceId = OC_Util::getInstanceId();
  104. $this->assertStringStartsWith('oc', $instanceId);
  105. $matchesRegex = preg_match('/^[a-z0-9]+$/', $instanceId);
  106. $this->assertSame(1, $matchesRegex);
  107. }
  108. /**
  109. * @dataProvider filenameValidationProvider
  110. */
  111. public function testFilenameValidation($file, $valid) {
  112. // private API
  113. $this->assertEquals($valid, \OC_Util::isValidFileName($file));
  114. // public API
  115. $this->assertEquals($valid, \OCP\Util::isValidFileName($file));
  116. }
  117. public function filenameValidationProvider() {
  118. return [
  119. // valid names
  120. ['boringname', true],
  121. ['something.with.extension', true],
  122. ['now with spaces', true],
  123. ['.a', true],
  124. ['..a', true],
  125. ['.dotfile', true],
  126. ['single\'quote', true],
  127. [' spaces before', true],
  128. ['spaces after ', true],
  129. ['allowed chars including the crazy ones $%&_-^@!,()[]{}=;#', true],
  130. ['汉字也能用', true],
  131. ['und Ümläüte sind auch willkommen', true],
  132. // disallowed names
  133. ['', false],
  134. [' ', false],
  135. ['.', false],
  136. ['..', false],
  137. ['back\\slash', false],
  138. ['sl/ash', false],
  139. ['lt<lt', true],
  140. ['gt>gt', true],
  141. ['col:on', true],
  142. ['double"quote', true],
  143. ['pi|pe', true],
  144. ['dont?ask?questions?', true],
  145. ['super*star', true],
  146. ['new\nline', false],
  147. // better disallow these to avoid unexpected trimming to have side effects
  148. [' ..', false],
  149. ['.. ', false],
  150. ['. ', false],
  151. [' .', false],
  152. // part files not allowed
  153. ['.part', false],
  154. ['notallowed.part', false],
  155. ['neither.filepart', false],
  156. // part in the middle is ok
  157. ['super movie part one.mkv', true],
  158. ['super.movie.part.mkv', true],
  159. ];
  160. }
  161. /**
  162. * Test needUpgrade() when the core version is increased
  163. */
  164. public function testNeedUpgradeCore() {
  165. $config = \OC::$server->getConfig();
  166. $oldConfigVersion = $config->getSystemValue('version', '0.0.0');
  167. $oldSessionVersion = \OC::$server->getSession()->get('OC_Version');
  168. $this->assertFalse(\OCP\Util::needUpgrade());
  169. $config->setSystemValue('version', '7.0.0.0');
  170. \OC::$server->getSession()->set('OC_Version', [7, 0, 0, 1]);
  171. self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);
  172. $this->assertTrue(\OCP\Util::needUpgrade());
  173. $config->setSystemValue('version', $oldConfigVersion);
  174. \OC::$server->getSession()->set('OC_Version', $oldSessionVersion);
  175. self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);
  176. $this->assertFalse(\OCP\Util::needUpgrade());
  177. }
  178. public function testCheckDataDirectoryValidity() {
  179. $dataDir = \OC::$server->getTempManager()->getTemporaryFolder();
  180. touch($dataDir . '/.ocdata');
  181. $errors = \OC_Util::checkDataDirectoryValidity($dataDir);
  182. $this->assertEmpty($errors);
  183. \OCP\Files::rmdirr($dataDir);
  184. $dataDir = \OC::$server->getTempManager()->getTemporaryFolder();
  185. // no touch
  186. $errors = \OC_Util::checkDataDirectoryValidity($dataDir);
  187. $this->assertNotEmpty($errors);
  188. \OCP\Files::rmdirr($dataDir);
  189. $errors = \OC_Util::checkDataDirectoryValidity('relative/path');
  190. $this->assertNotEmpty($errors);
  191. }
  192. protected function setUp(): void {
  193. parent::setUp();
  194. \OC_Util::$scripts = [];
  195. \OC_Util::$styles = [];
  196. }
  197. protected function tearDown(): void {
  198. parent::tearDown();
  199. \OC_Util::$scripts = [];
  200. \OC_Util::$styles = [];
  201. }
  202. public function testAddScript() {
  203. \OC_Util::addScript('core', 'myFancyJSFile1');
  204. \OC_Util::addScript('myApp', 'myFancyJSFile2');
  205. \OC_Util::addScript('core', 'myFancyJSFile0', true);
  206. \OC_Util::addScript('core', 'myFancyJSFile10', true);
  207. // add duplicate
  208. \OC_Util::addScript('core', 'myFancyJSFile1');
  209. $this->assertEquals([
  210. 'core/js/myFancyJSFile10',
  211. 'core/js/myFancyJSFile0',
  212. 'core/js/myFancyJSFile1',
  213. 'myApp/l10n/en',
  214. 'myApp/js/myFancyJSFile2',
  215. ], \OC_Util::$scripts);
  216. $this->assertEquals([], \OC_Util::$styles);
  217. }
  218. public function testAddVendorScript() {
  219. \OC_Util::addVendorScript('core', 'myFancyJSFile1');
  220. \OC_Util::addVendorScript('myApp', 'myFancyJSFile2');
  221. \OC_Util::addVendorScript('core', 'myFancyJSFile0', true);
  222. \OC_Util::addVendorScript('core', 'myFancyJSFile10', true);
  223. // add duplicate
  224. \OC_Util::addVendorScript('core', 'myFancyJSFile1');
  225. $this->assertEquals([
  226. 'core/vendor/myFancyJSFile10',
  227. 'core/vendor/myFancyJSFile0',
  228. 'core/vendor/myFancyJSFile1',
  229. 'myApp/vendor/myFancyJSFile2',
  230. ], \OC_Util::$scripts);
  231. $this->assertEquals([], \OC_Util::$styles);
  232. }
  233. public function testAddTranslations() {
  234. \OC_Util::addTranslations('appId', 'de');
  235. $this->assertEquals([
  236. 'appId/l10n/de'
  237. ], \OC_Util::$scripts);
  238. $this->assertEquals([], \OC_Util::$styles);
  239. }
  240. public function testAddStyle() {
  241. \OC_Util::addStyle('core', 'myFancyCSSFile1');
  242. \OC_Util::addStyle('myApp', 'myFancyCSSFile2');
  243. \OC_Util::addStyle('core', 'myFancyCSSFile0', true);
  244. \OC_Util::addStyle('core', 'myFancyCSSFile10', true);
  245. // add duplicate
  246. \OC_Util::addStyle('core', 'myFancyCSSFile1');
  247. $this->assertEquals([], \OC_Util::$scripts);
  248. $this->assertEquals([
  249. 'core/css/myFancyCSSFile10',
  250. 'core/css/myFancyCSSFile0',
  251. 'core/css/myFancyCSSFile1',
  252. 'myApp/css/myFancyCSSFile2',
  253. ], \OC_Util::$styles);
  254. }
  255. public function testAddVendorStyle() {
  256. \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
  257. \OC_Util::addVendorStyle('myApp', 'myFancyCSSFile2');
  258. \OC_Util::addVendorStyle('core', 'myFancyCSSFile0', true);
  259. \OC_Util::addVendorStyle('core', 'myFancyCSSFile10', true);
  260. // add duplicate
  261. \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
  262. $this->assertEquals([], \OC_Util::$scripts);
  263. $this->assertEquals([
  264. 'core/vendor/myFancyCSSFile10',
  265. 'core/vendor/myFancyCSSFile0',
  266. 'core/vendor/myFancyCSSFile1',
  267. 'myApp/vendor/myFancyCSSFile2',
  268. ], \OC_Util::$styles);
  269. }
  270. }