Add encrypt/decrypt tests for PGPainless (#1527)

* Remove redundant visibility modifiers

* Move key getter to TestUtils

* Add encrypt/decrypt tests to PGPainlessCryptoHandler
This commit is contained in:
Harsh Shandilya 2021-10-28 17:57:05 +05:30 committed by GitHub
parent 22ed045ea7
commit 7570bc8451
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 74 additions and 24 deletions

View file

@ -5,10 +5,10 @@
package dev.msfjarvis.aps.crypto package dev.msfjarvis.aps.crypto
internal object CryptoConstants { object CryptoConstants {
internal const val KEY_PASSPHRASE = "hunter2" const val KEY_PASSPHRASE = "hunter2"
internal const val PLAIN_TEXT = "encryption worthy content" const val PLAIN_TEXT = "encryption worthy content"
internal const val KEY_NAME = "John Doe" const val KEY_NAME = "John Doe"
internal const val KEY_EMAIL = "john.doe@example.com" const val KEY_EMAIL = "john.doe@example.com"
internal const val KEY_ID = "08edf7567183ce27" const val KEY_ID = "08edf7567183ce27"
} }

View file

@ -13,9 +13,9 @@ import org.junit.Test
import org.junit.rules.TemporaryFolder import org.junit.rules.TemporaryFolder
@OptIn(ExperimentalCoroutinesApi::class) @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 filesDir by lazy(LazyThreadSafetyMode.NONE) { temporaryFolder.root }
private val keysDir by lazy(LazyThreadSafetyMode.NONE) { private val keysDir by lazy(LazyThreadSafetyMode.NONE) {
File(filesDir, PGPKeyManager.KEY_DIR_NAME) File(filesDir, PGPKeyManager.KEY_DIR_NAME)
@ -24,10 +24,10 @@ public class PGPKeyManagerTest {
private val keyManager by lazy(LazyThreadSafetyMode.NONE) { private val keyManager by lazy(LazyThreadSafetyMode.NONE) {
PGPKeyManager(filesDir.absolutePath, testCoroutineDispatcher) PGPKeyManager(filesDir.absolutePath, testCoroutineDispatcher)
} }
private val key = PGPKeyManager.makeKey(getArmoredKey()) private val key = PGPKeyManager.makeKey(TestUtils.getArmoredPrivateKey())
@Test @Test
public fun testAddingKey() { fun testAddingKey() {
runBlockingTest { runBlockingTest {
// Check if the key id returned is correct // Check if the key id returned is correct
val keyId = keyManager.addKey(key).unwrap().getKeyId() val keyId = keyManager.addKey(key).unwrap().getKeyId()
@ -43,7 +43,7 @@ public class PGPKeyManagerTest {
} }
@Test @Test
public fun testAddingKeyWithoutReplaceFlag() { fun testAddingKeyWithoutReplaceFlag() {
runBlockingTest { runBlockingTest {
// Check adding the keys twice // Check adding the keys twice
keyManager.addKey(key, false).unwrap() keyManager.addKey(key, false).unwrap()
@ -54,7 +54,7 @@ public class PGPKeyManagerTest {
} }
@Test @Test
public fun testAddingKeyWithReplaceFlag() { fun testAddingKeyWithReplaceFlag() {
runBlockingTest { runBlockingTest {
// Check adding the keys twice // Check adding the keys twice
keyManager.addKey(key, true).unwrap() keyManager.addKey(key, true).unwrap()
@ -65,7 +65,7 @@ public class PGPKeyManagerTest {
} }
@Test @Test
public fun testRemovingKey() { fun testRemovingKey() {
runBlockingTest { runBlockingTest {
// Add key using KeyManager // Add key using KeyManager
keyManager.addKey(key).unwrap() keyManager.addKey(key).unwrap()
@ -81,7 +81,7 @@ public class PGPKeyManagerTest {
} }
@Test @Test
public fun testGetExistingKey() { fun testGetExistingKey() {
runBlockingTest { runBlockingTest {
// Add key using KeyManager // Add key using KeyManager
keyManager.addKey(key).unwrap() keyManager.addKey(key).unwrap()
@ -94,7 +94,7 @@ public class PGPKeyManagerTest {
} }
@Test @Test
public fun testGetNonExistentKey() { fun testGetNonExistentKey() {
runBlockingTest { runBlockingTest {
// Add key using KeyManager // Add key using KeyManager
keyManager.addKey(key).unwrap() keyManager.addKey(key).unwrap()
@ -109,7 +109,7 @@ public class PGPKeyManagerTest {
} }
@Test @Test
public fun testFindKeysWithoutAdding() { fun testFindKeysWithoutAdding() {
runBlockingTest { runBlockingTest {
// Check returned key // Check returned key
val error = keyManager.getKeyById("0x123456789").unwrapError() val error = keyManager.getKeyById("0x123456789").unwrapError()
@ -119,7 +119,7 @@ public class PGPKeyManagerTest {
} }
@Test @Test
public fun testGettingAllKeys() { fun testGettingAllKeys() {
runBlockingTest { runBlockingTest {
// TODO: Should we check for more than 1 keys? // TODO: Should we check for more than 1 keys?
// Check if KeyManager returns no key // Check if KeyManager returns no key
@ -134,6 +134,4 @@ public class PGPKeyManagerTest {
assertEquals(1, singleKeyList.size) assertEquals(1, singleKeyList.size)
} }
} }
private fun getArmoredKey() = this::class.java.classLoader.getResource("private_key").readText()
} }

View file

@ -9,15 +9,14 @@ import kotlin.test.Test
import kotlin.test.assertEquals import kotlin.test.assertEquals
import org.pgpainless.PGPainless import org.pgpainless.PGPainless
public class PGPKeyPairTest { class PGPKeyPairTest {
@Test @Test
public fun testIfKeyIdIsCorrect() { fun testIfKeyIdIsCorrect() {
val secretKey = PGPainless.readKeyRing().secretKeyRing(getKey()).secretKey val secretKey =
PGPainless.readKeyRing().secretKeyRing(TestUtils.getArmoredPrivateKey()).secretKey
val keyPair = PGPKeyPair(secretKey) val keyPair = PGPKeyPair(secretKey)
assertEquals(CryptoConstants.KEY_ID, keyPair.getKeyId()) assertEquals(CryptoConstants.KEY_ID, keyPair.getKeyId())
} }
private fun getKey(): String = this::class.java.classLoader.getResource("private_key").readText()
} }

View file

@ -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") }
}
}

View file

@ -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()
}