mirror of
https://github.com/pgpainless/pgpainless.git
synced 2024-11-27 06:42:05 +01:00
Add tests for PGPKeyRingCollection
This commit is contained in:
parent
ed8584df95
commit
6b99f0aadc
3 changed files with 92 additions and 13 deletions
|
@ -22,7 +22,8 @@ import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||||
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||||
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
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 javax.annotation.Nonnull;
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
|
@ -37,12 +38,12 @@ import java.util.List;
|
||||||
* {@link PGPSecretKeyRingCollection} and {@link PGPPublicKeyRingCollection}.
|
* {@link PGPSecretKeyRingCollection} and {@link PGPPublicKeyRingCollection}.
|
||||||
*/
|
*/
|
||||||
public class PGPKeyRingCollection {
|
public class PGPKeyRingCollection {
|
||||||
private PGPSecretKeyRingCollection pgpSecretKeyRingCollection;
|
|
||||||
private PGPPublicKeyRingCollection pgpPublicKeyRingCollection;
|
|
||||||
|
|
||||||
public PGPKeyRingCollection(@Nonnull byte[] encoding, @Nonnull KeyFingerPrintCalculator fingerPrintCalculator,
|
private final PGPSecretKeyRingCollection pgpSecretKeyRingCollection;
|
||||||
boolean isSilent) throws IOException, PGPException {
|
private final PGPPublicKeyRingCollection pgpPublicKeyRingCollection;
|
||||||
this(new ByteArrayInputStream(encoding), fingerPrintCalculator, isSilent);
|
|
||||||
|
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 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}
|
* @throws PGPException if an object is encountered which isn't a {@link PGPSecretKeyRing} or {@link PGPPublicKeyRing}
|
||||||
*/
|
*/
|
||||||
public PGPKeyRingCollection(@Nonnull InputStream in, @Nonnull KeyFingerPrintCalculator fingerPrintCalculator,
|
public PGPKeyRingCollection(@Nonnull InputStream in, boolean isSilent) throws IOException, PGPException {
|
||||||
boolean isSilent) throws IOException, PGPException {
|
// Double getDecoderStream because of #96
|
||||||
PGPObjectFactory pgpFact = new PGPObjectFactory(in, fingerPrintCalculator);
|
InputStream decoderStream = PGPUtil.getDecoderStream(PGPUtil.getDecoderStream(in));
|
||||||
|
PGPObjectFactory pgpFact = new PGPObjectFactory(decoderStream, ImplementationFactory.getInstance().getKeyFingerprintCalculator());
|
||||||
Object obj;
|
Object obj;
|
||||||
|
|
||||||
List<PGPSecretKeyRing> secretKeyRings = new ArrayList<>();
|
List<PGPSecretKeyRing> secretKeyRings = new ArrayList<>();
|
||||||
|
|
|
@ -130,10 +130,7 @@ public class KeyRingReader {
|
||||||
|
|
||||||
public static PGPKeyRingCollection readKeyRingCollection(@Nonnull InputStream inputStream, boolean isSilent)
|
public static PGPKeyRingCollection readKeyRingCollection(@Nonnull InputStream inputStream, boolean isSilent)
|
||||||
throws IOException, PGPException {
|
throws IOException, PGPException {
|
||||||
return new PGPKeyRingCollection(
|
return new PGPKeyRingCollection(inputStream, isSilent);
|
||||||
getDecoderStream(inputStream),
|
|
||||||
ImplementationFactory.getInstance().getKeyFingerprintCalculator(),
|
|
||||||
isSilent);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -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<PGPKeyRing> keys = Arrays.asList(first, second, secondPub);
|
||||||
|
|
||||||
|
PGPKeyRingCollection collection = new PGPKeyRingCollection(keys, true);
|
||||||
|
assertEquals(2, collection.getPGPSecretKeyRingCollection().size());
|
||||||
|
assertEquals(1, collection.getPgpPublicKeyRingCollection().size());
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in a new issue