Throw NoSuchElementException for non-existent certificates/keys

Fixes #2
This commit is contained in:
Paul Schaub 2022-08-27 12:16:53 +02:00
parent eab31b8c12
commit a248e0d717
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 75 additions and 10 deletions

View file

@ -15,6 +15,8 @@ import java.io.IOException;
import java.io.InputStream;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.Objects;
import java.util.Set;
import java.util.regex.Pattern;
@ -46,13 +48,17 @@ public class PGPCertificateDirectory
if (!openPgpV4FingerprintPattern.matcher(fingerprint).matches()) {
throw new BadNameException();
}
return backend.readByFingerprint(fingerprint);
Certificate certificate = backend.readByFingerprint(fingerprint);
if (certificate == null) {
throw new NoSuchElementException();
}
return certificate;
}
@Override
public Certificate getByFingerprintIfChanged(String fingerprint, long tag)
throws IOException, BadNameException, BadDataException {
if (tag != backend.getTagForFingerprint(fingerprint)) {
if (!Objects.equals(tag, backend.getTagForFingerprint(fingerprint))) {
return getByFingerprint(fingerprint);
}
return null;
@ -66,13 +72,13 @@ public class PGPCertificateDirectory
if (keyMaterial != null) {
return keyMaterial.asCertificate();
}
return null;
throw new NoSuchElementException();
}
@Override
public Certificate getBySpecialNameIfChanged(String specialName, long tag)
throws IOException, BadNameException, BadDataException {
if (tag != backend.getTagForSpecialName(specialName)) {
if (!Objects.equals(tag, backend.getTagForSpecialName(specialName))) {
return getBySpecialName(specialName);
}
return null;
@ -121,7 +127,11 @@ public class PGPCertificateDirectory
@Override
public KeyMaterial getTrustRoot() throws IOException, BadDataException {
try {
return backend.readBySpecialName(SpecialNames.TRUST_ROOT);
KeyMaterial keyMaterial = backend.readBySpecialName(SpecialNames.TRUST_ROOT);
if (keyMaterial == null) {
throw new NoSuchElementException();
}
return keyMaterial;
} catch (BadNameException e) {
throw new AssertionError("'" + SpecialNames.TRUST_ROOT + "' is implementation MUST");
}

View file

@ -33,6 +33,7 @@ import java.util.ArrayList;
import java.util.Collections;
import java.util.Iterator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.regex.Pattern;
/**
@ -348,7 +349,7 @@ public class FileBasedCertificateDirectoryBackend implements PGPCertificateDirec
private Long getTag(File file) throws IOException {
if (!file.exists()) {
throw new IllegalArgumentException("File MUST exist.");
throw new NoSuchElementException();
}
Path path = file.toPath();
BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);