Skip to content

Commit 68ef9de

Browse files
slandelleslandelle
slandelle
authored and
slandelle
committed
Make BasicHttpsTest threadsafe!
1 parent 3ab0c60 commit 68ef9de

File tree

1 file changed

+37
-40
lines changed

1 file changed

+37
-40
lines changed

api/src/test/java/org/asynchttpclient/async/BasicHttpsTest.java

Lines changed: 37 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -207,7 +207,7 @@ public void setUpGlobal() throws Exception {
207207
@Test(groups = { "standalone", "default_provider" })
208208
public void zeroCopyPostTest() throws Throwable {
209209

210-
final AsyncHttpClient client = getAsyncHttpClient(new Builder().setSSLContext(createSSLContext()).build());
210+
final AsyncHttpClient client = getAsyncHttpClient(new Builder().setSSLContext(createSSLContext(new AtomicBoolean(true))).build());
211211
try {
212212
ClassLoader cl = getClass().getClassLoader();
213213
// override system properties
@@ -226,7 +226,7 @@ public void zeroCopyPostTest() throws Throwable {
226226

227227
@Test(groups = { "standalone", "default_provider" })
228228
public void multipleSSLRequestsTest() throws Throwable {
229-
final AsyncHttpClient c = getAsyncHttpClient(new Builder().setSSLContext(createSSLContext()).build());
229+
final AsyncHttpClient c = getAsyncHttpClient(new Builder().setSSLContext(createSSLContext(new AtomicBoolean(true))).build());
230230
try {
231231
String body = "hello there";
232232

@@ -246,7 +246,7 @@ public void multipleSSLRequestsTest() throws Throwable {
246246

247247
@Test(groups = { "standalone", "default_provider" })
248248
public void multipleSSLWithoutCacheTest() throws Throwable {
249-
final AsyncHttpClient c = getAsyncHttpClient(new Builder().setSSLContext(createSSLContext()).setAllowSslConnectionPool(false).build());
249+
final AsyncHttpClient c = getAsyncHttpClient(new Builder().setSSLContext(createSSLContext(new AtomicBoolean(true))).setAllowSslConnectionPool(false).build());
250250
try {
251251
String body = "hello there";
252252
c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute();
@@ -263,40 +263,36 @@ public void multipleSSLWithoutCacheTest() throws Throwable {
263263

264264
@Test(groups = { "standalone", "default_provider" })
265265
public void reconnectsAfterFailedCertificationPath() throws Throwable {
266-
final AsyncHttpClient c = getAsyncHttpClient(new Builder().setSSLContext(createSSLContext()).build());
266+
AtomicBoolean trusted = new AtomicBoolean(false);
267+
final AsyncHttpClient c = getAsyncHttpClient(new Builder().setSSLContext(createSSLContext(trusted)).build());
267268
try {
268269
final String body = "hello there";
269270

270-
TRUST_SERVER_CERT.set(false);
271+
// first request fails because server certificate is rejected
271272
try {
272-
// first request fails because server certificate is rejected
273-
try {
274-
c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS);
275-
} catch (final ExecutionException e) {
276-
Throwable cause = e.getCause();
277-
if (cause instanceof ConnectException) {
278-
assertNotNull(cause.getCause());
279-
assertTrue(cause.getCause() instanceof SSLHandshakeException);
280-
} else {
281-
assertTrue(cause instanceof SSLHandshakeException);
282-
}
273+
c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS);
274+
} catch (final ExecutionException e) {
275+
Throwable cause = e.getCause();
276+
if (cause instanceof ConnectException) {
277+
assertNotNull(cause.getCause());
278+
assertTrue(cause.getCause() instanceof SSLHandshakeException);
279+
} else {
280+
assertTrue(cause instanceof SSLHandshakeException);
283281
}
282+
}
284283

285-
TRUST_SERVER_CERT.set(true);
284+
trusted.set(true);
286285

287-
// second request should succeed
288-
final Response response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS);
286+
// second request should succeed
287+
final Response response = c.preparePost(getTargetUrl()).setBody(body).setHeader("Content-Type", "text/html").execute().get(TIMEOUT, TimeUnit.SECONDS);
289288

290-
assertEquals(response.getResponseBody(), body);
291-
} finally {
292-
TRUST_SERVER_CERT.set(true);
293-
}
289+
assertEquals(response.getResponseBody(), body);
294290
} finally {
295291
c.close();
296292
}
297293
}
298294

299-
private static SSLContext createSSLContext() {
295+
private static SSLContext createSSLContext(AtomicBoolean trusted) {
300296
try {
301297
InputStream keyStoreStream = BasicHttpsTest.class.getResourceAsStream("ssltest-cacerts.jks");
302298
char[] keyStorePassword = "changeit".toCharArray();
@@ -310,7 +306,7 @@ private static SSLContext createSSLContext() {
310306

311307
// Initialize the SSLContext to work with our key managers.
312308
KeyManager[] keyManagers = kmf.getKeyManagers();
313-
TrustManager[] trustManagers = new TrustManager[] { DUMMY_TRUST_MANAGER };
309+
TrustManager[] trustManagers = new TrustManager[] { dummyTrustManager(trusted) };
314310
SecureRandom secureRandom = new SecureRandom();
315311

316312
SSLContext sslContext = SSLContext.getInstance("TLS");
@@ -322,20 +318,21 @@ private static SSLContext createSSLContext() {
322318
}
323319
}
324320

325-
private static final AtomicBoolean TRUST_SERVER_CERT = new AtomicBoolean(true);
326-
private static final TrustManager DUMMY_TRUST_MANAGER = new X509TrustManager() {
327-
public X509Certificate[] getAcceptedIssuers() {
328-
return new X509Certificate[0];
329-
}
330-
331-
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
332-
}
333-
334-
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
335-
if (!TRUST_SERVER_CERT.get()) {
336-
throw new CertificateException("Server certificate not trusted.");
337-
}
338-
}
339-
};
321+
private static final TrustManager dummyTrustManager(final AtomicBoolean trusted) {
322+
return new X509TrustManager() {
323+
public X509Certificate[] getAcceptedIssuers() {
324+
return new X509Certificate[0];
325+
}
326+
327+
public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
328+
}
329+
330+
public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
331+
if (!trusted.get()) {
332+
throw new CertificateException("Server certificate not trusted.");
333+
}
334+
}
335+
};
336+
}
340337

341338
}

0 commit comments

Comments
 (0)