pgpainless/pgpainless-core/src/test/java/org/pgpainless/decryption_verification/DecryptAndVerifyMessageTest...

89 lines
3.6 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 java.util.Collections;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
2018-06-10 17:12:44 +02:00
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
import org.bouncycastle.util.io.Streams;
2020-11-13 16:31:59 +01:00
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
import org.pgpainless.key.TestKeys;
import org.pgpainless.key.protection.UnprotectedKeysProtector;
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
}
@Test
public void decryptMessageAndVerifySignatureTest() throws Exception {
String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET;
2018-06-10 17:12:44 +02:00
2018-06-11 01:33:49 +02:00
DecryptionStream decryptor = PGPainless.createDecryptor()
2018-06-10 17:12:44 +02:00
.onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes()))
2018-06-27 15:48:53 +02:00
.decryptWith(new UnprotectedKeysProtector(), new PGPSecretKeyRingCollection(Collections.singleton(juliet)))
.verifyWith(Collections.singleton(new PGPPublicKeyRing(Collections.singletonList(juliet.getPublicKey()))))
2018-06-10 17:12:44 +02:00
.ignoreMissingPublicKeys()
.build();
ByteArrayOutputStream toPlain = new ByteArrayOutputStream();
Streams.pipeAll(decryptor, toPlain);
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.isIntegrityProtected());
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.getVerifiedSignatureKeyFingerprints().size());
assertTrue(metadata.containsVerifiedSignatureFrom(TestKeys.JULIET_FINGERPRINT));
assertEquals(TestKeys.JULIET_FINGERPRINT, metadata.getDecryptionFingerprint());
2018-06-10 17:12:44 +02:00
}
}