From 0dd54f27b7eeab8ce78c4dc52af3c8d6dc256c38 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 5 Sep 2022 15:43:32 +0200 Subject: [PATCH] Add test for processing message byte by byte --- .../DecryptAndVerifyMessageTest.java | 40 +++++++++++++++++++ 1 file changed, 40 insertions(+) diff --git a/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/DecryptAndVerifyMessageTest.java b/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/DecryptAndVerifyMessageTest.java index 152f4c33..f7402238 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/DecryptAndVerifyMessageTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/DecryptAndVerifyMessageTest.java @@ -78,4 +78,44 @@ public class DecryptAndVerifyMessageTest { assertTrue(metadata.containsVerifiedSignatureFrom(TestKeys.JULIET_FINGERPRINT)); assertEquals(new SubkeyIdentifier(TestKeys.JULIET_FINGERPRINT), metadata.getDecryptionKey()); } + + @TestTemplate + @ExtendWith(TestAllImplementations.class) + public void decryptMessageAndVerifySignatureByteByByteTest() throws Exception { + String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET; + + ConsumerOptions options = new ConsumerOptions() + .addDecryptionKey(juliet) + .addVerificationCert(KeyRingUtils.publicKeyRingFrom(juliet)); + + DecryptionStream decryptor = PGPainless.decryptAndOrVerify() + .onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes())) + .withOptions(options); + + ByteArrayOutputStream toPlain = new ByteArrayOutputStream(); + int r; + while ((r = decryptor.read()) != -1) { + toPlain.write(r); + } + + decryptor.close(); + toPlain.close(); + OpenPgpMetadata metadata = decryptor.getResult(); + + byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(UTF8); + byte[] actual = toPlain.toByteArray(); + + assertArrayEquals(expected, actual); + + assertTrue(metadata.isEncrypted()); + assertTrue(metadata.isSigned()); + assertFalse(metadata.isCleartextSigned()); + assertTrue(metadata.isVerified()); + assertEquals(CompressionAlgorithm.ZLIB, metadata.getCompressionAlgorithm()); + assertEquals(SymmetricKeyAlgorithm.AES_256, metadata.getSymmetricKeyAlgorithm()); + assertEquals(1, metadata.getSignatures().size()); + assertEquals(1, metadata.getVerifiedSignatures().size()); + assertTrue(metadata.containsVerifiedSignatureFrom(TestKeys.JULIET_FINGERPRINT)); + assertEquals(new SubkeyIdentifier(TestKeys.JULIET_FINGERPRINT), metadata.getDecryptionKey()); + } }