ProtocolNegotiators { public static ProtocolNegotiator tls(SslContext sslContext, String authority) { Preconditions.checkNotNull(sslContext, "sslContext"); URI uri = GrpcUtil.authorityToUri(Preconditions.checkNotNull(authority, "authority")); String host; int port; if (uri.getHost() != null) { host = uri.getHost(); port = uri.getPort(); } else { host = authority; port = -1; } return new TlsNegotiator(sslContext, host, port); } private  ProtocolNegotiators(); static ProtocolNegotiator serverPlaintext(); static ProtocolNegotiator serverTls(final SslContext sslContext); static ProtocolNegotiator tls(SslContext sslContext, String authority); static ProtocolNegotiator plaintextUpgrade(); static ProtocolNegotiator plaintext();  }
@Test public void tls_failsOnNullSslContext() { thrown.expect(NullPointerException.class); ProtocolNegotiators.tls(null, "authority"); }
@Test public void tls_hostAndPort() throws SSLException { SslContext ctx = GrpcSslContexts.forClient().build(); TlsNegotiator negotiator = (TlsNegotiator) ProtocolNegotiators.tls(ctx, "authority:1234"); assertEquals("authority", negotiator.getHost()); assertEquals(1234, negotiator.getPort()); }
@Test public void tls_host() throws SSLException { SslContext ctx = GrpcSslContexts.forClient().build(); TlsNegotiator negotiator = (TlsNegotiator) ProtocolNegotiators.tls(ctx, "[::1]"); assertEquals("[::1]", negotiator.getHost()); assertEquals(-1, negotiator.getPort()); }
@Test public void tls_invalidHost() throws SSLException { SslContext ctx = GrpcSslContexts.forClient().build(); TlsNegotiator negotiator = (TlsNegotiator) ProtocolNegotiators.tls(ctx, "bad_host:1234"); assertEquals("bad_host:1234", negotiator.getHost()); assertEquals(-1, negotiator.getPort()); }