AddressHandlerTest.php 5.7 KB

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