Execute tests on both inMemory and fileBased store backends

This commit is contained in:
Paul Schaub 2022-08-23 15:36:26 +02:00
parent 5e850581c0
commit d050cb5516
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311

View file

@ -5,18 +5,27 @@
package pgp.cert_d; package pgp.cert_d;
import org.bouncycastle.util.io.Streams; import org.bouncycastle.util.io.Streams;
import org.junit.jupiter.api.Test; import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.MethodSource;
import pgp.cert_d.subkey_lookup.InMemorySubkeyLookup;
import pgp.certificate_store.certificate.Certificate; import pgp.certificate_store.certificate.Certificate;
import pgp.certificate_store.certificate.Key; import pgp.certificate_store.certificate.Key;
import pgp.certificate_store.certificate.KeyMaterial; import pgp.certificate_store.certificate.KeyMaterial;
import pgp.certificate_store.exception.BadDataException; import pgp.certificate_store.exception.BadDataException;
import pgp.certificate_store.exception.BadNameException; import pgp.certificate_store.exception.BadNameException;
import pgp.certificate_store.exception.NotAStoreException;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException; import java.io.IOException;
import java.nio.charset.Charset; import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.Arrays;
import java.util.HashSet;
import java.util.Iterator; import java.util.Iterator;
import java.util.Set;
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;
@ -134,11 +143,23 @@ public class PGPCertificateDirectoryTest {
"-----END PGP PUBLIC KEY BLOCK-----\n"; "-----END PGP PUBLIC KEY BLOCK-----\n";
private static final String CEDRIC_FP = "5e75bf20646bc1a98d3b1bc2fe9cd472987c4021"; private static final String CEDRIC_FP = "5e75bf20646bc1a98d3b1bc2fe9cd472987c4021";
@Test private static Stream<PGPCertificateDirectory> provideTestSubjects() throws IOException, NotAStoreException {
public void lockDirectoryAndInsertWillFail() throws IOException, InterruptedException, BadDataException { PGPCertificateDirectory inMemory = PGPCertificateDirectories.inMemoryCertificateDirectory(
PGPCertificateDirectory directory = PGPCertificateDirectories.inMemoryCertificateDirectory(
new TestKeyMaterialReaderBackend()); new TestKeyMaterialReaderBackend());
File tempDir = Files.createTempDirectory("pgp-cert-d-test").toFile();
tempDir.deleteOnExit();
PGPCertificateDirectory fileBased = PGPCertificateDirectories.fileBasedCertificateDirectory(
new TestKeyMaterialReaderBackend(),
tempDir,
new InMemorySubkeyLookup());
return Stream.of(inMemory, fileBased);
}
@ParameterizedTest
@MethodSource("provideTestSubjects")
public void lockDirectoryAndInsertWillFail(PGPCertificateDirectory directory) throws IOException, InterruptedException, BadDataException {
// Manually lock the dir // Manually lock the dir
assertFalse(directory.backend.getLock().isLocked()); assertFalse(directory.backend.getLock().isLocked());
directory.backend.getLock().lockDirectory(); directory.backend.getLock().lockDirectory();
@ -153,19 +174,16 @@ public class PGPCertificateDirectoryTest {
assertNotNull(inserted); assertNotNull(inserted);
} }
@Test @ParameterizedTest
public void getByInvalidNameFails() { @MethodSource("provideTestSubjects")
PGPCertificateDirectory directory = PGPCertificateDirectories.inMemoryCertificateDirectory( public void getByInvalidNameFails(PGPCertificateDirectory directory) {
new TestKeyMaterialReaderBackend());
assertThrows(BadNameException.class, () -> directory.getBySpecialName("invalid")); assertThrows(BadNameException.class, () -> directory.getBySpecialName("invalid"));
} }
@Test @ParameterizedTest
public void testInsertAndGetSingleCert() throws BadDataException, IOException, InterruptedException, BadNameException { @MethodSource("provideTestSubjects")
PGPCertificateDirectory directory = PGPCertificateDirectories.inMemoryCertificateDirectory( public void testInsertAndGetSingleCert(PGPCertificateDirectory directory)
new TestKeyMaterialReaderBackend()); throws BadDataException, IOException, InterruptedException, BadNameException {
assertNull(directory.getByFingerprint(CEDRIC_FP), "Empty directory MUST NOT contain certificate"); assertNull(directory.getByFingerprint(CEDRIC_FP), "Empty directory MUST NOT contain certificate");
ByteArrayInputStream bytesIn = new ByteArrayInputStream(CEDRIC_CERT.getBytes(UTF8)); ByteArrayInputStream bytesIn = new ByteArrayInputStream(CEDRIC_CERT.getBytes(UTF8));
@ -182,11 +200,10 @@ public class PGPCertificateDirectoryTest {
assertArrayEquals(expected, actual.toByteArray(), "InputStream of cert MUST match what we gave in"); assertArrayEquals(expected, actual.toByteArray(), "InputStream of cert MUST match what we gave in");
} }
@Test @ParameterizedTest
public void testInsertAndGetTrustRootAndCert() throws BadDataException, IOException, InterruptedException { @MethodSource("provideTestSubjects")
PGPCertificateDirectory directory = PGPCertificateDirectories.inMemoryCertificateDirectory( public void testInsertAndGetTrustRootAndCert(PGPCertificateDirectory directory)
new TestKeyMaterialReaderBackend()); throws BadDataException, IOException, InterruptedException {
assertNull(directory.getTrustRoot()); assertNull(directory.getTrustRoot());
KeyMaterial trustRootMaterial = directory.insertTrustRoot( KeyMaterial trustRootMaterial = directory.insertTrustRoot(
@ -203,9 +220,14 @@ public class PGPCertificateDirectoryTest {
directory.tryInsert(new ByteArrayInputStream(RON_CERT.getBytes(UTF8)), new TestKeyMaterialMerger()); directory.tryInsert(new ByteArrayInputStream(RON_CERT.getBytes(UTF8)), new TestKeyMaterialMerger());
directory.insert(new ByteArrayInputStream(CEDRIC_CERT.getBytes(UTF8)), new TestKeyMaterialMerger()); directory.insert(new ByteArrayInputStream(CEDRIC_CERT.getBytes(UTF8)), new TestKeyMaterialMerger());
Set<String> expected = new HashSet<>(Arrays.asList(RON_FP, CEDRIC_FP));
Set<String> actual = new HashSet<>();
Iterator<String> fingerprints = directory.fingerprints(); Iterator<String> fingerprints = directory.fingerprints();
assertEquals(RON_FP, fingerprints.next()); actual.add(fingerprints.next());
assertEquals(CEDRIC_FP, fingerprints.next()); actual.add(fingerprints.next());
assertFalse(fingerprints.hasNext()); assertFalse(fingerprints.hasNext());
assertEquals(expected, actual);
} }
} }