diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2021-10-23 17:02:50 +0530 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-10-23 17:02:50 +0530 |
commit | aac74ae4515aa1d746f46287029441f5a945c98e (patch) | |
tree | 9d23e06592ecd884d6b58dd089692d9e4224a3f9 /crypto-pgpainless/src/test/kotlin/dev/msfjarvis | |
parent | 21c8653e6815ca34574e783a5ce7ac783b188228 (diff) |
Switch new PGP backend to use PGPainless (#1522)
* crypto-pgpainless: init
* crypto-pgpainless: add an opinionated CryptoHandler impl
* app: migrate to crypto-pgpainless
* crypto-pgp: remove
* github: remove now unused instrumentation tests job
* crypto-common: fixup package names
* wip(crypto-pgpainless): add `PGPKeyPair` and `PGPKeyManager`
Signed-off-by: Aditya Wasan <adityawasan55@gmail.com>
(cherry picked from commit 02d07e9e797a8600cc8c534a731dfffcc44cfdde)
* crypto-pgpainless: use hex-encoded key IDs
* crypto-pgpainless: replace legacy Gopenpgp-generated key file
* crypto-pgpainless: fix CryptoConstants source set
* crypto-pgpainless: fix tests
* crypto-pgpainless: reinstate PGPKeyManager tests
Co-authored-by: Aditya Wasan <adityawasan55@gmail.com>
Diffstat (limited to 'crypto-pgpainless/src/test/kotlin/dev/msfjarvis')
3 files changed, 176 insertions, 0 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 new file mode 100644 index 00000000..fad5308c --- /dev/null +++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/CryptoConstants.kt @@ -0,0 +1,14 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package dev.msfjarvis.aps.crypto + +internal object CryptoConstants { + internal const val KEY_PASSPHRASE = "hunter2" + internal const val PLAIN_TEXT = "encryption worthy content" + internal const val KEY_NAME = "John Doe" + internal const val KEY_EMAIL = "john.doe@example.com" + internal const val KEY_ID = "08edf7567183ce27" +} 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 new file mode 100644 index 00000000..12bb85ad --- /dev/null +++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt @@ -0,0 +1,139 @@ +package dev.msfjarvis.aps.crypto + +import com.github.michaelbull.result.unwrap +import com.github.michaelbull.result.unwrapError +import java.io.File +import kotlin.test.assertEquals +import kotlin.test.assertIs +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.TestCoroutineDispatcher +import kotlinx.coroutines.test.runBlockingTest +import org.junit.Rule +import org.junit.Test +import org.junit.rules.TemporaryFolder + +@OptIn(ExperimentalCoroutinesApi::class) +public class PGPKeyManagerTest { + + @get:Rule public val temporaryFolder: TemporaryFolder = TemporaryFolder() + private val filesDir by lazy(LazyThreadSafetyMode.NONE) { temporaryFolder.root } + private val keysDir by lazy(LazyThreadSafetyMode.NONE) { + File(filesDir, PGPKeyManager.KEY_DIR_NAME) + } + private val testCoroutineDispatcher = TestCoroutineDispatcher() + private val keyManager by lazy(LazyThreadSafetyMode.NONE) { + PGPKeyManager(filesDir.absolutePath, testCoroutineDispatcher) + } + private val key = PGPKeyManager.makeKey(getArmoredKey()) + + @Test + public fun testAddingKey() { + runBlockingTest { + // Check if the key id returned is correct + val keyId = keyManager.addKey(key).unwrap().getKeyId() + assertEquals(CryptoConstants.KEY_ID, keyId) + + // Check if the keys directory have one file + assertEquals(1, filesDir.list()?.size) + + // Check if the file name is correct + val keyFile = keysDir.listFiles()?.first() + assertEquals(keyFile?.name, "$keyId.${PGPKeyManager.KEY_EXTENSION}") + } + } + + @Test + public fun testAddingKeyWithoutReplaceFlag() { + runBlockingTest { + // Check adding the keys twice + keyManager.addKey(key, false).unwrap() + val error = keyManager.addKey(key, false).unwrapError() + + assertIs<KeyManagerException.KeyAlreadyExistsException>(error) + } + } + + @Test + public fun testAddingKeyWithReplaceFlag() { + runBlockingTest { + // Check adding the keys twice + keyManager.addKey(key, true).unwrap() + val keyId = keyManager.addKey(key, true).unwrap().getKeyId() + + assertEquals(CryptoConstants.KEY_ID, keyId) + } + } + + @Test + public fun testRemovingKey() { + runBlockingTest { + // Add key using KeyManager + keyManager.addKey(key).unwrap() + + // Check if the key id returned is correct + val keyId = keyManager.removeKey(key).unwrap().getKeyId() + assertEquals(CryptoConstants.KEY_ID, keyId) + + // Check if the keys directory have 0 files + val keysDir = File(filesDir, PGPKeyManager.KEY_DIR_NAME) + assertEquals(0, keysDir.list()?.size) + } + } + + @Test + public fun testGetExistingKey() { + runBlockingTest { + // Add key using KeyManager + keyManager.addKey(key).unwrap() + + // 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()) + } + } + + @Test + public fun testGetNonExistentKey() { + runBlockingTest { + // Add key using KeyManager + keyManager.addKey(key).unwrap() + + val randomKeyId = "0x123456789" + + // Check returned key + val error = keyManager.getKeyById(randomKeyId).unwrapError() + assertIs<KeyManagerException.KeyNotFoundException>(error) + assertEquals("No key found with id: $randomKeyId", error.message) + } + } + + @Test + public fun testFindKeysWithoutAdding() { + runBlockingTest { + // Check returned key + val error = keyManager.getKeyById("0x123456789").unwrapError() + assertIs<KeyManagerException.NoKeysAvailableException>(error) + assertEquals("No keys were found", error.message) + } + } + + @Test + public fun testGettingAllKeys() { + runBlockingTest { + // TODO: Should we check for more than 1 keys? + // Check if KeyManager returns no key + val noKeyList = keyManager.getAllKeys().unwrap() + assertEquals(0, noKeyList.size) + + // Add key using KeyManager + keyManager.addKey(key).unwrap() + + // Check if KeyManager returns one key + val singleKeyList = keyManager.getAllKeys().unwrap() + assertEquals(1, singleKeyList.size) + } + } + + private fun getArmoredKey() = this::class.java.classLoader.getResource("private_key").readText() +} 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 new file mode 100644 index 00000000..9fc3ed69 --- /dev/null +++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyPairTest.kt @@ -0,0 +1,23 @@ +/* + * 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 + +public class PGPKeyPairTest { + + @Test + public fun testIfKeyIdIsCorrect() { + val secretKey = PGPainless.readKeyRing().secretKeyRing(getKey()).secretKey + val keyPair = PGPKeyPair(secretKey) + + assertEquals(CryptoConstants.KEY_ID, keyPair.getKeyId()) + } + + private fun getKey(): String = this::class.java.classLoader.getResource("private_key").readText() +} |