Tests that a server configured to require client authentication actually does require client authentication.
()
| 294 | * authentication. |
| 295 | */ |
| 296 | @Test |
| 297 | public void noClientAuthFailure() throws Exception { |
| 298 | // Create & start a server. |
| 299 | File serverCertFile = TestUtils.loadCert("server1.pem"); |
| 300 | File serverPrivateKeyFile = TestUtils.loadCert("server1.key"); |
| 301 | X509Certificate[] serverTrustedCaCerts = { |
| 302 | TestUtils.loadX509Cert("ca.pem") |
| 303 | }; |
| 304 | server = serverBuilder(0, serverCertFile, serverPrivateKeyFile, serverTrustedCaCerts) |
| 305 | .addService(new SimpleServiceImpl()) |
| 306 | .build() |
| 307 | .start(); |
| 308 | |
| 309 | // Create a client. It has no credentials. |
| 310 | X509Certificate[] clientTrustedCaCerts = { |
| 311 | TestUtils.loadX509Cert("ca.pem") |
| 312 | }; |
| 313 | channel = clientChannel(server.getPort(), clientContextBuilder |
| 314 | .trustManager(clientTrustedCaCerts) |
| 315 | .build()); |
| 316 | SimpleServiceGrpc.SimpleServiceBlockingStub client = SimpleServiceGrpc.newBlockingStub(channel); |
| 317 | |
| 318 | // Check that the TLS handshake fails. |
| 319 | try { |
| 320 | client.unaryRpc(SimpleRequest.getDefaultInstance()); |
| 321 | fail("TLS handshake should have failed, but didn't; received RPC response"); |
| 322 | } catch (StatusRuntimeException e) { |
| 323 | // GRPC reports this situation by throwing a StatusRuntimeException that wraps either a |
| 324 | // javax.net.ssl.SSLHandshakeException or a java.nio.channels.ClosedChannelException. |
| 325 | // Thus, reliably detecting the underlying cause is not feasible. |
| 326 | assertEquals( |
| 327 | Throwables.getStackTraceAsString(e), |
| 328 | Status.Code.UNAVAILABLE, e.getStatus().getCode()); |
| 329 | } |
| 330 | // We really want to see TRANSIENT_FAILURE here, but if the test runs slowly the 1s backoff |
| 331 | // may be exceeded by the time the failure happens (since it counts from the start of the |
| 332 | // attempt). Even so, CONNECTING is a strong indicator that the handshake failed; otherwise we'd |
| 333 | assertThat(channel.getState(false)) |
| 334 | .isAnyOf(ConnectivityState.TRANSIENT_FAILURE, ConnectivityState.CONNECTING); |
| 335 | } |
| 336 | |
| 337 | |
| 338 | /** |
nothing calls this directly
no test coverage detected