mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-09 18:29:39 +02:00
Test edge-cases in inline-detach operation
This commit is contained in:
parent
bfd67abab7
commit
946d8aace0
1 changed files with 66 additions and 0 deletions
|
@ -5,6 +5,8 @@
|
||||||
package org.pgpainless.sop;
|
package org.pgpainless.sop;
|
||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
import java.io.ByteArrayInputStream;
|
import java.io.ByteArrayInputStream;
|
||||||
import java.io.ByteArrayOutputStream;
|
import java.io.ByteArrayOutputStream;
|
||||||
|
@ -23,10 +25,16 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
|
||||||
import org.bouncycastle.openpgp.PGPSignature;
|
import org.bouncycastle.openpgp.PGPSignature;
|
||||||
import org.bouncycastle.openpgp.PGPSignatureList;
|
import org.bouncycastle.openpgp.PGPSignatureList;
|
||||||
import org.bouncycastle.openpgp.api.OpenPGPImplementation;
|
import org.bouncycastle.openpgp.api.OpenPGPImplementation;
|
||||||
|
import org.bouncycastle.openpgp.api.OpenPGPKey;
|
||||||
import org.bouncycastle.util.io.Streams;
|
import org.bouncycastle.util.io.Streams;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.pgpainless.PGPainless;
|
import org.pgpainless.PGPainless;
|
||||||
|
import org.pgpainless.algorithm.CompressionAlgorithm;
|
||||||
|
import org.pgpainless.encryption_signing.EncryptionStream;
|
||||||
|
import org.pgpainless.encryption_signing.ProducerOptions;
|
||||||
|
import org.pgpainless.encryption_signing.SigningOptions;
|
||||||
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
import org.pgpainless.key.OpenPgpV4Fingerprint;
|
||||||
|
import org.pgpainless.key.protection.SecretKeyRingProtector;
|
||||||
import sop.ByteArrayAndResult;
|
import sop.ByteArrayAndResult;
|
||||||
import sop.SOP;
|
import sop.SOP;
|
||||||
import sop.Signatures;
|
import sop.Signatures;
|
||||||
|
@ -201,4 +209,62 @@ public class InlineDetachTest {
|
||||||
|
|
||||||
assertArrayEquals(data, message);
|
assertArrayEquals(data, message);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void detachSignaturesFromCompressedMessage() throws PGPException, IOException {
|
||||||
|
PGPainless api = PGPainless.getInstance();
|
||||||
|
OpenPGPKey key = api.generateKey()
|
||||||
|
.modernKeyRing("Alice <alice@pgpainless.org>");
|
||||||
|
byte[] cert = key.toCertificate().getEncoded();
|
||||||
|
|
||||||
|
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
||||||
|
EncryptionStream eOut = api.generateMessage()
|
||||||
|
.onOutputStream(bOut)
|
||||||
|
.withOptions(ProducerOptions.sign(
|
||||||
|
SigningOptions.get().addInlineSignature(SecretKeyRingProtector.unprotectedKeys(), key)
|
||||||
|
).overrideCompressionAlgorithm(CompressionAlgorithm.ZIP));
|
||||||
|
eOut.write("Hello, World!\n".getBytes(StandardCharsets.UTF_8));
|
||||||
|
eOut.close();
|
||||||
|
|
||||||
|
ByteArrayAndResult<Signatures> result = sop.inlineDetach()
|
||||||
|
.message(bOut.toByteArray())
|
||||||
|
.toByteArrayAndResult();
|
||||||
|
|
||||||
|
byte[] message = result.getBytes();
|
||||||
|
byte[] signatures = result.getResult().getBytes();
|
||||||
|
|
||||||
|
List<Verification> verifications = sop.detachedVerify()
|
||||||
|
.cert(cert)
|
||||||
|
.signatures(signatures)
|
||||||
|
.data(message);
|
||||||
|
|
||||||
|
assertFalse(verifications.isEmpty());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void detachMissingSignaturesFails() throws PGPException, IOException {
|
||||||
|
PGPainless api = PGPainless.getInstance();
|
||||||
|
|
||||||
|
ByteArrayOutputStream bOut = new ByteArrayOutputStream();
|
||||||
|
EncryptionStream eOut = api.generateMessage()
|
||||||
|
.onOutputStream(bOut)
|
||||||
|
.withOptions(ProducerOptions.noEncryptionNoSigning()
|
||||||
|
.overrideCompressionAlgorithm(CompressionAlgorithm.ZIP));
|
||||||
|
|
||||||
|
eOut.write("Hello, World!\n".getBytes(StandardCharsets.UTF_8));
|
||||||
|
eOut.close();
|
||||||
|
|
||||||
|
assertThrows(SOPGPException.BadData.class, () ->
|
||||||
|
sop.inlineDetach()
|
||||||
|
.message(bOut.toByteArray())
|
||||||
|
.toByteArrayAndResult());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void detachBadDataFails() {
|
||||||
|
byte[] bytes = "Hello, World\n".getBytes(StandardCharsets.UTF_8);
|
||||||
|
assertThrows(SOPGPException.BadData.class, () ->
|
||||||
|
sop.inlineDetach().message(bytes)
|
||||||
|
.toByteArrayAndResult());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue