2626import java .nio .file .Paths ;
2727import java .security .KeyStore ;
2828import java .security .SecureRandom ;
29- import java .util .List ;
3029import javax .net .ssl .KeyManagerFactory ;
3130import javax .net .ssl .SSLContext ;
3231import javax .net .ssl .SSLHandshakeException ;
33- import javax .net .ssl .SSLSocketFactory ;
3432import javax .net .ssl .TrustManagerFactory ;
35- import okhttp3 .ConnectionSpec ;
3633import okhttp3 .OkHttpClient ;
3734import okhttp3 .Request ;
3835import okhttp3 .Response ;
4138import okhttp3 .mockwebserver .MockWebServer ;
4239import okhttp3 .mockwebserver .RecordedRequest ;
4340import org .junit .After ;
44- import org .junit .Ignore ;
4541import org .junit .Rule ;
4642import org .junit .Test ;
47- import org .junit .experimental .theories .DataPoint ;
48- import org .junit .experimental .theories .Theories ;
4943import org .junit .experimental .theories .Theory ;
5044import org .junit .rules .ExpectedException ;
51- import org .junit .runner .RunWith ;
5245import org .sonarsource .scanner .api .internal .cache .Logger ;
5346
5447import static java .lang .String .format ;
5548import static org .assertj .core .api .Assertions .assertThat ;
5649import static org .assertj .core .api .Assertions .assertThatThrownBy ;
57- import static org .assertj . core . api . Assertions .fail ;
50+ import static org .junit . Assert .fail ;
5851import static org .mockito .Mockito .mock ;
5952
60- @ RunWith (Theories .class )
6153public class OkHttpClientFactoryTest {
6254
63- @ DataPoint
64- public static final String KEYSTORE_CLIENT_WITH_CA = "/client-with-ca.jks" ;
65- @ DataPoint
66- public static final String KEYSTORE_CLIENT_WITH_CERTIFICATE = "/client-with-certificate.jks" ;
55+ private static final String KEYSTORE_CLIENT_WITH_CA = "/client-with-ca.p12" ;
56+ private static final String CLIENT_WITH_CA_KEYSTORE_PASSWORD = "pwdClientCAP12" ;
6757
68- private static final String KEYSTORE_PASSWORD = "abcdef" ;
69- private static final String KEYSTORE_FILE = "/server.jks" ;
58+ private static final String KEYSTORE_CLIENT_WITH_CERTIFICATE = "/client-with-certificate.p12" ;
59+ private static final String CLIENT_WITH_CERTIFICATE_KEYSTORE_PASSWORD = "pwdClientP12" ;
60+
61+ private static final String SERVER_KEYSTORE_PASSWORD = "pwdServerP12" ;
62+ private static final String SERVER_KEYSTORE_FILE = "/server.p12" ;
7063 private static final Logger logger = mock (Logger .class );
7164 private static final String SONAR_WS_TIMEOUT = "sonar.ws.timeout" ;
7265 private static final String COOKIE = "BIGipServerpool_sonarqube.example.com_8443=123456789.12345.0000" ;
@@ -79,14 +72,6 @@ public void cleanSystemProperty() {
7972 System .clearProperty (SONAR_WS_TIMEOUT );
8073 }
8174
82- @ Test
83- public void support_tls_versions_of_java8 () {
84- OkHttpClient underTest = OkHttpClientFactory .create (logger );
85-
86- assertTlsAndClearTextSpecifications (underTest );
87- assertThat (underTest .sslSocketFactory ()).isInstanceOf (SSLSocketFactory .getDefault ().getClass ());
88- }
89-
9075 @ Test
9176 public void support_custom_timeouts () {
9277 int readTimeoutSec = 2000 ;
@@ -106,19 +91,6 @@ public void support_custom_timeouts_throws_exception_on_non_number() {
10691 assertThatThrownBy (() -> OkHttpClientFactory .create (logger )).isInstanceOf (NumberFormatException .class );
10792 }
10893
109- private void assertTlsAndClearTextSpecifications (OkHttpClient client ) {
110- List <ConnectionSpec > connectionSpecs = client .connectionSpecs ();
111- assertThat (connectionSpecs ).hasSize (2 );
112-
113- // TLS. tlsVersions()==null means all TLS versions
114- assertThat (connectionSpecs .get (0 ).tlsVersions ()).isNull ();
115- assertThat (connectionSpecs .get (0 ).isTls ()).isTrue ();
116-
117- // HTTP
118- assertThat (connectionSpecs .get (1 ).tlsVersions ()).isNull ();
119- assertThat (connectionSpecs .get (1 ).isTls ()).isFalse ();
120- }
121-
12294 @ Test
12395 public void test_with_external_http_server () throws IOException {
12496 Response response = call ("http://www.google.com" );
@@ -138,7 +110,7 @@ public void when_overriding_truststore_known_websites_are_failing(String clientK
138110 try {
139111 Path clientTruststore = Paths .get (getClass ().getResource (clientKeyStore ).toURI ()).toAbsolutePath ();
140112 System .setProperty ("javax.net.ssl.trustStore" , clientTruststore .toString ());
141- System .setProperty ("javax.net.ssl.trustStorePassword" , KEYSTORE_PASSWORD );
113+ System .setProperty ("javax.net.ssl.trustStorePassword" , SERVER_KEYSTORE_PASSWORD );
142114
143115 expectedException .expect (SSLHandshakeException .class );
144116 call ("https://www.google.com" );
@@ -149,9 +121,18 @@ public void when_overriding_truststore_known_websites_are_failing(String clientK
149121 }
150122 }
151123
152- @ Ignore // ignore to test cirrus QA
153- @ Theory
154- public void test_with_custom_https_server (String clientKeyStore ) throws Exception {
124+ @ Test
125+ public void test_with_custom_https_server_using_ca_in_truststore () throws Exception {
126+ test_with_custom_https_server (KEYSTORE_CLIENT_WITH_CA , CLIENT_WITH_CA_KEYSTORE_PASSWORD );
127+ }
128+
129+ @ Test
130+ public void test_with_custom_https_server_using_server_certificate_in_truststore () throws Exception {
131+ test_with_custom_https_server (KEYSTORE_CLIENT_WITH_CERTIFICATE , CLIENT_WITH_CERTIFICATE_KEYSTORE_PASSWORD );
132+ }
133+
134+ private void test_with_custom_https_server (String clientKeyStore , String keyStorePassword ) throws Exception {
135+ System .setProperty ("javax.net.debug" , "ssl,handshake,record" );
155136 try (MockWebServer server = buildTLSServer ()) {
156137 String url = format ("https://localhost:%d/" , server .getPort ());
157138
@@ -166,7 +147,7 @@ public void test_with_custom_https_server(String clientKeyStore) throws Exceptio
166147 // Add the truststore
167148 Path clientTruststore = Paths .get (getClass ().getResource (clientKeyStore ).toURI ()).toAbsolutePath ();
168149 System .setProperty ("javax.net.ssl.trustStore" , clientTruststore .toString ());
169- System .setProperty ("javax.net.ssl.trustStorePassword" , KEYSTORE_PASSWORD );
150+ System .setProperty ("javax.net.ssl.trustStorePassword" , keyStorePassword );
170151
171152 Response response = call (url );
172153 assertThat (response .code ()).isEqualTo (200 );
@@ -178,22 +159,30 @@ public void test_with_custom_https_server(String clientKeyStore) throws Exceptio
178159 }
179160 }
180161
181- @ Ignore // ignore to test cirrus QA
182- @ Theory
183- public void test_with_cookie (String clientKeyStore ) throws Exception {
162+ @ Test
163+ public void test_with_cookie_using_ca_in_truststore () throws Exception {
164+ test_with_cookie (KEYSTORE_CLIENT_WITH_CA , CLIENT_WITH_CA_KEYSTORE_PASSWORD );
165+ }
166+
167+ @ Test
168+ public void test_with_cookie_using_server_certificate_in_truststore () throws Exception {
169+ test_with_cookie (KEYSTORE_CLIENT_WITH_CERTIFICATE , CLIENT_WITH_CERTIFICATE_KEYSTORE_PASSWORD );
170+ }
171+
172+ private void test_with_cookie (String clientKeyStore , String keyStorePassword ) throws Exception {
184173 try (MockWebServer server = buildTLSServer ()) {
185174 String url = format ("https://localhost:%d/" , server .getPort ());
186175
187176 // Add the truststore
188177 Path clientTruststore = Paths .get (getClass ().getResource (clientKeyStore ).toURI ()).toAbsolutePath ();
189178 System .setProperty ("javax.net.ssl.trustStore" , clientTruststore .toString ());
190- System .setProperty ("javax.net.ssl.trustStorePassword" , KEYSTORE_PASSWORD );
179+ System .setProperty ("javax.net.ssl.trustStorePassword" , keyStorePassword );
191180
192- OkHttpClientFactory .COOKIE_MANAGER .getCookieStore ().removeAll (); // Clear any existing cookies
181+ OkHttpClientFactory .COOKIE_MANAGER .getCookieStore ().removeAll (); // Clear any existing cookies
193182
194183 Response response = call (url );
195- assertThat (response .header ("Set-Cookie" )).isEqualTo (COOKIE ); // The server should have asked us to set a cookie
196- assertThat (response .body ().string ()).doesNotContain (COOKIE );
184+ assertThat (response .header ("Set-Cookie" )).isEqualTo (COOKIE ); // The server should have asked us to set a cookie
185+ assertThat (response .body ().string ()).doesNotContain (COOKIE );
197186
198187 response = call (url );
199188 assertThat (response .body ().string ()).contains (COOKIE );
@@ -205,7 +194,6 @@ public void test_with_cookie(String clientKeyStore) throws Exception {
205194 }
206195 }
207196
208-
209197 private static Response call (String url ) throws IOException {
210198 return OkHttpClientFactory .create (logger ).newCall (
211199 new Request .Builder ()
@@ -240,17 +228,17 @@ public MockResponse dispatch(RecordedRequest request) {
240228 });
241229
242230 // JKS file storing the private key and TLS certificate
243- Path serverCertificate = Paths .get (getClass ().getResource (KEYSTORE_FILE ).toURI ()).toAbsolutePath ();
231+ Path serverCertificate = Paths .get (getClass ().getResource (SERVER_KEYSTORE_FILE ).toURI ()).toAbsolutePath ();
244232
245233 // Load the KeyStore
246- KeyStore serverKeyStore = KeyStore .getInstance (KeyStore . getDefaultType () );
234+ KeyStore serverKeyStore = KeyStore .getInstance ("pkcs12" );
247235 FileInputStream stream = new FileInputStream (serverCertificate .toFile ());
248- serverKeyStore .load (stream , KEYSTORE_PASSWORD .toCharArray ());
236+ serverKeyStore .load (stream , SERVER_KEYSTORE_PASSWORD .toCharArray ());
249237
250238 // Load the KeyManager from the KeyStore
251239 String kmfAlgorithm = KeyManagerFactory .getDefaultAlgorithm ();
252240 KeyManagerFactory kmf = KeyManagerFactory .getInstance (kmfAlgorithm );
253- kmf .init (serverKeyStore , "" .toCharArray ());
241+ kmf .init (serverKeyStore , SERVER_KEYSTORE_PASSWORD .toCharArray ());
254242
255243 // Add the "Keys" (ie. private key and TLS certificate to the TrustManager
256244 TrustManagerFactory trustManagerFactory = TrustManagerFactory .getInstance (kmfAlgorithm );
0 commit comments