2016-11-18 12:10:05 +03:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @copyright Copyright (c) 2016 Lukas Reschke <lukas@statuscode.ch>
|
|
|
|
*
|
|
|
|
* @license GNU AGPL version 3 or any later version
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU Affero General Public License as
|
|
|
|
* published by the Free Software Foundation, either version 3 of the
|
|
|
|
* License, or (at your option) any later version.
|
|
|
|
*
|
|
|
|
* 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
|
|
|
|
* along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
2016-11-22 16:53:09 +03:00
|
|
|
namespace Test\Security\IdentityProof;
|
2016-11-18 12:10:05 +03:00
|
|
|
|
2018-05-08 21:59:31 +03:00
|
|
|
use OC\Files\AppData\AppData;
|
2017-05-10 10:44:28 +03:00
|
|
|
use OC\Files\AppData\Factory;
|
2016-11-18 12:10:05 +03:00
|
|
|
use OC\Security\IdentityProof\Key;
|
|
|
|
use OC\Security\IdentityProof\Manager;
|
|
|
|
use OCP\Files\IAppData;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFile;
|
|
|
|
use OCP\Files\SimpleFS\ISimpleFolder;
|
2017-07-25 12:54:14 +03:00
|
|
|
use OCP\IConfig;
|
2019-09-09 21:39:19 +03:00
|
|
|
use OCP\ILogger;
|
2016-11-18 12:10:05 +03:00
|
|
|
use OCP\IUser;
|
|
|
|
use OCP\Security\ICrypto;
|
2019-09-09 21:39:19 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2016-11-18 12:10:05 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
2020-04-10 15:19:56 +03:00
|
|
|
class ManagerTest extends TestCase {
|
2019-09-09 21:39:19 +03:00
|
|
|
/** @var Factory|MockObject */
|
2017-05-10 10:44:28 +03:00
|
|
|
private $factory;
|
2019-09-09 21:39:19 +03:00
|
|
|
/** @var IAppData|MockObject */
|
2016-11-18 12:10:05 +03:00
|
|
|
private $appData;
|
2019-09-09 21:39:19 +03:00
|
|
|
/** @var ICrypto|MockObject */
|
2016-11-18 12:10:05 +03:00
|
|
|
private $crypto;
|
2019-09-09 21:39:19 +03:00
|
|
|
/** @var Manager|MockObject */
|
2016-11-18 12:10:05 +03:00
|
|
|
private $manager;
|
2019-09-09 21:39:19 +03:00
|
|
|
/** @var IConfig|MockObject */
|
2017-07-25 12:54:14 +03:00
|
|
|
private $config;
|
2019-09-09 21:39:19 +03:00
|
|
|
/** @var ILogger|MockObject */
|
|
|
|
private $logger;
|
2016-11-18 12:10:05 +03:00
|
|
|
|
2019-11-27 17:27:18 +03:00
|
|
|
protected function setUp(): void {
|
2016-11-18 12:10:05 +03:00
|
|
|
parent::setUp();
|
2017-05-10 10:44:28 +03:00
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Factory|\PHPUnit\Framework\MockObject\MockObject $factory */
|
2017-05-10 10:44:28 +03:00
|
|
|
$this->factory = $this->createMock(Factory::class);
|
2018-05-08 21:59:31 +03:00
|
|
|
$this->appData = $this->createMock(AppData::class);
|
2017-07-25 12:54:14 +03:00
|
|
|
$this->config = $this->createMock(IConfig::class);
|
2017-05-10 10:44:28 +03:00
|
|
|
$this->factory->expects($this->any())
|
|
|
|
->method('get')
|
|
|
|
->with('identityproof')
|
|
|
|
->willReturn($this->appData);
|
2019-09-09 21:39:19 +03:00
|
|
|
$this->logger = $this->createMock(ILogger::class);
|
2017-05-10 10:44:28 +03:00
|
|
|
|
2016-11-18 12:10:05 +03:00
|
|
|
$this->crypto = $this->createMock(ICrypto::class);
|
2017-07-25 12:54:14 +03:00
|
|
|
$this->manager = $this->getManager(['generateKeyPair']);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* create manager object
|
|
|
|
*
|
|
|
|
* @param array $setMethods
|
2020-08-11 22:32:18 +03:00
|
|
|
* @return Manager|\PHPUnit\Framework\MockObject\MockObject
|
2017-07-25 12:54:14 +03:00
|
|
|
*/
|
|
|
|
protected function getManager($setMethods = []) {
|
|
|
|
if (empty($setMethods)) {
|
|
|
|
return new Manager(
|
2017-05-10 10:44:28 +03:00
|
|
|
$this->factory,
|
2017-07-25 12:54:14 +03:00
|
|
|
$this->crypto,
|
2019-09-09 21:39:19 +03:00
|
|
|
$this->config,
|
|
|
|
$this->logger
|
2017-07-25 12:54:14 +03:00
|
|
|
);
|
|
|
|
} else {
|
|
|
|
return $this->getMockBuilder(Manager::class)
|
|
|
|
->setConstructorArgs([
|
|
|
|
$this->factory,
|
|
|
|
$this->crypto,
|
2019-09-09 21:39:19 +03:00
|
|
|
$this->config,
|
|
|
|
$this->logger
|
2017-07-25 12:54:14 +03:00
|
|
|
])->setMethods($setMethods)->getMock();
|
|
|
|
}
|
2016-11-18 12:10:05 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetKeyWithExistingKey() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('MyUid');
|
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$privateFile = $this->createMock(ISimpleFile::class);
|
|
|
|
$privateFile
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn('EncryptedPrivateKey');
|
|
|
|
$publicFile = $this->createMock(ISimpleFile::class);
|
|
|
|
$publicFile
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getContent')
|
|
|
|
->willReturn('MyPublicKey');
|
|
|
|
$this->crypto
|
|
|
|
->expects($this->once())
|
|
|
|
->method('decrypt')
|
|
|
|
->with('EncryptedPrivateKey')
|
|
|
|
->willReturn('MyPrivateKey');
|
|
|
|
$folder
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getFile')
|
|
|
|
->with('private')
|
|
|
|
->willReturn($privateFile);
|
|
|
|
$folder
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('getFile')
|
|
|
|
->with('public')
|
|
|
|
->willReturn($publicFile);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->once())
|
|
|
|
->method('getFolder')
|
2017-07-27 17:52:28 +03:00
|
|
|
->with('user-MyUid')
|
2016-11-18 12:10:05 +03:00
|
|
|
->willReturn($folder);
|
|
|
|
|
|
|
|
$expected = new Key('MyPublicKey', 'MyPrivateKey');
|
|
|
|
$this->assertEquals($expected, $this->manager->getKey($user));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGetKeyWithNotExistingKey() {
|
|
|
|
$user = $this->createMock(IUser::class);
|
|
|
|
$user
|
2017-07-25 12:54:14 +03:00
|
|
|
->expects($this->once())
|
2016-11-18 12:10:05 +03:00
|
|
|
->method('getUID')
|
|
|
|
->willReturn('MyUid');
|
|
|
|
$this->appData
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('getFolder')
|
2017-07-27 17:52:28 +03:00
|
|
|
->with('user-MyUid')
|
2016-11-18 12:10:05 +03:00
|
|
|
->willThrowException(new \Exception());
|
|
|
|
$this->manager
|
|
|
|
->expects($this->once())
|
|
|
|
->method('generateKeyPair')
|
|
|
|
->willReturn(['MyNewPublicKey', 'MyNewPrivateKey']);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('newFolder')
|
2017-07-27 17:52:28 +03:00
|
|
|
->with('user-MyUid');
|
2016-11-18 12:10:05 +03:00
|
|
|
$folder = $this->createMock(ISimpleFolder::class);
|
|
|
|
$this->crypto
|
|
|
|
->expects($this->once())
|
|
|
|
->method('encrypt')
|
|
|
|
->with('MyNewPrivateKey')
|
|
|
|
->willReturn('MyNewEncryptedPrivateKey');
|
|
|
|
$privateFile = $this->createMock(ISimpleFile::class);
|
|
|
|
$privateFile
|
|
|
|
->expects($this->once())
|
|
|
|
->method('putContent')
|
|
|
|
->with('MyNewEncryptedPrivateKey');
|
|
|
|
$publicFile = $this->createMock(ISimpleFile::class);
|
|
|
|
$publicFile
|
|
|
|
->expects($this->once())
|
|
|
|
->method('putContent')
|
|
|
|
->with('MyNewPublicKey');
|
|
|
|
$folder
|
|
|
|
->expects($this->at(0))
|
|
|
|
->method('newFile')
|
|
|
|
->with('private')
|
|
|
|
->willReturn($privateFile);
|
|
|
|
$folder
|
|
|
|
->expects($this->at(1))
|
|
|
|
->method('newFile')
|
|
|
|
->with('public')
|
|
|
|
->willReturn($publicFile);
|
|
|
|
$this->appData
|
|
|
|
->expects($this->at(2))
|
|
|
|
->method('getFolder')
|
2017-07-27 17:52:28 +03:00
|
|
|
->with('user-MyUid')
|
2016-11-18 12:10:05 +03:00
|
|
|
->willReturn($folder);
|
|
|
|
|
|
|
|
|
|
|
|
$expected = new Key('MyNewPublicKey', 'MyNewPrivateKey');
|
|
|
|
$this->assertEquals($expected, $this->manager->getKey($user));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testGenerateKeyPair() {
|
2017-07-25 12:54:14 +03:00
|
|
|
$manager = $this->getManager();
|
2016-11-18 12:10:05 +03:00
|
|
|
$data = 'MyTestData';
|
|
|
|
|
2017-05-10 10:44:28 +03:00
|
|
|
list($resultPublicKey, $resultPrivateKey) = self::invokePrivate($manager, 'generateKeyPair');
|
2016-11-18 12:10:05 +03:00
|
|
|
openssl_sign($data, $signature, $resultPrivateKey);
|
|
|
|
$details = openssl_pkey_get_details(openssl_pkey_get_public($resultPublicKey));
|
|
|
|
|
|
|
|
$this->assertSame(1, openssl_verify($data, $signature, $resultPublicKey));
|
|
|
|
$this->assertSame(2048, $details['bits']);
|
|
|
|
}
|
2017-07-25 12:54:14 +03:00
|
|
|
|
|
|
|
public function testGetSystemKey() {
|
|
|
|
$manager = $this->getManager(['retrieveKey']);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Key|\PHPUnit\Framework\MockObject\MockObject $key */
|
2017-07-25 12:54:14 +03:00
|
|
|
$key = $this->createMock(Key::class);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())->method('getSystemValue')
|
|
|
|
->with('instanceid', null)->willReturn('instanceId');
|
|
|
|
|
2017-07-27 17:52:28 +03:00
|
|
|
$manager->expects($this->once())->method('retrieveKey')->with('system-instanceId')
|
2017-07-25 12:54:14 +03:00
|
|
|
->willReturn($key);
|
|
|
|
|
|
|
|
$this->assertSame($key, $manager->getSystemKey());
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
|
2017-07-25 12:54:14 +03:00
|
|
|
public function testGetSystemKeyFailure() {
|
2019-11-27 17:27:18 +03:00
|
|
|
$this->expectException(\RuntimeException::class);
|
|
|
|
|
2017-07-25 12:54:14 +03:00
|
|
|
$manager = $this->getManager(['retrieveKey']);
|
|
|
|
|
2020-08-11 22:32:18 +03:00
|
|
|
/** @var Key|\PHPUnit\Framework\MockObject\MockObject $key */
|
2017-07-25 12:54:14 +03:00
|
|
|
$key = $this->createMock(Key::class);
|
|
|
|
|
|
|
|
$this->config->expects($this->once())->method('getSystemValue')
|
|
|
|
->with('instanceid', null)->willReturn(null);
|
|
|
|
|
|
|
|
$manager->getSystemKey();
|
|
|
|
}
|
2016-11-18 12:10:05 +03:00
|
|
|
}
|