TrustedServersTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329
  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 Christoph Wurst <christoph@winzerhof-wurst.at>
  8. * @author Joas Schilling <coding@schilljs.com>
  9. * @author Morris Jobke <hey@morrisjobke.de>
  10. * @author Roeland Jago Douma <roeland@famdouma.nl>
  11. * @author Thomas Müller <thomas.mueller@tmit.eu>
  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\Federation\Tests;
  29. use OCA\Federation\DbHandler;
  30. use OCA\Federation\TrustedServers;
  31. use OCP\AppFramework\Utility\ITimeFactory;
  32. use OCP\BackgroundJob\IJobList;
  33. use OCP\Http\Client\IClient;
  34. use OCP\Http\Client\IClientService;
  35. use OCP\Http\Client\IResponse;
  36. use OCP\IConfig;
  37. use OCP\Security\ISecureRandom;
  38. use OCP\EventDispatcher\IEventDispatcher;
  39. use Test\TestCase;
  40. use Psr\Log\LoggerInterface;
  41. class TrustedServersTest extends TestCase {
  42. /** @var \PHPUnit\Framework\MockObject\MockObject | TrustedServers */
  43. private $trustedServers;
  44. /** @var \PHPUnit\Framework\MockObject\MockObject | DbHandler */
  45. private $dbHandler;
  46. /** @var \PHPUnit\Framework\MockObject\MockObject | IClientService */
  47. private $httpClientService;
  48. /** @var \PHPUnit\Framework\MockObject\MockObject | IClient */
  49. private $httpClient;
  50. /** @var \PHPUnit\Framework\MockObject\MockObject | IResponse */
  51. private $response;
  52. /** @var \PHPUnit\Framework\MockObject\MockObject | LoggerInterface */
  53. private $logger;
  54. /** @var \PHPUnit\Framework\MockObject\MockObject | IJobList */
  55. private $jobList;
  56. /** @var \PHPUnit\Framework\MockObject\MockObject | ISecureRandom */
  57. private $secureRandom;
  58. /** @var \PHPUnit\Framework\MockObject\MockObject | IConfig */
  59. private $config;
  60. /** @var \PHPUnit\Framework\MockObject\MockObject | IEventDispatcher */
  61. private $dispatcher;
  62. /** @var \PHPUnit\Framework\MockObject\MockObject|ITimeFactory */
  63. private $timeFactory;
  64. protected function setUp(): void {
  65. parent::setUp();
  66. $this->dbHandler = $this->getMockBuilder(DbHandler::class)
  67. ->disableOriginalConstructor()->getMock();
  68. $this->dispatcher = $this->getMockBuilder(IEventDispatcher::class)
  69. ->disableOriginalConstructor()->getMock();
  70. $this->httpClientService = $this->getMockBuilder(IClientService::class)->getMock();
  71. $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
  72. $this->response = $this->getMockBuilder(IResponse::class)->getMock();
  73. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  74. $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
  75. $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock();
  76. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  77. $this->timeFactory = $this->createMock(ITimeFactory::class);
  78. $this->trustedServers = new TrustedServers(
  79. $this->dbHandler,
  80. $this->httpClientService,
  81. $this->logger,
  82. $this->jobList,
  83. $this->secureRandom,
  84. $this->config,
  85. $this->dispatcher,
  86. $this->timeFactory
  87. );
  88. }
  89. public function testAddServer(): void {
  90. /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers $trustedServers */
  91. $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
  92. ->setConstructorArgs(
  93. [
  94. $this->dbHandler,
  95. $this->httpClientService,
  96. $this->logger,
  97. $this->jobList,
  98. $this->secureRandom,
  99. $this->config,
  100. $this->dispatcher,
  101. $this->timeFactory
  102. ]
  103. )
  104. ->setMethods(['normalizeUrl', 'updateProtocol'])
  105. ->getMock();
  106. $trustedServers->expects($this->once())->method('updateProtocol')
  107. ->with('url')->willReturn('https://url');
  108. $this->timeFactory->method('getTime')
  109. ->willReturn(1234567);
  110. $this->dbHandler->expects($this->once())->method('addServer')->with('https://url')
  111. ->willReturn(1);
  112. $this->secureRandom->expects($this->once())->method('generate')
  113. ->willReturn('token');
  114. $this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token');
  115. $this->jobList->expects($this->once())->method('add')
  116. ->with('OCA\Federation\BackgroundJob\RequestSharedSecret',
  117. ['url' => 'https://url', 'token' => 'token', 'created' => 1234567]);
  118. $this->assertSame(
  119. $trustedServers->addServer('url'),
  120. 1
  121. );
  122. }
  123. public function testAddSharedSecret(): void {
  124. $this->dbHandler->expects($this->once())->method('addSharedSecret')
  125. ->with('url', 'secret');
  126. $this->trustedServers->addSharedSecret('url', 'secret');
  127. }
  128. public function testGetSharedSecret(): void {
  129. $this->dbHandler->expects($this->once())
  130. ->method('getSharedSecret')
  131. ->with('url')
  132. ->willReturn('secret');
  133. $this->assertSame(
  134. $this->trustedServers->getSharedSecret('url'),
  135. 'secret'
  136. );
  137. }
  138. public function testRemoveServer(): void {
  139. $id = 42;
  140. $server = ['url_hash' => 'url_hash'];
  141. $this->dbHandler->expects($this->once())->method('removeServer')->with($id);
  142. $this->dbHandler->expects($this->once())->method('getServerById')->with($id)
  143. ->willReturn($server);
  144. $this->dispatcher->expects($this->once())->method('dispatchTyped')
  145. ->willReturnCallback(
  146. function ($event) {
  147. $this->assertSame(get_class($event), \OCP\Federation\Events\TrustedServerRemovedEvent::class);
  148. /** @var \OCP\Federated\Events\TrustedServerRemovedEvent $event */
  149. $this->assertSame('url_hash', $event->getUrlHash());
  150. }
  151. );
  152. $this->trustedServers->removeServer($id);
  153. }
  154. public function testGetServers(): void {
  155. $this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(['servers']);
  156. $this->assertEquals(
  157. ['servers'],
  158. $this->trustedServers->getServers()
  159. );
  160. }
  161. public function testIsTrustedServer(): void {
  162. $this->dbHandler->expects($this->once())
  163. ->method('serverExists')->with('url')
  164. ->willReturn(true);
  165. $this->assertTrue(
  166. $this->trustedServers->isTrustedServer('url')
  167. );
  168. }
  169. public function testSetServerStatus() {
  170. $this->dbHandler->expects($this->once())->method('setServerStatus')
  171. ->with('url', 1);
  172. $this->trustedServers->setServerStatus('url', 1);
  173. }
  174. public function testGetServerStatus() {
  175. $this->dbHandler->expects($this->once())->method('getServerStatus')
  176. ->with('url')->willReturn(1);
  177. $this->assertSame(
  178. $this->trustedServers->getServerStatus('url'),
  179. 1
  180. );
  181. }
  182. /**
  183. * @dataProvider dataTestIsNextcloudServer
  184. */
  185. public function testIsNextcloudServer(int $statusCode, bool $isValidNextcloudVersion, bool $expected): void {
  186. $server = 'server1';
  187. /** @var \PHPUnit\Framework\MockObject\MockObject | TrustedServers $trustedServers */
  188. $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
  189. ->setConstructorArgs(
  190. [
  191. $this->dbHandler,
  192. $this->httpClientService,
  193. $this->logger,
  194. $this->jobList,
  195. $this->secureRandom,
  196. $this->config,
  197. $this->dispatcher,
  198. $this->timeFactory
  199. ]
  200. )
  201. ->setMethods(['checkNextcloudVersion'])
  202. ->getMock();
  203. $this->httpClientService->expects($this->once())->method('newClient')
  204. ->willReturn($this->httpClient);
  205. $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
  206. ->willReturn($this->response);
  207. $this->response->expects($this->once())->method('getStatusCode')
  208. ->willReturn($statusCode);
  209. if ($statusCode === 200) {
  210. $this->response->expects($this->once())->method('getBody')
  211. ->willReturn('');
  212. $trustedServers->expects($this->once())->method('checkNextcloudVersion')
  213. ->willReturn($isValidNextcloudVersion);
  214. } else {
  215. $trustedServers->expects($this->never())->method('checkNextcloudVersion');
  216. }
  217. $this->assertSame($expected,
  218. $trustedServers->isNextcloudServer($server)
  219. );
  220. }
  221. public function dataTestIsNextcloudServer(): array {
  222. return [
  223. [200, true, true],
  224. [200, false, false],
  225. [404, true, false],
  226. ];
  227. }
  228. /**
  229. * @expectedExceptionMessage simulated exception
  230. */
  231. public function testIsNextcloudServerFail(): void {
  232. $server = 'server1';
  233. $this->httpClientService->expects($this->once())->method('newClient')
  234. ->willReturn($this->httpClient);
  235. $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
  236. ->willReturnCallback(function () {
  237. throw new \Exception('simulated exception');
  238. });
  239. $this->assertFalse($this->trustedServers->isNextcloudServer($server));
  240. }
  241. /**
  242. * @dataProvider dataTestCheckNextcloudVersion
  243. */
  244. public function testCheckNextcloudVersion($status): void {
  245. $this->assertTrue($this->invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]));
  246. }
  247. public function dataTestCheckNextcloudVersion(): array {
  248. return [
  249. ['{"version":"9.0.0"}'],
  250. ['{"version":"9.1.0"}']
  251. ];
  252. }
  253. /**
  254. * @dataProvider dataTestCheckNextcloudVersionTooLow
  255. */
  256. public function testCheckNextcloudVersionTooLow(string $status): void {
  257. $this->expectException(\OCP\HintException::class);
  258. $this->expectExceptionMessage('Remote server version is too low. 9.0 is required.');
  259. $this->invokePrivate($this->trustedServers, 'checkNextcloudVersion', [$status]);
  260. }
  261. public function dataTestCheckNextcloudVersionTooLow(): array {
  262. return [
  263. ['{"version":"8.2.3"}'],
  264. ];
  265. }
  266. /**
  267. * @dataProvider dataTestUpdateProtocol
  268. */
  269. public function testUpdateProtocol(string $url, string $expected): void {
  270. $this->assertSame($expected,
  271. $this->invokePrivate($this->trustedServers, 'updateProtocol', [$url])
  272. );
  273. }
  274. public function dataTestUpdateProtocol(): array {
  275. return [
  276. ['http://owncloud.org', 'http://owncloud.org'],
  277. ['https://owncloud.org', 'https://owncloud.org'],
  278. ['owncloud.org', 'https://owncloud.org'],
  279. ['httpserver', 'https://httpserver'],
  280. ];
  281. }
  282. }