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

Port TryDecryptWithUnavailableGnuDummyKeyTest

This commit is contained in:
Paul Schaub 2025-04-02 15:59:04 +02:00
parent bad49de6aa
commit 0963f110a4
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -5,8 +5,8 @@
package org.pgpainless.decryption_verification; package org.pgpainless.decryption_verification;
import org.bouncycastle.openpgp.PGPException; import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing; import org.bouncycastle.openpgp.api.OpenPGPCertificate;
import org.bouncycastle.openpgp.PGPSecretKeyRing; import org.bouncycastle.openpgp.api.OpenPGPKey;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
@ -27,26 +27,27 @@ public class TryDecryptWithUnavailableGnuDummyKeyTest {
@Test @Test
public void testAttemptToDecryptWithRemovedPrivateKeysThrows() public void testAttemptToDecryptWithRemovedPrivateKeysThrows()
throws PGPException, IOException { throws PGPException, IOException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing() PGPainless api = PGPainless.getInstance();
.modernKeyRing("Hardy Hardware <hardy@hard.ware>") OpenPGPKey secretKeys = api.generateKey()
.getPGPSecretKeyRing(); .modernKeyRing("Hardy Hardware <hardy@hard.ware>");
PGPPublicKeyRing certificate = PGPainless.extractCertificate(secretKeys); OpenPGPCertificate certificate = secretKeys.toCertificate();
ByteArrayOutputStream ciphertextOut = new ByteArrayOutputStream(); ByteArrayOutputStream ciphertextOut = new ByteArrayOutputStream();
EncryptionStream encryptionStream = PGPainless.encryptAndOrSign() EncryptionStream encryptionStream = api.generateMessage()
.onOutputStream(ciphertextOut) .onOutputStream(ciphertextOut)
.withOptions( .withOptions(
ProducerOptions.encrypt(EncryptionOptions.get().addRecipient(certificate))); ProducerOptions.encrypt(EncryptionOptions.get(api).addRecipient(certificate)));
ByteArrayInputStream plaintextIn = new ByteArrayInputStream("Hello, World!\n".getBytes()); ByteArrayInputStream plaintextIn = new ByteArrayInputStream("Hello, World!\n".getBytes());
Streams.pipeAll(plaintextIn, encryptionStream); Streams.pipeAll(plaintextIn, encryptionStream);
encryptionStream.close(); encryptionStream.close();
PGPSecretKeyRing removedKeys = GnuPGDummyKeyUtil.modify(secretKeys) OpenPGPKey removedKeys = api.toKey(
.removePrivateKeys(GnuPGDummyKeyUtil.KeyFilter.any()); GnuPGDummyKeyUtil.modify(secretKeys)
.removePrivateKeys(GnuPGDummyKeyUtil.KeyFilter.any()));
ByteArrayInputStream ciphertextIn = new ByteArrayInputStream(ciphertextOut.toByteArray()); ByteArrayInputStream ciphertextIn = new ByteArrayInputStream(ciphertextOut.toByteArray());
assertThrows(MissingDecryptionMethodException.class, () -> PGPainless.decryptAndOrVerify() assertThrows(MissingDecryptionMethodException.class, () -> PGPainless.decryptAndOrVerify()
.onInputStream(ciphertextIn) .onInputStream(ciphertextIn)
.withOptions(ConsumerOptions.get().addDecryptionKey(removedKeys))); .withOptions(ConsumerOptions.get(api).addDecryptionKey(removedKeys)));
} }
} }