From c8694840d860a9f35e0ef46d7c3982ac10946681 Mon Sep 17 00:00:00 2001 From: Paul Schaub Date: Tue, 1 Apr 2025 14:13:21 +0200 Subject: [PATCH] Migrate some tests to new API --- .../RoundTripEncryptDecryptCmdTest.java | 22 +++++----- .../SecretKeyRingProtectorTest.java | 43 ++++++++----------- 2 files changed, 29 insertions(+), 36 deletions(-) diff --git a/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/RoundTripEncryptDecryptCmdTest.java b/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/RoundTripEncryptDecryptCmdTest.java index e4fd9e0c..bafc19e9 100644 --- a/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/RoundTripEncryptDecryptCmdTest.java +++ b/pgpainless-cli/src/test/java/org/pgpainless/cli/commands/RoundTripEncryptDecryptCmdTest.java @@ -12,8 +12,8 @@ import java.io.ByteArrayOutputStream; import java.io.File; import java.io.IOException; -import org.bouncycastle.openpgp.PGPPublicKeyRing; -import org.bouncycastle.openpgp.PGPSecretKeyRing; +import org.bouncycastle.openpgp.api.OpenPGPCertificate; +import org.bouncycastle.openpgp.api.OpenPGPKey; import org.junit.jupiter.api.Test; import org.pgpainless.PGPainless; import org.pgpainless.algorithm.KeyFlag; @@ -295,13 +295,13 @@ public class RoundTripEncryptDecryptCmdTest extends CLITest { @Test public void testEncryptWithIncapableCert() throws IOException { - PGPSecretKeyRing secretKeys = PGPainless.buildKeyRing() + PGPainless api = PGPainless.getInstance(); + OpenPGPKey key = api.buildKey() .addUserId("No Crypt ") .setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA)) - .build() - .getPGPSecretKeyRing(); - PGPPublicKeyRing cert = PGPainless.extractCertificate(secretKeys); + .build(); + OpenPGPCertificate cert = key.toCertificate(); File certFile = writeFile("cert.pgp", cert.getEncoded()); pipeStringToStdin("Hello, World!\n"); @@ -315,15 +315,15 @@ public class RoundTripEncryptDecryptCmdTest extends CLITest { @Test public void testSignWithIncapableKey() throws IOException { - PGPSecretKeyRing secretKeys = PGPainless.buildKeyRing() + PGPainless api = PGPainless.getInstance(); + OpenPGPKey key = api.buildKey() .addUserId("Cannot Sign ") .setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), KeyFlag.CERTIFY_OTHER)) .addSubkey(KeySpec.getBuilder( KeyType.XDH_LEGACY(XDHLegacySpec._X25519), KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE)) - .build() - .getPGPSecretKeyRing(); - File keyFile = writeFile("key.pgp", secretKeys.getEncoded()); - File certFile = writeFile("cert.pgp", PGPainless.extractCertificate(secretKeys).getEncoded()); + .build(); + File keyFile = writeFile("key.pgp", key.getEncoded()); + File certFile = writeFile("cert.pgp", key.toCertificate().getEncoded()); pipeStringToStdin("Hello, World!\n"); ByteArrayOutputStream out = pipeStdoutToStream(); diff --git a/pgpainless-core/src/test/java/org/pgpainless/key/protection/SecretKeyRingProtectorTest.java b/pgpainless-core/src/test/java/org/pgpainless/key/protection/SecretKeyRingProtectorTest.java index d1b585a1..1b0e404c 100644 --- a/pgpainless-core/src/test/java/org/pgpainless/key/protection/SecretKeyRingProtectorTest.java +++ b/pgpainless-core/src/test/java/org/pgpainless/key/protection/SecretKeyRingProtectorTest.java @@ -17,9 +17,7 @@ import java.util.concurrent.ConcurrentHashMap; import org.bouncycastle.bcpg.KeyIdentifier; import org.bouncycastle.openpgp.PGPException; -import org.bouncycastle.openpgp.PGPSecretKey; -import org.bouncycastle.openpgp.PGPSecretKeyRing; -import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor; +import org.bouncycastle.openpgp.api.OpenPGPKey; import org.jetbrains.annotations.NotNull; import org.junit.jupiter.api.Test; import org.junit.jupiter.api.TestTemplate; @@ -37,22 +35,18 @@ public class SecretKeyRingProtectorTest { public void testUnlockAllKeysWithSamePassword() throws IOException, PGPException { - PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing(); + OpenPGPKey key = TestKeys.getCryptieKey(); SecretKeyRingProtector protector = - SecretKeyRingProtector.unlockEachKeyWith(TestKeys.CRYPTIE_PASSPHRASE, secretKeys); - for (PGPSecretKey secretKey : secretKeys) { - PBESecretKeyDecryptor decryptor = protector.getDecryptor(secretKey.getKeyIdentifier()); - assertNotNull(decryptor); - secretKey.extractPrivateKey(decryptor); + SecretKeyRingProtector.unlockEachKeyWith(TestKeys.CRYPTIE_PASSPHRASE, key); + for (OpenPGPKey.OpenPGPSecretKey secretKey : key.getSecretKeys().values()) { + assertNotNull(secretKey.unlock(protector)); } - PGPSecretKeyRing unrelatedKeys = PGPainless.generateKeyRing().simpleEcKeyRing("unrelated", - "SecurePassword") - .getPGPSecretKeyRing(); - for (PGPSecretKey unrelatedKey : unrelatedKeys) { - PBESecretKeyDecryptor decryptor = protector.getDecryptor(unrelatedKey.getKeyIdentifier()); - assertNull(decryptor); - assertThrows(PGPException.class, - () -> unrelatedKey.extractPrivateKey(protector.getDecryptor(unrelatedKey.getKeyIdentifier()))); + + OpenPGPKey unrelatedKey = PGPainless.getInstance().generateKey() + .simpleEcKeyRing("unrelated", + "SecurePassword"); + for (OpenPGPKey.OpenPGPSecretKey k : unrelatedKey.getSecretKeys().values()) { + assertThrows(PGPException.class, () -> k.unlock(protector)); } } @@ -70,16 +64,15 @@ public class SecretKeyRingProtectorTest { @ExtendWith(TestAllImplementations.class) public void testUnlockSingleKeyWithPassphrase() throws IOException, PGPException { - - PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing(); - Iterator iterator = secretKeys.iterator(); - PGPSecretKey secretKey = iterator.next(); - PGPSecretKey subKey = iterator.next(); + OpenPGPKey secretKeys = TestKeys.getCryptieKey(); + Iterator iterator = secretKeys.getSecretKeys().values().iterator(); + OpenPGPKey.OpenPGPSecretKey key = iterator.next(); + OpenPGPKey.OpenPGPSecretKey subKey = iterator.next(); SecretKeyRingProtector protector = - SecretKeyRingProtector.unlockSingleKeyWith(TestKeys.CRYPTIE_PASSPHRASE, secretKey); - assertNotNull(protector.getDecryptor(secretKey.getKeyIdentifier())); - assertNotNull(protector.getEncryptor(secretKey.getPublicKey())); + SecretKeyRingProtector.unlockSingleKeyWith(TestKeys.CRYPTIE_PASSPHRASE, key); + assertNotNull(protector.getDecryptor(key.getKeyIdentifier())); + assertNotNull(protector.getEncryptor(key.getPublicKey())); assertNull(protector.getEncryptor(subKey.getPublicKey())); assertNull(protector.getDecryptor(subKey.getKeyIdentifier())); }