HelperTest.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * ownCloud
  4. *
  5. * @author Bjoern Schiessle
  6. * @copyright 2014 Bjoern Schiessle <schiessle@owncloud.com>
  7. *
  8. * This library is free software; you can redistribute it and/or
  9. * modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE
  10. * License as published by the Free Software Foundation; either
  11. * version 3 of the License, or any later version.
  12. *
  13. * This library is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU AFFERO GENERAL PUBLIC LICENSE for more details.
  17. *
  18. * You should have received a copy of the GNU Affero General Public
  19. * License along with this library. If not, see <http://www.gnu.org/licenses/>.
  20. */
  21. namespace Test\Share;
  22. /**
  23. * @group DB
  24. * Class Helper
  25. */
  26. class HelperTest extends \Test\TestCase {
  27. public function expireDateProvider() {
  28. return [
  29. // no default expire date, we take the users expire date
  30. [['defaultExpireDateSet' => false], 2000000000, 2000010000, 2000010000],
  31. // no default expire date and no user defined expire date, return false
  32. [['defaultExpireDateSet' => false], 2000000000, null, false],
  33. // unenforced expire data and no user defined expire date, return false (because the default is not enforced)
  34. [['defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => false], 2000000000, null, false],
  35. // enforced expire date and no user defined expire date, take default expire date
  36. [['defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => true], 2000000000, null, 2000086400],
  37. // unenforced expire date and user defined date > default expire date, take users expire date
  38. [['defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => false], 2000000000, 2000100000, 2000100000],
  39. // unenforced expire date and user expire date < default expire date, take users expire date
  40. [['defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => false], 2000000000, 2000010000, 2000010000],
  41. // enforced expire date and user expire date < default expire date, take users expire date
  42. [['defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => true], 2000000000, 2000010000, 2000010000],
  43. // enforced expire date and users expire date > default expire date, take default expire date
  44. [['defaultExpireDateSet' => true, 'expireAfterDays' => 1, 'enforceExpireDate' => true], 2000000000, 2000100000, 2000086400],
  45. ];
  46. }
  47. /**
  48. * @dataProvider expireDateProvider
  49. */
  50. public function testCalculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate, $expected) {
  51. $result = \OC\Share\Helper::calculateExpireDate($defaultExpireSettings, $creationTime, $userExpireDate);
  52. $this->assertSame($expected, $result);
  53. }
  54. public function dataTestSplitUserRemote() {
  55. $userPrefix = ['user@name', 'username'];
  56. $protocols = ['', 'http://', 'https://'];
  57. $remotes = [
  58. 'localhost',
  59. 'local.host',
  60. 'dev.local.host',
  61. 'dev.local.host/path',
  62. 'dev.local.host/at@inpath',
  63. '127.0.0.1',
  64. '::1',
  65. '::192.0.2.128',
  66. '::192.0.2.128/at@inpath',
  67. ];
  68. $testCases = [];
  69. foreach ($userPrefix as $user) {
  70. foreach ($remotes as $remote) {
  71. foreach ($protocols as $protocol) {
  72. $baseUrl = $user . '@' . $protocol . $remote;
  73. $testCases[] = [$baseUrl, $user, $protocol . $remote];
  74. $testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
  75. $testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
  76. $testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote];
  77. }
  78. }
  79. }
  80. return $testCases;
  81. }
  82. /**
  83. * @dataProvider dataTestSplitUserRemote
  84. *
  85. * @param string $remote
  86. * @param string $expectedUser
  87. * @param string $expectedUrl
  88. */
  89. public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) {
  90. list($remoteUser, $remoteUrl) = \OC\Share\Helper::splitUserRemote($remote);
  91. $this->assertSame($expectedUser, $remoteUser);
  92. $this->assertSame($expectedUrl, $remoteUrl);
  93. }
  94. public function dataTestSplitUserRemoteError() {
  95. return [
  96. // Invalid path
  97. ['user@'],
  98. // Invalid user
  99. ['@server'],
  100. ['us/er@server'],
  101. ['us:er@server'],
  102. // Invalid splitting
  103. ['user'],
  104. [''],
  105. ['us/erserver'],
  106. ['us:erserver'],
  107. ];
  108. }
  109. /**
  110. * @dataProvider dataTestSplitUserRemoteError
  111. *
  112. * @param string $id
  113. */
  114. public function testSplitUserRemoteError($id) {
  115. $this->expectException(\OC\HintException::class);
  116. \OC\Share\Helper::splitUserRemote($id);
  117. }
  118. /**
  119. * @dataProvider dataTestCompareServerAddresses
  120. *
  121. * @param string $server1
  122. * @param string $server2
  123. * @param bool $expected
  124. */
  125. public function testIsSameUserOnSameServer($user1, $server1, $user2, $server2, $expected) {
  126. $this->assertSame($expected,
  127. \OC\Share\Helper::isSameUserOnSameServer($user1, $server1, $user2, $server2)
  128. );
  129. }
  130. public function dataTestCompareServerAddresses() {
  131. return [
  132. ['user1', 'http://server1', 'user1', 'http://server1', true],
  133. ['user1', 'https://server1', 'user1', 'http://server1', true],
  134. ['user1', 'http://serVer1', 'user1', 'http://server1', true],
  135. ['user1', 'http://server1/', 'user1', 'http://server1', true],
  136. ['user1', 'server1', 'user1', 'http://server1', true],
  137. ['user1', 'http://server1', 'user1', 'http://server2', false],
  138. ['user1', 'https://server1', 'user1', 'http://server2', false],
  139. ['user1', 'http://serVer1', 'user1', 'http://serer2', false],
  140. ['user1', 'http://server1/', 'user1', 'http://server2', false],
  141. ['user1', 'server1', 'user1', 'http://server2', false],
  142. ['user1', 'http://server1', 'user2', 'http://server1', false],
  143. ['user1', 'https://server1', 'user2', 'http://server1', false],
  144. ['user1', 'http://serVer1', 'user2', 'http://server1', false],
  145. ['user1', 'http://server1/', 'user2', 'http://server1', false],
  146. ['user1', 'server1', 'user2', 'http://server1', false],
  147. ];
  148. }
  149. }