Add test for missing cert

This commit is contained in:
Paul Schaub 2022-03-21 13:11:00 +01:00
parent 21bdd04d80
commit f1505083c4
Signed by: vanitasvitae
GPG key ID: 62BEE9264BF17311
4 changed files with 43 additions and 17 deletions

View file

@ -10,6 +10,7 @@ import pgp.wkd.WKDAddress;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import java.net.URI;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
@ -67,6 +68,10 @@ public final class DiscoveryResponse {
return address; return address;
} }
public URI getUri() {
return getAddress().getUri(getMethod());
}
public boolean isSuccessful() { public boolean isSuccessful() {
return !hasFetchingFailure(); return !hasFetchingFailure();
} }

View file

@ -49,9 +49,11 @@ public class DiscoveryResult {
* *
* @param outputStream output stream * @param outputStream output stream
*/ */
public void write(OutputStream outputStream) throws IOException { public void write(OutputStream outputStream)
throws IOException {
if (!isSuccessful()) { if (!isSuccessful()) {
throw new CertNotFetchableException("Cannot fetch cert."); throwCertNotFetchableException();
} }
byte[] buf = new byte[4096]; byte[] buf = new byte[4096];
@ -64,6 +66,21 @@ public class DiscoveryResult {
} }
} }
private void throwCertNotFetchableException() {
Throwable cause = null;
for (DiscoveryResponse response : getItems()) {
// Find the most "useful" exception.
// Rejections are more useful than fetching failures
if (!response.getRejectedCertificates().isEmpty()) {
cause = response.getRejectedCertificates().get(0).getReasonForRejection();
break;
} else {
cause = response.getFetchingFailure();
}
}
throw new CertNotFetchableException("Could not fetch certificates.", cause);
}
@Nonnull @Nonnull
public List<DiscoveryResponse> getItems() { public List<DiscoveryResponse> getItems() {
return items; return items;

View file

@ -11,11 +11,7 @@ public class CertNotFetchableException extends RuntimeException {
public static final int ERROR_CODE = 3; public static final int ERROR_CODE = 3;
public CertNotFetchableException(String message) { public CertNotFetchableException(String message, Throwable cause) {
super(message); super(message, cause);
}
public CertNotFetchableException(String message, Throwable e) {
super(message, e);
} }
} }

View file

@ -5,6 +5,15 @@
package pgp.wkd.test_suite; package pgp.wkd.test_suite;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.PGPainless;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import pgp.wkd.discovery.DiscoveryMethod;
import java.io.File; import java.io.File;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.IOException; import java.io.IOException;
@ -18,15 +27,6 @@ import java.util.Iterator;
import java.util.List; import java.util.List;
import java.util.Random; import java.util.Random;
import org.bouncycastle.openpgp.PGPException;
import org.bouncycastle.openpgp.PGPPublicKey;
import org.bouncycastle.openpgp.PGPPublicKeyRing;
import org.bouncycastle.openpgp.PGPSecretKeyRing;
import org.bouncycastle.openpgp.PGPSignature;
import org.pgpainless.PGPainless;
import org.pgpainless.key.protection.SecretKeyRingProtector;
import pgp.wkd.discovery.DiscoveryMethod;
public class TestSuiteGenerator { public class TestSuiteGenerator {
@ -49,6 +49,7 @@ public class TestSuiteGenerator {
tests.addAll(test_baseCaseMultiUserIds(dirs)); tests.addAll(test_baseCaseMultiUserIds(dirs));
tests.add(test_secretKeyMaterial(dirs)); tests.add(test_secretKeyMaterial(dirs));
tests.add(test_randomBytes(dirs)); tests.add(test_randomBytes(dirs));
tests.add(test_missingCertificate(dirs));
return new TestSuite("0.1", tests); return new TestSuite("0.1", tests);
} }
@ -230,6 +231,13 @@ public class TestSuiteGenerator {
return TestCase.fail("Random Bytes", description, lookupMail, directoryStructure); return TestCase.fail("Random Bytes", description, lookupMail, directoryStructure);
} }
private TestCase test_missingCertificate(WkdDirectoryStructure dirs) {
String lookupMail = "missing-cert@" + domain;
String title = "Missing certificate";
String description = "There is no certificate for the lookup mail address '" + lookupMail + "'.";
return TestCase.fail(title, description, lookupMail, dirs);
}
// INTERNAL METHODS // INTERNAL METHODS
private WkdDirectoryStructure directoryStructureForMethod(File directory, DiscoveryMethod method) { private WkdDirectoryStructure directoryStructureForMethod(File directory, DiscoveryMethod method) {