From a59092a564aac49764eb17e4176800f3c4e49223 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Mon, 13 Mar 2023 19:16:55 +0100 Subject: [PATCH] Refactor test --- .../openpgp/test/PGPv6KeyTest.java | 55 +++++++++++++------ 1 file changed, 38 insertions(+), 17 deletions(-) diff --git a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPv6KeyTest.java b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPv6KeyTest.java index d6a80b1b06..53dc9953a2 100644 --- a/pg/src/test/java/org/bouncycastle/openpgp/test/PGPv6KeyTest.java +++ b/pg/src/test/java/org/bouncycastle/openpgp/test/PGPv6KeyTest.java @@ -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" + @@ -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" + @@ -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 pIt = publicKeys.getPublicKeys(); - PGPPublicKey key = pIt.next(); + Iterator 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 sIt = secretKeys.getSecretKeys(); - PGPSecretKey sKey = sIt.next(); - isTrue(Arrays.areEqual(PRIMARY_FINGERPRINT, sKey.getFingerprint())); + Iterator 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) {