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.2KB

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. class AddressHandlerTest extends \Test\TestCase {
  35. /** @var IManager|\PHPUnit\Framework\MockObject\MockObject */
  36. protected $contactsManager;
  37. /** @var AddressHandler */
  38. private $addressHandler;
  39. /** @var IURLGenerator | \PHPUnit\Framework\MockObject\MockObject */
  40. private $urlGenerator;
  41. /** @var IL10N | \PHPUnit\Framework\MockObject\MockObject */
  42. private $il10n;
  43. /** @var CloudIdManager */
  44. private $cloudIdManager;
  45. protected function setUp(): void {
  46. parent::setUp();
  47. $this->urlGenerator = $this->getMockBuilder(IURLGenerator::class)
  48. ->getMock();
  49. $this->il10n = $this->getMockBuilder(IL10N::class)
  50. ->getMock();
  51. $this->contactsManager = $this->createMock(IManager::class);
  52. $this->cloudIdManager = new CloudIdManager($this->contactsManager);
  53. $this->addressHandler = new AddressHandler($this->urlGenerator, $this->il10n, $this->cloudIdManager);
  54. }
  55. public function dataTestSplitUserRemote() {
  56. $userPrefix = ['user@name', 'username'];
  57. $protocols = ['', 'http://', 'https://'];
  58. $remotes = [
  59. 'localhost',
  60. 'local.host',
  61. 'dev.local.host',
  62. 'dev.local.host/path',
  63. 'dev.local.host/at@inpath',
  64. '127.0.0.1',
  65. '::1',
  66. '::192.0.2.128',
  67. '::192.0.2.128/at@inpath',
  68. ];
  69. $testCases = [];
  70. foreach ($userPrefix as $user) {
  71. foreach ($remotes as $remote) {
  72. foreach ($protocols as $protocol) {
  73. $baseUrl = $user . '@' . $protocol . $remote;
  74. $testCases[] = [$baseUrl, $user, $protocol . $remote];
  75. $testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
  76. $testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
  77. $testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote];
  78. }
  79. }
  80. }
  81. return $testCases;
  82. }
  83. /**
  84. * @dataProvider dataTestSplitUserRemote
  85. *
  86. * @param string $remote
  87. * @param string $expectedUser
  88. * @param string $expectedUrl
  89. */
  90. public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) {
  91. $this->contactsManager->expects($this->any())
  92. ->method('search')
  93. ->willReturn([]);
  94. [$remoteUser, $remoteUrl] = $this->addressHandler->splitUserRemote($remote);
  95. $this->assertSame($expectedUser, $remoteUser);
  96. $this->assertSame($expectedUrl, $remoteUrl);
  97. }
  98. public function dataTestSplitUserRemoteError() {
  99. return [
  100. // Invalid path
  101. ['user@'],
  102. // Invalid user
  103. ['@server'],
  104. ['us/er@server'],
  105. ['us:er@server'],
  106. // Invalid splitting
  107. ['user'],
  108. [''],
  109. ['us/erserver'],
  110. ['us:erserver'],
  111. ];
  112. }
  113. /**
  114. * @dataProvider dataTestSplitUserRemoteError
  115. *
  116. * @param string $id
  117. */
  118. public function testSplitUserRemoteError($id) {
  119. $this->expectException(\OC\HintException::class);
  120. $this->addressHandler->splitUserRemote($id);
  121. }
  122. /**
  123. * @dataProvider dataTestCompareAddresses
  124. *
  125. * @param string $user1
  126. * @param string $server1
  127. * @param string $user2
  128. * @param string $server2
  129. * @param bool $expected
  130. */
  131. public function testCompareAddresses($user1, $server1, $user2, $server2, $expected) {
  132. $this->assertSame($expected,
  133. $this->addressHandler->compareAddresses($user1, $server1, $user2, $server2)
  134. );
  135. }
  136. public function dataTestCompareAddresses() {
  137. return [
  138. ['user1', 'http://server1', 'user1', 'http://server1', true],
  139. ['user1', 'https://server1', 'user1', 'http://server1', true],
  140. ['user1', 'http://serVer1', 'user1', 'http://server1', true],
  141. ['user1', 'http://server1/', 'user1', 'http://server1', true],
  142. ['user1', 'server1', 'user1', 'http://server1', true],
  143. ['user1', 'http://server1', 'user1', 'http://server2', false],
  144. ['user1', 'https://server1', 'user1', 'http://server2', false],
  145. ['user1', 'http://serVer1', 'user1', 'http://serer2', false],
  146. ['user1', 'http://server1/', 'user1', 'http://server2', false],
  147. ['user1', 'server1', 'user1', 'http://server2', false],
  148. ['user1', 'http://server1', 'user2', 'http://server1', false],
  149. ['user1', 'https://server1', 'user2', 'http://server1', false],
  150. ['user1', 'http://serVer1', 'user2', 'http://server1', false],
  151. ['user1', 'http://server1/', 'user2', 'http://server1', false],
  152. ['user1', 'server1', 'user2', 'http://server1', false],
  153. ];
  154. }
  155. /**
  156. * @dataProvider dataTestRemoveProtocolFromUrl
  157. *
  158. * @param string $url
  159. * @param string $expectedResult
  160. */
  161. public function testRemoveProtocolFromUrl($url, $expectedResult) {
  162. $result = $this->addressHandler->removeProtocolFromUrl($url);
  163. $this->assertSame($expectedResult, $result);
  164. }
  165. public function dataTestRemoveProtocolFromUrl() {
  166. return [
  167. ['http://owncloud.org', 'owncloud.org'],
  168. ['https://owncloud.org', 'owncloud.org'],
  169. ['owncloud.org', 'owncloud.org'],
  170. ];
  171. }
  172. /**
  173. * @dataProvider dataTestUrlContainProtocol
  174. *
  175. * @param string $url
  176. * @param bool $expectedResult
  177. */
  178. public function testUrlContainProtocol($url, $expectedResult) {
  179. $result = $this->addressHandler->urlContainProtocol($url);
  180. $this->assertSame($expectedResult, $result);
  181. }
  182. public function dataTestUrlContainProtocol() {
  183. return [
  184. ['http://nextcloud.com', true],
  185. ['https://nextcloud.com', true],
  186. ['nextcloud.com', false],
  187. ['httpserver.com', false],
  188. ];
  189. }
  190. }