Extract X509Certificate for testing

pull/17/merge
Richard Körber 2015-12-15 22:32:02 +01:00
parent daf84bd3c0
commit 575902bd60
2 changed files with 19 additions and 7 deletions

View File

@ -22,12 +22,10 @@ import static uk.co.datumedge.hamcrest.json.SameJSONAs.sameJSONAs;
import java.io.ByteArrayInputStream; import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException; import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection; import java.net.HttpURLConnection;
import java.net.URI; import java.net.URI;
import java.net.URISyntaxException; import java.net.URISyntaxException;
import java.security.KeyPair; import java.security.KeyPair;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate; import java.security.cert.X509Certificate;
import java.util.HashMap; import java.util.HashMap;
import java.util.HashSet; import java.util.HashSet;
@ -386,11 +384,7 @@ public class DefaultConnectionTest {
*/ */
@Test @Test
public void testReadCertificate() throws Exception { public void testReadCertificate() throws Exception {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509"); X509Certificate original = TestUtils.createCertificate();
X509Certificate original;
try (InputStream cert = getClass().getResourceAsStream("/cert.pem")) {
original = (X509Certificate) certificateFactory.generateCertificate(cert);
}
when(mockUrlConnection.getHeaderField("Content-Type")).thenReturn("application/pkix-cert"); when(mockUrlConnection.getHeaderField("Content-Type")).thenReturn("application/pkix-cert");
when(mockUrlConnection.getInputStream()).thenReturn(new ByteArrayInputStream(original.getEncoded())); when(mockUrlConnection.getInputStream()).thenReturn(new ByteArrayInputStream(original.getEncoded()));

View File

@ -26,6 +26,9 @@ import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException; import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey; import java.security.PrivateKey;
import java.security.PublicKey; import java.security.PublicKey;
import java.security.cert.CertificateException;
import java.security.cert.CertificateFactory;
import java.security.cert.X509Certificate;
import java.security.spec.InvalidKeySpecException; import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec; import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec; import java.security.spec.X509EncodedKeySpec;
@ -133,6 +136,21 @@ public final class TestUtils {
} }
} }
/**
* Creates a standard certificate for testing. This certificate is read from a test
* resource and is guaranteed not to change between test runs.
*
* @return {@link X509Certificate} for testing
*/
public static X509Certificate createCertificate() throws IOException {
try (InputStream cert = TestUtils.class.getResourceAsStream("/cert.pem")) {
CertificateFactory certificateFactory = CertificateFactory.getInstance("X.509");
return (X509Certificate) certificateFactory.generateCertificate(cert);
} catch (CertificateException ex) {
throw new IOException(ex);
}
}
/** /**
* Generates a new keypair for unit tests, and return its N, E, KTY and THUMBPRINT * Generates a new keypair for unit tests, and return its N, E, KTY and THUMBPRINT
* parameters to be set in the {@link TestUtils} class. * parameters to be set in the {@link TestUtils} class.