mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-12-16 17:21:08 +01:00
Move Shared PGP Certificate Directory Exceptions to pgp certificate store
This commit is contained in:
parent
0c5cad677c
commit
983f39c56f
17 changed files with 88 additions and 197 deletions
|
|
@ -10,11 +10,11 @@ import java.util.Iterator;
|
|||
|
||||
import pgp.cert_d.SharedPGPCertificateDirectory;
|
||||
import pgp.cert_d.SpecialNames;
|
||||
import pgp.cert_d.exception.BadDataException;
|
||||
import pgp.cert_d.exception.BadNameException;
|
||||
import pgp.certificate_store.Certificate;
|
||||
import pgp.certificate_store.CertificateStore;
|
||||
import pgp.certificate_store.MergeCallback;
|
||||
import pgp.certificate_store.exception.BadDataException;
|
||||
import pgp.certificate_store.exception.BadNameException;
|
||||
|
||||
/**
|
||||
* Adapter class used to adapt the {@link SharedPGPCertificateDirectory} for use with
|
||||
|
|
@ -36,98 +36,50 @@ public class SharedPGPCertificateDirectoryAdapter
|
|||
|
||||
@Override
|
||||
public Certificate getCertificate(String identifier)
|
||||
throws IOException {
|
||||
throws IOException, BadDataException, BadNameException {
|
||||
String specialName = SpecialNames.lookupSpecialName(identifier);
|
||||
if (specialName != null) {
|
||||
try {
|
||||
return directory.getBySpecialName(specialName);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Unknown special name " + identifier, e);
|
||||
}
|
||||
return directory.getBySpecialName(specialName);
|
||||
}
|
||||
|
||||
try {
|
||||
return directory.getByFingerprint(identifier);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Invalid fingerprint " + identifier, e);
|
||||
} catch (BadDataException e) {
|
||||
throw new IOException("Bad data.", e);
|
||||
}
|
||||
return directory.getByFingerprint(identifier);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate getCertificateIfChanged(String identifier, String tag)
|
||||
throws IOException {
|
||||
throws IOException, BadDataException, BadNameException {
|
||||
String specialName = SpecialNames.lookupSpecialName(identifier);
|
||||
if (specialName != null) {
|
||||
try {
|
||||
return directory.getBySpecialNameIfChanged(specialName, tag);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Unknown special name " + identifier, e);
|
||||
}
|
||||
return directory.getBySpecialNameIfChanged(specialName, tag);
|
||||
}
|
||||
|
||||
try {
|
||||
return directory.getByFingerprintIfChanged(identifier, tag);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Invalid fingerprint " + identifier, e);
|
||||
} catch (BadDataException e) {
|
||||
throw new IOException("Bad data.", e);
|
||||
}
|
||||
return directory.getByFingerprintIfChanged(identifier, tag);
|
||||
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate insertCertificate(InputStream data, MergeCallback merge)
|
||||
throws IOException, InterruptedException {
|
||||
try {
|
||||
return directory.insert(data, merge);
|
||||
} catch (BadDataException e) {
|
||||
throw new IOException("Cannot insert certificate due to bad data", e);
|
||||
}
|
||||
throws IOException, InterruptedException, BadDataException {
|
||||
return directory.insert(data, merge);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate tryInsertCertificate(InputStream data, MergeCallback merge)
|
||||
throws IOException {
|
||||
try {
|
||||
return directory.tryInsert(data, merge);
|
||||
} catch (BadDataException e) {
|
||||
throw new IOException("Cannot insert certificate due to bad data", e);
|
||||
}
|
||||
throws IOException, BadDataException {
|
||||
return directory.tryInsert(data, merge);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate insertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
|
||||
throws IOException, InterruptedException {
|
||||
try {
|
||||
String specialNameValidated = SpecialNames.lookupSpecialName(specialName);
|
||||
if (specialNameValidated == null) {
|
||||
throw new IllegalArgumentException("Unknown special name " + specialName);
|
||||
}
|
||||
|
||||
return directory.insertWithSpecialName(specialNameValidated, data, merge);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Unknown special name " + specialName);
|
||||
} catch (BadDataException e) {
|
||||
throw new IOException("Cannot insert certificate due to bad data", e);
|
||||
}
|
||||
throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||
return directory.insertWithSpecialName(specialName, data, merge);
|
||||
}
|
||||
|
||||
@Override
|
||||
public Certificate tryInsertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
|
||||
throws IOException {
|
||||
try {
|
||||
String specialNameValidated = SpecialNames.lookupSpecialName(specialName);
|
||||
if (specialNameValidated == null) {
|
||||
throw new IllegalArgumentException("Unknown special name " + specialName);
|
||||
}
|
||||
|
||||
return directory.tryInsertWithSpecialName(specialNameValidated, data, merge);
|
||||
} catch (BadNameException e) {
|
||||
throw new IllegalArgumentException("Unknown special name " + specialName);
|
||||
} catch (BadDataException e) {
|
||||
throw new IOException("Cannot insert certificate due to bad data", e);
|
||||
}
|
||||
throws IOException, BadDataException, BadNameException {
|
||||
return directory.tryInsertWithSpecialName(specialName, data, merge);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
|
|
|||
|
|
@ -1,91 +0,0 @@
|
|||
// SPDX-FileCopyrightText: 2022 Paul Schaub <vanitasvitae@fsfe.org>
|
||||
//
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
|
||||
package org.pgpainless.cert_d;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||
import static org.mockito.ArgumentMatchers.any;
|
||||
import static org.mockito.ArgumentMatchers.eq;
|
||||
import static org.mockito.Mockito.mock;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
|
||||
import org.junit.jupiter.api.BeforeAll;
|
||||
import org.junit.jupiter.api.Test;
|
||||
import org.pgpainless.certificate_store.SharedPGPCertificateDirectoryAdapter;
|
||||
import pgp.cert_d.SharedPGPCertificateDirectory;
|
||||
import pgp.cert_d.exception.BadDataException;
|
||||
import pgp.cert_d.exception.BadNameException;
|
||||
import pgp.certificate_store.CertificateStore;
|
||||
import pgp.certificate_store.MergeCallback;
|
||||
|
||||
public class SharedPGPCertificateDirectoryAdapterMockTest {
|
||||
|
||||
private static final String invalidSpecialName = "trust-root";
|
||||
private static final String invalidFingerprint = "invalidFingerprint";
|
||||
private static final String badData = "badData";
|
||||
|
||||
private static CertificateStore store;
|
||||
private static MergeCallback mergeCallback;
|
||||
private static InputStream inputStream;
|
||||
|
||||
@BeforeAll
|
||||
public static void mockComponents() throws BadNameException, IOException, BadDataException, InterruptedException {
|
||||
mergeCallback = mock(MergeCallback.class);
|
||||
inputStream = mock(InputStream.class);
|
||||
SharedPGPCertificateDirectory mocked = mock(SharedPGPCertificateDirectory.class);
|
||||
store = new SharedPGPCertificateDirectoryAdapter(mocked);
|
||||
// bad name
|
||||
when(mocked.getBySpecialName(invalidSpecialName))
|
||||
.thenThrow(new BadNameException());
|
||||
when(mocked.getBySpecialNameIfChanged(eq(invalidSpecialName), any()))
|
||||
.thenThrow(new BadNameException());
|
||||
when(mocked.getByFingerprint(invalidFingerprint))
|
||||
.thenThrow(new BadNameException());
|
||||
when(mocked.getByFingerprintIfChanged(eq(invalidFingerprint), any()))
|
||||
.thenThrow(new BadNameException());
|
||||
// bad data
|
||||
when(mocked.getByFingerprint(badData))
|
||||
.thenThrow(new BadDataException());
|
||||
when(mocked.getByFingerprintIfChanged(eq(badData), any()))
|
||||
.thenThrow(new BadDataException());
|
||||
when(mocked.insert(any(), any()))
|
||||
.thenThrow(new BadDataException());
|
||||
when(mocked.tryInsert(any(), any()))
|
||||
.thenThrow(new BadDataException());
|
||||
when(mocked.insertWithSpecialName(eq(invalidSpecialName), any(), any()))
|
||||
.thenThrow(new BadDataException());
|
||||
when(mocked.tryInsertWithSpecialName(eq(invalidSpecialName), any(), any()))
|
||||
.thenThrow(new BadDataException());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUsingFingerprint_BadNameIsMappedToIAE() {
|
||||
assertThrows(IllegalArgumentException.class, () -> store.getCertificate(invalidFingerprint));
|
||||
assertThrows(IllegalArgumentException.class, () -> store.getCertificateIfChanged(invalidFingerprint, "tag"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGetUsingSpecialName_BadNameIsMappedToIAE() {
|
||||
assertThrows(IllegalArgumentException.class, () -> store.getCertificate(invalidSpecialName));
|
||||
assertThrows(IllegalArgumentException.class, () -> store.getCertificateIfChanged(invalidSpecialName, "tag"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testGet_BadDataIsMappedToIOE() {
|
||||
assertThrows(IOException.class, () -> store.getCertificate(badData));
|
||||
assertThrows(IOException.class, () -> store.getCertificateIfChanged(badData, "tag"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testInsert_BadDataIsMappedToIOE() {
|
||||
assertThrows(IOException.class, () -> store.insertCertificate(inputStream, mergeCallback));
|
||||
assertThrows(IOException.class, () -> store.insertCertificateBySpecialName(invalidSpecialName, inputStream, mergeCallback));
|
||||
|
||||
assertThrows(IOException.class, () -> store.tryInsertCertificate(inputStream, mergeCallback));
|
||||
assertThrows(IOException.class, () -> store.tryInsertCertificateBySpecialName(invalidSpecialName, inputStream, mergeCallback));
|
||||
}
|
||||
}
|
||||
|
|
@ -27,7 +27,9 @@ import org.junit.jupiter.api.Test;
|
|||
import org.pgpainless.certificate_store.CertificateReader;
|
||||
import org.pgpainless.certificate_store.SharedPGPCertificateDirectoryAdapter;
|
||||
import pgp.cert_d.SharedPGPCertificateDirectoryImpl;
|
||||
import pgp.cert_d.exception.NotAStoreException;
|
||||
import pgp.certificate_store.exception.BadDataException;
|
||||
import pgp.certificate_store.exception.BadNameException;
|
||||
import pgp.certificate_store.exception.NotAStoreException;
|
||||
import pgp.certificate_store.Certificate;
|
||||
import pgp.certificate_store.CertificateStore;
|
||||
|
||||
|
|
@ -53,18 +55,18 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void getNonExistentCertIsNull() throws IOException {
|
||||
public void getNonExistentCertIsNull() throws IOException, BadDataException, BadNameException {
|
||||
assertNull(store.getCertificate("trust-root"));
|
||||
assertNull(store.getCertificate("eb85bb5fa33a75e15e944e63f231550c4f47e38e"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void getInvalidIdentifierThrows() {
|
||||
assertThrows(IllegalArgumentException.class, () -> store.getCertificate("invalid"));
|
||||
assertThrows(BadNameException.class, () -> store.getCertificate("invalid"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void insertAndGet() throws IOException, InterruptedException {
|
||||
public void insertAndGet() throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||
byte[] bytes = Hex.decode(testCertificate);
|
||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||
String fingerprint = testCertFingerprint;
|
||||
|
|
@ -83,7 +85,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
|||
|
||||
|
||||
@Test
|
||||
public void tryInsertAndGet() throws IOException {
|
||||
public void tryInsertAndGet() throws IOException, BadDataException, BadNameException {
|
||||
byte[] bytes = Hex.decode(testCertificate);
|
||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||
String fingerprint = testCertFingerprint;
|
||||
|
|
@ -102,7 +104,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
|||
|
||||
|
||||
@Test
|
||||
public void insertAndGetIfChanged() throws IOException, InterruptedException {
|
||||
public void insertAndGetIfChanged() throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||
byte[] bytes = Hex.decode(testCertificate);
|
||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||
String fingerprint = testCertFingerprint;
|
||||
|
|
@ -115,7 +117,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void insertBySpecialNameAndGet() throws IOException, InterruptedException {
|
||||
public void insertBySpecialNameAndGet() throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||
byte[] bytes = Hex.decode(testCertificate);
|
||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||
String fingerprint = testCertFingerprint;
|
||||
|
|
@ -134,7 +136,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void tryInsertBySpecialNameAndGet() throws IOException {
|
||||
public void tryInsertBySpecialNameAndGet() throws IOException, BadDataException, BadNameException {
|
||||
byte[] bytes = Hex.decode(testCertificate);
|
||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||
String fingerprint = testCertFingerprint;
|
||||
|
|
@ -153,7 +155,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void insertBySpecialNameAndGetIfChanged() throws IOException, InterruptedException {
|
||||
public void insertBySpecialNameAndGetIfChanged() throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||
byte[] bytes = Hex.decode(testCertificate);
|
||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||
String fingerprint = testCertFingerprint;
|
||||
|
|
@ -170,7 +172,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
|||
}
|
||||
|
||||
@Test
|
||||
public void getItemsAndFingerprints() throws IOException, InterruptedException {
|
||||
public void getItemsAndFingerprints() throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||
byte[] bytes1 = Hex.decode(testCertificate);
|
||||
ByteArrayInputStream byteIn1 = new ByteArrayInputStream(bytes1);
|
||||
Certificate firstCert = store.insertCertificate(byteIn1, (data, existing) -> data);
|
||||
|
|
|
|||
|
|
@ -41,9 +41,9 @@ import pgp.cert_d.CachingSharedPGPCertificateDirectoryWrapper;
|
|||
import pgp.cert_d.FileLockingMechanism;
|
||||
import pgp.cert_d.SharedPGPCertificateDirectory;
|
||||
import pgp.cert_d.SharedPGPCertificateDirectoryImpl;
|
||||
import pgp.cert_d.exception.BadDataException;
|
||||
import pgp.cert_d.exception.BadNameException;
|
||||
import pgp.cert_d.exception.NotAStoreException;
|
||||
import pgp.certificate_store.exception.BadDataException;
|
||||
import pgp.certificate_store.exception.BadNameException;
|
||||
import pgp.certificate_store.exception.NotAStoreException;
|
||||
import pgp.certificate_store.Certificate;
|
||||
import pgp.certificate_store.MergeCallback;
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue