28
28
import io .netty .handler .ssl .util .InsecureTrustManagerFactory ;
29
29
import io .netty .handler .ssl .util .SelfSignedCertificate ;
30
30
import io .netty .util .Mapping ;
31
+ import io .netty .util .ReferenceCountUtil ;
32
+ import io .netty .util .ReferenceCounted ;
31
33
import io .netty .util .concurrent .Promise ;
32
34
import io .netty .util .internal .PlatformDependent ;
33
35
import org .junit .Assert ;
@@ -95,12 +97,13 @@ private static void testSniClient(SslProvider sslServerProvider, SslProvider ssl
95
97
String sniHostName = "sni.netty.io" ;
96
98
LocalAddress address = new LocalAddress ("test" );
97
99
EventLoopGroup group = new DefaultEventLoopGroup (1 );
100
+ SelfSignedCertificate cert = new SelfSignedCertificate ();
101
+ SslContext sslServerContext = null ;
102
+ SslContext sslClientContext = null ;
103
+
98
104
Channel sc = null ;
99
105
Channel cc = null ;
100
106
try {
101
- SelfSignedCertificate cert = new SelfSignedCertificate ();
102
-
103
- final SslContext sslServerContext ;
104
107
if ((sslServerProvider == SslProvider .OPENSSL || sslServerProvider == SslProvider .OPENSSL_REFCNT )
105
108
&& !OpenSsl .useKeyManagerFactory ()) {
106
109
sslServerContext = SslContextBuilder .forServer (cert .certificate (), cert .privateKey ())
@@ -118,6 +121,7 @@ private static void testSniClient(SslProvider sslServerProvider, SslProvider ssl
118
121
.build ();
119
122
}
120
123
124
+ final SslContext finalContext = sslServerContext ;
121
125
final Promise <String > promise = group .next ().newPromise ();
122
126
ServerBootstrap sb = new ServerBootstrap ();
123
127
sc = sb .group (group ).channel (LocalServerChannel .class ).childHandler (new ChannelInitializer <Channel >() {
@@ -127,7 +131,7 @@ protected void initChannel(Channel ch) throws Exception {
127
131
@ Override
128
132
public SslContext map (String input ) {
129
133
promise .setSuccess (input );
130
- return sslServerContext ;
134
+ return finalContext ;
131
135
}
132
136
}));
133
137
}
@@ -136,12 +140,12 @@ public SslContext map(String input) {
136
140
TrustManagerFactory tmf = PlatformDependent .javaVersion () >= 8 ?
137
141
SniClientJava8TestUtil .newSniX509TrustmanagerFactory (sniHostName ) :
138
142
InsecureTrustManagerFactory .INSTANCE ;
139
- SslContext sslContext = SslContextBuilder .forClient ().trustManager (tmf )
143
+ sslClientContext = SslContextBuilder .forClient ().trustManager (tmf )
140
144
.sslProvider (sslClientProvider ).build ();
141
145
Bootstrap cb = new Bootstrap ();
142
146
143
147
SslHandler handler = new SslHandler (
144
- sslContext .newEngine (ByteBufAllocator .DEFAULT , sniHostName , -1 ));
148
+ sslClientContext .newEngine (ByteBufAllocator .DEFAULT , sniHostName , -1 ));
145
149
cc = cb .group (group ).channel (LocalChannel .class ).handler (handler )
146
150
.connect (address ).syncUninterruptibly ().channel ();
147
151
Assert .assertEquals (sniHostName , promise .syncUninterruptibly ().getNow ());
@@ -160,6 +164,11 @@ public SslContext map(String input) {
160
164
if (sc != null ) {
161
165
sc .close ().syncUninterruptibly ();
162
166
}
167
+ ReferenceCountUtil .release (sslServerContext );
168
+ ReferenceCountUtil .release (sslClientContext );
169
+
170
+ cert .delete ();
171
+
163
172
group .shutdownGracefully ();
164
173
}
165
174
}
0 commit comments