From ccb33af854132f1b35b71393ff68d24850de6960 Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Sun, 9 Jan 2022 15:37:45 +0530 Subject: Refactor and simplify KeyManager API (#1650) --- .../dev/msfjarvis/aps/crypto/CryptoConstants.kt | 2 +- .../dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt | 40 +++++++++++++++++----- .../dev/msfjarvis/aps/crypto/PGPKeyPairTest.kt | 22 ------------ 3 files changed, 33 insertions(+), 31 deletions(-) delete mode 100644 crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyPairTest.kt (limited to 'crypto-pgpainless/src/test/kotlin/dev/msfjarvis') diff --git a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/CryptoConstants.kt b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/CryptoConstants.kt index 7aef4675..6d5eaf01 100644 --- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/CryptoConstants.kt +++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/CryptoConstants.kt @@ -10,5 +10,5 @@ object CryptoConstants { const val PLAIN_TEXT = "encryption worthy content" const val KEY_NAME = "John Doe" const val KEY_EMAIL = "john.doe@example.com" - const val KEY_ID = "08edf7567183ce27" + const val KEY_ID = "0x08edf7567183ce27" } diff --git a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt index 4b022e5e..c547bdd4 100644 --- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt +++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt @@ -8,6 +8,7 @@ import kotlin.test.BeforeTest import kotlin.test.Test import kotlin.test.assertEquals import kotlin.test.assertIs +import kotlin.test.assertNotNull import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.StandardTestDispatcher @@ -27,7 +28,7 @@ class PGPKeyManagerTest { private val dispatcher = StandardTestDispatcher() private val scope = TestScope(dispatcher) private val keyManager by unsafeLazy { PGPKeyManager(filesDir.absolutePath, dispatcher) } - private val key = PGPKeyManager.makeKey(TestUtils.getArmoredPrivateKey()) + private val key = Key(TestUtils.getArmoredPrivateKey().encodeToByteArray()) private fun unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE) { initializer.invoke() } @@ -46,7 +47,7 @@ class PGPKeyManagerTest { fun testAddingKey() = scope.runTest { // Check if the key id returned is correct - val keyId = keyManager.addKey(key).unwrap().getKeyId() + val keyId = keyManager.getKeyId(keyManager.addKey(key).unwrap()) assertEquals(CryptoConstants.KEY_ID, keyId) // Check if the keys directory have one file @@ -72,7 +73,7 @@ class PGPKeyManagerTest { scope.runTest { // Check adding the keys twice keyManager.addKey(key, true).unwrap() - val keyId = keyManager.addKey(key, true).unwrap().getKeyId() + val keyId = keyManager.getKeyId(keyManager.addKey(key, true).unwrap()) assertEquals(CryptoConstants.KEY_ID, keyId) } @@ -84,7 +85,7 @@ class PGPKeyManagerTest { keyManager.addKey(key).unwrap() // Check if the key id returned is correct - val keyId = keyManager.removeKey(key).unwrap().getKeyId() + val keyId = keyManager.getKeyId(keyManager.removeKey(key).unwrap()) assertEquals(CryptoConstants.KEY_ID, keyId) // Check if the keys directory have 0 files @@ -93,15 +94,38 @@ class PGPKeyManagerTest { } @Test - fun testGetExistingKey() = + fun testGetExistingKeyById() = scope.runTest { // Add key using KeyManager keyManager.addKey(key).unwrap() + val keyId = keyManager.getKeyId(key) + assertNotNull(keyId) + assertEquals(CryptoConstants.KEY_ID, keyManager.getKeyId(key)) + // Check returned key id matches the expected id and the created key id - val returnedKeyPair = keyManager.getKeyById(key.getKeyId()).unwrap() - assertEquals(CryptoConstants.KEY_ID, key.getKeyId()) - assertEquals(key.getKeyId(), returnedKeyPair.getKeyId()) + val returnedKey = keyManager.getKeyById(keyId).unwrap() + assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) + } + + @Test + fun testGetExistingKeyByFullUserId() = + scope.runTest { + keyManager.addKey(key).unwrap() + + val keyId = "${CryptoConstants.KEY_NAME} <${CryptoConstants.KEY_EMAIL}>" + val returnedKey = keyManager.getKeyById(keyId).unwrap() + assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) + } + + @Test + fun testGetExistingKeyByEmailUserId() = + scope.runTest { + keyManager.addKey(key).unwrap() + + val keyId = CryptoConstants.KEY_EMAIL + val returnedKey = keyManager.getKeyById(keyId).unwrap() + assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) } @Test diff --git a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyPairTest.kt b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyPairTest.kt deleted file mode 100644 index 45b25f30..00000000 --- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyPairTest.kt +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ - -package dev.msfjarvis.aps.crypto - -import kotlin.test.Test -import kotlin.test.assertEquals -import org.pgpainless.PGPainless - -class PGPKeyPairTest { - - @Test - fun testIfKeyIdIsCorrect() { - val secretKey = - PGPainless.readKeyRing().secretKeyRing(TestUtils.getArmoredPrivateKey()).secretKey - val keyPair = PGPKeyPair(secretKey) - - assertEquals(CryptoConstants.KEY_ID, keyPair.getKeyId()) - } -} -- cgit v1.2.3