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

WIP: Replace nesting with independent instancing

This commit is contained in:
Paul Schaub 2022-09-13 19:23:59 +02:00
parent bf8949d7f4
commit e86062c427
13 changed files with 1227 additions and 411 deletions

View file

@ -0,0 +1,86 @@
package org.pgpainless.decryption_verification;
import org.bouncycastle.bcpg.ArmoredInputStream;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.exception.MalformedOpenPgpMessageException;
import org.pgpainless.util.ArmoredInputStreamFactory;
import org.pgpainless.util.Passphrase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.pgpainless.decryption_verification.PGPDecryptionStreamTest.COMP;
import static org.pgpainless.decryption_verification.PGPDecryptionStreamTest.COMP_COMP_LIT;
import static org.pgpainless.decryption_verification.PGPDecryptionStreamTest.COMP_LIT;
import static org.pgpainless.decryption_verification.PGPDecryptionStreamTest.LIT;
import static org.pgpainless.decryption_verification.PGPDecryptionStreamTest.LIT_LIT;
import static org.pgpainless.decryption_verification.PGPDecryptionStreamTest.PASSPHRASE;
import static org.pgpainless.decryption_verification.PGPDecryptionStreamTest.PLAINTEXT;
import static org.pgpainless.decryption_verification.PGPDecryptionStreamTest.SENC_LIT;
import static org.pgpainless.decryption_verification.PGPDecryptionStreamTest.SIG_LIT;
public class OpenPgpMessageInputStreamTest {
@Test
public void testProcessLIT() throws IOException, PGPException {
String plain = process(LIT, ConsumerOptions.get());
assertEquals(PLAINTEXT, plain);
}
@Test
public void testProcessLIT_LIT_fails() {
assertThrows(MalformedOpenPgpMessageException.class,
() -> process(LIT_LIT, ConsumerOptions.get()));
}
@Test
public void testProcessCOMP_LIT() throws PGPException, IOException {
String plain = process(COMP_LIT, ConsumerOptions.get());
assertEquals(PLAINTEXT, plain);
}
@Test
public void testProcessCOMP_fails() {
assertThrows(MalformedOpenPgpMessageException.class,
() -> process(COMP, ConsumerOptions.get()));
}
@Test
public void testProcessCOMP_COMP_LIT() throws PGPException, IOException {
String plain = process(COMP_COMP_LIT, ConsumerOptions.get());
assertEquals(PLAINTEXT, plain);
}
@Test
public void testProcessSIG_LIT() throws PGPException, IOException {
String plain = process(SIG_LIT, ConsumerOptions.get());
assertEquals(PLAINTEXT, plain);
}
@Test
public void testProcessSENC_LIT() throws PGPException, IOException {
String plain = process(SENC_LIT, ConsumerOptions.get().addDecryptionPassphrase(Passphrase.fromPassword(PASSPHRASE)));
assertEquals(PLAINTEXT, plain);
}
private String process(String armoredMessage, ConsumerOptions options) throws PGPException, IOException {
OpenPgpMessageInputStream in = get(armoredMessage, options);
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.pipeAll(in, out);
in.close();
return out.toString();
}
private OpenPgpMessageInputStream get(String armored, ConsumerOptions options) throws IOException, PGPException {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(armored.getBytes(StandardCharsets.UTF_8));
ArmoredInputStream armorIn = ArmoredInputStreamFactory.get(bytesIn);
OpenPgpMessageInputStream pgpIn = new OpenPgpMessageInputStream(armorIn, options);
return pgpIn;
}
}

View file

@ -12,6 +12,8 @@ import org.bouncycastle.openpgp.PGPSignature;
import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test;
import org.pgpainless.PGPainless;
import org.pgpainless.algorithm.CompressionAlgorithm;
import org.pgpainless.encryption_signing.EncryptionOptions;
import org.pgpainless.encryption_signing.EncryptionResult;
import org.pgpainless.encryption_signing.EncryptionStream;
import org.pgpainless.encryption_signing.ProducerOptions;
@ -19,6 +21,7 @@ import org.pgpainless.encryption_signing.SigningOptions;
import org.pgpainless.exception.MalformedOpenPgpMessageException;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import org.pgpainless.util.ArmoredInputStreamFactory;
import org.pgpainless.util.Passphrase;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
@ -33,7 +36,7 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
public class PGPDecryptionStreamTest {
private static final String KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
public static final String KEY = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n" +
"Version: PGPainless\n" +
"Comment: DA05 848F 37D4 68E6 F982 C889 7A70 1FC6 904D 3F4C\n" +
"Comment: Alice <alice@pgpainless.org>\n" +
@ -58,9 +61,10 @@ public class PGPDecryptionStreamTest {
"=THgv\n" +
"-----END PGP PRIVATE KEY BLOCK-----";
private static final String PLAINTEXT = "Hello, World!\n";
public static final String PLAINTEXT = "Hello, World!\n";
public static final String PASSPHRASE = "sw0rdf1sh";
private static final String LIT = "" +
public static final String LIT = "" +
"-----BEGIN PGP MESSAGE-----\n" +
"Version: PGPainless\n" +
"\n" +
@ -68,7 +72,7 @@ public class PGPDecryptionStreamTest {
"=WGju\n" +
"-----END PGP MESSAGE-----";
private static final String LIT_LIT = "" +
public static final String LIT_LIT = "" +
"-----BEGIN PGP MESSAGE-----\n" +
"Version: BCPG v1.71\n" +
"\n" +
@ -76,7 +80,7 @@ public class PGPDecryptionStreamTest {
"=A91Q\n" +
"-----END PGP MESSAGE-----";
private static final String COMP_LIT = "" +
public static final String COMP_LIT = "" +
"-----BEGIN PGP MESSAGE-----\n" +
"Version: BCPG v1.71\n" +
"\n" +
@ -84,7 +88,7 @@ public class PGPDecryptionStreamTest {
"=ZYDg\n" +
"-----END PGP MESSAGE-----";
private static final String COMP = "" +
public static final String COMP = "" +
"-----BEGIN PGP MESSAGE-----\n" +
"Version: BCPG v1.71\n" +
"\n" +
@ -92,7 +96,7 @@ public class PGPDecryptionStreamTest {
"=MDzg\n" +
"-----END PGP MESSAGE-----";
private static final String COMP_COMP_LIT = "" +
public static final String COMP_COMP_LIT = "" +
"-----BEGIN PGP MESSAGE-----\n" +
"Version: BCPG v1.71\n" +
"\n" +
@ -101,7 +105,7 @@ public class PGPDecryptionStreamTest {
"=K9Zl\n" +
"-----END PGP MESSAGE-----";
private static final String SIG_LIT = "" +
public static final String SIG_LIT = "" +
"-----BEGIN PGP MESSAGE-----\n" +
"Version: BCPG v1.71\n" +
"\n" +
@ -111,6 +115,15 @@ public class PGPDecryptionStreamTest {
"=WKPn\n" +
"-----END PGP MESSAGE-----";
public static final String SENC_LIT = "" +
"-----BEGIN PGP MESSAGE-----\n" +
"Version: PGPainless\n" +
"\n" +
"jA0ECQMCuZ0qHNXWnGhg0j8Bdm1cxV65sYb7jDgb4rRMtdNpQ1dC4UpSYuk9YWS2\n" +
"DpNEijbX8b/P1UOK2kJczNDADMRegZuLEI+dNsBnJjk=\n" +
"=i4Y0\n" +
"-----END PGP MESSAGE-----";
@Test
public void genLIT() throws IOException {
ArmoredOutputStream armorOut = new ArmoredOutputStream(System.out);
@ -125,7 +138,7 @@ public class PGPDecryptionStreamTest {
public void processLIT() throws IOException, PGPException {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(LIT.getBytes(StandardCharsets.UTF_8));
ArmoredInputStream armorIn = ArmoredInputStreamFactory.get(bytesIn);
PGPDecryptionStream decIn = new PGPDecryptionStream(armorIn);
MessageDecryptionStream decIn = new MessageDecryptionStream(armorIn, ConsumerOptions.get());
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.pipeAll(decIn, out);
@ -152,10 +165,10 @@ public class PGPDecryptionStreamTest {
public void processLIT_LIT() throws IOException, PGPException {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(LIT_LIT.getBytes(StandardCharsets.UTF_8));
ArmoredInputStream armorIn = ArmoredInputStreamFactory.get(bytesIn);
PGPDecryptionStream decIn = new PGPDecryptionStream(armorIn);
MessageDecryptionStream decIn = new MessageDecryptionStream(armorIn, ConsumerOptions.get());
ByteArrayOutputStream out = new ByteArrayOutputStream();
assertThrows(MalformedOpenPgpMessageException.RTE.class, () -> Streams.pipeAll(decIn, out));
assertThrows(MalformedOpenPgpMessageException.class, () -> Streams.pipeAll(decIn, out));
}
@Test
@ -175,7 +188,7 @@ public class PGPDecryptionStreamTest {
public void processCOMP_LIT() throws IOException, PGPException {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(COMP_LIT.getBytes(StandardCharsets.UTF_8));
ArmoredInputStream armorIn = ArmoredInputStreamFactory.get(bytesIn);
PGPDecryptionStream decIn = new PGPDecryptionStream(armorIn);
MessageDecryptionStream decIn = new MessageDecryptionStream(armorIn, ConsumerOptions.get());
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.pipeAll(decIn, out);
@ -198,8 +211,8 @@ public class PGPDecryptionStreamTest {
public void processCOMP() throws IOException {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(COMP.getBytes(StandardCharsets.UTF_8));
ArmoredInputStream armorIn = ArmoredInputStreamFactory.get(bytesIn);
assertThrows(MalformedOpenPgpMessageException.RTE.class, () -> {
PGPDecryptionStream decIn = new PGPDecryptionStream(armorIn);
assertThrows(MalformedOpenPgpMessageException.class, () -> {
MessageDecryptionStream decIn = new MessageDecryptionStream(armorIn, ConsumerOptions.get());
Streams.drain(decIn);
});
}
@ -228,7 +241,7 @@ public class PGPDecryptionStreamTest {
public void processCOMP_COMP_LIT() throws PGPException, IOException {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(COMP_COMP_LIT.getBytes(StandardCharsets.UTF_8));
ArmoredInputStream armorIn = ArmoredInputStreamFactory.get(bytesIn);
PGPDecryptionStream decIn = new PGPDecryptionStream(armorIn);
MessageDecryptionStream decIn = new MessageDecryptionStream(armorIn, ConsumerOptions.get());
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.pipeAll(decIn, out);
@ -279,7 +292,35 @@ public class PGPDecryptionStreamTest {
public void processSIG_LIT() throws IOException, PGPException {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(SIG_LIT.getBytes(StandardCharsets.UTF_8));
ArmoredInputStream armorIn = ArmoredInputStreamFactory.get(bytesIn);
PGPDecryptionStream decIn = new PGPDecryptionStream(armorIn);
MessageDecryptionStream decIn = new MessageDecryptionStream(armorIn, ConsumerOptions.get());
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.pipeAll(decIn, out);
decIn.close();
System.out.println(out);
}
@Test
public void genSENC_LIT() throws PGPException, IOException {
ByteArrayOutputStream out = new ByteArrayOutputStream();
EncryptionStream enc = PGPainless.encryptAndOrSign()
.onOutputStream(out)
.withOptions(ProducerOptions.encrypt(EncryptionOptions.get()
.addPassphrase(Passphrase.fromPassword(PASSPHRASE)))
.overrideCompressionAlgorithm(CompressionAlgorithm.UNCOMPRESSED));
enc.write(PLAINTEXT.getBytes(StandardCharsets.UTF_8));
enc.close();
System.out.println(out);
}
@Test
public void processSENC_LIT() throws IOException, PGPException {
ByteArrayInputStream bytesIn = new ByteArrayInputStream(SENC_LIT.getBytes(StandardCharsets.UTF_8));
ArmoredInputStream armorIn = ArmoredInputStreamFactory.get(bytesIn);
MessageDecryptionStream decIn = new MessageDecryptionStream(armorIn, ConsumerOptions.get()
.addDecryptionPassphrase(Passphrase.fromPassword(PASSPHRASE)));
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.pipeAll(decIn, out);

View file

@ -1,205 +0,0 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.decryption_verification;
import org.junit.jupiter.api.Test;
import org.pgpainless.exception.MalformedOpenPgpMessageException;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class PushDownAutomatonTest {
/**
* MSG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testSimpleLiteralMessageIsValid() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* OPS MSG SIG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testSimpleOpsSignedMesssageIsValid() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.OnePassSignatures);
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
automaton.next(PushdownAutomaton.InputAlphabet.Signatures);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* SIG MSG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testSimplePrependSignedMessageIsValid() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.Signatures);
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* OPS COMP(MSG) SIG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testOPSSignedCompressedMessageIsValid() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.OnePassSignatures);
automaton.next(PushdownAutomaton.InputAlphabet.CompressedData);
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
automaton.next(PushdownAutomaton.InputAlphabet.Signatures);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* OPS ENC(COMP(COMP(MSG))) SIG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testOpsSignedEncryptedCompressedCompressedMessageIsValid() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.OnePassSignatures);
automaton.next(PushdownAutomaton.InputAlphabet.EncryptedData);
automaton.next(PushdownAutomaton.InputAlphabet.CompressedData);
automaton.next(PushdownAutomaton.InputAlphabet.CompressedData);
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
automaton.next(PushdownAutomaton.InputAlphabet.Signatures);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* MSG SIG is invalid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testLiteralPlusSigsFails() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(PushdownAutomaton.InputAlphabet.Signatures));
}
/**
* MSG MSG is invalid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testTwoLiteralDataPacketsFails() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(PushdownAutomaton.InputAlphabet.LiteralData));
}
/**
* OPS COMP(MSG MSG) SIG is invalid (two literal packets are illegal).
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testOPSSignedMessageWithTwoLiteralDataPacketsFails() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.OnePassSignatures);
automaton.next(PushdownAutomaton.InputAlphabet.CompressedData);
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(PushdownAutomaton.InputAlphabet.LiteralData));
}
/**
* OPS COMP(MSG) MSG SIG is invalid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testOPSSignedMessageWithTwoLiteralDataPacketsFails2() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.OnePassSignatures);
automaton.next(PushdownAutomaton.InputAlphabet.CompressedData);
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(PushdownAutomaton.InputAlphabet.LiteralData));
}
/**
* OPS COMP(MSG SIG) is invalid (MSG SIG does not form valid nested message).
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testCorrespondingSignaturesOfOpsSignedMessageAreLayerFurtherDownFails() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.OnePassSignatures);
automaton.next(PushdownAutomaton.InputAlphabet.CompressedData);
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(PushdownAutomaton.InputAlphabet.Signatures));
}
/**
* Empty COMP is invalid.
*/
@Test
public void testEmptyCompressedDataIsInvalid() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.CompressedData);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence));
}
@Test
public void testOPSSignedEncryptedCompressedOPSSignedMessageIsValid() throws MalformedOpenPgpMessageException {
PushdownAutomaton automaton = new PushdownAutomaton();
automaton.next(PushdownAutomaton.InputAlphabet.OnePassSignatures);
automaton.next(PushdownAutomaton.InputAlphabet.EncryptedData);
automaton.next(PushdownAutomaton.InputAlphabet.OnePassSignatures);
automaton.next(PushdownAutomaton.InputAlphabet.CompressedData);
automaton.next(PushdownAutomaton.InputAlphabet.LiteralData);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
automaton.next(PushdownAutomaton.InputAlphabet.Signatures);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
automaton.next(PushdownAutomaton.InputAlphabet.Signatures);
automaton.next(PushdownAutomaton.InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
}

View file

@ -0,0 +1,205 @@
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package org.pgpainless.decryption_verification.automaton;
import org.junit.jupiter.api.Test;
import org.pgpainless.exception.MalformedOpenPgpMessageException;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class NestingPDATest {
/**
* MSG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testSimpleLiteralMessageIsValid() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.LiteralData);
automaton.next(InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* OPS MSG SIG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testSimpleOpsSignedMesssageIsValid() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.OnePassSignatures);
automaton.next(InputAlphabet.LiteralData);
automaton.next(InputAlphabet.Signatures);
automaton.next(InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* SIG MSG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testSimplePrependSignedMessageIsValid() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.Signatures);
automaton.next(InputAlphabet.LiteralData);
automaton.next(InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* OPS COMP(MSG) SIG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testOPSSignedCompressedMessageIsValid() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.OnePassSignatures);
automaton.next(InputAlphabet.CompressedData);
automaton.next(InputAlphabet.LiteralData);
automaton.next(InputAlphabet.EndOfSequence);
automaton.next(InputAlphabet.Signatures);
automaton.next(InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* OPS ENC(COMP(COMP(MSG))) SIG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testOpsSignedEncryptedCompressedCompressedMessageIsValid() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.OnePassSignatures);
automaton.next(InputAlphabet.EncryptedData);
automaton.next(InputAlphabet.CompressedData);
automaton.next(InputAlphabet.CompressedData);
automaton.next(InputAlphabet.LiteralData);
automaton.next(InputAlphabet.EndOfSequence);
automaton.next(InputAlphabet.EndOfSequence);
automaton.next(InputAlphabet.EndOfSequence);
automaton.next(InputAlphabet.Signatures);
automaton.next(InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* MSG SIG is invalid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testLiteralPlusSigsFails() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.LiteralData);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(InputAlphabet.Signatures));
}
/**
* MSG MSG is invalid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testTwoLiteralDataPacketsFails() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.LiteralData);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(InputAlphabet.LiteralData));
}
/**
* OPS COMP(MSG MSG) SIG is invalid (two literal packets are illegal).
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testOPSSignedMessageWithTwoLiteralDataPacketsFails() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.OnePassSignatures);
automaton.next(InputAlphabet.CompressedData);
automaton.next(InputAlphabet.LiteralData);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(InputAlphabet.LiteralData));
}
/**
* OPS COMP(MSG) MSG SIG is invalid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testOPSSignedMessageWithTwoLiteralDataPacketsFails2() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.OnePassSignatures);
automaton.next(InputAlphabet.CompressedData);
automaton.next(InputAlphabet.LiteralData);
automaton.next(InputAlphabet.EndOfSequence);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(InputAlphabet.LiteralData));
}
/**
* OPS COMP(MSG SIG) is invalid (MSG SIG does not form valid nested message).
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testCorrespondingSignaturesOfOpsSignedMessageAreLayerFurtherDownFails() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.OnePassSignatures);
automaton.next(InputAlphabet.CompressedData);
automaton.next(InputAlphabet.LiteralData);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(InputAlphabet.Signatures));
}
/**
* Empty COMP is invalid.
*/
@Test
public void testEmptyCompressedDataIsInvalid() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.CompressedData);
assertThrows(MalformedOpenPgpMessageException.class,
() -> automaton.next(InputAlphabet.EndOfSequence));
}
@Test
public void testOPSSignedEncryptedCompressedOPSSignedMessageIsValid() throws MalformedOpenPgpMessageException {
NestingPDA automaton = new NestingPDA();
automaton.next(InputAlphabet.OnePassSignatures);
automaton.next(InputAlphabet.EncryptedData);
automaton.next(InputAlphabet.OnePassSignatures);
automaton.next(InputAlphabet.CompressedData);
automaton.next(InputAlphabet.LiteralData);
automaton.next(InputAlphabet.EndOfSequence);
automaton.next(InputAlphabet.Signatures);
automaton.next(InputAlphabet.EndOfSequence);
automaton.next(InputAlphabet.Signatures);
automaton.next(InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
}

View file

@ -0,0 +1,75 @@
package org.pgpainless.decryption_verification.automaton;
import org.junit.jupiter.api.Test;
import org.pgpainless.exception.MalformedOpenPgpMessageException;
import static org.junit.jupiter.api.Assertions.assertTrue;
public class PDATest {
/**
* MSG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testSimpleLiteralMessageIsValid() throws MalformedOpenPgpMessageException {
PDA automaton = new PDA();
automaton.next(InputAlphabet.LiteralData);
automaton.next(InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* OPS MSG SIG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testSimpleOpsSignedMesssageIsValid() throws MalformedOpenPgpMessageException {
PDA automaton = new PDA();
automaton.next(InputAlphabet.OnePassSignatures);
automaton.next(InputAlphabet.LiteralData);
automaton.next(InputAlphabet.Signatures);
automaton.next(InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* SIG MSG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testSimplePrependSignedMessageIsValid() throws MalformedOpenPgpMessageException {
PDA automaton = new PDA();
automaton.next(InputAlphabet.Signatures);
automaton.next(InputAlphabet.LiteralData);
automaton.next(InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
/**
* OPS COMP(MSG) SIG is valid.
*
* @throws MalformedOpenPgpMessageException fail
*/
@Test
public void testOPSSignedCompressedMessageIsValid() throws MalformedOpenPgpMessageException {
PDA automaton = new PDA();
automaton.next(InputAlphabet.OnePassSignatures);
automaton.next(InputAlphabet.CompressedData);
// Here would be a nested PDA for the LiteralData packet
automaton.next(InputAlphabet.Signatures);
automaton.next(InputAlphabet.EndOfSequence);
assertTrue(automaton.isValid());
}
}