2015-10-29 19:27:14 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Bjoern Schiessle <bjoern@schiessle.org>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Björn Schießle <bjoern@schiessle.org>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2016-03-01 19:25:15 +03:00
|
|
|
* @author Thomas Müller <thomas.mueller@tmit.eu>
|
2015-10-29 19:27:14 +03:00
|
|
|
*
|
|
|
|
* @license AGPL-3.0
|
|
|
|
*
|
|
|
|
* This code is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License, version 3,
|
|
|
|
* as published by the Free Software Foundation.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU Affero General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Affero General Public License, version 3,
|
2019-12-03 21:57:53 +03:00
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
2015-10-29 19:27:14 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-05-09 16:37:51 +03:00
|
|
|
namespace OCA\Federation\Tests;
|
2015-10-29 19:27:14 +03:00
|
|
|
|
|
|
|
use OCA\Federation\DbHandler;
|
|
|
|
use OCA\Federation\TrustedServers;
|
2017-08-01 12:23:40 +03:00
|
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
2015-11-10 12:50:59 +03:00
|
|
|
use OCP\BackgroundJob\IJobList;
|
2015-10-29 19:27:14 +03:00
|
|
|
use OCP\Http\Client\IClient;
|
|
|
|
use OCP\Http\Client\IClientService;
|
|
|
|
use OCP\Http\Client\IResponse;
|
2015-11-10 12:50:59 +03:00
|
|
|
use OCP\IConfig;
|
2015-10-29 19:27:14 +03:00
|
|
|
use OCP\ILogger;
|
2015-11-10 12:50:59 +03:00
|
|
|
use OCP\Security\ISecureRandom;
|
2016-02-26 18:59:53 +03:00
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
|
2015-10-29 19:27:14 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class TrustedServersTest extends TestCase {
|
|
|
|
|
2016-05-09 16:37:51 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers */
|
2015-10-29 19:27:14 +03:00
|
|
|
private $trustedServers;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | DbHandler */
|
|
|
|
private $dbHandler;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | IClientService */
|
|
|
|
private $httpClientService;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | IClient */
|
|
|
|
private $httpClient;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | IResponse */
|
|
|
|
private $response;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | ILogger */
|
|
|
|
private $logger;
|
|
|
|
|
2015-11-10 12:50:59 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | IJobList */
|
|
|
|
private $jobList;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | ISecureRandom */
|
|
|
|
private $secureRandom;
|
|
|
|
|
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | IConfig */
|
|
|
|
private $config;
|
|
|
|
|
2016-02-26 18:59:53 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | EventDispatcherInterface */
|
|
|
|
private $dispatcher;
|
|
|
|
|
2017-08-01 12:23:40 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|ITimeFactory */
|
|
|
|
private $timeFactory;
|
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2015-10-29 19:27:14 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2016-08-31 21:54:17 +03:00
|
|
|
$this->dbHandler = $this->getMockBuilder(DbHandler::class)
|
2015-10-29 19:27:14 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-08-31 21:54:17 +03:00
|
|
|
$this->dispatcher = $this->getMockBuilder(EventDispatcherInterface::class)
|
2016-02-26 18:59:53 +03:00
|
|
|
->disableOriginalConstructor()->getMock();
|
2016-08-31 21:54:17 +03:00
|
|
|
$this->httpClientService = $this->getMockBuilder(IClientService::class)->getMock();
|
|
|
|
$this->httpClient = $this->getMockBuilder(IClient::class)->getMock();
|
|
|
|
$this->response = $this->getMockBuilder(IResponse::class)->getMock();
|
|
|
|
$this->logger = $this->getMockBuilder(ILogger::class)->getMock();
|
|
|
|
$this->jobList = $this->getMockBuilder(IJobList::class)->getMock();
|
|
|
|
$this->secureRandom = $this->getMockBuilder(ISecureRandom::class)->getMock();
|
|
|
|
$this->config = $this->getMockBuilder(IConfig::class)->getMock();
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->timeFactory = $this->createMock(ITimeFactory::class);
|
2015-10-29 19:27:14 +03:00
|
|
|
|
|
|
|
$this->trustedServers = new TrustedServers(
|
|
|
|
$this->dbHandler,
|
|
|
|
$this->httpClientService,
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->logger,
|
|
|
|
$this->jobList,
|
|
|
|
$this->secureRandom,
|
2016-02-26 18:59:53 +03:00
|
|
|
$this->config,
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->dispatcher,
|
|
|
|
$this->timeFactory
|
2015-10-29 19:27:14 +03:00
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2015-11-10 12:50:59 +03:00
|
|
|
/**
|
2015-11-19 19:49:43 +03:00
|
|
|
* @dataProvider dataTrueFalse
|
2015-11-10 12:50:59 +03:00
|
|
|
*
|
|
|
|
* @param bool $success
|
|
|
|
*/
|
|
|
|
public function testAddServer($success) {
|
2016-05-09 16:37:51 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject|TrustedServers $trustedServers */
|
2015-10-29 19:27:14 +03:00
|
|
|
$trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->dbHandler,
|
|
|
|
$this->httpClientService,
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->logger,
|
|
|
|
$this->jobList,
|
|
|
|
$this->secureRandom,
|
2016-02-26 18:59:53 +03:00
|
|
|
$this->config,
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->dispatcher,
|
|
|
|
$this->timeFactory
|
2015-10-29 19:27:14 +03:00
|
|
|
]
|
|
|
|
)
|
2017-08-01 12:23:40 +03:00
|
|
|
->setMethods(['normalizeUrl', 'updateProtocol'])
|
2015-10-29 19:27:14 +03:00
|
|
|
->getMock();
|
2015-11-10 12:50:59 +03:00
|
|
|
$trustedServers->expects($this->once())->method('updateProtocol')
|
|
|
|
->with('url')->willReturn('https://url');
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->timeFactory->method('getTime')
|
|
|
|
->willReturn(1234567);
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->dbHandler->expects($this->once())->method('addServer')->with('https://url')
|
|
|
|
->willReturn($success);
|
|
|
|
|
|
|
|
if ($success) {
|
|
|
|
$this->secureRandom->expects($this->once())->method('generate')
|
|
|
|
->willReturn('token');
|
|
|
|
$this->dbHandler->expects($this->once())->method('addToken')->with('https://url', 'token');
|
|
|
|
$this->jobList->expects($this->once())->method('add')
|
|
|
|
->with('OCA\Federation\BackgroundJob\RequestSharedSecret',
|
2017-07-28 15:50:40 +03:00
|
|
|
['url' => 'https://url', 'token' => 'token', 'created' => 1234567]);
|
2015-11-10 12:50:59 +03:00
|
|
|
} else {
|
|
|
|
$this->jobList->expects($this->never())->method('add');
|
|
|
|
}
|
2015-10-29 19:27:14 +03:00
|
|
|
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->assertSame($success,
|
2015-10-29 19:27:14 +03:00
|
|
|
$trustedServers->addServer('url')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:49:43 +03:00
|
|
|
public function dataTrueFalse() {
|
2015-11-10 12:50:59 +03:00
|
|
|
return [
|
|
|
|
[true],
|
|
|
|
[false]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:49:43 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTrueFalse
|
|
|
|
*
|
|
|
|
* @param bool $status
|
|
|
|
*/
|
|
|
|
public function testSetAutoAddServers($status) {
|
|
|
|
if ($status) {
|
|
|
|
$this->config->expects($this->once())->method('setAppValue')
|
|
|
|
->with('federation', 'autoAddServers', '1');
|
|
|
|
} else {
|
|
|
|
$this->config->expects($this->once())->method('setAppValue')
|
|
|
|
->with('federation', 'autoAddServers', '0');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->trustedServers->setAutoAddServers($status);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestGetAutoAddServers
|
|
|
|
*
|
|
|
|
* @param string $status
|
|
|
|
* @param bool $expected
|
|
|
|
*/
|
|
|
|
public function testGetAutoAddServers($status, $expected) {
|
|
|
|
$this->config->expects($this->once())->method('getAppValue')
|
2016-11-17 14:51:54 +03:00
|
|
|
->with('federation', 'autoAddServers', '0')->willReturn($status);
|
2015-11-19 19:49:43 +03:00
|
|
|
|
|
|
|
$this->assertSame($expected,
|
2016-05-09 16:37:51 +03:00
|
|
|
$this->trustedServers->getAutoAddServers()
|
2015-11-19 19:49:43 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestGetAutoAddServers() {
|
|
|
|
return [
|
|
|
|
['1', true],
|
|
|
|
['0', false]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddSharedSecret() {
|
|
|
|
$this->dbHandler->expects($this->once())->method('addSharedSecret')
|
|
|
|
->with('url', 'secret');
|
|
|
|
$this->trustedServers->addSharedSecret('url', 'secret');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSharedSecret() {
|
|
|
|
$this->dbHandler->expects($this->once())->method('getSharedSecret')
|
|
|
|
->with('url')->willReturn(true);
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->trustedServers->getSharedSecret('url')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-29 19:27:14 +03:00
|
|
|
public function testRemoveServer() {
|
|
|
|
$id = 42;
|
2016-02-26 18:59:53 +03:00
|
|
|
$server = ['url_hash' => 'url_hash'];
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->dbHandler->expects($this->once())->method('removeServer')->with($id);
|
2016-02-26 18:59:53 +03:00
|
|
|
$this->dbHandler->expects($this->once())->method('getServerById')->with($id)
|
|
|
|
->willReturn($server);
|
|
|
|
$this->dispatcher->expects($this->once())->method('dispatch')
|
|
|
|
->willReturnCallback(
|
|
|
|
function($eventId, $event) {
|
|
|
|
$this->assertSame($eventId, 'OCP\Federation\TrustedServerEvent::remove');
|
|
|
|
$this->assertInstanceOf('Symfony\Component\EventDispatcher\GenericEvent', $event);
|
2016-05-09 16:37:51 +03:00
|
|
|
/** @var \Symfony\Component\EventDispatcher\GenericEvent $event */
|
2016-02-26 18:59:53 +03:00
|
|
|
$this->assertSame('url_hash', $event->getSubject());
|
|
|
|
}
|
|
|
|
);
|
2015-10-29 19:27:14 +03:00
|
|
|
$this->trustedServers->removeServer($id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetServers() {
|
2016-05-09 16:37:51 +03:00
|
|
|
$this->dbHandler->expects($this->once())->method('getAllServer')->willReturn(['servers']);
|
2015-10-29 19:27:14 +03:00
|
|
|
|
2016-05-09 16:37:51 +03:00
|
|
|
$this->assertEquals(
|
|
|
|
['servers'],
|
2015-10-29 19:27:14 +03:00
|
|
|
$this->trustedServers->getServers()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public function testIsTrustedServer() {
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->dbHandler->expects($this->once())->method('serverExists')->with('url')
|
|
|
|
->willReturn(true);
|
2015-10-29 19:27:14 +03:00
|
|
|
|
|
|
|
$this->assertTrue(
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->trustedServers->isTrustedServer('url')
|
2015-10-29 19:27:14 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:49:43 +03:00
|
|
|
public function testSetServerStatus() {
|
|
|
|
$this->dbHandler->expects($this->once())->method('setServerStatus')
|
|
|
|
->with('url', 'status');
|
|
|
|
$this->trustedServers->setServerStatus('url', 'status');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetServerStatus() {
|
|
|
|
$this->dbHandler->expects($this->once())->method('getServerStatus')
|
|
|
|
->with('url')->willReturn(true);
|
|
|
|
$this->assertTrue(
|
|
|
|
$this->trustedServers->getServerStatus('url')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-10-29 19:27:14 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestIsOwnCloudServer
|
|
|
|
*
|
|
|
|
* @param int $statusCode
|
|
|
|
* @param bool $isValidOwnCloudVersion
|
|
|
|
* @param bool $expected
|
|
|
|
*/
|
|
|
|
public function testIsOwnCloudServer($statusCode, $isValidOwnCloudVersion, $expected) {
|
|
|
|
|
|
|
|
$server = 'server1';
|
|
|
|
|
2016-05-09 16:37:51 +03:00
|
|
|
/** @var \PHPUnit_Framework_MockObject_MockObject | TrustedServers $trustedServers */
|
2015-10-29 19:27:14 +03:00
|
|
|
$trustedServers = $this->getMockBuilder('OCA\Federation\TrustedServers')
|
|
|
|
->setConstructorArgs(
|
|
|
|
[
|
|
|
|
$this->dbHandler,
|
|
|
|
$this->httpClientService,
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->logger,
|
|
|
|
$this->jobList,
|
|
|
|
$this->secureRandom,
|
2016-02-26 18:59:53 +03:00
|
|
|
$this->config,
|
2017-08-01 12:23:40 +03:00
|
|
|
$this->dispatcher,
|
|
|
|
$this->timeFactory
|
2015-10-29 19:27:14 +03:00
|
|
|
]
|
|
|
|
)
|
|
|
|
->setMethods(['checkOwnCloudVersion'])
|
|
|
|
->getMock();
|
|
|
|
|
|
|
|
$this->httpClientService->expects($this->once())->method('newClient')
|
|
|
|
->willReturn($this->httpClient);
|
|
|
|
|
|
|
|
$this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
|
|
|
|
->willReturn($this->response);
|
|
|
|
|
|
|
|
$this->response->expects($this->once())->method('getStatusCode')
|
|
|
|
->willReturn($statusCode);
|
|
|
|
|
|
|
|
if ($statusCode === 200) {
|
|
|
|
$trustedServers->expects($this->once())->method('checkOwnCloudVersion')
|
|
|
|
->willReturn($isValidOwnCloudVersion);
|
|
|
|
} else {
|
|
|
|
$trustedServers->expects($this->never())->method('checkOwnCloudVersion');
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->assertSame($expected,
|
|
|
|
$trustedServers->isOwnCloudServer($server)
|
|
|
|
);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestIsOwnCloudServer() {
|
|
|
|
return [
|
|
|
|
[200, true, true],
|
|
|
|
[200, false, false],
|
|
|
|
[404, true, false],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2016-02-09 15:58:13 +03:00
|
|
|
/**
|
|
|
|
* @expectedExceptionMessage simulated exception
|
|
|
|
*/
|
2015-10-29 19:27:14 +03:00
|
|
|
public function testIsOwnCloudServerFail() {
|
|
|
|
$server = 'server1';
|
|
|
|
|
|
|
|
$this->httpClientService->expects($this->once())->method('newClient')
|
|
|
|
->willReturn($this->httpClient);
|
|
|
|
|
|
|
|
$this->httpClient->expects($this->once())->method('get')->with($server . '/status.php')
|
2015-11-10 12:50:59 +03:00
|
|
|
->willReturnCallback(function () {
|
2015-10-29 19:27:14 +03:00
|
|
|
throw new \Exception('simulated exception');
|
|
|
|
});
|
|
|
|
|
2016-08-08 15:56:24 +03:00
|
|
|
$this->assertFalse($this->trustedServers->isOwnCloudServer($server));
|
2015-10-29 19:27:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestCheckOwnCloudVersion
|
|
|
|
*/
|
2016-02-09 15:58:13 +03:00
|
|
|
public function testCheckOwnCloudVersion($status) {
|
|
|
|
$this->assertTrue($this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]));
|
2015-10-29 19:27:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestCheckOwnCloudVersion() {
|
|
|
|
return [
|
2016-02-09 15:58:13 +03:00
|
|
|
['{"version":"9.0.0"}'],
|
|
|
|
['{"version":"9.1.0"}']
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @dataProvider dataTestCheckOwnCloudVersionTooLow
|
|
|
|
*/
|
|
|
|
public function testCheckOwnCloudVersionTooLow($status) {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\OC\HintException::class);
|
|
|
|
$this->expectExceptionMessage('Remote server version is too low. 9.0 is required.');
|
|
|
|
|
2016-02-09 15:58:13 +03:00
|
|
|
$this->invokePrivate($this->trustedServers, 'checkOwnCloudVersion', [$status]);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestCheckOwnCloudVersionTooLow() {
|
|
|
|
return [
|
|
|
|
['{"version":"8.2.3"}'],
|
2015-10-29 19:27:14 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-10 12:50:59 +03:00
|
|
|
* @dataProvider dataTestUpdateProtocol
|
2015-10-29 19:27:14 +03:00
|
|
|
* @param string $url
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
2015-11-10 12:50:59 +03:00
|
|
|
public function testUpdateProtocol($url, $expected) {
|
2015-10-29 19:27:14 +03:00
|
|
|
$this->assertSame($expected,
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->invokePrivate($this->trustedServers, 'updateProtocol', [$url])
|
2015-10-29 19:27:14 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-10 12:50:59 +03:00
|
|
|
public function dataTestUpdateProtocol() {
|
2015-10-29 19:27:14 +03:00
|
|
|
return [
|
2015-11-10 12:50:59 +03:00
|
|
|
['http://owncloud.org', 'http://owncloud.org'],
|
|
|
|
['https://owncloud.org', 'https://owncloud.org'],
|
|
|
|
['owncloud.org', 'https://owncloud.org'],
|
|
|
|
['httpserver', 'https://httpserver'],
|
2015-10-29 19:27:14 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
}
|