From 3fef40368d6919313aecc81755d26fe804f9708c Mon Sep 17 00:00:00 2001 From: Carl Mastrangelo Date: Mon, 7 Dec 2015 15:24:49 -0800 Subject: [PATCH] Make TestUtils able to read from input stream. This makes it easier to pass in an input stream from a resource --- .../grpc/testing/integration/Http2OkHttpTest.java | 3 +-- .../src/main/java/io/grpc/testing/TestUtils.java | 14 +++++++++++++- 2 files changed, 14 insertions(+), 3 deletions(-) diff --git a/interop-testing/src/test/java/io/grpc/testing/integration/Http2OkHttpTest.java b/interop-testing/src/test/java/io/grpc/testing/integration/Http2OkHttpTest.java index 3a59f7dbf7..652d3ca457 100644 --- a/interop-testing/src/test/java/io/grpc/testing/integration/Http2OkHttpTest.java +++ b/interop-testing/src/test/java/io/grpc/testing/integration/Http2OkHttpTest.java @@ -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); } diff --git a/testing/src/main/java/io/grpc/testing/TestUtils.java b/testing/src/main/java/io/grpc/testing/TestUtils.java index 1127cfbc43..f29baec2af 100644 --- a/testing/src/main/java/io/grpc/testing/TestUtils.java +++ b/testing/src/main/java/io/grpc/testing/TestUtils.java @@ -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);