summaryrefslogtreecommitdiff
path: root/crypto-pgpainless/src/test
diff options
context:
space:
mode:
authorHarsh Shandilya <me@msfjarvis.dev>2021-10-28 17:57:05 +0530
committerGitHub <noreply@github.com>2021-10-28 17:57:05 +0530
commit7570bc84510e8bdf7440c52168e19874885638b8 (patch)
tree03818b913dc577f5620c804a1124e15470938ef4 /crypto-pgpainless/src/test
parent22ed045ea70658038f0c2b65ce5bc68f56660f26 (diff)
Add encrypt/decrypt tests for PGPainless (#1527)
* Remove redundant visibility modifiers * Move key getter to TestUtils * Add encrypt/decrypt tests to PGPainlessCryptoHandler
Diffstat (limited to 'crypto-pgpainless/src/test')
-rw-r--r--crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/CryptoConstants.kt12
-rw-r--r--crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyManagerTest.kt24
-rw-r--r--crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyPairTest.kt9
-rw-r--r--crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPainlessCryptoHandlerTest.kt43
-rw-r--r--crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/TestUtils.kt10
5 files changed, 74 insertions, 24 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 fad5308c..7aef4675 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
@@ -5,10 +5,10 @@
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"
+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 = "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
index 12bb85ad..87e1d567 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
@@ -13,9 +13,9 @@ import org.junit.Test
import org.junit.rules.TemporaryFolder
@OptIn(ExperimentalCoroutinesApi::class)
-public class PGPKeyManagerTest {
+class PGPKeyManagerTest {
- @get:Rule public val temporaryFolder: TemporaryFolder = TemporaryFolder()
+ @get:Rule 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)
@@ -24,10 +24,10 @@ public class PGPKeyManagerTest {
private val keyManager by lazy(LazyThreadSafetyMode.NONE) {
PGPKeyManager(filesDir.absolutePath, testCoroutineDispatcher)
}
- private val key = PGPKeyManager.makeKey(getArmoredKey())
+ private val key = PGPKeyManager.makeKey(TestUtils.getArmoredPrivateKey())
@Test
- public fun testAddingKey() {
+ fun testAddingKey() {
runBlockingTest {
// Check if the key id returned is correct
val keyId = keyManager.addKey(key).unwrap().getKeyId()
@@ -43,7 +43,7 @@ public class PGPKeyManagerTest {
}
@Test
- public fun testAddingKeyWithoutReplaceFlag() {
+ fun testAddingKeyWithoutReplaceFlag() {
runBlockingTest {
// Check adding the keys twice
keyManager.addKey(key, false).unwrap()
@@ -54,7 +54,7 @@ public class PGPKeyManagerTest {
}
@Test
- public fun testAddingKeyWithReplaceFlag() {
+ fun testAddingKeyWithReplaceFlag() {
runBlockingTest {
// Check adding the keys twice
keyManager.addKey(key, true).unwrap()
@@ -65,7 +65,7 @@ public class PGPKeyManagerTest {
}
@Test
- public fun testRemovingKey() {
+ fun testRemovingKey() {
runBlockingTest {
// Add key using KeyManager
keyManager.addKey(key).unwrap()
@@ -81,7 +81,7 @@ public class PGPKeyManagerTest {
}
@Test
- public fun testGetExistingKey() {
+ fun testGetExistingKey() {
runBlockingTest {
// Add key using KeyManager
keyManager.addKey(key).unwrap()
@@ -94,7 +94,7 @@ public class PGPKeyManagerTest {
}
@Test
- public fun testGetNonExistentKey() {
+ fun testGetNonExistentKey() {
runBlockingTest {
// Add key using KeyManager
keyManager.addKey(key).unwrap()
@@ -109,7 +109,7 @@ public class PGPKeyManagerTest {
}
@Test
- public fun testFindKeysWithoutAdding() {
+ fun testFindKeysWithoutAdding() {
runBlockingTest {
// Check returned key
val error = keyManager.getKeyById("0x123456789").unwrapError()
@@ -119,7 +119,7 @@ public class PGPKeyManagerTest {
}
@Test
- public fun testGettingAllKeys() {
+ fun testGettingAllKeys() {
runBlockingTest {
// TODO: Should we check for more than 1 keys?
// Check if KeyManager returns no key
@@ -134,6 +134,4 @@ public class PGPKeyManagerTest {
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
index 9fc3ed69..45b25f30 100644
--- a/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyPairTest.kt
+++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPKeyPairTest.kt
@@ -9,15 +9,14 @@ import kotlin.test.Test
import kotlin.test.assertEquals
import org.pgpainless.PGPainless
-public class PGPKeyPairTest {
+class PGPKeyPairTest {
@Test
- public fun testIfKeyIdIsCorrect() {
- val secretKey = PGPainless.readKeyRing().secretKeyRing(getKey()).secretKey
+ fun testIfKeyIdIsCorrect() {
+ val secretKey =
+ PGPainless.readKeyRing().secretKeyRing(TestUtils.getArmoredPrivateKey()).secretKey
val keyPair = PGPKeyPair(secretKey)
assertEquals(CryptoConstants.KEY_ID, keyPair.getKeyId())
}
-
- private fun getKey(): String = this::class.java.classLoader.getResource("private_key").readText()
}
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
new file mode 100644
index 00000000..c5afeaa2
--- /dev/null
+++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/PGPainlessCryptoHandlerTest.kt
@@ -0,0 +1,43 @@
+/*
+ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package dev.msfjarvis.aps.crypto
+
+import java.io.ByteArrayOutputStream
+import kotlin.test.Test
+import kotlin.test.assertEquals
+import kotlin.test.assertFalse
+import kotlin.test.assertTrue
+
+class PGPainlessCryptoHandlerTest {
+
+ private val cryptoHandler = PGPainlessCryptoHandler()
+
+ @Test
+ fun encrypt_and_decrypt() {
+ val key = TestUtils.getArmoredPrivateKey()
+ val ciphertextStream = ByteArrayOutputStream()
+ cryptoHandler.encrypt(
+ listOf(key),
+ CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8),
+ ciphertextStream,
+ )
+ val plaintextStream = ByteArrayOutputStream()
+ cryptoHandler.decrypt(
+ key,
+ CryptoConstants.KEY_PASSPHRASE,
+ ciphertextStream.toByteArray().inputStream(),
+ plaintextStream,
+ )
+ assertEquals(CryptoConstants.PLAIN_TEXT, plaintextStream.toString(Charsets.UTF_8))
+ }
+
+ @Test
+ fun can_handle_filters_formats() {
+ 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
new file mode 100644
index 00000000..1e01da51
--- /dev/null
+++ b/crypto-pgpainless/src/test/kotlin/dev/msfjarvis/aps/crypto/TestUtils.kt
@@ -0,0 +1,10 @@
+/*
+ * Copyright © 2014-2021 The Android Password Store Authors. All Rights Reserved.
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+package dev.msfjarvis.aps.crypto
+
+object TestUtils {
+ fun getArmoredPrivateKey() = this::class.java.classLoader.getResource("private_key").readText()
+}