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

Port TryDecryptWithUnavailableGnuDummyKeyTest

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

View file

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