From d79436541117aa888968d8db268831da86ee5806 Mon Sep 17 00:00:00 2001 From: Norman Maurer Date: Thu, 10 Oct 2019 10:40:45 +0400 Subject: [PATCH] Fix SSL tests that use SslProvider.OPENSSL_REFCNT (#9649) Motivation: 031c2e2e8899d037228a492a458ccd194eb8df9c introduced some change to reduce the risk of have the `ReferenceCountedOpenSslContext` be destroyed while the `ReferenceCountedSslEngine` is still in us. Unfortunaly it missed to adjust a few tests which make assumptions about the refCnt of the context. Modifications: Adjust tests to take new semenatics into acount. Result: No more tests failures --- .../src/test/java/io/netty/handler/ssl/SniHandlerTest.java | 7 +++++-- .../src/test/java/io/netty/handler/ssl/SslHandlerTest.java | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/handler/src/test/java/io/netty/handler/ssl/SniHandlerTest.java b/handler/src/test/java/io/netty/handler/ssl/SniHandlerTest.java index 42805b4113..c8d74c31b3 100644 --- a/handler/src/test/java/io/netty/handler/ssl/SniHandlerTest.java +++ b/handler/src/test/java/io/netty/handler/ssl/SniHandlerTest.java @@ -461,6 +461,7 @@ public class SniHandlerTest { boolean success = false; try { + assertEquals(1, ((ReferenceCountedOpenSslContext) sslContext).refCnt()); // The SniHandler's replaceHandler() method allows us to implement custom behavior. // As an example, we want to release() the SslContext upon channelInactive() or rather // when the SslHandler closes it's SslEngine. If you take a close look at SslHandler @@ -468,6 +469,7 @@ public class SniHandlerTest { SSLEngine sslEngine = sslContext.newEngine(ctx.alloc()); try { + assertEquals(2, ((ReferenceCountedOpenSslContext) sslContext).refCnt()); SslHandler customSslHandler = new CustomSslHandler(sslContext, sslEngine) { @Override public void handlerRemoved0(ChannelHandlerContext ctx) throws Exception { @@ -514,8 +516,9 @@ public class SniHandlerTest { cc.writeAndFlush(Unpooled.wrappedBuffer("Hello, World!".getBytes())) .syncUninterruptibly(); - // Notice how the server's SslContext refCnt is 1 - assertEquals(1, ((ReferenceCounted) sslServerContext).refCnt()); + // Notice how the server's SslContext refCnt is 2 as it is incremented when the SSLEngine is created + // and only decremented once it is destroyed. + assertEquals(2, ((ReferenceCounted) sslServerContext).refCnt()); // The client disconnects cc.close().syncUninterruptibly(); diff --git a/handler/src/test/java/io/netty/handler/ssl/SslHandlerTest.java b/handler/src/test/java/io/netty/handler/ssl/SslHandlerTest.java index 0b1d780467..fd3bebfe8e 100644 --- a/handler/src/test/java/io/netty/handler/ssl/SslHandlerTest.java +++ b/handler/src/test/java/io/netty/handler/ssl/SslHandlerTest.java @@ -329,10 +329,11 @@ public class SslHandlerTest { .sslProvider(SslProvider.OPENSSL) .build(); try { + assertEquals(1, ((ReferenceCounted) sslContext).refCnt()); SSLEngine sslEngine = sslContext.newEngine(ByteBufAllocator.DEFAULT); EmbeddedChannel ch = new EmbeddedChannel(new SslHandler(sslEngine)); - assertEquals(1, ((ReferenceCounted) sslContext).refCnt()); + assertEquals(2, ((ReferenceCounted) sslContext).refCnt()); assertEquals(1, ((ReferenceCounted) sslEngine).refCnt()); assertTrue(ch.finishAndReleaseAll());