Skip to content

Commit 3fef403

Browse files
Make TestUtils able to read from input stream. This makes it easier to pass in an input stream from a resource
1 parent 529b14c commit 3fef403

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

interop-testing/src/test/java/io/grpc/testing/integration/Http2OkHttpTest.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,8 +88,7 @@ protected ManagedChannel createChannel() {
8888
.overrideAuthority(GrpcUtil.authorityFromHostAndPort(
8989
TestUtils.TEST_SERVER_HOST, serverPort));
9090
try {
91-
builder.sslSocketFactory(TestUtils.newSslSocketFactoryForCa(
92-
TestUtils.loadCert("ca.pem")));
91+
builder.sslSocketFactory(TestUtils.newSslSocketFactoryForCa(TestUtils.loadCert("ca.pem")));
9392
} catch (Exception e) {
9493
throw new RuntimeException(e);
9594
}

testing/src/main/java/io/grpc/testing/TestUtils.java

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,23 @@ public static File loadCert(String name) throws IOException {
217217
* Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
218218
*/
219219
public static SSLSocketFactory newSslSocketFactoryForCa(File certChainFile) throws Exception {
220+
InputStream is = new FileInputStream(certChainFile);
221+
try {
222+
return newSslSocketFactoryForCa(is);
223+
} finally {
224+
is.close();
225+
}
226+
}
227+
228+
/**
229+
* Creates an SSLSocketFactory which contains {@code certChainFile} as its only root certificate.
230+
*/
231+
public static SSLSocketFactory newSslSocketFactoryForCa(InputStream certChain) throws Exception {
220232
KeyStore ks = KeyStore.getInstance("JKS");
221233
ks.load(null, null);
222234
CertificateFactory cf = CertificateFactory.getInstance("X.509");
223235
X509Certificate cert = (X509Certificate) cf.generateCertificate(
224-
new BufferedInputStream(new FileInputStream(certChainFile)));
236+
new BufferedInputStream(certChain));
225237
X500Principal principal = cert.getSubjectX500Principal();
226238
ks.setCertificateEntry(principal.getName("RFC2253"), cert);
227239

0 commit comments

Comments
 (0)