Skip to content

Commit abc984b

Browse files
committed
added more test cases
1 parent 4c48f27 commit abc984b

File tree

3 files changed

+112
-36
lines changed

3 files changed

+112
-36
lines changed

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/Apache5HttpClient.java

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -661,19 +661,17 @@ public Builder socketFactory(SSLConnectionSocketFactory socketFactory) {
661661
return this;
662662
}
663663

664+
public void setSocketFactory(SSLConnectionSocketFactory socketFactory) {
665+
socketFactory(socketFactory);
666+
}
667+
664668
@Override
665669
public Builder tlsSocketStrategy(TlsSocketStrategy tlsSocketStrategy) {
666670
this.tlsStrategy = tlsSocketStrategy;
667671
this.legacySocketFactory = null; // Clear any legacy factory
668672
return this;
669673
}
670674

671-
672-
673-
public void setLegacySocketFactory(SSLConnectionSocketFactory legacySocketFactory) {
674-
socketFactory(legacySocketFactory);
675-
}
676-
677675
@Override
678676
public Builder httpRoutePlanner(HttpRoutePlanner httpRoutePlanner) {
679677
this.httpRoutePlanner = httpRoutePlanner;
@@ -744,7 +742,6 @@ public SdkHttpClient buildWithDefaults(AttributeMap serviceDefaults) {
744742
}
745743

746744
// Internal method to get the effective TLS strategy
747-
748745
TlsSocketStrategy getEffectiveTlsStrategy() {
749746
if (tlsStrategy != null) {
750747
return tlsStrategy;
@@ -754,8 +751,6 @@ TlsSocketStrategy getEffectiveTlsStrategy() {
754751
}
755752
return null;
756753
}
757-
758-
759754
}
760755

761756
private static class ApacheConnectionManagerFactory {

http-clients/apache5-client/src/main/java/software/amazon/awssdk/http/apache5/internal/conn/SslSocketFactoryToTlsStrategyAdapter.java

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,9 @@ public SSLSocket upgrade(Socket socket,
4242
Object attachment,
4343
HttpContext context) throws IOException {
4444
Socket layeredSocket = legacySocketFactory.createLayeredSocket(socket, target, port, context);
45-
45+
if (layeredSocket == null) {
46+
throw new IOException("SSLConnectionSocketFactory.createLayeredSocket returned null");
47+
}
4648
if (!(layeredSocket instanceof SSLSocket)) {
4749
throw new IOException("SSLConnectionSocketFactory.createLayeredSocket did not return an SSLSocket. " +
4850
"Returned type: " + layeredSocket.getClass().getName());

http-clients/apache5-client/src/test/java/software/amazon/awssdk/http/apache5/Apache5ClientTlsAuthTest.java

Lines changed: 105 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import static com.github.tomakehurst.wiremock.client.WireMock.urlMatching;
2121
import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
2222
import static org.assertj.core.api.Assertions.assertThat;
23+
import static org.assertj.core.api.Assertions.assertThatExceptionOfType;
2324
import static org.assertj.core.api.Assertions.assertThatThrownBy;
2425
import static software.amazon.awssdk.utils.JavaSystemSetting.SSL_KEY_STORE;
2526
import static software.amazon.awssdk.utils.JavaSystemSetting.SSL_KEY_STORE_PASSWORD;
@@ -56,6 +57,7 @@
5657
import software.amazon.awssdk.http.SdkHttpRequest;
5758
import software.amazon.awssdk.http.TlsKeyManagersProvider;
5859
import software.amazon.awssdk.http.apache5.internal.conn.SdkTlsSocketFactory;
60+
import software.amazon.awssdk.http.apache5.internal.conn.SslSocketFactoryToTlsStrategyAdapter;
5961
import software.amazon.awssdk.internal.http.NoneTlsKeyManagersProvider;
6062

6163
/**
@@ -258,26 +260,28 @@ private HttpExecuteResponse makeRequestWithHttpClient(SdkHttpClient httpClient)
258260
return httpClient.prepareRequest(request).call();
259261
}
260262

261-
262263
@Test
263-
public void build_settingTlsStrategy_configuresClientWithGivenStrategy() throws Exception {
264-
TlsKeyManagersProvider provider = FileStoreTlsKeyManagersProvider.create(clientKeyStore,
265-
CLIENT_STORE_TYPE,
266-
STORE_PASSWORD);
267-
KeyManager[] keyManagers = provider.keyManagers();
268-
264+
public void tls_strategy_configuration() throws Exception {
265+
// Setup TLS context
266+
KeyManager[] keyManagers = keyManagersProvider.keyManagers();
269267
SSLContext sslContext = SSLContext.getInstance("TLS");
270268
sslContext.init(keyManagers, null, null);
271269

272-
SdkTlsSocketFactory socketFactory = new SdkTlsSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
273-
SdkTlsSocketFactory socketFactorySpy = Mockito.spy(socketFactory);
270+
// Create and spy on TlsSocketStrategy
271+
TlsSocketStrategy tlsStrategy = new SdkTlsSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
272+
TlsSocketStrategy tlsStrategySpy = Mockito.spy(tlsStrategy);
274273

274+
// Build client with TLS strategy
275275
client = Apache5HttpClient.builder()
276-
.tlsSocketStrategy(socketFactorySpy) // Modern approach
276+
.tlsSocketStrategy(tlsStrategySpy)
277277
.build();
278-
makeRequestWithHttpClient(client);
279278

280-
Mockito.verify(socketFactorySpy).upgrade(
279+
// Make request and verify
280+
HttpExecuteResponse response = makeRequestWithHttpClient(client);
281+
assertThat(response.httpResponse().isSuccessful()).isTrue();
282+
283+
// Verify upgrade method was called
284+
Mockito.verify(tlsStrategySpy).upgrade(
281285
Mockito.any(Socket.class),
282286
Mockito.anyString(),
283287
Mockito.anyInt(),
@@ -286,36 +290,111 @@ public void build_settingTlsStrategy_configuresClientWithGivenStrategy() throws
286290
);
287291
}
288292

289-
290293
@Test
291-
public void build_settingLegacySocketFactory_configuresClientWithAdapter() throws IOException,
292-
NoSuchAlgorithmException,
293-
KeyManagementException {
294-
TlsKeyManagersProvider provider = FileStoreTlsKeyManagersProvider.create(clientKeyStore,
295-
CLIENT_STORE_TYPE,
296-
STORE_PASSWORD);
297-
KeyManager[] keyManagers = provider.keyManagers();
298-
294+
public void testTlsStrategyOverridesLegacyFactory() throws Exception {
295+
// Setup TLS context
296+
KeyManager[] keyManagers = keyManagersProvider.keyManagers();
299297
SSLContext sslContext = SSLContext.getInstance("TLS");
300298
sslContext.init(keyManagers, null, null);
301299

302-
// Create a legacy SSLConnectionSocketFactory
303-
SSLConnectionSocketFactory legacyFactory = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE);
300+
// Create spies for both approaches
301+
SSLConnectionSocketFactory legacyFactory = new SSLConnectionSocketFactory(
302+
sslContext,
303+
NoopHostnameVerifier.INSTANCE
304+
);
304305
SSLConnectionSocketFactory legacyFactorySpy = Mockito.spy(legacyFactory);
305306

307+
TlsSocketStrategy tlsStrategy = new SdkTlsSocketFactory(
308+
sslContext,
309+
NoopHostnameVerifier.INSTANCE
310+
);
311+
TlsSocketStrategy tlsStrategySpy = Mockito.spy(tlsStrategy);
312+
313+
// Build client with both - TLS strategy should take precedence
306314
client = Apache5HttpClient.builder()
307-
.socketFactory(legacyFactorySpy) // Legacy approach
315+
.socketFactory(legacyFactorySpy)
316+
.tlsSocketStrategy(tlsStrategySpy) // This should override
308317
.build();
309-
makeRequestWithHttpClient(client);
310318

311-
Mockito.verify(legacyFactorySpy).createLayeredSocket(
319+
// Make request
320+
HttpExecuteResponse response = makeRequestWithHttpClient(client);
321+
assertThat(response.httpResponse().isSuccessful()).isTrue();
322+
323+
// Verify only TLS strategy was used, not legacy
324+
Mockito.verify(tlsStrategySpy).upgrade(
312325
Mockito.any(Socket.class),
313326
Mockito.anyString(),
314327
Mockito.anyInt(),
328+
Mockito.any(),
315329
Mockito.any(HttpContext.class)
316330
);
331+
332+
Mockito.verifyNoInteractions(legacyFactorySpy);
333+
}
334+
335+
@Test
336+
public void testAdapterConvertsNonSslSocketException() throws Exception {
337+
// Create mock that returns a regular Socket (not SSLSocket)
338+
SSLConnectionSocketFactory mockFactory = Mockito.mock(SSLConnectionSocketFactory.class);
339+
Socket nonSslSocket = Mockito.mock(Socket.class);
340+
341+
// Setup mock to return non-SSL socket
342+
Mockito.when(mockFactory.createLayeredSocket(
343+
Mockito.any(Socket.class),
344+
Mockito.eq("example.com"),
345+
Mockito.eq(443),
346+
Mockito.any()
347+
)).thenReturn(nonSslSocket);
348+
349+
// Create adapter
350+
SslSocketFactoryToTlsStrategyAdapter adapter =
351+
new SslSocketFactoryToTlsStrategyAdapter(mockFactory);
352+
353+
// Test should throw IOException
354+
Socket plainSocket = Mockito.mock(Socket.class);
355+
356+
assertThatExceptionOfType(IOException.class)
357+
.isThrownBy(() -> adapter.upgrade(plainSocket, "example.com", 443, null, null))
358+
.withMessageContaining("did not return an SSLSocket")
359+
.withMessageContaining(nonSslSocket.getClass().getName());
317360
}
318361

319362

363+
@Test
364+
public void testAdapterHandlesNullSocket() throws Exception {
365+
// Create mock that returns null
366+
SSLConnectionSocketFactory mockFactory = Mockito.mock(SSLConnectionSocketFactory.class);
367+
368+
Mockito.when(mockFactory.createLayeredSocket(
369+
Mockito.any(Socket.class),
370+
Mockito.anyString(),
371+
Mockito.anyInt(),
372+
Mockito.any(HttpContext.class)
373+
)).thenReturn(null);
374+
375+
// Create adapter
376+
SslSocketFactoryToTlsStrategyAdapter adapter =
377+
new SslSocketFactoryToTlsStrategyAdapter(mockFactory);
378+
379+
// Test should throw IOException
380+
Socket plainSocket = Mockito.mock(Socket.class);
381+
382+
assertThatExceptionOfType(IOException.class)
383+
.isThrownBy(() -> adapter.upgrade(plainSocket, "example.com", 443, null, null))
384+
.withMessageContaining("returned null");
385+
}
386+
387+
@Test
388+
public void testNullTlsStrategyFallsBackToDefault() throws Exception {
389+
// Test that setting tlsSocketStrategy(null) works correctly
390+
client = Apache5HttpClient.builder()
391+
.tlsSocketStrategy(null)
392+
.tlsKeyManagersProvider(keyManagersProvider)
393+
.build();
394+
395+
HttpExecuteResponse response = makeRequestWithHttpClient(client);
396+
assertThat(response.httpResponse().isSuccessful()).isTrue();
397+
}
398+
320399

321400
}

0 commit comments

Comments
 (0)