use OCP\IConfig;
use OCP\ILogger;
use OCP\IUserSession;
+use OCP\Lock\ILockingProvider;
+use OCP\Lock\LockedException;
+use PHPUnit\Framework\MockObject\MockObject;
use Test\TestCase;
class KeyManagerTest extends TestCase {
/** @var \OCP\IConfig|\PHPUnit\Framework\MockObject\MockObject */
private $configMock;
+ /** @var ILockingProvider|MockObject */
+ private $lockingProviderMock;
+
protected function setUp(): void {
parent::setUp();
$this->userId = 'user1';
$this->utilMock = $this->getMockBuilder(Util::class)
->disableOriginalConstructor()
->getMock();
+ $this->lockingProviderMock = $this->createMock(ILockingProvider::class);
$this->instance = new KeyManager(
$this->keyStorageMock,
$this->userMock,
$this->sessionMock,
$this->logMock,
- $this->utilMock);
+ $this->utilMock,
+ $this->lockingProviderMock
+ );
}
public function testDeleteShareKey() {
$this->userMock,
$this->sessionMock,
$this->logMock,
- $this->utilMock
+ $this->utilMock,
+ $this->lockingProviderMock
]
)->setMethods(['getMasterKeyId', 'getMasterKeyPassword', 'getSystemPrivateKey', 'getPrivateKey'])
->getMock();
$this->userMock,
$this->sessionMock,
$this->logMock,
- $this->utilMock
+ $this->utilMock,
+ $this->lockingProviderMock
]
)->setMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
->getMock();
$this->cryptMock->expects($this->once())->method('encryptPrivateKey')
->with('private', 'masterKeyPassword', 'systemKeyId')
->willReturn('EncryptedKey');
+ $this->lockingProviderMock->expects($this->once())
+ ->method('acquireLock');
$instance->expects($this->once())->method('setSystemPrivateKey')
->with('systemKeyId', 'headerEncryptedKey');
} else {
$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() {
return [
['masterKey'],
if ($hasKeys) {
$this->keyManagerMock->expects($this->never())->method('storeKeyPair');
} 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')
- ->with('uid', 'password', 'keyPair')->willReturn(true);
+ ->with('uid', 'password', ['publicKey' => 'publicKey', 'privateKey' => 'privateKey'])->willReturn(true);
}
$this->assertSame($expected,