Add some PGPCertificateDirectory tests

This commit is contained in:
Paul Schaub 2022-08-23 15:19:01 +02:00
parent 70367e98f0
commit 5e850581c0
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
8 changed files with 399 additions and 8 deletions

View file

@ -13,23 +13,32 @@ import pgp.certificate_store.exception.NotAStoreException;
import java.io.File;
/**
* Static factory methods that return implementations of the {@link PGPCertificateDirectory} class.
*/
public final class PGPCertificateDirectories {
private PGPCertificateDirectories() {
}
public static PGPCertificateDirectory inMemoryCertificateDirectory(KeyMaterialReaderBackend keyReader) {
return new PGPCertificateDirectory(new InMemoryCertificateDirectoryBackend(keyReader), new InMemorySubkeyLookup());
public static PGPCertificateDirectory inMemoryCertificateDirectory(
KeyMaterialReaderBackend keyReader) {
return new PGPCertificateDirectory(
new InMemoryCertificateDirectoryBackend(keyReader), new InMemorySubkeyLookup());
}
public static PGPCertificateDirectory defaultFileBasedCertificateDirectory(KeyMaterialReaderBackend keyReader, SubkeyLookup subkeyLookup)
public static PGPCertificateDirectory defaultFileBasedCertificateDirectory(
KeyMaterialReaderBackend keyReader,
SubkeyLookup subkeyLookup)
throws NotAStoreException {
return fileBasedCertificateDirectory(keyReader, BaseDirectoryProvider.getDefaultBaseDir(), subkeyLookup);
}
public static PGPCertificateDirectory fileBasedCertificateDirectory(
KeyMaterialReaderBackend keyReader, File baseDirectory, SubkeyLookup subkeyLookup)
KeyMaterialReaderBackend keyReader,
File baseDirectory,
SubkeyLookup subkeyLookup)
throws NotAStoreException {
return new PGPCertificateDirectory(
new FileBasedCertificateDirectoryBackend(baseDirectory, keyReader), subkeyLookup);

View file

@ -20,8 +20,8 @@ import java.util.Set;
public class PGPCertificateDirectory
implements ReadOnlyPGPCertificateDirectory, WritingPGPCertificateDirectory, SubkeyLookup {
private final Backend backend;
private final SubkeyLookup subkeyLookup;
final Backend backend;
final SubkeyLookup subkeyLookup;
public PGPCertificateDirectory(Backend backend, SubkeyLookup subkeyLookup) {
this.backend = backend;

View file

@ -76,7 +76,10 @@ public class InMemoryCertificateDirectoryBackend implements PGPCertificateDirect
@Override
public KeyMaterial readBySpecialName(String specialName) {
public KeyMaterial readBySpecialName(String specialName) throws BadNameException {
if (SpecialNames.lookupSpecialName(specialName) == null) {
throw new BadNameException("Invalid special name " + specialName);
}
return keyMaterialSpecialNameMap.get(specialName);
}
@ -89,7 +92,13 @@ public class InMemoryCertificateDirectoryBackend implements PGPCertificateDirect
public KeyMaterial doInsertTrustRoot(InputStream data, KeyMaterialMerger merge)
throws BadDataException, IOException {
KeyMaterial update = reader.read(data);
KeyMaterial existing = readBySpecialName(SpecialNames.TRUST_ROOT);
KeyMaterial existing = null;
try {
existing = readBySpecialName(SpecialNames.TRUST_ROOT);
} catch (BadNameException e) {
// Does not happen
throw new RuntimeException(e);
}
KeyMaterial merged = merge.merge(update, existing);
keyMaterialSpecialNameMap.put(SpecialNames.TRUST_ROOT, merged);
return merged;