From f34c787a4dd75090ffe6adac1fb2d2e76be1a705 Mon Sep 17 00:00:00 2001 From: DenBond7 Date: Wed, 17 Mar 2021 13:49:31 +0200 Subject: [PATCH] Added working tests(string, bytes) to EncryptDecryptTest --- .../key/parsing/KeyRingReaderTest.java | 24 ++++++++++++++++--- 1 file changed, 21 insertions(+), 3 deletions(-) diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java index dd8712f5..72396414 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/parsing/KeyRingReaderTest.java @@ -21,11 +21,13 @@ import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; +import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.net.URISyntaxException; import java.net.URL; +import java.nio.charset.StandardCharsets; import java.nio.file.Files; import static org.junit.jupiter.api.Assertions.assertEquals; @@ -43,17 +45,33 @@ class KeyRingReaderTest { void publicKeyRingCollectionFromString() throws IOException, PGPException, URISyntaxException { URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc"); String armoredString = new String(Files.readAllBytes(new File(resource.toURI()).toPath())); - - PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(armoredString); + ArmoredInputStream armoredInputStream = new ArmoredInputStream(new ByteArrayInputStream(armoredString.getBytes(StandardCharsets.UTF_8))); + PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(armoredInputStream); assertEquals(rings.size(), 10); } @Test void publicKeyRingCollectionFromBytes() throws IOException, PGPException, URISyntaxException { URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc"); - byte[] bytes = Files.readAllBytes(new File(resource.toURI()).toPath()); + byte[] bytes = Files.readAllBytes(new File(resource.toURI()).toPath()); + InputStream armoredInputStream = new ArmoredInputStream(new ByteArrayInputStream(bytes)); + PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(armoredInputStream); + assertEquals(rings.size(), 10); + } + @Test + void publicKeyRingCollectionFromBytesFailed() throws IOException, PGPException, URISyntaxException { + URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc"); + byte[] bytes = Files.readAllBytes(new File(resource.toURI()).toPath()); PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(bytes); assertEquals(rings.size(), 10); } + + @Test + void publicKeyRingCollectionFromStringFailed() throws IOException, PGPException, URISyntaxException { + URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc"); + String armoredString = new String(Files.readAllBytes(new File(resource.toURI()).toPath())); + PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(armoredString); + assertEquals(rings.size(), 10); + } } \ No newline at end of file