UtilTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408
  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. self::invokePrivate(\OCP\Util::class, 'scripts', [[]]);
  197. self::invokePrivate(\OCP\Util::class, 'scriptDeps', [[]]);
  198. }
  199. protected function tearDown(): void {
  200. parent::tearDown();
  201. \OC_Util::$scripts = [];
  202. \OC_Util::$styles = [];
  203. self::invokePrivate(\OCP\Util::class, 'scripts', [[]]);
  204. self::invokePrivate(\OCP\Util::class, 'scriptDeps', [[]]);
  205. }
  206. public function testAddScript() {
  207. \OCP\Util::addScript('first', 'myFirstJSFile');
  208. \OCP\Util::addScript('core', 'myFancyJSFile1');
  209. \OCP\Util::addScript('files', 'myFancyJSFile2', 'core');
  210. \OCP\Util::addScript('myApp5', 'myApp5JSFile', 'myApp2');
  211. \OCP\Util::addScript('myApp', 'myFancyJSFile3');
  212. \OCP\Util::addScript('core', 'myFancyJSFile4');
  213. // after itself
  214. \OCP\Util::addScript('core', 'myFancyJSFile5', 'core');
  215. // add duplicate
  216. \OCP\Util::addScript('core', 'myFancyJSFile1');
  217. // dependency chain
  218. \OCP\Util::addScript('myApp4', 'myApp4JSFile', 'myApp3');
  219. \OCP\Util::addScript('myApp3', 'myApp3JSFile', 'myApp2');
  220. \OCP\Util::addScript('myApp2', 'myApp2JSFile', 'myApp');
  221. \OCP\Util::addScript('core', 'common');
  222. \OCP\Util::addScript('core', 'main');
  223. $scripts = \OCP\Util::getScripts();
  224. // Core should appear first
  225. $this->assertEquals(
  226. 0,
  227. array_search('core/js/common', $scripts, true)
  228. );
  229. $this->assertEquals(
  230. 1,
  231. array_search('core/js/main', $scripts, true)
  232. );
  233. $this->assertEquals(
  234. 2,
  235. array_search('core/js/myFancyJSFile1', $scripts, true)
  236. );
  237. $this->assertEquals(
  238. 3,
  239. array_search('core/js/myFancyJSFile4', $scripts, true)
  240. );
  241. // Dependencies should appear before their children
  242. $this->assertLessThan(
  243. array_search('files/js/myFancyJSFile2', $scripts, true),
  244. array_search('core/js/myFancyJSFile3', $scripts, true)
  245. );
  246. $this->assertLessThan(
  247. array_search('myApp2/js/myApp2JSFile', $scripts, true),
  248. array_search('myApp/js/myFancyJSFile3', $scripts, true)
  249. );
  250. $this->assertLessThan(
  251. array_search('myApp3/js/myApp3JSFile', $scripts, true),
  252. array_search('myApp2/js/myApp2JSFile', $scripts, true)
  253. );
  254. $this->assertLessThan(
  255. array_search('myApp4/js/myApp4JSFile', $scripts, true),
  256. array_search('myApp3/js/myApp3JSFile', $scripts, true)
  257. );
  258. $this->assertLessThan(
  259. array_search('myApp5/js/myApp5JSFile', $scripts, true),
  260. array_search('myApp2/js/myApp2JSFile', $scripts, true)
  261. );
  262. // No duplicates
  263. $this->assertEquals(
  264. $scripts,
  265. array_unique($scripts)
  266. );
  267. // All scripts still there
  268. $scripts = [
  269. "core/js/common",
  270. "core/js/main",
  271. "core/js/myFancyJSFile1",
  272. "core/js/myFancyJSFile4",
  273. "core/js/myFancyJSFile5",
  274. "first/l10n/en",
  275. "first/js/myFirstJSFile",
  276. "files/l10n/en",
  277. "files/js/myFancyJSFile2",
  278. "myApp/l10n/en",
  279. "myApp/js/myFancyJSFile3",
  280. "myApp2/l10n/en",
  281. "myApp2/js/myApp2JSFile",
  282. "myApp5/l10n/en",
  283. "myApp5/js/myApp5JSFile",
  284. "myApp3/l10n/en",
  285. "myApp3/js/myApp3JSFile",
  286. "myApp4/l10n/en",
  287. "myApp4/js/myApp4JSFile",
  288. ];
  289. foreach ($scripts as $script) {
  290. $this->assertContains($script, $scripts);
  291. }
  292. }
  293. public function testAddScriptCircularDependency() {
  294. \OCP\Util::addScript('circular', 'file1', 'dependency');
  295. \OCP\Util::addScript('dependency', 'file2', 'circular');
  296. $scripts = \OCP\Util::getScripts();
  297. $this->assertContains('circular/js/file1', $scripts);
  298. $this->assertContains('dependency/js/file2', $scripts);
  299. }
  300. public function testAddVendorScript() {
  301. \OC_Util::addVendorScript('core', 'myFancyJSFile1');
  302. \OC_Util::addVendorScript('myApp', 'myFancyJSFile2');
  303. \OC_Util::addVendorScript('core', 'myFancyJSFile0', true);
  304. \OC_Util::addVendorScript('core', 'myFancyJSFile10', true);
  305. // add duplicate
  306. \OC_Util::addVendorScript('core', 'myFancyJSFile1');
  307. $this->assertEquals([
  308. 'core/vendor/myFancyJSFile10',
  309. 'core/vendor/myFancyJSFile0',
  310. 'core/vendor/myFancyJSFile1',
  311. 'myApp/vendor/myFancyJSFile2',
  312. ], \OC_Util::$scripts);
  313. $this->assertEquals([], \OC_Util::$styles);
  314. }
  315. public function testAddTranslations() {
  316. \OC_Util::addTranslations('appId', 'de');
  317. $this->assertEquals([
  318. 'appId/l10n/de'
  319. ], \OC_Util::$scripts);
  320. $this->assertEquals([], \OC_Util::$styles);
  321. }
  322. public function testAddStyle() {
  323. \OC_Util::addStyle('core', 'myFancyCSSFile1');
  324. \OC_Util::addStyle('myApp', 'myFancyCSSFile2');
  325. \OC_Util::addStyle('core', 'myFancyCSSFile0', true);
  326. \OC_Util::addStyle('core', 'myFancyCSSFile10', true);
  327. // add duplicate
  328. \OC_Util::addStyle('core', 'myFancyCSSFile1');
  329. $this->assertEquals([], \OC_Util::$scripts);
  330. $this->assertEquals([
  331. 'core/css/myFancyCSSFile10',
  332. 'core/css/myFancyCSSFile0',
  333. 'core/css/myFancyCSSFile1',
  334. 'myApp/css/myFancyCSSFile2',
  335. ], \OC_Util::$styles);
  336. }
  337. public function testAddVendorStyle() {
  338. \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
  339. \OC_Util::addVendorStyle('myApp', 'myFancyCSSFile2');
  340. \OC_Util::addVendorStyle('core', 'myFancyCSSFile0', true);
  341. \OC_Util::addVendorStyle('core', 'myFancyCSSFile10', true);
  342. // add duplicate
  343. \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
  344. $this->assertEquals([], \OC_Util::$scripts);
  345. $this->assertEquals([
  346. 'core/vendor/myFancyCSSFile10',
  347. 'core/vendor/myFancyCSSFile0',
  348. 'core/vendor/myFancyCSSFile1',
  349. 'myApp/vendor/myFancyCSSFile2',
  350. ], \OC_Util::$styles);
  351. }
  352. public function testShortenMultibyteString() {
  353. $this->assertEquals('Short nuff', \OCP\Util::shortenMultibyteString('Short nuff', 255));
  354. $this->assertEquals('ABC', \OCP\Util::shortenMultibyteString('ABCDEF', 3));
  355. // each of the characters is 12 bytes
  356. $this->assertEquals('🙈', \OCP\Util::shortenMultibyteString('🙈🙊🙉', 16, 2));
  357. }
  358. }