1
0

UtilTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-or-later
  6. */
  7. namespace Test;
  8. use OC_Util;
  9. /**
  10. * Class UtilTest
  11. *
  12. * @package Test
  13. * @group DB
  14. */
  15. class UtilTest extends \Test\TestCase {
  16. public function testGetVersion() {
  17. $version = \OCP\Util::getVersion();
  18. $this->assertTrue(is_array($version));
  19. foreach ($version as $num) {
  20. $this->assertTrue(is_int($num));
  21. }
  22. }
  23. public function testGetVersionString() {
  24. $version = \OC_Util::getVersionString();
  25. $this->assertTrue(is_string($version));
  26. }
  27. public function testGetEditionString() {
  28. $edition = \OC_Util::getEditionString();
  29. $this->assertTrue(is_string($edition));
  30. }
  31. public function testSanitizeHTML() {
  32. $badArray = [
  33. 'While it is unusual to pass an array',
  34. 'this function actually <blink>supports</blink> it.',
  35. 'And therefore there needs to be a <script>alert("Unit"+\'test\')</script> for it!',
  36. [
  37. 'And It Even May <strong>Nest</strong>',
  38. ],
  39. ];
  40. $goodArray = [
  41. 'While it is unusual to pass an array',
  42. 'this function actually &lt;blink&gt;supports&lt;/blink&gt; it.',
  43. 'And therefore there needs to be a &lt;script&gt;alert(&quot;Unit&quot;+&#039;test&#039;)&lt;/script&gt; for it!',
  44. [
  45. 'And It Even May &lt;strong&gt;Nest&lt;/strong&gt;'
  46. ],
  47. ];
  48. $result = OC_Util::sanitizeHTML($badArray);
  49. $this->assertEquals($goodArray, $result);
  50. $badString = '<img onload="alert(1)" />';
  51. $result = OC_Util::sanitizeHTML($badString);
  52. $this->assertEquals('&lt;img onload=&quot;alert(1)&quot; /&gt;', $result);
  53. $badString = "<script>alert('Hacked!');</script>";
  54. $result = OC_Util::sanitizeHTML($badString);
  55. $this->assertEquals('&lt;script&gt;alert(&#039;Hacked!&#039;);&lt;/script&gt;', $result);
  56. $goodString = 'This is a good string without HTML.';
  57. $result = OC_Util::sanitizeHTML($goodString);
  58. $this->assertEquals('This is a good string without HTML.', $result);
  59. }
  60. public function testEncodePath() {
  61. $component = '/§#@test%&^ä/-child';
  62. $result = OC_Util::encodePath($component);
  63. $this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result);
  64. }
  65. public function testIsNonUTF8Locale() {
  66. // OC_Util::isNonUTF8Locale() assumes escapeshellcmd('§') returns '' with non-UTF-8 locale.
  67. $locale = setlocale(LC_CTYPE, 0);
  68. setlocale(LC_CTYPE, 'C');
  69. $this->assertEquals('', escapeshellcmd('§'));
  70. $this->assertEquals('\'\'', escapeshellarg('§'));
  71. setlocale(LC_CTYPE, 'C.UTF-8');
  72. $this->assertEquals('§', escapeshellcmd('§'));
  73. $this->assertEquals('\'§\'', escapeshellarg('§'));
  74. setlocale(LC_CTYPE, $locale);
  75. }
  76. public function testFileInfoLoaded() {
  77. $expected = function_exists('finfo_open');
  78. $this->assertEquals($expected, \OC_Util::fileInfoLoaded());
  79. }
  80. public function testGetDefaultEmailAddress() {
  81. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  82. $this->assertEquals('no-reply@localhost', $email);
  83. }
  84. public function testGetDefaultEmailAddressFromConfig() {
  85. $config = \OC::$server->getConfig();
  86. $config->setSystemValue('mail_domain', 'example.com');
  87. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  88. $this->assertEquals('no-reply@example.com', $email);
  89. $config->deleteSystemValue('mail_domain');
  90. }
  91. public function testGetConfiguredEmailAddressFromConfig() {
  92. $config = \OC::$server->getConfig();
  93. $config->setSystemValue('mail_domain', 'example.com');
  94. $config->setSystemValue('mail_from_address', 'owncloud');
  95. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  96. $this->assertEquals('owncloud@example.com', $email);
  97. $config->deleteSystemValue('mail_domain');
  98. $config->deleteSystemValue('mail_from_address');
  99. }
  100. public function testGetInstanceIdGeneratesValidId() {
  101. \OC::$server->getConfig()->deleteSystemValue('instanceid');
  102. $instanceId = OC_Util::getInstanceId();
  103. $this->assertStringStartsWith('oc', $instanceId);
  104. $matchesRegex = preg_match('/^[a-z0-9]+$/', $instanceId);
  105. $this->assertSame(1, $matchesRegex);
  106. }
  107. /**
  108. * @dataProvider filenameValidationProvider
  109. */
  110. public function testFilenameValidation($file, $valid) {
  111. // private API
  112. $this->assertEquals($valid, \OC_Util::isValidFileName($file));
  113. // public API
  114. $this->assertEquals($valid, \OCP\Util::isValidFileName($file));
  115. }
  116. public function filenameValidationProvider() {
  117. return [
  118. // valid names
  119. ['boringname', true],
  120. ['something.with.extension', true],
  121. ['now with spaces', true],
  122. ['.a', true],
  123. ['..a', true],
  124. ['.dotfile', true],
  125. ['single\'quote', true],
  126. [' spaces before', true],
  127. ['spaces after ', true],
  128. ['allowed chars including the crazy ones $%&_-^@!,()[]{}=;#', true],
  129. ['汉字也能用', true],
  130. ['und Ümläüte sind auch willkommen', true],
  131. // disallowed names
  132. ['', false],
  133. [' ', false],
  134. ['.', false],
  135. ['..', false],
  136. ['back\\slash', false],
  137. ['sl/ash', false],
  138. ['lt<lt', true],
  139. ['gt>gt', true],
  140. ['col:on', true],
  141. ['double"quote', true],
  142. ['pi|pe', true],
  143. ['dont?ask?questions?', true],
  144. ['super*star', true],
  145. ['new\nline', false],
  146. // better disallow these to avoid unexpected trimming to have side effects
  147. [' ..', false],
  148. ['.. ', false],
  149. ['. ', false],
  150. [' .', false],
  151. // part files not allowed
  152. ['.part', false],
  153. ['notallowed.part', false],
  154. ['neither.filepart', false],
  155. // part in the middle is ok
  156. ['super movie part one.mkv', true],
  157. ['super.movie.part.mkv', true],
  158. ];
  159. }
  160. /**
  161. * Test needUpgrade() when the core version is increased
  162. */
  163. public function testNeedUpgradeCore() {
  164. $config = \OC::$server->getConfig();
  165. $oldConfigVersion = $config->getSystemValue('version', '0.0.0');
  166. $oldSessionVersion = \OC::$server->getSession()->get('OC_Version');
  167. $this->assertFalse(\OCP\Util::needUpgrade());
  168. $config->setSystemValue('version', '7.0.0.0');
  169. \OC::$server->getSession()->set('OC_Version', [7, 0, 0, 1]);
  170. self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);
  171. $this->assertTrue(\OCP\Util::needUpgrade());
  172. $config->setSystemValue('version', $oldConfigVersion);
  173. \OC::$server->getSession()->set('OC_Version', $oldSessionVersion);
  174. self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);
  175. $this->assertFalse(\OCP\Util::needUpgrade());
  176. }
  177. public function testCheckDataDirectoryValidity() {
  178. $dataDir = \OC::$server->getTempManager()->getTemporaryFolder();
  179. touch($dataDir . '/.ocdata');
  180. $errors = \OC_Util::checkDataDirectoryValidity($dataDir);
  181. $this->assertEmpty($errors);
  182. \OCP\Files::rmdirr($dataDir);
  183. $dataDir = \OC::$server->getTempManager()->getTemporaryFolder();
  184. // no touch
  185. $errors = \OC_Util::checkDataDirectoryValidity($dataDir);
  186. $this->assertNotEmpty($errors);
  187. \OCP\Files::rmdirr($dataDir);
  188. $errors = \OC_Util::checkDataDirectoryValidity('relative/path');
  189. $this->assertNotEmpty($errors);
  190. }
  191. protected function setUp(): void {
  192. parent::setUp();
  193. \OC_Util::$scripts = [];
  194. \OC_Util::$styles = [];
  195. self::invokePrivate(\OCP\Util::class, 'scripts', [[]]);
  196. self::invokePrivate(\OCP\Util::class, 'scriptDeps', [[]]);
  197. }
  198. protected function tearDown(): void {
  199. parent::tearDown();
  200. \OC_Util::$scripts = [];
  201. \OC_Util::$styles = [];
  202. self::invokePrivate(\OCP\Util::class, 'scripts', [[]]);
  203. self::invokePrivate(\OCP\Util::class, 'scriptDeps', [[]]);
  204. }
  205. public function testAddScript() {
  206. \OCP\Util::addScript('first', 'myFirstJSFile');
  207. \OCP\Util::addScript('core', 'myFancyJSFile1');
  208. \OCP\Util::addScript('files', 'myFancyJSFile2', 'core');
  209. \OCP\Util::addScript('myApp5', 'myApp5JSFile', 'myApp2');
  210. \OCP\Util::addScript('myApp', 'myFancyJSFile3');
  211. \OCP\Util::addScript('core', 'myFancyJSFile4');
  212. // after itself
  213. \OCP\Util::addScript('core', 'myFancyJSFile5', 'core');
  214. // add duplicate
  215. \OCP\Util::addScript('core', 'myFancyJSFile1');
  216. // dependency chain
  217. \OCP\Util::addScript('myApp4', 'myApp4JSFile', 'myApp3');
  218. \OCP\Util::addScript('myApp3', 'myApp3JSFile', 'myApp2');
  219. \OCP\Util::addScript('myApp2', 'myApp2JSFile', 'myApp');
  220. \OCP\Util::addScript('core', 'common');
  221. \OCP\Util::addScript('core', 'main');
  222. $scripts = \OCP\Util::getScripts();
  223. // Core should appear first
  224. $this->assertEquals(
  225. 0,
  226. array_search('core/js/common', $scripts, true)
  227. );
  228. $this->assertEquals(
  229. 1,
  230. array_search('core/js/main', $scripts, true)
  231. );
  232. $this->assertEquals(
  233. 2,
  234. array_search('core/js/myFancyJSFile1', $scripts, true)
  235. );
  236. $this->assertEquals(
  237. 3,
  238. array_search('core/js/myFancyJSFile4', $scripts, true)
  239. );
  240. // Dependencies should appear before their children
  241. $this->assertLessThan(
  242. array_search('files/js/myFancyJSFile2', $scripts, true),
  243. array_search('core/js/myFancyJSFile3', $scripts, true)
  244. );
  245. $this->assertLessThan(
  246. array_search('myApp2/js/myApp2JSFile', $scripts, true),
  247. array_search('myApp/js/myFancyJSFile3', $scripts, true)
  248. );
  249. $this->assertLessThan(
  250. array_search('myApp3/js/myApp3JSFile', $scripts, true),
  251. array_search('myApp2/js/myApp2JSFile', $scripts, true)
  252. );
  253. $this->assertLessThan(
  254. array_search('myApp4/js/myApp4JSFile', $scripts, true),
  255. array_search('myApp3/js/myApp3JSFile', $scripts, true)
  256. );
  257. $this->assertLessThan(
  258. array_search('myApp5/js/myApp5JSFile', $scripts, true),
  259. array_search('myApp2/js/myApp2JSFile', $scripts, true)
  260. );
  261. // No duplicates
  262. $this->assertEquals(
  263. $scripts,
  264. array_unique($scripts)
  265. );
  266. // All scripts still there
  267. $scripts = [
  268. "core/js/common",
  269. "core/js/main",
  270. "core/js/myFancyJSFile1",
  271. "core/js/myFancyJSFile4",
  272. "core/js/myFancyJSFile5",
  273. "first/l10n/en",
  274. "first/js/myFirstJSFile",
  275. "files/l10n/en",
  276. "files/js/myFancyJSFile2",
  277. "myApp/l10n/en",
  278. "myApp/js/myFancyJSFile3",
  279. "myApp2/l10n/en",
  280. "myApp2/js/myApp2JSFile",
  281. "myApp5/l10n/en",
  282. "myApp5/js/myApp5JSFile",
  283. "myApp3/l10n/en",
  284. "myApp3/js/myApp3JSFile",
  285. "myApp4/l10n/en",
  286. "myApp4/js/myApp4JSFile",
  287. ];
  288. foreach ($scripts as $script) {
  289. $this->assertContains($script, $scripts);
  290. }
  291. }
  292. public function testAddScriptCircularDependency() {
  293. \OCP\Util::addScript('circular', 'file1', 'dependency');
  294. \OCP\Util::addScript('dependency', 'file2', 'circular');
  295. $scripts = \OCP\Util::getScripts();
  296. $this->assertContains('circular/js/file1', $scripts);
  297. $this->assertContains('dependency/js/file2', $scripts);
  298. }
  299. public function testAddVendorScript() {
  300. \OC_Util::addVendorScript('core', 'myFancyJSFile1');
  301. \OC_Util::addVendorScript('myApp', 'myFancyJSFile2');
  302. \OC_Util::addVendorScript('core', 'myFancyJSFile0', true);
  303. \OC_Util::addVendorScript('core', 'myFancyJSFile10', true);
  304. // add duplicate
  305. \OC_Util::addVendorScript('core', 'myFancyJSFile1');
  306. $this->assertEquals([
  307. 'core/vendor/myFancyJSFile10',
  308. 'core/vendor/myFancyJSFile0',
  309. 'core/vendor/myFancyJSFile1',
  310. 'myApp/vendor/myFancyJSFile2',
  311. ], \OC_Util::$scripts);
  312. $this->assertEquals([], \OC_Util::$styles);
  313. }
  314. public function testAddTranslations() {
  315. \OC_Util::addTranslations('appId', 'de');
  316. $this->assertEquals([
  317. 'appId/l10n/de'
  318. ], \OC_Util::$scripts);
  319. $this->assertEquals([], \OC_Util::$styles);
  320. }
  321. public function testAddStyle() {
  322. \OC_Util::addStyle('core', 'myFancyCSSFile1');
  323. \OC_Util::addStyle('myApp', 'myFancyCSSFile2');
  324. \OC_Util::addStyle('core', 'myFancyCSSFile0', true);
  325. \OC_Util::addStyle('core', 'myFancyCSSFile10', true);
  326. // add duplicate
  327. \OC_Util::addStyle('core', 'myFancyCSSFile1');
  328. $this->assertEquals([], \OC_Util::$scripts);
  329. $this->assertEquals([
  330. 'core/css/myFancyCSSFile10',
  331. 'core/css/myFancyCSSFile0',
  332. 'core/css/myFancyCSSFile1',
  333. 'myApp/css/myFancyCSSFile2',
  334. ], \OC_Util::$styles);
  335. }
  336. public function testAddVendorStyle() {
  337. \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
  338. \OC_Util::addVendorStyle('myApp', 'myFancyCSSFile2');
  339. \OC_Util::addVendorStyle('core', 'myFancyCSSFile0', true);
  340. \OC_Util::addVendorStyle('core', 'myFancyCSSFile10', true);
  341. // add duplicate
  342. \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
  343. $this->assertEquals([], \OC_Util::$scripts);
  344. $this->assertEquals([
  345. 'core/vendor/myFancyCSSFile10',
  346. 'core/vendor/myFancyCSSFile0',
  347. 'core/vendor/myFancyCSSFile1',
  348. 'myApp/vendor/myFancyCSSFile2',
  349. ], \OC_Util::$styles);
  350. }
  351. public function testShortenMultibyteString() {
  352. $this->assertEquals('Short nuff', \OCP\Util::shortenMultibyteString('Short nuff', 255));
  353. $this->assertEquals('ABC', \OCP\Util::shortenMultibyteString('ABCDEF', 3));
  354. // each of the characters is 12 bytes
  355. $this->assertEquals('🙈', \OCP\Util::shortenMultibyteString('🙈🙊🙉', 16, 2));
  356. }
  357. }