Restructure external-sop tests into flexible test suite

This commit is contained in:
Paul Schaub 2023-01-22 16:47:44 +01:00
parent 0c8f6baf98
commit 0b96a5314f
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
15 changed files with 398 additions and 539 deletions

View file

@ -14,7 +14,9 @@ repositories {
dependencies { dependencies {
testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion" testImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
testImplementation "com.google.code.gson:gson:2.10.1"
api project(":sop-java") api project(":sop-java")
@ -30,4 +32,6 @@ dependencies {
test { test {
useJUnitPlatform() useJUnitPlatform()
// since we test external backends, we ignore test failures in this module
ignoreFailures = true
} }

View file

@ -2,5 +2,4 @@
# #
# SPDX-License-Identifier: CC0-1.0 # SPDX-License-Identifier: CC0-1.0
backend.local.properties config.json
backend.env

View file

@ -1,7 +0,0 @@
# SPDX-FileCopyrightText: 2023 Paul Schaub <info@pgpainless.org>
#
# SPDX-License-Identifier: CC0-1.0
## Do not change this file. To overwrite the SOP backend used during testing,
## simply create a file 'backend.local.properties' in this directory and override sop.backend in there.
sop.backend=/path/to/backend

View file

@ -0,0 +1,17 @@
{
"backends": [
{
"name": "Example-SOP",
"sop": "/usr/bin/example-sop"
},
{
"name": "Awesome-SOP",
"sop": "/usr/local/bin/awesome-sop",
"env": [
{
"key": "myEnvironmentVariable", "value": "FooBar"
}
]
}
]
}

View file

@ -4,174 +4,78 @@
package sop.external; package sop.external;
import org.apache.maven.artifact.versioning.ComparableVersion; import com.google.gson.Gson;
import org.slf4j.Logger; import org.junit.jupiter.api.Named;
import org.slf4j.LoggerFactory; import org.junit.jupiter.params.provider.Arguments;
import sop.SOP; import sop.SOP;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assumptions.assumeFalse; import static org.junit.jupiter.api.Assumptions.assumeFalse;
import static org.junit.jupiter.api.Assumptions.assumeTrue; import static org.junit.jupiter.api.Assumptions.assumeTrue;
public abstract class AbstractExternalSOPTest { public abstract class AbstractExternalSOPTest {
private static final Logger LOGGER = LoggerFactory.getLogger(AbstractExternalSOPTest.class); private static final List<Arguments> backends = new ArrayList<>();
private final SOP sop; static {
TestSuite suite = readConfiguration();
assumeTrue(suite != null);
assumeFalse(suite.backends.isEmpty());
public AbstractExternalSOPTest() { for (TestSubject subject : suite.backends) {
String backend = readSopBackendFromProperties(); if (!new File(subject.sop).exists()) {
assumeTrue(backend != null); continue;
Properties environment = readBackendEnvironment();
sop = new ExternalSOP(backend, environment);
}
/**
* Return the SOP backend.
*
* @return SOP backend
*/
public SOP getSop() {
return sop;
}
/**
* Return <pre>true</pre> iff the specified SOP backend binary is available and accessible.
*
* @return true if external SOP backend is usable
*/
public static boolean isExternalSopInstalled() {
String binary = readSopBackendFromProperties();
if (binary == null) {
return false;
}
return new File(binary).exists();
}
/**
* Relational enum.
*/
public enum Is {
/**
* Less than.
*/
le("<"),
/**
* Less or equal than.
*/
leq("<="),
/**
* Equal.
*/
eq("=="),
/**
* Not equal.
*/
neq("!="),
/**
* Greater or equal than.
*/
geq(">="),
/**
* Greater than.
*/
ge(">"),
;
private final String display;
Is(String display) {
this.display = display;
}
public String toDisplay() {
return display;
}
}
/**
* Ignore a test if the tested binary version matches a version criterion.
* Example:
* If the installed version of example-sop is 0.1.3, <pre>ignoreIf("example-sop", Is.le, "0.1.4")</pre> will
* make the test be ignored.
* <pre>ignoreIf("example-sop", Is.eq, "0.1.3")</pre> will skip the test as well.
* <pre>ignoreIf("another-sop", Is.gt, "0.0.0")</pre> will not skip the test, since the binary name does not match.
*
* @param name name of the binary
* @param is relation of the version
* @param version the reference version
*/
public void ignoreIf(String name, Is is, String version) {
String actualName = getSop().version().getName();
String actualVersion = getSop().version().getVersion();
if (!name.matches(actualName)) {
// Name mismatch, do not ignore
return;
}
ComparableVersion reference = new ComparableVersion(version);
ComparableVersion actual = new ComparableVersion(actualVersion);
int res = actual.compareTo(reference);
String msg = "Skip since installed " + name + " " + actual + " " + is.toDisplay() + " " + reference;
switch (is) {
case le:
assumeFalse(res < 0, msg);
break;
case leq:
assumeFalse(res <= 0, msg);
case eq:
assumeFalse(res == 0, msg);
break;
case neq:
assumeFalse(res != 0, msg);
break;
case geq:
assumeFalse(res >= 0, msg);
break;
case ge:
assumeFalse(res > 0, msg);
break;
}
}
static String readSopBackendFromProperties() {
Properties properties = new Properties();
try {
InputStream resourceIn = AbstractExternalSOPTest.class.getResourceAsStream("backend.local.properties");
if (resourceIn == null) {
LOGGER.info("Could not find backend.local.properties file. Try backend.properties instead.");
resourceIn = AbstractExternalSOPTest.class.getResourceAsStream("backend.properties");
}
if (resourceIn == null) {
throw new FileNotFoundException("Could not find backend.properties file.");
} }
properties.load(resourceIn); Properties env = new Properties();
return properties.getProperty("sop.backend"); if (subject.env != null) {
} catch (IOException e) { for (Var var : subject.env) {
env.put(var.key, var.value);
}
}
SOP sop = new ExternalSOP(subject.sop, env);
backends.add(Arguments.of(Named.of(subject.name, sop)));
}
}
public static Stream<Arguments> provideBackends() {
return backends.stream();
}
public static TestSuite readConfiguration() {
Gson gson = new Gson();
InputStream inputStream = AbstractExternalSOPTest.class.getResourceAsStream("config.json");
if (inputStream == null) {
return null; return null;
} }
InputStreamReader reader = new InputStreamReader(inputStream);
TestSuite suite = gson.fromJson(reader, TestSuite.class);
return suite;
} }
protected static Properties readBackendEnvironment() { // JSON DTOs
Properties properties = new Properties();
try { public static class TestSuite {
InputStream resourceIn = AbstractExternalSOPTest.class.getResourceAsStream("backend.env"); List<TestSubject> backends;
if (resourceIn == null) { }
LOGGER.info("Could not read backend.env file.");
} else { public static class TestSubject {
properties.load(resourceIn); String name;
} String sop;
} catch (IOException e) { List<Var> env;
throw new RuntimeException(e); }
}
return properties; public static class Var {
String key;
String value;
} }
} }

View file

@ -4,8 +4,9 @@
package sop.external; package sop.external;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
@ -16,7 +17,6 @@ import static sop.external.JUtils.arrayStartsWith;
import static sop.external.JUtils.assertArrayStartsWith; import static sop.external.JUtils.assertArrayStartsWith;
import static sop.external.JUtils.assertAsciiArmorEquals; import static sop.external.JUtils.assertAsciiArmorEquals;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest { public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
private static final String BEGIN_PGP_PRIVATE_KEY_BLOCK = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n"; private static final String BEGIN_PGP_PRIVATE_KEY_BLOCK = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n";
@ -28,17 +28,18 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
private static final String BEGIN_PGP_SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n"; private static final String BEGIN_PGP_SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n";
private static final byte[] BEGIN_PGP_SIGNATURE_BYTES = BEGIN_PGP_SIGNATURE.getBytes(StandardCharsets.UTF_8); private static final byte[] BEGIN_PGP_SIGNATURE_BYTES = BEGIN_PGP_SIGNATURE.getBytes(StandardCharsets.UTF_8);
@Test @ParameterizedTest
public void dearmorArmorAliceKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void dearmorArmorAliceKey(SOP sop) throws IOException {
byte[] aliceKey = TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8); byte[] aliceKey = TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = sop.dearmor()
.data(aliceKey) .data(aliceKey)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES)); assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES));
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
@ -46,17 +47,18 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
assertAsciiArmorEquals(aliceKey, armored); assertAsciiArmorEquals(aliceKey, armored);
} }
@Test @ParameterizedTest
public void dearmorArmorAliceCert() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void dearmorArmorAliceCert(SOP sop) throws IOException {
byte[] aliceCert = TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8); byte[] aliceCert = TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = sop.dearmor()
.data(aliceCert) .data(aliceCert)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES)); assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES));
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
@ -64,17 +66,18 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
assertAsciiArmorEquals(aliceCert, armored); assertAsciiArmorEquals(aliceCert, armored);
} }
@Test @ParameterizedTest
public void dearmorArmorBobKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void dearmorArmorBobKey(SOP sop) throws IOException {
byte[] bobKey = TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8); byte[] bobKey = TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = sop.dearmor()
.data(bobKey) .data(bobKey)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES)); assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES));
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
@ -82,17 +85,18 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
assertAsciiArmorEquals(bobKey, armored); assertAsciiArmorEquals(bobKey, armored);
} }
@Test @ParameterizedTest
public void dearmorArmorBobCert() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void dearmorArmorBobCert(SOP sop) throws IOException {
byte[] bobCert = TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8); byte[] bobCert = TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = sop.dearmor()
.data(bobCert) .data(bobCert)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES)); assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES));
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
@ -100,17 +104,18 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
assertAsciiArmorEquals(bobCert, armored); assertAsciiArmorEquals(bobCert, armored);
} }
@Test @ParameterizedTest
public void dearmorArmorCarolKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void dearmorArmorCarolKey(SOP sop) throws IOException {
byte[] carolKey = TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8); byte[] carolKey = TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = sop.dearmor()
.data(carolKey) .data(carolKey)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES)); assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES));
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
@ -118,17 +123,18 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
assertAsciiArmorEquals(carolKey, armored); assertAsciiArmorEquals(carolKey, armored);
} }
@Test @ParameterizedTest
public void dearmorArmorCarolCert() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void dearmorArmorCarolCert(SOP sop) throws IOException {
byte[] carolCert = TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8); byte[] carolCert = TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = sop.dearmor()
.data(carolCert) .data(carolCert)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES)); assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES));
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
@ -136,9 +142,9 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
assertAsciiArmorEquals(carolCert, armored); assertAsciiArmorEquals(carolCert, armored);
} }
@Test @ParameterizedTest
public void dearmorArmorMessage() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // falsely reports Invalid Data Type public void dearmorArmorMessage(SOP sop) throws IOException {
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" + byte[] message = ("-----BEGIN PGP MESSAGE-----\n" +
"\n" + "\n" +
"wV4DR2b2udXyHrYSAQdAMZy9Iqb1IxszjI3v+TsfK//0lnJ9PKHDqVAB5ohp+RMw\n" + "wV4DR2b2udXyHrYSAQdAMZy9Iqb1IxszjI3v+TsfK//0lnJ9PKHDqVAB5ohp+RMw\n" +
@ -147,13 +153,13 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
"CePQFpprprnGEzpE3flQLUc=\n" + "CePQFpprprnGEzpE3flQLUc=\n" +
"=ZiFR\n" + "=ZiFR\n" +
"-----END PGP MESSAGE-----\n").getBytes(StandardCharsets.UTF_8); "-----END PGP MESSAGE-----\n").getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = sop.dearmor()
.data(message) .data(message)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_MESSAGE_BYTES)); assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_MESSAGE_BYTES));
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
@ -161,8 +167,9 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
assertAsciiArmorEquals(message, armored); assertAsciiArmorEquals(message, armored);
} }
@Test @ParameterizedTest
public void dearmorArmorSignature() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void dearmorArmorSignature(SOP sop) throws IOException {
byte[] signature = ("-----BEGIN PGP SIGNATURE-----\n" + byte[] signature = ("-----BEGIN PGP SIGNATURE-----\n" +
"\n" + "\n" +
"wr0EABYKAG8FgmPBdRAJEPIxVQxPR+OORxQAAAAAAB4AIHNhbHRAbm90YXRpb25z\n" + "wr0EABYKAG8FgmPBdRAJEPIxVQxPR+OORxQAAAAAAB4AIHNhbHRAbm90YXRpb25z\n" +
@ -172,13 +179,13 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
"=GHvQ\n" + "=GHvQ\n" +
"-----END PGP SIGNATURE-----\n").getBytes(StandardCharsets.UTF_8); "-----END PGP SIGNATURE-----\n").getBytes(StandardCharsets.UTF_8);
byte[] dearmored = getSop().dearmor() byte[] dearmored = sop.dearmor()
.data(signature) .data(signature)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_SIGNATURE_BYTES)); assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_SIGNATURE_BYTES));
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
@ -186,23 +193,23 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
assertAsciiArmorEquals(signature, armored); assertAsciiArmorEquals(signature, armored);
} }
@Test @ParameterizedTest
public void testDearmoringTwiceIsIdempotent() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.eq, "0.27.2"); // IO error because: EOF public void testDearmoringTwiceIsIdempotent(SOP sop) throws IOException {
byte[] dearmored = sop.dearmor()
byte[] dearmored = getSop().dearmor()
.data(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .data(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.getBytes(); .getBytes();
byte[] dearmoredAgain = getSop().dearmor() byte[] dearmoredAgain = sop.dearmor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
assertArrayEquals(dearmored, dearmoredAgain); assertArrayEquals(dearmored, dearmoredAgain);
} }
@Test @ParameterizedTest
public void testArmoringTwiceIsIdempotent() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void testArmoringTwiceIsIdempotent(SOP sop) throws IOException {
byte[] armored = ("-----BEGIN PGP SIGNATURE-----\n" + byte[] armored = ("-----BEGIN PGP SIGNATURE-----\n" +
"\n" + "\n" +
"wr0EABYKAG8FgmPBdRAJEPIxVQxPR+OORxQAAAAAAB4AIHNhbHRAbm90YXRpb25z\n" + "wr0EABYKAG8FgmPBdRAJEPIxVQxPR+OORxQAAAAAAB4AIHNhbHRAbm90YXRpb25z\n" +
@ -212,7 +219,7 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
"=GHvQ\n" + "=GHvQ\n" +
"-----END PGP SIGNATURE-----\n").getBytes(StandardCharsets.UTF_8); "-----END PGP SIGNATURE-----\n").getBytes(StandardCharsets.UTF_8);
byte[] armoredAgain = getSop().armor() byte[] armoredAgain = sop.armor()
.data(armored) .data(armored)
.getBytes(); .getBytes();

View file

@ -4,10 +4,11 @@
package sop.external; package sop.external;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.params.provider.MethodSource;
import sop.ByteArrayAndResult; import sop.ByteArrayAndResult;
import sop.DecryptionResult; import sop.DecryptionResult;
import sop.SOP;
import sop.SessionKey; import sop.SessionKey;
import java.io.IOException; import java.io.IOException;
@ -16,7 +17,6 @@ import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalDecryptWithSessionKeyTest extends AbstractExternalSOPTest { public class ExternalDecryptWithSessionKeyTest extends AbstractExternalSOPTest {
private static final String CIPHERTEXT = "-----BEGIN PGP MESSAGE-----\n" + private static final String CIPHERTEXT = "-----BEGIN PGP MESSAGE-----\n" +
@ -29,9 +29,10 @@ public class ExternalDecryptWithSessionKeyTest extends AbstractExternalSOPTest {
"-----END PGP MESSAGE-----\n"; "-----END PGP MESSAGE-----\n";
private static final String SESSION_KEY = "9:ED682800F5FEA829A82E8B7DDF8CE9CF4BF9BB45024B017764462EE53101C36A"; private static final String SESSION_KEY = "9:ED682800F5FEA829A82E8B7DDF8CE9CF4BF9BB45024B017764462EE53101C36A";
@Test @ParameterizedTest
public void testDecryptAndExtractSessionKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() public void testDecryptAndExtractSessionKey(SOP sop) throws IOException {
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(CIPHERTEXT.getBytes(StandardCharsets.UTF_8)) .ciphertext(CIPHERTEXT.getBytes(StandardCharsets.UTF_8))
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -41,9 +42,10 @@ public class ExternalDecryptWithSessionKeyTest extends AbstractExternalSOPTest {
assertArrayEquals("Hello, World!\n".getBytes(StandardCharsets.UTF_8), bytesAndResult.getBytes()); assertArrayEquals("Hello, World!\n".getBytes(StandardCharsets.UTF_8), bytesAndResult.getBytes());
} }
@Test @ParameterizedTest
public void testDecryptWithSessionKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
byte[] decrypted = getSop().decrypt() public void testDecryptWithSessionKey(SOP sop) throws IOException {
byte[] decrypted = sop.decrypt()
.withSessionKey(SessionKey.fromString(SESSION_KEY)) .withSessionKey(SessionKey.fromString(SESSION_KEY))
.ciphertext(CIPHERTEXT.getBytes(StandardCharsets.UTF_8)) .ciphertext(CIPHERTEXT.getBytes(StandardCharsets.UTF_8))
.toByteArrayAndResult() .toByteArrayAndResult()

View file

@ -4,8 +4,9 @@
package sop.external; package sop.external;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP;
import sop.Verification; import sop.Verification;
import sop.enums.SignAs; import sop.enums.SignAs;
import sop.exception.SOPGPException; import sop.exception.SOPGPException;
@ -20,23 +21,23 @@ import static org.junit.jupiter.api.Assertions.assertThrows;
import static sop.external.JUtils.assertArrayStartsWith; import static sop.external.JUtils.assertArrayStartsWith;
import static sop.external.JUtils.assertSignedBy; import static sop.external.JUtils.assertSignedBy;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOPTest { public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOPTest {
private static final String BEGIN_PGP_SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n"; private static final String BEGIN_PGP_SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n";
private static final byte[] BEGIN_PGP_SIGNATURE_BYTES = BEGIN_PGP_SIGNATURE.getBytes(StandardCharsets.UTF_8); private static final byte[] BEGIN_PGP_SIGNATURE_BYTES = BEGIN_PGP_SIGNATURE.getBytes(StandardCharsets.UTF_8);
@Test @ParameterizedTest
public void signVerifyWithAliceKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyWithAliceKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign() byte[] signature = sop.detachedSign()
.key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message); .data(message);
@ -45,18 +46,19 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT); assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@Test @ParameterizedTest
public void signVerifyTextModeWithAliceKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyTextModeWithAliceKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign() byte[] signature = sop.detachedSign()
.key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.mode(SignAs.Text) .mode(SignAs.Text)
.data(message) .data(message)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message); .data(message);
@ -65,12 +67,13 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT); assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@Test @ParameterizedTest
public void verifyKnownMessageWithAliceCert() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void verifyKnownMessageWithAliceCert(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8); byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message); .data(message);
@ -79,17 +82,18 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT, TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE); assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT, TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE);
} }
@Test @ParameterizedTest
public void signVerifyWithBobKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyWithBobKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign() byte[] signature = sop.detachedSign()
.key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message); .data(message);
@ -98,17 +102,18 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT); assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT);
} }
@Test @ParameterizedTest
public void signVerifyWithCarolKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyWithCarolKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign() byte[] signature = sop.detachedSign()
.key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message); .data(message);
@ -117,11 +122,12 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
assertSignedBy(verificationList, TestData.CAROL_SIGNING_FINGERPRINT, TestData.CAROL_PRIMARY_FINGERPRINT); assertSignedBy(verificationList, TestData.CAROL_SIGNING_FINGERPRINT, TestData.CAROL_PRIMARY_FINGERPRINT);
} }
@Test @ParameterizedTest
public void signVerifyWithEncryptedKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyWithEncryptedKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign() byte[] signature = sop.detachedSign()
.key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.withKeyPassword(TestData.PASSWORD) .withKeyPassword(TestData.PASSWORD)
.data(message) .data(message)
@ -130,7 +136,7 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
assertArrayStartsWith(signature, BEGIN_PGP_SIGNATURE_BYTES); assertArrayStartsWith(signature, BEGIN_PGP_SIGNATURE_BYTES);
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message); .data(message);
@ -138,22 +144,23 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
assertFalse(verificationList.isEmpty()); assertFalse(verificationList.isEmpty());
} }
@Test @ParameterizedTest
public void signArmorVerifyWithBobKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signArmorVerifyWithBobKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().detachedSign() byte[] signature = sop.detachedSign()
.key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.noArmor() .noArmor()
.data(message) .data(message)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(signature) .data(signature)
.getBytes(); .getBytes();
List<Verification> verificationList = getSop().detachedVerify() List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(armored) .signatures(armored)
.data(message); .data(message);
@ -162,32 +169,30 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT); assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT);
} }
@Test @ParameterizedTest
public void verifyNotAfterThrowsNoSignature() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE) public void verifyNotAfterThrowsNoSignature(SOP sop) {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8); byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
Date signatureDate = TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE; Date signatureDate = TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE;
Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before sig Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before sig
assertThrows(SOPGPException.NoSignature.class, () -> getSop().detachedVerify() assertThrows(SOPGPException.NoSignature.class, () -> sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.notAfter(beforeSignature) .notAfter(beforeSignature)
.signatures(signature) .signatures(signature)
.data(message)); .data(message));
} }
@Test @ParameterizedTest
public void verifyNotBeforeThrowsNoSignature() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE) public void verifyNotBeforeThrowsNoSignature(SOP sop) {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8); byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
Date signatureDate = TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE; Date signatureDate = TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE;
Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec after sig Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec after sig
assertThrows(SOPGPException.NoSignature.class, () -> getSop().detachedVerify() assertThrows(SOPGPException.NoSignature.class, () -> sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.notBefore(afterSignature) .notBefore(afterSignature)
.signatures(signature) .signatures(signature)
@ -195,24 +200,24 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
} }
@Test @ParameterizedTest
public void signVerifyWithEncryptedKeyWithoutPassphraseFails() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.27.2"); // does not return exit code 67 for encrypted keys without passphrase public void signVerifyWithEncryptedKeyWithoutPassphraseFails(SOP sop) {
assertThrows(SOPGPException.KeyIsProtected.class, () -> assertThrows(SOPGPException.KeyIsProtected.class, () ->
getSop().detachedSign() sop.detachedSign()
.key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.data(TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8)) .data(TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8))
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes()); .getBytes());
} }
@Test @ParameterizedTest
public void signWithProtectedKeyAndMultiplePassphrasesTest() @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signWithProtectedKeyAndMultiplePassphrasesTest(SOP sop)
throws IOException { throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = getSop().sign() byte[] signature = sop.sign()
.key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.withKeyPassword("wrong") .withKeyPassword("wrong")
.withKeyPassword(TestData.PASSWORD) // correct .withKeyPassword(TestData.PASSWORD) // correct
@ -221,22 +226,20 @@ public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOP
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
assertFalse(getSop().verify() assertFalse(sop.verify()
.cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature) .signatures(signature)
.data(message) .data(message)
.isEmpty()); .isEmpty());
} }
@Test @ParameterizedTest
public void verifyMissingCertCausesMissingArg() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.27.3"); public void verifyMissingCertCausesMissingArg(SOP sop) {
ignoreIf("PGPainless-SOP", Is.geq, "0.0.0"); // PGPainless uses picocli which throws
// UNSUPPORTED_OPTION for missing arg
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
assertThrows(SOPGPException.MissingArg.class, () -> assertThrows(SOPGPException.MissingArg.class, () ->
getSop().verify() sop.verify()
.signatures(TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8)) .signatures(TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8))
.data(message)); .data(message));
} }

View file

@ -4,10 +4,11 @@
package sop.external; package sop.external;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.params.provider.MethodSource;
import sop.ByteArrayAndResult; import sop.ByteArrayAndResult;
import sop.DecryptionResult; import sop.DecryptionResult;
import sop.SOP;
import sop.Verification; import sop.Verification;
import sop.enums.EncryptAs; import sop.enums.EncryptAs;
import sop.exception.SOPGPException; import sop.exception.SOPGPException;
@ -25,18 +26,18 @@ import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest { public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest {
@Test @ParameterizedTest
public void encryptDecryptRoundTripPasswordTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void encryptDecryptRoundTripPasswordTest(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = sop.encrypt()
.withPassword("sw0rdf1sh") .withPassword("sw0rdf1sh")
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
byte[] plaintext = getSop().decrypt() byte[] plaintext = sop.decrypt()
.withPassword("sw0rdf1sh") .withPassword("sw0rdf1sh")
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult() .toByteArrayAndResult()
@ -45,15 +46,16 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertArrayEquals(message, plaintext); assertArrayEquals(message, plaintext);
} }
@Test @ParameterizedTest
public void encryptDecryptRoundTripAliceTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void encryptDecryptRoundTripAliceTest(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = sop.encrypt()
.withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -65,15 +67,16 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertNotNull(result.getSessionKey().get()); assertNotNull(result.getSessionKey().get());
} }
@Test @ParameterizedTest
public void encryptDecryptRoundTripBobTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void encryptDecryptRoundTripBobTest(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = sop.encrypt()
.withCert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
byte[] plaintext = getSop().decrypt() byte[] plaintext = sop.decrypt()
.withKey(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult() .toByteArrayAndResult()
@ -82,17 +85,16 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertArrayEquals(message, plaintext); assertArrayEquals(message, plaintext);
} }
@Test @ParameterizedTest
public void encryptDecryptRoundTripCarolTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.geq, "0.0.0"); // sqop reports cert not encryption capable public void encryptDecryptRoundTripCarolTest(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = sop.encrypt()
.withCert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
byte[] plaintext = getSop().decrypt() byte[] plaintext = sop.decrypt()
.withKey(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult() .toByteArrayAndResult()
@ -101,22 +103,21 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertArrayEquals(message, plaintext); assertArrayEquals(message, plaintext);
} }
@Test @ParameterizedTest
public void encryptNoArmorThenArmorThenDecryptRoundTrip() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // Invalid data type public void encryptNoArmorThenArmorThenDecryptRoundTrip(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = sop.encrypt()
.withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.noArmor() .noArmor()
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(ciphertext) .data(ciphertext)
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(armored) .ciphertext(armored)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -125,16 +126,17 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertArrayEquals(message, plaintext); assertArrayEquals(message, plaintext);
} }
@Test @ParameterizedTest
public void encryptSignDecryptVerifyRoundTripAliceTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void encryptSignDecryptVerifyRoundTripAliceTest(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = sop.encrypt()
.withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signWith(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .signWith(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
@ -150,17 +152,18 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertTrue(verificationList.get(0).toString().contains("EB85BB5FA33A75E15E944E63F231550C4F47E38E EB85BB5FA33A75E15E944E63F231550C4F47E38E")); assertTrue(verificationList.get(0).toString().contains("EB85BB5FA33A75E15E944E63F231550C4F47E38E EB85BB5FA33A75E15E944E63F231550C4F47E38E"));
} }
@Test @ParameterizedTest
public void encryptSignAsTextDecryptVerifyRoundTripAliceTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void encryptSignAsTextDecryptVerifyRoundTripAliceTest(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = sop.encrypt()
.withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signWith(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .signWith(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.mode(EncryptAs.Text) .mode(EncryptAs.Text)
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
@ -176,29 +179,28 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertTrue(verificationList.get(0).toString().contains("EB85BB5FA33A75E15E944E63F231550C4F47E38E EB85BB5FA33A75E15E944E63F231550C4F47E38E")); assertTrue(verificationList.get(0).toString().contains("EB85BB5FA33A75E15E944E63F231550C4F47E38E EB85BB5FA33A75E15E944E63F231550C4F47E38E"));
} }
@Test @ParameterizedTest
public void encryptSignDecryptVerifyRoundTripWithFreshEncryptedKeyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); public void encryptSignDecryptVerifyRoundTripWithFreshEncryptedKeyTest(SOP sop) throws IOException {
byte[] keyPassword = "sw0rdf1sh".getBytes(StandardCharsets.UTF_8); byte[] keyPassword = "sw0rdf1sh".getBytes(StandardCharsets.UTF_8);
byte[] key = getSop().generateKey() byte[] key = sop.generateKey()
.withKeyPassword(keyPassword) .withKeyPassword(keyPassword)
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.generate() .generate()
.getBytes(); .getBytes();
byte[] cert = getSop().extractCert() byte[] cert = sop.extractCert()
.key(key) .key(key)
.getBytes(); .getBytes();
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = getSop().encrypt() byte[] ciphertext = sop.encrypt()
.withCert(cert) .withCert(cert)
.signWith(key) .signWith(key)
.withKeyPassword(keyPassword) .withKeyPassword(keyPassword)
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(key) .withKey(key)
.withKeyPassword(keyPassword) .withKeyPassword(keyPassword)
.verifyWithCert(cert) .verifyWithCert(cert)
@ -209,11 +211,9 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertArrayEquals(message, bytesAndResult.getBytes()); assertArrayEquals(message, bytesAndResult.getBytes());
} }
@Test @ParameterizedTest
public void decryptVerifyNotAfterTest() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("PGPainless-SOP", Is.le, "1.4.2"); // does not recognize --verify-not-after public void decryptVerifyNotAfterTest(SOP sop) {
ignoreIf("sqop", Is.leq, "0.27.2"); // does not throw NoSignature
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" + byte[] message = ("-----BEGIN PGP MESSAGE-----\n" +
"\n" + "\n" +
"wV4DR2b2udXyHrYSAQdAwlOwwyxFDJta5+H9abgSj8jum9v7etUc9usdrElESmow\n" + "wV4DR2b2udXyHrYSAQdAwlOwwyxFDJta5+H9abgSj8jum9v7etUc9usdrElESmow\n" +
@ -231,7 +231,7 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before signing date Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before signing date
assertThrows(SOPGPException.NoSignature.class, () -> { assertThrows(SOPGPException.NoSignature.class, () -> {
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.verifyNotAfter(beforeSignature) .verifyNotAfter(beforeSignature)
@ -244,11 +244,9 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
}); });
} }
@Test @ParameterizedTest
public void decryptVerifyNotBeforeTest() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("PGPainless-SOP", Is.le, "1.4.2"); // does not recognize --verify-not-after public void decryptVerifyNotBeforeTest(SOP sop) {
ignoreIf("sqop", Is.leq, "0.27.2"); // does not throw NoSignature
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" + byte[] message = ("-----BEGIN PGP MESSAGE-----\n" +
"\n" + "\n" +
"wV4DR2b2udXyHrYSAQdAwlOwwyxFDJta5+H9abgSj8jum9v7etUc9usdrElESmow\n" + "wV4DR2b2udXyHrYSAQdAwlOwwyxFDJta5+H9abgSj8jum9v7etUc9usdrElESmow\n" +
@ -266,7 +264,7 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec after signing date Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec after signing date
assertThrows(SOPGPException.NoSignature.class, () -> { assertThrows(SOPGPException.NoSignature.class, () -> {
ByteArrayAndResult<DecryptionResult> bytesAndResult = getSop().decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.verifyNotBefore(afterSignature) .verifyNotBefore(afterSignature)
@ -279,12 +277,12 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
}); });
} }
@Test @ParameterizedTest
public void missingArgsTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.27.3"); public void missingArgsTest(SOP sop) {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
assertThrows(SOPGPException.MissingArg.class, () -> getSop().encrypt() assertThrows(SOPGPException.MissingArg.class, () -> sop.encrypt()
.plaintext(message) .plaintext(message)
.getBytes()); .getBytes());
} }

View file

@ -4,8 +4,9 @@
package sop.external; package sop.external;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
@ -16,58 +17,59 @@ import static sop.external.JUtils.arrayStartsWith;
import static sop.external.JUtils.assertArrayStartsWith; import static sop.external.JUtils.assertArrayStartsWith;
import static sop.external.JUtils.assertAsciiArmorEquals; import static sop.external.JUtils.assertAsciiArmorEquals;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalExtractCertTest extends AbstractExternalSOPTest { public class ExternalExtractCertTest extends AbstractExternalSOPTest {
private static final String BEGIN_PGP_PUBLIC_KEY_BLOCK = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n"; private static final String BEGIN_PGP_PUBLIC_KEY_BLOCK = "-----BEGIN PGP PUBLIC KEY BLOCK-----\n";
private static final byte[] BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES = BEGIN_PGP_PUBLIC_KEY_BLOCK.getBytes(StandardCharsets.UTF_8); private static final byte[] BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES = BEGIN_PGP_PUBLIC_KEY_BLOCK.getBytes(StandardCharsets.UTF_8);
@Test @ParameterizedTest
public void extractArmoredCertFromArmoredKeyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
InputStream keyIn = getSop().generateKey() public void extractArmoredCertFromArmoredKeyTest(SOP sop) throws IOException {
InputStream keyIn = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.generate() .generate()
.getInputStream(); .getInputStream();
byte[] cert = getSop().extractCert().key(keyIn).getBytes(); byte[] cert = sop.extractCert().key(keyIn).getBytes();
assertArrayStartsWith(cert, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES); assertArrayStartsWith(cert, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES);
} }
@Test @ParameterizedTest
public void extractAliceCertFromAliceKeyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("PGPainless-SOP", Is.geq, "0.0.0"); // PGPainless uses old CTB public void extractAliceCertFromAliceKeyTest(SOP sop) throws IOException {
byte[] armoredCert = getSop().extractCert() byte[] armoredCert = sop.extractCert()
.key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.getBytes(); .getBytes();
assertAsciiArmorEquals(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8), armoredCert); assertAsciiArmorEquals(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8), armoredCert);
} }
@Test @ParameterizedTest
public void extractBobsCertFromBobsKeyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("PGPainless-SOP", Is.geq, "0.0.0"); // PGPainless uses old CTB public void extractBobsCertFromBobsKeyTest(SOP sop) throws IOException {
byte[] armoredCert = getSop().extractCert() byte[] armoredCert = sop.extractCert()
.key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.getBytes(); .getBytes();
assertAsciiArmorEquals(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8), armoredCert); assertAsciiArmorEquals(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8), armoredCert);
} }
@Test @ParameterizedTest
public void extractCarolsCertFromCarolsKeyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("PGPainless-SOP", Is.geq, "0.0.0"); // PGPainless uses old CTB public void extractCarolsCertFromCarolsKeyTest(SOP sop) throws IOException {
byte[] armoredCert = getSop().extractCert() byte[] armoredCert = sop.extractCert()
.key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.getBytes(); .getBytes();
assertAsciiArmorEquals(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8), armoredCert); assertAsciiArmorEquals(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8), armoredCert);
} }
@Test @ParameterizedTest
public void extractUnarmoredCertFromArmoredKeyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
InputStream keyIn = getSop().generateKey() public void extractUnarmoredCertFromArmoredKeyTest(SOP sop) throws IOException {
InputStream keyIn = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.generate() .generate()
.getInputStream(); .getInputStream();
byte[] cert = getSop().extractCert() byte[] cert = sop.extractCert()
.noArmor() .noArmor()
.key(keyIn) .key(keyIn)
.getBytes(); .getBytes();
@ -75,30 +77,32 @@ public class ExternalExtractCertTest extends AbstractExternalSOPTest {
assertFalse(arrayStartsWith(cert, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES)); assertFalse(arrayStartsWith(cert, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES));
} }
@Test @ParameterizedTest
public void extractArmoredCertFromUnarmoredKeyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
InputStream keyIn = getSop().generateKey() public void extractArmoredCertFromUnarmoredKeyTest(SOP sop) throws IOException {
InputStream keyIn = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.noArmor() .noArmor()
.generate() .generate()
.getInputStream(); .getInputStream();
byte[] cert = getSop().extractCert() byte[] cert = sop.extractCert()
.key(keyIn) .key(keyIn)
.getBytes(); .getBytes();
assertArrayStartsWith(cert, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES); assertArrayStartsWith(cert, BEGIN_PGP_PUBLIC_KEY_BLOCK_BYTES);
} }
@Test @ParameterizedTest
public void extractUnarmoredCertFromUnarmoredKeyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
InputStream keyIn = getSop().generateKey() public void extractUnarmoredCertFromUnarmoredKeyTest(SOP sop) throws IOException {
InputStream keyIn = sop.generateKey()
.noArmor() .noArmor()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.generate() .generate()
.getInputStream(); .getInputStream();
byte[] cert = getSop().extractCert() byte[] cert = sop.extractCert()
.noArmor() .noArmor()
.key(keyIn) .key(keyIn)
.getBytes(); .getBytes();

View file

@ -4,8 +4,9 @@
package sop.external; package sop.external;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
@ -14,16 +15,16 @@ import java.nio.charset.StandardCharsets;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static sop.external.JUtils.assertArrayStartsWith; import static sop.external.JUtils.assertArrayStartsWith;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalGenerateKeyTest extends AbstractExternalSOPTest { public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
private static final Charset UTF8 = StandardCharsets.UTF_8; private static final Charset UTF8 = StandardCharsets.UTF_8;
private static final String BEGIN_PGP_PRIVATE_KEY_BLOCK = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n"; private static final String BEGIN_PGP_PRIVATE_KEY_BLOCK = "-----BEGIN PGP PRIVATE KEY BLOCK-----\n";
byte[] BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES = BEGIN_PGP_PRIVATE_KEY_BLOCK.getBytes(UTF8); byte[] BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES = BEGIN_PGP_PRIVATE_KEY_BLOCK.getBytes(UTF8);
@Test @ParameterizedTest
public void generateKeyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
byte[] key = getSop().generateKey() public void generateKeyTest(SOP sop) throws IOException {
byte[] key = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.generate() .generate()
.getBytes(); .getBytes();
@ -31,9 +32,10 @@ public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES); assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES);
} }
@Test @ParameterizedTest
public void generateKeyNoArmor() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
byte[] key = getSop().generateKey() public void generateKeyNoArmor(SOP sop) throws IOException {
byte[] key = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.noArmor() .noArmor()
.generate() .generate()
@ -42,9 +44,10 @@ public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
assertFalse(JUtils.arrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES)); assertFalse(JUtils.arrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES));
} }
@Test @ParameterizedTest
public void generateKeyWithMultipleUserIdsTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
byte[] key = getSop().generateKey() public void generateKeyWithMultipleUserIdsTest(SOP sop) throws IOException {
byte[] key = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.userId("Bob <bob@openpgp.org>") .userId("Bob <bob@openpgp.org>")
.generate() .generate()
@ -53,23 +56,20 @@ public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES); assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES);
} }
@Test @ParameterizedTest
public void generateKeyWithoutUserIdTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("pgpainless-cli", Is.le, "1.3.15"); public void generateKeyWithoutUserIdTest(SOP sop) throws IOException {
byte[] key = sop.generateKey()
byte[] key = getSop().generateKey()
.generate() .generate()
.getBytes(); .getBytes();
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES); assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES);
} }
@Test @ParameterizedTest
public void generateKeyWithPasswordTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.le, "0.27.0"); public void generateKeyWithPasswordTest(SOP sop) throws IOException {
ignoreIf("pgpainless-cli", Is.le, "1.3.0"); byte[] key = sop.generateKey()
byte[] key = getSop().generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.withKeyPassword("sw0rdf1sh") .withKeyPassword("sw0rdf1sh")
.generate() .generate()
@ -78,14 +78,10 @@ public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES); assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK_BYTES);
} }
@Test @ParameterizedTest
public void generateKeyWithMultipleUserIdsAndPassword() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.le, "0.27.0"); public void generateKeyWithMultipleUserIdsAndPassword(SOP sop) throws IOException {
ignoreIf("PGPainless-SOP", Is.le, "1.3.15"); byte[] key = sop.generateKey()
ignoreIf("PGPainless-SOP", Is.eq, "1.4.0");
ignoreIf("PGPainless-SOP", Is.eq, "1.4.1");
byte[] key = getSop().generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.userId("Bob <bob@openpgp.org>") .userId("Bob <bob@openpgp.org>")
.withKeyPassword("sw0rdf1sh") .withKeyPassword("sw0rdf1sh")

View file

@ -4,9 +4,10 @@
package sop.external; package sop.external;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.params.provider.MethodSource;
import sop.ByteArrayAndResult; import sop.ByteArrayAndResult;
import sop.SOP;
import sop.Signatures; import sop.Signatures;
import sop.Verification; import sop.Verification;
@ -19,23 +20,21 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static sop.external.JUtils.arrayStartsWith; import static sop.external.JUtils.arrayStartsWith;
import static sop.external.JUtils.assertArrayStartsWith; import static sop.external.JUtils.assertArrayStartsWith;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExternalSOPTest { public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExternalSOPTest {
private static final byte[] BEGIN_PGP_SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n".getBytes(StandardCharsets.UTF_8); private static final byte[] BEGIN_PGP_SIGNATURE = "-----BEGIN PGP SIGNATURE-----\n".getBytes(StandardCharsets.UTF_8);
@Test @ParameterizedTest
public void inlineSignThenDetachThenDetachedVerifyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void inlineSignThenDetachThenDetachedVerifyTest(SOP sop) throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = sop.inlineSign()
.key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.getBytes(); .getBytes();
ByteArrayAndResult<Signatures> bytesAndResult = getSop().inlineDetach() ByteArrayAndResult<Signatures> bytesAndResult = sop.inlineDetach()
.message(inlineSigned) .message(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -45,7 +44,7 @@ public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExterna
byte[] signatures = bytesAndResult.getResult() byte[] signatures = bytesAndResult.getResult()
.getBytes(); .getBytes();
List<Verification> verifications = getSop().detachedVerify() List<Verification> verifications = sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signatures) .signatures(signatures)
.data(plaintext); .data(plaintext);
@ -53,18 +52,17 @@ public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExterna
assertFalse(verifications.isEmpty()); assertFalse(verifications.isEmpty());
} }
@Test @ParameterizedTest
public void inlineSignThenDetachNoArmorThenArmorThenDetachedVerifyTest() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void inlineSignThenDetachNoArmorThenArmorThenDetachedVerifyTest(SOP sop) throws IOException {
byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8); byte[] message = "Hello, World!\n".getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = sop.inlineSign()
.key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.getBytes(); .getBytes();
ByteArrayAndResult<Signatures> bytesAndResult = getSop().inlineDetach() ByteArrayAndResult<Signatures> bytesAndResult = sop.inlineDetach()
.noArmor() .noArmor()
.message(inlineSigned) .message(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -76,12 +74,12 @@ public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExterna
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(signatures, BEGIN_PGP_SIGNATURE)); assertFalse(arrayStartsWith(signatures, BEGIN_PGP_SIGNATURE));
byte[] armored = getSop().armor() byte[] armored = sop.armor()
.data(signatures) .data(signatures)
.getBytes(); .getBytes();
assertArrayStartsWith(armored, BEGIN_PGP_SIGNATURE); assertArrayStartsWith(armored, BEGIN_PGP_SIGNATURE);
List<Verification> verifications = getSop().detachedVerify() List<Verification> verifications = sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(armored) .signatures(armored)
.data(plaintext); .data(plaintext);

View file

@ -4,9 +4,10 @@
package sop.external; package sop.external;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.params.provider.MethodSource;
import sop.ByteArrayAndResult; import sop.ByteArrayAndResult;
import sop.SOP;
import sop.Verification; import sop.Verification;
import sop.enums.InlineSignAs; import sop.enums.InlineSignAs;
import sop.exception.SOPGPException; import sop.exception.SOPGPException;
@ -21,7 +22,6 @@ import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static sop.external.JUtils.assertSignedBy; import static sop.external.JUtils.assertSignedBy;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest { public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
private static final String BEGIN_PGP_MESSAGE = "-----BEGIN PGP MESSAGE-----\n"; private static final String BEGIN_PGP_MESSAGE = "-----BEGIN PGP MESSAGE-----\n";
@ -29,20 +29,19 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
private static final String BEGIN_PGP_SIGNED_MESSAGE = "-----BEGIN PGP SIGNED MESSAGE-----\n"; private static final String BEGIN_PGP_SIGNED_MESSAGE = "-----BEGIN PGP SIGNED MESSAGE-----\n";
private static final byte[] BEGIN_PGP_SIGNED_MESSAGE_BYTES = BEGIN_PGP_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8); private static final byte[] BEGIN_PGP_SIGNED_MESSAGE_BYTES = BEGIN_PGP_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
@Test @ParameterizedTest
public void inlineSignVerifyAlice() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void inlineSignVerifyAlice(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = sop.inlineSign()
.key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.getBytes(); .getBytes();
JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES); JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES);
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned) .data(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -52,13 +51,12 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT); assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@Test @ParameterizedTest
public void inlineSignVerifyAliceNoArmor() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void inlineSignVerifyAliceNoArmor(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = sop.inlineSign()
.key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.noArmor() .noArmor()
.data(message) .data(message)
@ -66,7 +64,7 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertFalse(JUtils.arrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES)); assertFalse(JUtils.arrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES));
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned) .data(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -76,13 +74,12 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT); assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@Test @ParameterizedTest
public void clearsignVerifyAlice() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void clearsignVerifyAlice(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] clearsigned = getSop().inlineSign() byte[] clearsigned = sop.inlineSign()
.key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.mode(InlineSignAs.clearsigned) .mode(InlineSignAs.clearsigned)
.data(message) .data(message)
@ -90,7 +87,7 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
JUtils.assertArrayStartsWith(clearsigned, BEGIN_PGP_SIGNED_MESSAGE_BYTES); JUtils.assertArrayStartsWith(clearsigned, BEGIN_PGP_SIGNED_MESSAGE_BYTES);
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(clearsigned) .data(clearsigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -100,15 +97,13 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT); assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@Test @ParameterizedTest
public void inlineVerifyCompareSignatureDate() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void inlineVerifyCompareSignatureDate(SOP sop) throws IOException {
ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE)
byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE; Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -116,52 +111,47 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT, signatureDate); assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT, signatureDate);
} }
@Test @ParameterizedTest
public void assertNotBeforeThrowsNoSignature() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void assertNotBeforeThrowsNoSignature(SOP sop) {
ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE)
byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE; Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec before sig Date afterSignature = new Date(signatureDate.getTime() + 1000); // 1 sec before sig
assertThrows(SOPGPException.NoSignature.class, () -> getSop().inlineVerify() assertThrows(SOPGPException.NoSignature.class, () -> sop.inlineVerify()
.notBefore(afterSignature) .notBefore(afterSignature)
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult()); .toByteArrayAndResult());
} }
@Test @ParameterizedTest
public void assertNotAfterThrowsNoSignature() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void assertNotAfterThrowsNoSignature(SOP sop) {
ignoreIf("sqop", Is.leq, "0.27.2"); // returns 1 instead of 3 (NO_SIGNATURE)
byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.ALICE_INLINE_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE; Date signatureDate = TestData.ALICE_INLINE_SIGNED_MESSAGE_DATE;
Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before sig Date beforeSignature = new Date(signatureDate.getTime() - 1000); // 1 sec before sig
assertThrows(SOPGPException.NoSignature.class, () -> getSop().inlineVerify() assertThrows(SOPGPException.NoSignature.class, () -> sop.inlineVerify()
.notAfter(beforeSignature) .notAfter(beforeSignature)
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult()); .toByteArrayAndResult());
} }
@Test @ParameterizedTest
public void inlineSignVerifyBob() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void inlineSignVerifyBob(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = sop.inlineSign()
.key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.getBytes(); .getBytes();
JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES); JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES);
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned) .data(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -171,20 +161,19 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT); assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT);
} }
@Test @ParameterizedTest
public void inlineSignVerifyCarol() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void inlineSignVerifyCarol(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = sop.inlineSign()
.key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.getBytes(); .getBytes();
JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES); JUtils.assertArrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE_BYTES);
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned) .data(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -194,20 +183,19 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertSignedBy(verificationList, TestData.CAROL_SIGNING_FINGERPRINT, TestData.CAROL_PRIMARY_FINGERPRINT); assertSignedBy(verificationList, TestData.CAROL_SIGNING_FINGERPRINT, TestData.CAROL_PRIMARY_FINGERPRINT);
} }
@Test @ParameterizedTest
public void inlineSignVerifyProtectedKey() throws IOException { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
ignoreIf("sqop", Is.leq, "0.26.1"); // inline-sign not supported public void inlineSignVerifyProtectedKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = getSop().inlineSign() byte[] inlineSigned = sop.inlineSign()
.withKeyPassword(TestData.PASSWORD) .withKeyPassword(TestData.PASSWORD)
.key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.mode(InlineSignAs.binary) .mode(InlineSignAs.binary)
.data(message) .data(message)
.getBytes(); .getBytes();
ByteArrayAndResult<List<Verification>> bytesAndResult = getSop().inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned) .data(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();

View file

@ -4,38 +4,42 @@
package sop.external; package sop.external;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertNotNull; import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.junit.jupiter.api.Assertions.assertTrue; import static org.junit.jupiter.api.Assertions.assertTrue;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class ExternalVersionTest extends AbstractExternalSOPTest { public class ExternalVersionTest extends AbstractExternalSOPTest {
@Test @ParameterizedTest
public void versionNameTest() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
String name = getSop().version().getName(); public void versionNameTest(SOP sop) {
String name = sop.version().getName();
assertNotNull(name); assertNotNull(name);
assertFalse(name.isEmpty()); assertFalse(name.isEmpty());
} }
@Test @ParameterizedTest
public void versionVersionTest() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
String version = getSop().version().getVersion(); public void versionVersionTest(SOP sop) {
String version = sop.version().getVersion();
assertTrue(version.matches("\\d+(\\.\\d+)*\\S*")); assertTrue(version.matches("\\d+(\\.\\d+)*\\S*"));
} }
@Test @ParameterizedTest
public void backendVersionTest() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
String backend = getSop().version().getBackendVersion(); public void backendVersionTest(SOP sop) {
String backend = sop.version().getBackendVersion();
assertFalse(backend.isEmpty()); assertFalse(backend.isEmpty());
} }
@Test @ParameterizedTest
public void extendedVersionTest() { @MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
String extended = getSop().version().getExtendedVersion(); public void extendedVersionTest(SOP sop) {
String extended = sop.version().getExtendedVersion();
assertFalse(extended.isEmpty()); assertFalse(extended.isEmpty());
} }

View file

@ -1,58 +0,0 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.external;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.condition.EnabledIf;
import sop.exception.SOPGPException;
import java.io.IOException;
import java.util.Properties;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assumptions.assumeTrue;
@EnabledIf("sop.external.AbstractExternalSOPTest#isExternalSopInstalled")
public class UnsupportedSubcommandTest extends AbstractExternalSOPTest {
private final UnsupportedSubcommandExternal unsupportedSubcommand;
public UnsupportedSubcommandTest() {
String backend = readSopBackendFromProperties();
assumeTrue(backend != null);
Properties environment = readBackendEnvironment();
unsupportedSubcommand = new UnsupportedSubcommandExternal(backend, environment);
}
@Test
public void testUnsupportedSubcommand() {
// "sop unsupported" returns error code UNSUPPORTED_SUBCOMMAND
assertThrows(SOPGPException.UnsupportedSubcommand.class,
unsupportedSubcommand::executeUnsupportedSubcommand);
}
private static class UnsupportedSubcommandExternal {
private final Runtime runtime = Runtime.getRuntime();
private final String binary;
private final Properties environment;
UnsupportedSubcommandExternal(String binaryName, Properties environment) {
this.binary = binaryName;
this.environment = environment;
}
public void executeUnsupportedSubcommand() {
String[] command = new String[] {binary, "unsupported"}; // ~$ sop unsupported
String[] env = ExternalSOP.propertiesToEnv(environment).toArray(new String[0]);
try {
Process process = runtime.exec(command, env);
ExternalSOP.finish(process);
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}
}