1
0
Fork 0
mirror of https://github.com/pgpainless/pgpainless.git synced 2024-06-23 20:14:49 +02:00
pgpainless/pgpainless-sop/src/test/java/org/pgpainless/sop/DetachedSignTest.java

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

155 lines
5 KiB
Java
Raw Normal View History

2021-10-07 15:48:52 +02:00
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
2021-10-04 16:28:56 +02:00
package org.pgpainless.sop;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPSignature;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import org.pgpainless.algorithm.SignatureType;
import org.pgpainless.signature.SignatureUtils;
import sop.SOP;
import sop.Verification;
import sop.enums.SignAs;
2023-04-18 18:43:56 +02:00
import sop.enums.SignatureMode;
2021-10-04 16:28:56 +02:00
import sop.exception.SOPGPException;
2023-04-27 15:15:42 +02:00
import sop.testsuite.assertions.VerificationListAssert;
2021-10-04 16:28:56 +02:00
2022-06-16 13:09:42 +02:00
public class DetachedSignTest {
2021-10-04 16:28:56 +02:00
private static SOP sop;
private static byte[] key;
private static byte[] cert;
private static byte[] data;
@BeforeAll
public static void setup() throws IOException {
sop = new SOPImpl();
key = sop.generateKey()
.userId("Alice")
.generate()
.getBytes();
cert = sop.extractCert()
.key(key)
2021-10-04 16:28:56 +02:00
.getBytes();
data = "Hello, World\n".getBytes(StandardCharsets.UTF_8);
}
@Test
public void signArmored() throws IOException {
byte[] signature = sop.sign()
.key(key)
2023-11-15 13:40:22 +01:00
.mode(SignAs.binary)
.data(data)
.toByteArrayAndResult().getBytes();
2021-10-04 16:28:56 +02:00
assertTrue(new String(signature).startsWith("-----BEGIN PGP SIGNATURE-----"));
List<Verification> verifications = sop.verify()
.cert(cert)
.notAfter(new Date(System.currentTimeMillis() + 10000))
.notBefore(new Date(System.currentTimeMillis() - 10000))
.signatures(signature)
.data(data);
2021-10-04 16:28:56 +02:00
2023-04-27 15:15:42 +02:00
VerificationListAssert.assertThatVerificationList(verifications)
.hasSingleItem()
.hasMode(SignatureMode.binary);
2021-10-04 16:28:56 +02:00
}
@Test
public void signUnarmored() throws IOException {
byte[] signature = sop.sign()
.key(key)
2021-10-04 16:28:56 +02:00
.noArmor()
.data(data)
.toByteArrayAndResult().getBytes();
2021-10-04 16:28:56 +02:00
assertFalse(new String(signature).startsWith("-----BEGIN PGP SIGNATURE-----"));
List<Verification> verifications = sop.verify()
.cert(cert)
.notAfter(new Date(System.currentTimeMillis() + 10000))
.notBefore(new Date(System.currentTimeMillis() - 10000))
.signatures(signature)
.data(data);
2021-10-04 16:28:56 +02:00
2023-04-27 15:15:42 +02:00
VerificationListAssert.assertThatVerificationList(verifications)
.hasSingleItem();
2021-10-04 16:28:56 +02:00
}
2023-04-18 18:43:56 +02:00
@Test
public void textSig() throws IOException {
byte[] signature = sop.sign()
.key(key)
.noArmor()
2023-11-15 13:40:22 +01:00
.mode(SignAs.text)
2023-04-18 18:43:56 +02:00
.data(data)
.toByteArrayAndResult().getBytes();
List<Verification> verifications = sop.verify()
.cert(cert)
.notAfter(new Date(System.currentTimeMillis() + 10000))
.notBefore(new Date(System.currentTimeMillis() - 10000))
2023-04-18 18:43:56 +02:00
.signatures(signature)
.data(data);
2023-04-27 15:15:42 +02:00
VerificationListAssert.assertThatVerificationList(verifications)
.hasSingleItem()
.hasMode(SignatureMode.text);
2023-04-18 18:43:56 +02:00
}
2021-10-04 16:28:56 +02:00
@Test
public void rejectSignatureAsTooOld() throws IOException {
byte[] signature = sop.sign()
.key(key)
.data(data)
.toByteArrayAndResult().getBytes();
2021-10-04 16:28:56 +02:00
assertThrows(SOPGPException.NoSignature.class, () -> sop.verify()
.cert(cert)
.notAfter(new Date(System.currentTimeMillis() - 10000)) // Sig is older
.signatures(signature)
.data(data));
2021-10-04 16:28:56 +02:00
}
@Test
public void rejectSignatureAsTooYoung() throws IOException {
byte[] signature = sop.sign()
.key(key)
.data(data)
.toByteArrayAndResult().getBytes();
2021-10-04 16:28:56 +02:00
assertThrows(SOPGPException.NoSignature.class, () -> sop.verify()
.cert(cert)
.notBefore(new Date(System.currentTimeMillis() + 10000)) // Sig is younger
.signatures(signature)
.data(data));
2021-10-04 16:28:56 +02:00
}
@Test
public void mode() throws IOException, PGPException {
byte[] signature = sop.sign()
2023-11-15 13:40:22 +01:00
.mode(SignAs.text)
.key(key)
.data(data)
.toByteArrayAndResult().getBytes();
2021-10-04 16:28:56 +02:00
PGPSignature sig = SignatureUtils.readSignatures(signature).get(0);
2021-10-04 16:28:56 +02:00
assertEquals(SignatureType.CANONICAL_TEXT_DOCUMENT.getCode(), sig.getSignatureType());
}
}