From 7480c47fa7c9fe9eac5029c02617ac0b6c79840a Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 8 Sep 2022 18:15:52 +0200 Subject: [PATCH] Add behavior test to ensure that ArmoredInputStream cuts away any data outside of the armor --- ...rArmoredDataWithAppendedCleartextTest.java | 38 +++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 pgpainless-core/src/test/java/org/bouncycastle/ArmoredInputStreamOverArmoredDataWithAppendedCleartextTest.java diff --git a/pgpainless-core/src/test/java/org/bouncycastle/ArmoredInputStreamOverArmoredDataWithAppendedCleartextTest.java b/pgpainless-core/src/test/java/org/bouncycastle/ArmoredInputStreamOverArmoredDataWithAppendedCleartextTest.java new file mode 100644 index 00000000..06921294 --- /dev/null +++ b/pgpainless-core/src/test/java/org/bouncycastle/ArmoredInputStreamOverArmoredDataWithAppendedCleartextTest.java @@ -0,0 +1,38 @@ +// SPDX-FileCopyrightText: 2022 Paul Schaub +// +// SPDX-License-Identifier: Apache-2.0 + +package org.bouncycastle; + +import org.bouncycastle.bcpg.ArmoredInputStream; +import org.bouncycastle.util.io.Streams; +import org.junit.jupiter.api.Test; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.nio.charset.StandardCharsets; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +public class ArmoredInputStreamOverArmoredDataWithAppendedCleartextTest { + + private static final String ASCII_ARMORED_WITH_APPENDED_GARBAGE = "-----BEGIN PGP MESSAGE-----\n" + + "Version: PGPainless\n" + + "\n" + + "yxRiAAAAAABIZWxsbywgV29ybGQhCg==\n" + + "=WGju\n" + + "-----END PGP MESSAGE-----\n" + + "This is a bunch of crap that we appended."; + @Test + public void testArmoredInputStreamCutsOffAnyDataAfterTheAsciiArmor() throws IOException { + InputStream inputStream = new ByteArrayInputStream(ASCII_ARMORED_WITH_APPENDED_GARBAGE.getBytes(StandardCharsets.UTF_8)); + ArmoredInputStream armorIn = new ArmoredInputStream(inputStream); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + Streams.pipeAll(armorIn, out); + armorIn.close(); + + assertEquals(22, out.size(), "ArmoredInputStream cuts off any appended data outside the ASCII armor."); + } +}