Allow for extension of test suite from 3rd party SOP implementations

This commit is contained in:
Paul Schaub 2023-01-31 18:20:27 +01:00
parent fd426b533c
commit 0709bce35c
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
33 changed files with 731 additions and 547 deletions

View file

@ -7,7 +7,7 @@ pipeline:
# Checkout code # Checkout code
- git checkout $CI_COMMIT_BRANCH - git checkout $CI_COMMIT_BRANCH
# Prepare CI # Prepare CI
- cp external-sop/src/main/resources/sop/external/config.json.ci external-sop/src/main/resources/sop/external/config.json - cp external-sop/src/main/resources/sop/testsuite/external/config.json.ci external-sop/src/main/resources/sop/testsuite/external/config.json
# Code works # Code works
- gradle test - gradle test
# Code is clean # Code is clean

View file

@ -16,21 +16,27 @@ 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" testImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion" testRuntimeOnly "org.junit.jupiter:junit-jupiter-engine:$junitVersion"
// Read json config file
testImplementation "com.google.code.gson:gson:2.10.1"
api project(":sop-java") api project(":sop-java")
testImplementation(testFixtures(project(":sop-java")))
api "org.slf4j:slf4j-api:$slf4jVersion" api "org.slf4j:slf4j-api:$slf4jVersion"
testImplementation "ch.qos.logback:logback-classic:$logbackVersion" testImplementation "ch.qos.logback:logback-classic:$logbackVersion"
// @Nonnull, @Nullable... // @Nonnull, @Nullable...
implementation "com.google.code.findbugs:jsr305:$jsrVersion" implementation "com.google.code.findbugs:jsr305:$jsrVersion"
// The ExternalTestSubjectFactory reads json config file to find configured SOP binaries...
testImplementation "com.google.code.gson:gson:2.10.1"
// ...and extends TestSubjectFactory
testImplementation(testFixtures(project(":sop-java")))
} }
test { test {
// Inject configured external SOP instances using our custom TestSubjectFactory
environment("test.implementation", "sop.testsuite.external.ExternalSOPInstanceFactory")
useJUnitPlatform() useJUnitPlatform()
// since we test external backends, we ignore test failures in this module // since we test external backends, we ignore test failures in this module
ignoreFailures = true ignoreFailures = true
} }

View file

@ -1,263 +0,0 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.external;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP;
import sop.Verification;
import sop.enums.SignAs;
import sop.exception.SOPGPException;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static sop.testing.JUtils.assertArrayStartsWith;
import static sop.testing.JUtils.assertSignedBy;
import static sop.testing.TestData.ALICE_CERT;
import static sop.testing.TestData.ALICE_DETACHED_SIGNED_MESSAGE;
import static sop.testing.TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE;
import static sop.testing.TestData.ALICE_KEY;
import static sop.testing.TestData.ALICE_PRIMARY_FINGERPRINT;
import static sop.testing.TestData.ALICE_SIGNING_FINGERPRINT;
import static sop.testing.TestData.BEGIN_PGP_SIGNATURE;
import static sop.testing.TestData.BOB_CERT;
import static sop.testing.TestData.BOB_KEY;
import static sop.testing.TestData.BOB_PRIMARY_FINGERPRINT;
import static sop.testing.TestData.BOB_SIGNING_FINGERPRINT;
import static sop.testing.TestData.CAROL_CERT;
import static sop.testing.TestData.CAROL_KEY;
import static sop.testing.TestData.CAROL_PRIMARY_FINGERPRINT;
import static sop.testing.TestData.CAROL_SIGNING_FINGERPRINT;
import static sop.testing.TestData.PASSWORD;
import static sop.testing.TestData.PASSWORD_PROTECTED_CERT;
import static sop.testing.TestData.PASSWORD_PROTECTED_KEY;
import static sop.testing.TestData.PLAINTEXT;
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends")
public class ExternalDetachedSignVerifyRoundTripTest extends AbstractExternalSOPTest {
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyWithAliceKey(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message)
.toByteArrayAndResult()
.getBytes();
List<Verification> verificationList = sop.detachedVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT);
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyTextModeWithAliceKey(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.mode(SignAs.Text)
.data(message)
.toByteArrayAndResult()
.getBytes();
List<Verification> verificationList = sop.detachedVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT);
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void verifyKnownMessageWithAliceCert(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
List<Verification> verificationList = sop.detachedVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT, ALICE_DETACHED_SIGNED_MESSAGE_DATE);
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyWithBobKey(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(BOB_KEY.getBytes(StandardCharsets.UTF_8))
.data(message)
.toByteArrayAndResult()
.getBytes();
List<Verification> verificationList = sop.detachedVerify()
.cert(BOB_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
assertSignedBy(verificationList, BOB_SIGNING_FINGERPRINT, BOB_PRIMARY_FINGERPRINT);
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyWithCarolKey(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.data(message)
.toByteArrayAndResult()
.getBytes();
List<Verification> verificationList = sop.detachedVerify()
.cert(CAROL_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
assertSignedBy(verificationList, CAROL_SIGNING_FINGERPRINT, CAROL_PRIMARY_FINGERPRINT);
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyWithEncryptedKey(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.withKeyPassword(PASSWORD)
.data(message)
.toByteArrayAndResult()
.getBytes();
assertArrayStartsWith(signature, BEGIN_PGP_SIGNATURE);
List<Verification> verificationList = sop.detachedVerify()
.cert(PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signArmorVerifyWithBobKey(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(BOB_KEY.getBytes(StandardCharsets.UTF_8))
.noArmor()
.data(message)
.toByteArrayAndResult()
.getBytes();
byte[] armored = sop.armor()
.data(signature)
.getBytes();
List<Verification> verificationList = sop.detachedVerify()
.cert(BOB_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(armored)
.data(message);
assertFalse(verificationList.isEmpty());
assertSignedBy(verificationList, BOB_SIGNING_FINGERPRINT, BOB_PRIMARY_FINGERPRINT);
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void verifyNotAfterThrowsNoSignature(SOP sop) {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
Date beforeSignature = new Date(ALICE_DETACHED_SIGNED_MESSAGE_DATE.getTime() - 1000); // 1 sec before sig
assertThrows(SOPGPException.NoSignature.class, () -> sop.detachedVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.notAfter(beforeSignature)
.signatures(signature)
.data(message));
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void verifyNotBeforeThrowsNoSignature(SOP sop) {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
Date afterSignature = new Date(ALICE_DETACHED_SIGNED_MESSAGE_DATE.getTime() + 1000); // 1 sec after sig
assertThrows(SOPGPException.NoSignature.class, () -> sop.detachedVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.notBefore(afterSignature)
.signatures(signature)
.data(message));
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signVerifyWithEncryptedKeyWithoutPassphraseFails(SOP sop) {
assertThrows(SOPGPException.KeyIsProtected.class, () ->
sop.detachedSign()
.key(PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.data(PLAINTEXT.getBytes(StandardCharsets.UTF_8))
.toByteArrayAndResult()
.getBytes());
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void signWithProtectedKeyAndMultiplePassphrasesTest(SOP sop)
throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.sign()
.key(PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.withKeyPassword("wrong")
.withKeyPassword(PASSWORD) // correct
.withKeyPassword("wrong2")
.data(message)
.toByteArrayAndResult()
.getBytes();
assertFalse(sop.verify()
.cert(PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message)
.isEmpty());
}
@ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends")
public void verifyMissingCertCausesMissingArg(SOP sop) {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8);
assertThrows(SOPGPException.MissingArg.class, () ->
sop.verify()
.signatures(ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8))
.data(message));
}
}

View file

@ -2,26 +2,26 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.external; package sop.testsuite.external;
import com.google.gson.Gson; import com.google.gson.Gson;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.params.provider.Arguments;
import sop.SOP; import sop.SOP;
import sop.external.ExternalSOP;
import sop.testsuite.SOPInstanceFactory;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.InputStreamReader; import java.io.InputStreamReader;
import java.util.ArrayList; import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Properties; import java.util.Properties;
import java.util.stream.Stream;
public abstract class AbstractExternalSOPTest { public class ExternalSOPInstanceFactory extends SOPInstanceFactory {
private static final List<Arguments> backends = new ArrayList<>(); @Override
public Map<String, SOP> provideSOPInstances() {
static { Map<String, SOP> backends = new HashMap<>();
TestSuite suite = readConfiguration(); TestSuite suite = readConfiguration();
if (suite != null && !suite.backends.isEmpty()) { if (suite != null && !suite.backends.isEmpty()) {
for (TestSubject subject : suite.backends) { for (TestSubject subject : suite.backends) {
@ -37,30 +37,24 @@ public abstract class AbstractExternalSOPTest {
} }
SOP sop = new ExternalSOP(subject.sop, env); SOP sop = new ExternalSOP(subject.sop, env);
backends.add(Arguments.of(Named.of(subject.name, sop))); backends.put(subject.name, sop);
} }
} }
return backends;
} }
public static Stream<Arguments> provideBackends() {
return backends.stream();
}
public static TestSuite readConfiguration() { public static TestSuite readConfiguration() {
Gson gson = new Gson(); Gson gson = new Gson();
InputStream inputStream = AbstractExternalSOPTest.class.getResourceAsStream("config.json"); InputStream inputStream = ExternalSOPInstanceFactory.class.getResourceAsStream("config.json");
if (inputStream == null) { if (inputStream == null) {
return null; return null;
} }
InputStreamReader reader = new InputStreamReader(inputStream); InputStreamReader reader = new InputStreamReader(inputStream);
TestSuite suite = gson.fromJson(reader, TestSuite.class); return gson.fromJson(reader, TestSuite.class);
return suite;
} }
public static boolean hasBackends() {
return !backends.isEmpty();
}
// JSON DTOs // JSON DTOs

View file

@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.external.operation;
import sop.testsuite.operation.ArmorDearmorTest;
public class ExternalArmorDearmorTest extends ArmorDearmorTest {
}

View file

@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.external.operation;
import sop.testsuite.operation.DecryptWithSessionKeyTest;
public class ExternalDecryptWithSessionKeyTest extends DecryptWithSessionKeyTest {
}

View file

@ -0,0 +1,10 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.external.operation;
import sop.testsuite.operation.DetachedSignDetachedVerifyTest;
public class ExternalDetachedSignDetachedVerifyTest extends DetachedSignDetachedVerifyTest {
}

View file

@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.external.operation;
import sop.testsuite.operation.EncryptDecryptTest;
public class ExternalEncryptDecryptTest extends EncryptDecryptTest {
}

View file

@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.external.operation;
import sop.testsuite.operation.ExtractCertTest;
public class ExternalExtractCertTest extends ExtractCertTest {
}

View file

@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.external.operation;
import sop.testsuite.operation.GenerateKeyTest;
public class ExternalGenerateKeyTest extends GenerateKeyTest {
}

View file

@ -0,0 +1,12 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.external.operation;
import sop.testsuite.operation.InlineSignInlineDetachDetachedVerifyTest;
public class ExternalInlineSignInlineDetachDetachedVerifyTest
extends InlineSignInlineDetachDetachedVerifyTest {
}

View file

@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.external.operation;
import sop.testsuite.operation.InlineSignInlineVerifyTest;
public class ExternalInlineSignInlineVerifyTest extends InlineSignInlineVerifyTest {
}

View file

@ -0,0 +1,11 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.external.operation;
import sop.testsuite.operation.VersionTest;
public class ExternalVersionTest extends VersionTest {
}

View file

@ -15,8 +15,10 @@ 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"
testFixturesImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion" testFixturesImplementation "org.junit.jupiter:junit-jupiter-api:$junitVersion"
testFixturesImplementation "org.junit.jupiter:junit-jupiter-params:$junitVersion"
} }
test { test {

View file

@ -1,8 +0,0 @@
// SPDX-FileCopyrightText: 2021 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* Stateless OpenPGP Interface for Java.
*/
package sop.testing;

View file

@ -2,7 +2,7 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.testing; package sop.testsuite;
import sop.Verification; import sop.Verification;
import sop.util.UTCUtil; import sop.util.UTCUtil;

View file

@ -0,0 +1,30 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite;
import sop.SOP;
import java.util.Map;
/**
* Factory class to instantiate SOP implementations for testing.
* Overwrite this class and the {@link #provideSOPInstances()} method to return the SOP instances you want
* to test.
* Then, add the following line to your <pre>build.gradle</pre> files <pre>dependencies</pre> section:
* <pre>{@code
* testImplementation(testFixtures("org.pgpainless:sop-java:<version>"))
* }</pre>
* To inject the factory class into the test suite, add the following line to your modules <pre>test</pre> task:
* <pre>{@code
* environment("test.implementation", "org.example.YourTestSubjectFactory")
* }</pre>
* Next, in your <pre>test</pre> sources, extend all test classes from the <pre>testFixtures</pre>
* <pre>sop.operation</pre> package.
* Take a look at the <pre>external-sop</pre> module for an example.
*/
public abstract class SOPInstanceFactory {
public abstract Map<String, SOP> provideSOPInstances();
}

View file

@ -2,7 +2,7 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.testing; package sop.testsuite;
import sop.util.UTCUtil; import sop.util.UTCUtil;

View file

@ -0,0 +1,51 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.operation;
import org.junit.jupiter.api.Named;
import org.junit.jupiter.params.provider.Arguments;
import sop.SOP;
import sop.testsuite.SOPInstanceFactory;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Stream;
public abstract class AbstractSOPTest {
private static final List<Arguments> backends = new ArrayList<>();
static {
// populate instances list via configured test subject factory
String factoryName = System.getenv("test.implementation");
if (factoryName != null) {
try {
Class testSubjectFactoryClass = Class.forName(factoryName);
SOPInstanceFactory factory = (SOPInstanceFactory) testSubjectFactoryClass.newInstance();
Map<String, SOP> testSubjects = factory.provideSOPInstances();
for (String key : testSubjects.keySet()) {
backends.add(Arguments.of(Named.of(key, testSubjects.get(key))));
}
} catch (ClassNotFoundException e) {
throw new RuntimeException(e);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
}
public static Stream<Arguments> provideBackends() {
return backends.stream();
}
public static boolean hasBackends() {
return !backends.isEmpty();
}
}

View file

@ -2,37 +2,32 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.external; package sop.testsuite.operation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP; import sop.SOP;
import sop.testing.TestData; import sop.testsuite.JUtils;
import sop.testsuite.TestData;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static sop.testing.JUtils.arrayStartsWith;
import static sop.testing.JUtils.assertArrayEndsWithIgnoreNewlines;
import static sop.testing.JUtils.assertArrayStartsWith;
import static sop.testing.JUtils.assertAsciiArmorEquals;
import static sop.testing.TestData.BEGIN_PGP_MESSAGE;
import static sop.testing.TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK;
import static sop.testing.TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK;
import static sop.testing.TestData.BEGIN_PGP_SIGNATURE;
import static sop.testing.TestData.END_PGP_MESSAGE;
import static sop.testing.TestData.END_PGP_PRIVATE_KEY_BLOCK;
import static sop.testing.TestData.END_PGP_PUBLIC_KEY_BLOCK;
import static sop.testing.TestData.END_PGP_SIGNATURE;
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends") @EnabledIf("sop.operation.AbstractSOPTest#hasBackends")
public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest { public class ArmorDearmorTest {
static Stream<Arguments> provideInstances() {
return AbstractSOPTest.provideBackends();
}
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void dearmorArmorAliceKey(SOP sop) throws IOException { 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);
@ -40,20 +35,20 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
.data(aliceKey) .data(aliceKey)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK)); Assertions.assertFalse(JUtils.arrayStartsWith(dearmored, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK));
byte[] armored = sop.armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
assertArrayStartsWith(armored, BEGIN_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayStartsWith(armored, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(armored, END_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(armored, TestData.END_PGP_PRIVATE_KEY_BLOCK);
// assertAsciiArmorEquals(aliceKey, armored); // assertAsciiArmorEquals(aliceKey, armored);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void dearmorArmorAliceCert(SOP sop) throws IOException { 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);
@ -61,20 +56,20 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
.data(aliceCert) .data(aliceCert)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK)); Assertions.assertFalse(JUtils.arrayStartsWith(dearmored, TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK));
byte[] armored = sop.armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
assertArrayStartsWith(armored, BEGIN_PGP_PUBLIC_KEY_BLOCK); JUtils.assertArrayStartsWith(armored, TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(armored, END_PGP_PUBLIC_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(armored, TestData.END_PGP_PUBLIC_KEY_BLOCK);
// assertAsciiArmorEquals(aliceCert, armored); // assertAsciiArmorEquals(aliceCert, armored);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void dearmorArmorBobKey(SOP sop) throws IOException { 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);
@ -82,20 +77,20 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
.data(bobKey) .data(bobKey)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK)); Assertions.assertFalse(JUtils.arrayStartsWith(dearmored, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK));
byte[] armored = sop.armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
assertArrayStartsWith(armored, BEGIN_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayStartsWith(armored, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(armored, END_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(armored, TestData.END_PGP_PRIVATE_KEY_BLOCK);
// assertAsciiArmorEquals(bobKey, armored); // assertAsciiArmorEquals(bobKey, armored);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void dearmorArmorBobCert(SOP sop) throws IOException { 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);
@ -103,20 +98,20 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
.data(bobCert) .data(bobCert)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK)); Assertions.assertFalse(JUtils.arrayStartsWith(dearmored, TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK));
byte[] armored = sop.armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
assertArrayStartsWith(armored, BEGIN_PGP_PUBLIC_KEY_BLOCK); JUtils.assertArrayStartsWith(armored, TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(armored, END_PGP_PUBLIC_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(armored, TestData.END_PGP_PUBLIC_KEY_BLOCK);
// assertAsciiArmorEquals(bobCert, armored); // assertAsciiArmorEquals(bobCert, armored);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void dearmorArmorCarolKey(SOP sop) throws IOException { 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);
@ -124,20 +119,20 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
.data(carolKey) .data(carolKey)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PRIVATE_KEY_BLOCK)); Assertions.assertFalse(JUtils.arrayStartsWith(dearmored, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK));
byte[] armored = sop.armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
assertArrayStartsWith(armored, BEGIN_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayStartsWith(armored, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(armored, END_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(armored, TestData.END_PGP_PRIVATE_KEY_BLOCK);
// assertAsciiArmorEquals(carolKey, armored); // assertAsciiArmorEquals(carolKey, armored);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void dearmorArmorCarolCert(SOP sop) throws IOException { 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);
@ -145,20 +140,20 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
.data(carolCert) .data(carolCert)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_PUBLIC_KEY_BLOCK)); Assertions.assertFalse(JUtils.arrayStartsWith(dearmored, TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK));
byte[] armored = sop.armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
assertArrayStartsWith(armored, BEGIN_PGP_PUBLIC_KEY_BLOCK); JUtils.assertArrayStartsWith(armored, TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(armored, END_PGP_PUBLIC_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(armored, TestData.END_PGP_PUBLIC_KEY_BLOCK);
// assertAsciiArmorEquals(carolCert, armored); // assertAsciiArmorEquals(carolCert, armored);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void dearmorArmorMessage(SOP sop) throws IOException { public void dearmorArmorMessage(SOP sop) throws IOException {
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" + byte[] message = ("-----BEGIN PGP MESSAGE-----\n" +
"\n" + "\n" +
@ -172,20 +167,20 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
.data(message) .data(message)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_MESSAGE)); Assertions.assertFalse(JUtils.arrayStartsWith(dearmored, TestData.BEGIN_PGP_MESSAGE));
byte[] armored = sop.armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
assertArrayStartsWith(armored, BEGIN_PGP_MESSAGE); JUtils.assertArrayStartsWith(armored, TestData.BEGIN_PGP_MESSAGE);
assertArrayEndsWithIgnoreNewlines(armored, END_PGP_MESSAGE); JUtils.assertArrayEndsWithIgnoreNewlines(armored, TestData.END_PGP_MESSAGE);
// assertAsciiArmorEquals(message, armored); // assertAsciiArmorEquals(message, armored);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void dearmorArmorSignature(SOP sop) throws IOException { public void dearmorArmorSignature(SOP sop) throws IOException {
byte[] signature = ("-----BEGIN PGP SIGNATURE-----\n" + byte[] signature = ("-----BEGIN PGP SIGNATURE-----\n" +
"\n" + "\n" +
@ -200,20 +195,20 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
.data(signature) .data(signature)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(dearmored, BEGIN_PGP_SIGNATURE)); Assertions.assertFalse(JUtils.arrayStartsWith(dearmored, TestData.BEGIN_PGP_SIGNATURE));
byte[] armored = sop.armor() byte[] armored = sop.armor()
.data(dearmored) .data(dearmored)
.getBytes(); .getBytes();
assertArrayStartsWith(armored, BEGIN_PGP_SIGNATURE); JUtils.assertArrayStartsWith(armored, TestData.BEGIN_PGP_SIGNATURE);
assertArrayEndsWithIgnoreNewlines(armored, END_PGP_SIGNATURE); JUtils.assertArrayEndsWithIgnoreNewlines(armored, TestData.END_PGP_SIGNATURE);
assertAsciiArmorEquals(signature, armored); JUtils.assertAsciiArmorEquals(signature, armored);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void testDearmoringTwiceIsIdempotent(SOP sop) throws IOException { public void testDearmoringTwiceIsIdempotent(SOP sop) throws IOException {
byte[] dearmored = sop.dearmor() byte[] dearmored = sop.dearmor()
.data(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .data(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
@ -227,7 +222,7 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void testArmoringTwiceIsIdempotent(SOP sop) throws IOException { public void testArmoringTwiceIsIdempotent(SOP sop) throws IOException {
byte[] armored = ("-----BEGIN PGP SIGNATURE-----\n" + byte[] armored = ("-----BEGIN PGP SIGNATURE-----\n" +
"\n" + "\n" +
@ -242,7 +237,7 @@ public class ExternalArmorDearmorRoundTripTest extends AbstractExternalSOPTest {
.data(armored) .data(armored)
.getBytes(); .getBytes();
assertAsciiArmorEquals(armored, armoredAgain); JUtils.assertAsciiArmorEquals(armored, armoredAgain);
} }
} }

View file

@ -2,26 +2,27 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.external; package sop.testsuite.operation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import sop.ByteArrayAndResult; import sop.ByteArrayAndResult;
import sop.DecryptionResult; import sop.DecryptionResult;
import sop.SOP; import sop.SOP;
import sop.SessionKey; import sop.SessionKey;
import sop.testsuite.TestData;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static sop.testing.TestData.ALICE_KEY;
import static sop.testing.TestData.PLAINTEXT;
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends") @EnabledIf("sop.operation.AbstractSOPTest#hasBackends")
public class ExternalDecryptWithSessionKeyTest extends AbstractExternalSOPTest { public class DecryptWithSessionKeyTest extends AbstractSOPTest {
private static final String CIPHERTEXT = "-----BEGIN PGP MESSAGE-----\n" + private static final String CIPHERTEXT = "-----BEGIN PGP MESSAGE-----\n" +
"\n" + "\n" +
@ -33,21 +34,25 @@ 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";
static Stream<Arguments> provideInstances() {
return provideBackends();
}
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void testDecryptAndExtractSessionKey(SOP sop) throws IOException { public void testDecryptAndExtractSessionKey(SOP sop) throws IOException {
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(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();
assertEquals(SESSION_KEY, bytesAndResult.getResult().getSessionKey().get().toString()); assertEquals(SESSION_KEY, bytesAndResult.getResult().getSessionKey().get().toString());
assertArrayEquals(PLAINTEXT.getBytes(StandardCharsets.UTF_8), bytesAndResult.getBytes()); Assertions.assertArrayEquals(TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8), bytesAndResult.getBytes());
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void testDecryptWithSessionKey(SOP sop) throws IOException { public void testDecryptWithSessionKey(SOP sop) throws IOException {
byte[] decrypted = sop.decrypt() byte[] decrypted = sop.decrypt()
.withSessionKey(SessionKey.fromString(SESSION_KEY)) .withSessionKey(SessionKey.fromString(SESSION_KEY))
@ -55,6 +60,6 @@ public class ExternalDecryptWithSessionKeyTest extends AbstractExternalSOPTest {
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
assertArrayEquals(PLAINTEXT.getBytes(StandardCharsets.UTF_8), decrypted); Assertions.assertArrayEquals(TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8), decrypted);
} }
} }

View file

@ -0,0 +1,250 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
package sop.testsuite.operation;
import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP;
import sop.Verification;
import sop.enums.SignAs;
import sop.exception.SOPGPException;
import sop.testsuite.JUtils;
import sop.testsuite.TestData;
import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.util.Date;
import java.util.List;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows;
@EnabledIf("sop.operation.AbstractSOPTest#hasBackends")
public class DetachedSignDetachedVerifyTest extends AbstractSOPTest {
static Stream<Arguments> provideInstances() {
return provideBackends();
}
@ParameterizedTest
@MethodSource("provideInstances")
public void signVerifyWithAliceKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message)
.toByteArrayAndResult()
.getBytes();
List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
JUtils.assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
}
@ParameterizedTest
@MethodSource("provideInstances")
public void signVerifyTextModeWithAliceKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.mode(SignAs.Text)
.data(message)
.toByteArrayAndResult()
.getBytes();
List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
JUtils.assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
}
@ParameterizedTest
@MethodSource("provideInstances")
public void verifyKnownMessageWithAliceCert(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
JUtils.assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT, TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE);
}
@ParameterizedTest
@MethodSource("provideInstances")
public void signVerifyWithBobKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.data(message)
.toByteArrayAndResult()
.getBytes();
List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
JUtils.assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT);
}
@ParameterizedTest
@MethodSource("provideInstances")
public void signVerifyWithCarolKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.data(message)
.toByteArrayAndResult()
.getBytes();
List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
JUtils.assertSignedBy(verificationList, TestData.CAROL_SIGNING_FINGERPRINT, TestData.CAROL_PRIMARY_FINGERPRINT);
}
@ParameterizedTest
@MethodSource("provideInstances")
public void signVerifyWithEncryptedKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.withKeyPassword(TestData.PASSWORD)
.data(message)
.toByteArrayAndResult()
.getBytes();
JUtils.assertArrayStartsWith(signature, TestData.BEGIN_PGP_SIGNATURE);
List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message);
assertFalse(verificationList.isEmpty());
}
@ParameterizedTest
@MethodSource("provideInstances")
public void signArmorVerifyWithBobKey(SOP sop) throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.detachedSign()
.key(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.noArmor()
.data(message)
.toByteArrayAndResult()
.getBytes();
byte[] armored = sop.armor()
.data(signature)
.getBytes();
List<Verification> verificationList = sop.detachedVerify()
.cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(armored)
.data(message);
assertFalse(verificationList.isEmpty());
JUtils.assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT);
}
@ParameterizedTest
@MethodSource("provideInstances")
public void verifyNotAfterThrowsNoSignature(SOP sop) {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
Date beforeSignature = new Date(TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE.getTime() - 1000); // 1 sec before sig
assertThrows(SOPGPException.NoSignature.class, () -> sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.notAfter(beforeSignature)
.signatures(signature)
.data(message));
}
@ParameterizedTest
@MethodSource("provideInstances")
public void verifyNotBeforeThrowsNoSignature(SOP sop) {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8);
Date afterSignature = new Date(TestData.ALICE_DETACHED_SIGNED_MESSAGE_DATE.getTime() + 1000); // 1 sec after sig
assertThrows(SOPGPException.NoSignature.class, () -> sop.detachedVerify()
.cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.notBefore(afterSignature)
.signatures(signature)
.data(message));
}
@ParameterizedTest
@MethodSource("provideInstances")
public void signVerifyWithEncryptedKeyWithoutPassphraseFails(SOP sop) {
assertThrows(SOPGPException.KeyIsProtected.class, () ->
sop.detachedSign()
.key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.data(TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8))
.toByteArrayAndResult()
.getBytes());
}
@ParameterizedTest
@MethodSource("provideInstances")
public void signWithProtectedKeyAndMultiplePassphrasesTest(SOP sop)
throws IOException {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] signature = sop.sign()
.key(TestData.PASSWORD_PROTECTED_KEY.getBytes(StandardCharsets.UTF_8))
.withKeyPassword("wrong")
.withKeyPassword(TestData.PASSWORD) // correct
.withKeyPassword("wrong2")
.data(message)
.toByteArrayAndResult()
.getBytes();
assertFalse(sop.verify()
.cert(TestData.PASSWORD_PROTECTED_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signature)
.data(message)
.isEmpty());
}
@ParameterizedTest
@MethodSource("provideInstances")
public void verifyMissingCertCausesMissingArg(SOP sop) {
byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
assertThrows(SOPGPException.MissingArg.class, () ->
sop.verify()
.signatures(TestData.ALICE_DETACHED_SIGNED_MESSAGE.getBytes(StandardCharsets.UTF_8))
.data(message));
}
}

View file

@ -2,10 +2,11 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.external; package sop.testsuite.operation;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import sop.ByteArrayAndResult; import sop.ByteArrayAndResult;
import sop.DecryptionResult; import sop.DecryptionResult;
@ -13,36 +14,33 @@ 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;
import sop.testsuite.JUtils;
import sop.testsuite.TestData;
import sop.util.UTCUtil; import sop.util.UTCUtil;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Stream;
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;
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.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static sop.testing.JUtils.assertSignedBy;
import static sop.testing.TestData.ALICE_CERT;
import static sop.testing.TestData.ALICE_KEY;
import static sop.testing.TestData.ALICE_PRIMARY_FINGERPRINT;
import static sop.testing.TestData.ALICE_SIGNING_FINGERPRINT;
import static sop.testing.TestData.BOB_CERT;
import static sop.testing.TestData.BOB_KEY;
import static sop.testing.TestData.CAROL_CERT;
import static sop.testing.TestData.CAROL_KEY;
import static sop.testing.TestData.PLAINTEXT;
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends") @EnabledIf("sop.operation.AbstractSOPTest#hasBackends")
public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest { public class EncryptDecryptTest extends AbstractSOPTest {
static Stream<Arguments> provideInstances() {
return provideBackends();
}
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void encryptDecryptRoundTripPasswordTest(SOP sop) throws IOException { public void encryptDecryptRoundTripPasswordTest(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = sop.encrypt() byte[] ciphertext = sop.encrypt()
.withPassword("sw0rdf1sh") .withPassword("sw0rdf1sh")
.plaintext(message) .plaintext(message)
@ -58,16 +56,16 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void encryptDecryptRoundTripAliceTest(SOP sop) throws IOException { public void encryptDecryptRoundTripAliceTest(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = sop.encrypt() byte[] ciphertext = sop.encrypt()
.withCert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -79,16 +77,16 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void encryptDecryptRoundTripBobTest(SOP sop) throws IOException { public void encryptDecryptRoundTripBobTest(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = sop.encrypt() byte[] ciphertext = sop.encrypt()
.withCert(BOB_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
byte[] plaintext = sop.decrypt() byte[] plaintext = sop.decrypt()
.withKey(BOB_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.BOB_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
@ -97,16 +95,16 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void encryptDecryptRoundTripCarolTest(SOP sop) throws IOException { public void encryptDecryptRoundTripCarolTest(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = sop.encrypt() byte[] ciphertext = sop.encrypt()
.withCert(CAROL_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
byte[] plaintext = sop.decrypt() byte[] plaintext = sop.decrypt()
.withKey(CAROL_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.CAROL_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult() .toByteArrayAndResult()
.getBytes(); .getBytes();
@ -115,11 +113,11 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void encryptNoArmorThenArmorThenDecryptRoundTrip(SOP sop) throws IOException { public void encryptNoArmorThenArmorThenDecryptRoundTrip(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = sop.encrypt() byte[] ciphertext = sop.encrypt()
.withCert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.noArmor() .noArmor()
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
@ -129,7 +127,7 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.ciphertext(armored) .ciphertext(armored)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -138,18 +136,18 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void encryptSignDecryptVerifyRoundTripAliceTest(SOP sop) throws IOException { public void encryptSignDecryptVerifyRoundTripAliceTest(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = sop.encrypt() byte[] ciphertext = sop.encrypt()
.withCert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signWith(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .signWith(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.plaintext(message) .plaintext(message)
.getBytes(); .getBytes();
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -160,23 +158,23 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertNotNull(result.getSessionKey().get()); assertNotNull(result.getSessionKey().get());
List<Verification> verificationList = result.getVerifications(); List<Verification> verificationList = result.getVerifications();
assertEquals(1, verificationList.size()); assertEquals(1, verificationList.size());
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT); JUtils.assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void encryptSignAsTextDecryptVerifyRoundTripAliceTest(SOP sop) throws IOException { public void encryptSignAsTextDecryptVerifyRoundTripAliceTest(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] ciphertext = sop.encrypt() byte[] ciphertext = sop.encrypt()
.withCert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .withCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signWith(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 = sop.decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.ciphertext(ciphertext) .ciphertext(ciphertext)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -187,11 +185,11 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertNotNull(result.getSessionKey().get()); assertNotNull(result.getSessionKey().get());
List<Verification> verificationList = result.getVerifications(); List<Verification> verificationList = result.getVerifications();
assertEquals(1, verificationList.size()); assertEquals(1, verificationList.size());
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT); JUtils.assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void encryptSignDecryptVerifyRoundTripWithFreshEncryptedKeyTest(SOP sop) throws IOException { public void encryptSignDecryptVerifyRoundTripWithFreshEncryptedKeyTest(SOP sop) throws IOException {
byte[] keyPassword = "sw0rdf1sh".getBytes(StandardCharsets.UTF_8); byte[] keyPassword = "sw0rdf1sh".getBytes(StandardCharsets.UTF_8);
byte[] key = sop.generateKey() byte[] key = sop.generateKey()
@ -223,7 +221,7 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void decryptVerifyNotAfterTest(SOP sop) { public void decryptVerifyNotAfterTest(SOP sop) {
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" + byte[] message = ("-----BEGIN PGP MESSAGE-----\n" +
"\n" + "\n" +
@ -243,8 +241,8 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertThrows(SOPGPException.NoSignature.class, () -> { assertThrows(SOPGPException.NoSignature.class, () -> {
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.verifyNotAfter(beforeSignature) .verifyNotAfter(beforeSignature)
.ciphertext(message) .ciphertext(message)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -256,7 +254,7 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void decryptVerifyNotBeforeTest(SOP sop) { public void decryptVerifyNotBeforeTest(SOP sop) {
byte[] message = ("-----BEGIN PGP MESSAGE-----\n" + byte[] message = ("-----BEGIN PGP MESSAGE-----\n" +
"\n" + "\n" +
@ -276,8 +274,8 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
assertThrows(SOPGPException.NoSignature.class, () -> { assertThrows(SOPGPException.NoSignature.class, () -> {
ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt() ByteArrayAndResult<DecryptionResult> bytesAndResult = sop.decrypt()
.withKey(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .withKey(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.verifyWithCert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .verifyWithCert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.verifyNotBefore(afterSignature) .verifyNotBefore(afterSignature)
.ciphertext(message) .ciphertext(message)
.toByteArrayAndResult(); .toByteArrayAndResult();
@ -289,9 +287,9 @@ public class ExternalEncryptDecryptRoundTripTest extends AbstractExternalSOPTest
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void missingArgsTest(SOP sop) { public void missingArgsTest(SOP sop) {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
assertThrows(SOPGPException.MissingArg.class, () -> sop.encrypt() assertThrows(SOPGPException.MissingArg.class, () -> sop.encrypt()
.plaintext(message) .plaintext(message)

View file

@ -2,31 +2,31 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.external; package sop.testsuite.operation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP; import sop.SOP;
import sop.testing.TestData; import sop.testsuite.JUtils;
import sop.testsuite.TestData;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream; import java.io.InputStream;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertFalse; @EnabledIf("sop.operation.AbstractSOPTest#hasBackends")
import static sop.testing.JUtils.arrayStartsWith; public class ExtractCertTest extends AbstractSOPTest {
import static sop.testing.JUtils.assertArrayEndsWithIgnoreNewlines;
import static sop.testing.JUtils.assertArrayStartsWith;
import static sop.testing.JUtils.assertAsciiArmorEquals;
import static sop.testing.TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK;
import static sop.testing.TestData.END_PGP_PUBLIC_KEY_BLOCK;
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends") static Stream<Arguments> provideInstances() {
public class ExternalExtractCertTest extends AbstractExternalSOPTest { return provideBackends();
}
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void extractArmoredCertFromArmoredKeyTest(SOP sop) throws IOException { public void extractArmoredCertFromArmoredKeyTest(SOP sop) throws IOException {
InputStream keyIn = sop.generateKey() InputStream keyIn = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
@ -34,39 +34,39 @@ public class ExternalExtractCertTest extends AbstractExternalSOPTest {
.getInputStream(); .getInputStream();
byte[] cert = sop.extractCert().key(keyIn).getBytes(); byte[] cert = sop.extractCert().key(keyIn).getBytes();
assertArrayStartsWith(cert, BEGIN_PGP_PUBLIC_KEY_BLOCK); JUtils.assertArrayStartsWith(cert, TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(cert, END_PGP_PUBLIC_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(cert, TestData.END_PGP_PUBLIC_KEY_BLOCK);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void extractAliceCertFromAliceKeyTest(SOP sop) throws IOException { public void extractAliceCertFromAliceKeyTest(SOP sop) throws IOException {
byte[] armoredCert = sop.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); JUtils.assertAsciiArmorEquals(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8), armoredCert);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void extractBobsCertFromBobsKeyTest(SOP sop) throws IOException { public void extractBobsCertFromBobsKeyTest(SOP sop) throws IOException {
byte[] armoredCert = sop.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); JUtils.assertAsciiArmorEquals(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8), armoredCert);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void extractCarolsCertFromCarolsKeyTest(SOP sop) throws IOException { public void extractCarolsCertFromCarolsKeyTest(SOP sop) throws IOException {
byte[] armoredCert = sop.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); JUtils.assertAsciiArmorEquals(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8), armoredCert);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void extractUnarmoredCertFromArmoredKeyTest(SOP sop) throws IOException { public void extractUnarmoredCertFromArmoredKeyTest(SOP sop) throws IOException {
InputStream keyIn = sop.generateKey() InputStream keyIn = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
@ -78,11 +78,11 @@ public class ExternalExtractCertTest extends AbstractExternalSOPTest {
.key(keyIn) .key(keyIn)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(cert, BEGIN_PGP_PUBLIC_KEY_BLOCK)); Assertions.assertFalse(JUtils.arrayStartsWith(cert, TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK));
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void extractArmoredCertFromUnarmoredKeyTest(SOP sop) throws IOException { public void extractArmoredCertFromUnarmoredKeyTest(SOP sop) throws IOException {
InputStream keyIn = sop.generateKey() InputStream keyIn = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
@ -94,12 +94,12 @@ public class ExternalExtractCertTest extends AbstractExternalSOPTest {
.key(keyIn) .key(keyIn)
.getBytes(); .getBytes();
assertArrayStartsWith(cert, BEGIN_PGP_PUBLIC_KEY_BLOCK); JUtils.assertArrayStartsWith(cert, TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(cert, END_PGP_PUBLIC_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(cert, TestData.END_PGP_PUBLIC_KEY_BLOCK);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void extractUnarmoredCertFromUnarmoredKeyTest(SOP sop) throws IOException { public void extractUnarmoredCertFromUnarmoredKeyTest(SOP sop) throws IOException {
InputStream keyIn = sop.generateKey() InputStream keyIn = sop.generateKey()
.noArmor() .noArmor()
@ -112,6 +112,6 @@ public class ExternalExtractCertTest extends AbstractExternalSOPTest {
.key(keyIn) .key(keyIn)
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(cert, BEGIN_PGP_PUBLIC_KEY_BLOCK)); Assertions.assertFalse(JUtils.arrayStartsWith(cert, TestData.BEGIN_PGP_PUBLIC_KEY_BLOCK));
} }
} }

View file

@ -2,39 +2,41 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.external; package sop.testsuite.operation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP; import sop.SOP;
import sop.testing.JUtils; import sop.testsuite.JUtils;
import sop.testsuite.TestData;
import java.io.IOException; import java.io.IOException;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertFalse; @EnabledIf("sop.operation.AbstractSOPTest#hasBackends")
import static sop.testing.JUtils.assertArrayEndsWithIgnoreNewlines; public class GenerateKeyTest extends AbstractSOPTest {
import static sop.testing.JUtils.assertArrayStartsWith;
import static sop.testing.TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK;
import static sop.testing.TestData.END_PGP_PRIVATE_KEY_BLOCK;
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends") static Stream<Arguments> provideInstances() {
public class ExternalGenerateKeyTest extends AbstractExternalSOPTest { return provideBackends();
}
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void generateKeyTest(SOP sop) throws IOException { public void generateKeyTest(SOP sop) throws IOException {
byte[] key = sop.generateKey() byte[] key = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
.generate() .generate()
.getBytes(); .getBytes();
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayStartsWith(key, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(key, END_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(key, TestData.END_PGP_PRIVATE_KEY_BLOCK);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void generateKeyNoArmor(SOP sop) throws IOException { public void generateKeyNoArmor(SOP sop) throws IOException {
byte[] key = sop.generateKey() byte[] key = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
@ -42,11 +44,11 @@ public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
.generate() .generate()
.getBytes(); .getBytes();
assertFalse(JUtils.arrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK)); Assertions.assertFalse(JUtils.arrayStartsWith(key, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK));
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void generateKeyWithMultipleUserIdsTest(SOP sop) throws IOException { public void generateKeyWithMultipleUserIdsTest(SOP sop) throws IOException {
byte[] key = sop.generateKey() byte[] key = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
@ -54,23 +56,23 @@ public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
.generate() .generate()
.getBytes(); .getBytes();
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayStartsWith(key, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(key, END_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(key, TestData.END_PGP_PRIVATE_KEY_BLOCK);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void generateKeyWithoutUserIdTest(SOP sop) throws IOException { public void generateKeyWithoutUserIdTest(SOP sop) throws IOException {
byte[] key = sop.generateKey() byte[] key = sop.generateKey()
.generate() .generate()
.getBytes(); .getBytes();
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayStartsWith(key, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(key, END_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(key, TestData.END_PGP_PRIVATE_KEY_BLOCK);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void generateKeyWithPasswordTest(SOP sop) throws IOException { public void generateKeyWithPasswordTest(SOP sop) throws IOException {
byte[] key = sop.generateKey() byte[] key = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
@ -78,12 +80,12 @@ public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
.generate() .generate()
.getBytes(); .getBytes();
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayStartsWith(key, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(key, END_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(key, TestData.END_PGP_PRIVATE_KEY_BLOCK);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void generateKeyWithMultipleUserIdsAndPassword(SOP sop) throws IOException { public void generateKeyWithMultipleUserIdsAndPassword(SOP sop) throws IOException {
byte[] key = sop.generateKey() byte[] key = sop.generateKey()
.userId("Alice <alice@openpgp.org>") .userId("Alice <alice@openpgp.org>")
@ -92,7 +94,7 @@ public class ExternalGenerateKeyTest extends AbstractExternalSOPTest {
.generate() .generate()
.getBytes(); .getBytes();
assertArrayStartsWith(key, BEGIN_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayStartsWith(key, TestData.BEGIN_PGP_PRIVATE_KEY_BLOCK);
assertArrayEndsWithIgnoreNewlines(key, END_PGP_PRIVATE_KEY_BLOCK); JUtils.assertArrayEndsWithIgnoreNewlines(key, TestData.END_PGP_PRIVATE_KEY_BLOCK);
} }
} }

View file

@ -2,39 +2,42 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.external; package sop.testsuite.operation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import sop.ByteArrayAndResult; import sop.ByteArrayAndResult;
import sop.SOP; import sop.SOP;
import sop.Signatures; import sop.Signatures;
import sop.Verification; import sop.Verification;
import sop.testsuite.JUtils;
import sop.testsuite.TestData;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.List; import java.util.List;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse; import static org.junit.jupiter.api.Assertions.assertFalse;
import static sop.testing.JUtils.arrayStartsWith;
import static sop.testing.JUtils.assertArrayStartsWith;
import static sop.testing.TestData.ALICE_CERT;
import static sop.testing.TestData.ALICE_KEY;
import static sop.testing.TestData.BEGIN_PGP_SIGNATURE;
import static sop.testing.TestData.PLAINTEXT;
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends") @EnabledIf("sop.operation.AbstractSOPTest#hasBackends")
public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExternalSOPTest { public class InlineSignInlineDetachDetachedVerifyTest extends AbstractSOPTest {
static Stream<Arguments> provideInstances() {
return provideBackends();
}
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void inlineSignThenDetachThenDetachedVerifyTest(SOP sop) throws IOException { public void inlineSignThenDetachThenDetachedVerifyTest(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = sop.inlineSign() byte[] inlineSigned = sop.inlineSign()
.key(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.getBytes(); .getBytes();
@ -49,7 +52,7 @@ public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExterna
.getBytes(); .getBytes();
List<Verification> verifications = sop.detachedVerify() List<Verification> verifications = sop.detachedVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(signatures) .signatures(signatures)
.data(plaintext); .data(plaintext);
@ -57,12 +60,12 @@ public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExterna
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void inlineSignThenDetachNoArmorThenArmorThenDetachedVerifyTest(SOP sop) throws IOException { 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 = sop.inlineSign() byte[] inlineSigned = sop.inlineSign()
.key(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.getBytes(); .getBytes();
@ -76,15 +79,15 @@ public class ExternalInlineSignDetachVerifyRoundTripTest extends AbstractExterna
byte[] signatures = bytesAndResult.getResult() byte[] signatures = bytesAndResult.getResult()
.getBytes(); .getBytes();
assertFalse(arrayStartsWith(signatures, BEGIN_PGP_SIGNATURE)); Assertions.assertFalse(JUtils.arrayStartsWith(signatures, TestData.BEGIN_PGP_SIGNATURE));
byte[] armored = sop.armor() byte[] armored = sop.armor()
.data(signatures) .data(signatures)
.getBytes(); .getBytes();
assertArrayStartsWith(armored, BEGIN_PGP_SIGNATURE); JUtils.assertArrayStartsWith(armored, TestData.BEGIN_PGP_SIGNATURE);
List<Verification> verifications = sop.detachedVerify() List<Verification> verifications = sop.detachedVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.signatures(armored) .signatures(armored)
.data(plaintext); .data(plaintext);

View file

@ -2,123 +2,121 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.external; package sop.testsuite.operation;
import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import sop.ByteArrayAndResult; import sop.ByteArrayAndResult;
import sop.SOP; 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;
import sop.testing.JUtils; import sop.testsuite.JUtils;
import sop.testing.TestData; import sop.testsuite.TestData;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.Date; import java.util.Date;
import java.util.List; import java.util.List;
import java.util.stream.Stream;
import static org.junit.jupiter.api.Assertions.assertArrayEquals; import static org.junit.jupiter.api.Assertions.assertArrayEquals;
import static org.junit.jupiter.api.Assertions.assertFalse;
import static org.junit.jupiter.api.Assertions.assertThrows; import static org.junit.jupiter.api.Assertions.assertThrows;
import static sop.testing.JUtils.assertSignedBy;
import static sop.testing.TestData.ALICE_CERT;
import static sop.testing.TestData.ALICE_KEY;
import static sop.testing.TestData.ALICE_PRIMARY_FINGERPRINT;
import static sop.testing.TestData.ALICE_SIGNING_FINGERPRINT;
import static sop.testing.TestData.BEGIN_PGP_MESSAGE;
import static sop.testing.TestData.BEGIN_PGP_SIGNED_MESSAGE;
import static sop.testing.TestData.PLAINTEXT;
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends") @EnabledIf("sop.operation.AbstractSOPTest#hasBackends")
public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest { public class InlineSignInlineVerifyTest extends AbstractSOPTest {
static Stream<Arguments> provideInstances() {
return provideBackends();
}
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void inlineSignVerifyAlice(SOP sop) throws IOException { public void inlineSignVerifyAlice(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = sop.inlineSign() byte[] inlineSigned = sop.inlineSign()
.key(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); JUtils.assertArrayStartsWith(inlineSigned, TestData.BEGIN_PGP_MESSAGE);
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned) .data(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
assertArrayEquals(message, bytesAndResult.getBytes()); assertArrayEquals(message, bytesAndResult.getBytes());
List<Verification> verificationList = bytesAndResult.getResult(); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT); JUtils.assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void inlineSignVerifyAliceNoArmor(SOP sop) throws IOException { public void inlineSignVerifyAliceNoArmor(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = sop.inlineSign() byte[] inlineSigned = sop.inlineSign()
.key(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.noArmor() .noArmor()
.data(message) .data(message)
.getBytes(); .getBytes();
assertFalse(JUtils.arrayStartsWith(inlineSigned, BEGIN_PGP_MESSAGE)); Assertions.assertFalse(JUtils.arrayStartsWith(inlineSigned, TestData.BEGIN_PGP_MESSAGE));
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(inlineSigned) .data(inlineSigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
assertArrayEquals(message, bytesAndResult.getBytes()); assertArrayEquals(message, bytesAndResult.getBytes());
List<Verification> verificationList = bytesAndResult.getResult(); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT); JUtils.assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void clearsignVerifyAlice(SOP sop) throws IOException { public void clearsignVerifyAlice(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] clearsigned = sop.inlineSign() byte[] clearsigned = sop.inlineSign()
.key(ALICE_KEY.getBytes(StandardCharsets.UTF_8)) .key(TestData.ALICE_KEY.getBytes(StandardCharsets.UTF_8))
.mode(InlineSignAs.clearsigned) .mode(InlineSignAs.clearsigned)
.data(message) .data(message)
.getBytes(); .getBytes();
JUtils.assertArrayStartsWith(clearsigned, BEGIN_PGP_SIGNED_MESSAGE); JUtils.assertArrayStartsWith(clearsigned, TestData.BEGIN_PGP_SIGNED_MESSAGE);
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(clearsigned) .data(clearsigned)
.toByteArrayAndResult(); .toByteArrayAndResult();
assertArrayEquals(message, bytesAndResult.getBytes()); assertArrayEquals(message, bytesAndResult.getBytes());
List<Verification> verificationList = bytesAndResult.getResult(); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT); JUtils.assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void inlineVerifyCompareSignatureDate(SOP sop) throws IOException { public void inlineVerifyCompareSignatureDate(SOP sop) throws IOException {
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 = sop.inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult(); .toByteArrayAndResult();
List<Verification> verificationList = bytesAndResult.getResult(); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, ALICE_SIGNING_FINGERPRINT, ALICE_PRIMARY_FINGERPRINT, signatureDate); JUtils.assertSignedBy(verificationList, TestData.ALICE_SIGNING_FINGERPRINT, TestData.ALICE_PRIMARY_FINGERPRINT, signatureDate);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void assertNotBeforeThrowsNoSignature(SOP sop) { public void assertNotBeforeThrowsNoSignature(SOP sop) {
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;
@ -126,13 +124,13 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertThrows(SOPGPException.NoSignature.class, () -> sop.inlineVerify() assertThrows(SOPGPException.NoSignature.class, () -> sop.inlineVerify()
.notBefore(afterSignature) .notBefore(afterSignature)
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult()); .toByteArrayAndResult());
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void assertNotAfterThrowsNoSignature(SOP sop) { public void assertNotAfterThrowsNoSignature(SOP sop) {
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;
@ -140,22 +138,22 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertThrows(SOPGPException.NoSignature.class, () -> sop.inlineVerify() assertThrows(SOPGPException.NoSignature.class, () -> sop.inlineVerify()
.notAfter(beforeSignature) .notAfter(beforeSignature)
.cert(ALICE_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.ALICE_CERT.getBytes(StandardCharsets.UTF_8))
.data(message) .data(message)
.toByteArrayAndResult()); .toByteArrayAndResult());
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void inlineSignVerifyBob(SOP sop) throws IOException { public void inlineSignVerifyBob(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = sop.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); JUtils.assertArrayStartsWith(inlineSigned, TestData.BEGIN_PGP_MESSAGE);
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.BOB_CERT.getBytes(StandardCharsets.UTF_8))
@ -164,20 +162,20 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertArrayEquals(message, bytesAndResult.getBytes()); assertArrayEquals(message, bytesAndResult.getBytes());
List<Verification> verificationList = bytesAndResult.getResult(); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT); JUtils.assertSignedBy(verificationList, TestData.BOB_SIGNING_FINGERPRINT, TestData.BOB_PRIMARY_FINGERPRINT);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void inlineSignVerifyCarol(SOP sop) throws IOException { public void inlineSignVerifyCarol(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = sop.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); JUtils.assertArrayStartsWith(inlineSigned, TestData.BEGIN_PGP_MESSAGE);
ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify() ByteArrayAndResult<List<Verification>> bytesAndResult = sop.inlineVerify()
.cert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8)) .cert(TestData.CAROL_CERT.getBytes(StandardCharsets.UTF_8))
@ -186,13 +184,13 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
assertArrayEquals(message, bytesAndResult.getBytes()); assertArrayEquals(message, bytesAndResult.getBytes());
List<Verification> verificationList = bytesAndResult.getResult(); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, TestData.CAROL_SIGNING_FINGERPRINT, TestData.CAROL_PRIMARY_FINGERPRINT); JUtils.assertSignedBy(verificationList, TestData.CAROL_SIGNING_FINGERPRINT, TestData.CAROL_PRIMARY_FINGERPRINT);
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void inlineSignVerifyProtectedKey(SOP sop) throws IOException { public void inlineSignVerifyProtectedKey(SOP sop) throws IOException {
byte[] message = PLAINTEXT.getBytes(StandardCharsets.UTF_8); byte[] message = TestData.PLAINTEXT.getBytes(StandardCharsets.UTF_8);
byte[] inlineSigned = sop.inlineSign() byte[] inlineSigned = sop.inlineSign()
.withKeyPassword(TestData.PASSWORD) .withKeyPassword(TestData.PASSWORD)
@ -207,7 +205,7 @@ public class ExternalInlineSignVerifyTest extends AbstractExternalSOPTest {
.toByteArrayAndResult(); .toByteArrayAndResult();
List<Verification> verificationList = bytesAndResult.getResult(); List<Verification> verificationList = bytesAndResult.getResult();
assertSignedBy(verificationList, TestData.PASSWORD_PROTECTED_SIGNING_FINGERPRINT, TestData.PASSWORD_PROTECTED_PRIMARY_FINGERPRINT); JUtils.assertSignedBy(verificationList, TestData.PASSWORD_PROTECTED_SIGNING_FINGERPRINT, TestData.PASSWORD_PROTECTED_PRIMARY_FINGERPRINT);
} }
} }

View file

@ -2,22 +2,28 @@
// //
// SPDX-License-Identifier: Apache-2.0 // SPDX-License-Identifier: Apache-2.0
package sop.external; package sop.testsuite.operation;
import org.junit.jupiter.api.condition.EnabledIf; import org.junit.jupiter.api.condition.EnabledIf;
import org.junit.jupiter.params.ParameterizedTest; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.Arguments;
import org.junit.jupiter.params.provider.MethodSource; import org.junit.jupiter.params.provider.MethodSource;
import sop.SOP; import sop.SOP;
import java.util.stream.Stream;
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;
@EnabledIf("sop.external.AbstractExternalSOPTest#hasBackends") @EnabledIf("sop.operation.AbstractSOPTest#hasBackends")
public class ExternalVersionTest extends AbstractExternalSOPTest { public class VersionTest extends AbstractSOPTest {
static Stream<Arguments> provideInstances() {
return provideBackends();
}
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void versionNameTest(SOP sop) { public void versionNameTest(SOP sop) {
String name = sop.version().getName(); String name = sop.version().getName();
assertNotNull(name); assertNotNull(name);
@ -25,21 +31,21 @@ public class ExternalVersionTest extends AbstractExternalSOPTest {
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void versionVersionTest(SOP sop) { public void versionVersionTest(SOP sop) {
String version = sop.version().getVersion(); String version = sop.version().getVersion();
assertTrue(version.matches("\\d+(\\.\\d+)*\\S*")); assertFalse(version.isEmpty());
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void backendVersionTest(SOP sop) { public void backendVersionTest(SOP sop) {
String backend = sop.version().getBackendVersion(); String backend = sop.version().getBackendVersion();
assertFalse(backend.isEmpty()); assertFalse(backend.isEmpty());
} }
@ParameterizedTest @ParameterizedTest
@MethodSource("sop.external.AbstractExternalSOPTest#provideBackends") @MethodSource("provideInstances")
public void extendedVersionTest(SOP sop) { public void extendedVersionTest(SOP sop) {
String extended = sop.version().getExtendedVersion(); String extended = sop.version().getExtendedVersion();
assertFalse(extended.isEmpty()); assertFalse(extended.isEmpty());

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* SOP binary test suite.
*/
package sop.testsuite.operation;

View file

@ -0,0 +1,8 @@
// SPDX-FileCopyrightText: 2023 Paul Schaub <vanitasvitae@fsfe.org>
//
// SPDX-License-Identifier: Apache-2.0
/**
* SOP binary test suite.
*/
package sop.testsuite;