1
0

UtilTest.php 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  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. /**
  81. * Host is "localhost" this is a valid for emails,
  82. * but not for default strict email verification that requires a top level domain.
  83. * So we check that with strict email verification we fallback to the default
  84. */
  85. public function testGetDefaultEmailAddressStrict() {
  86. $email = \OCP\Util::getDefaultEmailAddress('no-reply');
  87. $this->assertEquals('no-reply@localhost.localdomain', $email);
  88. }
  89. /**
  90. * If no strict email check is enabled "localhost" should validate as a valid email domain
  91. */
  92. public function testGetDefaultEmailAddress() {
  93. $config = \OC::$server->getConfig();
  94. $config->setAppValue('core', 'enforce_strict_email_check', 'no');
  95. $email = \OCP\Util::getDefaultEmailAddress('no-reply');
  96. $this->assertEquals('no-reply@localhost', $email);
  97. $config->deleteAppValue('core', 'enforce_strict_email_check');
  98. }
  99. public function testGetDefaultEmailAddressFromConfig() {
  100. $config = \OC::$server->getConfig();
  101. $config->setSystemValue('mail_domain', 'example.com');
  102. $email = \OCP\Util::getDefaultEmailAddress('no-reply');
  103. $this->assertEquals('no-reply@example.com', $email);
  104. $config->deleteSystemValue('mail_domain');
  105. }
  106. public function testGetConfiguredEmailAddressFromConfig() {
  107. $config = \OC::$server->getConfig();
  108. $config->setSystemValue('mail_domain', 'example.com');
  109. $config->setSystemValue('mail_from_address', 'owncloud');
  110. $email = \OCP\Util::getDefaultEmailAddress('no-reply');
  111. $this->assertEquals('owncloud@example.com', $email);
  112. $config->deleteSystemValue('mail_domain');
  113. $config->deleteSystemValue('mail_from_address');
  114. }
  115. public function testGetInstanceIdGeneratesValidId() {
  116. \OC::$server->getConfig()->deleteSystemValue('instanceid');
  117. $instanceId = OC_Util::getInstanceId();
  118. $this->assertStringStartsWith('oc', $instanceId);
  119. $matchesRegex = preg_match('/^[a-z0-9]+$/', $instanceId);
  120. $this->assertSame(1, $matchesRegex);
  121. }
  122. /**
  123. * Test needUpgrade() when the core version is increased
  124. */
  125. public function testNeedUpgradeCore() {
  126. $config = \OC::$server->getConfig();
  127. $oldConfigVersion = $config->getSystemValue('version', '0.0.0');
  128. $oldSessionVersion = \OC::$server->getSession()->get('OC_Version');
  129. $this->assertFalse(\OCP\Util::needUpgrade());
  130. $config->setSystemValue('version', '7.0.0.0');
  131. \OC::$server->getSession()->set('OC_Version', [7, 0, 0, 1]);
  132. self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);
  133. $this->assertTrue(\OCP\Util::needUpgrade());
  134. $config->setSystemValue('version', $oldConfigVersion);
  135. \OC::$server->getSession()->set('OC_Version', $oldSessionVersion);
  136. self::invokePrivate(new \OCP\Util, 'needUpgradeCache', [null]);
  137. $this->assertFalse(\OCP\Util::needUpgrade());
  138. }
  139. public function testCheckDataDirectoryValidity() {
  140. $dataDir = \OC::$server->getTempManager()->getTemporaryFolder();
  141. touch($dataDir . '/.ncdata');
  142. $errors = \OC_Util::checkDataDirectoryValidity($dataDir);
  143. $this->assertEmpty($errors);
  144. \OCP\Files::rmdirr($dataDir);
  145. $dataDir = \OC::$server->getTempManager()->getTemporaryFolder();
  146. // no touch
  147. $errors = \OC_Util::checkDataDirectoryValidity($dataDir);
  148. $this->assertNotEmpty($errors);
  149. \OCP\Files::rmdirr($dataDir);
  150. $errors = \OC_Util::checkDataDirectoryValidity('relative/path');
  151. $this->assertNotEmpty($errors);
  152. }
  153. protected function setUp(): void {
  154. parent::setUp();
  155. \OC_Util::$scripts = [];
  156. \OC_Util::$styles = [];
  157. self::invokePrivate(\OCP\Util::class, 'scripts', [[]]);
  158. self::invokePrivate(\OCP\Util::class, 'scriptDeps', [[]]);
  159. }
  160. protected function tearDown(): void {
  161. parent::tearDown();
  162. \OC_Util::$scripts = [];
  163. \OC_Util::$styles = [];
  164. self::invokePrivate(\OCP\Util::class, 'scripts', [[]]);
  165. self::invokePrivate(\OCP\Util::class, 'scriptDeps', [[]]);
  166. }
  167. public function testAddScript() {
  168. \OCP\Util::addScript('first', 'myFirstJSFile');
  169. \OCP\Util::addScript('core', 'myFancyJSFile1');
  170. \OCP\Util::addScript('files', 'myFancyJSFile2', 'core');
  171. \OCP\Util::addScript('myApp5', 'myApp5JSFile', 'myApp2');
  172. \OCP\Util::addScript('myApp', 'myFancyJSFile3');
  173. \OCP\Util::addScript('core', 'myFancyJSFile4');
  174. // after itself
  175. \OCP\Util::addScript('core', 'myFancyJSFile5', 'core');
  176. // add duplicate
  177. \OCP\Util::addScript('core', 'myFancyJSFile1');
  178. // dependency chain
  179. \OCP\Util::addScript('myApp4', 'myApp4JSFile', 'myApp3');
  180. \OCP\Util::addScript('myApp3', 'myApp3JSFile', 'myApp2');
  181. \OCP\Util::addScript('myApp2', 'myApp2JSFile', 'myApp');
  182. \OCP\Util::addScript('core', 'common');
  183. \OCP\Util::addScript('core', 'main');
  184. $scripts = \OCP\Util::getScripts();
  185. // Core should appear first
  186. $this->assertEquals(
  187. 0,
  188. array_search('core/js/common', $scripts, true)
  189. );
  190. $this->assertEquals(
  191. 1,
  192. array_search('core/js/main', $scripts, true)
  193. );
  194. $this->assertEquals(
  195. 2,
  196. array_search('core/js/myFancyJSFile1', $scripts, true)
  197. );
  198. $this->assertEquals(
  199. 3,
  200. array_search('core/js/myFancyJSFile4', $scripts, true)
  201. );
  202. // Dependencies should appear before their children
  203. $this->assertLessThan(
  204. array_search('files/js/myFancyJSFile2', $scripts, true),
  205. array_search('core/js/myFancyJSFile3', $scripts, true)
  206. );
  207. $this->assertLessThan(
  208. array_search('myApp2/js/myApp2JSFile', $scripts, true),
  209. array_search('myApp/js/myFancyJSFile3', $scripts, true)
  210. );
  211. $this->assertLessThan(
  212. array_search('myApp3/js/myApp3JSFile', $scripts, true),
  213. array_search('myApp2/js/myApp2JSFile', $scripts, true)
  214. );
  215. $this->assertLessThan(
  216. array_search('myApp4/js/myApp4JSFile', $scripts, true),
  217. array_search('myApp3/js/myApp3JSFile', $scripts, true)
  218. );
  219. $this->assertLessThan(
  220. array_search('myApp5/js/myApp5JSFile', $scripts, true),
  221. array_search('myApp2/js/myApp2JSFile', $scripts, true)
  222. );
  223. // No duplicates
  224. $this->assertEquals(
  225. $scripts,
  226. array_unique($scripts)
  227. );
  228. // All scripts still there
  229. $scripts = [
  230. 'core/js/common',
  231. 'core/js/main',
  232. 'core/js/myFancyJSFile1',
  233. 'core/js/myFancyJSFile4',
  234. 'core/js/myFancyJSFile5',
  235. 'first/l10n/en',
  236. 'first/js/myFirstJSFile',
  237. 'files/l10n/en',
  238. 'files/js/myFancyJSFile2',
  239. 'myApp/l10n/en',
  240. 'myApp/js/myFancyJSFile3',
  241. 'myApp2/l10n/en',
  242. 'myApp2/js/myApp2JSFile',
  243. 'myApp5/l10n/en',
  244. 'myApp5/js/myApp5JSFile',
  245. 'myApp3/l10n/en',
  246. 'myApp3/js/myApp3JSFile',
  247. 'myApp4/l10n/en',
  248. 'myApp4/js/myApp4JSFile',
  249. ];
  250. foreach ($scripts as $script) {
  251. $this->assertContains($script, $scripts);
  252. }
  253. }
  254. public function testAddScriptCircularDependency() {
  255. \OCP\Util::addScript('circular', 'file1', 'dependency');
  256. \OCP\Util::addScript('dependency', 'file2', 'circular');
  257. $scripts = \OCP\Util::getScripts();
  258. $this->assertContains('circular/js/file1', $scripts);
  259. $this->assertContains('dependency/js/file2', $scripts);
  260. }
  261. public function testAddVendorScript() {
  262. \OC_Util::addVendorScript('core', 'myFancyJSFile1');
  263. \OC_Util::addVendorScript('myApp', 'myFancyJSFile2');
  264. \OC_Util::addVendorScript('core', 'myFancyJSFile0', true);
  265. \OC_Util::addVendorScript('core', 'myFancyJSFile10', true);
  266. // add duplicate
  267. \OC_Util::addVendorScript('core', 'myFancyJSFile1');
  268. $this->assertEquals([
  269. 'core/vendor/myFancyJSFile10',
  270. 'core/vendor/myFancyJSFile0',
  271. 'core/vendor/myFancyJSFile1',
  272. 'myApp/vendor/myFancyJSFile2',
  273. ], \OC_Util::$scripts);
  274. $this->assertEquals([], \OC_Util::$styles);
  275. }
  276. public function testAddTranslations() {
  277. \OC_Util::addTranslations('appId', 'de');
  278. $this->assertEquals([
  279. 'appId/l10n/de'
  280. ], \OC_Util::$scripts);
  281. $this->assertEquals([], \OC_Util::$styles);
  282. }
  283. public function testAddStyle() {
  284. \OC_Util::addStyle('core', 'myFancyCSSFile1');
  285. \OC_Util::addStyle('myApp', 'myFancyCSSFile2');
  286. \OC_Util::addStyle('core', 'myFancyCSSFile0', true);
  287. \OC_Util::addStyle('core', 'myFancyCSSFile10', true);
  288. // add duplicate
  289. \OC_Util::addStyle('core', 'myFancyCSSFile1');
  290. $this->assertEquals([], \OC_Util::$scripts);
  291. $this->assertEquals([
  292. 'core/css/myFancyCSSFile10',
  293. 'core/css/myFancyCSSFile0',
  294. 'core/css/myFancyCSSFile1',
  295. 'myApp/css/myFancyCSSFile2',
  296. ], \OC_Util::$styles);
  297. }
  298. public function testAddVendorStyle() {
  299. \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
  300. \OC_Util::addVendorStyle('myApp', 'myFancyCSSFile2');
  301. \OC_Util::addVendorStyle('core', 'myFancyCSSFile0', true);
  302. \OC_Util::addVendorStyle('core', 'myFancyCSSFile10', true);
  303. // add duplicate
  304. \OC_Util::addVendorStyle('core', 'myFancyCSSFile1');
  305. $this->assertEquals([], \OC_Util::$scripts);
  306. $this->assertEquals([
  307. 'core/vendor/myFancyCSSFile10',
  308. 'core/vendor/myFancyCSSFile0',
  309. 'core/vendor/myFancyCSSFile1',
  310. 'myApp/vendor/myFancyCSSFile2',
  311. ], \OC_Util::$styles);
  312. }
  313. public function testShortenMultibyteString() {
  314. $this->assertEquals('Short nuff', \OCP\Util::shortenMultibyteString('Short nuff', 255));
  315. $this->assertEquals('ABC', \OCP\Util::shortenMultibyteString('ABCDEF', 3));
  316. // each of the characters is 12 bytes
  317. $this->assertEquals('🙈', \OCP\Util::shortenMultibyteString('🙈🙊🙉', 16, 2));
  318. }
  319. }