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.

AddressHandlerTest.php 6.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016, ownCloud, Inc.
  4. *
  5. * @author Arthur Schiwon <blizzz@arthur-schiwon.de>
  6. * @author Bjoern Schiessle <bjoern@schiessle.org>
  7. * @author Björn Schießle <bjoern@schiessle.org>
  8. * @author Christoph Wurst <christoph@winzerhof-wurst.at>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Robin Appelman <robin@icewind.nl>
  11. * @author Roeland Jago Douma <roeland@famdouma.nl>
  12. *
  13. * @license AGPL-3.0
  14. *
  15. * This code is free software: you can redistribute it and/or modify
  16. * it under the terms of the GNU Affero General Public License, version 3,
  17. * as published by the Free Software Foundation.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU Affero General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU Affero General Public License, version 3,
  25. * along with this program. If not, see <http://www.gnu.org/licenses/>
  26. *
  27. */
  28. namespace OCA\FederatedFileSharing\Tests;
  29. use OC\Federation\CloudIdManager;
  30. use OCA\FederatedFileSharing\AddressHandler;
  31. use OCP\Contacts\IManager;
  32. use OCP\IL10N;
  33. use OCP\IURLGenerator;
  34. use OCP\IUserManager;
  35. class AddressHandlerTest extends \Test\TestCase {
  36. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  37. protected $contactsManager;
  38. /** @var AddressHandler */
  39. private $addressHandler;
  40. /** @var IURLGenerator | \PHPUnit\Framework\MockObject\MockObject */
  41. private $urlGenerator;
  42. /** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
  43. private $il10n;
  44. /** @var CloudIdManager */
  45. private $cloudIdManager;
  46. protected function setUp(): void {
  47. parent::setUp();
  48. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  49. ->getMock();
  50. $this->il10n = $this->getMockBuilder(IL10N::class)
  51. ->getMock();
  52. $this->contactsManager = $this->createMock(IManager::class);
  53. $this->cloudIdManager = new CloudIdManager($this->contactsManager, $this->urlGenerator, $this->createMock(IUserManager::class));
  54. $this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager);
  55. }
  56. public function dataTestSplitUserRemote() {
  57. $userPrefix = ['user@name', 'username'];
  58. $protocols = ['', 'http://', 'https://'];
  59. $remotes = [
  60. 'localhost',
  61. 'local.host',
  62. 'dev.local.host',
  63. 'dev.local.host/path',
  64. 'dev.local.host/at@inpath',
  65. '127.0.0.1',
  66. '::1',
  67. '::192.0.2.128',
  68. '::192.0.2.128/at@inpath',
  69. ];
  70. $testCases = [];
  71. foreach ($userPrefix as $user) {
  72. foreach ($remotes as $remote) {
  73. foreach ($protocols as $protocol) {
  74. $baseUrl = $user . '@' . $protocol . $remote;
  75. $testCases[] = [$baseUrl, $user, $protocol . $remote];
  76. $testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
  77. $testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
  78. $testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote];
  79. }
  80. }
  81. }
  82. return $testCases;
  83. }
  84. /**
  85. * @dataProvider dataTestSplitUserRemote
  86. *
  87. * @param string $remote
  88. * @param string $expectedUser
  89. * @param string $expectedUrl
  90. */
  91. public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) {
  92. $this->contactsManager->expects($this->any())
  93. ->method('search')
  94. ->willReturn([]);
  95. [$remoteUser, $remoteUrl] = $this->addressHandler->splitUserRemote($remote);
  96. $this->assertSame($expectedUser, $remoteUser);
  97. $this->assertSame($expectedUrl, $remoteUrl);
  98. }
  99. public function dataTestSplitUserRemoteError() {
  100. return [
  101. // Invalid path
  102. ['user@'],
  103. // Invalid user
  104. ['@server'],
  105. ['us/er@server'],
  106. ['us:er@server'],
  107. // Invalid splitting
  108. ['user'],
  109. [''],
  110. ['us/erserver'],
  111. ['us:erserver'],
  112. ];
  113. }
  114. /**
  115. * @dataProvider dataTestSplitUserRemoteError
  116. *
  117. * @param string $id
  118. */
  119. public function testSplitUserRemoteError($id) {
  120. $this->expectException(\OCP\HintException::class);
  121. $this->addressHandler->splitUserRemote($id);
  122. }
  123. /**
  124. * @dataProvider dataTestCompareAddresses
  125. *
  126. * @param string $user1
  127. * @param string $server1
  128. * @param string $user2
  129. * @param string $server2
  130. * @param bool $expected
  131. */
  132. public function testCompareAddresses($user1, $server1, $user2, $server2, $expected) {
  133. $this->assertSame($expected,
  134. $this->addressHandler->compareAddresses($user1, $server1, $user2, $server2)
  135. );
  136. }
  137. public function dataTestCompareAddresses() {
  138. return [
  139. ['user1', 'http://server1', 'user1', 'http://server1', true],
  140. ['user1', 'https://server1', 'user1', 'http://server1', true],
  141. ['user1', 'http://serVer1', 'user1', 'http://server1', true],
  142. ['user1', 'http://server1/', 'user1', 'http://server1', true],
  143. ['user1', 'server1', 'user1', 'http://server1', true],
  144. ['user1', 'http://server1', 'user1', 'http://server2', false],
  145. ['user1', 'https://server1', 'user1', 'http://server2', false],
  146. ['user1', 'http://serVer1', 'user1', 'http://serer2', false],
  147. ['user1', 'http://server1/', 'user1', 'http://server2', false],
  148. ['user1', 'server1', 'user1', 'http://server2', false],
  149. ['user1', 'http://server1', 'user2', 'http://server1', false],
  150. ['user1', 'https://server1', 'user2', 'http://server1', false],
  151. ['user1', 'http://serVer1', 'user2', 'http://server1', false],
  152. ['user1', 'http://server1/', 'user2', 'http://server1', false],
  153. ['user1', 'server1', 'user2', 'http://server1', false],
  154. ];
  155. }
  156. /**
  157. * @dataProvider dataTestRemoveProtocolFromUrl
  158. *
  159. * @param string $url
  160. * @param string $expectedResult
  161. */
  162. public function testRemoveProtocolFromUrl($url, $expectedResult) {
  163. $result = $this->addressHandler->removeProtocolFromUrl($url);
  164. $this->assertSame($expectedResult, $result);
  165. }
  166. public function dataTestRemoveProtocolFromUrl() {
  167. return [
  168. ['http://owncloud.org', 'owncloud.org'],
  169. ['https://owncloud.org', 'owncloud.org'],
  170. ['owncloud.org', 'owncloud.org'],
  171. ];
  172. }
  173. /**
  174. * @dataProvider dataTestUrlContainProtocol
  175. *
  176. * @param string $url
  177. * @param bool $expectedResult
  178. */
  179. public function testUrlContainProtocol($url, $expectedResult) {
  180. $result = $this->addressHandler->urlContainProtocol($url);
  181. $this->assertSame($expectedResult, $result);
  182. }
  183. public function dataTestUrlContainProtocol() {
  184. return [
  185. ['http://nextcloud.com', true],
  186. ['https://nextcloud.com', true],
  187. ['nextcloud.com', false],
  188. ['httpserver.com', false],
  189. ];
  190. }
  191. }