Search in sources :

Example 46 with LocalAddress

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);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) ClientTlsProtocolNegotiator(io.grpc.netty.ProtocolNegotiators.ClientTlsProtocolNegotiator) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) LocalAddress(io.netty.channel.local.LocalAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelHandler(io.netty.channel.ChannelHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) SslContext(io.netty.handler.ssl.SslContext) Test(org.junit.Test)

Example 47 with LocalAddress

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();
}
Also used : LocalAddress(io.netty.channel.local.LocalAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelHandler(io.netty.channel.ChannelHandler) WaitUntilActiveHandler(io.grpc.netty.ProtocolNegotiators.WaitUntilActiveHandler) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.Test)

Example 48 with LocalAddress

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);
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) Http2ServerUpgradeCodec(io.netty.handler.codec.http2.Http2ServerUpgradeCodec) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Channel(io.netty.channel.Channel) LocalChannel(io.netty.channel.local.LocalChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) UpgradeCodecFactory(io.netty.handler.codec.http.HttpServerUpgradeHandler.UpgradeCodecFactory) ChannelHandler(io.netty.channel.ChannelHandler) HttpServerUpgradeHandler(io.netty.handler.codec.http.HttpServerUpgradeHandler) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ClientTlsProtocolNegotiator(io.grpc.netty.ProtocolNegotiators.ClientTlsProtocolNegotiator) HttpServerCodec(io.netty.handler.codec.http.HttpServerCodec) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInitializer(io.netty.channel.ChannelInitializer) Test(org.junit.Test)

Example 49 with LocalAddress

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");
    }
}
Also used : ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ChannelFuture(io.netty.channel.ChannelFuture) Status(io.grpc.Status) LocalAddress(io.netty.channel.local.LocalAddress) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Example 50 with LocalAddress

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");
    }
}
Also used : ChannelHandlerAdapter(io.netty.channel.ChannelHandlerAdapter) ChannelFuture(io.netty.channel.ChannelFuture) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ConnectException(java.net.ConnectException) ConnectException(java.net.ConnectException) Test(org.junit.Test)

Aggregations

LocalAddress (io.netty.channel.local.LocalAddress)116 Bootstrap (io.netty.bootstrap.Bootstrap)66 LocalChannel (io.netty.channel.local.LocalChannel)66 LocalServerChannel (io.netty.channel.local.LocalServerChannel)66 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)63 Channel (io.netty.channel.Channel)61 Test (org.junit.Test)47 Test (org.junit.jupiter.api.Test)39 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)32 EventLoopGroup (io.netty.channel.EventLoopGroup)32 DefaultEventLoopGroup (io.netty.channel.DefaultEventLoopGroup)31 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)28 ConnectException (java.net.ConnectException)27 ArrayBlockingQueue (java.util.concurrent.ArrayBlockingQueue)27 Subscription (rx.Subscription)27 HttpInitiator (org.jocean.http.client.HttpClient.HttpInitiator)26 HttpTrade (org.jocean.http.server.HttpServerBuilder.HttpTrade)26 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)25 ChannelFuture (io.netty.channel.ChannelFuture)24 SSLException (javax.net.ssl.SSLException)22