123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753 |
- <?php
- namespace OCA\Encryption\Tests;
- use OC\Files\FileInfo;
- use OC\Files\View;
- use OCA\Encryption\Crypto\Crypt;
- use OCA\Encryption\KeyManager;
- use OCA\Encryption\Session;
- use OCA\Encryption\Util;
- use OCP\Encryption\Keys\IStorage;
- use OCP\Files\Cache\ICache;
- use OCP\Files\Storage;
- use OCP\IConfig;
- use Psr\Log\LoggerInterface;
- use OCP\IUserSession;
- use OCP\Lock\ILockingProvider;
- use OCP\Lock\LockedException;
- use PHPUnit\Framework\MockObject\MockObject;
- use Test\TestCase;
- class KeyManagerTest extends TestCase {
-
- private $instance;
-
- private $userId;
-
- private $systemKeyId;
-
- private $keyStorageMock;
-
- private $cryptMock;
-
- private $userMock;
-
- private $sessionMock;
-
- private $logMock;
-
- private $utilMock;
-
- private $configMock;
-
- private $lockingProviderMock;
- protected function setUp(): void {
- parent::setUp();
- $this->userId = 'user1';
- $this->systemKeyId = 'systemKeyId';
- $this->keyStorageMock = $this->createMock(IStorage::class);
- $this->cryptMock = $this->getMockBuilder(Crypt::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->configMock = $this->createMock(IConfig::class);
- $this->configMock->expects($this->any())
- ->method('getAppValue')
- ->willReturn($this->systemKeyId);
- $this->userMock = $this->createMock(IUserSession::class);
- $this->sessionMock = $this->getMockBuilder(Session::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->logMock = $this->createMock(LoggerInterface::class);
- $this->utilMock = $this->getMockBuilder(Util::class)
- ->disableOriginalConstructor()
- ->getMock();
- $this->lockingProviderMock = $this->createMock(ILockingProvider::class);
- $this->instance = new KeyManager(
- $this->keyStorageMock,
- $this->cryptMock,
- $this->configMock,
- $this->userMock,
- $this->sessionMock,
- $this->logMock,
- $this->utilMock,
- $this->lockingProviderMock
- );
- }
- public function testDeleteShareKey() {
- $this->keyStorageMock->expects($this->any())
- ->method('deleteFileKey')
- ->with($this->equalTo('/path'), $this->equalTo('keyId.shareKey'))
- ->willReturn(true);
- $this->assertTrue(
- $this->instance->deleteShareKey('/path', 'keyId')
- );
- }
- public function testGetPrivateKey() {
- $this->keyStorageMock->expects($this->any())
- ->method('getUserKey')
- ->with($this->equalTo($this->userId), $this->equalTo('privateKey'))
- ->willReturn('privateKey');
- $this->assertSame('privateKey',
- $this->instance->getPrivateKey($this->userId)
- );
- }
- public function testGetPublicKey() {
- $this->keyStorageMock->expects($this->any())
- ->method('getUserKey')
- ->with($this->equalTo($this->userId), $this->equalTo('publicKey'))
- ->willReturn('publicKey');
- $this->assertSame('publicKey',
- $this->instance->getPublicKey($this->userId)
- );
- }
- public function testRecoveryKeyExists() {
- $this->keyStorageMock->expects($this->any())
- ->method('getSystemUserKey')
- ->with($this->equalTo($this->systemKeyId . '.publicKey'))
- ->willReturn('recoveryKey');
- $this->assertTrue($this->instance->recoveryKeyExists());
- }
- public function testCheckRecoveryKeyPassword() {
- $this->keyStorageMock->expects($this->any())
- ->method('getSystemUserKey')
- ->with($this->equalTo($this->systemKeyId . '.privateKey'))
- ->willReturn('recoveryKey');
- $this->cryptMock->expects($this->any())
- ->method('decryptPrivateKey')
- ->with($this->equalTo('recoveryKey'), $this->equalTo('pass'))
- ->willReturn('decryptedRecoveryKey');
- $this->assertTrue($this->instance->checkRecoveryPassword('pass'));
- }
- public function testSetPublicKey() {
- $this->keyStorageMock->expects($this->any())
- ->method('setUserKey')
- ->with(
- $this->equalTo($this->userId),
- $this->equalTo('publicKey'),
- $this->equalTo('key'))
- ->willReturn(true);
- $this->assertTrue(
- $this->instance->setPublicKey($this->userId, 'key')
- );
- }
- public function testSetPrivateKey() {
- $this->keyStorageMock->expects($this->any())
- ->method('setUserKey')
- ->with(
- $this->equalTo($this->userId),
- $this->equalTo('privateKey'),
- $this->equalTo('key'))
- ->willReturn(true);
- $this->assertTrue(
- $this->instance->setPrivateKey($this->userId, 'key')
- );
- }
-
- public function testUserHasKeys($key, $expected) {
- $this->keyStorageMock->expects($this->exactly(2))
- ->method('getUserKey')
- ->with($this->equalTo($this->userId), $this->anything())
- ->willReturn($key);
- $this->assertSame($expected,
- $this->instance->userHasKeys($this->userId)
- );
- }
- public function dataTestUserHasKeys() {
- return [
- ['key', true],
- ['', false]
- ];
- }
- public function testUserHasKeysMissingPrivateKey() {
- $this->expectException(\OCA\Encryption\Exceptions\PrivateKeyMissingException::class);
- $this->keyStorageMock->expects($this->exactly(2))
- ->method('getUserKey')
- ->willReturnCallback(function ($uid, $keyID, $encryptionModuleId) {
- if ($keyID === 'privateKey') {
- return '';
- }
- return 'key';
- });
- $this->instance->userHasKeys($this->userId);
- }
- public function testUserHasKeysMissingPublicKey() {
- $this->expectException(\OCA\Encryption\Exceptions\PublicKeyMissingException::class);
- $this->keyStorageMock->expects($this->exactly(2))
- ->method('getUserKey')
- ->willReturnCallback(function ($uid, $keyID, $encryptionModuleId) {
- if ($keyID === 'publicKey') {
- return '';
- }
- return 'key';
- });
- $this->instance->userHasKeys($this->userId);
- }
-
- public function testInit($useMasterKey) {
-
- $instance = $this->getMockBuilder(KeyManager::class)
- ->setConstructorArgs(
- [
- $this->keyStorageMock,
- $this->cryptMock,
- $this->configMock,
- $this->userMock,
- $this->sessionMock,
- $this->logMock,
- $this->utilMock,
- $this->lockingProviderMock
- ]
- )->setMethods(['getMasterKeyId', 'getMasterKeyPassword', 'getSystemPrivateKey', 'getPrivateKey'])
- ->getMock();
- $this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
- ->willReturn($useMasterKey);
- $this->sessionMock->expects($this->exactly(2))->method('setStatus')
- ->withConsecutive(
- [Session::INIT_EXECUTED],
- [Session::INIT_SUCCESSFUL],
- );
- $instance->expects($this->any())->method('getMasterKeyId')->willReturn('masterKeyId');
- $instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
- $instance->expects($this->any())->method('getSystemPrivateKey')->with('masterKeyId')->willReturn('privateMasterKey');
- $instance->expects($this->any())->method('getPrivateKey')->with($this->userId)->willReturn('privateUserKey');
- if ($useMasterKey) {
- $this->cryptMock->expects($this->once())->method('decryptPrivateKey')
- ->with('privateMasterKey', 'masterKeyPassword', 'masterKeyId')
- ->willReturn('key');
- } else {
- $this->cryptMock->expects($this->once())->method('decryptPrivateKey')
- ->with('privateUserKey', 'pass', $this->userId)
- ->willReturn('key');
- }
- $this->sessionMock->expects($this->once())->method('setPrivateKey')
- ->with('key');
- $this->assertTrue($instance->init($this->userId, 'pass'));
- }
- public function dataTestInit() {
- return [
- [true],
- [false]
- ];
- }
- public function testSetRecoveryKey() {
- $this->keyStorageMock->expects($this->exactly(2))
- ->method('setSystemUserKey')
- ->willReturn(true);
- $this->cryptMock->expects($this->any())
- ->method('encryptPrivateKey')
- ->with($this->equalTo('privateKey'), $this->equalTo('pass'))
- ->willReturn('decryptedPrivateKey');
- $this->assertTrue(
- $this->instance->setRecoveryKey('pass',
- ['publicKey' => 'publicKey', 'privateKey' => 'privateKey'])
- );
- }
- public function testSetSystemPrivateKey() {
- $this->keyStorageMock->expects($this->exactly(1))
- ->method('setSystemUserKey')
- ->with($this->equalTo('keyId.privateKey'), $this->equalTo('key'))
- ->willReturn(true);
- $this->assertTrue(
- $this->instance->setSystemPrivateKey('keyId', 'key')
- );
- }
- public function testGetSystemPrivateKey() {
- $this->keyStorageMock->expects($this->exactly(1))
- ->method('getSystemUserKey')
- ->with($this->equalTo('keyId.privateKey'))
- ->willReturn('systemPrivateKey');
- $this->assertSame('systemPrivateKey',
- $this->instance->getSystemPrivateKey('keyId')
- );
- }
- public function testGetEncryptedFileKey() {
- $this->keyStorageMock->expects($this->once())
- ->method('getFileKey')
- ->with('/', 'fileKey')
- ->willReturn(true);
- $this->assertTrue($this->instance->getEncryptedFileKey('/'));
- }
- public function dataTestGetFileKey() {
- return [
- ['user1', false, 'privateKey', 'legacyKey', 'multiKeyDecryptResult'],
- ['user1', false, 'privateKey', '', 'multiKeyDecryptResult'],
- ['user1', false, false, 'legacyKey', ''],
- ['user1', false, false, '', ''],
- ['user1', true, 'privateKey', 'legacyKey', 'multiKeyDecryptResult'],
- ['user1', true, 'privateKey', '', 'multiKeyDecryptResult'],
- ['user1', true, false, 'legacyKey', ''],
- ['user1', true, false, '', ''],
- [null, false, 'privateKey', 'legacyKey', 'multiKeyDecryptResult'],
- [null, false, 'privateKey', '', 'multiKeyDecryptResult'],
- [null, false, false, 'legacyKey', ''],
- [null, false, false, '', ''],
- [null, true, 'privateKey', 'legacyKey', 'multiKeyDecryptResult'],
- [null, true, 'privateKey', '', 'multiKeyDecryptResult'],
- [null, true, false, 'legacyKey', ''],
- [null, true, false, '', ''],
- ];
- }
-
- public function testGetFileKey($uid, $isMasterKeyEnabled, $privateKey, $encryptedFileKey, $expected) {
- $path = '/foo.txt';
- if ($isMasterKeyEnabled) {
- $expectedUid = 'masterKeyId';
- $this->configMock->expects($this->any())->method('getSystemValue')->with('secret')
- ->willReturn('password');
- } elseif (!$uid) {
- $expectedUid = 'systemKeyId';
- } else {
- $expectedUid = $uid;
- }
- $this->invokePrivate($this->instance, 'masterKeyId', ['masterKeyId']);
- $this->keyStorageMock->expects($this->exactly(2))
- ->method('getFileKey')
- ->withConsecutive(
- [$path, 'fileKey', 'OC_DEFAULT_MODULE'],
- [$path, $expectedUid . '.shareKey', 'OC_DEFAULT_MODULE'],
- )
- ->willReturnOnConsecutiveCalls(
- $encryptedFileKey,
- 'fileKey',
- );
- $this->utilMock->expects($this->any())->method('isMasterKeyEnabled')
- ->willReturn($isMasterKeyEnabled);
- if (is_null($uid)) {
- $this->keyStorageMock->expects($this->once())
- ->method('getSystemUserKey')
- ->willReturn(true);
- $this->cryptMock->expects($this->once())
- ->method('decryptPrivateKey')
- ->willReturn($privateKey);
- } else {
- $this->keyStorageMock->expects($this->never())
- ->method('getSystemUserKey');
- $this->sessionMock->expects($this->once())->method('getPrivateKey')->willReturn($privateKey);
- }
- if (!empty($encryptedFileKey)) {
- $this->cryptMock->expects($this->never())
- ->method('multiKeyDecrypt');
- if ($privateKey) {
- $this->cryptMock->expects($this->once())
- ->method('multiKeyDecryptLegacy')
- ->willReturn('multiKeyDecryptResult');
- } else {
- $this->cryptMock->expects($this->never())
- ->method('multiKeyDecryptLegacy');
- }
- } else {
- $this->cryptMock->expects($this->never())
- ->method('multiKeyDecryptLegacy');
- if ($privateKey) {
- $this->cryptMock->expects($this->once())
- ->method('multiKeyDecrypt')
- ->willReturn('multiKeyDecryptResult');
- } else {
- $this->cryptMock->expects($this->never())
- ->method('multiKeyDecrypt');
- }
- }
- $this->assertSame($expected,
- $this->instance->getFileKey($path, $uid, null)
- );
- }
- public function testDeletePrivateKey() {
- $this->keyStorageMock->expects($this->once())
- ->method('deleteUserKey')
- ->with('user1', 'privateKey')
- ->willReturn(true);
- $this->assertTrue(self::invokePrivate($this->instance,
- 'deletePrivateKey',
- [$this->userId]));
- }
- public function testDeleteAllFileKeys() {
- $this->keyStorageMock->expects($this->once())
- ->method('deleteAllFileKeys')
- ->willReturn(true);
- $this->assertTrue($this->instance->deleteAllFileKeys('/'));
- }
-
- public function testAddSystemKeys($accessList, $publicKeys, $uid, $expectedKeys) {
- $publicShareKeyId = 'publicShareKey';
- $recoveryKeyId = 'recoveryKey';
- $this->keyStorageMock->expects($this->any())
- ->method('getSystemUserKey')
- ->willReturnCallback(function ($keyId, $encryptionModuleId) {
- return $keyId;
- });
- $this->utilMock->expects($this->any())
- ->method('isRecoveryEnabledForUser')
- ->willReturnCallback(function ($uid) {
- if ($uid === 'user1') {
- return true;
- }
- return false;
- });
-
- self::invokePrivate($this->instance, 'publicShareKeyId', [$publicShareKeyId]);
- self::invokePrivate($this->instance, 'recoveryKeyId', [$recoveryKeyId]);
- $result = $this->instance->addSystemKeys($accessList, $publicKeys, $uid);
- foreach ($expectedKeys as $expected) {
- $this->assertArrayHasKey($expected, $result);
- }
- $this->assertSameSize($expectedKeys, $result);
- }
-
- public function dataTestAddSystemKeys() {
- return [
- [['public' => true],[], 'user1', ['publicShareKey', 'recoveryKey']],
- [['public' => false], [], 'user1', ['recoveryKey']],
- [['public' => true],[], 'user2', ['publicShareKey']],
- [['public' => false], [], 'user2', []],
- ];
- }
- public function testGetMasterKeyId() {
- $this->assertSame('systemKeyId', $this->instance->getMasterKeyId());
- }
- public function testGetPublicMasterKey() {
- $this->keyStorageMock->expects($this->once())->method('getSystemUserKey')
- ->with('systemKeyId.publicKey', \OCA\Encryption\Crypto\Encryption::ID)
- ->willReturn(true);
- $this->assertTrue(
- $this->instance->getPublicMasterKey()
- );
- }
- public function testGetMasterKeyPassword() {
- $this->configMock->expects($this->once())->method('getSystemValue')->with('secret')
- ->willReturn('password');
- $this->assertSame('password',
- $this->invokePrivate($this->instance, 'getMasterKeyPassword', [])
- );
- }
- public function testGetMasterKeyPasswordException() {
- $this->expectException(\Exception::class);
- $this->configMock->expects($this->once())->method('getSystemValue')->with('secret')
- ->willReturn('');
- $this->invokePrivate($this->instance, 'getMasterKeyPassword', []);
- }
-
- public function testValidateMasterKey($masterKey) {
-
- $instance = $this->getMockBuilder(KeyManager::class)
- ->setConstructorArgs(
- [
- $this->keyStorageMock,
- $this->cryptMock,
- $this->configMock,
- $this->userMock,
- $this->sessionMock,
- $this->logMock,
- $this->utilMock,
- $this->lockingProviderMock
- ]
- )->setMethods(['getPublicMasterKey', 'setSystemPrivateKey', 'getMasterKeyPassword'])
- ->getMock();
- $this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
- ->willReturn(true);
- $instance->expects($this->once())->method('getPublicMasterKey')
- ->willReturn($masterKey);
- $instance->expects($this->any())->method('getMasterKeyPassword')->willReturn('masterKeyPassword');
- $this->cryptMock->expects($this->any())->method('generateHeader')->willReturn('header');
- if (empty($masterKey)) {
- $this->cryptMock->expects($this->once())->method('createKeyPair')
- ->willReturn(['publicKey' => 'public', 'privateKey' => 'private']);
- $this->keyStorageMock->expects($this->once())->method('setSystemUserKey')
- ->with('systemKeyId.publicKey', 'public', \OCA\Encryption\Crypto\Encryption::ID);
- $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 {
- $this->cryptMock->expects($this->never())->method('createKeyPair');
- $this->keyStorageMock->expects($this->never())->method('setSystemUserKey');
- $this->cryptMock->expects($this->never())->method('encryptPrivateKey');
- $instance->expects($this->never())->method('setSystemPrivateKey');
- }
- $instance->validateMasterKey();
- }
- public function testValidateMasterKeyLocked() {
-
- $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();
- $this->utilMock->expects($this->once())->method('isMasterKeyEnabled')
- ->willReturn(true);
- $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'],
- ['']
- ];
- }
- public function testGetVersionWithoutFileInfo() {
- $view = $this->getMockBuilder(View::class)
- ->disableOriginalConstructor()->getMock();
- $view->expects($this->once())
- ->method('getFileInfo')
- ->with('/admin/files/myfile.txt')
- ->willReturn(false);
-
- $this->assertSame(0, $this->instance->getVersion('/admin/files/myfile.txt', $view));
- }
- public function testGetVersionWithFileInfo() {
- $view = $this->getMockBuilder(View::class)
- ->disableOriginalConstructor()->getMock();
- $fileInfo = $this->getMockBuilder(FileInfo::class)
- ->disableOriginalConstructor()->getMock();
- $fileInfo->expects($this->once())
- ->method('getEncryptedVersion')
- ->willReturn(1337);
- $view->expects($this->once())
- ->method('getFileInfo')
- ->with('/admin/files/myfile.txt')
- ->willReturn($fileInfo);
-
- $this->assertSame(1337, $this->instance->getVersion('/admin/files/myfile.txt', $view));
- }
- public function testSetVersionWithFileInfo() {
- $view = $this->getMockBuilder(View::class)
- ->disableOriginalConstructor()->getMock();
- $cache = $this->getMockBuilder(ICache::class)
- ->disableOriginalConstructor()->getMock();
- $cache->expects($this->once())
- ->method('update')
- ->with(123, ['encrypted' => 5, 'encryptedVersion' => 5]);
- $storage = $this->getMockBuilder(Storage::class)
- ->disableOriginalConstructor()->getMock();
- $storage->expects($this->once())
- ->method('getCache')
- ->willReturn($cache);
- $fileInfo = $this->getMockBuilder(FileInfo::class)
- ->disableOriginalConstructor()->getMock();
- $fileInfo->expects($this->once())
- ->method('getStorage')
- ->willReturn($storage);
- $fileInfo->expects($this->once())
- ->method('getId')
- ->willReturn(123);
- $view->expects($this->once())
- ->method('getFileInfo')
- ->with('/admin/files/myfile.txt')
- ->willReturn($fileInfo);
-
- $this->instance->setVersion('/admin/files/myfile.txt', 5, $view);
- }
- public function testSetVersionWithoutFileInfo() {
- $view = $this->getMockBuilder(View::class)
- ->disableOriginalConstructor()->getMock();
- $view->expects($this->once())
- ->method('getFileInfo')
- ->with('/admin/files/myfile.txt')
- ->willReturn(false);
-
- $this->instance->setVersion('/admin/files/myfile.txt', 5, $view);
- }
- public function testBackupUserKeys() {
- $this->keyStorageMock->expects($this->once())->method('backupUserKeys')
- ->with('OC_DEFAULT_MODULE', 'test', 'user1');
- $this->instance->backupUserKeys('test', 'user1');
- }
- }
|