You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

TrustedServersTest.php 11KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385
  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. /**
  138. * @dataProvider dataTrueFalse
  139. *
  140. * @param bool $status
  141. */
  142. public function testSetAutoAddServers($status) {
  143. if ($status) {
  144. $this->config->expects($this->once())->method('setAppValue')
  145. ->with('federation', 'autoAddServers', '1');
  146. } else {
  147. $this->config->expects($this->once())->method('setAppValue')
  148. ->with('federation', 'autoAddServers', '0');
  149. }
  150. $this->trustedServers->setAutoAddServers($status);
  151. }
  152. /**
  153. * @dataProvider dataTestGetAutoAddServers
  154. *
  155. * @param string $status
  156. * @param bool $expected
  157. */
  158. public function testGetAutoAddServers($status, $expected) {
  159. $this->config->expects($this->once())->method('getAppValue')
  160. ->with('federation', 'autoAddServers', '0')->willReturn($status);
  161. $this->assertSame($expected,
  162. $this->trustedServers->getAutoAddServers()
  163. );
  164. }
  165. public function dataTestGetAutoAddServers() {
  166. return [
  167. ['1', true],
  168. ['0', false]
  169. ];
  170. }
  171. public function testAddSharedSecret() {
  172. $this->dbHandler->expects($this->once())->method('addSharedSecret')
  173. ->with('url', 'secret');
  174. $this->trustedServers->addSharedSecret('url', 'secret');
  175. }
  176. public function testGetSharedSecret() {
  177. $this->dbHandler->expects($this->once())->method('getSharedSecret')
  178. ->with('url')->willReturn(true);
  179. $this->assertTrue(
  180. $this->trustedServers->getSharedSecret('url')
  181. );
  182. }
  183. public function testRemoveServer() {
  184. $id = 42;
  185. $server = ['url_hash' => 'url_hash'];
  186. $this->dbHandler->expects($this->once())->method('removeServer')->with($id);
  187. $this->dbHandler->expects($this->once())->method('getServerById')->with($id)
  188. ->willReturn($server);
  189. $this->dispatcher->expects($this->once())->method('dispatch')
  190. ->willReturnCallback(
  191. function ($eventId, $event) {
  192. $this->assertSame($eventId, 'OCP\Federation\TrustedServerEvent::remove');
  193. $this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
  194. /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
  195. $this->assertSame('url_hash', $event->getSubject());
  196. }
  197. );
  198. $this->trustedServers->removeServer($id);
  199. }
  200. public function testGetServers() {
  201. $this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(['servers']);
  202. $this->assertEquals(
  203. ['servers'],
  204. $this->trustedServers->getServers()
  205. );
  206. }
  207. public function testIsTrustedServer() {
  208. $this->dbHandler->expects($this->once())->method('serverExists')->with('url')
  209. ->willReturn(true);
  210. $this->assertTrue(
  211. $this->trustedServers->isTrustedServer('url')
  212. );
  213. }
  214. public function testSetServerStatus() {
  215. $this->dbHandler->expects($this->once())->method('setServerStatus')
  216. ->with('url', 'status');
  217. $this->trustedServers->setServerStatus('url', 'status');
  218. }
  219. public function testGetServerStatus() {
  220. $this->dbHandler->expects($this->once())->method('getServerStatus')
  221. ->with('url')->willReturn(true);
  222. $this->assertTrue(
  223. $this->trustedServers->getServerStatus('url')
  224. );
  225. }
  226. /**
  227. * @dataProvider dataTestIsOwnCloudServer
  228. *
  229. * @param int $statusCode
  230. * @param bool $isValidOwnCloudVersion
  231. * @param bool $expected
  232. */
  233. public function testIsOwnCloudServer($statusCode, $isValidOwnCloudVersion, $expected) {
  234. $server = 'server1';
  235. /** @var \PHPUnit\Framework\MockObject\MockObject | TrustedServers $trustedServers */
  236. $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
  237. ->setConstructorArgs(
  238. [
  239. $this->dbHandler,
  240. $this->httpClientService,
  241. $this->logger,
  242. $this->jobList,
  243. $this->secureRandom,
  244. $this->config,
  245. $this->dispatcher,
  246. $this->timeFactory
  247. ]
  248. )
  249. ->setMethods(['checkOwnCloudVersion'])
  250. ->getMock();
  251. $this->httpClientService->expects($this->once())->method('newClient')
  252. ->willReturn($this->httpClient);
  253. $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
  254. ->willReturn($this->response);
  255. $this->response->expects($this->once())->method('getStatusCode')
  256. ->willReturn($statusCode);
  257. if ($statusCode === 200) {
  258. $trustedServers->expects($this->once())->method('checkOwnCloudVersion')
  259. ->willReturn($isValidOwnCloudVersion);
  260. } else {
  261. $trustedServers->expects($this->never())->method('checkOwnCloudVersion');
  262. }
  263. $this->assertSame($expected,
  264. $trustedServers->isOwnCloudServer($server)
  265. );
  266. }
  267. public function dataTestIsOwnCloudServer() {
  268. return [
  269. [200, true, true],
  270. [200, false, false],
  271. [404, true, false],
  272. ];
  273. }
  274. /**
  275. * @expectedExceptionMessage simulated exception
  276. */
  277. public function testIsOwnCloudServerFail() {
  278. $server = 'server1';
  279. $this->httpClientService->expects($this->once())->method('newClient')
  280. ->willReturn($this->httpClient);
  281. $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
  282. ->willReturnCallback(function () {
  283. throw new \Exception('simulated exception');
  284. });
  285. $this->assertFalse($this->trustedServers->isOwnCloudServer($server));
  286. }
  287. /**
  288. * @dataProvider dataTestCheckOwnCloudVersion
  289. */
  290. public function testCheckOwnCloudVersion($status) {
  291. $this->assertTrue($this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]));
  292. }
  293. public function dataTestCheckOwnCloudVersion() {
  294. return [
  295. ['{"version":"9.0.0"}'],
  296. ['{"version":"9.1.0"}']
  297. ];
  298. }
  299. /**
  300. * @dataProvider dataTestCheckOwnCloudVersionTooLow
  301. */
  302. public function testCheckOwnCloudVersionTooLow($status) {
  303. $this->expectException(\OC\HintException::class);
  304. $this->expectExceptionMessage('Remote server version is too low. 9.0 is required.');
  305. $this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]);
  306. }
  307. public function dataTestCheckOwnCloudVersionTooLow() {
  308. return [
  309. ['{"version":"8.2.3"}'],
  310. ];
  311. }
  312. /**
  313. * @dataProvider dataTestUpdateProtocol
  314. * @param string $url
  315. * @param string $expected
  316. */
  317. public function testUpdateProtocol($url, $expected) {
  318. $this->assertSame($expected,
  319. $this->invokePrivate($this->trustedServers, 'updateProtocol', [$url])
  320. );
  321. }
  322. public function dataTestUpdateProtocol() {
  323. return [
  324. ['http://owncloud.org', 'http://owncloud.org'],
  325. ['https://owncloud.org', 'https://owncloud.org'],
  326. ['owncloud.org', 'https://owncloud.org'],
  327. ['httpserver', 'https://httpserver'],
  328. ];
  329. }
  330. }