mirror of
https://codeberg.org/PGPainless/cert-d-java.git
synced 2025-09-10 03:39:40 +02:00
Throw NoSuchElementException for non-existent certificates/keys
Fixes #2
This commit is contained in:
parent
eab31b8c12
commit
a248e0d717
4 changed files with 75 additions and 10 deletions
|
@ -31,6 +31,7 @@ import java.nio.file.Files;
|
|||
import java.util.Arrays;
|
||||
import java.util.HashSet;
|
||||
import java.util.Iterator;
|
||||
import java.util.NoSuchElementException;
|
||||
import java.util.Set;
|
||||
import java.util.stream.Stream;
|
||||
|
||||
|
@ -68,6 +69,55 @@ public class PGPCertificateDirectoryTest {
|
|||
Arguments.of(Named.of("FileBasedCertificateDirectory", fileBased)));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestSubjects")
|
||||
public void getNonExistentCertByFingerprintThrowsNoSuchElementException(PGPCertificateDirectory directory) {
|
||||
assertThrows(NoSuchElementException.class, () ->
|
||||
directory.getByFingerprint("0000000000000000000000000000000000000000"));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestSubjects")
|
||||
public void getNonExistentCertByFingerprintIfChangedThrowsNoSuchElementException(PGPCertificateDirectory directory) {
|
||||
assertThrows(NoSuchElementException.class, () ->
|
||||
directory.getByFingerprintIfChanged("0000000000000000000000000000000000000000", 12));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestSubjects")
|
||||
public void getNonExistentCertBySpecialNameThrowsNoSuchElementException(PGPCertificateDirectory directory) {
|
||||
assertThrows(NoSuchElementException.class, () ->
|
||||
directory.getBySpecialName(SpecialNames.TRUST_ROOT));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestSubjects")
|
||||
public void getNonExistentCertBySpecialNameIfChangedThrowsNoSuchElementException(PGPCertificateDirectory directory) {
|
||||
assertThrows(NoSuchElementException.class, () ->
|
||||
directory.getBySpecialNameIfChanged(SpecialNames.TRUST_ROOT, 12));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestSubjects")
|
||||
public void getNonExistentTrustRootThrowsNoSuchElementException(PGPCertificateDirectory directory) {
|
||||
assertThrows(NoSuchElementException.class, () ->
|
||||
directory.getTrustRoot());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestSubjects")
|
||||
public void getNonExistentTrustRootIfChangedThrowsNoSuchElementException(PGPCertificateDirectory directory) {
|
||||
assertThrows(NoSuchElementException.class, () ->
|
||||
directory.getTrustRootCertificateIfChanged(12));
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestSubjects")
|
||||
public void getNonExistentTrustRootCertificateThrowsNoSuchElementException(PGPCertificateDirectory directory) {
|
||||
assertThrows(NoSuchElementException.class, () ->
|
||||
directory.getTrustRootCertificate());
|
||||
}
|
||||
|
||||
@ParameterizedTest
|
||||
@MethodSource("provideTestSubjects")
|
||||
public void lockDirectoryAndTryInsertWillFail(PGPCertificateDirectory directory)
|
||||
|
@ -130,7 +180,7 @@ public class PGPCertificateDirectoryTest {
|
|||
@MethodSource("provideTestSubjects")
|
||||
public void testInsertAndGetSingleCert(PGPCertificateDirectory directory)
|
||||
throws BadDataException, IOException, InterruptedException, BadNameException {
|
||||
assertNull(directory.getByFingerprint(CEDRIC_FP), "Empty directory MUST NOT contain certificate");
|
||||
assertThrows(NoSuchElementException.class, () -> directory.getByFingerprint(CEDRIC_FP), "Empty directory MUST NOT contain certificate");
|
||||
|
||||
Certificate certificate = directory.insert(TestKeys.getCedricCert(), merger);
|
||||
assertEquals(CEDRIC_FP, certificate.getFingerprint(), "Fingerprint of inserted cert MUST match");
|
||||
|
@ -148,7 +198,7 @@ public class PGPCertificateDirectoryTest {
|
|||
@MethodSource("provideTestSubjects")
|
||||
public void testInsertAndGetTrustRootAndCert(PGPCertificateDirectory directory)
|
||||
throws BadDataException, IOException, InterruptedException {
|
||||
assertNull(directory.getTrustRoot());
|
||||
assertThrows(NoSuchElementException.class, () -> directory.getTrustRoot());
|
||||
|
||||
KeyMaterial trustRootMaterial = directory.insertTrustRoot(
|
||||
TestKeys.getHarryKey(), merger);
|
||||
|
@ -188,6 +238,7 @@ public class PGPCertificateDirectoryTest {
|
|||
assertNotNull(directory.getTrustRootCertificateIfChanged(tag + 1));
|
||||
|
||||
Long oldTag = tag;
|
||||
Thread.sleep(10);
|
||||
// "update" key
|
||||
trustRootMaterial = directory.insertTrustRoot(
|
||||
TestKeys.getHarryKey(), merger);
|
||||
|
@ -241,10 +292,12 @@ public class PGPCertificateDirectoryTest {
|
|||
|
||||
Long oldTag = tag;
|
||||
|
||||
Thread.sleep(10);
|
||||
// Change the file on disk directly, this invalidates the tag due to changed modification date
|
||||
File certFile = resolver.getCertFileByFingerprint(certificate.getFingerprint());
|
||||
FileOutputStream fileOut = new FileOutputStream(certFile);
|
||||
Streams.pipeAll(certificate.getInputStream(), fileOut);
|
||||
fileOut.write("\n".getBytes());
|
||||
fileOut.close();
|
||||
|
||||
// Old invalidated tag indicates a change, so the modified certificate is returned
|
||||
|
|
|
@ -17,6 +17,7 @@ import pgp.certificate_store.exception.BadNameException;
|
|||
import java.io.IOException;
|
||||
import java.util.Iterator;
|
||||
import java.util.List;
|
||||
import java.util.NoSuchElementException;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||
|
@ -52,7 +53,7 @@ public class PGPCertificateStoreAdapterTest {
|
|||
@Test
|
||||
public void testInsertGetCertificate()
|
||||
throws BadDataException, IOException, InterruptedException, BadNameException {
|
||||
assertNull(adapter.getCertificate(TestKeys.CEDRIC_FP));
|
||||
assertThrows(NoSuchElementException.class, () -> adapter.getCertificate(TestKeys.CEDRIC_FP));
|
||||
assertFalse(adapter.getCertificates().hasNext());
|
||||
|
||||
Certificate certificate = adapter.insertCertificate(TestKeys.getCedricCert(), merger);
|
||||
|
@ -70,7 +71,7 @@ public class PGPCertificateStoreAdapterTest {
|
|||
@Test
|
||||
public void testInsertGetTrustRoot()
|
||||
throws BadDataException, BadNameException, IOException, InterruptedException {
|
||||
assertNull(adapter.getCertificate(SpecialNames.TRUST_ROOT));
|
||||
assertThrows(NoSuchElementException.class, () -> adapter.getCertificate(SpecialNames.TRUST_ROOT));
|
||||
|
||||
Certificate certificate = adapter.insertCertificateBySpecialName(
|
||||
SpecialNames.TRUST_ROOT, TestKeys.getHarryKey(), merger);
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue