1
0

AddressHandlerTest.php 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. <?php
  2. /**
  3. * SPDX-FileCopyrightText: 2016-2024 Nextcloud GmbH and Nextcloud contributors
  4. * SPDX-FileCopyrightText: 2016 ownCloud, Inc.
  5. * SPDX-License-Identifier: AGPL-3.0-only
  6. */
  7. namespace OCA\FederatedFileSharing\Tests;
  8. use OC\Federation\CloudIdManager;
  9. use OCA\FederatedFileSharing\AddressHandler;
  10. use OCP\Contacts\IManager;
  11. use OCP\EventDispatcher\IEventDispatcher;
  12. use OCP\HintException;
  13. use OCP\ICacheFactory;
  14. use OCP\IL10N;
  15. use OCP\IURLGenerator;
  16. use OCP\IUserManager;
  17. class AddressHandlerTest extends \Test\TestCase {
  18. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  19. protected $contactsManager;
  20. /** @var AddressHandler */
  21. private $addressHandler;
  22. /** @var IURLGenerator | \PHPUnit\Framework\MockObject\MockObject */
  23. private $urlGenerator;
  24. /** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
  25. private $il10n;
  26. /** @var CloudIdManager */
  27. private $cloudIdManager;
  28. protected function setUp(): void {
  29. parent::setUp();
  30. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  31. ->getMock();
  32. $this->il10n = $this->getMockBuilder(IL10N::class)
  33. ->getMock();
  34. $this->contactsManager = $this->createMock(IManager::class);
  35. $this->cloudIdManager = new CloudIdManager(
  36. $this->contactsManager,
  37. $this->urlGenerator,
  38. $this->createMock(IUserManager::class),
  39. $this->createMock(ICacheFactory::class),
  40. $this->createMock(IEventDispatcher::class)
  41. );
  42. $this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager);
  43. }
  44. public function dataTestSplitUserRemote() {
  45. $userPrefix = ['user@name', 'username'];
  46. $protocols = ['', 'http://', 'https://'];
  47. $remotes = [
  48. 'localhost',
  49. 'local.host',
  50. 'dev.local.host',
  51. 'dev.local.host/path',
  52. 'dev.local.host/at@inpath',
  53. '127.0.0.1',
  54. '::1',
  55. '::192.0.2.128',
  56. '::192.0.2.128/at@inpath',
  57. ];
  58. $testCases = [];
  59. foreach ($userPrefix as $user) {
  60. foreach ($remotes as $remote) {
  61. foreach ($protocols as $protocol) {
  62. $baseUrl = $user . '@' . $protocol . $remote;
  63. if ($protocol === '') {
  64. // https:// protocol is expected in the final result
  65. $protocol = 'https://';
  66. }
  67. $testCases[] = [$baseUrl, $user, $protocol . $remote];
  68. $testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
  69. $testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
  70. $testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote];
  71. }
  72. }
  73. }
  74. return $testCases;
  75. }
  76. /**
  77. * @dataProvider dataTestSplitUserRemote
  78. *
  79. * @param string $remote
  80. * @param string $expectedUser
  81. * @param string $expectedUrl
  82. */
  83. public function testSplitUserRemote($remote, $expectedUser, $expectedUrl): void {
  84. $this->contactsManager->expects($this->any())
  85. ->method('search')
  86. ->willReturn([]);
  87. [$remoteUser, $remoteUrl] = $this->addressHandler->splitUserRemote($remote);
  88. $this->assertSame($expectedUser, $remoteUser);
  89. $this->assertSame($expectedUrl, $remoteUrl);
  90. }
  91. public function dataTestSplitUserRemoteError() {
  92. return [
  93. // Invalid path
  94. ['user@'],
  95. // Invalid user
  96. ['@server'],
  97. ['us/er@server'],
  98. ['us:er@server'],
  99. // Invalid splitting
  100. ['user'],
  101. [''],
  102. ['us/erserver'],
  103. ['us:erserver'],
  104. ];
  105. }
  106. /**
  107. * @dataProvider dataTestSplitUserRemoteError
  108. *
  109. * @param string $id
  110. */
  111. public function testSplitUserRemoteError($id): void {
  112. $this->expectException(HintException::class);
  113. $this->addressHandler->splitUserRemote($id);
  114. }
  115. /**
  116. * @dataProvider dataTestCompareAddresses
  117. *
  118. * @param string $user1
  119. * @param string $server1
  120. * @param string $user2
  121. * @param string $server2
  122. * @param bool $expected
  123. */
  124. public function testCompareAddresses($user1, $server1, $user2, $server2, $expected): void {
  125. $this->assertSame($expected,
  126. $this->addressHandler->compareAddresses($user1, $server1, $user2, $server2)
  127. );
  128. }
  129. public function dataTestCompareAddresses() {
  130. return [
  131. ['user1', 'http://server1', 'user1', 'http://server1', true],
  132. ['user1', 'https://server1', 'user1', 'http://server1', true],
  133. ['user1', 'http://serVer1', 'user1', 'http://server1', true],
  134. ['user1', 'http://server1/', 'user1', 'http://server1', true],
  135. ['user1', 'server1', 'user1', 'http://server1', true],
  136. ['user1', 'http://server1', 'user1', 'http://server2', false],
  137. ['user1', 'https://server1', 'user1', 'http://server2', false],
  138. ['user1', 'http://serVer1', 'user1', 'http://serer2', false],
  139. ['user1', 'http://server1/', 'user1', 'http://server2', false],
  140. ['user1', 'server1', 'user1', 'http://server2', false],
  141. ['user1', 'http://server1', 'user2', 'http://server1', false],
  142. ['user1', 'https://server1', 'user2', 'http://server1', false],
  143. ['user1', 'http://serVer1', 'user2', 'http://server1', false],
  144. ['user1', 'http://server1/', 'user2', 'http://server1', false],
  145. ['user1', 'server1', 'user2', 'http://server1', false],
  146. ];
  147. }
  148. /**
  149. * @dataProvider dataTestRemoveProtocolFromUrl
  150. *
  151. * @param string $url
  152. * @param string $expectedResult
  153. */
  154. public function testRemoveProtocolFromUrl($url, $expectedResult): void {
  155. $result = $this->addressHandler->removeProtocolFromUrl($url);
  156. $this->assertSame($expectedResult, $result);
  157. }
  158. public function dataTestRemoveProtocolFromUrl() {
  159. return [
  160. ['http://owncloud.org', 'owncloud.org'],
  161. ['https://owncloud.org', 'owncloud.org'],
  162. ['owncloud.org', 'owncloud.org'],
  163. ];
  164. }
  165. /**
  166. * @dataProvider dataTestUrlContainProtocol
  167. *
  168. * @param string $url
  169. * @param bool $expectedResult
  170. */
  171. public function testUrlContainProtocol($url, $expectedResult): void {
  172. $result = $this->addressHandler->urlContainProtocol($url);
  173. $this->assertSame($expectedResult, $result);
  174. }
  175. public function dataTestUrlContainProtocol() {
  176. return [
  177. ['http://nextcloud.com', true],
  178. ['https://nextcloud.com', true],
  179. ['nextcloud.com', false],
  180. ['httpserver.com', false],
  181. ];
  182. }
  183. }