2015-10-29 19:27:14 +03:00
|
|
|
<?php
|
|
|
|
/**
|
2016-07-21 17:49:16 +03:00
|
|
|
* @copyright Copyright (c) 2016, ownCloud, Inc.
|
|
|
|
*
|
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,
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
2016-05-09 16:37:51 +03:00
|
|
|
namespace OCA\Federation\Tests;
|
2015-10-29 19:27:14 +03:00
|
|
|
|
|
|
|
|
|
|
|
use OCA\Federation\DbHandler;
|
2015-11-19 19:49:43 +03:00
|
|
|
use OCA\Federation\TrustedServers;
|
2015-10-29 19:27:14 +03:00
|
|
|
use OCP\IDBConnection;
|
2015-12-07 17:28:06 +03:00
|
|
|
use OCP\IL10N;
|
2015-10-29 19:27:14 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
2015-11-10 12:50:59 +03:00
|
|
|
/**
|
|
|
|
* @group DB
|
|
|
|
*/
|
2015-10-29 19:27:14 +03:00
|
|
|
class DbHandlerTest extends TestCase {
|
|
|
|
|
|
|
|
/** @var DbHandler */
|
|
|
|
private $dbHandler;
|
|
|
|
|
2015-12-07 17:28:06 +03:00
|
|
|
/** @var IL10N | \PHPUnit_Framework_MockObject_MockObject */
|
2015-10-29 19:27:14 +03:00
|
|
|
private $il10n;
|
|
|
|
|
|
|
|
/** @var IDBConnection */
|
|
|
|
private $connection;
|
|
|
|
|
|
|
|
/** @var string */
|
|
|
|
private $dbTable = 'trusted_servers';
|
|
|
|
|
|
|
|
public function setUp() {
|
|
|
|
parent::setUp();
|
|
|
|
|
|
|
|
$this->connection = \OC::$server->getDatabaseConnection();
|
2016-08-31 21:54:17 +03:00
|
|
|
$this->il10n = $this->getMockBuilder(IL10N::class)->getMock();
|
2015-10-29 19:27:14 +03:00
|
|
|
|
|
|
|
$this->dbHandler = new DbHandler(
|
|
|
|
$this->connection,
|
|
|
|
$this->il10n
|
|
|
|
);
|
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
$this->assertEmpty($result, 'we need to start with a empty trusted_servers table');
|
|
|
|
}
|
|
|
|
|
|
|
|
public function tearDown() {
|
|
|
|
parent::tearDown();
|
|
|
|
$query = $this->connection->getQueryBuilder()->delete($this->dbTable);
|
|
|
|
$query->execute();
|
|
|
|
}
|
|
|
|
|
2015-11-24 15:07:40 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestAddServer
|
|
|
|
*
|
|
|
|
* @param string $url passed to the method
|
|
|
|
* @param string $expectedUrl the url we expect to be written to the db
|
|
|
|
* @param string $expectedHash the hash value we expect to be written to the db
|
|
|
|
*/
|
|
|
|
public function testAddServer($url, $expectedUrl, $expectedHash) {
|
|
|
|
$id = $this->dbHandler->addServer($url);
|
2015-10-29 19:27:14 +03:00
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
$this->assertSame(1, count($result));
|
2015-11-24 15:07:40 +03:00
|
|
|
$this->assertSame($expectedUrl, $result[0]['url']);
|
2015-11-19 19:49:43 +03:00
|
|
|
$this->assertSame($id, (int)$result[0]['id']);
|
2015-11-24 15:07:40 +03:00
|
|
|
$this->assertSame($expectedHash, $result[0]['url_hash']);
|
2015-11-19 19:49:43 +03:00
|
|
|
$this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
|
2015-10-29 19:27:14 +03:00
|
|
|
}
|
|
|
|
|
2015-11-24 15:07:40 +03:00
|
|
|
public function dataTestAddServer() {
|
|
|
|
return [
|
2016-02-26 19:02:13 +03:00
|
|
|
['http://owncloud.org', 'http://owncloud.org', sha1('owncloud.org')],
|
|
|
|
['https://owncloud.org', 'https://owncloud.org', sha1('owncloud.org')],
|
|
|
|
['http://owncloud.org/', 'http://owncloud.org', sha1('owncloud.org')],
|
2015-11-24 15:07:40 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-10-29 19:27:14 +03:00
|
|
|
public function testRemove() {
|
2015-11-10 12:50:59 +03:00
|
|
|
$id1 = $this->dbHandler->addServer('server1');
|
|
|
|
$id2 = $this->dbHandler->addServer('server2');
|
2015-10-29 19:27:14 +03:00
|
|
|
|
|
|
|
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
$this->assertSame(2, count($result));
|
|
|
|
$this->assertSame('server1', $result[0]['url']);
|
|
|
|
$this->assertSame('server2', $result[1]['url']);
|
2015-11-19 19:49:43 +03:00
|
|
|
$this->assertSame($id1, (int)$result[0]['id']);
|
|
|
|
$this->assertSame($id2, (int)$result[1]['id']);
|
2015-10-29 19:27:14 +03:00
|
|
|
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->dbHandler->removeServer($id2);
|
2015-10-29 19:27:14 +03:00
|
|
|
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
$this->assertSame(1, count($result));
|
|
|
|
$this->assertSame('server1', $result[0]['url']);
|
2015-11-19 19:49:43 +03:00
|
|
|
$this->assertSame($id1, (int)$result[0]['id']);
|
2015-10-29 19:27:14 +03:00
|
|
|
}
|
|
|
|
|
2016-02-26 18:59:53 +03:00
|
|
|
|
|
|
|
public function testGetServerById() {
|
|
|
|
$this->dbHandler->addServer('server1');
|
|
|
|
$id = $this->dbHandler->addServer('server2');
|
|
|
|
|
|
|
|
$result = $this->dbHandler->getServerById($id);
|
|
|
|
$this->assertSame('server2', $result['url']);
|
|
|
|
}
|
|
|
|
|
2015-10-29 19:27:14 +03:00
|
|
|
public function testGetAll() {
|
2015-11-10 12:50:59 +03:00
|
|
|
$id1 = $this->dbHandler->addServer('server1');
|
|
|
|
$id2 = $this->dbHandler->addServer('server2');
|
2015-10-29 19:27:14 +03:00
|
|
|
|
2015-11-10 12:50:59 +03:00
|
|
|
$result = $this->dbHandler->getAllServer();
|
2015-10-29 19:27:14 +03:00
|
|
|
$this->assertSame(2, count($result));
|
|
|
|
$this->assertSame('server1', $result[0]['url']);
|
|
|
|
$this->assertSame('server2', $result[1]['url']);
|
2015-11-19 19:49:43 +03:00
|
|
|
$this->assertSame($id1, (int)$result[0]['id']);
|
|
|
|
$this->assertSame($id2, (int)$result[1]['id']);
|
2015-10-29 19:27:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2015-11-19 19:49:43 +03:00
|
|
|
* @dataProvider dataTestServerExists
|
2015-10-29 19:27:14 +03:00
|
|
|
*
|
|
|
|
* @param string $serverInTable
|
|
|
|
* @param string $checkForServer
|
|
|
|
* @param bool $expected
|
|
|
|
*/
|
2015-11-19 19:49:43 +03:00
|
|
|
public function testServerExists($serverInTable, $checkForServer, $expected) {
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->dbHandler->addServer($serverInTable);
|
2015-10-29 19:27:14 +03:00
|
|
|
$this->assertSame($expected,
|
2015-11-10 12:50:59 +03:00
|
|
|
$this->dbHandler->serverExists($checkForServer)
|
2015-10-29 19:27:14 +03:00
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:49:43 +03:00
|
|
|
public function dataTestServerExists() {
|
2015-10-29 19:27:14 +03:00
|
|
|
return [
|
|
|
|
['server1', 'server1', true],
|
2015-11-19 19:49:43 +03:00
|
|
|
['server1', 'http://server1', true],
|
2015-10-29 19:27:14 +03:00
|
|
|
['server1', 'server2', false]
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-11-19 19:49:43 +03:00
|
|
|
public function testAddToken() {
|
|
|
|
$this->dbHandler->addServer('server1');
|
|
|
|
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
$this->assertSame(1, count($result));
|
|
|
|
$this->assertSame(null, $result[0]['token']);
|
|
|
|
$this->dbHandler->addToken('http://server1', 'token');
|
|
|
|
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
$this->assertSame(1, count($result));
|
|
|
|
$this->assertSame('token', $result[0]['token']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetToken() {
|
|
|
|
$this->dbHandler->addServer('server1');
|
|
|
|
$this->dbHandler->addToken('http://server1', 'token');
|
|
|
|
$this->assertSame('token',
|
|
|
|
$this->dbHandler->getToken('https://server1')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testAddSharedSecret() {
|
|
|
|
$this->dbHandler->addServer('server1');
|
|
|
|
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
$this->assertSame(1, count($result));
|
|
|
|
$this->assertSame(null, $result[0]['shared_secret']);
|
|
|
|
$this->dbHandler->addSharedSecret('http://server1', 'secret');
|
|
|
|
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
$this->assertSame(1, count($result));
|
|
|
|
$this->assertSame('secret', $result[0]['shared_secret']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetSharedSecret() {
|
|
|
|
$this->dbHandler->addServer('server1');
|
|
|
|
$this->dbHandler->addSharedSecret('http://server1', 'secret');
|
|
|
|
$this->assertSame('secret',
|
|
|
|
$this->dbHandler->getSharedSecret('https://server1')
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetServerStatus() {
|
|
|
|
$this->dbHandler->addServer('server1');
|
|
|
|
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
$this->assertSame(1, count($result));
|
|
|
|
$this->assertSame(TrustedServers::STATUS_PENDING, (int)$result[0]['status']);
|
|
|
|
$this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
|
|
|
|
$query = $this->connection->getQueryBuilder()->select('*')->from($this->dbTable);
|
|
|
|
$result = $query->execute()->fetchAll();
|
|
|
|
$this->assertSame(1, count($result));
|
|
|
|
$this->assertSame(TrustedServers::STATUS_OK, (int)$result[0]['status']);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetServerStatus() {
|
|
|
|
$this->dbHandler->addServer('server1');
|
|
|
|
$this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK);
|
|
|
|
$this->assertSame(TrustedServers::STATUS_OK,
|
|
|
|
$this->dbHandler->getServerStatus('https://server1')
|
|
|
|
);
|
2015-12-07 17:28:06 +03:00
|
|
|
|
|
|
|
// test sync token
|
|
|
|
$this->dbHandler->setServerStatus('http://server1', TrustedServers::STATUS_OK, 'token1234567890');
|
|
|
|
$servers = $this->dbHandler->getAllServer();
|
|
|
|
$this->assertSame('token1234567890', $servers[0]['sync_token']);
|
2015-11-19 19:49:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* hash should always be computed with the normalized URL
|
|
|
|
*
|
|
|
|
* @dataProvider dataTestHash
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testHash($url, $expected) {
|
|
|
|
$this->assertSame($expected,
|
|
|
|
$this->invokePrivate($this->dbHandler, 'hash', [$url])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestHash() {
|
|
|
|
return [
|
2016-02-26 19:02:13 +03:00
|
|
|
['server1', sha1('server1')],
|
|
|
|
['http://server1', sha1('server1')],
|
|
|
|
['https://server1', sha1('server1')],
|
|
|
|
['http://server1/', sha1('server1')],
|
2015-11-19 19:49:43 +03:00
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-11-10 12:50:59 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider dataTestNormalizeUrl
|
|
|
|
*
|
|
|
|
* @param string $url
|
|
|
|
* @param string $expected
|
|
|
|
*/
|
|
|
|
public function testNormalizeUrl($url, $expected) {
|
|
|
|
$this->assertSame($expected,
|
|
|
|
$this->invokePrivate($this->dbHandler, 'normalizeUrl', [$url])
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function dataTestNormalizeUrl() {
|
|
|
|
return [
|
|
|
|
['owncloud.org', 'owncloud.org'],
|
|
|
|
['http://owncloud.org', 'owncloud.org'],
|
|
|
|
['https://owncloud.org', 'owncloud.org'],
|
|
|
|
['https://owncloud.org//mycloud', 'owncloud.org/mycloud'],
|
|
|
|
['https://owncloud.org/mycloud/', 'owncloud.org/mycloud'],
|
|
|
|
];
|
|
|
|
}
|
|
|
|
|
2015-12-07 17:28:06 +03:00
|
|
|
/**
|
|
|
|
* @dataProvider providesAuth
|
|
|
|
*/
|
|
|
|
public function testAuth($expectedResult, $user, $password) {
|
|
|
|
if ($expectedResult) {
|
|
|
|
$this->dbHandler->addServer('url1');
|
|
|
|
$this->dbHandler->addSharedSecret('url1', $password);
|
|
|
|
}
|
|
|
|
$result = $this->dbHandler->auth($user, $password);
|
|
|
|
$this->assertEquals($expectedResult, $result);
|
|
|
|
}
|
|
|
|
|
|
|
|
public function providesAuth() {
|
|
|
|
return [
|
|
|
|
[false, 'foo', ''],
|
|
|
|
[true, 'system', '123456789'],
|
|
|
|
];
|
|
|
|
}
|
2015-10-29 19:27:14 +03:00
|
|
|
}
|