1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 10:19:39 +02:00

Migrate some tests to new API

This commit is contained in:
Paul Schaub 2025-04-01 14:13:21 +02:00
parent 4260ed7969
commit 6273f93d59
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 29 additions and 36 deletions

View file

@ -12,8 +12,8 @@ import java.io.ByteArrayOutputStream;
import java.io.File; import java.io.File;
import java.io.IOException; import java.io.IOException;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.KeyFlag; import org.pgpainless.algorithm.KeyFlag;
@ -295,13 +295,13 @@ public class RoundTripEncryptDecryptCmdTest extends CLITest {
@Test @Test
public void testEncryptWithIncapableCert() throws IOException { public void testEncryptWithIncapableCert() throws IOException {
PGPSecretKeyRing secretKeys = PGPainless.buildKeyRing() PGPainless api = PGPainless.getInstance();
OpenPGPKey key = api.buildKey()
.addUserId("No Crypt <no@crypt.key>") .addUserId("No Crypt <no@crypt.key>")
.setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), .setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519),
KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA)) KeyFlag.CERTIFY_OTHER, KeyFlag.SIGN_DATA))
.build() .build();
.getPGPSecretKeyRing(); OpenPGPCertificate cert = key.toCertificate();
PGPPublicKeyRing cert = PGPainless.extractCertificate(secretKeys);
File certFile = writeFile("cert.pgp", cert.getEncoded()); File certFile = writeFile("cert.pgp", cert.getEncoded());
pipeStringToStdin("Hello, World!\n"); pipeStringToStdin("Hello, World!\n");
@ -315,15 +315,15 @@ public class RoundTripEncryptDecryptCmdTest extends CLITest {
@Test @Test
public void testSignWithIncapableKey() public void testSignWithIncapableKey()
throws IOException { throws IOException {
PGPSecretKeyRing secretKeys = PGPainless.buildKeyRing() PGPainless api = PGPainless.getInstance();
OpenPGPKey key = api.buildKey()
.addUserId("Cannot Sign <cannot@sign.key>") .addUserId("Cannot Sign <cannot@sign.key>")
.setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), KeyFlag.CERTIFY_OTHER)) .setPrimaryKey(KeySpec.getBuilder(KeyType.EDDSA_LEGACY(EdDSALegacyCurve._Ed25519), KeyFlag.CERTIFY_OTHER))
.addSubkey(KeySpec.getBuilder( .addSubkey(KeySpec.getBuilder(
KeyType.XDH_LEGACY(XDHLegacySpec._X25519), KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE)) KeyType.XDH_LEGACY(XDHLegacySpec._X25519), KeyFlag.ENCRYPT_COMMS, KeyFlag.ENCRYPT_STORAGE))
.build() .build();
.getPGPSecretKeyRing(); File keyFile = writeFile("key.pgp", key.getEncoded());
File keyFile = writeFile("key.pgp", secretKeys.getEncoded()); File certFile = writeFile("cert.pgp", key.toCertificate().getEncoded());
File certFile = writeFile("cert.pgp", PGPainless.extractCertificate(secretKeys).getEncoded());
pipeStringToStdin("Hello, World!\n"); pipeStringToStdin("Hello, World!\n");
ByteArrayOutputStream out = pipeStdoutToStream(); ByteArrayOutputStream out = pipeStdoutToStream();

View file

@ -17,9 +17,7 @@ import java.util.concurrent.ConcurrentHashMap;
import org.bouncycastle.bcpg.KeyIdentifier; import org.bouncycastle.bcpg.KeyIdentifier;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKey; import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.operator.PBESecretKeyDecryptor;
import org.jetbrains.annotations.NotNull; import org.jetbrains.annotations.NotNull;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.TestTemplate; import org.junit.jupiter.api.TestTemplate;
@ -37,22 +35,18 @@ public class SecretKeyRingProtectorTest {
public void testUnlockAllKeysWithSamePassword() public void testUnlockAllKeysWithSamePassword()
throws IOException, PGPException { throws IOException, PGPException {
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing(); OpenPGPKey key = TestKeys.getCryptieKey();
SecretKeyRingProtector protector = SecretKeyRingProtector protector =
SecretKeyRingProtector.unlockEachKeyWith(TestKeys.CRYPTIE_PASSPHRASE, secretKeys); SecretKeyRingProtector.unlockEachKeyWith(TestKeys.CRYPTIE_PASSPHRASE, key);
for (PGPSecretKey secretKey : secretKeys) { for (OpenPGPKey.OpenPGPSecretKey secretKey : key.getSecretKeys().values()) {
PBESecretKeyDecryptor decryptor = protector.getDecryptor(secretKey.getKeyIdentifier()); assertNotNull(secretKey.unlock(protector));
assertNotNull(decryptor);
secretKey.extractPrivateKey(decryptor);
} }
PGPSecretKeyRing unrelatedKeys = PGPainless.generateKeyRing().simpleEcKeyRing("unrelated",
"SecurePassword") OpenPGPKey unrelatedKey = PGPainless.getInstance().generateKey()
.getPGPSecretKeyRing(); .simpleEcKeyRing("unrelated",
for (PGPSecretKey unrelatedKey : unrelatedKeys) { "SecurePassword");
PBESecretKeyDecryptor decryptor = protector.getDecryptor(unrelatedKey.getKeyIdentifier()); for (OpenPGPKey.OpenPGPSecretKey k : unrelatedKey.getSecretKeys().values()) {
assertNull(decryptor); assertThrows(PGPException.class, () -> k.unlock(protector));
assertThrows(PGPException.class,
() -> unrelatedKey.extractPrivateKey(protector.getDecryptor(unrelatedKey.getKeyIdentifier())));
} }
} }
@ -70,16 +64,15 @@ public class SecretKeyRingProtectorTest {
@ExtendWith(TestAllImplementations.class) @ExtendWith(TestAllImplementations.class)
public void testUnlockSingleKeyWithPassphrase() public void testUnlockSingleKeyWithPassphrase()
throws IOException, PGPException { throws IOException, PGPException {
OpenPGPKey secretKeys = TestKeys.getCryptieKey();
PGPSecretKeyRing secretKeys = TestKeys.getCryptieSecretKeyRing(); Iterator<OpenPGPKey.OpenPGPSecretKey> iterator = secretKeys.getSecretKeys().values().iterator();
Iterator<PGPSecretKey> iterator = secretKeys.iterator(); OpenPGPKey.OpenPGPSecretKey key = iterator.next();
PGPSecretKey secretKey = iterator.next(); OpenPGPKey.OpenPGPSecretKey subKey = iterator.next();
PGPSecretKey subKey = iterator.next();
SecretKeyRingProtector protector = SecretKeyRingProtector protector =
SecretKeyRingProtector.unlockSingleKeyWith(TestKeys.CRYPTIE_PASSPHRASE, secretKey); SecretKeyRingProtector.unlockSingleKeyWith(TestKeys.CRYPTIE_PASSPHRASE, key);
assertNotNull(protector.getDecryptor(secretKey.getKeyIdentifier())); assertNotNull(protector.getDecryptor(key.getKeyIdentifier()));
assertNotNull(protector.getEncryptor(secretKey.getPublicKey())); assertNotNull(protector.getEncryptor(key.getPublicKey()));
assertNull(protector.getEncryptor(subKey.getPublicKey())); assertNull(protector.getEncryptor(subKey.getPublicKey()));
assertNull(protector.getDecryptor(subKey.getKeyIdentifier())); assertNull(protector.getDecryptor(subKey.getKeyIdentifier()));
} }