TrustedServersTest.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388
  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 Joas Schilling <coding@schilljs.com>
  8. * @author Morris Jobke <hey@morrisjobke.de>
  9. * @author Roeland Jago Douma <roeland@famdouma.nl>
  10. * @author Thomas Müller <thomas.mueller@tmit.eu>
  11. *
  12. * @license AGPL-3.0
  13. *
  14. * This code is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU Affero General Public License, version 3,
  16. * as published by the Free Software Foundation.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU Affero General Public License for more details.
  22. *
  23. * You should have received a copy of the GNU Affero General Public License, version 3,
  24. * along with this program. If not, see <http://www.gnu.org/licenses/>
  25. *
  26. */
  27. namespace OCA\Federation\Tests;
  28. use OCA\Federation\DbHandler;
  29. use OCA\Federation\TrustedServers;
  30. use OCP\AppFramework\Utility\ITimeFactory;
  31. use OCP\BackgroundJob\IJobList;
  32. use OCP\Http\Client\IClient;
  33. use OCP\Http\Client\IClientService;
  34. use OCP\Http\Client\IResponse;
  35. use OCP\IConfig;
  36. use OCP\ILogger;
  37. use OCP\Security\ISecureRandom;
  38. use Symfony\Component\EventDispatcher\EventDispatcherInterface;
  39. use Test\TestCase;
  40. class TrustedServersTest extends TestCase {
  41. /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */
  42. private $trustedServers;
  43. /** @var \PHPUnit_Framework_MockObject_MockObject | DbHandler */
  44. private $dbHandler;
  45. /** @var \PHPUnit_Framework_MockObject_MockObject | IClientService */
  46. private $httpClientService;
  47. /** @var \PHPUnit_Framework_MockObject_MockObject | IClient */
  48. private $httpClient;
  49. /** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */
  50. private $response;
  51. /** @var \PHPUnit_Framework_MockObject_MockObject | ILogger */
  52. private $logger;
  53. /** @var \PHPUnit_Framework_MockObject_MockObject | IJobList */
  54. private $jobList;
  55. /** @var \PHPUnit_Framework_MockObject_MockObject | ISecureRandom */
  56. private $secureRandom;
  57. /** @var \PHPUnit_Framework_MockObject_MockObject | IConfig */
  58. private $config;
  59. /** @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcherInterface */
  60. private $dispatcher;
  61. /** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
  62. private $timeFactory;
  63. public function setUp() {
  64. parent::setUp();
  65. $this->dbHandler = $this->getMockBuilder(DbHandler::class)
  66. ->disableOriginalConstructor()->getMock();
  67. $this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
  68. ->disableOriginalConstructor()->getMock();
  69. $this->httpClientService = $this->getMockBuilder(IClientService::class)->getMock();
  70. $this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
  71. $this->response = $this->getMockBuilder(IResponse::class)->getMock();
  72. $this->logger = $this->getMockBuilder(ILogger::class)->getMock();
  73. $this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
  74. $this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock();
  75. $this->config = $this->getMockBuilder(IConfig::class)->getMock();
  76. $this->timeFactory = $this->createMock(ITimeFactory::class);
  77. $this->trustedServers = new TrustedServers(
  78. $this->dbHandler,
  79. $this->httpClientService,
  80. $this->logger,
  81. $this->jobList,
  82. $this->secureRandom,
  83. $this->config,
  84. $this->dispatcher,
  85. $this->timeFactory
  86. );
  87. }
  88. /**
  89. * @dataProvider dataTrueFalse
  90. *
  91. * @param bool $success
  92. */
  93. public function testAddServer($success) {
  94. /** @var \PHPUnit_Framework_MockObject_MockObject|TrustedServers $trustedServers */
  95. $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
  96. ->setConstructorArgs(
  97. [
  98. $this->dbHandler,
  99. $this->httpClientService,
  100. $this->logger,
  101. $this->jobList,
  102. $this->secureRandom,
  103. $this->config,
  104. $this->dispatcher,
  105. $this->timeFactory
  106. ]
  107. )
  108. ->setMethods(['normalizeUrl', 'updateProtocol'])
  109. ->getMock();
  110. $trustedServers->expects($this->once())->method('updateProtocol')
  111. ->with('url')->willReturn('https://url');
  112. $this->timeFactory->method('getTime')
  113. ->willReturn(1234567);
  114. $this->dbHandler->expects($this->once())->method('addServer')->with('https://url')
  115. ->willReturn($success);
  116. if ($success) {
  117. $this->secureRandom->expects($this->once())->method('generate')
  118. ->willReturn('token');
  119. $this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token');
  120. $this->jobList->expects($this->once())->method('add')
  121. ->with('OCA\Federation\BackgroundJob\RequestSharedSecret',
  122. ['url' => 'https://url', 'token' => 'token', 'created' => 1234567]);
  123. } else {
  124. $this->jobList->expects($this->never())->method('add');
  125. }
  126. $this->assertSame($success,
  127. $trustedServers->addServer('url')
  128. );
  129. }
  130. public function dataTrueFalse() {
  131. return [
  132. [true],
  133. [false]
  134. ];
  135. }
  136. /**
  137. * @dataProvider dataTrueFalse
  138. *
  139. * @param bool $status
  140. */
  141. public function testSetAutoAddServers($status) {
  142. if ($status) {
  143. $this->config->expects($this->once())->method('setAppValue')
  144. ->with('federation', 'autoAddServers', '1');
  145. } else {
  146. $this->config->expects($this->once())->method('setAppValue')
  147. ->with('federation', 'autoAddServers', '0');
  148. }
  149. $this->trustedServers->setAutoAddServers($status);
  150. }
  151. /**
  152. * @dataProvider dataTestGetAutoAddServers
  153. *
  154. * @param string $status
  155. * @param bool $expected
  156. */
  157. public function testGetAutoAddServers($status, $expected) {
  158. $this->config->expects($this->once())->method('getAppValue')
  159. ->with('federation', 'autoAddServers', '0')->willReturn($status);
  160. $this->assertSame($expected,
  161. $this->trustedServers->getAutoAddServers()
  162. );
  163. }
  164. public function dataTestGetAutoAddServers() {
  165. return [
  166. ['1', true],
  167. ['0', false]
  168. ];
  169. }
  170. public function testAddSharedSecret() {
  171. $this->dbHandler->expects($this->once())->method('addSharedSecret')
  172. ->with('url', 'secret');
  173. $this->trustedServers->addSharedSecret('url', 'secret');
  174. }
  175. public function testGetSharedSecret() {
  176. $this->dbHandler->expects($this->once())->method('getSharedSecret')
  177. ->with('url')->willReturn(true);
  178. $this->assertTrue(
  179. $this->trustedServers->getSharedSecret('url')
  180. );
  181. }
  182. public function testRemoveServer() {
  183. $id = 42;
  184. $server = ['url_hash' => 'url_hash'];
  185. $this->dbHandler->expects($this->once())->method('removeServer')->with($id);
  186. $this->dbHandler->expects($this->once())->method('getServerById')->with($id)
  187. ->willReturn($server);
  188. $this->dispatcher->expects($this->once())->method('dispatch')
  189. ->willReturnCallback(
  190. function($eventId, $event) {
  191. $this->assertSame($eventId, 'OCP\Federation\TrustedServerEvent::remove');
  192. $this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
  193. /** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
  194. $this->assertSame('url_hash', $event->getSubject());
  195. }
  196. );
  197. $this->trustedServers->removeServer($id);
  198. }
  199. public function testGetServers() {
  200. $this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(['servers']);
  201. $this->assertEquals(
  202. ['servers'],
  203. $this->trustedServers->getServers()
  204. );
  205. }
  206. public function testIsTrustedServer() {
  207. $this->dbHandler->expects($this->once())->method('serverExists')->with('url')
  208. ->willReturn(true);
  209. $this->assertTrue(
  210. $this->trustedServers->isTrustedServer('url')
  211. );
  212. }
  213. public function testSetServerStatus() {
  214. $this->dbHandler->expects($this->once())->method('setServerStatus')
  215. ->with('url', 'status');
  216. $this->trustedServers->setServerStatus('url', 'status');
  217. }
  218. public function testGetServerStatus() {
  219. $this->dbHandler->expects($this->once())->method('getServerStatus')
  220. ->with('url')->willReturn(true);
  221. $this->assertTrue(
  222. $this->trustedServers->getServerStatus('url')
  223. );
  224. }
  225. /**
  226. * @dataProvider dataTestIsOwnCloudServer
  227. *
  228. * @param int $statusCode
  229. * @param bool $isValidOwnCloudVersion
  230. * @param bool $expected
  231. */
  232. public function testIsOwnCloudServer($statusCode, $isValidOwnCloudVersion, $expected) {
  233. $server = 'server1';
  234. /** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers $trustedServers */
  235. $trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
  236. ->setConstructorArgs(
  237. [
  238. $this->dbHandler,
  239. $this->httpClientService,
  240. $this->logger,
  241. $this->jobList,
  242. $this->secureRandom,
  243. $this->config,
  244. $this->dispatcher,
  245. $this->timeFactory
  246. ]
  247. )
  248. ->setMethods(['checkOwnCloudVersion'])
  249. ->getMock();
  250. $this->httpClientService->expects($this->once())->method('newClient')
  251. ->willReturn($this->httpClient);
  252. $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
  253. ->willReturn($this->response);
  254. $this->response->expects($this->once())->method('getStatusCode')
  255. ->willReturn($statusCode);
  256. if ($statusCode === 200) {
  257. $trustedServers->expects($this->once())->method('checkOwnCloudVersion')
  258. ->willReturn($isValidOwnCloudVersion);
  259. } else {
  260. $trustedServers->expects($this->never())->method('checkOwnCloudVersion');
  261. }
  262. $this->assertSame($expected,
  263. $trustedServers->isOwnCloudServer($server)
  264. );
  265. }
  266. public function dataTestIsOwnCloudServer() {
  267. return [
  268. [200, true, true],
  269. [200, false, false],
  270. [404, true, false],
  271. ];
  272. }
  273. /**
  274. * @expectedExceptionMessage simulated exception
  275. */
  276. public function testIsOwnCloudServerFail() {
  277. $server = 'server1';
  278. $this->httpClientService->expects($this->once())->method('newClient')
  279. ->willReturn($this->httpClient);
  280. $this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
  281. ->willReturnCallback(function () {
  282. throw new \Exception('simulated exception');
  283. });
  284. $this->assertFalse($this->trustedServers->isOwnCloudServer($server));
  285. }
  286. /**
  287. * @dataProvider dataTestCheckOwnCloudVersion
  288. */
  289. public function testCheckOwnCloudVersion($status) {
  290. $this->assertTrue($this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]));
  291. }
  292. public function dataTestCheckOwnCloudVersion() {
  293. return [
  294. ['{"version":"9.0.0"}'],
  295. ['{"version":"9.1.0"}']
  296. ];
  297. }
  298. /**
  299. * @dataProvider dataTestCheckOwnCloudVersionTooLow
  300. * @expectedException \OC\HintException
  301. * @expectedExceptionMessage Remote server version is too low. 9.0 is required.
  302. */
  303. public function testCheckOwnCloudVersionTooLow($status) {
  304. $this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]);
  305. }
  306. public function dataTestCheckOwnCloudVersionTooLow() {
  307. return [
  308. ['{"version":"8.2.3"}'],
  309. ];
  310. }
  311. /**
  312. * @dataProvider dataTestUpdateProtocol
  313. * @param string $url
  314. * @param string $expected
  315. */
  316. public function testUpdateProtocol($url, $expected) {
  317. $this->assertSame($expected,
  318. $this->invokePrivate($this->trustedServers, 'updateProtocol', [$url])
  319. );
  320. }
  321. public function dataTestUpdateProtocol() {
  322. return [
  323. ['http://owncloud.org', 'http://owncloud.org'],
  324. ['https://owncloud.org', 'https://owncloud.org'],
  325. ['owncloud.org', 'https://owncloud.org'],
  326. ['httpserver', 'https://httpserver'],
  327. ];
  328. }
  329. }