Skip to content

Commit

Permalink
Fix leak in SniClientJava8TestUtil (netty#8326)
Browse files Browse the repository at this point in the history
Motivation:

4d14586 did fix some leaks in SniClientTest but missed the ones in SniClientJava8TestUtil.

Modifications:

Correctly release SslContext.

Result:

No more leaks in SNI tests.
  • Loading branch information
normanmaurer authored Sep 27, 2018
1 parent 9acd927 commit 652650e
Showing 1 changed file with 17 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
import io.netty.handler.ssl.util.InsecureTrustManagerFactory;
import io.netty.handler.ssl.util.SelfSignedCertificate;
import io.netty.handler.ssl.util.SimpleTrustManagerFactory;
import io.netty.util.ReferenceCountUtil;
import io.netty.util.concurrent.Promise;
import io.netty.util.internal.EmptyArrays;
import io.netty.util.internal.ThrowableUtil;
Expand Down Expand Up @@ -76,20 +77,25 @@ private SniClientJava8TestUtil() { }
static void testSniClient(SslProvider sslClientProvider, SslProvider sslServerProvider, final boolean match)
throws Exception {
final String sniHost = "sni.netty.io";
SelfSignedCertificate cert = new SelfSignedCertificate();
LocalAddress address = new LocalAddress("test");
EventLoopGroup group = new DefaultEventLoopGroup(1);
SslContext sslServerContext = null;
SslContext sslClientContext = null;

Channel sc = null;
Channel cc = null;
try {
SelfSignedCertificate cert = new SelfSignedCertificate();
final SslContext sslServerContext = SslContextBuilder.forServer(cert.key(), cert.cert())
sslServerContext = SslContextBuilder.forServer(cert.key(), cert.cert())
.sslProvider(sslServerProvider).build();
final Promise<Void> promise = group.next().newPromise();
ServerBootstrap sb = new ServerBootstrap();

final SslContext finalContext = sslServerContext;
sc = sb.group(group).channel(LocalServerChannel.class).childHandler(new ChannelInitializer<Channel>() {
@Override
protected void initChannel(Channel ch) throws Exception {
SslHandler handler = sslServerContext.newHandler(ch.alloc());
SslHandler handler = finalContext.newHandler(ch.alloc());
SSLParameters parameters = handler.engine().getSSLParameters();
SNIMatcher matcher = new SNIMatcher(0) {
@Override
Expand Down Expand Up @@ -132,11 +138,11 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
}
}).bind(address).syncUninterruptibly().channel();

SslContext sslContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE)
sslClientContext = SslContextBuilder.forClient().trustManager(InsecureTrustManagerFactory.INSTANCE)
.sslProvider(sslClientProvider).build();

SslHandler sslHandler = new SslHandler(
sslContext.newEngine(ByteBufAllocator.DEFAULT, sniHost, -1));
sslClientContext.newEngine(ByteBufAllocator.DEFAULT, sniHost, -1));
Bootstrap cb = new Bootstrap();
cc = cb.group(group).channel(LocalChannel.class).handler(sslHandler)
.connect(address).syncUninterruptibly().channel();
Expand All @@ -150,6 +156,12 @@ public void userEventTriggered(ChannelHandlerContext ctx, Object evt) throws Exc
if (sc != null) {
sc.close().syncUninterruptibly();
}

ReferenceCountUtil.release(sslServerContext);
ReferenceCountUtil.release(sslClientContext);

cert.delete();

group.shutdownGracefully();
}
}
Expand Down

0 comments on commit 652650e

Please sign in to comment.