Search in sources :

Example 31 with Timeout

use of org.junit.jupiter.api.Timeout in project netty by netty.

the class SslHandlerTest method testHandshakeFailBeforeWritePromise.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testHandshakeFailBeforeWritePromise() throws Exception {
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    final SslContext sslServerCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).build();
    final CountDownLatch latch = new CountDownLatch(2);
    final CountDownLatch latch2 = new CountDownLatch(2);
    final BlockingQueue<Object> events = new LinkedBlockingQueue<Object>();
    Channel serverChannel = null;
    Channel clientChannel = null;
    EventLoopGroup group = new DefaultEventLoopGroup();
    try {
        ServerBootstrap sb = new ServerBootstrap();
        sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(sslServerCtx.newHandler(ch.alloc()));
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) {
                        ByteBuf buf = ctx.alloc().buffer(10);
                        buf.writeZero(buf.capacity());
                        ctx.writeAndFlush(buf).addListener(new ChannelFutureListener() {

                            @Override
                            public void operationComplete(ChannelFuture future) {
                                events.add(future);
                                latch.countDown();
                            }
                        });
                    }

                    @Override
                    public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
                        if (evt instanceof SslCompletionEvent) {
                            events.add(evt);
                            latch.countDown();
                            latch2.countDown();
                        }
                    }
                });
            }
        });
        Bootstrap cb = new Bootstrap();
        cb.group(group).channel(LocalChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addFirst(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void channelActive(ChannelHandlerContext ctx) {
                        ByteBuf buf = ctx.alloc().buffer(1000);
                        buf.writeZero(buf.capacity());
                        ctx.writeAndFlush(buf);
                    }
                });
            }
        });
        serverChannel = sb.bind(new LocalAddress("SslHandlerTest")).sync().channel();
        clientChannel = cb.connect(serverChannel.localAddress()).sync().channel();
        latch.await();
        SslCompletionEvent evt = (SslCompletionEvent) events.take();
        assertTrue(evt instanceof SslHandshakeCompletionEvent);
        assertThat(evt.cause(), is(instanceOf(SSLException.class)));
        ChannelFuture future = (ChannelFuture) events.take();
        assertThat(future.cause(), is(instanceOf(SSLException.class)));
        serverChannel.close().sync();
        serverChannel = null;
        clientChannel.close().sync();
        clientChannel = null;
        latch2.await();
        evt = (SslCompletionEvent) events.take();
        assertTrue(evt instanceof SslCloseCompletionEvent);
        assertThat(evt.cause(), is(instanceOf(ClosedChannelException.class)));
        assertTrue(events.isEmpty());
    } finally {
        if (serverChannel != null) {
            serverChannel.close();
        }
        if (clientChannel != null) {
            clientChannel.close();
        }
        group.shutdownGracefully();
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) CountDownLatch(java.util.concurrent.CountDownLatch) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) ByteBuf(io.netty.buffer.ByteBuf) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 32 with Timeout

use of org.junit.jupiter.api.Timeout in project netty by netty.

the class SslHandlerTest method testNonApplicationDataFailureFailsQueuedWrites.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testNonApplicationDataFailureFailsQueuedWrites() throws NoSuchAlgorithmException, InterruptedException {
    final CountDownLatch writeLatch = new CountDownLatch(1);
    final Queue<ChannelPromise> writesToFail = new ConcurrentLinkedQueue<ChannelPromise>();
    SSLEngine engine = newClientModeSSLEngine();
    SslHandler handler = new SslHandler(engine) {

        @Override
        public void write(final ChannelHandlerContext ctx, Object msg, ChannelPromise promise) throws Exception {
            super.write(ctx, msg, promise);
            writeLatch.countDown();
        }
    };
    EmbeddedChannel ch = new EmbeddedChannel(new ChannelDuplexHandler() {

        @Override
        public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
            if (msg instanceof ByteBuf) {
                if (((ByteBuf) msg).isReadable()) {
                    writesToFail.add(promise);
                } else {
                    promise.setSuccess();
                }
            }
            ReferenceCountUtil.release(msg);
        }
    }, handler);
    try {
        final CountDownLatch writeCauseLatch = new CountDownLatch(1);
        final AtomicReference<Throwable> failureRef = new AtomicReference<Throwable>();
        ch.write(Unpooled.wrappedBuffer(new byte[] { 1 })).addListener(new ChannelFutureListener() {

            @Override
            public void operationComplete(ChannelFuture future) {
                failureRef.compareAndSet(null, future.cause());
                writeCauseLatch.countDown();
            }
        });
        writeLatch.await();
        // Simulate failing the SslHandler non-application writes after there are applications writes queued.
        ChannelPromise promiseToFail;
        while ((promiseToFail = writesToFail.poll()) != null) {
            promiseToFail.setFailure(new RuntimeException("fake exception"));
        }
        writeCauseLatch.await();
        Throwable writeCause = failureRef.get();
        assertNotNull(writeCause);
        assertThat(writeCause, is(CoreMatchers.<Throwable>instanceOf(SSLException.class)));
        Throwable cause = handler.handshakeFuture().cause();
        assertNotNull(cause);
        assertThat(cause, is(CoreMatchers.<Throwable>instanceOf(SSLException.class)));
    } finally {
        assertFalse(ch.finishAndReleaseAll());
    }
}
Also used : ChannelFuture(io.netty.channel.ChannelFuture) SSLEngine(javax.net.ssl.SSLEngine) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ChannelPromise(io.netty.channel.ChannelPromise) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) AtomicReference(java.util.concurrent.atomic.AtomicReference) CountDownLatch(java.util.concurrent.CountDownLatch) ByteBuf(io.netty.buffer.ByteBuf) ChannelFutureListener(io.netty.channel.ChannelFutureListener) ChannelDuplexHandler(io.netty.channel.ChannelDuplexHandler) ConcurrentLinkedQueue(java.util.concurrent.ConcurrentLinkedQueue) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 33 with Timeout

use of org.junit.jupiter.api.Timeout in project netty by netty.

the class SslHandlerTest method testHandshakeAndClosePromiseFailedOnRemoval.

@Test
@Timeout(value = 5000, unit = TimeUnit.MILLISECONDS)
public void testHandshakeAndClosePromiseFailedOnRemoval() throws Exception {
    SSLEngine engine = SSLContext.getDefault().createSSLEngine();
    engine.setUseClientMode(true);
    SslHandler handler = new SslHandler(engine);
    final AtomicReference<Throwable> handshakeRef = new AtomicReference<Throwable>();
    final AtomicReference<Throwable> closeRef = new AtomicReference<Throwable>();
    EmbeddedChannel ch = new EmbeddedChannel(handler, new ChannelInboundHandlerAdapter() {

        @Override
        public void userEventTriggered(ChannelHandlerContext ctx, Object evt) {
            if (evt instanceof SslHandshakeCompletionEvent) {
                handshakeRef.set(((SslHandshakeCompletionEvent) evt).cause());
            } else if (evt instanceof SslCloseCompletionEvent) {
                closeRef.set(((SslCloseCompletionEvent) evt).cause());
            }
        }
    });
    assertFalse(handler.handshakeFuture().isDone());
    assertFalse(handler.sslCloseFuture().isDone());
    ch.pipeline().remove(handler);
    try {
        while (!handler.handshakeFuture().isDone() || handshakeRef.get() == null || !handler.sslCloseFuture().isDone() || closeRef.get() == null) {
            Thread.sleep(10);
            // Continue running all pending tasks until we notified for everything.
            ch.runPendingTasks();
        }
        assertSame(handler.handshakeFuture().cause(), handshakeRef.get());
        assertSame(handler.sslCloseFuture().cause(), closeRef.get());
    } finally {
        ch.finishAndReleaseAll();
    }
}
Also used : SSLEngine(javax.net.ssl.SSLEngine) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 34 with Timeout

use of org.junit.jupiter.api.Timeout in project netty by netty.

the class SslHandlerTest method testCloseOnHandshakeFailure.

@Test
@Timeout(value = 10000, unit = TimeUnit.MILLISECONDS)
public void testCloseOnHandshakeFailure() throws Exception {
    final SelfSignedCertificate ssc = new SelfSignedCertificate();
    final SslContext sslServerCtx = SslContextBuilder.forServer(ssc.key(), ssc.cert()).build();
    final SslContext sslClientCtx = SslContextBuilder.forClient().trustManager(new SelfSignedCertificate().cert()).build();
    EventLoopGroup group = new NioEventLoopGroup(1);
    Channel sc = null;
    Channel cc = null;
    try {
        LocalAddress address = new LocalAddress(getClass().getSimpleName() + ".testCloseOnHandshakeFailure");
        ServerBootstrap sb = new ServerBootstrap().group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(sslServerCtx.newHandler(ch.alloc()));
            }
        });
        sc = sb.bind(address).syncUninterruptibly().channel();
        final AtomicReference<SslHandler> sslHandlerRef = new AtomicReference<SslHandler>();
        Bootstrap b = new Bootstrap().group(group).channel(LocalChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                SslHandler handler = sslClientCtx.newHandler(ch.alloc());
                // We propagate the SslHandler via an AtomicReference to the outer-scope as using
                // pipeline.get(...) may return null if the pipeline was teared down by the time we call it.
                // This will happen if the channel was closed in the meantime.
                sslHandlerRef.set(handler);
                ch.pipeline().addLast(handler);
            }
        });
        cc = b.connect(sc.localAddress()).syncUninterruptibly().channel();
        SslHandler handler = sslHandlerRef.get();
        handler.handshakeFuture().awaitUninterruptibly();
        assertFalse(handler.handshakeFuture().isSuccess());
        cc.closeFuture().syncUninterruptibly();
    } finally {
        if (cc != null) {
            cc.close().syncUninterruptibly();
        }
        if (sc != null) {
            sc.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
        ReferenceCountUtil.release(sslServerCtx);
        ReferenceCountUtil.release(sslClientCtx);
    }
}
Also used : SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) LocalAddress(io.netty.channel.local.LocalAddress) LocalChannel(io.netty.channel.local.LocalChannel) LocalServerChannel(io.netty.channel.local.LocalServerChannel) LocalChannel(io.netty.channel.local.LocalChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) Channel(io.netty.channel.Channel) AtomicReference(java.util.concurrent.atomic.AtomicReference) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) EventLoopGroup(io.netty.channel.EventLoopGroup) DefaultEventLoopGroup(io.netty.channel.DefaultEventLoopGroup) LocalServerChannel(io.netty.channel.local.LocalServerChannel) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Test(org.junit.jupiter.api.Test) Timeout(org.junit.jupiter.api.Timeout)

Example 35 with Timeout

use of org.junit.jupiter.api.Timeout in project netty by netty.

the class SslErrorTest method testCorrectAlert.

@ParameterizedTest(name = "{index}: serverProvider = {0}, clientProvider = {1}, exception = {2}, serverProduceError = {3}")
@MethodSource("data")
@Timeout(value = 30000, unit = TimeUnit.MILLISECONDS)
public void testCorrectAlert(SslProvider serverProvider, final SslProvider clientProvider, final CertificateException exception, final boolean serverProduceError) throws Exception {
    // As this only works correctly at the moment when OpenSslEngine is used on the server-side there is
    // no need to run it if there is no openssl is available at all.
    OpenSsl.ensureAvailability();
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    SslContextBuilder sslServerCtxBuilder = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey()).sslProvider(serverProvider).clientAuth(ClientAuth.REQUIRE);
    SslContextBuilder sslClientCtxBuilder = SslContextBuilder.forClient().keyManager(new File(getClass().getResource("test.crt").getFile()), new File(getClass().getResource("test_unencrypted.pem").getFile())).sslProvider(clientProvider);
    if (serverProduceError) {
        sslServerCtxBuilder.trustManager(new ExceptionTrustManagerFactory(exception));
        sslClientCtxBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
    } else {
        sslServerCtxBuilder.trustManager(InsecureTrustManagerFactory.INSTANCE);
        sslClientCtxBuilder.trustManager(new ExceptionTrustManagerFactory(exception));
    }
    final SslContext sslServerCtx = sslServerCtxBuilder.build();
    final SslContext sslClientCtx = sslClientCtxBuilder.build();
    Channel serverChannel = null;
    Channel clientChannel = null;
    EventLoopGroup group = new NioEventLoopGroup();
    final Promise<Void> promise = group.next().newPromise();
    try {
        serverChannel = new ServerBootstrap().group(group).channel(NioServerSocketChannel.class).handler(new LoggingHandler(LogLevel.INFO)).childHandler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(sslServerCtx.newHandler(ch.alloc()));
                if (!serverProduceError) {
                    ch.pipeline().addLast(new AlertValidationHandler(clientProvider, serverProduceError, exception, promise));
                }
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        ctx.close();
                    }
                });
            }
        }).bind(0).sync().channel();
        clientChannel = new Bootstrap().group(group).channel(NioSocketChannel.class).handler(new ChannelInitializer<Channel>() {

            @Override
            protected void initChannel(Channel ch) {
                ch.pipeline().addLast(sslClientCtx.newHandler(ch.alloc()));
                if (serverProduceError) {
                    ch.pipeline().addLast(new AlertValidationHandler(clientProvider, serverProduceError, exception, promise));
                }
                ch.pipeline().addLast(new ChannelInboundHandlerAdapter() {

                    @Override
                    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) {
                        ctx.close();
                    }
                });
            }
        }).connect(serverChannel.localAddress()).syncUninterruptibly().channel();
        // Block until we received the correct exception
        promise.syncUninterruptibly();
    } finally {
        if (clientChannel != null) {
            clientChannel.close().syncUninterruptibly();
        }
        if (serverChannel != null) {
            serverChannel.close().syncUninterruptibly();
        }
        group.shutdownGracefully();
        ReferenceCountUtil.release(sslServerCtx);
        ReferenceCountUtil.release(sslClientCtx);
    }
}
Also used : LoggingHandler(io.netty.handler.logging.LoggingHandler) SelfSignedCertificate(io.netty.handler.ssl.util.SelfSignedCertificate) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) NioServerSocketChannel(io.netty.channel.socket.nio.NioServerSocketChannel) Channel(io.netty.channel.Channel) ChannelHandlerContext(io.netty.channel.ChannelHandlerContext) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) NioSocketChannel(io.netty.channel.socket.nio.NioSocketChannel) EventLoopGroup(io.netty.channel.EventLoopGroup) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) Bootstrap(io.netty.bootstrap.Bootstrap) ServerBootstrap(io.netty.bootstrap.ServerBootstrap) File(java.io.File) NioEventLoopGroup(io.netty.channel.nio.NioEventLoopGroup) ChannelInboundHandlerAdapter(io.netty.channel.ChannelInboundHandlerAdapter) Timeout(org.junit.jupiter.api.Timeout) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

Timeout (org.junit.jupiter.api.Timeout)291 Test (org.junit.jupiter.api.Test)235 CountDownLatch (java.util.concurrent.CountDownLatch)71 ZooKeeper (org.apache.zookeeper.ZooKeeper)33 AtomicReference (java.util.concurrent.atomic.AtomicReference)32 ArrayList (java.util.ArrayList)31 AtomicBoolean (java.util.concurrent.atomic.AtomicBoolean)29 RepeatedTest (org.junit.jupiter.api.RepeatedTest)29 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)29 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)28 LocalChannel (io.netty.channel.local.LocalChannel)27 CountdownWatcher (org.apache.zookeeper.test.ClientBase.CountdownWatcher)26 ServerBootstrap (io.netty.bootstrap.ServerBootstrap)25 IOException (java.io.IOException)25 Bootstrap (io.netty.bootstrap.Bootstrap)24 MethodSource (org.junit.jupiter.params.provider.MethodSource)24 Channel (io.netty.channel.Channel)23 NioEventLoopGroup (io.netty.channel.nio.NioEventLoopGroup)21 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)19 ChannelInboundHandlerAdapter (io.netty.channel.ChannelInboundHandlerAdapter)19