use of io.netty.channel.local.LocalAddress in project grpc-java by grpc.
the class ProtocolNegotiatorsTest method clientTlsHandler_firesNegotiation.
@Test
public void clientTlsHandler_firesNegotiation() throws Exception {
SelfSignedCertificate cert = new SelfSignedCertificate("authority");
SslContext clientSslContext = GrpcSslContexts.configure(SslContextBuilder.forClient().trustManager(cert.cert())).build();
SslContext serverSslContext = GrpcSslContexts.configure(SslContextBuilder.forServer(cert.key(), cert.cert())).build();
FakeGrpcHttp2ConnectionHandler gh = FakeGrpcHttp2ConnectionHandler.newHandler();
ClientTlsProtocolNegotiator pn = new ClientTlsProtocolNegotiator(clientSslContext, null);
WriteBufferingAndExceptionHandler clientWbaeh = new WriteBufferingAndExceptionHandler(pn.newHandler(gh));
SocketAddress addr = new LocalAddress("addr");
ChannelHandler sh = ProtocolNegotiators.serverTls(serverSslContext).newHandler(FakeGrpcHttp2ConnectionHandler.noopHandler());
WriteBufferingAndExceptionHandler serverWbaeh = new WriteBufferingAndExceptionHandler(sh);
Channel s = new ServerBootstrap().childHandler(serverWbaeh).group(group).channel(LocalServerChannel.class).bind(addr).sync().channel();
Channel c = new Bootstrap().handler(clientWbaeh).channel(LocalChannel.class).group(group).register().sync().channel();
ChannelFuture write = c.writeAndFlush(NettyClientHandler.NOOP_MESSAGE);
c.connect(addr).sync();
write.sync();
boolean completed = gh.negotiated.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (!completed) {
assertTrue("failed to negotiated", write.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
// sync should fail if we are in this block.
write.sync();
throw new AssertionError("neither wrote nor negotiated");
}
c.close();
s.close();
pn.close();
assertThat(gh.securityInfo).isNotNull();
assertThat(gh.securityInfo.tls).isNotNull();
assertThat(gh.attrs.get(GrpcAttributes.ATTR_SECURITY_LEVEL)).isEqualTo(SecurityLevel.PRIVACY_AND_INTEGRITY);
assertThat(gh.attrs.get(Grpc.TRANSPORT_ATTR_SSL_SESSION)).isInstanceOf(SSLSession.class);
// This is not part of the ClientTls negotiation, but shows that the negotiation event happens
// in the right order.
assertThat(gh.attrs.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)).isEqualTo(addr);
}
use of io.netty.channel.local.LocalAddress in project grpc-java by grpc.
the class ProtocolNegotiatorsTest method waitUntilActiveHandler_firesNegotiation.
@Test
public void waitUntilActiveHandler_firesNegotiation() throws Exception {
EventLoopGroup elg = new DefaultEventLoopGroup(1);
SocketAddress addr = new LocalAddress("addr");
final AtomicReference<Object> event = new AtomicReference<>();
ChannelHandler next = new ChannelInboundHandlerAdapter() {
@Override
public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
event.set(evt);
ctx.close();
}
};
Channel s = new ServerBootstrap().childHandler(new ChannelInboundHandlerAdapter()).group(elg).channel(LocalServerChannel.class).bind(addr).sync().channel();
Channel c = new Bootstrap().handler(new WaitUntilActiveHandler(next, noopLogger)).channel(LocalChannel.class).group(group).connect(addr).sync().channel();
c.pipeline().fireUserEventTriggered(ProtocolNegotiationEvent.DEFAULT);
SocketAddress localAddr = c.localAddress();
ProtocolNegotiationEvent expectedEvent = ProtocolNegotiationEvent.DEFAULT.withAttributes(Attributes.newBuilder().set(Grpc.TRANSPORT_ATTR_LOCAL_ADDR, localAddr).set(Grpc.TRANSPORT_ATTR_REMOTE_ADDR, addr).set(GrpcAttributes.ATTR_SECURITY_LEVEL, SecurityLevel.NONE).build());
c.closeFuture().sync();
assertThat(event.get()).isInstanceOf(ProtocolNegotiationEvent.class);
ProtocolNegotiationEvent actual = (ProtocolNegotiationEvent) event.get();
assertThat(actual).isEqualTo(expectedEvent);
s.close();
elg.shutdownGracefully();
}
use of io.netty.channel.local.LocalAddress in project grpc-java by grpc.
the class ProtocolNegotiatorsTest method plaintextUpgradeNegotiator.
@Test
public void plaintextUpgradeNegotiator() throws Exception {
LocalAddress addr = new LocalAddress("plaintextUpgradeNegotiator");
UpgradeCodecFactory ucf = new UpgradeCodecFactory() {
@Override
public UpgradeCodec newUpgradeCodec(CharSequence protocol) {
return new Http2ServerUpgradeCodec(FakeGrpcHttp2ConnectionHandler.newHandler());
}
};
final HttpServerCodec serverCodec = new HttpServerCodec();
final HttpServerUpgradeHandler serverUpgradeHandler = new HttpServerUpgradeHandler(serverCodec, ucf);
Channel serverChannel = new ServerBootstrap().group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
ch.pipeline().addLast(serverCodec, serverUpgradeHandler);
}
}).bind(addr).sync().channel();
FakeGrpcHttp2ConnectionHandler gh = FakeGrpcHttp2ConnectionHandler.newHandler();
ProtocolNegotiator nego = ProtocolNegotiators.plaintextUpgrade();
ChannelHandler ch = nego.newHandler(gh);
WriteBufferingAndExceptionHandler wbaeh = new WriteBufferingAndExceptionHandler(ch);
Channel channel = new Bootstrap().group(group).channel(LocalChannel.class).handler(wbaeh).register().sync().channel();
ChannelFuture write = channel.writeAndFlush(NettyClientHandler.NOOP_MESSAGE);
channel.connect(serverChannel.localAddress());
boolean completed = gh.negotiated.await(TIMEOUT_SECONDS, TimeUnit.SECONDS);
if (!completed) {
assertTrue("failed to negotiated", write.await(TIMEOUT_SECONDS, TimeUnit.SECONDS));
// sync should fail if we are in this block.
write.sync();
throw new AssertionError("neither wrote nor negotiated");
}
channel.close().sync();
serverChannel.close();
assertThat(gh.securityInfo).isNull();
assertThat(gh.attrs.get(GrpcAttributes.ATTR_SECURITY_LEVEL)).isEqualTo(SecurityLevel.NONE);
assertThat(gh.attrs.get(Grpc.TRANSPORT_ATTR_REMOTE_ADDR)).isEqualTo(addr);
}
use of io.netty.channel.local.LocalAddress in project grpc-java by grpc.
the class WriteBufferingAndExceptionHandlerTest method uncaughtExceptionFailuresPropagated.
@Test
public void uncaughtExceptionFailuresPropagated() throws Exception {
WriteBufferingAndExceptionHandler handler = new WriteBufferingAndExceptionHandler(new ChannelHandlerAdapter() {
});
LocalAddress addr = new LocalAddress("local");
ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register();
chan = cf.channel();
cf.sync();
ChannelFuture sf = new ServerBootstrap().channel(LocalServerChannel.class).childHandler(new ChannelHandlerAdapter() {
}).group(group).bind(addr);
server = sf.channel();
sf.sync();
ChannelFuture wf = chan.writeAndFlush(new Object());
chan.connect(addr);
chan.pipeline().fireExceptionCaught(Status.ABORTED.withDescription("zap").asRuntimeException());
try {
wf.sync();
fail();
} catch (Exception e) {
Status status = Status.fromThrowable(e);
assertThat(status.getCode()).isEqualTo(Code.ABORTED);
assertThat(status.getDescription()).contains("zap");
}
}
use of io.netty.channel.local.LocalAddress in project grpc-java by grpc.
the class WriteBufferingAndExceptionHandlerTest method connectionFailuresPropagated.
@Test
public void connectionFailuresPropagated() throws Exception {
WriteBufferingAndExceptionHandler handler = new WriteBufferingAndExceptionHandler(new ChannelHandlerAdapter() {
});
ChannelFuture cf = new Bootstrap().channel(LocalChannel.class).handler(handler).group(group).register();
chan = cf.channel();
cf.sync();
// Write before connect. In the event connect fails, the pipeline is torn down and the handler
// won't be able to fail the writes with the correct exception.
ChannelFuture wf = chan.writeAndFlush(new Object());
chan.connect(new LocalAddress("bogus"));
try {
wf.sync();
fail();
} catch (Exception e) {
assertThat(e).isInstanceOf(ConnectException.class);
assertThat(e).hasMessageThat().contains("connection refused");
}
}
Aggregations