1
0

AddressHandlerTest.php 6.5 KB

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