2020import nl .altindag .ssl .util .KeyStoreUtils ;
2121import nl .altindag .ssl .util .SSLSessionUtils ;
2222import nl .altindag .ssl .util .TrustManagerUtils ;
23+ import org .junit .jupiter .api .AfterAll ;
24+ import org .junit .jupiter .api .AfterEach ;
2325import org .junit .jupiter .api .BeforeAll ;
26+ import org .junit .jupiter .api .BeforeEach ;
2427import org .junit .jupiter .api .MethodOrderer ;
2528import org .junit .jupiter .api .Order ;
2629import org .junit .jupiter .api .Test ;
@@ -46,6 +49,7 @@ class HotSwappableX509ExtendedTrustManagerIT {
4649 private static SSLSocketFactory sslSocketFactory ;
4750 private static SSLSessionContext sslSessionContext ;
4851 private static X509ExtendedTrustManager trustManager ;
52+ private static Server server ;
4953
5054 @ BeforeAll
5155 static void setUpSSLSocketFactory () {
@@ -60,54 +64,47 @@ static void setUpSSLSocketFactory() {
6064
6165 sslSocketFactory = sslFactory .getSslSocketFactory ();
6266 sslSessionContext = sslFactory .getSslContext ().getClientSessionContext ();
63- }
6467
65- @ Test
66- @ Order (1 )
67- void executeHttpsRequestWithSslSocketFactoryContainingTrustManager () throws IOException {
6868 SSLFactory sslFactoryForServer = SSLFactory .builder ()
6969 .withIdentityMaterial ("keystore/client-server/server-one/identity.jks" , "secret" .toCharArray ())
7070 .withTrustMaterial ("keystore/client-server/server-one/truststore.jks" , "secret" .toCharArray ())
7171 .withNeedClientAuthentication ()
7272 .build ();
7373
74- Server server = Server .createDefault (sslFactoryForServer , 7985 );
74+ server = Server .createDefault (sslFactoryForServer , 7000 );
75+ }
7576
76- HttpsURLConnection connection = (HttpsURLConnection ) new URL ("https://localhost:7985/api/hello" ).openConnection ();
77+ @ AfterAll
78+ static void stopServer () {
79+ server .stop ();
80+ }
81+
82+ @ Test
83+ @ Order (1 )
84+ void executeHttpsRequestWithSslSocketFactoryContainingTrustManager () throws IOException {
85+ HttpsURLConnection connection = (HttpsURLConnection ) new URL ("https://localhost:7000/api/hello" ).openConnection ();
7786 connection .setSSLSocketFactory (sslSocketFactory );
7887 connection .setRequestMethod ("GET" );
7988
8089 int statusCode = connection .getResponseCode ();
8190 connection .disconnect ();
8291
8392 assertThat (statusCode ).isEqualTo (200 );
84-
85- server .stop ();
8693 }
8794
8895 @ Test
8996 @ Order (2 )
9097 void executeHttpsRequestWithExistingSslSocketFactoryContainingASwappedUnsafeTrustManager () throws IOException {
91- SSLFactory sslFactoryForServer = SSLFactory .builder ()
92- .withIdentityMaterial ("keystore/client-server/server-one/identity.jks" , "secret" .toCharArray ())
93- .withTrustMaterial ("keystore/client-server/server-one/truststore.jks" , "secret" .toCharArray ())
94- .withNeedClientAuthentication ()
95- .build ();
96-
97- Server server = Server .createDefault (sslFactoryForServer , 7985 );
98-
9998 KeyStore trustStoreWithClientTwo = KeyStoreUtils .loadKeyStore ("keystore/client-server/client-two/truststore.jks" , "secret" .toCharArray ());
10099 X509ExtendedTrustManager trustManagerWithClientTwo = TrustManagerUtils .createTrustManager (trustStoreWithClientTwo );
101100 TrustManagerUtils .swapTrustManager (trustManager , trustManagerWithClientTwo );
102101 SSLSessionUtils .invalidateCaches (sslSessionContext );
103102
104- HttpsURLConnection connection = (HttpsURLConnection ) new URL ("https://localhost:7985 /api/hello" ).openConnection ();
103+ HttpsURLConnection connection = (HttpsURLConnection ) new URL ("https://localhost:7000 /api/hello" ).openConnection ();
105104 connection .setSSLSocketFactory (sslSocketFactory );
106105 connection .setRequestMethod ("GET" );
107106
108107 assertThatThrownBy (connection ::getResponseCode );
109-
110- server .stop ();
111108 }
112109
113110}
0 commit comments