Improve readability of wkd tests

This commit is contained in:
Paul Schaub 2022-03-19 14:08:35 +01:00
parent 661c870d1a
commit a6b52562a8
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
3 changed files with 61 additions and 44 deletions

View file

@ -9,6 +9,7 @@ import org.junit.jupiter.api.DynamicTest;
import org.junit.jupiter.api.TestFactory; import org.junit.jupiter.api.TestFactory;
import pgp.wkd.cli.WKDCLI; import pgp.wkd.cli.WKDCLI;
import pgp.wkd.cli.command.Fetch; import pgp.wkd.cli.command.Fetch;
import pgp.wkd.discovery.DiscoveryMethod;
import pgp.wkd.test_suite.TestCase; import pgp.wkd.test_suite.TestCase;
import pgp.wkd.test_suite.TestSuite; import pgp.wkd.test_suite.TestSuite;
import pgp.wkd.test_suite.TestSuiteGenerator; import pgp.wkd.test_suite.TestSuiteGenerator;
@ -35,7 +36,7 @@ public class TestSuiteTestRunner {
// Generate test certificates inside the temp wkd // Generate test certificates inside the temp wkd
String domain = "example.com"; String domain = "example.com";
TestSuiteGenerator generator = new TestSuiteGenerator(domain); TestSuiteGenerator generator = new TestSuiteGenerator(domain);
suite = generator.generateTestSuiteInDirectory(tempFile, TestSuiteGenerator.Method.direct); suite = generator.generateTestSuiteInDirectory(tempFile, DiscoveryMethod.direct);
// Fetch certificates from a local directory instead of the internetzzz. // Fetch certificates from a local directory instead of the internetzzz.
Fetch.fetcher = new DirectoryBasedCertificateFetcher(tempPath); Fetch.fetcher = new DirectoryBasedCertificateFetcher(tempPath);

View file

@ -10,6 +10,7 @@ import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter; import com.fasterxml.jackson.databind.ObjectWriter;
import com.fasterxml.jackson.databind.json.JsonMapper; import com.fasterxml.jackson.databind.json.JsonMapper;
import com.fasterxml.jackson.dataformat.xml.XmlMapper; import com.fasterxml.jackson.dataformat.xml.XmlMapper;
import pgp.wkd.discovery.DiscoveryMethod;
import picocli.CommandLine; import picocli.CommandLine;
import java.io.File; import java.io.File;
@ -49,7 +50,7 @@ public class Main implements Runnable {
@CommandLine.Option(names = {"--method", "-m"}, @CommandLine.Option(names = {"--method", "-m"},
paramLabel = "{direct|advanced}", paramLabel = "{direct|advanced}",
description = "Method for key discovery. If absent, assume direct.") description = "Method for key discovery. If absent, assume direct.")
private TestSuiteGenerator.Method method = TestSuiteGenerator.Method.direct; private DiscoveryMethod method = DiscoveryMethod.direct;
@CommandLine.Spec // injected by picocli @CommandLine.Spec // injected by picocli
CommandLine.Model.CommandSpec spec; CommandLine.Model.CommandSpec spec;

View file

@ -25,13 +25,10 @@ import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature; import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.PGPainless; import org.pgpainless.PGPainless;
import org.pgpainless.key.protection.SecretKeyRingProtector; import org.pgpainless.key.protection.SecretKeyRingProtector;
import pgp.wkd.discovery.DiscoveryMethod;
public class TestSuiteGenerator { public class TestSuiteGenerator {
public enum Method {
direct,
advanced
}
private final String domain; private final String domain;
@ -39,42 +36,26 @@ public class TestSuiteGenerator {
this.domain = domain; this.domain = domain;
} }
public TestSuite generateTestSuiteInDirectory(File directory, Method method) throws Exception { public TestSuite generateTestSuiteInDirectory(File directory, DiscoveryMethod method) throws Exception {
WkdDirectoryStructure structure = directoryStructureForMethod(directory, method); WkdDirectoryStructure dirs = directoryStructureForMethod(directory, method);
structure.mkdirs(); dirs.mkdirs();
List<TestCase> tests = new ArrayList<>(); List<TestCase> tests = new ArrayList<>();
tests.add(baseCase(structure)); tests.add(test_baseCase(dirs));
tests.add(baseCaseMultipleCertificates(structure)); tests.add(test_baseCaseMultipleCertificates(dirs));
tests.add(wrongUserId(structure)); tests.add(test_wrongUserId(dirs));
tests.add(noUserId(structure)); tests.add(test_noUserId(dirs));
tests.add(unboundUserId(structure)); tests.add(test_unboundUserId(dirs));
tests.addAll(baseCaseMultiUserIds(structure)); tests.addAll(test_baseCaseMultiUserIds(dirs));
tests.add(secretKeyMaterial(structure)); tests.add(test_secretKeyMaterial(dirs));
tests.add(randomBytes(structure)); tests.add(test_randomBytes(dirs));
return new TestSuite("0.1", tests); return new TestSuite("0.1", tests);
} }
private WkdDirectoryStructure directoryStructureForMethod(File directory, Method method) { // TEST CASES
WkdDirectoryStructure structure;
if (method == Method.direct) {
structure = new WkdDirectoryStructure.DirectMethod(directory, domain);
} else if (method == Method.advanced) {
structure = new WkdDirectoryStructure.AdvancedMethod(directory, domain);
} else {
throw new IllegalArgumentException("Invalid value for parameter 'method'.");
}
return structure;
}
private PGPPublicKeyRing certificate(String userId) throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException { private TestCase test_baseCase(WkdDirectoryStructure directoryStructure) throws Exception {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().modernKeyRing(userId, null);
PGPPublicKeyRing publicKeys = PGPainless.extractCertificate(secretKeys);
return publicKeys;
}
private TestCase baseCase(WkdDirectoryStructure directoryStructure) throws Exception {
String lookupMail = "base-case@" + domain; String lookupMail = "base-case@" + domain;
String userId = "WKD-Test Base Case <base-case@" + domain + ">"; String userId = "WKD-Test Base Case <base-case@" + domain + ">";
String description = "Certificate has a single, valid user-id '" + userId + "'"; String description = "Certificate has a single, valid user-id '" + userId + "'";
@ -88,10 +69,10 @@ public class TestSuiteGenerator {
} }
}); });
return TestCase.ok("Base Csae", description, lookupMail, directoryStructure); return TestCase.ok("Base Case", description, lookupMail, directoryStructure);
} }
private List<TestCase> baseCaseMultiUserIds(WkdDirectoryStructure directoryStructure) throws Exception { private List<TestCase> test_baseCaseMultiUserIds(WkdDirectoryStructure directoryStructure) throws Exception {
String primaryLookupMail = "primary-uid@" + domain; String primaryLookupMail = "primary-uid@" + domain;
String secondaryLookupMail = "secondary-uid@" + domain; String secondaryLookupMail = "secondary-uid@" + domain;
String primaryUserId = "WKD-Test Primary User-ID <" + primaryLookupMail + ">"; String primaryUserId = "WKD-Test Primary User-ID <" + primaryLookupMail + ">";
@ -123,7 +104,7 @@ public class TestSuiteGenerator {
); );
} }
private TestCase baseCaseMultipleCertificates(WkdDirectoryStructure directoryStructure) throws Exception { private TestCase test_baseCaseMultipleCertificates(WkdDirectoryStructure directoryStructure) throws Exception {
String title = "Multiple Certificates"; String title = "Multiple Certificates";
String description = "The result contains multiple certificates."; String description = "The result contains multiple certificates.";
String lookupMail = "multiple-certificates@" + domain; String lookupMail = "multiple-certificates@" + domain;
@ -144,7 +125,7 @@ public class TestSuiteGenerator {
return TestCase.ok(title, description, lookupMail, directoryStructure); return TestCase.ok(title, description, lookupMail, directoryStructure);
} }
private TestCase wrongUserId(WkdDirectoryStructure directoryStructure) throws Exception { private TestCase test_wrongUserId(WkdDirectoryStructure directoryStructure) throws Exception {
String lookupMail = "wrong-userid@" + domain; String lookupMail = "wrong-userid@" + domain;
String userId = "WKD-Test Different User-ID <different-userid@" + domain + ">"; String userId = "WKD-Test Different User-ID <different-userid@" + domain + ">";
String description = "Certificate has a single, valid user-id '" + userId + "', but is deposited for mail address '" + lookupMail + "'."; String description = "Certificate has a single, valid user-id '" + userId + "', but is deposited for mail address '" + lookupMail + "'.";
@ -160,7 +141,7 @@ public class TestSuiteGenerator {
return TestCase.fail("Wrong User-ID", description, lookupMail, directoryStructure); return TestCase.fail("Wrong User-ID", description, lookupMail, directoryStructure);
} }
private TestCase unboundUserId(WkdDirectoryStructure directoryStructure) throws Exception { private TestCase test_unboundUserId(WkdDirectoryStructure directoryStructure) throws Exception {
String lookupMail = "unbound-userid@" + domain; String lookupMail = "unbound-userid@" + domain;
String userId = "WKD-Test Unbound User-ID <" + lookupMail + ">"; String userId = "WKD-Test Unbound User-ID <" + lookupMail + ">";
String description = "Certificate has a single User-ID '" + userId + "' without binding signature."; String description = "Certificate has a single User-ID '" + userId + "' without binding signature.";
@ -190,7 +171,7 @@ public class TestSuiteGenerator {
return TestCase.fail("Unbound UserId", description, lookupMail, directoryStructure); return TestCase.fail("Unbound UserId", description, lookupMail, directoryStructure);
} }
private TestCase noUserId(WkdDirectoryStructure directoryStructure) throws Exception { private TestCase test_noUserId(WkdDirectoryStructure directoryStructure) throws Exception {
String lookupMail = "absent-userid@" + domain; String lookupMail = "absent-userid@" + domain;
String description = "Certificate has no user-id, but is deposited for mail address '" + lookupMail + "'."; String description = "Certificate has no user-id, but is deposited for mail address '" + lookupMail + "'.";
// Generate certificate with temp user-id // Generate certificate with temp user-id
@ -218,10 +199,10 @@ public class TestSuiteGenerator {
return TestCase.fail("No User-ID", description, lookupMail, directoryStructure); return TestCase.fail("No User-ID", description, lookupMail, directoryStructure);
} }
private TestCase secretKeyMaterial(WkdDirectoryStructure directoryStructure) throws Exception { private TestCase test_secretKeyMaterial(WkdDirectoryStructure directoryStructure) throws Exception {
String lookupMail = "test-secret-key@" + domain; String lookupMail = "test-secret-key@" + domain;
String description = "Certificate file contains secret key material."; String description = "Certificate file contains secret key material.";
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().modernKeyRing("WKD-Test Secret Key <" + lookupMail + ">", null); PGPSecretKeyRing secretKeys = secretKey("WKD-Test Secret Key <" + lookupMail + ">");
writeDataFor(lookupMail, directoryStructure, new DataSink() { writeDataFor(lookupMail, directoryStructure, new DataSink() {
@Override @Override
@ -233,7 +214,8 @@ public class TestSuiteGenerator {
return TestCase.fail("Secret Key Material", description, lookupMail, directoryStructure); return TestCase.fail("Secret Key Material", description, lookupMail, directoryStructure);
} }
private TestCase randomBytes(WkdDirectoryStructure directoryStructure) throws IOException { private TestCase test_randomBytes(WkdDirectoryStructure directoryStructure)
throws IOException {
String lookupMail = "random-bytes@" + domain; String lookupMail = "random-bytes@" + domain;
String description = "Certificate file contains random bytes."; String description = "Certificate file contains random bytes.";
@ -248,6 +230,31 @@ public class TestSuiteGenerator {
return TestCase.fail("Random Bytes", description, lookupMail, directoryStructure); return TestCase.fail("Random Bytes", description, lookupMail, directoryStructure);
} }
// INTERNAL METHODS
private WkdDirectoryStructure directoryStructureForMethod(File directory, DiscoveryMethod method) {
WkdDirectoryStructure structure;
if (method == DiscoveryMethod.direct) {
structure = new WkdDirectoryStructure.DirectMethod(directory, domain);
} else if (method == DiscoveryMethod.advanced) {
structure = new WkdDirectoryStructure.AdvancedMethod(directory, domain);
} else {
throw new IllegalArgumentException("Invalid value for parameter 'method'.");
}
return structure;
}
private PGPSecretKeyRing secretKey(String userId) throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
PGPSecretKeyRing secretKeys = PGPainless.generateKeyRing().modernKeyRing(userId, null);
return secretKeys;
}
private PGPPublicKeyRing certificate(String userId) throws PGPException, InvalidAlgorithmParameterException, NoSuchAlgorithmException {
PGPSecretKeyRing secretKeys = secretKey(userId);
PGPPublicKeyRing publicKeys = PGPainless.extractCertificate(secretKeys);
return publicKeys;
}
private void writeDataFor(String mailAddress, WkdDirectoryStructure directory, DataSink sink) private void writeDataFor(String mailAddress, WkdDirectoryStructure directory, DataSink sink)
throws IOException { throws IOException {
Path path = directory.getRelativeCertificatePath(mailAddress); Path path = directory.getRelativeCertificatePath(mailAddress);
@ -263,7 +270,15 @@ public class TestSuiteGenerator {
} }
private interface DataSink { private interface DataSink {
/**
* Write data into the {@link OutputStream}.
*
* @param outputStream output stream
* @throws IOException in case of an IO error
*/
void write(OutputStream outputStream) throws IOException; void write(OutputStream outputStream) throws IOException;
} }
} }