1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-16 08:34:53 +02:00
pgpainless/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/DecryptAndVerifyMessageTest.java

91 lines
3.7 KiB
Java
Raw Normal View History

/*
* Copyright 2020 Paul Schaub.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.pgpainless.decryption_verification;
2018-06-10 17:12:44 +02:00
2020-11-13 16:31:59 +01:00
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
2018-06-10 17:12:44 +02:00
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;
2018-06-10 17:12:44 +02:00
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
2021-07-31 20:40:31 +02:00
import org.bouncycastle.util.io.Streams;
2020-11-13 16:31:59 +01:00
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.implementation.ImplementationFactory;
import org.pgpainless.key.SubkeyIdentifier;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.util.KeyRingUtils;
2018-06-10 17:12:44 +02:00
public class DecryptAndVerifyMessageTest {
2018-06-10 17:12:44 +02:00
// Don't use StandardCharsets.UTF8 because of Android API level.
private static final Charset UTF8 = Charset.forName("UTF-8");
2018-06-10 17:12:44 +02:00
private PGPSecretKeyRing juliet;
private PGPSecretKeyRing romeo;
2020-11-13 16:31:59 +01:00
@BeforeEach
public void loadKeys() throws IOException, PGPException {
juliet = TestKeys.getJulietSecretKeyRing();
romeo = TestKeys.getRomeoSecretKeyRing();
2018-06-10 17:12:44 +02:00
}
@ParameterizedTest
@MethodSource("org.pgpainless.util.TestImplementationFactoryProvider#provideImplementationFactories")
public void decryptMessageAndVerifySignatureTest(ImplementationFactory implementationFactory) throws Exception {
ImplementationFactory.setFactoryImplementation(implementationFactory);
String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET;
2018-06-10 17:12:44 +02:00
ConsumerOptions options = new ConsumerOptions()
.addDecryptionKey(juliet)
.addVerificationCert(KeyRingUtils.publicKeyRingFrom(juliet));
DecryptionStream decryptor = PGPainless.decryptAndOrVerify()
2018-06-10 17:12:44 +02:00
.onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes()))
.withOptions(options);
2018-06-10 17:12:44 +02:00
ByteArrayOutputStream toPlain = new ByteArrayOutputStream();
2021-07-31 20:40:31 +02:00
Streams.pipeAll(decryptor, toPlain);
2018-06-10 17:12:44 +02:00
decryptor.close();
toPlain.close();
OpenPgpMetadata metadata = decryptor.getResult();
2018-06-10 17:12:44 +02:00
byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(UTF8);
2018-06-10 17:12:44 +02:00
byte[] actual = toPlain.toByteArray();
assertArrayEquals(expected, actual);
assertTrue(metadata.isEncrypted());
assertTrue(metadata.isSigned());
assertTrue(metadata.isVerified());
assertEquals(CompressionAlgorithm.ZLIB, metadata.getCompressionAlgorithm());
assertEquals(SymmetricKeyAlgorithm.AES_256, metadata.getSymmetricKeyAlgorithm());
2020-08-24 16:26:29 +02:00
assertEquals(1, metadata.getSignatures().size());
assertEquals(1, metadata.getVerifiedSignatures().size());
assertTrue(metadata.containsVerifiedSignatureFrom(TestKeys.JULIET_FINGERPRINT));
assertEquals(new SubkeyIdentifier(TestKeys.JULIET_FINGERPRINT), metadata.getDecryptionKey());
2018-06-10 17:12:44 +02:00
}
}