1818
1919package org .apache .zookeeper .common ;
2020
21+ import static org .apache .zookeeper .common .X509Util .FIPS_MODE_PROPERTY ;
22+ import static org .apache .zookeeper .common .X509Util .TLS_1_2 ;
23+ import static org .apache .zookeeper .common .X509Util .TLS_1_3 ;
2124import static org .junit .jupiter .api .Assertions .assertArrayEquals ;
2225import static org .junit .jupiter .api .Assertions .assertEquals ;
2326import static org .junit .jupiter .api .Assertions .assertThrows ;
@@ -90,6 +93,7 @@ public void cleanUp() {
9093 System .clearProperty (x509Util .getSslHandshakeDetectionTimeoutMillisProperty ());
9194 System .clearProperty (ServerCnxnFactory .ZOOKEEPER_SERVER_CNXN_FACTORY );
9295 System .clearProperty (ZKClientConfig .ZOOKEEPER_CLIENT_CNXN_SOCKET );
96+ System .clearProperty (FIPS_MODE_PROPERTY );
9397 x509Util .close ();
9498 }
9599
@@ -100,24 +104,36 @@ public void testCreateSSLContextWithoutCustomProtocol(
100104 X509KeyType caKeyType , X509KeyType certKeyType , String keyPassword , Integer paramIndex )
101105 throws Exception {
102106 init (caKeyType , certKeyType , keyPassword , paramIndex );
107+ System .setProperty (FIPS_MODE_PROPERTY , Boolean .FALSE .toString ());
103108 SSLContext sslContext = x509Util .getDefaultSSLContext ();
104- assertEquals (X509Util .DEFAULT_PROTOCOL , sslContext .getProtocol ());
109+ String defaultTlsProtocol = X509Util .defaultTlsProtocol (new ZKConfig ());
110+ assertEquals (defaultTlsProtocol , sslContext .getProtocol ());
105111
106- // Check that TLSv1.3 is selected in JDKs that support it (OpenJDK 8u272 and later).
107112 List <String > supported = Arrays .asList (SSLContext .getDefault ().getSupportedSSLParameters ().getProtocols ());
108- if (supported .contains (X509Util .TLS_1_3 )) {
109- // SSLContext protocol.
110- assertEquals (X509Util .TLS_1_3 , sslContext .getProtocol ());
111- // Enabled protocols.
113+ if (supported .contains (TLS_1_3 )) {
114+ assertEquals (TLS_1_3 , sslContext .getProtocol ());
112115 List <String > protos = Arrays .asList (sslContext .getDefaultSSLParameters ().getProtocols ());
113- assertTrue (protos .contains (X509Util . TLS_1_2 ));
114- assertTrue (protos .contains (X509Util . TLS_1_3 ));
116+ assertTrue (protos .contains (TLS_1_2 ));
117+ assertTrue (protos .contains (TLS_1_3 ));
115118 } else {
116- assertEquals (X509Util . TLS_1_2 , sslContext .getProtocol ());
117- assertArrayEquals (new String []{X509Util . TLS_1_2 }, sslContext .getDefaultSSLParameters ().getProtocols ());
119+ assertEquals (TLS_1_2 , sslContext .getProtocol ());
120+ assertArrayEquals (new String []{TLS_1_2 }, sslContext .getDefaultSSLParameters ().getProtocols ());
118121 }
119122 }
120123
124+ @ ParameterizedTest
125+ @ MethodSource ("data" )
126+ @ Timeout (value = 5 )
127+ public void testCreateSSLContextWithoutCustomProtocol_FIPSEnabled (
128+ X509KeyType caKeyType , X509KeyType certKeyType , String keyPassword , Integer paramIndex )
129+ throws Exception {
130+ init (caKeyType , certKeyType , keyPassword , paramIndex );
131+ System .setProperty (FIPS_MODE_PROPERTY , Boolean .TRUE .toString ());
132+ SSLContext sslContext = x509Util .getDefaultSSLContext ();
133+ assertEquals (TLS_1_2 , sslContext .getProtocol ());
134+ assertArrayEquals (new String []{TLS_1_2 }, sslContext .getDefaultSSLParameters ().getProtocols ());
135+ }
136+
121137 @ ParameterizedTest
122138 @ MethodSource ("data" )
123139 @ Timeout (value = 5 )
@@ -873,4 +889,5 @@ private void testCreateSSLContext_withWrongPasswordFromFile(final String keyPass
873889 x509Util .getDefaultSSLContext ();
874890 });
875891 }
892+
876893}
0 commit comments