1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-23 12:52:07 +01:00

Fix checkstyle issues in tests and make small adjustments

This commit is contained in:
Paul Schaub 2021-03-23 01:06:15 +01:00
parent ce0bf970d6
commit 46140e6561
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 39 additions and 40 deletions

View file

@ -1,5 +1,5 @@
/* /*
* Copyright 2018 Paul Schaub. * Copyright 2021 Paul Schaub
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -15,16 +15,13 @@
*/ */
package org.pgpainless.key.parsing; package org.pgpainless.key.parsing;
import org.bouncycastle.bcpg.ArmoredInputStream; import static org.junit.jupiter.api.Assertions.assertEquals;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.key.util.KeyRingUtils;
import java.io.*; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.net.URL; import java.net.URL;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -34,16 +31,34 @@ import java.security.NoSuchAlgorithmException;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collection; import java.util.Collection;
import static org.junit.jupiter.api.Assertions.assertEquals; import org.bouncycastle.openpgp.PGPException;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPPublicKeyRingCollection;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPUtil;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.util.KeyRingUtils;
class KeyRingReaderTest { class KeyRingReaderTest {
@Test @Test
void publicKeyRingCollectionFromArmoredStream() throws IOException, PGPException { public void assertThatPGPUtilsDetectAsciiArmoredData() throws IOException, PGPException {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("pub_keys_10_pieces.asc"); InputStream inputStream = getClass().getClassLoader().getResourceAsStream("pub_keys_10_pieces.asc");
ArmoredInputStream armoredInputStream = new ArmoredInputStream(inputStream);
PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(armoredInputStream); InputStream possiblyArmored = PGPUtil.getDecoderStream(PGPUtil.getDecoderStream(inputStream));
assertEquals(rings.size(), 10);
PGPPublicKeyRingCollection collection = new PGPPublicKeyRingCollection(
possiblyArmored, ImplementationFactory.getInstance().getKeyFingerprintCalculator());
assertEquals(10, collection.size());
}
@Test
void publicKeyRingCollectionFromStream() throws IOException, PGPException {
InputStream inputStream = getClass().getClassLoader().getResourceAsStream("pub_keys_10_pieces.asc");
PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(inputStream);
assertEquals(10, rings.size());
} }
@Test @Test
@ -62,7 +77,7 @@ class KeyRingReaderTest {
ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray()); ByteArrayInputStream inputStream = new ByteArrayInputStream(out.toByteArray());
PGPPublicKeyRingCollection parsedRings = PGPainless.readKeyRing().publicKeyRingCollection(inputStream); PGPPublicKeyRingCollection parsedRings = PGPainless.readKeyRing().publicKeyRingCollection(inputStream);
assertEquals(parsedRings.size(), 10); assertEquals(10, parsedRings.size());
} }
@Test @Test
@ -70,33 +85,17 @@ class KeyRingReaderTest {
URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc"); URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc");
String armoredString = new String(Files.readAllBytes(new File(resource.toURI()).toPath())); String armoredString = new String(Files.readAllBytes(new File(resource.toURI()).toPath()));
InputStream inputStream = new ByteArrayInputStream(armoredString.getBytes(StandardCharsets.UTF_8)); InputStream inputStream = new ByteArrayInputStream(armoredString.getBytes(StandardCharsets.UTF_8));
ArmoredInputStream armoredInputStream = new ArmoredInputStream(inputStream); PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(inputStream);
PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(armoredInputStream); assertEquals(10, rings.size());
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);
assertNotEquals(rings.size(), 10);
} }
@Test @Test
void publicKeyRingCollectionFromBytes() throws IOException, PGPException, URISyntaxException { void publicKeyRingCollectionFromBytes() throws IOException, PGPException, URISyntaxException {
URL resource = getClass().getClassLoader().getResource("pub_keys_10_pieces.asc"); 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)); InputStream byteArrayInputStream = new ByteArrayInputStream(bytes);
PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(armoredInputStream); PGPPublicKeyRingCollection rings = PGPainless.readKeyRing().publicKeyRingCollection(byteArrayInputStream);
assertEquals(rings.size(), 10); assertEquals(10, rings.size());
} }
@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);
assertNotEquals(rings.size(), 10);
}
}

View file

@ -136,4 +136,4 @@ qdrEvcSyAOolaAMBCAeIdQQYFgoAHQUCYFHdcgIbDAUWAgMBAAQLCQgHBRUKCQgL
Ah4BAAoJEON4/e8A5cOxpusA/iyib7m5aFuStRWlBs9MYfmEv3vbDGrDZoN+6wj0 Ah4BAAoJEON4/e8A5cOxpusA/iyib7m5aFuStRWlBs9MYfmEv3vbDGrDZoN+6wj0
U3y7AQDskovK4Wd/vf2ljUOxP6knS01zriQpHy9ro5Qgi8bTCQ== U3y7AQDskovK4Wd/vf2ljUOxP6knS01zriQpHy9ro5Qgi8bTCQ==
=p/wh =p/wh
-----END PGP PUBLIC KEY BLOCK----- -----END PGP PUBLIC KEY BLOCK-----