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.

221 lines
6.6 KiB

14 years ago
13 years ago
  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. $badString = "<script>alert('Hacked!');</script>";
  39. $result = OC_Util::sanitizeHTML($badString);
  40. $this->assertEquals("&lt;script&gt;alert(&#039;Hacked!&#039;);&lt;/script&gt;", $result);
  41. $goodString = "This is an harmless string.";
  42. $result = OC_Util::sanitizeHTML($goodString);
  43. $this->assertEquals("This is an harmless string.", $result);
  44. }
  45. function testEncodePath(){
  46. $component = '/§#@test%&^ä/-child';
  47. $result = OC_Util::encodePath($component);
  48. $this->assertEquals("/%C2%A7%23%40test%25%26%5E%C3%A4/-child", $result);
  49. }
  50. public function testFileInfoLoaded() {
  51. $expected = function_exists('finfo_open');
  52. $this->assertEquals($expected, \OC_Util::fileInfoLoaded());
  53. }
  54. public function testIsInternetConnectionEnabled() {
  55. \OC_Config::setValue("has_internet_connection", false);
  56. $this->assertFalse(\OC_Util::isInternetConnectionEnabled());
  57. \OC_Config::setValue("has_internet_connection", true);
  58. $this->assertTrue(\OC_Util::isInternetConnectionEnabled());
  59. }
  60. function testGenerateRandomBytes() {
  61. $result = strlen(OC_Util::generateRandomBytes(59));
  62. $this->assertEquals(59, $result);
  63. }
  64. function testGetDefaultEmailAddress() {
  65. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  66. $this->assertEquals('no-reply@localhost.localdomain', $email);
  67. }
  68. function testGetDefaultEmailAddressFromConfig() {
  69. OC_Config::setValue('mail_domain', 'example.com');
  70. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  71. $this->assertEquals('no-reply@example.com', $email);
  72. OC_Config::deleteKey('mail_domain');
  73. }
  74. function testGetConfiguredEmailAddressFromConfig() {
  75. OC_Config::setValue('mail_domain', 'example.com');
  76. OC_Config::setValue('mail_from_address', 'owncloud');
  77. $email = \OCP\Util::getDefaultEmailAddress("no-reply");
  78. $this->assertEquals('owncloud@example.com', $email);
  79. OC_Config::deleteKey('mail_domain');
  80. OC_Config::deleteKey('mail_from_address');
  81. }
  82. function testGetInstanceIdGeneratesValidId() {
  83. OC_Config::deleteKey('instanceid');
  84. $this->assertStringStartsWith('oc', OC_Util::getInstanceId());
  85. }
  86. /**
  87. * Tests that the home storage is not wrapped when no quota exists.
  88. */
  89. function testHomeStorageWrapperWithoutQuota() {
  90. $user1 = uniqid();
  91. \OC_User::createUser($user1, 'test');
  92. OC_Preferences::setValue($user1, 'files', 'quota', 'none');
  93. \OC_User::setUserId($user1);
  94. \OC_Util::setupFS($user1);
  95. $userMount = \OC\Files\Filesystem::getMountManager()->find('/' . $user1 . '/');
  96. $this->assertNotNull($userMount);
  97. $this->assertNotInstanceOf('\OC\Files\Storage\Wrapper\Quota', $userMount->getStorage());
  98. // clean up
  99. \OC_User::setUserId('');
  100. \OC_User::deleteUser($user1);
  101. OC_Preferences::deleteUser($user1);
  102. \OC_Util::tearDownFS();
  103. }
  104. /**
  105. * Tests that the home storage is not wrapped when no quota exists.
  106. */
  107. function testHomeStorageWrapperWithQuota() {
  108. $user1 = uniqid();
  109. \OC_User::createUser($user1, 'test');
  110. OC_Preferences::setValue($user1, 'files', 'quota', '1024');
  111. \OC_User::setUserId($user1);
  112. \OC_Util::setupFS($user1);
  113. $userMount = \OC\Files\Filesystem::getMountManager()->find('/' . $user1 . '/');
  114. $this->assertNotNull($userMount);
  115. $this->assertInstanceOf('\OC\Files\Storage\Wrapper\Quota', $userMount->getStorage());
  116. // ensure that root wasn't wrapped
  117. $rootMount = \OC\Files\Filesystem::getMountManager()->find('/');
  118. $this->assertNotNull($rootMount);
  119. $this->assertNotInstanceOf('\OC\Files\Storage\Wrapper\Quota', $rootMount->getStorage());
  120. // clean up
  121. \OC_User::setUserId('');
  122. \OC_User::deleteUser($user1);
  123. OC_Preferences::deleteUser($user1);
  124. \OC_Util::tearDownFS();
  125. }
  126. /**
  127. * @dataProvider baseNameProvider
  128. */
  129. public function testBaseName($expected, $file)
  130. {
  131. $base = \OC_Util::basename($file);
  132. $this->assertEquals($expected, $base);
  133. }
  134. public function baseNameProvider()
  135. {
  136. return array(
  137. array('public_html', '/home/user/public_html/'),
  138. array('public_html', '/home/user/public_html'),
  139. array('', '/'),
  140. array('public_html', 'public_html'),
  141. array('442aa682de2a64db1e010f50e60fd9c9', 'local::C:\Users\ADMINI~1\AppData\Local\Temp\2/442aa682de2a64db1e010f50e60fd9c9/')
  142. );
  143. }
  144. /**
  145. * @dataProvider filenameValidationProvider
  146. */
  147. public function testFilenameValidation($file, $valid) {
  148. // private API
  149. $this->assertEquals($valid, \OC_Util::isValidFileName($file));
  150. // public API
  151. $this->assertEquals($valid, \OCP\Util::isValidFileName($file));
  152. }
  153. public function filenameValidationProvider() {
  154. return array(
  155. // valid names
  156. array('boringname', true),
  157. array('something.with.extension', true),
  158. array('now with spaces', true),
  159. array('.a', true),
  160. array('..a', true),
  161. array('.dotfile', true),
  162. array('single\'quote', true),
  163. array(' spaces before', true),
  164. array('spaces after ', true),
  165. array('allowed chars including the crazy ones $%&_-^@!,()[]{}=;#', true),
  166. array('汉字也能用', true),
  167. array('und Ümläüte sind auch willkommen', true),
  168. // disallowed names
  169. array('', false),
  170. array(' ', false),
  171. array('.', false),
  172. array('..', false),
  173. array('back\\slash', false),
  174. array('sl/ash', false),
  175. array('lt<lt', false),
  176. array('gt>gt', false),
  177. array('col:on', false),
  178. array('double"quote', false),
  179. array('pi|pe', false),
  180. array('dont?ask?questions?', false),
  181. array('super*star', false),
  182. array('new\nline', false),
  183. // better disallow these to avoid unexpected trimming to have side effects
  184. array(' ..', false),
  185. array('.. ', false),
  186. array('. ', false),
  187. array(' .', false),
  188. );
  189. }
  190. }