3232import com .rabbitmq .stream .sasl .DefaultSaslConfiguration ;
3333import io .netty .handler .ssl .SslContext ;
3434import io .netty .handler .ssl .SslContextBuilder ;
35+ import io .netty .handler .ssl .SslProvider ;
3536import java .io .File ;
3637import java .io .FileInputStream ;
3738import java .net .InetAddress ;
5455import javax .net .ssl .SSLHandshakeException ;
5556import org .junit .jupiter .api .Test ;
5657import org .junit .jupiter .api .extension .ExtendWith ;
58+ import org .junit .jupiter .params .Parameter ;
59+ import org .junit .jupiter .params .ParameterizedClass ;
60+ import org .junit .jupiter .params .provider .EnumSource ;
5761
5862@ DisabledIfTlsNotEnabled
5963@ ExtendWith (TestUtils .StreamTestInfrastructureExtension .class )
64+ @ ParameterizedClass
65+ @ EnumSource (names = {"JDK" , "OPENSSL" })
6066public class TlsTest {
6167
68+ @ Parameter SslProvider sslProvider ;
69+
6270 String stream ;
6371
6472 TestUtils .ClientFactory cf ;
6573 int credit = 10 ;
6674
67- static SslContext alwaysTrustSslContext () {
75+ SslContext alwaysTrustSslContext () {
6876 try {
69- return SslContextBuilder . forClient ().trustManager (TRUST_EVERYTHING_TRUST_MANAGER ).build ();
77+ return builder ().trustManager (TRUST_EVERYTHING_TRUST_MANAGER ).build ();
7078 } catch (SSLException e ) {
7179 throw new RuntimeException (e );
7280 }
@@ -191,7 +199,7 @@ void unverifiedConnection() {
191199 void verifiedConnectionWithCorrectServerCertificate () throws Exception {
192200 // in server certificate SAN
193201 String hostname = "localhost" ;
194- SslContext context = SslContextBuilder . forClient ().trustManager (caCertificate ()).build ();
202+ SslContext context = builder ().trustManager (caCertificate ()).build ();
195203 cf .get (new ClientParameters ().host (hostname ).sslContext (context ));
196204 }
197205
@@ -200,25 +208,22 @@ void verifiedConnectionWithCorrectServerCertificateWithSni() throws Exception {
200208 // not in server certificate SAN, but setting SNI makes it work
201209 String hostname = "127.0.0.1" ;
202210 SslContext context =
203- SslContextBuilder .forClient ()
204- .trustManager (caCertificate ())
205- .serverName (new SNIHostName ("localhost" ))
206- .build ();
211+ builder ().trustManager (caCertificate ()).serverName (new SNIHostName ("localhost" )).build ();
207212 cf .get (new ClientParameters ().host (hostname ).sslContext (context ));
208213 }
209214
210215 @ Test
211216 void verifiedConnectionWithCorrectServerCertificateFailsIfHostnameNotInSan () throws Exception {
212217 // not in server certificate SAN
213218 String hostname = "127.0.0.1" ;
214- SslContext context = SslContextBuilder . forClient ().trustManager (caCertificate ()).build ();
219+ SslContext context = builder ().trustManager (caCertificate ()).build ();
215220 assertThatThrownBy (() -> cf .get (new ClientParameters ().host (hostname ).sslContext (context )))
216221 .hasCauseInstanceOf (SSLHandshakeException .class );
217222 }
218223
219224 @ Test
220225 void verifiedConnectionWithWrongServerCertificate () throws Exception {
221- SslContext context = SslContextBuilder . forClient ().trustManager (clientCertificate ()).build ();
226+ SslContext context = builder ().trustManager (clientCertificate ()).build ();
222227 assertThatThrownBy (() -> cf .get (new ClientParameters ().sslContext (context )))
223228 .isInstanceOf (StreamException .class )
224229 .hasCauseInstanceOf (SSLHandshakeException .class );
@@ -227,7 +232,7 @@ void verifiedConnectionWithWrongServerCertificate() throws Exception {
227232 @ Test
228233 void verifiedConnectionWithCorrectClientPrivateKey () throws Exception {
229234 SslContext context =
230- SslContextBuilder . forClient ()
235+ builder ()
231236 .trustManager (caCertificate ())
232237 .keyManager (clientKey (), clientCertificate ())
233238 .build ();
@@ -241,10 +246,7 @@ void verifiedConnectionWithCorrectClientPrivateKey() throws Exception {
241246 void saslExternalShouldSucceedWithUserForClientCertificate () throws Exception {
242247 X509Certificate clientCertificate = clientCertificate ();
243248 SslContext context =
244- SslContextBuilder .forClient ()
245- .trustManager (caCertificate ())
246- .keyManager (clientKey (), clientCertificate )
247- .build ();
249+ builder ().trustManager (caCertificate ()).keyManager (clientKey (), clientCertificate ).build ();
248250
249251 String username = clientCertificate .getSubjectX500Principal ().getName ();
250252 Cli .rabbitmqctlIgnoreError (format ("delete_user %s" , username ));
@@ -268,10 +270,7 @@ void saslExternalShouldSucceedWithUserForClientCertificate() throws Exception {
268270 void saslExternalShouldFailIfNoUserForClientCertificate () throws Exception {
269271 X509Certificate clientCertificate = clientCertificate ();
270272 SslContext context =
271- SslContextBuilder .forClient ()
272- .trustManager (caCertificate ())
273- .keyManager (clientKey (), clientCertificate )
274- .build ();
273+ builder ().trustManager (caCertificate ()).keyManager (clientKey (), clientCertificate ).build ();
275274
276275 String username = clientCertificate .getSubjectX500Principal ().getName ();
277276 Cli .rabbitmqctlIgnoreError (format ("delete_user %s" , username ));
@@ -288,7 +287,7 @@ void saslExternalShouldFailIfNoUserForClientCertificate() throws Exception {
288287
289288 @ Test
290289 void hostnameVerificationShouldFailWhenSettingHostToLoopbackInterface () throws Exception {
291- SslContext context = SslContextBuilder . forClient ().trustManager (caCertificate ()).build ();
290+ SslContext context = builder ().trustManager (caCertificate ()).build ();
292291 assertThatThrownBy (() -> cf .get (new ClientParameters ().sslContext (context ).host ("127.0.0.1" )))
293292 .isInstanceOf (StreamException .class )
294293 .hasCauseInstanceOf (SSLHandshakeException .class );
@@ -298,10 +297,7 @@ void hostnameVerificationShouldFailWhenSettingHostToLoopbackInterface() throws E
298297 void shouldConnectWhenSettingHostToLoopbackInterfaceAndDisablingHostnameVerification ()
299298 throws Exception {
300299 SslContext context =
301- SslContextBuilder .forClient ()
302- .endpointIdentificationAlgorithm (null )
303- .trustManager (caCertificate ())
304- .build ();
300+ builder ().endpointIdentificationAlgorithm (null ).trustManager (caCertificate ()).build ();
305301 cf .get (new ClientParameters ().sslContext (context ).host ("127.0.0.1" ));
306302 }
307303
@@ -325,7 +321,7 @@ void environmentPublisherConsumer() throws Exception {
325321 .uri ("rabbitmq-stream+tls://localhost" )
326322 .addressResolver (addr -> new Address ("localhost" , Client .DEFAULT_TLS_PORT ))
327323 .tls ()
328- .sslContext (SslContextBuilder . forClient ().trustManager (caCertificate ()).build ())
324+ .sslContext (builder ().trustManager (caCertificate ()).build ())
329325 .environmentBuilder ()
330326 .build ()) {
331327
@@ -371,4 +367,8 @@ private static String hostname() {
371367 private static String tlsArtefactPath (String in ) {
372368 return in .replace ("$(hostname)" , hostname ()).replace ("$(hostname -s)" , hostname ());
373369 }
370+
371+ private SslContextBuilder builder () {
372+ return SslContextBuilder .forClient ().sslProvider (sslProvider );
373+ }
374374}
0 commit comments