1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2025-09-09 02:09:38 +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.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 <no@crypt.key>")
.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 <cannot@sign.key>")
.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();

View file

@ -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<PGPSecretKey> iterator = secretKeys.iterator();
PGPSecretKey secretKey = iterator.next();
PGPSecretKey subKey = iterator.next();
OpenPGPKey secretKeys = TestKeys.getCryptieKey();
Iterator<OpenPGPKey.OpenPGPSecretKey> 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()));
}