diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2022-01-18 17:40:16 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-01-18 17:40:16 +0530 |
commit | 5509558eedbcd90f130b32d4e6c41632da291b15 (patch) | |
tree | 7b6839928889698ebe0da48a83b2e1532ed6d094 /crypto-pgpainless/src/test/kotlin | |
parent | 3ead6596ba9c2b65434f6cd948bf6ebf41a230eb (diff) |
Parameterize key and key identifier types for KeyManager (#1669)
Diffstat (limited to 'crypto-pgpainless/src/test/kotlin')
4 files changed, 57 insertions, 14 deletions
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 6d5eaf01..ede8f08d 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 = "0x08edf7567183ce27" + const val KEY_ID = 0x08edf7567183ce27 } diff --git a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifierTest.kt b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifierTest.kt new file mode 100644 index 00000000..3860873c --- /dev/null +++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifierTest.kt @@ -0,0 +1,41 @@ +/* + * Copyright © 2014-2022 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.assertNotNull +import kotlin.test.assertTrue + +class GpgIdentifierTest { + + @Test + fun `parses hexadecimal key id without leading 0x`() { + val identifier = GpgIdentifier.fromString("79E8208280490C77") + assertNotNull(identifier) + assertTrue { identifier is GpgIdentifier.KeyId } + } + + @Test + fun `parses hexadecimal key id`() { + val identifier = GpgIdentifier.fromString("0x79E8208280490C77") + assertNotNull(identifier) + assertTrue { identifier is GpgIdentifier.KeyId } + } + + @Test + fun `parses email as user id`() { + val identifier = GpgIdentifier.fromString("john.doe@example.org") + assertNotNull(identifier) + assertTrue { identifier is GpgIdentifier.UserId } + } + + @Test + fun `parses user@host without TLD`() { + val identifier = GpgIdentifier.fromString("john.doe@example") + assertNotNull(identifier) + assertTrue { identifier is GpgIdentifier.UserId } + } +} 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 ed2b00c3..24b27ef8 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 @@ -2,6 +2,8 @@ package dev.msfjarvis.aps.crypto import com.github.michaelbull.result.unwrap import com.github.michaelbull.result.unwrapError +import dev.msfjarvis.aps.crypto.GpgIdentifier.KeyId +import dev.msfjarvis.aps.crypto.GpgIdentifier.UserId import java.io.File import kotlin.test.AfterTest import kotlin.test.BeforeTest @@ -28,7 +30,7 @@ class PGPKeyManagerTest { private val dispatcher = StandardTestDispatcher() private val scope = TestScope(dispatcher) private val keyManager by unsafeLazy { PGPKeyManager(filesDir.absolutePath, dispatcher) } - private val key = Key(TestUtils.getArmoredPrivateKey()) + private val key = PGPKey(TestUtils.getArmoredPrivateKey()) private fun <T> unsafeLazy(initializer: () -> T) = lazy(LazyThreadSafetyMode.NONE) { initializer.invoke() } @@ -48,7 +50,7 @@ class PGPKeyManagerTest { scope.runTest { // Check if the key id returned is correct val keyId = keyManager.getKeyId(keyManager.addKey(key).unwrap()) - assertEquals(CryptoConstants.KEY_ID, keyId) + assertEquals(KeyId(CryptoConstants.KEY_ID), keyId) // Check if the keys directory have one file assertEquals(1, filesDir.list()?.size) @@ -75,7 +77,7 @@ class PGPKeyManagerTest { keyManager.addKey(key, true).unwrap() val keyId = keyManager.getKeyId(keyManager.addKey(key, true).unwrap()) - assertEquals(CryptoConstants.KEY_ID, keyId) + assertEquals(KeyId(CryptoConstants.KEY_ID), keyId) } @Test @@ -86,7 +88,7 @@ class PGPKeyManagerTest { // Check if the key id returned is correct val keyId = keyManager.getKeyId(keyManager.removeKey(key).unwrap()) - assertEquals(CryptoConstants.KEY_ID, keyId) + assertEquals(KeyId(CryptoConstants.KEY_ID), keyId) // Check if the keys directory have 0 files val keysDir = File(filesDir, PGPKeyManager.KEY_DIR_NAME) @@ -101,7 +103,7 @@ class PGPKeyManagerTest { val keyId = keyManager.getKeyId(key) assertNotNull(keyId) - assertEquals(CryptoConstants.KEY_ID, keyManager.getKeyId(key)) + assertEquals(KeyId(CryptoConstants.KEY_ID), keyManager.getKeyId(key)) // Check returned key id matches the expected id and the created key id val returnedKey = keyManager.getKeyById(keyId).unwrap() @@ -114,7 +116,7 @@ class PGPKeyManagerTest { keyManager.addKey(key).unwrap() val keyId = "${CryptoConstants.KEY_NAME} <${CryptoConstants.KEY_EMAIL}>" - val returnedKey = keyManager.getKeyById(keyId).unwrap() + val returnedKey = keyManager.getKeyById(UserId(keyId)).unwrap() assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) } @@ -124,7 +126,7 @@ class PGPKeyManagerTest { keyManager.addKey(key).unwrap() val keyId = CryptoConstants.KEY_EMAIL - val returnedKey = keyManager.getKeyById(keyId).unwrap() + val returnedKey = keyManager.getKeyById(UserId(keyId)).unwrap() assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) } @@ -134,19 +136,19 @@ class PGPKeyManagerTest { // Add key using KeyManager keyManager.addKey(key).unwrap() - val randomKeyId = "0x123456789" + val keyId = KeyId(0x08edf7567183ce44) // Check returned key - val error = keyManager.getKeyById(randomKeyId).unwrapError() + val error = keyManager.getKeyById(keyId).unwrapError() assertIs<KeyManagerException.KeyNotFoundException>(error) - assertEquals("No key found with id: $randomKeyId", error.message) + assertEquals("No key found with id: $keyId", error.message) } @Test fun testFindKeysWithoutAdding() = scope.runTest { // Check returned key - val error = keyManager.getKeyById("0x123456789").unwrapError() + val error = keyManager.getKeyById(KeyId(0x08edf7567183ce44)).unwrapError() assertIs<KeyManagerException.NoKeysAvailableException>(error) assertEquals("No keys were found", error.message) } diff --git a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPainlessCryptoHandlerTest.kt b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPainlessCryptoHandlerTest.kt index 6ec6ccee..9b4cb664 100644 --- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPainlessCryptoHandlerTest.kt +++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPainlessCryptoHandlerTest.kt @@ -14,8 +14,8 @@ import kotlin.test.assertTrue class PGPainlessCryptoHandlerTest { private val cryptoHandler = PGPainlessCryptoHandler() - private val privateKey = Key(TestUtils.getArmoredPrivateKey()) - private val publicKey = Key(TestUtils.getArmoredPublicKey()) + private val privateKey = PGPKey(TestUtils.getArmoredPrivateKey()) + private val publicKey = PGPKey(TestUtils.getArmoredPublicKey()) @Test fun encryptAndDecrypt() { |