util.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 PHPUnit_Framework_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 11:53';
  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 testCallRegister() {
  34. $result = strlen(OC_Util::callRegister());
  35. $this->assertEquals(20, $result);
  36. }
  37. function testSanitizeHTML() {
  38. $badArray = array(
  39. 'While it is unusual to pass an array',
  40. 'this function actually <blink>supports</blink> it.',
  41. 'And therefore there needs to be a <script>alert("Unit"+\'test\')</script> for it!'
  42. );
  43. $goodArray = array(
  44. 'While it is unusual to pass an array',
  45. 'this function actually &lt;blink&gt;supports&lt;/blink&gt; it.',
  46. 'And therefore there needs to be a &lt;script&gt;alert(&quot;Unit&quot;+&#039;test&#039;)&lt;/script&gt; for it!'
  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. 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 testFileInfoLoaded() {
  66. $expected = function_exists('finfo_open');
  67. $this->assertEquals($expected, \OC_Util::fileInfoLoaded());
  68. }
  69. public function testIsInternetConnectionEnabled() {
  70. \OC_Config::setValue("has_internet_connection", false);
  71. $this->assertFalse(\OC_Util::isInternetConnectionEnabled());
  72. \OC_Config::setValue("has_internet_connection", true);
  73. $this->assertTrue(\OC_Util::isInternetConnectionEnabled());
  74. }
  75. function testGenerateRandomBytes() {
  76. $result = strlen(OC_Util::generateRandomBytes(59));
  77. $this->assertEquals(59, $result);
  78. }
  79. function testGetDefaultEmailAddress() {
  80. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  81. $this->assertEquals('no-reply@localhost', $email);
  82. }
  83. function testGetDefaultEmailAddressFromConfig() {
  84. OC_Config::setValue('mail_domain', 'example.com');
  85. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  86. $this->assertEquals('no-reply@example.com', $email);
  87. OC_Config::deleteKey('mail_domain');
  88. }
  89. function testGetConfiguredEmailAddressFromConfig() {
  90. OC_Config::setValue('mail_domain', 'example.com');
  91. OC_Config::setValue('mail_from_address', 'owncloud');
  92. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  93. $this->assertEquals('owncloud@example.com', $email);
  94. OC_Config::deleteKey('mail_domain');
  95. OC_Config::deleteKey('mail_from_address');
  96. }
  97. function testGetInstanceIdGeneratesValidId() {
  98. OC_Config::deleteKey('instanceid');
  99. $this->assertStringStartsWith('oc', OC_Util::getInstanceId());
  100. }
  101. /**
  102. * Tests that the home storage is not wrapped when no quota exists.
  103. */
  104. function testHomeStorageWrapperWithoutQuota() {
  105. $user1 = uniqid();
  106. \OC_User::createUser($user1, 'test');
  107. OC_Preferences::setValue($user1, 'files', 'quota', 'none');
  108. \OC_User::setUserId($user1);
  109. \OC_Util::setupFS($user1);
  110. $userMount = \OC\Files\Filesystem::getMountManager()->find('/' . $user1 . '/');
  111. $this->assertNotNull($userMount);
  112. $this->assertNotInstanceOf('\OC\Files\Storage\Wrapper\Quota', $userMount->getStorage());
  113. // clean up
  114. \OC_User::setUserId('');
  115. \OC_User::deleteUser($user1);
  116. OC_Preferences::deleteUser($user1);
  117. \OC_Util::tearDownFS();
  118. }
  119. /**
  120. * Tests that the home storage is not wrapped when no quota exists.
  121. */
  122. function testHomeStorageWrapperWithQuota() {
  123. $user1 = uniqid();
  124. \OC_User::createUser($user1, 'test');
  125. OC_Preferences::setValue($user1, 'files', 'quota', '1024');
  126. \OC_User::setUserId($user1);
  127. \OC_Util::setupFS($user1);
  128. $userMount = \OC\Files\Filesystem::getMountManager()->find('/' . $user1 . '/');
  129. $this->assertNotNull($userMount);
  130. $this->assertInstanceOf('\OC\Files\Storage\Wrapper\Quota', $userMount->getStorage());
  131. // ensure that root wasn't wrapped
  132. $rootMount = \OC\Files\Filesystem::getMountManager()->find('/');
  133. $this->assertNotNull($rootMount);
  134. $this->assertNotInstanceOf('\OC\Files\Storage\Wrapper\Quota', $rootMount->getStorage());
  135. // clean up
  136. \OC_User::setUserId('');
  137. \OC_User::deleteUser($user1);
  138. OC_Preferences::deleteUser($user1);
  139. \OC_Util::tearDownFS();
  140. }
  141. /**
  142. * @dataProvider baseNameProvider
  143. */
  144. public function testBaseName($expected, $file)
  145. {
  146. $base = \OC_Util::basename($file);
  147. $this->assertEquals($expected, $base);
  148. }
  149. public function baseNameProvider()
  150. {
  151. return array(
  152. array('public_html', '/home/user/public_html/'),
  153. array('public_html', '/home/user/public_html'),
  154. array('', '/'),
  155. array('public_html', 'public_html'),
  156. array('442aa682de2a64db1e010f50e60fd9c9', 'local::C:\Users\ADMINI~1\AppData\Local\Temp\2/442aa682de2a64db1e010f50e60fd9c9/')
  157. );
  158. }
  159. /**
  160. * @dataProvider filenameValidationProvider
  161. */
  162. public function testFilenameValidation($file, $valid) {
  163. // private API
  164. $this->assertEquals($valid, \OC_Util::isValidFileName($file));
  165. // public API
  166. $this->assertEquals($valid, \OCP\Util::isValidFileName($file));
  167. }
  168. public function filenameValidationProvider() {
  169. return array(
  170. // valid names
  171. array('boringname', true),
  172. array('something.with.extension', true),
  173. array('now with spaces', true),
  174. array('.a', true),
  175. array('..a', true),
  176. array('.dotfile', true),
  177. array('single\'quote', true),
  178. array(' spaces before', true),
  179. array('spaces after ', true),
  180. array('allowed chars including the crazy ones $%&_-^@!,()[]{}=;#', true),
  181. array('汉字也能用', true),
  182. array('und Ümläüte sind auch willkommen', true),
  183. // disallowed names
  184. array('', false),
  185. array(' ', false),
  186. array('.', false),
  187. array('..', false),
  188. array('back\\slash', false),
  189. array('sl/ash', false),
  190. array('lt<lt', false),
  191. array('gt>gt', false),
  192. array('col:on', false),
  193. array('double"quote', false),
  194. array('pi|pe', false),
  195. array('dont?ask?questions?', false),
  196. array('super*star', false),
  197. array('new\nline', false),
  198. // better disallow these to avoid unexpected trimming to have side effects
  199. array(' ..', false),
  200. array('.. ', false),
  201. array('. ', false),
  202. array(' .', false),
  203. );
  204. }
  205. /**
  206. * @dataProvider dataProviderForTestIsSharingDisabledForUser
  207. * @param array $groups existing groups
  208. * @param array $membership groups the user belong to
  209. * @param array $excludedGroups groups which should be excluded from sharing
  210. * @param bool $expected expected result
  211. */
  212. function testIsSharingDisabledForUser($groups, $membership, $excludedGroups, $expected) {
  213. $uid = "user1";
  214. \OC_User::setUserId($uid);
  215. \OC_User::createUser($uid, "passwd");
  216. foreach($groups as $group) {
  217. \OC_Group::createGroup($group);
  218. }
  219. foreach($membership as $group) {
  220. \OC_Group::addToGroup($uid, $group);
  221. }
  222. $appConfig = \OC::$server->getAppConfig();
  223. $appConfig->setValue('core', 'shareapi_exclude_groups_list', implode(',', $excludedGroups));
  224. $appConfig->setValue('core', 'shareapi_exclude_groups', 'yes');
  225. $result = \OCP\Util::isSharingDisabledForUser();
  226. $this->assertSame($expected, $result);
  227. // cleanup
  228. \OC_User::deleteUser($uid);
  229. \OC_User::setUserId('');
  230. foreach($groups as $group) {
  231. \OC_Group::deleteGroup($group);
  232. }
  233. $appConfig->setValue('core', 'shareapi_exclude_groups_list', '');
  234. $appConfig->setValue('core', 'shareapi_exclude_groups', 'no');
  235. }
  236. public function dataProviderForTestIsSharingDisabledForUser() {
  237. return array(
  238. // existing groups, groups the user belong to, groups excluded from sharing, expected result
  239. array(array('g1', 'g2', 'g3'), array(), array('g1'), false),
  240. array(array('g1', 'g2', 'g3'), array(), array(), false),
  241. array(array('g1', 'g2', 'g3'), array('g2'), array('g1'), false),
  242. array(array('g1', 'g2', 'g3'), array('g2'), array(), false),
  243. array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1'), false),
  244. array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1', 'g2'), true),
  245. array(array('g1', 'g2', 'g3'), array('g1', 'g2'), array('g1', 'g2', 'g3'), true),
  246. );
  247. }
  248. }