AddressHandlerTest.php 5.9 KB

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