From 549ee790d3e52bc62565ddf92e6a556e98b5195e Mon Sep 17 00:00:00 2001 From: Harsh Shandilya Date: Fri, 15 Jul 2022 00:53:48 +0530 Subject: all: re-do package structure yet again --- .../app/passwordstore/crypto/CryptoConstants.kt | 14 ++ .../app/passwordstore/crypto/GpgIdentifierTest.kt | 41 +++++ .../app/passwordstore/crypto/KeyUtilsTest.kt | 24 +++ .../app/passwordstore/crypto/PGPKeyManagerTest.kt | 189 +++++++++++++++++++++ .../crypto/PGPainlessCryptoHandlerTest.kt | 78 +++++++++ .../kotlin/app/passwordstore/crypto/TestUtils.kt | 16 ++ .../dev/msfjarvis/aps/crypto/CryptoConstants.kt | 14 -- .../dev/msfjarvis/aps/crypto/GpgIdentifierTest.kt | 41 ----- .../dev/msfjarvis/aps/crypto/KeyUtilsTest.kt | 24 --- .../dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt | 189 --------------------- .../aps/crypto/PGPainlessCryptoHandlerTest.kt | 78 --------- .../kotlin/dev/msfjarvis/aps/crypto/TestUtils.kt | 16 -- 12 files changed, 362 insertions(+), 362 deletions(-) create mode 100644 crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/CryptoConstants.kt create mode 100644 crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/GpgIdentifierTest.kt create mode 100644 crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/KeyUtilsTest.kt create mode 100644 crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt create mode 100644 crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt create mode 100644 crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/TestUtils.kt delete mode 100644 crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/CryptoConstants.kt delete mode 100644 crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifierTest.kt delete mode 100644 crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/KeyUtilsTest.kt delete mode 100644 crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt delete mode 100644 crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPainlessCryptoHandlerTest.kt delete mode 100644 crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/TestUtils.kt (limited to 'crypto-pgpainless/src/test/kotlin') diff --git a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/CryptoConstants.kt b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/CryptoConstants.kt new file mode 100644 index 00000000..d827e169 --- /dev/null +++ b/crypto-pgpainless/src/test/kotlin/app/passwordstore/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 app.passwordstore.crypto + +object CryptoConstants { + const val KEY_PASSPHRASE = "hunter2" + 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 +} diff --git a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/GpgIdentifierTest.kt b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/GpgIdentifierTest.kt new file mode 100644 index 00000000..009976a2 --- /dev/null +++ b/crypto-pgpainless/src/test/kotlin/app/passwordstore/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 app.passwordstore.crypto + +import kotlin.test.Test +import kotlin.test.assertNotNull +import kotlin.test.assertTrue + +class GpgIdentifierTest { + + @Test + fun parseHexKeyIdWithout0xPrefix() { + val identifier = GpgIdentifier.fromString("79E8208280490C77") + assertNotNull(identifier) + assertTrue { identifier is GpgIdentifier.KeyId } + } + + @Test + fun parseHexKeyId() { + val identifier = GpgIdentifier.fromString("0x79E8208280490C77") + assertNotNull(identifier) + assertTrue { identifier is GpgIdentifier.KeyId } + } + + @Test + fun parseValidEmail() { + val identifier = GpgIdentifier.fromString("john.doe@example.org") + assertNotNull(identifier) + assertTrue { identifier is GpgIdentifier.UserId } + } + + @Test + fun parseEmailWithoutTLD() { + val identifier = GpgIdentifier.fromString("john.doe@example") + assertNotNull(identifier) + assertTrue { identifier is GpgIdentifier.UserId } + } +} diff --git a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/KeyUtilsTest.kt b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/KeyUtilsTest.kt new file mode 100644 index 00000000..209865ea --- /dev/null +++ b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/KeyUtilsTest.kt @@ -0,0 +1,24 @@ +package app.passwordstore.crypto + +import app.passwordstore.crypto.KeyUtils.tryGetId +import app.passwordstore.crypto.KeyUtils.tryParseKeyring +import app.passwordstore.crypto.TestUtils.getArmoredPrivateKeyWithMultipleIdentities +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertIs +import kotlin.test.assertNotNull +import org.bouncycastle.openpgp.PGPSecretKeyRing + +class KeyUtilsTest { + @Test + fun parseKeyWithMultipleIdentities() { + val key = PGPKey(getArmoredPrivateKeyWithMultipleIdentities()) + val keyring = tryParseKeyring(key) + assertNotNull(keyring) + assertIs(keyring) + val keyId = tryGetId(key) + assertNotNull(keyId) + assertIs(keyId) + assertEquals("b950ae2813841585", keyId.toString()) + } +} diff --git a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt new file mode 100644 index 00000000..c446cee4 --- /dev/null +++ b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPKeyManagerTest.kt @@ -0,0 +1,189 @@ +package app.passwordstore.crypto + +import app.passwordstore.crypto.GpgIdentifier.KeyId +import app.passwordstore.crypto.GpgIdentifier.UserId +import app.passwordstore.crypto.TestUtils.getArmoredPrivateKeyWithMultipleIdentities +import app.passwordstore.crypto.errors.KeyAlreadyExistsException +import app.passwordstore.crypto.errors.KeyNotFoundException +import app.passwordstore.crypto.errors.NoKeysAvailableException +import com.github.michaelbull.result.unwrap +import com.github.michaelbull.result.unwrapError +import java.io.File +import kotlin.test.AfterTest +import kotlin.test.BeforeTest +import kotlin.test.Test +import kotlin.test.assertContentEquals +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 +import kotlinx.coroutines.test.TestScope +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.runTest +import kotlinx.coroutines.test.setMain +import org.junit.Rule +import org.junit.rules.TemporaryFolder + +@OptIn(ExperimentalCoroutinesApi::class) +class PGPKeyManagerTest { + + @get:Rule val temporaryFolder: TemporaryFolder = TemporaryFolder() + private val filesDir by unsafeLazy { temporaryFolder.root } + private val keysDir by unsafeLazy { File(filesDir, PGPKeyManager.KEY_DIR_NAME) } + private val dispatcher = StandardTestDispatcher() + private val scope = TestScope(dispatcher) + private val keyManager by unsafeLazy { PGPKeyManager(filesDir.absolutePath, dispatcher) } + private val key = PGPKey(TestUtils.getArmoredPrivateKey()) + + private fun unsafeLazy(initializer: () -> T) = + lazy(LazyThreadSafetyMode.NONE) { initializer.invoke() } + + @BeforeTest + fun setUp() { + Dispatchers.setMain(dispatcher) + } + + @AfterTest + fun tearDown() { + Dispatchers.resetMain() + } + + @Test + fun addKey() = + scope.runTest { + // Check if the key id returned is correct + val keyId = keyManager.getKeyId(keyManager.addKey(key).unwrap()) + assertEquals(KeyId(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 + fun addKeyWithoutReplaceFlag() = + scope.runTest { + // Check adding the keys twice + keyManager.addKey(key, false).unwrap() + val error = keyManager.addKey(key, false).unwrapError() + + assertIs(error) + } + + @Test + fun addKeyWithReplaceFlag() = + scope.runTest { + // Check adding the keys twice + keyManager.addKey(key, true).unwrap() + val keyId = keyManager.getKeyId(keyManager.addKey(key, true).unwrap()) + + assertEquals(KeyId(CryptoConstants.KEY_ID), keyId) + } + + @Test + fun removeKey() = + scope.runTest { + // Add key using KeyManager + keyManager.addKey(key).unwrap() + + // Check if the key id returned is correct + val keyId = keyManager.getKeyId(keyManager.removeKey(key).unwrap()) + assertEquals(KeyId(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 + fun getKeyById() = + scope.runTest { + // Add key using KeyManager + keyManager.addKey(key).unwrap() + + val keyId = keyManager.getKeyId(key) + assertNotNull(keyId) + 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() + assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) + } + + @Test + fun getKeyByFullUserId() = + scope.runTest { + keyManager.addKey(key).unwrap() + + val keyId = "${CryptoConstants.KEY_NAME} <${CryptoConstants.KEY_EMAIL}>" + val returnedKey = keyManager.getKeyById(UserId(keyId)).unwrap() + assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) + } + + @Test + fun getKeyByEmailUserId() = + scope.runTest { + keyManager.addKey(key).unwrap() + + val keyId = CryptoConstants.KEY_EMAIL + val returnedKey = keyManager.getKeyById(UserId(keyId)).unwrap() + assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) + } + + @Test + fun getNonExistentKey() = + scope.runTest { + // Add key using KeyManager + keyManager.addKey(key).unwrap() + + val keyId = KeyId(0x08edf7567183ce44) + + // Check returned key + val error = keyManager.getKeyById(keyId).unwrapError() + assertIs(error) + assertEquals("No key found with id: $keyId", error.message) + } + + @Test + fun findNonExistentKey() = + scope.runTest { + // Check returned key + val error = keyManager.getKeyById(KeyId(0x08edf7567183ce44)).unwrapError() + assertIs(error) + assertEquals("No keys were found", error.message) + } + + @Test + fun getAllKeys() = + scope.runTest { + // Check if KeyManager returns no key + val noKeyList = keyManager.getAllKeys().unwrap() + assertEquals(0, noKeyList.size) + + // Add key using KeyManager + keyManager.addKey(key).unwrap() + keyManager.addKey(PGPKey(getArmoredPrivateKeyWithMultipleIdentities())).unwrap() + + // Check if KeyManager returns one key + val singleKeyList = keyManager.getAllKeys().unwrap() + assertEquals(2, singleKeyList.size) + } + + @Test + fun getMultipleIdentityKeyWithAllIdentities() { + scope.runTest { + val key = PGPKey(getArmoredPrivateKeyWithMultipleIdentities()) + keyManager.addKey(key).unwrap() + + val johnKey = keyManager.getKeyById(UserId("john@doe.org")).unwrap() + val janeKey = keyManager.getKeyById(UserId("jane@doe.org")).unwrap() + + assertContentEquals(johnKey.contents, janeKey.contents) + } + } +} diff --git a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt new file mode 100644 index 00000000..60e8fb6e --- /dev/null +++ b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt @@ -0,0 +1,78 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ + +package app.passwordstore.crypto + +import app.passwordstore.crypto.errors.IncorrectPassphraseException +import com.github.michaelbull.result.Err +import com.github.michaelbull.result.getError +import com.google.testing.junit.testparameterinjector.TestParameter +import com.google.testing.junit.testparameterinjector.TestParameterInjector +import java.io.ByteArrayOutputStream +import kotlin.test.Test +import kotlin.test.assertEquals +import kotlin.test.assertFalse +import kotlin.test.assertIs +import kotlin.test.assertTrue +import org.junit.runner.RunWith + +@Suppress("Unused") // Test runner handles it internally +enum class EncryptionKey(val key: PGPKey) { + PUBLIC(PGPKey(TestUtils.getArmoredPublicKey())), + SECRET(PGPKey(TestUtils.getArmoredPrivateKey())), +} + +@RunWith(TestParameterInjector::class) +class PGPainlessCryptoHandlerTest { + + @TestParameter private lateinit var encryptionKey: EncryptionKey + private val cryptoHandler = PGPainlessCryptoHandler() + private val privateKey = PGPKey(TestUtils.getArmoredPrivateKey()) + + @Test + fun encryptAndDecrypt() { + val ciphertextStream = ByteArrayOutputStream() + cryptoHandler.encrypt( + listOf(encryptionKey.key), + CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8), + ciphertextStream, + ) + val plaintextStream = ByteArrayOutputStream() + cryptoHandler.decrypt( + privateKey, + CryptoConstants.KEY_PASSPHRASE, + ciphertextStream.toByteArray().inputStream(), + plaintextStream, + ) + assertEquals(CryptoConstants.PLAIN_TEXT, plaintextStream.toString(Charsets.UTF_8)) + } + + @Test + fun decryptWithWrongPassphrase() { + val ciphertextStream = ByteArrayOutputStream() + cryptoHandler.encrypt( + listOf(encryptionKey.key), + CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8), + ciphertextStream, + ) + val plaintextStream = ByteArrayOutputStream() + val result = + cryptoHandler.decrypt( + privateKey, + "very incorrect passphrase", + ciphertextStream.toByteArray().inputStream(), + plaintextStream, + ) + assertIs>(result) + assertIs(result.getError()) + } + + @Test + fun canHandleFiltersFormats() { + assertFalse { cryptoHandler.canHandle("example.com") } + assertTrue { cryptoHandler.canHandle("example.com.gpg") } + assertFalse { cryptoHandler.canHandle("example.com.asc") } + } +} diff --git a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/TestUtils.kt b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/TestUtils.kt new file mode 100644 index 00000000..1e5aea8c --- /dev/null +++ b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/TestUtils.kt @@ -0,0 +1,16 @@ +/* + * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. + * SPDX-License-Identifier: GPL-3.0-only + */ +@file:Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") + +package app.passwordstore.crypto + +object TestUtils { + fun getArmoredPrivateKey() = this::class.java.classLoader.getResource("private_key").readBytes() + fun getArmoredPublicKey() = this::class.java.classLoader.getResource("public_key").readBytes() + fun getArmoredPrivateKeyWithMultipleIdentities() = + this::class.java.classLoader.getResource("private_key_multiple_identities").readBytes() + fun getArmoredPublicKeyWithMultipleIdentities() = + this::class.java.classLoader.getResource("public_key_multiple_identities").readBytes() +} 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 deleted file mode 100644 index ede8f08d..00000000 --- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/CryptoConstants.kt +++ /dev/null @@ -1,14 +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 - -object CryptoConstants { - const val KEY_PASSPHRASE = "hunter2" - 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 -} 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 deleted file mode 100644 index aaf20cd8..00000000 --- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/GpgIdentifierTest.kt +++ /dev/null @@ -1,41 +0,0 @@ -/* - * 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 parseHexKeyIdWithout0xPrefix() { - val identifier = GpgIdentifier.fromString("79E8208280490C77") - assertNotNull(identifier) - assertTrue { identifier is GpgIdentifier.KeyId } - } - - @Test - fun parseHexKeyId() { - val identifier = GpgIdentifier.fromString("0x79E8208280490C77") - assertNotNull(identifier) - assertTrue { identifier is GpgIdentifier.KeyId } - } - - @Test - fun parseValidEmail() { - val identifier = GpgIdentifier.fromString("john.doe@example.org") - assertNotNull(identifier) - assertTrue { identifier is GpgIdentifier.UserId } - } - - @Test - fun parseEmailWithoutTLD() { - 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/KeyUtilsTest.kt b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/KeyUtilsTest.kt deleted file mode 100644 index 33597f9f..00000000 --- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/KeyUtilsTest.kt +++ /dev/null @@ -1,24 +0,0 @@ -package dev.msfjarvis.aps.crypto - -import dev.msfjarvis.aps.crypto.KeyUtils.tryGetId -import dev.msfjarvis.aps.crypto.KeyUtils.tryParseKeyring -import dev.msfjarvis.aps.crypto.TestUtils.getArmoredPrivateKeyWithMultipleIdentities -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertIs -import kotlin.test.assertNotNull -import org.bouncycastle.openpgp.PGPSecretKeyRing - -class KeyUtilsTest { - @Test - fun parseKeyWithMultipleIdentities() { - val key = PGPKey(getArmoredPrivateKeyWithMultipleIdentities()) - val keyring = tryParseKeyring(key) - assertNotNull(keyring) - assertIs(keyring) - val keyId = tryGetId(key) - assertNotNull(keyId) - assertIs(keyId) - assertEquals("b950ae2813841585", keyId.toString()) - } -} 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 deleted file mode 100644 index 60436d3b..00000000 --- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt +++ /dev/null @@ -1,189 +0,0 @@ -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 dev.msfjarvis.aps.crypto.TestUtils.getArmoredPrivateKeyWithMultipleIdentities -import dev.msfjarvis.aps.crypto.errors.KeyAlreadyExistsException -import dev.msfjarvis.aps.crypto.errors.KeyNotFoundException -import dev.msfjarvis.aps.crypto.errors.NoKeysAvailableException -import java.io.File -import kotlin.test.AfterTest -import kotlin.test.BeforeTest -import kotlin.test.Test -import kotlin.test.assertContentEquals -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 -import kotlinx.coroutines.test.TestScope -import kotlinx.coroutines.test.resetMain -import kotlinx.coroutines.test.runTest -import kotlinx.coroutines.test.setMain -import org.junit.Rule -import org.junit.rules.TemporaryFolder - -@OptIn(ExperimentalCoroutinesApi::class) -class PGPKeyManagerTest { - - @get:Rule val temporaryFolder: TemporaryFolder = TemporaryFolder() - private val filesDir by unsafeLazy { temporaryFolder.root } - private val keysDir by unsafeLazy { File(filesDir, PGPKeyManager.KEY_DIR_NAME) } - private val dispatcher = StandardTestDispatcher() - private val scope = TestScope(dispatcher) - private val keyManager by unsafeLazy { PGPKeyManager(filesDir.absolutePath, dispatcher) } - private val key = PGPKey(TestUtils.getArmoredPrivateKey()) - - private fun unsafeLazy(initializer: () -> T) = - lazy(LazyThreadSafetyMode.NONE) { initializer.invoke() } - - @BeforeTest - fun setUp() { - Dispatchers.setMain(dispatcher) - } - - @AfterTest - fun tearDown() { - Dispatchers.resetMain() - } - - @Test - fun addKey() = - scope.runTest { - // Check if the key id returned is correct - val keyId = keyManager.getKeyId(keyManager.addKey(key).unwrap()) - assertEquals(KeyId(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 - fun addKeyWithoutReplaceFlag() = - scope.runTest { - // Check adding the keys twice - keyManager.addKey(key, false).unwrap() - val error = keyManager.addKey(key, false).unwrapError() - - assertIs(error) - } - - @Test - fun addKeyWithReplaceFlag() = - scope.runTest { - // Check adding the keys twice - keyManager.addKey(key, true).unwrap() - val keyId = keyManager.getKeyId(keyManager.addKey(key, true).unwrap()) - - assertEquals(KeyId(CryptoConstants.KEY_ID), keyId) - } - - @Test - fun removeKey() = - scope.runTest { - // Add key using KeyManager - keyManager.addKey(key).unwrap() - - // Check if the key id returned is correct - val keyId = keyManager.getKeyId(keyManager.removeKey(key).unwrap()) - assertEquals(KeyId(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 - fun getKeyById() = - scope.runTest { - // Add key using KeyManager - keyManager.addKey(key).unwrap() - - val keyId = keyManager.getKeyId(key) - assertNotNull(keyId) - 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() - assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) - } - - @Test - fun getKeyByFullUserId() = - scope.runTest { - keyManager.addKey(key).unwrap() - - val keyId = "${CryptoConstants.KEY_NAME} <${CryptoConstants.KEY_EMAIL}>" - val returnedKey = keyManager.getKeyById(UserId(keyId)).unwrap() - assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) - } - - @Test - fun getKeyByEmailUserId() = - scope.runTest { - keyManager.addKey(key).unwrap() - - val keyId = CryptoConstants.KEY_EMAIL - val returnedKey = keyManager.getKeyById(UserId(keyId)).unwrap() - assertEquals(keyManager.getKeyId(key), keyManager.getKeyId(returnedKey)) - } - - @Test - fun getNonExistentKey() = - scope.runTest { - // Add key using KeyManager - keyManager.addKey(key).unwrap() - - val keyId = KeyId(0x08edf7567183ce44) - - // Check returned key - val error = keyManager.getKeyById(keyId).unwrapError() - assertIs(error) - assertEquals("No key found with id: $keyId", error.message) - } - - @Test - fun findNonExistentKey() = - scope.runTest { - // Check returned key - val error = keyManager.getKeyById(KeyId(0x08edf7567183ce44)).unwrapError() - assertIs(error) - assertEquals("No keys were found", error.message) - } - - @Test - fun getAllKeys() = - scope.runTest { - // Check if KeyManager returns no key - val noKeyList = keyManager.getAllKeys().unwrap() - assertEquals(0, noKeyList.size) - - // Add key using KeyManager - keyManager.addKey(key).unwrap() - keyManager.addKey(PGPKey(getArmoredPrivateKeyWithMultipleIdentities())).unwrap() - - // Check if KeyManager returns one key - val singleKeyList = keyManager.getAllKeys().unwrap() - assertEquals(2, singleKeyList.size) - } - - @Test - fun getMultipleIdentityKeyWithAllIdentities() { - scope.runTest { - val key = PGPKey(getArmoredPrivateKeyWithMultipleIdentities()) - keyManager.addKey(key).unwrap() - - val johnKey = keyManager.getKeyById(UserId("john@doe.org")).unwrap() - val janeKey = keyManager.getKeyById(UserId("jane@doe.org")).unwrap() - - assertContentEquals(johnKey.contents, janeKey.contents) - } - } -} 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 deleted file mode 100644 index e39bc06e..00000000 --- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPainlessCryptoHandlerTest.kt +++ /dev/null @@ -1,78 +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 com.github.michaelbull.result.Err -import com.github.michaelbull.result.getError -import com.google.testing.junit.testparameterinjector.TestParameter -import com.google.testing.junit.testparameterinjector.TestParameterInjector -import dev.msfjarvis.aps.crypto.errors.IncorrectPassphraseException -import java.io.ByteArrayOutputStream -import kotlin.test.Test -import kotlin.test.assertEquals -import kotlin.test.assertFalse -import kotlin.test.assertIs -import kotlin.test.assertTrue -import org.junit.runner.RunWith - -@Suppress("Unused") // Test runner handles it internally -enum class EncryptionKey(val key: PGPKey) { - PUBLIC(PGPKey(TestUtils.getArmoredPublicKey())), - SECRET(PGPKey(TestUtils.getArmoredPrivateKey())), -} - -@RunWith(TestParameterInjector::class) -class PGPainlessCryptoHandlerTest { - - @TestParameter private lateinit var encryptionKey: EncryptionKey - private val cryptoHandler = PGPainlessCryptoHandler() - private val privateKey = PGPKey(TestUtils.getArmoredPrivateKey()) - - @Test - fun encryptAndDecrypt() { - val ciphertextStream = ByteArrayOutputStream() - cryptoHandler.encrypt( - listOf(encryptionKey.key), - CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8), - ciphertextStream, - ) - val plaintextStream = ByteArrayOutputStream() - cryptoHandler.decrypt( - privateKey, - CryptoConstants.KEY_PASSPHRASE, - ciphertextStream.toByteArray().inputStream(), - plaintextStream, - ) - assertEquals(CryptoConstants.PLAIN_TEXT, plaintextStream.toString(Charsets.UTF_8)) - } - - @Test - fun decryptWithWrongPassphrase() { - val ciphertextStream = ByteArrayOutputStream() - cryptoHandler.encrypt( - listOf(encryptionKey.key), - CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8), - ciphertextStream, - ) - val plaintextStream = ByteArrayOutputStream() - val result = - cryptoHandler.decrypt( - privateKey, - "very incorrect passphrase", - ciphertextStream.toByteArray().inputStream(), - plaintextStream, - ) - assertIs>(result) - assertIs(result.getError()) - } - - @Test - fun canHandleFiltersFormats() { - assertFalse { cryptoHandler.canHandle("example.com") } - assertTrue { cryptoHandler.canHandle("example.com.gpg") } - assertFalse { cryptoHandler.canHandle("example.com.asc") } - } -} diff --git a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/TestUtils.kt b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/TestUtils.kt deleted file mode 100644 index d002180d..00000000 --- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/TestUtils.kt +++ /dev/null @@ -1,16 +0,0 @@ -/* - * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved. - * SPDX-License-Identifier: GPL-3.0-only - */ -@file:Suppress("RECEIVER_NULLABILITY_MISMATCH_BASED_ON_JAVA_ANNOTATIONS") - -package dev.msfjarvis.aps.crypto - -object TestUtils { - fun getArmoredPrivateKey() = this::class.java.classLoader.getResource("private_key").readBytes() - fun getArmoredPublicKey() = this::class.java.classLoader.getResource("public_key").readBytes() - fun getArmoredPrivateKeyWithMultipleIdentities() = - this::class.java.classLoader.getResource("private_key_multiple_identities").readBytes() - fun getArmoredPublicKeyWithMultipleIdentities() = - this::class.java.classLoader.getResource("public_key_multiple_identities").readBytes() -} -- cgit v1.2.3