From 6b99f0aadc9aafb9b312ba150cb9f432d2130614 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Thu, 10 Jun 2021 16:21:04 +0200 Subject: [PATCH] Add tests for PGPKeyRingCollection --- .../key/collection/PGPKeyRingCollection.java | 20 ++--- .../pgpainless/key/parsing/KeyRingReader.java | 5 +- .../collection/PGPKeyRingCollectionTest.java | 80 +++++++++++++++++++ 3 files changed, 92 insertions(+), 13 deletions(-) create mode 100644 pgpainless-core/src/test/java/org/pgpainless/key/collection/PGPKeyRingCollectionTest.java diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/collection/PGPKeyRingCollection.java b/pgpainless-core/src/main/java/org/pgpainless/key/collection/PGPKeyRingCollection.java index e6e55fca..76c578f9 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/collection/PGPKeyRingCollection.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/collection/PGPKeyRingCollection.java @@ -22,7 +22,8 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.PGPPublicKeyRingCollection; import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.PGPSecretKeyRingCollection; -import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator; +import org.bouncycastle.openpgp.PGPUtil; +import org.pgpainless.implementation.ImplementationFactory; import javax.annotation.Nonnull; import java.io.ByteArrayInputStream; @@ -37,12 +38,12 @@ import java.util.List; * {@link PGPSecretKeyRingCollection} and {@link PGPPublicKeyRingCollection}. */ public class PGPKeyRingCollection { - private PGPSecretKeyRingCollection pgpSecretKeyRingCollection; - private PGPPublicKeyRingCollection pgpPublicKeyRingCollection; - public PGPKeyRingCollection(@Nonnull byte[] encoding, @Nonnull KeyFingerPrintCalculator fingerPrintCalculator, - boolean isSilent) throws IOException, PGPException { - this(new ByteArrayInputStream(encoding), fingerPrintCalculator, isSilent); + private final PGPSecretKeyRingCollection pgpSecretKeyRingCollection; + private final PGPPublicKeyRingCollection pgpPublicKeyRingCollection; + + public PGPKeyRingCollection(@Nonnull byte[] encoding, boolean isSilent) throws IOException, PGPException { + this(new ByteArrayInputStream(encoding), isSilent); } /** @@ -53,9 +54,10 @@ public class PGPKeyRingCollection { * @throws IOException if a problem parsing the base stream occurs * @throws PGPException if an object is encountered which isn't a {@link PGPSecretKeyRing} or {@link PGPPublicKeyRing} */ - public PGPKeyRingCollection(@Nonnull InputStream in, @Nonnull KeyFingerPrintCalculator fingerPrintCalculator, - boolean isSilent) throws IOException, PGPException { - PGPObjectFactory pgpFact = new PGPObjectFactory(in, fingerPrintCalculator); + public PGPKeyRingCollection(@Nonnull InputStream in, boolean isSilent) throws IOException, PGPException { + // Double getDecoderStream because of #96 + InputStream decoderStream = PGPUtil.getDecoderStream(PGPUtil.getDecoderStream(in)); + PGPObjectFactory pgpFact = new PGPObjectFactory(decoderStream, ImplementationFactory.getInstance().getKeyFingerprintCalculator()); Object obj; List secretKeyRings = new ArrayList<>(); diff --git a/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java b/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java index 43cb8140..6678cac2 100644 --- a/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java +++ b/pgpainless-core/src/main/java/org/pgpainless/key/parsing/KeyRingReader.java @@ -130,10 +130,7 @@ public class KeyRingReader { public static PGPKeyRingCollection readKeyRingCollection(@Nonnull InputStream inputStream, boolean isSilent) throws IOException, PGPException { - return new PGPKeyRingCollection( - getDecoderStream(inputStream), - ImplementationFactory.getInstance().getKeyFingerprintCalculator(), - isSilent); + return new PGPKeyRingCollection(inputStream, isSilent); } /** diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/collection/PGPKeyRingCollectionTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/collection/PGPKeyRingCollectionTest.java new file mode 100644 index 00000000..839f1859 --- /dev/null +++ b/pgpainless-core/src/test/java/org/pgpainless/key/collection/PGPKeyRingCollectionTest.java @@ -0,0 +1,80 @@ +/* + * Copyright 2021 Paul Schaub. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.pgpainless.key.collection; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.IOException; +import java.nio.charset.StandardCharsets; +import java.security.InvalidAlgorithmParameterException; +import java.security.NoSuchAlgorithmException; +import java.util.Arrays; +import java.util.Collection; + +import org.bouncycastle.openpgp.PGPException; +import org.bouncycastle.openpgp.PGPKeyRing; +import org.bouncycastle.openpgp.PGPPublicKeyRing; +import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.junit.jupiter.api.Test; +import org.pgpainless.PGPainless; +import org.pgpainless.key.util.KeyRingUtils; + +public class PGPKeyRingCollectionTest { + + @Test + public void constructorThrowsForInvalidInput() throws PGPException, IOException { + // This is neither a public key, nor a private key + String invalidKeyRing = "-----BEGIN PGP SIGNATURE-----\n" + + "\n" + + "wsE7BAABCABlBYJgv4U3RxQAAAAAAB4AIHNhbHRAbm90YXRpb25zLnNlcXVvaWEt\n" + + "cGdwLm9yZyPTSDcCaeqXuP6nWREE/C94XG6ABwpIlZHM08WewmWgFiEE0aZuGiOx\n" + + "gsmYD3iM+/zIKgFeczAACgkQ+/zIKgFeczBdoQv+MqOUYG+LA0Sa6P/827ILn9lW\n" + + "4IQJZVBo/ssvIU0Q8GjvCl4c/kRf+VKSprfkBz+q+vjHI1Ob0rW8amuTvvlSzBQX\n" + + "6Kb20ahvRYDo445Twuo+/rnp+j5PdRpYITY665AhdwfHSYoEEMhqrEDSQGT8O2xO\n" + + "1115FJEJaepomY4K9rv2pI8+pX9jOht7sCBK/qkTy/8RC22VyYWoPLSpxldzOBAK\n" + + "/AcuVCXFGfBt6ybRDXKJJduOVDQ5UFCoC71tq4ah7YmwIMD+/cXYPd+0Xg66U8Eq\n" + + "3F9MPYCzDYwnhEYZPuHODucHXZ0xpiFRlRkVMZT//+3b4NdOONRf/8sz9YUIMNxB\n" + + "U2qrEUnByfz3k1ZdkNwn40jYYWHp7hxVmaIDN3v+o5lZfzrKLTWU6wZThZL2FQlf\n" + + "SqGzjkxOW/c5OcO/EtCUNwIATpou32d1srfK0wGy2ar/o40yezukr5DLTo6B67Hq\n" + + "Z0D2bD+77247LmD4iuHMHxTI3yv2J+ecfM11Ejir\n" + + "=pQWH\n" + + "-----END PGP SIGNATURE-----\n"; + + byte[] bytes = invalidKeyRing.getBytes(StandardCharsets.UTF_8); + + // silent = false -> Exception + assertThrows(PGPException.class, () -> + new PGPKeyRingCollection(bytes, false)); + + // silent = true -> No exception, but not keys either + PGPKeyRingCollection collection = new PGPKeyRingCollection(bytes, true); + assertEquals(0, collection.getPgpPublicKeyRingCollection().size()); + assertEquals(0, collection.getPGPSecretKeyRingCollection().size()); + } + + @Test + public void testConstructorFromCollection() throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException, IOException { + PGPSecretKeyRing first = PGPainless.generateKeyRing().simpleEcKeyRing("alice@wonderland.lit"); + PGPSecretKeyRing second = PGPainless.generateKeyRing().simpleEcKeyRing("bob@the-builder.tv"); + PGPPublicKeyRing secondPub = KeyRingUtils.publicKeyRingFrom(second); + Collection keys = Arrays.asList(first, second, secondPub); + + PGPKeyRingCollection collection = new PGPKeyRingCollection(keys, true); + assertEquals(2, collection.getPGPSecretKeyRingCollection().size()); + assertEquals(1, collection.getPgpPublicKeyRingCollection().size()); + } +}