AddressHandlerTest.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231
  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\EventDispatcher\IEventDispatcher;
  33. use OCP\ICacheFactory;
  34. use OCP\IL10N;
  35. use OCP\IURLGenerator;
  36. use OCP\IUserManager;
  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. if ($protocol === '') {
  84. // https:// protocol is expected in the final result
  85. $protocol = 'https://';
  86. }
  87. $testCases[] = [$baseUrl, $user, $protocol . $remote];
  88. $testCases[] = [$baseUrl . '/', $user, $protocol . $remote];
  89. $testCases[] = [$baseUrl . '/index.php', $user, $protocol . $remote];
  90. $testCases[] = [$baseUrl . '/index.php/s/token', $user, $protocol . $remote];
  91. }
  92. }
  93. }
  94. return $testCases;
  95. }
  96. /**
  97. * @dataProvider dataTestSplitUserRemote
  98. *
  99. * @param string $remote
  100. * @param string $expectedUser
  101. * @param string $expectedUrl
  102. */
  103. public function testSplitUserRemote($remote, $expectedUser, $expectedUrl) {
  104. $this->contactsManager->expects($this->any())
  105. ->method('search')
  106. ->willReturn([]);
  107. [$remoteUser, $remoteUrl] = $this->addressHandler->splitUserRemote($remote);
  108. $this->assertSame($expectedUser, $remoteUser);
  109. $this->assertSame($expectedUrl, $remoteUrl);
  110. }
  111. public function dataTestSplitUserRemoteError() {
  112. return [
  113. // Invalid path
  114. ['user@'],
  115. // Invalid user
  116. ['@server'],
  117. ['us/er@server'],
  118. ['us:er@server'],
  119. // Invalid splitting
  120. ['user'],
  121. [''],
  122. ['us/erserver'],
  123. ['us:erserver'],
  124. ];
  125. }
  126. /**
  127. * @dataProvider dataTestSplitUserRemoteError
  128. *
  129. * @param string $id
  130. */
  131. public function testSplitUserRemoteError($id) {
  132. $this->expectException(\OCP\HintException::class);
  133. $this->addressHandler->splitUserRemote($id);
  134. }
  135. /**
  136. * @dataProvider dataTestCompareAddresses
  137. *
  138. * @param string $user1
  139. * @param string $server1
  140. * @param string $user2
  141. * @param string $server2
  142. * @param bool $expected
  143. */
  144. public function testCompareAddresses($user1, $server1, $user2, $server2, $expected) {
  145. $this->assertSame($expected,
  146. $this->addressHandler->compareAddresses($user1, $server1, $user2, $server2)
  147. );
  148. }
  149. public function dataTestCompareAddresses() {
  150. return [
  151. ['user1', 'http://server1', 'user1', 'http://server1', true],
  152. ['user1', 'https://server1', 'user1', 'http://server1', true],
  153. ['user1', 'http://serVer1', 'user1', 'http://server1', true],
  154. ['user1', 'http://server1/', 'user1', 'http://server1', true],
  155. ['user1', 'server1', 'user1', 'http://server1', true],
  156. ['user1', 'http://server1', 'user1', 'http://server2', false],
  157. ['user1', 'https://server1', 'user1', 'http://server2', false],
  158. ['user1', 'http://serVer1', 'user1', 'http://serer2', false],
  159. ['user1', 'http://server1/', 'user1', 'http://server2', false],
  160. ['user1', 'server1', 'user1', 'http://server2', false],
  161. ['user1', 'http://server1', 'user2', 'http://server1', false],
  162. ['user1', 'https://server1', 'user2', 'http://server1', false],
  163. ['user1', 'http://serVer1', 'user2', 'http://server1', false],
  164. ['user1', 'http://server1/', 'user2', 'http://server1', false],
  165. ['user1', 'server1', 'user2', 'http://server1', false],
  166. ];
  167. }
  168. /**
  169. * @dataProvider dataTestRemoveProtocolFromUrl
  170. *
  171. * @param string $url
  172. * @param string $expectedResult
  173. */
  174. public function testRemoveProtocolFromUrl($url, $expectedResult) {
  175. $result = $this->addressHandler->removeProtocolFromUrl($url);
  176. $this->assertSame($expectedResult, $result);
  177. }
  178. public function dataTestRemoveProtocolFromUrl() {
  179. return [
  180. ['http://owncloud.org', 'owncloud.org'],
  181. ['https://owncloud.org', 'owncloud.org'],
  182. ['owncloud.org', 'owncloud.org'],
  183. ];
  184. }
  185. /**
  186. * @dataProvider dataTestUrlContainProtocol
  187. *
  188. * @param string $url
  189. * @param bool $expectedResult
  190. */
  191. public function testUrlContainProtocol($url, $expectedResult) {
  192. $result = $this->addressHandler->urlContainProtocol($url);
  193. $this->assertSame($expectedResult, $result);
  194. }
  195. public function dataTestUrlContainProtocol() {
  196. return [
  197. ['http://nextcloud.com', true],
  198. ['https://nextcloud.com', true],
  199. ['nextcloud.com', false],
  200. ['httpserver.com', false],
  201. ];
  202. }
  203. }