util.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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 testGetInstanceIdGeneratesValidId() {
  75. OC_Config::deleteKey('instanceid');
  76. $this->assertStringStartsWith('oc', OC_Util::getInstanceId());
  77. }
  78. /**
  79. * Tests that the home storage is not wrapped when no quota exists.
  80. */
  81. function testHomeStorageWrapperWithoutQuota() {
  82. $user1 = uniqid();
  83. \OC_User::createUser($user1, 'test');
  84. OC_Preferences::setValue($user1, 'files', 'quota', 'none');
  85. \OC_User::setUserId($user1);
  86. \OC_Util::setupFS($user1);
  87. $userMount = \OC\Files\Filesystem::getMountManager()->find('/' . $user1 . '/');
  88. $this->assertNotNull($userMount);
  89. $this->assertNotInstanceOf('\OC\Files\Storage\Wrapper\Quota', $userMount->getStorage());
  90. // clean up
  91. \OC_User::setUserId('');
  92. \OC_User::deleteUser($user1);
  93. OC_Preferences::deleteUser($user1);
  94. \OC_Util::tearDownFS();
  95. }
  96. /**
  97. * Tests that the home storage is not wrapped when no quota exists.
  98. */
  99. function testHomeStorageWrapperWithQuota() {
  100. $user1 = uniqid();
  101. \OC_User::createUser($user1, 'test');
  102. OC_Preferences::setValue($user1, 'files', 'quota', '1024');
  103. \OC_User::setUserId($user1);
  104. \OC_Util::setupFS($user1);
  105. $userMount = \OC\Files\Filesystem::getMountManager()->find('/' . $user1 . '/');
  106. $this->assertNotNull($userMount);
  107. $this->assertInstanceOf('\OC\Files\Storage\Wrapper\Quota', $userMount->getStorage());
  108. // ensure that root wasn't wrapped
  109. $rootMount = \OC\Files\Filesystem::getMountManager()->find('/');
  110. $this->assertNotNull($rootMount);
  111. $this->assertNotInstanceOf('\OC\Files\Storage\Wrapper\Quota', $rootMount->getStorage());
  112. // clean up
  113. \OC_User::setUserId('');
  114. \OC_User::deleteUser($user1);
  115. OC_Preferences::deleteUser($user1);
  116. \OC_Util::tearDownFS();
  117. }
  118. /**
  119. * @dataProvider baseNameProvider
  120. */
  121. public function testBaseName($expected, $file)
  122. {
  123. $base = \OC_Util::basename($file);
  124. $this->assertEquals($expected, $base);
  125. }
  126. public function baseNameProvider()
  127. {
  128. return array(
  129. array('public_html', '/home/user/public_html/'),
  130. array('public_html', '/home/user/public_html'),
  131. array('', '/'),
  132. array('public_html', 'public_html'),
  133. array('442aa682de2a64db1e010f50e60fd9c9', 'local::C:\Users\ADMINI~1\AppData\Local\Temp\2/442aa682de2a64db1e010f50e60fd9c9/')
  134. );
  135. }
  136. }