From 85c3b9d5cf9fe9c9d884c7a08b0da46e4e585720 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 8 Jul 2015 18:23:18 +0200 Subject: only cleanUp the remaining keys if the migration really finished succesfully --- apps/encryption/lib/migration.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'apps/encryption/lib') diff --git a/apps/encryption/lib/migration.php b/apps/encryption/lib/migration.php index 98fc5be777a..391267603cd 100644 --- a/apps/encryption/lib/migration.php +++ b/apps/encryption/lib/migration.php @@ -50,7 +50,7 @@ class Migration { $this->config = $config; } - public function __destruct() { + public function finalCleanUp() { $this->view->deleteAll('files_encryption/public_keys'); $this->updateFileCache(); $this->config->deleteAppValue('files_encryption', 'installed_version'); -- cgit v1.2.3 From 1e284b15ff07b7a90067c5166ee7f85547095a34 Mon Sep 17 00:00:00 2001 From: Bjoern Schiessle Date: Wed, 8 Jul 2015 19:08:41 +0200 Subject: only create new key pair if both keys are missing --- apps/encryption/lib/keymanager.php | 25 +++++++++++--- apps/encryption/tests/lib/KeyManagerTest.php | 50 ++++++++++++++++++++++++++-- 2 files changed, 68 insertions(+), 7 deletions(-) (limited to 'apps/encryption/lib') diff --git a/apps/encryption/lib/keymanager.php b/apps/encryption/lib/keymanager.php index 05d23873482..8c8c1f8fd78 100644 --- a/apps/encryption/lib/keymanager.php +++ b/apps/encryption/lib/keymanager.php @@ -406,19 +406,36 @@ class KeyManager { } /** - * @param $userId + * check if user has a private and a public key + * + * @param string $userId * @return bool + * @throws PrivateKeyMissingException + * @throws PublicKeyMissingException */ public function userHasKeys($userId) { + $privateKey = $publicKey = true; + try { $this->getPrivateKey($userId); - $this->getPublicKey($userId); } catch (PrivateKeyMissingException $e) { - return false; + $privateKey = false; + $exception = $e; + } + try { + $this->getPublicKey($userId); } catch (PublicKeyMissingException $e) { + $publicKey = false; + $exception = $e; + } + + if ($privateKey && $publicKey) { + return true; + } elseif (!$privateKey && !$publicKey) { return false; + } else { + throw $exception; } - return true; } /** diff --git a/apps/encryption/tests/lib/KeyManagerTest.php b/apps/encryption/tests/lib/KeyManagerTest.php index 2561b29462f..0bac5e0341b 100644 --- a/apps/encryption/tests/lib/KeyManagerTest.php +++ b/apps/encryption/tests/lib/KeyManagerTest.php @@ -182,18 +182,62 @@ class KeyManagerTest extends TestCase { ); } - public function testUserHasKeys() { + /** + * @dataProvider dataTestUserHasKeys + */ + public function testUserHasKeys($key, $expected) { $this->keyStorageMock->expects($this->exactly(2)) ->method('getUserKey') ->with($this->equalTo($this->userId), $this->anything()) - ->willReturn('key'); + ->willReturn($key); - $this->assertTrue( + $this->assertSame($expected, $this->instance->userHasKeys($this->userId) ); } + public function dataTestUserHasKeys() { + return [ + ['key', true], + ['', false] + ]; + } + + /** + * @expectedException \OCA\Encryption\Exceptions\PrivateKeyMissingException + */ + public function testUserHasKeysMissingPrivateKey() { + $this->keyStorageMock->expects($this->exactly(2)) + ->method('getUserKey') + ->willReturnCallback(function ($uid, $keyID, $encryptionModuleId) { + if ($keyID=== 'privateKey') { + return ''; + } + return 'key'; + }); + + $this->instance->userHasKeys($this->userId); + } + + /** + * @expectedException \OCA\Encryption\Exceptions\PublicKeyMissingException + */ + public function testUserHasKeysMissingPublicKey() { + $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() { $this->keyStorageMock->expects($this->any()) ->method('getUserKey') -- cgit v1.2.3