1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-11-26 22:32:07 +01:00

Test MissingDecryptionMethodException

This commit is contained in:
Paul Schaub 2021-05-28 23:29:41 +02:00
parent fd0734b247
commit 7bbc23d826
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -16,11 +16,13 @@
package org.pgpainless.symmetric_encryption;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Random;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
@ -31,7 +33,10 @@ import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.PGPainless;
import org.pgpainless.decryption_verification.DecryptionStream;
import org.pgpainless.encryption_signing.EncryptionBuilderInterface;
import org.pgpainless.encryption_signing.EncryptionOptions;
import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions;
import org.pgpainless.exception.MissingDecryptionMethodException;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.protection.KeyRingProtectionSettings;
@ -101,4 +106,28 @@ public class SymmetricEncryptionTest {
assertArrayEquals(plaintext, decrypted.toByteArray());
}
@ParameterizedTest
@MethodSource("org.pgpainless.util.TestUtil#provideImplementationFactories")
public void testMissmatchPassphraseFails(ImplementationFactory implementationFactory) throws IOException, PGPException {
ImplementationFactory.setFactoryImplementation(implementationFactory);
byte[] bytes = new byte[5000];
new Random().nextBytes(bytes);
ByteArrayOutputStream ciphertextOut = new ByteArrayOutputStream();
EncryptionStream encryptor = PGPainless.encryptAndOrSign().onOutputStream(ciphertextOut)
.withOptions(ProducerOptions.encrypt(
EncryptionOptions.encryptCommunications()
.addPassphrase(Passphrase.fromPassword("mellon"))));
Streams.pipeAll(new ByteArrayInputStream(bytes), encryptor);
encryptor.close();
assertThrows(MissingDecryptionMethodException.class, () -> PGPainless.decryptAndOrVerify()
.onInputStream(new ByteArrayInputStream(ciphertextOut.toByteArray()))
.decryptWith(Passphrase.fromPassword("meldir"))
.doNotVerify()
.build());
}
}