mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-12 11:49:38 +02:00
Improve tests and add signatures to result
This commit is contained in:
parent
47300a0694
commit
530a22ba0e
8 changed files with 146 additions and 86 deletions
|
@ -142,10 +142,18 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest {
|
|||
.signWith(keyDecryptor, senderSec)
|
||||
.noArmor();
|
||||
|
||||
Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
|
||||
encryptor.close();
|
||||
byte[] encryptedSecretMessage = envelope.toByteArray();
|
||||
|
||||
LOGGER.log(Level.INFO, "Sender: " + PublicKeyAlgorithm.fromId(senderPub.getPublicKey().getAlgorithm()) +
|
||||
" Receiver: " + PublicKeyAlgorithm.fromId(recipientPub.getPublicKey().getAlgorithm()) +
|
||||
" Encrypted Length: " + encryptedSecretMessage.length);
|
||||
|
||||
OpenPgpMetadata encryptionResult = encryptor.getResult();
|
||||
|
||||
assertFalse(encryptionResult.getAllSignatureKeyFingerprints().isEmpty());
|
||||
for (long keyId : encryptionResult.getAllSignatureKeyFingerprints()) {
|
||||
assertFalse(encryptionResult.getSignatureKeyIDs().isEmpty());
|
||||
for (long keyId : encryptionResult.getSignatureKeyIDs()) {
|
||||
assertTrue(BCUtil.keyRingContainsKeyWithId(senderPub, keyId));
|
||||
}
|
||||
|
||||
|
@ -156,14 +164,6 @@ public class EncryptDecryptTest extends AbstractPGPainlessTest {
|
|||
|
||||
assertEquals(SymmetricKeyAlgorithm.AES_256, encryptionResult.getSymmetricKeyAlgorithm());
|
||||
|
||||
Streams.pipeAll(new ByteArrayInputStream(secretMessage), encryptor);
|
||||
encryptor.close();
|
||||
byte[] encryptedSecretMessage = envelope.toByteArray();
|
||||
|
||||
LOGGER.log(Level.INFO, "Sender: " + PublicKeyAlgorithm.fromId(senderPub.getPublicKey().getAlgorithm()) +
|
||||
" Receiver: " + PublicKeyAlgorithm.fromId(recipientPub.getPublicKey().getAlgorithm()) +
|
||||
" Encrypted Length: " + encryptedSecretMessage.length);
|
||||
|
||||
// Juliet trieth to comprehend Romeos words
|
||||
|
||||
ByteArrayInputStream envelopeIn = new ByteArrayInputStream(encryptedSecretMessage);
|
||||
|
|
|
@ -26,6 +26,7 @@ import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
|||
import org.bouncycastle.openpgp.PGPUtil;
|
||||
import org.bouncycastle.openpgp.operator.KeyFingerPrintCalculator;
|
||||
import org.bouncycastle.openpgp.operator.bc.BcKeyFingerprintCalculator;
|
||||
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
||||
|
||||
public class TestKeys extends AbstractPGPainlessTest {
|
||||
|
||||
|
@ -42,6 +43,8 @@ public class TestKeys extends AbstractPGPainlessTest {
|
|||
|
||||
public static final String JULIET_UID = "xmpp:juliet@capulet.lit";
|
||||
public static final long JULIET_KEY_ID = -5425419407118114754L;
|
||||
public static final String JULIET_FINGERPRINT_STRING = "1D018C772DF8C5EF86A1DCC9B4B509CB5936E03E";
|
||||
public static final OpenPgpV4Fingerprint JULIET_FINGERPRINT = new OpenPgpV4Fingerprint(JULIET_FINGERPRINT_STRING);
|
||||
|
||||
/**
|
||||
* Public key of xmpp:juliet@capulet.lit.
|
||||
|
@ -104,6 +107,8 @@ public class TestKeys extends AbstractPGPainlessTest {
|
|||
|
||||
public static final String ROMEO_UID = "xmpp:romeo@montague.lit";
|
||||
public static final long ROMEO_KEY_ID = 334147643349279223L;
|
||||
public static final String ROMEO_FINGERPRINT_STRING = "35D299D08A2F7D80230B095D04A32182E05E21F7";
|
||||
public static final OpenPgpV4Fingerprint ROMEO_FINGERPRINT = new OpenPgpV4Fingerprint(ROMEO_FINGERPRINT_STRING);
|
||||
|
||||
/**
|
||||
* Public key of xmpp:romeo@montague.lit.
|
||||
|
@ -233,35 +238,24 @@ public class TestKeys extends AbstractPGPainlessTest {
|
|||
/**
|
||||
* Test Message signed with {@link #JULIET_SEC} and encrypted for {@link #JULIET_PUB}.
|
||||
*/
|
||||
public static final String TEST_MESSAGE_01 = "-----BEGIN PGP MESSAGE-----\n" +
|
||||
public static final String MSG_SIGN_CRYPT_JULIET_JULIET =
|
||||
"-----BEGIN PGP MESSAGE-----\n" +
|
||||
"\n" +
|
||||
"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\n" +
|
||||
"=GAhR\n" +
|
||||
"hQEMA7S1CctZNuA+AQf/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"-----END PGP MESSAGE-----";
|
||||
}
|
||||
|
|
|
@ -15,23 +15,27 @@
|
|||
*/
|
||||
package org.pgpainless;
|
||||
|
||||
import static junit.framework.TestCase.assertEquals;
|
||||
import static junit.framework.TestCase.assertTrue;
|
||||
import static org.junit.Assert.assertArrayEquals;
|
||||
|
||||
import java.io.ByteArrayInputStream;
|
||||
import java.io.ByteArrayOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.Charset;
|
||||
import java.util.Arrays;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Collections;
|
||||
|
||||
import junit.framework.TestCase;
|
||||
import org.bouncycastle.openpgp.PGPException;
|
||||
import org.bouncycastle.openpgp.PGPPublicKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||
import org.bouncycastle.openpgp.PGPSecretKeyRingCollection;
|
||||
import org.bouncycastle.util.io.Streams;
|
||||
import org.junit.Test;
|
||||
import org.pgpainless.algorithm.CompressionAlgorithm;
|
||||
import org.pgpainless.algorithm.SymmetricKeyAlgorithm;
|
||||
import org.pgpainless.decryption_verification.DecryptionStream;
|
||||
import org.pgpainless.decryption_verification.OpenPgpMetadata;
|
||||
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
||||
import org.pgpainless.key.protection.UnprotectedKeysProtector;
|
||||
|
||||
public class TestKeysTest extends AbstractPGPainlessTest {
|
||||
|
@ -45,14 +49,26 @@ public class TestKeysTest extends AbstractPGPainlessTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void keyIdTest() {
|
||||
TestCase.assertEquals(TestKeys.JULIET_KEY_ID, juliet.getSecretKey().getKeyID());
|
||||
TestCase.assertEquals(TestKeys.ROMEO_KEY_ID, romeo.getSecretKey().getKeyID());
|
||||
public void julietKeyTest() {
|
||||
assertEquals(TestKeys.JULIET_KEY_ID, juliet.getSecretKey().getKeyID());
|
||||
assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet));
|
||||
assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet.getPublicKey()));
|
||||
assertEquals(TestKeys.JULIET_FINGERPRINT, new OpenPgpV4Fingerprint(juliet.getSecretKey()));
|
||||
assertEquals(TestKeys.JULIET_KEY_ID, TestKeys.JULIET_FINGERPRINT.getKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void romeoKeyTest() {
|
||||
assertEquals(TestKeys.ROMEO_KEY_ID, romeo.getSecretKey().getKeyID());
|
||||
assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo));
|
||||
assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo.getPublicKey()));
|
||||
assertEquals(TestKeys.ROMEO_FINGERPRINT, new OpenPgpV4Fingerprint(romeo.getSecretKey()));
|
||||
assertEquals(TestKeys.ROMEO_KEY_ID, TestKeys.ROMEO_FINGERPRINT.getKeyId());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void decryptVerifyTest() throws Exception {
|
||||
String encryptedMessage = TestKeys.TEST_MESSAGE_01;
|
||||
String encryptedMessage = TestKeys.MSG_SIGN_CRYPT_JULIET_JULIET;
|
||||
|
||||
DecryptionStream decryptor = PGPainless.createDecryptor()
|
||||
.onInputStream(new ByteArrayInputStream(encryptedMessage.getBytes()))
|
||||
|
@ -65,10 +81,23 @@ public class TestKeysTest extends AbstractPGPainlessTest {
|
|||
Streams.pipeAll(decryptor, toPlain);
|
||||
decryptor.close();
|
||||
toPlain.close();
|
||||
OpenPgpMetadata metadata = decryptor.getResult();
|
||||
|
||||
byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(Charset.forName("UTF-8"));
|
||||
byte[] expected = TestKeys.TEST_MESSAGE_01_PLAIN.getBytes(StandardCharsets.UTF_8);
|
||||
byte[] actual = toPlain.toByteArray();
|
||||
|
||||
assertTrue(Arrays.equals(expected, actual));
|
||||
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());
|
||||
assertEquals(1, metadata.getSignatureKeyIDs().size());
|
||||
assertEquals(1, metadata.getVerifiedSignatureKeyFingerprints().size());
|
||||
assertTrue(metadata.containsVerifiedSignatureFrom(TestKeys.JULIET_FINGERPRINT));
|
||||
assertEquals(TestKeys.JULIET_FINGERPRINT, metadata.getDecryptionFingerprint());
|
||||
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue