Increase coverage of PGPCertificateDirectory

This commit is contained in:
Paul Schaub 2022-08-25 11:26:12 +02:00
parent f34c6d7735
commit 4ce9f46846
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
2 changed files with 39 additions and 1 deletions

View file

@ -21,6 +21,10 @@ import java.util.Iterator;
import java.util.Map; import java.util.Map;
import java.util.concurrent.atomic.AtomicLong; import java.util.concurrent.atomic.AtomicLong;
/**
* Implementation of the {@link PGPCertificateDirectory.Backend} which stores key material in-memory.
* It uses object locking with {@link #wait()} and {@link #notify()} to synchronize write-access.
*/
public class InMemoryCertificateDirectoryBackend implements PGPCertificateDirectory.Backend { public class InMemoryCertificateDirectoryBackend implements PGPCertificateDirectory.Backend {
protected static class ObjectLockingMechanism implements PGPCertificateDirectory.LockingMechanism { protected static class ObjectLockingMechanism implements PGPCertificateDirectory.LockingMechanism {

View file

@ -70,7 +70,7 @@ public class PGPCertificateDirectoryTest {
@ParameterizedTest @ParameterizedTest
@MethodSource("provideTestSubjects") @MethodSource("provideTestSubjects")
public void lockDirectoryAndInsertWillFail(PGPCertificateDirectory directory) public void lockDirectoryAndTryInsertWillFail(PGPCertificateDirectory directory)
throws IOException, InterruptedException, BadDataException { throws IOException, InterruptedException, BadDataException {
// Manually lock the dir // Manually lock the dir
assertFalse(directory.backend.getLock().isLocked()); assertFalse(directory.backend.getLock().isLocked());
@ -86,6 +86,40 @@ public class PGPCertificateDirectoryTest {
assertNotNull(inserted); assertNotNull(inserted);
} }
@ParameterizedTest
@MethodSource("provideTestSubjects")
public void lockDirectoryAndTryInsertTrustRootWillFail(PGPCertificateDirectory directory)
throws IOException, InterruptedException, BadDataException {
// Manually lock the dir
assertFalse(directory.backend.getLock().isLocked());
directory.backend.getLock().lockDirectory();
assertTrue(directory.backend.getLock().isLocked());
KeyMaterial inserted = directory.tryInsertTrustRoot(TestKeys.getHarryKey(), merger);
assertNull(inserted);
directory.backend.getLock().releaseDirectory();
inserted = directory.tryInsertTrustRoot(TestKeys.getHarryKey(), merger);
assertNotNull(inserted);
}
@ParameterizedTest
@MethodSource("provideTestSubjects")
public void lockDirectoryAndTryInsertWithSpecialNameWillFail(PGPCertificateDirectory directory)
throws IOException, InterruptedException, BadDataException, BadNameException {
// Manually lock the dir
assertFalse(directory.backend.getLock().isLocked());
directory.backend.getLock().lockDirectory();
assertTrue(directory.backend.getLock().isLocked());
Certificate inserted = directory.tryInsertWithSpecialName(SpecialNames.TRUST_ROOT, TestKeys.getHarryKey(), merger);
assertNull(inserted);
directory.backend.getLock().releaseDirectory();
inserted = directory.tryInsertWithSpecialName(SpecialNames.TRUST_ROOT, TestKeys.getHarryKey(), merger);
assertNotNull(inserted);
}
@ParameterizedTest @ParameterizedTest
@MethodSource("provideTestSubjects") @MethodSource("provideTestSubjects")
public void getByInvalidNameFails(PGPCertificateDirectory directory) { public void getByInvalidNameFails(PGPCertificateDirectory directory) {