mirror of
https://github.com/pgpainless/pgpainless.git
synced 2025-09-15 13:19:38 +02: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
|
@ -4,8 +4,8 @@
|
||||||
|
|
||||||
package pgp.cert_d;
|
package pgp.cert_d;
|
||||||
|
|
||||||
import pgp.cert_d.exception.BadDataException;
|
import pgp.certificate_store.exception.BadDataException;
|
||||||
import pgp.cert_d.exception.BadNameException;
|
import pgp.certificate_store.exception.BadNameException;
|
||||||
import pgp.certificate_store.Certificate;
|
import pgp.certificate_store.Certificate;
|
||||||
import pgp.certificate_store.MergeCallback;
|
import pgp.certificate_store.MergeCallback;
|
||||||
|
|
||||||
|
@ -96,7 +96,7 @@ public class CachingSharedPGPCertificateDirectoryWrapper
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Certificate getBySpecialName(String specialName)
|
public Certificate getBySpecialName(String specialName)
|
||||||
throws IOException, BadNameException {
|
throws IOException, BadNameException, BadDataException {
|
||||||
Certificate certificate = certificateMap.get(specialName);
|
Certificate certificate = certificateMap.get(specialName);
|
||||||
if (certificate == null) {
|
if (certificate == null) {
|
||||||
certificate = underlyingCertificateDirectory.getBySpecialName(specialName);
|
certificate = underlyingCertificateDirectory.getBySpecialName(specialName);
|
||||||
|
@ -119,7 +119,7 @@ public class CachingSharedPGPCertificateDirectoryWrapper
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Certificate getBySpecialNameIfChanged(String specialName, String tag)
|
public Certificate getBySpecialNameIfChanged(String specialName, String tag)
|
||||||
throws IOException, BadNameException {
|
throws IOException, BadNameException, BadDataException {
|
||||||
if (tagChanged(specialName, tag)) {
|
if (tagChanged(specialName, tag)) {
|
||||||
return getBySpecialName(specialName);
|
return getBySpecialName(specialName);
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@
|
||||||
|
|
||||||
package pgp.cert_d;
|
package pgp.cert_d;
|
||||||
|
|
||||||
import pgp.cert_d.exception.BadNameException;
|
import pgp.certificate_store.exception.BadNameException;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.util.regex.Pattern;
|
import java.util.regex.Pattern;
|
||||||
|
|
|
@ -8,8 +8,8 @@ import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
|
||||||
import pgp.cert_d.exception.BadDataException;
|
import pgp.certificate_store.exception.BadDataException;
|
||||||
import pgp.cert_d.exception.BadNameException;
|
import pgp.certificate_store.exception.BadNameException;
|
||||||
import pgp.certificate_store.Certificate;
|
import pgp.certificate_store.Certificate;
|
||||||
import pgp.certificate_store.MergeCallback;
|
import pgp.certificate_store.MergeCallback;
|
||||||
|
|
||||||
|
@ -21,13 +21,13 @@ public interface SharedPGPCertificateDirectory {
|
||||||
throws IOException, BadNameException, BadDataException;
|
throws IOException, BadNameException, BadDataException;
|
||||||
|
|
||||||
Certificate getBySpecialName(String specialName)
|
Certificate getBySpecialName(String specialName)
|
||||||
throws IOException, BadNameException;
|
throws IOException, BadNameException, BadDataException;
|
||||||
|
|
||||||
Certificate getByFingerprintIfChanged(String fingerprint, String tag)
|
Certificate getByFingerprintIfChanged(String fingerprint, String tag)
|
||||||
throws IOException, BadNameException, BadDataException;
|
throws IOException, BadNameException, BadDataException;
|
||||||
|
|
||||||
Certificate getBySpecialNameIfChanged(String specialName, String tag)
|
Certificate getBySpecialNameIfChanged(String specialName, String tag)
|
||||||
throws IOException, BadNameException;
|
throws IOException, BadNameException, BadDataException;
|
||||||
|
|
||||||
Certificate insert(InputStream data, MergeCallback merge)
|
Certificate insert(InputStream data, MergeCallback merge)
|
||||||
throws IOException, BadDataException, InterruptedException;
|
throws IOException, BadDataException, InterruptedException;
|
||||||
|
|
|
@ -16,9 +16,9 @@ import java.util.Collections;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
|
|
||||||
import pgp.cert_d.exception.BadDataException;
|
import pgp.certificate_store.exception.BadDataException;
|
||||||
import pgp.cert_d.exception.BadNameException;
|
import pgp.certificate_store.exception.BadNameException;
|
||||||
import pgp.cert_d.exception.NotAStoreException;
|
import pgp.certificate_store.exception.NotAStoreException;
|
||||||
import pgp.certificate_store.Certificate;
|
import pgp.certificate_store.Certificate;
|
||||||
import pgp.certificate_store.CertificateReaderBackend;
|
import pgp.certificate_store.CertificateReaderBackend;
|
||||||
import pgp.certificate_store.MergeCallback;
|
import pgp.certificate_store.MergeCallback;
|
||||||
|
|
|
@ -6,7 +6,7 @@ package pgp.cert_d;
|
||||||
|
|
||||||
import org.junit.jupiter.api.BeforeEach;
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import pgp.cert_d.exception.BadNameException;
|
import pgp.certificate_store.exception.BadNameException;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
|
|
@ -4,6 +4,9 @@
|
||||||
|
|
||||||
package pgp.certificate_store;
|
package pgp.certificate_store;
|
||||||
|
|
||||||
|
import pgp.certificate_store.exception.BadDataException;
|
||||||
|
import pgp.certificate_store.exception.BadNameException;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.io.InputStream;
|
import java.io.InputStream;
|
||||||
import java.util.Iterator;
|
import java.util.Iterator;
|
||||||
|
@ -28,7 +31,8 @@ public interface CertificateStore {
|
||||||
*
|
*
|
||||||
* @throws IOException in case of an IO-error
|
* @throws IOException in case of an IO-error
|
||||||
*/
|
*/
|
||||||
Certificate getCertificate(String identifier) throws IOException;
|
Certificate getCertificate(String identifier)
|
||||||
|
throws IOException, BadNameException, BadDataException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return the certificate that matches the given identifier, but only iff it changed since the last invocation.
|
* Return the certificate that matches the given identifier, but only iff it changed since the last invocation.
|
||||||
|
@ -41,7 +45,8 @@ public interface CertificateStore {
|
||||||
*
|
*
|
||||||
* @throws IOException in case of an IO-error
|
* @throws IOException in case of an IO-error
|
||||||
*/
|
*/
|
||||||
Certificate getCertificateIfChanged(String identifier, String tag) throws IOException;
|
Certificate getCertificateIfChanged(String identifier, String tag)
|
||||||
|
throws IOException, BadNameException, BadDataException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a certificate into the store.
|
* Insert a certificate into the store.
|
||||||
|
@ -59,7 +64,8 @@ public interface CertificateStore {
|
||||||
* @throws IOException in case of an IO-error
|
* @throws IOException in case of an IO-error
|
||||||
* @throws InterruptedException in case the inserting thread gets interrupted
|
* @throws InterruptedException in case the inserting thread gets interrupted
|
||||||
*/
|
*/
|
||||||
Certificate insertCertificate(InputStream data, MergeCallback merge) throws IOException, InterruptedException;
|
Certificate insertCertificate(InputStream data, MergeCallback merge)
|
||||||
|
throws IOException, InterruptedException, BadDataException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a certificate into the store.
|
* Insert a certificate into the store.
|
||||||
|
@ -78,7 +84,8 @@ public interface CertificateStore {
|
||||||
*
|
*
|
||||||
* @throws IOException in case of an IO-error
|
* @throws IOException in case of an IO-error
|
||||||
*/
|
*/
|
||||||
Certificate tryInsertCertificate(InputStream data, MergeCallback merge) throws IOException;
|
Certificate tryInsertCertificate(InputStream data, MergeCallback merge)
|
||||||
|
throws IOException, BadDataException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a certificate into the store.
|
* Insert a certificate into the store.
|
||||||
|
@ -97,7 +104,8 @@ public interface CertificateStore {
|
||||||
*
|
*
|
||||||
* @throws IOException in case of an IO-error
|
* @throws IOException in case of an IO-error
|
||||||
*/
|
*/
|
||||||
Certificate insertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge) throws IOException, InterruptedException;
|
Certificate insertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
|
||||||
|
throws IOException, InterruptedException, BadDataException, BadNameException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Insert a certificate into the store.
|
* Insert a certificate into the store.
|
||||||
|
@ -118,7 +126,8 @@ public interface CertificateStore {
|
||||||
*
|
*
|
||||||
* @throws IOException in case of an IO-error
|
* @throws IOException in case of an IO-error
|
||||||
*/
|
*/
|
||||||
Certificate tryInsertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge) throws IOException;
|
Certificate tryInsertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
|
||||||
|
throws IOException, BadDataException, BadNameException;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Return an {@link Iterator} containing all certificates in the store.
|
* Return an {@link Iterator} containing all certificates in the store.
|
||||||
|
|
|
@ -2,7 +2,7 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package pgp.cert_d.exception;
|
package pgp.certificate_store.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The data was not a valid OpenPGP cert or key in binary format.
|
* The data was not a valid OpenPGP cert or key in binary format.
|
|
@ -2,11 +2,18 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package pgp.cert_d.exception;
|
package pgp.certificate_store.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Provided name was neither a valid fingerprint, nor a known special name.
|
* Provided name was neither a valid fingerprint, nor a known special name.
|
||||||
*/
|
*/
|
||||||
public class BadNameException extends Exception {
|
public class BadNameException extends Exception {
|
||||||
|
|
||||||
|
public BadNameException() {
|
||||||
|
super();
|
||||||
|
}
|
||||||
|
|
||||||
|
public BadNameException(String message) {
|
||||||
|
super(message);
|
||||||
|
}
|
||||||
}
|
}
|
|
@ -2,7 +2,7 @@
|
||||||
//
|
//
|
||||||
// SPDX-License-Identifier: Apache-2.0
|
// SPDX-License-Identifier: Apache-2.0
|
||||||
|
|
||||||
package pgp.cert_d.exception;
|
package pgp.certificate_store.exception;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* The base dir cannot possibly contain a store.
|
* The base dir cannot possibly contain a store.
|
|
@ -7,4 +7,4 @@
|
||||||
*
|
*
|
||||||
* @see <a href="https://sequoia-pgp.gitlab.io/pgp-cert-d/#name-failure-modes">Failure Modes</a>
|
* @see <a href="https://sequoia-pgp.gitlab.io/pgp-cert-d/#name-failure-modes">Failure Modes</a>
|
||||||
*/
|
*/
|
||||||
package pgp.cert_d.exception;
|
package pgp.certificate_store.exception;
|
|
@ -9,7 +9,7 @@ import org.pgpainless.certificate_store.SharedPGPCertificateDirectoryAdapter;
|
||||||
import pgp.cert_d.SharedPGPCertificateDirectoryImpl;
|
import pgp.cert_d.SharedPGPCertificateDirectoryImpl;
|
||||||
import pgp.cert_d.cli.commands.Get;
|
import pgp.cert_d.cli.commands.Get;
|
||||||
import pgp.cert_d.cli.commands.Import;
|
import pgp.cert_d.cli.commands.Import;
|
||||||
import pgp.cert_d.exception.NotAStoreException;
|
import pgp.certificate_store.exception.NotAStoreException;
|
||||||
import pgp.certificate_store.CertificateStore;
|
import pgp.certificate_store.CertificateStore;
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
|
|
||||||
|
|
|
@ -11,6 +11,8 @@ import org.slf4j.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
import pgp.cert_d.cli.PGPCertDCli;
|
import pgp.cert_d.cli.PGPCertDCli;
|
||||||
import pgp.certificate_store.Certificate;
|
import pgp.certificate_store.Certificate;
|
||||||
|
import pgp.certificate_store.exception.BadDataException;
|
||||||
|
import pgp.certificate_store.exception.BadNameException;
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
|
|
||||||
@CommandLine.Command(name = "get",
|
@CommandLine.Command(name = "get",
|
||||||
|
@ -36,7 +38,13 @@ public class Get implements Runnable {
|
||||||
}
|
}
|
||||||
Streams.pipeAll(certificate.getInputStream(), System.out);
|
Streams.pipeAll(certificate.getInputStream(), System.out);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.info("IO Error", e);
|
LOGGER.error("IO Error", e);
|
||||||
|
System.exit(-1);
|
||||||
|
} catch (BadDataException e) {
|
||||||
|
LOGGER.error("Certificate file contains bad data.", e);
|
||||||
|
System.exit(-1);
|
||||||
|
} catch (BadNameException e) {
|
||||||
|
LOGGER.error("Certificate fingerprint mismatch.", e);
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,6 +11,7 @@ import org.slf4j.LoggerFactory;
|
||||||
import pgp.cert_d.cli.PGPCertDCli;
|
import pgp.cert_d.cli.PGPCertDCli;
|
||||||
import pgp.certificate_store.Certificate;
|
import pgp.certificate_store.Certificate;
|
||||||
import pgp.certificate_store.MergeCallback;
|
import pgp.certificate_store.MergeCallback;
|
||||||
|
import pgp.certificate_store.exception.BadDataException;
|
||||||
import picocli.CommandLine;
|
import picocli.CommandLine;
|
||||||
|
|
||||||
@CommandLine.Command(name = "import",
|
@CommandLine.Command(name = "import",
|
||||||
|
@ -32,10 +33,13 @@ public class Import implements Runnable {
|
||||||
System.out.println(certificate.getFingerprint());
|
System.out.println(certificate.getFingerprint());
|
||||||
// CHECKSTYLE:ON
|
// CHECKSTYLE:ON
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
LOGGER.info("IO-Error", e);
|
LOGGER.error("IO-Error.", e);
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
} catch (InterruptedException e) {
|
} catch (InterruptedException e) {
|
||||||
LOGGER.info("Thread interrupted.", e);
|
LOGGER.error("Thread interrupted.", e);
|
||||||
|
System.exit(-1);
|
||||||
|
} catch (BadDataException e) {
|
||||||
|
LOGGER.error("Certificate contains bad data.", e);
|
||||||
System.exit(-1);
|
System.exit(-1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,11 +10,11 @@ import java.util.Iterator;
|
||||||
|
|
||||||
import pgp.cert_d.SharedPGPCertificateDirectory;
|
import pgp.cert_d.SharedPGPCertificateDirectory;
|
||||||
import pgp.cert_d.SpecialNames;
|
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.Certificate;
|
||||||
import pgp.certificate_store.CertificateStore;
|
import pgp.certificate_store.CertificateStore;
|
||||||
import pgp.certificate_store.MergeCallback;
|
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
|
* Adapter class used to adapt the {@link SharedPGPCertificateDirectory} for use with
|
||||||
|
@ -36,98 +36,50 @@ public class SharedPGPCertificateDirectoryAdapter
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Certificate getCertificate(String identifier)
|
public Certificate getCertificate(String identifier)
|
||||||
throws IOException {
|
throws IOException, BadDataException, BadNameException {
|
||||||
String specialName = SpecialNames.lookupSpecialName(identifier);
|
String specialName = SpecialNames.lookupSpecialName(identifier);
|
||||||
if (specialName != null) {
|
if (specialName != null) {
|
||||||
try {
|
return directory.getBySpecialName(specialName);
|
||||||
return directory.getBySpecialName(specialName);
|
|
||||||
} catch (BadNameException e) {
|
|
||||||
throw new IllegalArgumentException("Unknown special name " + identifier, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return directory.getByFingerprint(identifier);
|
||||||
return directory.getByFingerprint(identifier);
|
|
||||||
} catch (BadNameException e) {
|
|
||||||
throw new IllegalArgumentException("Invalid fingerprint " + identifier, e);
|
|
||||||
} catch (BadDataException e) {
|
|
||||||
throw new IOException("Bad data.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Certificate getCertificateIfChanged(String identifier, String tag)
|
public Certificate getCertificateIfChanged(String identifier, String tag)
|
||||||
throws IOException {
|
throws IOException, BadDataException, BadNameException {
|
||||||
String specialName = SpecialNames.lookupSpecialName(identifier);
|
String specialName = SpecialNames.lookupSpecialName(identifier);
|
||||||
if (specialName != null) {
|
if (specialName != null) {
|
||||||
try {
|
return directory.getBySpecialNameIfChanged(specialName, tag);
|
||||||
return directory.getBySpecialNameIfChanged(specialName, tag);
|
|
||||||
} catch (BadNameException e) {
|
|
||||||
throw new IllegalArgumentException("Unknown special name " + identifier, e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
try {
|
return directory.getByFingerprintIfChanged(identifier, tag);
|
||||||
return directory.getByFingerprintIfChanged(identifier, tag);
|
|
||||||
} catch (BadNameException e) {
|
|
||||||
throw new IllegalArgumentException("Invalid fingerprint " + identifier, e);
|
|
||||||
} catch (BadDataException e) {
|
|
||||||
throw new IOException("Bad data.", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Certificate insertCertificate(InputStream data, MergeCallback merge)
|
public Certificate insertCertificate(InputStream data, MergeCallback merge)
|
||||||
throws IOException, InterruptedException {
|
throws IOException, InterruptedException, BadDataException {
|
||||||
try {
|
return directory.insert(data, merge);
|
||||||
return directory.insert(data, merge);
|
|
||||||
} catch (BadDataException e) {
|
|
||||||
throw new IOException("Cannot insert certificate due to bad data", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Certificate tryInsertCertificate(InputStream data, MergeCallback merge)
|
public Certificate tryInsertCertificate(InputStream data, MergeCallback merge)
|
||||||
throws IOException {
|
throws IOException, BadDataException {
|
||||||
try {
|
return directory.tryInsert(data, merge);
|
||||||
return directory.tryInsert(data, merge);
|
|
||||||
} catch (BadDataException e) {
|
|
||||||
throw new IOException("Cannot insert certificate due to bad data", e);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Certificate insertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
|
public Certificate insertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
|
||||||
throws IOException, InterruptedException {
|
throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||||
try {
|
return directory.insertWithSpecialName(specialName, data, merge);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Certificate tryInsertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
|
public Certificate tryInsertCertificateBySpecialName(String specialName, InputStream data, MergeCallback merge)
|
||||||
throws IOException {
|
throws IOException, BadDataException, BadNameException {
|
||||||
try {
|
return directory.tryInsertWithSpecialName(specialName, data, merge);
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@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.CertificateReader;
|
||||||
import org.pgpainless.certificate_store.SharedPGPCertificateDirectoryAdapter;
|
import org.pgpainless.certificate_store.SharedPGPCertificateDirectoryAdapter;
|
||||||
import pgp.cert_d.SharedPGPCertificateDirectoryImpl;
|
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.Certificate;
|
||||||
import pgp.certificate_store.CertificateStore;
|
import pgp.certificate_store.CertificateStore;
|
||||||
|
|
||||||
|
@ -53,18 +55,18 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getNonExistentCertIsNull() throws IOException {
|
public void getNonExistentCertIsNull() throws IOException, BadDataException, BadNameException {
|
||||||
assertNull(store.getCertificate("trust-root"));
|
assertNull(store.getCertificate("trust-root"));
|
||||||
assertNull(store.getCertificate("eb85bb5fa33a75e15e944e63f231550c4f47e38e"));
|
assertNull(store.getCertificate("eb85bb5fa33a75e15e944e63f231550c4f47e38e"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getInvalidIdentifierThrows() {
|
public void getInvalidIdentifierThrows() {
|
||||||
assertThrows(IllegalArgumentException.class, () -> store.getCertificate("invalid"));
|
assertThrows(BadNameException.class, () -> store.getCertificate("invalid"));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void insertAndGet() throws IOException, InterruptedException {
|
public void insertAndGet() throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||||
byte[] bytes = Hex.decode(testCertificate);
|
byte[] bytes = Hex.decode(testCertificate);
|
||||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||||
String fingerprint = testCertFingerprint;
|
String fingerprint = testCertFingerprint;
|
||||||
|
@ -83,7 +85,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void tryInsertAndGet() throws IOException {
|
public void tryInsertAndGet() throws IOException, BadDataException, BadNameException {
|
||||||
byte[] bytes = Hex.decode(testCertificate);
|
byte[] bytes = Hex.decode(testCertificate);
|
||||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||||
String fingerprint = testCertFingerprint;
|
String fingerprint = testCertFingerprint;
|
||||||
|
@ -102,7 +104,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
||||||
|
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void insertAndGetIfChanged() throws IOException, InterruptedException {
|
public void insertAndGetIfChanged() throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||||
byte[] bytes = Hex.decode(testCertificate);
|
byte[] bytes = Hex.decode(testCertificate);
|
||||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||||
String fingerprint = testCertFingerprint;
|
String fingerprint = testCertFingerprint;
|
||||||
|
@ -115,7 +117,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void insertBySpecialNameAndGet() throws IOException, InterruptedException {
|
public void insertBySpecialNameAndGet() throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||||
byte[] bytes = Hex.decode(testCertificate);
|
byte[] bytes = Hex.decode(testCertificate);
|
||||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||||
String fingerprint = testCertFingerprint;
|
String fingerprint = testCertFingerprint;
|
||||||
|
@ -134,7 +136,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void tryInsertBySpecialNameAndGet() throws IOException {
|
public void tryInsertBySpecialNameAndGet() throws IOException, BadDataException, BadNameException {
|
||||||
byte[] bytes = Hex.decode(testCertificate);
|
byte[] bytes = Hex.decode(testCertificate);
|
||||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||||
String fingerprint = testCertFingerprint;
|
String fingerprint = testCertFingerprint;
|
||||||
|
@ -153,7 +155,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void insertBySpecialNameAndGetIfChanged() throws IOException, InterruptedException {
|
public void insertBySpecialNameAndGetIfChanged() throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||||
byte[] bytes = Hex.decode(testCertificate);
|
byte[] bytes = Hex.decode(testCertificate);
|
||||||
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
ByteArrayInputStream byteIn = new ByteArrayInputStream(bytes);
|
||||||
String fingerprint = testCertFingerprint;
|
String fingerprint = testCertFingerprint;
|
||||||
|
@ -170,7 +172,7 @@ public class SharedPGPCertificateDirectoryAdapterTest {
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void getItemsAndFingerprints() throws IOException, InterruptedException {
|
public void getItemsAndFingerprints() throws IOException, InterruptedException, BadDataException, BadNameException {
|
||||||
byte[] bytes1 = Hex.decode(testCertificate);
|
byte[] bytes1 = Hex.decode(testCertificate);
|
||||||
ByteArrayInputStream byteIn1 = new ByteArrayInputStream(bytes1);
|
ByteArrayInputStream byteIn1 = new ByteArrayInputStream(bytes1);
|
||||||
Certificate firstCert = store.insertCertificate(byteIn1, (data, existing) -> data);
|
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.FileLockingMechanism;
|
||||||
import pgp.cert_d.SharedPGPCertificateDirectory;
|
import pgp.cert_d.SharedPGPCertificateDirectory;
|
||||||
import pgp.cert_d.SharedPGPCertificateDirectoryImpl;
|
import pgp.cert_d.SharedPGPCertificateDirectoryImpl;
|
||||||
import pgp.cert_d.exception.BadDataException;
|
import pgp.certificate_store.exception.BadDataException;
|
||||||
import pgp.cert_d.exception.BadNameException;
|
import pgp.certificate_store.exception.BadNameException;
|
||||||
import pgp.cert_d.exception.NotAStoreException;
|
import pgp.certificate_store.exception.NotAStoreException;
|
||||||
import pgp.certificate_store.Certificate;
|
import pgp.certificate_store.Certificate;
|
||||||
import pgp.certificate_store.MergeCallback;
|
import pgp.certificate_store.MergeCallback;
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue