Skip to content

Commit d362406

Browse files
committed
Remove HostnameVerifier overrides; use common channel creation
1 parent be92f57 commit d362406

File tree

1 file changed

+18
-63
lines changed

1 file changed

+18
-63
lines changed

okhttp/src/test/java/io/grpc/okhttp/TlsTest.java

Lines changed: 18 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,9 @@
5151
import java.security.cert.X509Certificate;
5252
import java.util.Arrays;
5353
import java.util.Optional;
54-
import javax.net.ssl.HostnameVerifier;
5554
import javax.net.ssl.SSLContext;
5655
import javax.net.ssl.SSLEngine;
5756
import javax.net.ssl.SSLPeerUnverifiedException;
58-
import javax.net.ssl.SSLSession;
5957
import javax.net.ssl.SSLSocketFactory;
6058
import javax.net.ssl.TrustManager;
6159
import javax.net.ssl.TrustManagerFactory;
@@ -128,11 +126,7 @@ public void perRpcAuthorityOverride_hostnameVerification_success()
128126
ChannelCredentials channelCreds = TlsChannelCredentials.newBuilder()
129127
.trustManager(getFakeX509ExtendedTrustManager())
130128
.build();
131-
ManagedChannel channel = grpcCleanupRule.register(grpcCleanupRule.register(
132-
OkHttpChannelBuilder.forAddress("localhost", server.getPort(), channelCreds)
133-
.overrideAuthority(TestUtils.TEST_SERVER_HOST)
134-
.directExecutor()
135-
.build()));
129+
ManagedChannel channel = grpcCleanupRule.register(clientChannel(server, channelCreds));
136130

137131
ClientCalls.blockingUnaryCall(channel, SimpleServiceGrpc.getUnaryRpcMethod(),
138132
CallOptions.DEFAULT.withAuthority("foo.test.google.fr"),
@@ -155,33 +149,20 @@ public void perRpcAuthorityOverride_hostnameVerification_failure_rpcFails()
155149
.build();
156150
}
157151
Server server = grpcCleanupRule.register(server(serverCreds));
158-
SSLSocketFactory sslSocketFactory = TestUtils.newSslSocketFactoryForCa(
159-
Platform.get().getProvider(), TestUtils.loadCert("ca.pem"));
160-
ManagedChannel channel = grpcCleanupRule.register(grpcCleanupRule.register(
161-
OkHttpChannelBuilder.forAddress("localhost", server.getPort())
162-
.directExecutor()
163-
.sslSocketFactory(sslSocketFactory)
164-
.hostnameVerifier(new HostnameVerifier() {
165-
private int callCount;
166-
@Override
167-
public boolean verify(String hostname, SSLSession session) {
168-
if (++callCount == 1) {
169-
return true;
170-
}
171-
return hostname.equals("foo.test.google.fr");
172-
}
173-
})
174-
.build()));
152+
ChannelCredentials channelCreds = TlsChannelCredentials.newBuilder()
153+
.trustManager(getFakeX509ExtendedTrustManager())
154+
.build();
155+
ManagedChannel channel = grpcCleanupRule.register(clientChannel(server, channelCreds));
175156

176157
try {
177158
ClientCalls.blockingUnaryCall(channel, SimpleServiceGrpc.getUnaryRpcMethod(),
178-
CallOptions.DEFAULT.withAuthority("foo.test.google.in"),
159+
CallOptions.DEFAULT.withAuthority("disallowed.name.com"),
179160
SimpleRequest.getDefaultInstance());
180161
fail("Expected exception for hostname verifier failure.");
181162
} catch (StatusRuntimeException ex) {
182163
assertThat(ex.getStatus().getCode()).isEqualTo(Status.Code.UNAVAILABLE);
183164
assertThat(ex.getStatus().getDescription()).isEqualTo(
184-
"HostNameVerifier verification failed for authority 'foo.test.google.in'");
165+
"HostNameVerifier verification failed for authority 'disallowed.name.com'");
185166
}
186167
} finally {
187168
OkHttpClientTransport.enablePerRpcAuthorityCheck = false;
@@ -199,26 +180,13 @@ public void perRpcAuthorityOverride_hostnameVerification_failure_flagDisabled_rp
199180
.build();
200181
}
201182
Server server = grpcCleanupRule.register(server(serverCreds));
202-
SSLSocketFactory sslSocketFactory = TestUtils.newSslSocketFactoryForCa(
203-
Platform.get().getProvider(), TestUtils.loadCert("ca.pem"));
204-
ManagedChannel channel = grpcCleanupRule.register(grpcCleanupRule.register(
205-
OkHttpChannelBuilder.forAddress("localhost", server.getPort())
206-
.directExecutor()
207-
.sslSocketFactory(sslSocketFactory)
208-
.hostnameVerifier(new HostnameVerifier() {
209-
private int callCount;
210-
@Override
211-
public boolean verify(String hostname, SSLSession session) {
212-
if (++callCount == 1) {
213-
return true;
214-
}
215-
return hostname.equals("foo.test.google.fr");
216-
}
217-
})
218-
.build()));
183+
ChannelCredentials channelCreds = TlsChannelCredentials.newBuilder()
184+
.trustManager(getFakeX509ExtendedTrustManager())
185+
.build();
186+
ManagedChannel channel = grpcCleanupRule.register(clientChannel(server, channelCreds));
219187

220188
ClientCalls.blockingUnaryCall(channel, SimpleServiceGrpc.getUnaryRpcMethod(),
221-
CallOptions.DEFAULT.withAuthority("foo.test.google.in"),
189+
CallOptions.DEFAULT.withAuthority("disallowed.name.com"),
222190
SimpleRequest.getDefaultInstance());
223191
}
224192

@@ -237,31 +205,22 @@ public void perRpcAuthorityOverride_noTlsCredentialsUsedToBuildChannel_disallows
237205
Server server = grpcCleanupRule.register(server(serverCreds));
238206
SSLSocketFactory sslSocketFactory = TestUtils.newSslSocketFactoryForCa(
239207
Platform.get().getProvider(), TestUtils.loadCert("ca.pem"));
240-
ManagedChannel channel = grpcCleanupRule.register(grpcCleanupRule.register(
208+
ManagedChannel channel = grpcCleanupRule.register(
241209
OkHttpChannelBuilder.forAddress("localhost", server.getPort())
210+
.overrideAuthority(TestUtils.TEST_SERVER_HOST)
242211
.directExecutor()
243212
.sslSocketFactory(sslSocketFactory)
244-
.hostnameVerifier(new HostnameVerifier() {
245-
private int callCount;
246-
@Override
247-
public boolean verify(String hostname, SSLSession session) {
248-
if (++callCount == 1) {
249-
return true;
250-
}
251-
return hostname.equals("foo.test.google.fr");
252-
}
253-
})
254-
.build()));
213+
.build());
255214

256215
try {
257216
ClientCalls.blockingUnaryCall(channel, SimpleServiceGrpc.getUnaryRpcMethod(),
258-
CallOptions.DEFAULT.withAuthority("foo.test.google.fr"),
217+
CallOptions.DEFAULT.withAuthority("bar.test.google.fr"),
259218
SimpleRequest.getDefaultInstance());
260219
fail("Expected exception for authority verification failure.");
261220
} catch (StatusRuntimeException ex) {
262221
assertThat(ex.getStatus().getCode()).isEqualTo(Status.Code.UNAVAILABLE);
263222
assertThat(ex.getStatus().getDescription()).isEqualTo(
264-
"Could not verify authority 'foo.test.google.fr' for the rpc with no "
223+
"Could not verify authority 'bar.test.google.fr' for the rpc with no "
265224
+ "X509TrustManager available");
266225
}
267226
} finally {
@@ -330,11 +289,7 @@ public void perRpcAuthorityOverride_peerVerificationFails_rpcFails()
330289
.build();
331290
}
332291
Server server = grpcCleanupRule.register(server(serverCreds));
333-
ManagedChannel channel = grpcCleanupRule.register(
334-
OkHttpChannelBuilder.forAddress("localhost", server.getPort(), channelCreds)
335-
.overrideAuthority(TestUtils.TEST_SERVER_HOST)
336-
.directExecutor()
337-
.build());
292+
ManagedChannel channel = grpcCleanupRule.register(clientChannel(server, channelCreds));
338293

339294
try {
340295
fakeTrustManager.setFailCheckServerTrusted();

0 commit comments

Comments
 (0)