2015-04-03 18:37:54 +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>
|
2020-03-31 11:49:10 +03:00
|
|
|
* @author Christoph Wurst <christoph@winzerhof-wurst.at>
|
2015-04-07 18:02:49 +03:00
|
|
|
* @author Clark Tomlinson <fallen013@gmail.com>
|
2016-07-21 17:49:16 +03:00
|
|
|
* @author Joas Schilling <coding@schilljs.com>
|
2016-05-26 20:56:05 +03:00
|
|
|
* @author Lukas Reschke <lukas@statuscode.ch>
|
2017-11-06 17:56:42 +03:00
|
|
|
* @author Morris Jobke <hey@morrisjobke.de>
|
|
|
|
* @author Roeland Jago Douma <roeland@famdouma.nl>
|
2015-04-07 18:02:49 +03:00
|
|
|
*
|
2015-04-03 18:37:54 +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-04-03 18:37:54 +03:00
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace OCA\Encryption\Tests;
|
|
|
|
|
|
|
|
|
2016-09-02 11:29:05 +03:00
|
|
|
use OC\Files\View;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCA\Encryption\Crypto\Crypt;
|
|
|
|
use OCA\Encryption\KeyManager;
|
2015-04-03 18:37:54 +03:00
|
|
|
use OCA\Encryption\Recovery;
|
2016-09-02 11:29:05 +03:00
|
|
|
use OCP\Encryption\IFile;
|
|
|
|
use OCP\IConfig;
|
2019-08-26 16:22:00 +03:00
|
|
|
use OCP\IUser;
|
2017-10-26 14:46:16 +03:00
|
|
|
use OCP\IUserSession;
|
2019-08-26 16:22:00 +03:00
|
|
|
use PHPUnit\Framework\MockObject\MockObject;
|
2015-04-03 18:37:54 +03:00
|
|
|
use Test\TestCase;
|
|
|
|
|
|
|
|
class RecoveryTest extends TestCase {
|
|
|
|
private static $tempStorage = [];
|
2015-04-06 18:49:18 +03:00
|
|
|
/**
|
2016-05-12 10:42:19 +03:00
|
|
|
* @var \OCP\Encryption\IFile|\PHPUnit_Framework_MockObject_MockObject
|
2015-04-06 18:49:18 +03:00
|
|
|
*/
|
|
|
|
private $fileMock;
|
2015-04-03 18:37:54 +03:00
|
|
|
/**
|
2016-05-12 10:42:19 +03:00
|
|
|
* @var \OC\Files\View|\PHPUnit_Framework_MockObject_MockObject
|
2015-04-03 18:37:54 +03:00
|
|
|
*/
|
|
|
|
private $viewMock;
|
|
|
|
/**
|
2016-05-12 10:42:19 +03:00
|
|
|
* @var \OCP\IUserSession|\PHPUnit_Framework_MockObject_MockObject
|
2015-04-03 18:37:54 +03:00
|
|
|
*/
|
|
|
|
private $userSessionMock;
|
2019-08-26 16:22:00 +03:00
|
|
|
/**
|
|
|
|
* @var MockObject|IUser
|
|
|
|
*/
|
|
|
|
private $user;
|
2015-04-03 18:37:54 +03:00
|
|
|
/**
|
2016-05-12 10:42:19 +03:00
|
|
|
* @var \OCA\Encryption\KeyManager|\PHPUnit_Framework_MockObject_MockObject
|
2015-04-03 18:37:54 +03:00
|
|
|
*/
|
|
|
|
private $keyManagerMock;
|
|
|
|
/**
|
2016-05-12 10:42:19 +03:00
|
|
|
* @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject
|
2015-04-03 18:37:54 +03:00
|
|
|
*/
|
|
|
|
private $configMock;
|
|
|
|
/**
|
2016-05-12 10:42:19 +03:00
|
|
|
* @var \OCA\Encryption\Crypto\Crypt|\PHPUnit_Framework_MockObject_MockObject
|
2015-04-03 18:37:54 +03:00
|
|
|
*/
|
|
|
|
private $cryptMock;
|
|
|
|
/**
|
|
|
|
* @var Recovery
|
|
|
|
*/
|
|
|
|
private $instance;
|
|
|
|
|
2015-12-08 11:28:49 +03:00
|
|
|
public function testEnableAdminRecoverySuccessful() {
|
2015-04-03 18:37:54 +03:00
|
|
|
$this->keyManagerMock->expects($this->exactly(2))
|
|
|
|
->method('recoveryKeyExists')
|
|
|
|
->willReturnOnConsecutiveCalls(false, true);
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
->method('createKeyPair')
|
2015-12-08 11:28:49 +03:00
|
|
|
->willReturn([
|
|
|
|
'publicKey' => 'privateKey',
|
|
|
|
'privateKey' => 'publicKey',
|
|
|
|
]);
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
->method('setRecoveryKey')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->exactly(2))
|
|
|
|
->method('checkRecoveryPassword')
|
|
|
|
->willReturnOnConsecutiveCalls(true, true);
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->enableAdminRecovery('password'));
|
|
|
|
$this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
|
|
|
|
$this->assertEquals(1, self::$tempStorage['recoveryAdminEnabled']);
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->enableAdminRecovery('password'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testEnableAdminRecoveryCouldNotCheckPassword() {
|
|
|
|
$this->keyManagerMock->expects($this->exactly(2))
|
|
|
|
->method('recoveryKeyExists')
|
|
|
|
->willReturnOnConsecutiveCalls(false, true);
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
->method('createKeyPair')
|
|
|
|
->willReturn([
|
|
|
|
'publicKey' => 'privateKey',
|
|
|
|
'privateKey' => 'publicKey',
|
|
|
|
]);
|
2015-04-03 18:37:54 +03:00
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
->method('setRecoveryKey')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->exactly(2))
|
|
|
|
->method('checkRecoveryPassword')
|
|
|
|
->willReturnOnConsecutiveCalls(true, false);
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->enableAdminRecovery('password'));
|
|
|
|
$this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
|
|
|
|
$this->assertEquals(1, self::$tempStorage['recoveryAdminEnabled']);
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->enableAdminRecovery('password'));
|
|
|
|
}
|
|
|
|
|
2015-12-08 11:28:49 +03:00
|
|
|
public function testEnableAdminRecoveryCouldNotCreateKey() {
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
->method('recoveryKeyExists')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
->method('createKeyPair')
|
|
|
|
->willReturn(false);
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->enableAdminRecovery('password'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testChangeRecoveryKeyPasswordSuccessful() {
|
2015-04-03 18:37:54 +03:00
|
|
|
$this->assertFalse($this->instance->changeRecoveryKeyPassword('password',
|
|
|
|
'passwordOld'));
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
->method('getSystemPrivateKey');
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
->method('decryptPrivateKey');
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
2015-08-07 15:04:17 +03:00
|
|
|
->method('encryptPrivateKey')
|
2015-04-03 18:37:54 +03:00
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->changeRecoveryKeyPassword('password',
|
|
|
|
'passwordOld'));
|
|
|
|
}
|
|
|
|
|
2015-12-08 11:28:49 +03:00
|
|
|
public function testChangeRecoveryKeyPasswordCouldNotDecryptPrivateRecoveryKey() {
|
|
|
|
$this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld'));
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
->method('getSystemPrivateKey');
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
->method('decryptPrivateKey')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturn(false);
|
2015-12-08 11:28:49 +03:00
|
|
|
|
|
|
|
$this->assertFalse($this->instance->changeRecoveryKeyPassword('password', 'passwordOld'));
|
|
|
|
}
|
|
|
|
|
2015-04-03 18:37:54 +03:00
|
|
|
public function testDisableAdminRecovery() {
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->exactly(2))
|
|
|
|
->method('checkRecoveryPassword')
|
|
|
|
->willReturnOnConsecutiveCalls(true, false);
|
|
|
|
|
|
|
|
$this->assertArrayHasKey('recoveryAdminEnabled', self::$tempStorage);
|
|
|
|
$this->assertTrue($this->instance->disableAdminRecovery('password'));
|
|
|
|
$this->assertEquals(0, self::$tempStorage['recoveryAdminEnabled']);
|
|
|
|
|
|
|
|
$this->assertFalse($this->instance->disableAdminRecovery('password'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsRecoveryEnabledForUser() {
|
|
|
|
|
|
|
|
$this->configMock->expects($this->exactly(2))
|
|
|
|
->method('getUserValue')
|
|
|
|
->willReturnOnConsecutiveCalls('1', '0');
|
|
|
|
|
|
|
|
$this->assertTrue($this->instance->isRecoveryEnabledForUser());
|
|
|
|
$this->assertFalse($this->instance->isRecoveryEnabledForUser('admin'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testIsRecoveryKeyEnabled() {
|
|
|
|
$this->assertFalse($this->instance->isRecoveryKeyEnabled());
|
|
|
|
self::$tempStorage['recoveryAdminEnabled'] = '1';
|
|
|
|
$this->assertTrue($this->instance->isRecoveryKeyEnabled());
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testSetRecoveryFolderForUser() {
|
|
|
|
$this->viewMock->expects($this->exactly(2))
|
|
|
|
->method('getDirectoryContent')
|
|
|
|
->willReturn([]);
|
|
|
|
$this->assertTrue($this->instance->setRecoveryForUser(0));
|
|
|
|
$this->assertTrue($this->instance->setRecoveryForUser('1'));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testRecoverUserFiles() {
|
|
|
|
$this->viewMock->expects($this->once())
|
|
|
|
->method('getDirectoryContent')
|
|
|
|
->willReturn([]);
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
->method('decryptPrivateKey');
|
2016-05-12 10:42:19 +03:00
|
|
|
$this->instance->recoverUsersFiles('password', 'admin');
|
2018-01-25 13:23:12 +03:00
|
|
|
$this->addToAssertionCount(1);
|
2015-04-03 18:37:54 +03:00
|
|
|
}
|
|
|
|
|
2015-04-06 18:49:18 +03:00
|
|
|
public function testRecoverFile() {
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
->method('getEncryptedFileKey')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
->method('getShareKey')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
->method('multiKeyDecrypt')
|
|
|
|
->willReturn(true);
|
|
|
|
|
|
|
|
$this->fileMock->expects($this->once())
|
|
|
|
->method('getAccessList')
|
|
|
|
->willReturn(['users' => ['admin']]);
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
->method('getPublicKey')
|
|
|
|
->willReturn('publicKey');
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
->method('addSystemKeys')
|
2015-04-29 18:18:41 +03:00
|
|
|
->with($this->anything(), $this->anything(), $this->equalTo('admin'))
|
2015-04-06 18:49:18 +03:00
|
|
|
->willReturn(['admin' => 'publicKey']);
|
|
|
|
|
|
|
|
|
|
|
|
$this->cryptMock->expects($this->once())
|
|
|
|
->method('multiKeyEncrypt');
|
|
|
|
|
|
|
|
$this->keyManagerMock->expects($this->once())
|
|
|
|
->method('setAllFileKeys');
|
|
|
|
|
2015-06-03 13:03:02 +03:00
|
|
|
$this->assertNull(self::invokePrivate($this->instance,
|
2015-04-06 18:49:18 +03:00
|
|
|
'recoverFile',
|
2015-04-29 18:18:41 +03:00
|
|
|
['/', 'testkey', 'admin']));
|
2015-04-06 18:49:18 +03:00
|
|
|
}
|
|
|
|
|
2019-11-21 18:40:38 +03:00
|
|
|
protected function setUp(): void {
|
2015-04-03 18:37:54 +03:00
|
|
|
parent::setUp();
|
|
|
|
|
2019-08-26 16:22:00 +03:00
|
|
|
$this->user = $this->createMock(IUser::class);
|
|
|
|
$this->user->expects($this->any())
|
|
|
|
->method('getUID')
|
|
|
|
->willReturn('admin');
|
2015-04-03 18:37:54 +03:00
|
|
|
|
2019-08-26 16:22:00 +03:00
|
|
|
$this->userSessionMock = $this->createMock(IUserSession::class);
|
|
|
|
$this->userSessionMock->expects($this->any())
|
|
|
|
->method('getUser')
|
|
|
|
->willReturn($this->user);
|
2015-04-03 18:37:54 +03:00
|
|
|
$this->userSessionMock->expects($this->any())
|
2019-08-26 16:22:00 +03:00
|
|
|
->method('isLoggedIn')
|
|
|
|
->willReturn(true);
|
2015-04-03 18:37:54 +03:00
|
|
|
|
2017-10-26 14:46:16 +03:00
|
|
|
$this->cryptMock = $this->getMockBuilder(Crypt::class)->disableOriginalConstructor()->getMock();
|
|
|
|
$this->keyManagerMock = $this->getMockBuilder(KeyManager::class)->disableOriginalConstructor()->getMock();
|
2016-09-02 11:29:05 +03:00
|
|
|
$this->configMock = $this->createMock(IConfig::class);
|
|
|
|
$this->fileMock = $this->createMock(IFile::class);
|
|
|
|
$this->viewMock = $this->createMock(View::class);
|
2015-04-03 18:37:54 +03:00
|
|
|
|
|
|
|
$this->configMock->expects($this->any())
|
|
|
|
->method('setAppValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback([$this, 'setValueTester']);
|
2015-04-03 18:37:54 +03:00
|
|
|
|
|
|
|
$this->configMock->expects($this->any())
|
|
|
|
->method('getAppValue')
|
2020-03-26 00:21:27 +03:00
|
|
|
->willReturnCallback([$this, 'getValueTester']);
|
2015-04-03 18:37:54 +03:00
|
|
|
|
|
|
|
$this->instance = new Recovery($this->userSessionMock,
|
|
|
|
$this->cryptMock,
|
|
|
|
$this->keyManagerMock,
|
|
|
|
$this->configMock,
|
2015-04-06 18:49:18 +03:00
|
|
|
$this->fileMock,
|
2015-04-03 18:37:54 +03:00
|
|
|
$this->viewMock);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $app
|
|
|
|
* @param $key
|
|
|
|
* @param $value
|
|
|
|
*/
|
|
|
|
public function setValueTester($app, $key, $value) {
|
|
|
|
self::$tempStorage[$key] = $value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $key
|
|
|
|
*/
|
|
|
|
public function removeValueTester($key) {
|
|
|
|
unset(self::$tempStorage[$key]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param $app
|
|
|
|
* @param $key
|
|
|
|
* @return mixed
|
|
|
|
*/
|
|
|
|
public function getValueTester($app, $key) {
|
|
|
|
if (!empty(self::$tempStorage[$key])) {
|
|
|
|
return self::$tempStorage[$key];
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
}
|