diff options
author | Harsh Shandilya <me@msfjarvis.dev> | 2022-11-09 14:06:30 +0530 |
---|---|---|
committer | Harsh Shandilya <me@msfjarvis.dev> | 2022-11-09 14:59:38 +0530 |
commit | 53a6007cacfd8ad6dd904f01cce42af1cb56fbb9 (patch) | |
tree | 85f9234a84e714f8dce00608244db72890ba10c1 /crypto-pgpainless/src/test/kotlin | |
parent | 7e99d1718d8efb37568319326d1fb1c2b3eb320e (diff) |
feat(crypto-pgpainless): add PGPainlessCryptoHandler test for keys with shared email
Diffstat (limited to 'crypto-pgpainless/src/test/kotlin')
-rw-r--r-- | crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt | 50 |
1 files changed, 45 insertions, 5 deletions
diff --git a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt index 4bb08a2c..8bf6ba1e 100644 --- a/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt +++ b/crypto-pgpainless/src/test/kotlin/app/passwordstore/crypto/PGPainlessCryptoHandlerTest.kt @@ -6,6 +6,8 @@ package app.passwordstore.crypto +import app.passwordstore.crypto.CryptoConstants.KEY_PASSPHRASE +import app.passwordstore.crypto.CryptoConstants.PLAIN_TEXT import app.passwordstore.crypto.errors.IncorrectPassphraseException import com.github.michaelbull.result.Err import com.github.michaelbull.result.Ok @@ -20,6 +22,8 @@ import kotlin.test.assertFalse import kotlin.test.assertIs import kotlin.test.assertTrue import org.junit.runner.RunWith +import org.pgpainless.PGPainless +import org.pgpainless.decryption_verification.MessageInspector @Suppress("Unused") // Test runner handles it internally enum class EncryptionKey(val keySet: List<PGPKey>) { @@ -40,7 +44,7 @@ class PGPainlessCryptoHandlerTest { val encryptRes = cryptoHandler.encrypt( encryptionKey.keySet, - CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8), + PLAIN_TEXT.byteInputStream(Charsets.UTF_8), ciphertextStream, PGPEncryptOptions.Builder().build(), ) @@ -49,13 +53,13 @@ class PGPainlessCryptoHandlerTest { val decryptRes = cryptoHandler.decrypt( listOf(secretKey), - CryptoConstants.KEY_PASSPHRASE, + KEY_PASSPHRASE, ciphertextStream.toByteArray().inputStream(), plaintextStream, PGPDecryptOptions.Builder().build(), ) assertIs<Ok<Unit>>(decryptRes) - assertEquals(CryptoConstants.PLAIN_TEXT, plaintextStream.toString(Charsets.UTF_8)) + assertEquals(PLAIN_TEXT, plaintextStream.toString(Charsets.UTF_8)) } @Test @@ -64,7 +68,7 @@ class PGPainlessCryptoHandlerTest { val encryptRes = cryptoHandler.encrypt( encryptionKey.keySet, - CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8), + PLAIN_TEXT.byteInputStream(Charsets.UTF_8), ciphertextStream, PGPEncryptOptions.Builder().build(), ) @@ -88,7 +92,7 @@ class PGPainlessCryptoHandlerTest { val encryptRes = cryptoHandler.encrypt( encryptionKey.keySet, - CryptoConstants.PLAIN_TEXT.byteInputStream(Charsets.UTF_8), + PLAIN_TEXT.byteInputStream(Charsets.UTF_8), ciphertextStream, PGPEncryptOptions.Builder().withAsciiArmor(true).build(), ) @@ -100,6 +104,42 @@ class PGPainlessCryptoHandlerTest { } @Test + fun encryptMultiple() { + val alice = + PGPainless.generateKeyRing().modernKeyRing("Alice <owner@example.com>", KEY_PASSPHRASE) + val bob = PGPainless.generateKeyRing().modernKeyRing("Bob <owner@example.com>", KEY_PASSPHRASE) + val aliceKey = PGPKey(PGPainless.asciiArmor(alice).encodeToByteArray()) + val bobKey = PGPKey(PGPainless.asciiArmor(bob).encodeToByteArray()) + val ciphertextStream = ByteArrayOutputStream() + val encryptRes = + cryptoHandler.encrypt( + listOf(aliceKey, bobKey), + PLAIN_TEXT.byteInputStream(Charsets.UTF_8), + ciphertextStream, + PGPEncryptOptions.Builder().withAsciiArmor(true).build(), + ) + assertIs<Ok<Unit>>(encryptRes) + val message = ciphertextStream.toByteArray().decodeToString() + val info = MessageInspector.determineEncryptionInfoForMessage(message) + assertTrue(info.isEncrypted) + assertEquals(2, info.keyIds.size) + assertFalse(info.isSignedOnly) + for (key in listOf(aliceKey, bobKey)) { + val ciphertextStreamCopy = message.byteInputStream() + val plaintextStream = ByteArrayOutputStream() + val res = + cryptoHandler.decrypt( + listOf(key), + KEY_PASSPHRASE, + ciphertextStreamCopy, + plaintextStream, + PGPDecryptOptions.Builder().build(), + ) + assertIs<Ok<Unit>>(res) + } + } + + @Test fun canHandleFiltersFormats() { assertFalse { cryptoHandler.canHandle("example.com") } assertTrue { cryptoHandler.canHandle("example.com.gpg") } |