UtilTest.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424
  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. /**
  82. * Host is "localhost" this is a valid for emails,
  83. * but not for default strict email verification that requires a top level domain.
  84. * So we check that with strict email verification we fallback to the default
  85. */
  86. public function testGetDefaultEmailAddress() {
  87. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  88. $this->assertEquals('no-reply@localhost', $email);
  89. }
  90. /**
  91. * If strict email check is enabled "localhost.localdomain" should validate as a valid email domain
  92. */
  93. public function testGetDefaultEmailAddressStrict() {
  94. $config = \OC::$server->getConfig();
  95. $config->setAppValue('core', 'enforce_strict_email_check', 'yes');
  96. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  97. $this->assertEquals('no-reply@localhost.localdomain', $email);
  98. $config->deleteAppValue('core', 'enforce_strict_email_check');
  99. }
  100. public function testGetDefaultEmailAddressFromConfig() {
  101. $config = \OC::$server->getConfig();
  102. $config->setSystemValue('mail_domain', 'example.com');
  103. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  104. $this->assertEquals('no-reply@example.com', $email);
  105. $config->deleteSystemValue('mail_domain');
  106. }
  107. public function testGetConfiguredEmailAddressFromConfig() {
  108. $config = \OC::$server->getConfig();
  109. $config->setSystemValue('mail_domain', 'example.com');
  110. $config->setSystemValue('mail_from_address', 'owncloud');
  111. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  112. $this->assertEquals('owncloud@example.com', $email);
  113. $config->deleteSystemValue('mail_domain');
  114. $config->deleteSystemValue('mail_from_address');
  115. }
  116. public function testGetInstanceIdGeneratesValidId() {
  117. \OC::$server->getConfig()->deleteSystemValue('instanceid');
  118. $instanceId = OC_Util::getInstanceId();
  119. $this->assertStringStartsWith('oc', $instanceId);
  120. $matchesRegex = preg_match('/^[a-z0-9]+$/', $instanceId);
  121. $this->assertSame(1, $matchesRegex);
  122. }
  123. /**
  124. * @dataProvider filenameValidationProvider
  125. */
  126. public function testFilenameValidation($file, $valid) {
  127. // private API
  128. $this->assertEquals($valid, \OC_Util::isValidFileName($file));
  129. // public API
  130. $this->assertEquals($valid, \OCP\Util::isValidFileName($file));
  131. }
  132. public function filenameValidationProvider() {
  133. return [
  134. // valid names
  135. ['boringname', true],
  136. ['something.with.extension', true],
  137. ['now with spaces', true],
  138. ['.a', true],
  139. ['..a', true],
  140. ['.dotfile', true],
  141. ['single\'quote', true],
  142. [' spaces before', true],
  143. ['spaces after ', true],
  144. ['allowed chars including the crazy ones $%&_-^@!,()[]{}=;#', true],
  145. ['汉字也能用', true],
  146. ['und Ümläüte sind auch willkommen', true],
  147. // disallowed names
  148. ['', false],
  149. [' ', false],
  150. ['.', false],
  151. ['..', false],
  152. ['back\\slash', false],
  153. ['sl/ash', false],
  154. ['lt<lt', true],
  155. ['gt>gt', true],
  156. ['col:on', true],
  157. ['double"quote', true],
  158. ['pi|pe', true],
  159. ['dont?ask?questions?', true],
  160. ['super*star', true],
  161. ['new\nline', false],
  162. // better disallow these to avoid unexpected trimming to have side effects
  163. [' ..', false],
  164. ['.. ', false],
  165. ['. ', false],
  166. [' .', false],
  167. // part files not allowed
  168. ['.part', false],
  169. ['notallowed.part', false],
  170. ['neither.filepart', false],
  171. // part in the middle is ok
  172. ['super movie part one.mkv', true],
  173. ['super.movie.part.mkv', true],
  174. ];
  175. }
  176. /**
  177. * Test needUpgrade() when the core version is increased
  178. */
  179. public function testNeedUpgradeCore() {
  180. $config = \OC::$server->getConfig();
  181. $oldConfigVersion = $config->getSystemValue('version', '0.0.0');
  182. $oldSessionVersion = \OC::$server->getSession()->get('OC_Version');
  183. $this->assertFalse(\OCP\Util::needUpgrade());
  184. $config->setSystemValue('version', '7.0.0.0');
  185. \OC::$server->getSession()->set('OC_Version', [7, 0, 0, 1]);
  186. self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);
  187. $this->assertTrue(\OCP\Util::needUpgrade());
  188. $config->setSystemValue('version', $oldConfigVersion);
  189. \OC::$server->getSession()->set('OC_Version', $oldSessionVersion);
  190. self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);
  191. $this->assertFalse(\OCP\Util::needUpgrade());
  192. }
  193. public function testCheckDataDirectoryValidity() {
  194. $dataDir = \OC::$server->getTempManager()->getTemporaryFolder();
  195. touch($dataDir . '/.ocdata');
  196. $errors = \OC_Util::checkDataDirectoryValidity($dataDir);
  197. $this->assertEmpty($errors);
  198. \OCP\Files::rmdirr($dataDir);
  199. $dataDir = \OC::$server->getTempManager()->getTemporaryFolder();
  200. // no touch
  201. $errors = \OC_Util::checkDataDirectoryValidity($dataDir);
  202. $this->assertNotEmpty($errors);
  203. \OCP\Files::rmdirr($dataDir);
  204. $errors = \OC_Util::checkDataDirectoryValidity('relative/path');
  205. $this->assertNotEmpty($errors);
  206. }
  207. protected function setUp(): void {
  208. parent::setUp();
  209. \OC_Util::$scripts = [];
  210. \OC_Util::$styles = [];
  211. self::invokePrivate(\OCP\Util::class, 'scripts', [[]]);
  212. self::invokePrivate(\OCP\Util::class, 'scriptDeps', [[]]);
  213. }
  214. protected function tearDown(): void {
  215. parent::tearDown();
  216. \OC_Util::$scripts = [];
  217. \OC_Util::$styles = [];
  218. self::invokePrivate(\OCP\Util::class, 'scripts', [[]]);
  219. self::invokePrivate(\OCP\Util::class, 'scriptDeps', [[]]);
  220. }
  221. public function testAddScript() {
  222. \OCP\Util::addScript('first', 'myFirstJSFile');
  223. \OCP\Util::addScript('core', 'myFancyJSFile1');
  224. \OCP\Util::addScript('files', 'myFancyJSFile2', 'core');
  225. \OCP\Util::addScript('myApp5', 'myApp5JSFile', 'myApp2');
  226. \OCP\Util::addScript('myApp', 'myFancyJSFile3');
  227. \OCP\Util::addScript('core', 'myFancyJSFile4');
  228. // after itself
  229. \OCP\Util::addScript('core', 'myFancyJSFile5', 'core');
  230. // add duplicate
  231. \OCP\Util::addScript('core', 'myFancyJSFile1');
  232. // dependency chain
  233. \OCP\Util::addScript('myApp4', 'myApp4JSFile', 'myApp3');
  234. \OCP\Util::addScript('myApp3', 'myApp3JSFile', 'myApp2');
  235. \OCP\Util::addScript('myApp2', 'myApp2JSFile', 'myApp');
  236. \OCP\Util::addScript('core', 'common');
  237. \OCP\Util::addScript('core', 'main');
  238. $scripts = \OCP\Util::getScripts();
  239. // Core should appear first
  240. $this->assertEquals(
  241. 0,
  242. array_search('core/js/common', $scripts, true)
  243. );
  244. $this->assertEquals(
  245. 1,
  246. array_search('core/js/main', $scripts, true)
  247. );
  248. $this->assertEquals(
  249. 2,
  250. array_search('core/js/myFancyJSFile1', $scripts, true)
  251. );
  252. $this->assertEquals(
  253. 3,
  254. array_search('core/js/myFancyJSFile4', $scripts, true)
  255. );
  256. // Dependencies should appear before their children
  257. $this->assertLessThan(
  258. array_search('files/js/myFancyJSFile2', $scripts, true),
  259. array_search('core/js/myFancyJSFile3', $scripts, true)
  260. );
  261. $this->assertLessThan(
  262. array_search('myApp2/js/myApp2JSFile', $scripts, true),
  263. array_search('myApp/js/myFancyJSFile3', $scripts, true)
  264. );
  265. $this->assertLessThan(
  266. array_search('myApp3/js/myApp3JSFile', $scripts, true),
  267. array_search('myApp2/js/myApp2JSFile', $scripts, true)
  268. );
  269. $this->assertLessThan(
  270. array_search('myApp4/js/myApp4JSFile', $scripts, true),
  271. array_search('myApp3/js/myApp3JSFile', $scripts, true)
  272. );
  273. $this->assertLessThan(
  274. array_search('myApp5/js/myApp5JSFile', $scripts, true),
  275. array_search('myApp2/js/myApp2JSFile', $scripts, true)
  276. );
  277. // No duplicates
  278. $this->assertEquals(
  279. $scripts,
  280. array_unique($scripts)
  281. );
  282. // All scripts still there
  283. $scripts = [
  284. "core/js/common",
  285. "core/js/main",
  286. "core/js/myFancyJSFile1",
  287. "core/js/myFancyJSFile4",
  288. "core/js/myFancyJSFile5",
  289. "first/l10n/en",
  290. "first/js/myFirstJSFile",
  291. "files/l10n/en",
  292. "files/js/myFancyJSFile2",
  293. "myApp/l10n/en",
  294. "myApp/js/myFancyJSFile3",
  295. "myApp2/l10n/en",
  296. "myApp2/js/myApp2JSFile",
  297. "myApp5/l10n/en",
  298. "myApp5/js/myApp5JSFile",
  299. "myApp3/l10n/en",
  300. "myApp3/js/myApp3JSFile",
  301. "myApp4/l10n/en",
  302. "myApp4/js/myApp4JSFile",
  303. ];
  304. foreach ($scripts as $script) {
  305. $this->assertContains($script, $scripts);
  306. }
  307. }
  308. public function testAddScriptCircularDependency() {
  309. \OCP\Util::addScript('circular', 'file1', 'dependency');
  310. \OCP\Util::addScript('dependency', 'file2', 'circular');
  311. $scripts = \OCP\Util::getScripts();
  312. $this->assertContains('circular/js/file1', $scripts);
  313. $this->assertContains('dependency/js/file2', $scripts);
  314. }
  315. public function testAddVendorScript() {
  316. \OC_Util::addVendorScript('core', 'myFancyJSFile1');
  317. \OC_Util::addVendorScript('myApp', 'myFancyJSFile2');
  318. \OC_Util::addVendorScript('core', 'myFancyJSFile0', true);
  319. \OC_Util::addVendorScript('core', 'myFancyJSFile10', true);
  320. // add duplicate
  321. \OC_Util::addVendorScript('core', 'myFancyJSFile1');
  322. $this->assertEquals([
  323. 'core/vendor/myFancyJSFile10',
  324. 'core/vendor/myFancyJSFile0',
  325. 'core/vendor/myFancyJSFile1',
  326. 'myApp/vendor/myFancyJSFile2',
  327. ], \OC_Util::$scripts);
  328. $this->assertEquals([], \OC_Util::$styles);
  329. }
  330. public function testAddTranslations() {
  331. \OC_Util::addTranslations('appId', 'de');
  332. $this->assertEquals([
  333. 'appId/l10n/de'
  334. ], \OC_Util::$scripts);
  335. $this->assertEquals([], \OC_Util::$styles);
  336. }
  337. public function testAddStyle() {
  338. \OC_Util::addStyle('core', 'myFancyCSSFile1');
  339. \OC_Util::addStyle('myApp', 'myFancyCSSFile2');
  340. \OC_Util::addStyle('core', 'myFancyCSSFile0', true);
  341. \OC_Util::addStyle('core', 'myFancyCSSFile10', true);
  342. // add duplicate
  343. \OC_Util::addStyle('core', 'myFancyCSSFile1');
  344. $this->assertEquals([], \OC_Util::$scripts);
  345. $this->assertEquals([
  346. 'core/css/myFancyCSSFile10',
  347. 'core/css/myFancyCSSFile0',
  348. 'core/css/myFancyCSSFile1',
  349. 'myApp/css/myFancyCSSFile2',
  350. ], \OC_Util::$styles);
  351. }
  352. public function testAddVendorStyle() {
  353. \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
  354. \OC_Util::addVendorStyle('myApp', 'myFancyCSSFile2');
  355. \OC_Util::addVendorStyle('core', 'myFancyCSSFile0', true);
  356. \OC_Util::addVendorStyle('core', 'myFancyCSSFile10', true);
  357. // add duplicate
  358. \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
  359. $this->assertEquals([], \OC_Util::$scripts);
  360. $this->assertEquals([
  361. 'core/vendor/myFancyCSSFile10',
  362. 'core/vendor/myFancyCSSFile0',
  363. 'core/vendor/myFancyCSSFile1',
  364. 'myApp/vendor/myFancyCSSFile2',
  365. ], \OC_Util::$styles);
  366. }
  367. public function testShortenMultibyteString() {
  368. $this->assertEquals('Short nuff', \OCP\Util::shortenMultibyteString('Short nuff', 255));
  369. $this->assertEquals('ABC', \OCP\Util::shortenMultibyteString('ABCDEF', 3));
  370. // each of the characters is 12 bytes
  371. $this->assertEquals('🙈', \OCP\Util::shortenMultibyteString('🙈🙊🙉', 16, 2));
  372. }
  373. }