5656import io .grpc .Status ;
5757import io .grpc .Status .Code ;
5858import io .grpc .StatusException ;
59+ import io .grpc .TlsChannelCredentials ;
5960import io .grpc .internal .ClientStream ;
6061import io .grpc .internal .ClientStreamListener ;
6162import io .grpc .internal .ClientTransport ;
@@ -830,6 +831,45 @@ public void tlsNegotiationServerExecutorShouldSucceed() throws Exception {
830831 assertEquals (false , serverExecutorPool .isInUse ());
831832 }
832833
834+ /**
835+ * This test tests the case of TlsCredentials passed to ProtocolNegotiators not having an instance
836+ * of X509ExtendedTrustManager (this is not testable in ProtocolNegotiatorsTest without creating
837+ * accessors for the internal state of negotiator whether it has a X509ExtendedTrustManager,
838+ * hence the need to test it in this class instead). To establish a successful handshake we create
839+ * a fake X509TrustManager not implementing X509ExtendedTrustManager but wraps the real
840+ * X509ExtendedTrustManager.
841+ */
842+ @ Test
843+ public void authorityOverrideInCallOptions_noX509ExtendedTrustManager_newStreamCreationFails ()
844+ throws IOException , InterruptedException , GeneralSecurityException {
845+ startServer ();
846+ InputStream caCert = TlsTesting .loadCert ("ca.pem" );
847+ X509TrustManager x509ExtendedTrustManager =
848+ (X509TrustManager ) getX509ExtendedTrustManager (caCert ).get ();
849+ ProtocolNegotiators .FromChannelCredentialsResult result =
850+ ProtocolNegotiators .from (TlsChannelCredentials .newBuilder ()
851+ .trustManager (new FakeTrustManager (x509ExtendedTrustManager )).build ());
852+ NettyClientTransport transport = newTransport (result .negotiator .newNegotiator ());
853+ FakeClientTransportListener fakeClientTransportListener = new FakeClientTransportListener ();
854+ callMeMaybe (transport .start (fakeClientTransportListener ));
855+ synchronized (fakeClientTransportListener ) {
856+ fakeClientTransportListener .wait (10000 );
857+ }
858+ assertThat (fakeClientTransportListener .isConnected ).isTrue ();
859+
860+ ClientStream stream = transport .newStream (
861+ Rpc .METHOD , new Metadata (), CallOptions .DEFAULT .withAuthority ("foo.test.google.in" ),
862+ new ClientStreamTracer []{new ClientStreamTracer () {
863+ }});
864+
865+ assertThat (stream ).isInstanceOf (FailingClientStream .class );
866+ InsightBuilder insightBuilder = new InsightBuilder ();
867+ stream .appendTimeoutInsight (insightBuilder );
868+ assertThat (insightBuilder .toString ()).contains (
869+ "Status{code=INTERNAL, description=Can't allow authority override in rpc when "
870+ + "X509ExtendedTrustManager is not available, cause=null}" );
871+ }
872+
833873 @ Test
834874 public void authorityOverrideInCallOptions_doesntMatchServerPeerHost_newStreamCreationFails ()
835875 throws IOException , InterruptedException , GeneralSecurityException {
@@ -1227,26 +1267,6 @@ public void log(ChannelLogLevel level, String message) {}
12271267 public void log (ChannelLogLevel level , String messageFormat , Object ... args ) {}
12281268 }
12291269
1230- static class FakeTrustManager implements X509TrustManager {
1231-
1232- @ Override
1233- public void checkClientTrusted (X509Certificate [] x509Certificates , String s )
1234- throws CertificateException {
1235-
1236- }
1237-
1238- @ Override
1239- public void checkServerTrusted (X509Certificate [] x509Certificates , String s )
1240- throws CertificateException {
1241-
1242- }
1243-
1244- @ Override
1245- public X509Certificate [] getAcceptedIssuers () {
1246- return new X509Certificate [0 ];
1247- }
1248- }
1249-
12501270 static class FakeClientTransportListener implements ManagedClientTransport .Listener {
12511271 private boolean isConnected = false ;
12521272
@@ -1273,4 +1293,30 @@ public void transportInUse(boolean inUse) {
12731293
12741294 }
12751295 }
1296+
1297+ private class FakeTrustManager implements X509TrustManager {
1298+
1299+ private final X509TrustManager delegate ;
1300+
1301+ public FakeTrustManager (X509TrustManager x509ExtendedTrustManager ) {
1302+ this .delegate = x509ExtendedTrustManager ;
1303+ }
1304+
1305+ @ Override
1306+ public void checkClientTrusted (X509Certificate [] x509Certificates , String s )
1307+ throws CertificateException {
1308+ delegate .checkClientTrusted (x509Certificates , s );
1309+ }
1310+
1311+ @ Override
1312+ public void checkServerTrusted (X509Certificate [] x509Certificates , String s )
1313+ throws CertificateException {
1314+ delegate .checkServerTrusted (x509Certificates , s );
1315+ }
1316+
1317+ @ Override
1318+ public X509Certificate [] getAcceptedIssuers () {
1319+ return delegate .getAcceptedIssuers ();
1320+ }
1321+ }
12761322}
0 commit comments