Make TestUtils able to read from input stream. This makes it easier to pass in an input stream from a resource

This commit is contained in:
Carl Mastrangelo 2015-12-07 15:24:49 -08:00
parent 529b14c07b
commit 3fef40368d
2 changed files with 14 additions and 3 deletions

View File

@ -88,8 +88,7 @@ public class Http2OkHttpTest extends AbstractTransportTest {
.overrideAuthority(GrpcUtil.authorityFromHostAndPort(
TestUtils.TEST_SERVER_HOST, serverPort));
try {
builder.sslSocketFactory(TestUtils.newSslSocketFactoryForCa(
TestUtils.loadCert("ca.pem")));
builder.sslSocketFactory(TestUtils.newSslSocketFactoryForCa(TestUtils.loadCert("ca.pem")));
} catch (Exception e) {
throw new RuntimeException(e);
}

View File

@ -217,11 +217,23 @@ public class TestUtils {
* Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
*/
public static SSLSocketFactory newSslSocketFactoryForCa(File certChainFile) throws Exception {
InputStream is = new FileInputStream(certChainFile);
try {
return newSslSocketFactoryForCa(is);
} finally {
is.close();
}
}
/**
* Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
*/
public static SSLSocketFactory newSslSocketFactoryForCa(InputStream certChain) throws Exception {
KeyStore ks = KeyStore.getInstance("JKS");
ks.load(null, null);
CertificateFactory cf = CertificateFactory.getInstance("X.509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(
new BufferedInputStream(new FileInputStream(certChainFile)));
new BufferedInputStream(certChain));
X500Principal principal = cert.getSubjectX500Principal();
ks.setCertificateEntry(principal.getName("RFC2253"), cert);