TrustedServersTest.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345
  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\ILogger;
  38. use OCP\Security\ISecureRandom;
  39. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  40. use Test\TestCase;
  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 | ILogger */
  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 | EventDispatcherInterface */
  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(EventDispatcherInterface::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(ILogger::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. /**
  90. * @dataProvider dataTrueFalse
  91. *
  92. * @param bool $success
  93. */
  94. public function testAddServer($success) {
  95. /** @var \PHPUnit\Framework\MockObject\MockObject|TrustedServers $trustedServers */
  96. $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
  97. ->setConstructorArgs(
  98. [
  99. $this->dbHandler,
  100. $this->httpClientService,
  101. $this->logger,
  102. $this->jobList,
  103. $this->secureRandom,
  104. $this->config,
  105. $this->dispatcher,
  106. $this->timeFactory
  107. ]
  108. )
  109. ->setMethods(['normalizeUrl', 'updateProtocol'])
  110. ->getMock();
  111. $trustedServers->expects($this->once())->method('updateProtocol')
  112. ->with('url')->willReturn('https://url');
  113. $this->timeFactory->method('getTime')
  114. ->willReturn(1234567);
  115. $this->dbHandler->expects($this->once())->method('addServer')->with('https://url')
  116. ->willReturn($success);
  117. if ($success) {
  118. $this->secureRandom->expects($this->once())->method('generate')
  119. ->willReturn('token');
  120. $this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token');
  121. $this->jobList->expects($this->once())->method('add')
  122. ->with('OCA\Federation\BackgroundJob\RequestSharedSecret',
  123. ['url' => 'https://url', 'token' => 'token', 'created' => 1234567]);
  124. } else {
  125. $this->jobList->expects($this->never())->method('add');
  126. }
  127. $this->assertSame($success,
  128. $trustedServers->addServer('url')
  129. );
  130. }
  131. public function dataTrueFalse() {
  132. return [
  133. [true],
  134. [false]
  135. ];
  136. }
  137. public function testAddSharedSecret() {
  138. $this->dbHandler->expects($this->once())->method('addSharedSecret')
  139. ->with('url', 'secret');
  140. $this->trustedServers->addSharedSecret('url', 'secret');
  141. }
  142. public function testGetSharedSecret() {
  143. $this->dbHandler->expects($this->once())->method('getSharedSecret')
  144. ->with('url')->willReturn(true);
  145. $this->assertTrue(
  146. $this->trustedServers->getSharedSecret('url')
  147. );
  148. }
  149. public function testRemoveServer() {
  150. $id = 42;
  151. $server = ['url_hash' => 'url_hash'];
  152. $this->dbHandler->expects($this->once())->method('removeServer')->with($id);
  153. $this->dbHandler->expects($this->once())->method('getServerById')->with($id)
  154. ->willReturn($server);
  155. $this->dispatcher->expects($this->once())->method('dispatch')
  156. ->willReturnCallback(
  157. function ($eventId, $event) {
  158. $this->assertSame($eventId, 'OCP\Federation\TrustedServerEvent::remove');
  159. $this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
  160. /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
  161. $this->assertSame('url_hash', $event->getSubject());
  162. }
  163. );
  164. $this->trustedServers->removeServer($id);
  165. }
  166. public function testGetServers() {
  167. $this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(['servers']);
  168. $this->assertEquals(
  169. ['servers'],
  170. $this->trustedServers->getServers()
  171. );
  172. }
  173. public function testIsTrustedServer() {
  174. $this->dbHandler->expects($this->once())->method('serverExists')->with('url')
  175. ->willReturn(true);
  176. $this->assertTrue(
  177. $this->trustedServers->isTrustedServer('url')
  178. );
  179. }
  180. public function testSetServerStatus() {
  181. $this->dbHandler->expects($this->once())->method('setServerStatus')
  182. ->with('url', 'status');
  183. $this->trustedServers->setServerStatus('url', 'status');
  184. }
  185. public function testGetServerStatus() {
  186. $this->dbHandler->expects($this->once())->method('getServerStatus')
  187. ->with('url')->willReturn(true);
  188. $this->assertTrue(
  189. $this->trustedServers->getServerStatus('url')
  190. );
  191. }
  192. /**
  193. * @dataProvider dataTestIsOwnCloudServer
  194. *
  195. * @param int $statusCode
  196. * @param bool $isValidOwnCloudVersion
  197. * @param bool $expected
  198. */
  199. public function testIsOwnCloudServer($statusCode, $isValidOwnCloudVersion, $expected) {
  200. $server = 'server1';
  201. /** @var \PHPUnit\Framework\MockObject\MockObject | TrustedServers $trustedServers */
  202. $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
  203. ->setConstructorArgs(
  204. [
  205. $this->dbHandler,
  206. $this->httpClientService,
  207. $this->logger,
  208. $this->jobList,
  209. $this->secureRandom,
  210. $this->config,
  211. $this->dispatcher,
  212. $this->timeFactory
  213. ]
  214. )
  215. ->setMethods(['checkOwnCloudVersion'])
  216. ->getMock();
  217. $this->httpClientService->expects($this->once())->method('newClient')
  218. ->willReturn($this->httpClient);
  219. $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
  220. ->willReturn($this->response);
  221. $this->response->expects($this->once())->method('getStatusCode')
  222. ->willReturn($statusCode);
  223. if ($statusCode === 200) {
  224. $trustedServers->expects($this->once())->method('checkOwnCloudVersion')
  225. ->willReturn($isValidOwnCloudVersion);
  226. } else {
  227. $trustedServers->expects($this->never())->method('checkOwnCloudVersion');
  228. }
  229. $this->assertSame($expected,
  230. $trustedServers->isOwnCloudServer($server)
  231. );
  232. }
  233. public function dataTestIsOwnCloudServer() {
  234. return [
  235. [200, true, true],
  236. [200, false, false],
  237. [404, true, false],
  238. ];
  239. }
  240. /**
  241. * @expectedExceptionMessage simulated exception
  242. */
  243. public function testIsOwnCloudServerFail() {
  244. $server = 'server1';
  245. $this->httpClientService->expects($this->once())->method('newClient')
  246. ->willReturn($this->httpClient);
  247. $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
  248. ->willReturnCallback(function () {
  249. throw new \Exception('simulated exception');
  250. });
  251. $this->assertFalse($this->trustedServers->isOwnCloudServer($server));
  252. }
  253. /**
  254. * @dataProvider dataTestCheckOwnCloudVersion
  255. */
  256. public function testCheckOwnCloudVersion($status) {
  257. $this->assertTrue($this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]));
  258. }
  259. public function dataTestCheckOwnCloudVersion() {
  260. return [
  261. ['{"version":"9.0.0"}'],
  262. ['{"version":"9.1.0"}']
  263. ];
  264. }
  265. /**
  266. * @dataProvider dataTestCheckOwnCloudVersionTooLow
  267. */
  268. public function testCheckOwnCloudVersionTooLow($status) {
  269. $this->expectException(\OCP\HintException::class);
  270. $this->expectExceptionMessage('Remote server version is too low. 9.0 is required.');
  271. $this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]);
  272. }
  273. public function dataTestCheckOwnCloudVersionTooLow() {
  274. return [
  275. ['{"version":"8.2.3"}'],
  276. ];
  277. }
  278. /**
  279. * @dataProvider dataTestUpdateProtocol
  280. * @param string $url
  281. * @param string $expected
  282. */
  283. public function testUpdateProtocol($url, $expected) {
  284. $this->assertSame($expected,
  285. $this->invokePrivate($this->trustedServers, 'updateProtocol', [$url])
  286. );
  287. }
  288. public function dataTestUpdateProtocol() {
  289. return [
  290. ['http://owncloud.org', 'http://owncloud.org'],
  291. ['https://owncloud.org', 'https://owncloud.org'],
  292. ['owncloud.org', 'https://owncloud.org'],
  293. ['httpserver', 'https://httpserver'],
  294. ];
  295. }
  296. }