Skip to content

Commit

Permalink
Refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
vanitasvitae committed Mar 13, 2023
1 parent 2d381f2 commit a59092a
Showing 1 changed file with 38 additions and 17 deletions.
55 changes: 38 additions & 17 deletions pg/src/test/java/org/bouncycastle/openpgp/test/PGPv6KeyTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,25 @@

import org.bouncycastle.bcpg.ArmoredInputStream;
import org.bouncycastle.bcpg.BCPGInputStream;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
import org.bouncycastle.openpgp.operator.jcajce.JcaKeyFingerprintCalculator;
import org.bouncycastle.util.Arrays;
import org.bouncycastle.util.encoders.Hex;
import org.bouncycastle.util.test.SimpleTest;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.util.Iterator;

public class PGPv6KeyTest extends SimpleTest {

// test vector from https://openpgp-wg.gitlab.io/rfc4880bis/#name-sample-v6-certificate-trans
private static final String ARMORED_CERT = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n" +
"\n" +
"xioGY4d/4xsAAAAg+U2nu0jWCmHlZ3BqZYfQMxmZu52JGggkLq2EVD34laPCsQYf\n" +
Expand All @@ -29,6 +33,7 @@ public class PGPv6KeyTest extends SimpleTest {
"j+VjFM21J0hqWlEg+bdiojWnKfA5AQpWUWtnNwDEM0g12vYxoWM8Y81W+bHBw805\n" +
"I8kWVkXU6vFOi+HWvv/ira7ofJu16NnoUkhclkUrk0mXubZvyl4GBg==\n" +
"-----END PGP PUBLIC KEY BLOCK-----";
// test vector from https://openpgp-wg.gitlab.io/rfc4880bis/#name-sample-v6-secret-key-transf
private static final String ARMORED_KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
"\n" +
"xUsGY4d/4xsAAAAg+U2nu0jWCmHlZ3BqZYfQMxmZu52JGggkLq2EVD34laMAGXKB\n" +
Expand All @@ -43,42 +48,58 @@ public class PGPv6KeyTest extends SimpleTest {
"M0g12vYxoWM8Y81W+bHBw805I8kWVkXU6vFOi+HWvv/ira7ofJu16NnoUkhclkUr\n" +
"k0mXubZvyl4GBg==\n" +
"-----END PGP PRIVATE KEY BLOCK-----";

private static final byte[] PRIMARY_FINGERPRINT = Hex.decode("CB186C4F0609A697E4D52DFA6C722B0C1F1E27C18A56708F6525EC27BAD9ACC9");
private static final byte[] SUBKEY_FINGERPRINT = Hex.decode("12C83F1E706F6308FE151A417743A1F033790E93E9978488D1DB378DA9930885");


@Override
public String getName() {
return getClass().getSimpleName();
public void performTest() throws Exception {
testFingerprintCalculation();
}

@Override
public void performTest() throws Exception {
KeyFingerPrintCalculator fingerPrintCalculator = new BcKeyFingerprintCalculator();
private void testFingerprintCalculation() throws IOException, PGPException {
KeyFingerPrintCalculator bcFpCalc = new BcKeyFingerprintCalculator();
testCertificateFingerprintCalculation(bcFpCalc);
testKeyFingerprintCalculation(bcFpCalc);

KeyFingerPrintCalculator jcaFpCalc = new JcaKeyFingerprintCalculator();
testCertificateFingerprintCalculation(jcaFpCalc);
testKeyFingerprintCalculation(jcaFpCalc);
}

private void testCertificateFingerprintCalculation(KeyFingerPrintCalculator fingerPrintCalculator) throws IOException {
ByteArrayInputStream bIn = new ByteArrayInputStream(ARMORED_CERT.getBytes());
ArmoredInputStream armorIn = new ArmoredInputStream(bIn);
BCPGInputStream bcIn = new BCPGInputStream(armorIn);

PGPPublicKeyRing publicKeys = new PGPPublicKeyRing(bcIn, fingerPrintCalculator);

Iterator<PGPPublicKey> pIt = publicKeys.getPublicKeys();
PGPPublicKey key = pIt.next();
Iterator<PGPPublicKey> it = publicKeys.getPublicKeys();
PGPPublicKey key = it.next();
isTrue(Arrays.areEqual(PRIMARY_FINGERPRINT, key.getFingerprint()));
key = pIt.next();
key = it.next();
isTrue(Arrays.areEqual(SUBKEY_FINGERPRINT, key.getFingerprint()));
}

bIn = new ByteArrayInputStream(ARMORED_KEY.getBytes());
armorIn = new ArmoredInputStream(bIn);
bcIn = new BCPGInputStream(armorIn);
private void testKeyFingerprintCalculation(KeyFingerPrintCalculator fingerPrintCalculator)
throws IOException, PGPException
{
ByteArrayInputStream bIn = new ByteArrayInputStream(ARMORED_KEY.getBytes());
ArmoredInputStream armorIn = new ArmoredInputStream(bIn);
BCPGInputStream bcIn = new BCPGInputStream(armorIn);

PGPSecretKeyRing secretKeys = new PGPSecretKeyRing(bcIn, fingerPrintCalculator);

Iterator<PGPSecretKey> sIt = secretKeys.getSecretKeys();
PGPSecretKey sKey = sIt.next();
isTrue(Arrays.areEqual(PRIMARY_FINGERPRINT, sKey.getFingerprint()));
Iterator<PGPSecretKey> it = secretKeys.getSecretKeys();
PGPSecretKey key = it.next();
isTrue(Arrays.areEqual(PRIMARY_FINGERPRINT, key.getFingerprint()));
key = it.next();
isTrue(Arrays.areEqual(SUBKEY_FINGERPRINT, key.getFingerprint()));
}

sKey = sIt.next();
isTrue(Arrays.areEqual(SUBKEY_FINGERPRINT, sKey.getFingerprint()));
@Override
public String getName() {
return getClass().getSimpleName();
}

public static void main(String[] args) {
Expand Down

0 comments on commit a59092a

Please sign in to comment.