AddressHandlerTest.php 6.1 KB

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