From e8bf2ea9e7c38642a0031998b0f9133f6a865d58 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Wed, 27 Oct 2021 15:54:50 +0200 Subject: [PATCH] Add tests for message inspection --- .../MessageInspectorTest.java | 46 ++++++++++++++++--- 1 file changed, 39 insertions(+), 7 deletions(-) diff --git a/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/MessageInspectorTest.java b/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/MessageInspectorTest.java index 2f08a432..dd47ec4b 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/MessageInspectorTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/MessageInspectorTest.java @@ -8,9 +8,7 @@ import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertTrue; -import java.io.ByteArrayInputStream; import java.io.IOException; -import java.nio.charset.StandardCharsets; import org.bouncycastle.openpgp.PGPException; import org.junit.jupiter.api.Test; @@ -29,8 +27,7 @@ public class MessageInspectorTest { "=IICf\n" + "-----END PGP MESSAGE-----\n"; - MessageInspector.EncryptionInfo info = MessageInspector.determineEncryptionInfoForMessage( - new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))); + MessageInspector.EncryptionInfo info = MessageInspector.determineEncryptionInfoForMessage(message); assertFalse(info.isPassphraseEncrypted()); assertFalse(info.isSignedOnly()); @@ -55,7 +52,7 @@ public class MessageInspectorTest { "=z6e0\n" + "-----END PGP MESSAGE-----"; - MessageInspector.EncryptionInfo info = MessageInspector.determineEncryptionInfoForMessage(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))); + MessageInspector.EncryptionInfo info = MessageInspector.determineEncryptionInfoForMessage(message); assertTrue(info.isEncrypted()); assertTrue(info.isPassphraseEncrypted()); @@ -77,7 +74,7 @@ public class MessageInspectorTest { "=nt5n\n" + "-----END PGP MESSAGE-----"; - MessageInspector.EncryptionInfo info = MessageInspector.determineEncryptionInfoForMessage(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))); + MessageInspector.EncryptionInfo info = MessageInspector.determineEncryptionInfoForMessage(message); assertTrue(info.isSignedOnly()); @@ -100,7 +97,7 @@ public class MessageInspectorTest { "KK0Ymg5GrsBTEGFm4jb1p+V85PPhsIioX3np/N3fkIfxFguTGZza33/GHy61+DTy\n" + "=SZU6\n" + "-----END PGP MESSAGE-----"; - MessageInspector.EncryptionInfo info = MessageInspector.determineEncryptionInfoForMessage(new ByteArrayInputStream(message.getBytes(StandardCharsets.UTF_8))); + MessageInspector.EncryptionInfo info = MessageInspector.determineEncryptionInfoForMessage(message); // Message is encrypted, so we cannot determine if it is signed or not. // It is not signed only @@ -110,4 +107,39 @@ public class MessageInspectorTest { assertTrue(info.isPassphraseEncrypted()); assertEquals(1, info.getKeyIds().size()); } + + @Test + public void testPlaintextMessage() throws IOException, PGPException { + String message = "-----BEGIN PGP MESSAGE-----\n" + + "Version: PGPainless\n" + + "Comment: Literal Data\n" + + "\n" + + "yyl0CF9DT05TT0xFYXlXgUp1c3Qgc29tZSB1bmVuY3J5cHRlZCBkYXRhLg==\n" + + "=jVNT\n" + + "-----END PGP MESSAGE-----"; + + MessageInspector.EncryptionInfo info = MessageInspector.determineEncryptionInfoForMessage(message); + assertFalse(info.isEncrypted()); + assertFalse(info.isSignedOnly()); + assertFalse(info.isPassphraseEncrypted()); + assertTrue(info.getKeyIds().isEmpty()); + } + + @Test + public void testCompressedPlaintextMessage() throws IOException, PGPException { + String message = "-----BEGIN PGP MESSAGE-----\n" + + "Version: PGPainless\n" + + "Comment: Compressed Literal Data\n" + + "\n" + + "owE7HVDCEe/s7xfs7+OaWBmxJDw1L08hIzOvJLVIwS0nMzU9NQ9Op0FoHRgDQ0Fe\n" + + "YnKGHgA=\n" + + "=jw3E\n" + + "-----END PGP MESSAGE-----"; + + MessageInspector.EncryptionInfo info = MessageInspector.determineEncryptionInfoForMessage(message); + assertFalse(info.isEncrypted()); + assertFalse(info.isSignedOnly()); + assertFalse(info.isPassphraseEncrypted()); + assertTrue(info.getKeyIds().isEmpty()); + } }