Test for locking state in key generation

Signed-off-by: Julius Härtl <jus@bitgrid.net>
This commit is contained in:
Julius Härtl 2020-07-27 13:28:44 +02:00 committed by backportbot[bot]
parent dd4dc60f4c
commit c86e568a5d
2 changed files with 51 additions and 5 deletions

View File

@ -43,6 +43,9 @@ use OCP\Files\Storage;
use OCP\IConfig; use OCP\IConfig;
use OCP\ILogger; use OCP\ILogger;
use OCP\IUserSession; use OCP\IUserSession;
use OCP\Lock\ILockingProvider;
use OCP\Lock\LockedException;
use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase; use Test\TestCase;
class KeyManagerTest extends TestCase { class KeyManagerTest extends TestCase {
@ -79,6 +82,9 @@ class KeyManagerTest extends TestCase {
/** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */ /** @var \OCP\IConfig|\PHPUnit_Framework_MockObject_MockObject */
private $configMock; private $configMock;
/** @var ILockingProvider|MockObject */
private $lockingProviderMock;
protected function setUp(): void { protected function setUp(): void {
parent::setUp(); parent::setUp();
$this->userId = 'user1'; $this->userId = 'user1';
@ -99,6 +105,7 @@ class KeyManagerTest extends TestCase {
$this->utilMock = $this->getMockBuilder(Util::class) $this->utilMock = $this->getMockBuilder(Util::class)
->disableOriginalConstructor() ->disableOriginalConstructor()
->getMock(); ->getMock();
$this->lockingProviderMock = $this->createMock(ILockingProvider::class);
$this->instance = new KeyManager( $this->instance = new KeyManager(
$this->keyStorageMock, $this->keyStorageMock,
@ -107,7 +114,9 @@ class KeyManagerTest extends TestCase {
$this->userMock, $this->userMock,
$this->sessionMock, $this->sessionMock,
$this->logMock, $this->logMock,
$this->utilMock); $this->utilMock,
$this->lockingProviderMock
);
} }
public function testDeleteShareKey() { public function testDeleteShareKey() {
@ -269,7 +278,8 @@ class KeyManagerTest extends TestCase {
$this->userMock, $this->userMock,
$this->sessionMock, $this->sessionMock,
$this->logMock, $this->logMock,
$this->utilMock $this->utilMock,
$this->lockingProviderMock
] ]
)->setMethods(['getMasterKeyId', 'getMasterKeyPassword', 'getSystemPrivateKey', 'getPrivateKey']) )->setMethods(['getMasterKeyId', 'getMasterKeyPassword', 'getSystemPrivateKey', 'getPrivateKey'])
->getMock(); ->getMock();
@ -559,7 +569,8 @@ class KeyManagerTest extends TestCase {
$this->userMock, $this->userMock,
$this->sessionMock, $this->sessionMock,
$this->logMock, $this->logMock,
$this->utilMock $this->utilMock,
$this->lockingProviderMock
] ]
)->setMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword']) )->setMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
->getMock(); ->getMock();
@ -578,6 +589,8 @@ class KeyManagerTest extends TestCase {
$this->cryptMock->expects($this->once())->method('encryptPrivateKey') $this->cryptMock->expects($this->once())->method('encryptPrivateKey')
->with('private', 'masterKeyPassword', 'systemKeyId') ->with('private', 'masterKeyPassword', 'systemKeyId')
->willReturn('EncryptedKey'); ->willReturn('EncryptedKey');
$this->lockingProviderMock->expects($this->once())
->method('acquireLock');
$instance->expects($this->once())->method('setSystemPrivateKey') $instance->expects($this->once())->method('setSystemPrivateKey')
->with('systemKeyId', 'headerEncryptedKey'); ->with('systemKeyId', 'headerEncryptedKey');
} else { } else {
@ -590,6 +603,39 @@ class KeyManagerTest extends TestCase {
$instance->validateMasterKey(); $instance->validateMasterKey();
} }
public function testValidateMasterKeyLocked() {
/** @var \OCA\Encryption\KeyManager | \PHPUnit_Framework_MockObject_MockObject $instance */
$instance = $this->getMockBuilder(KeyManager::class)
->setConstructorArgs(
[
$this->keyStorageMock,
$this->cryptMock,
$this->configMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
$this->utilMock,
$this->lockingProviderMock
]
)->setMethods(['getPublicMasterKey', 'getPrivateMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
->getMock();
$instance->expects($this->once())->method('getPublicMasterKey')
->willReturn('');
$instance->expects($this->once())->method('getPrivateMasterKey')
->willReturn('');
$instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
$this->cryptMock->expects($this->any())->method('generateHeader')->willReturn('header');
$this->lockingProviderMock->expects($this->once())
->method('acquireLock')
->willThrowException(new LockedException('encryption-generateMasterKey'));
$this->expectException(LockedException::class);
$instance->validateMasterKey();
}
public function dataTestValidateMasterKey() { public function dataTestValidateMasterKey() {
return [ return [
['masterKey'], ['masterKey'],

View File

@ -90,9 +90,9 @@ class SetupTest extends TestCase {
if ($hasKeys) { if ($hasKeys) {
$this->keyManagerMock->expects($this->never())->method('storeKeyPair'); $this->keyManagerMock->expects($this->never())->method('storeKeyPair');
} else { } else {
$this->cryptMock->expects($this->once())->method('createKeyPair')->willReturn('keyPair'); $this->cryptMock->expects($this->once())->method('createKeyPair')->willReturn(['publicKey' => 'publicKey', 'privateKey' => 'privateKey']);
$this->keyManagerMock->expects($this->once())->method('storeKeyPair') $this->keyManagerMock->expects($this->once())->method('storeKeyPair')
->with('uid', 'password', 'keyPair')->willReturn(true); ->with('uid', 'password', ['publicKey' => 'publicKey', 'privateKey' => 'privateKey'])->willReturn(true);
} }
$this->assertSame($expected, $this->assertSame($expected,