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.

util.php 12KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410
  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. class Test_Util extends \Test\TestCase {
  9. public function testGetVersion() {
  10. $version = \OC_Util::getVersion();
  11. $this->assertTrue(is_array($version));
  12. foreach ($version as $num) {
  13. $this->assertTrue(is_int($num));
  14. }
  15. }
  16. public function testGetVersionString() {
  17. $version = \OC_Util::getVersionString();
  18. $this->assertTrue(is_string($version));
  19. }
  20. public function testGetEditionString() {
  21. $edition = \OC_Util::getEditionString();
  22. $this->assertTrue(is_string($edition));
  23. }
  24. function testFormatDate() {
  25. date_default_timezone_set("UTC");
  26. $result = OC_Util::formatDate(1350129205);
  27. $expected = 'October 13, 2012 at 11:53:25 AM GMT+0';
  28. $this->assertEquals($expected, $result);
  29. $result = OC_Util::formatDate(1102831200, true);
  30. $expected = 'December 12, 2004';
  31. $this->assertEquals($expected, $result);
  32. }
  33. function testFormatDateWithTZ() {
  34. date_default_timezone_set("UTC");
  35. $result = OC_Util::formatDate(1350129205, false, 'Europe/Berlin');
  36. $expected = 'October 13, 2012 at 1:53:25 PM GMT+0';
  37. $this->assertEquals($expected, $result);
  38. }
  39. /**
  40. * @expectedException Exception
  41. */
  42. function testFormatDateWithInvalidTZ() {
  43. OC_Util::formatDate(1350129205, false, 'Mordor/Barad-dûr');
  44. }
  45. function testFormatDateWithTZFromSession() {
  46. date_default_timezone_set("UTC");
  47. \OC::$server->getSession()->set('timezone', 3);
  48. $result = OC_Util::formatDate(1350129205, false);
  49. $expected = 'October 13, 2012 at 2:53:25 PM GMT+0';
  50. $this->assertEquals($expected, $result);
  51. }
  52. function testCallRegister() {
  53. $result = strlen(OC_Util::callRegister());
  54. $this->assertEquals(30, $result);
  55. }
  56. function testSanitizeHTML() {
  57. $badArray = array(
  58. 'While it is unusual to pass an array',
  59. 'this function actually <blink>supports</blink> it.',
  60. 'And therefore there needs to be a <script>alert("Unit"+\'test\')</script> for it!'
  61. );
  62. $goodArray = array(
  63. 'While it is unusual to pass an array',
  64. 'this function actually &lt;blink&gt;supports&lt;/blink&gt; it.',
  65. 'And therefore there needs to be a &lt;script&gt;alert(&quot;Unit&quot;+&#039;test&#039;)&lt;/script&gt; for it!'
  66. );
  67. $result = OC_Util::sanitizeHTML($badArray);
  68. $this->assertEquals($goodArray, $result);
  69. $badString = '<img onload="alert(1)" />';
  70. $result = OC_Util::sanitizeHTML($badString);
  71. $this->assertEquals('&lt;img onload=&quot;alert(1)&quot; /&gt;', $result);
  72. $badString = "<script>alert('Hacked!');</script>";
  73. $result = OC_Util::sanitizeHTML($badString);
  74. $this->assertEquals('&lt;script&gt;alert(&#039;Hacked!&#039;);&lt;/script&gt;', $result);
  75. $goodString = 'This is a good string without HTML.';
  76. $result = OC_Util::sanitizeHTML($goodString);
  77. $this->assertEquals('This is a good string without HTML.', $result);
  78. }
  79. function testEncodePath(){
  80. $component = '/§#@test%&^ä/-child';
  81. $result = OC_Util::encodePath($component);
  82. $this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result);
  83. }
  84. public function testFileInfoLoaded() {
  85. $expected = function_exists('finfo_open');
  86. $this->assertEquals($expected, \OC_Util::fileInfoLoaded());
  87. }
  88. public function testIsInternetConnectionEnabled() {
  89. \OC_Config::setValue("has_internet_connection", false);
  90. $this->assertFalse(\OC_Util::isInternetConnectionEnabled());
  91. \OC_Config::setValue("has_internet_connection", true);
  92. $this->assertTrue(\OC_Util::isInternetConnectionEnabled());
  93. }
  94. function testGenerateRandomBytes() {
  95. $result = strlen(OC_Util::generateRandomBytes(59));
  96. $this->assertEquals(59, $result);
  97. }
  98. function testGetDefaultEmailAddress() {
  99. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  100. $this->assertEquals('no-reply@localhost', $email);
  101. }
  102. function testGetDefaultEmailAddressFromConfig() {
  103. OC_Config::setValue('mail_domain', 'example.com');
  104. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  105. $this->assertEquals('no-reply@example.com', $email);
  106. OC_Config::deleteKey('mail_domain');
  107. }
  108. function testGetConfiguredEmailAddressFromConfig() {
  109. OC_Config::setValue('mail_domain', 'example.com');
  110. OC_Config::setValue('mail_from_address', 'owncloud');
  111. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  112. $this->assertEquals('owncloud@example.com', $email);
  113. OC_Config::deleteKey('mail_domain');
  114. OC_Config::deleteKey('mail_from_address');
  115. }
  116. function testGetInstanceIdGeneratesValidId() {
  117. OC_Config::deleteKey('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. * Tests that the home storage is not wrapped when no quota exists.
  125. */
  126. function testHomeStorageWrapperWithoutQuota() {
  127. $user1 = $this->getUniqueID();
  128. \OC_User::createUser($user1, 'test');
  129. \OC::$server->getConfig()->setUserValue($user1, 'files', 'quota', 'none');
  130. \OC_User::setUserId($user1);
  131. \OC_Util::setupFS($user1);
  132. $userMount = \OC\Files\Filesystem::getMountManager()->find('/' . $user1 . '/');
  133. $this->assertNotNull($userMount);
  134. $this->assertNotInstanceOf('\OC\Files\Storage\Wrapper\Quota', $userMount->getStorage());
  135. // clean up
  136. \OC_User::setUserId('');
  137. \OC_User::deleteUser($user1);
  138. \OC::$server->getConfig()->deleteAllUserValues($user1);
  139. \OC_Util::tearDownFS();
  140. }
  141. /**
  142. * Tests that the home storage is not wrapped when no quota exists.
  143. */
  144. function testHomeStorageWrapperWithQuota() {
  145. $user1 = $this->getUniqueID();
  146. \OC_User::createUser($user1, 'test');
  147. \OC::$server->getConfig()->setUserValue($user1, 'files', 'quota', '1024');
  148. \OC_User::setUserId($user1);
  149. \OC_Util::setupFS($user1);
  150. $userMount = \OC\Files\Filesystem::getMountManager()->find('/' . $user1 . '/');
  151. $this->assertNotNull($userMount);
  152. $this->assertTrue($userMount->getStorage()->instanceOfStorage('\OC\Files\Storage\Wrapper\Quota'));
  153. // ensure that root wasn't wrapped
  154. $rootMount = \OC\Files\Filesystem::getMountManager()->find('/');
  155. $this->assertNotNull($rootMount);
  156. $this->assertNotInstanceOf('\OC\Files\Storage\Wrapper\Quota', $rootMount->getStorage());
  157. // clean up
  158. \OC_User::setUserId('');
  159. \OC_User::deleteUser($user1);
  160. \OC::$server->getConfig()->deleteAllUserValues($user1);
  161. \OC_Util::tearDownFS();
  162. }
  163. /**
  164. * @dataProvider baseNameProvider
  165. */
  166. public function testBaseName($expected, $file)
  167. {
  168. $base = \OC_Util::basename($file);
  169. $this->assertEquals($expected, $base);
  170. }
  171. public function baseNameProvider()
  172. {
  173. return array(
  174. array('public_html', '/home/user/public_html/'),
  175. array('public_html', '/home/user/public_html'),
  176. array('', '/'),
  177. array('public_html', 'public_html'),
  178. array('442aa682de2a64db1e010f50e60fd9c9', 'local::C:\Users\ADMINI~1\AppData\Local\Temp\2/442aa682de2a64db1e010f50e60fd9c9/')
  179. );
  180. }
  181. /**
  182. * @dataProvider filenameValidationProvider
  183. */
  184. public function testFilenameValidation($file, $valid) {
  185. // private API
  186. $this->assertEquals($valid, \OC_Util::isValidFileName($file));
  187. // public API
  188. $this->assertEquals($valid, \OCP\Util::isValidFileName($file));
  189. }
  190. public function filenameValidationProvider() {
  191. return array(
  192. // valid names
  193. array('boringname', true),
  194. array('something.with.extension', true),
  195. array('now with spaces', true),
  196. array('.a', true),
  197. array('..a', true),
  198. array('.dotfile', true),
  199. array('single\'quote', true),
  200. array(' spaces before', true),
  201. array('spaces after ', true),
  202. array('allowed chars including the crazy ones $%&_-^@!,()[]{}=;#', true),
  203. array('汉字也能用', true),
  204. array('und Ümläüte sind auch willkommen', true),
  205. // disallowed names
  206. array('', false),
  207. array(' ', false),
  208. array('.', false),
  209. array('..', false),
  210. array('back\\slash', false),
  211. array('sl/ash', false),
  212. array('lt<lt', false),
  213. array('gt>gt', false),
  214. array('col:on', false),
  215. array('double"quote', false),
  216. array('pi|pe', false),
  217. array('dont?ask?questions?', false),
  218. array('super*star', false),
  219. array('new\nline', false),
  220. // better disallow these to avoid unexpected trimming to have side effects
  221. array(' ..', false),
  222. array('.. ', false),
  223. array('. ', false),
  224. array(' .', false),
  225. );
  226. }
  227. /**
  228. * @dataProvider dataProviderForTestIsSharingDisabledForUser
  229. * @param array $groups existing groups
  230. * @param array $membership groups the user belong to
  231. * @param array $excludedGroups groups which should be excluded from sharing
  232. * @param bool $expected expected result
  233. */
  234. function testIsSharingDisabledForUser($groups, $membership, $excludedGroups, $expected) {
  235. $uid = "user1";
  236. \OC_User::setUserId($uid);
  237. \OC_User::createUser($uid, "passwd");
  238. foreach($groups as $group) {
  239. \OC_Group::createGroup($group);
  240. }
  241. foreach($membership as $group) {
  242. \OC_Group::addToGroup($uid, $group);
  243. }
  244. $appConfig = \OC::$server->getAppConfig();
  245. $appConfig->setValue('core', 'shareapi_exclude_groups_list', implode(',', $excludedGroups));
  246. $appConfig->setValue('core', 'shareapi_exclude_groups', 'yes');
  247. $result = \OCP\Util::isSharingDisabledForUser();
  248. $this->assertSame($expected, $result);
  249. // cleanup
  250. \OC_User::deleteUser($uid);
  251. \OC_User::setUserId('');
  252. foreach($groups as $group) {
  253. \OC_Group::deleteGroup($group);
  254. }
  255. $appConfig->setValue('core', 'shareapi_exclude_groups_list', '');
  256. $appConfig->setValue('core', 'shareapi_exclude_groups', 'no');
  257. }
  258. public function dataProviderForTestIsSharingDisabledForUser() {
  259. return array(
  260. // existing groups, groups the user belong to, groups excluded from sharing, expected result
  261. array(array('g1', 'g2', 'g3'), array(), array('g1'), false),
  262. array(array('g1', 'g2', 'g3'), array(), array(), false),
  263. array(array('g1', 'g2', 'g3'), array('g2'), array('g1'), false),
  264. array(array('g1', 'g2', 'g3'), array('g2'), array(), false),
  265. array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1'), false),
  266. array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1', 'g2'), true),
  267. array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1', 'g2', 'g3'), true),
  268. );
  269. }
  270. /**
  271. * Test default apps
  272. *
  273. * @dataProvider defaultAppsProvider
  274. */
  275. function testDefaultApps($defaultAppConfig, $expectedPath, $enabledApps) {
  276. $oldDefaultApps = \OCP\Config::getSystemValue('core', 'defaultapp', '');
  277. // CLI is doing messy stuff with the webroot, so need to work it around
  278. $oldWebRoot = \OC::$WEBROOT;
  279. \OC::$WEBROOT = '';
  280. Dummy_OC_App::setEnabledApps($enabledApps);
  281. // need to set a user id to make sure enabled apps are read from cache
  282. \OC_User::setUserId($this->getUniqueID());
  283. \OCP\Config::setSystemValue('defaultapp', $defaultAppConfig);
  284. $this->assertEquals('http://localhost/' . $expectedPath, \OC_Util::getDefaultPageUrl());
  285. // restore old state
  286. \OC::$WEBROOT = $oldWebRoot;
  287. Dummy_OC_App::restore();
  288. \OCP\Config::setSystemValue('defaultapp', $oldDefaultApps);
  289. \OC_User::setUserId(null);
  290. }
  291. function defaultAppsProvider() {
  292. return array(
  293. // none specified, default to files
  294. array(
  295. '',
  296. 'index.php/apps/files/',
  297. array('files'),
  298. ),
  299. // unexisting or inaccessible app specified, default to files
  300. array(
  301. 'unexist',
  302. 'index.php/apps/files/',
  303. array('files'),
  304. ),
  305. // non-standard app
  306. array(
  307. 'calendar',
  308. 'index.php/apps/calendar/',
  309. array('files', 'calendar'),
  310. ),
  311. // non-standard app with fallback
  312. array(
  313. 'contacts,calendar',
  314. 'index.php/apps/calendar/',
  315. array('files', 'calendar'),
  316. ),
  317. );
  318. }
  319. /**
  320. * Test needUpgrade() when the core version is increased
  321. */
  322. public function testNeedUpgradeCore() {
  323. $oldConfigVersion = OC_Config::getValue('version', '0.0.0');
  324. $oldSessionVersion = \OC::$server->getSession()->get('OC_Version');
  325. $this->assertFalse(\OCP\Util::needUpgrade());
  326. OC_Config::setValue('version', '7.0.0.0');
  327. \OC::$server->getSession()->set('OC_Version', array(7, 0, 0, 1));
  328. $this->assertTrue(\OCP\Util::needUpgrade());
  329. OC_Config::setValue('version', $oldConfigVersion);
  330. $oldSessionVersion = \OC::$server->getSession()->set('OC_Version', $oldSessionVersion);
  331. $this->assertFalse(\OCP\Util::needUpgrade());
  332. }
  333. }
  334. /**
  335. * Dummy OC Apps class to make it possible to override
  336. * enabled apps
  337. */
  338. class Dummy_OC_App extends OC_App {
  339. private static $enabledAppsCacheBackup;
  340. public static function setEnabledApps($enabledApps) {
  341. self::$enabledAppsCacheBackup = self::$enabledAppsCache;
  342. self::$enabledAppsCache = $enabledApps;
  343. }
  344. public static function restore() {
  345. self::$enabledAppsCache = self::$enabledAppsCacheBackup;
  346. }
  347. }