Skip to content

Instantly share code, notes, and snippets.

@johanhammar
Created March 24, 2021 07:50
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save johanhammar/9d73a9db10784b704232b74ad2378dd2 to your computer and use it in GitHub Desktop.
Save johanhammar/9d73a9db10784b704232b74ad2378dd2 to your computer and use it in GitHub Desktop.
...
@Test
public void testSetSslEndpointIdentificationAlgorithm() throws Exception {
RestService restService = new RestService("https://localhost:8081");
Map<String, Object> configs = new HashMap<>();
configs.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "");
restService.configure(configs);
HostnameVerifier hostnameVerifier = getHostnameVerifier(restService);
SSLSession sslSession = createMock(SSLSession.class);
assertNotNull(hostnameVerifier);
assertTrue(hostnameVerifier.verify("hostname", sslSession));
}
@Test
public void testSetHttpsSslEndpointIdentificationAlgorithm() throws Exception {
RestService restService = new RestService("https://localhost:8081");
Map<String, Object> configs = new HashMap<>();
configs.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "https");
restService.configure(configs);
HostnameVerifier hostnameVerifier = getHostnameVerifier(restService);
assertNull(hostnameVerifier);
}
@Test
public void testNoConfiguredSslEndpointIdentificationAlgorithm() throws Exception {
RestService restService = new RestService("https://localhost:8081");
HostnameVerifier hostnameVerifier = getHostnameVerifier(restService);
assertNull(hostnameVerifier);
}
@Test(expected = ConfigException.class)
public void testSetInvalidSslEndpointIdentificationAlgorithm() throws Exception {
RestService restService = new RestService("https://localhost:8081");
Map<String, Object> configs = new HashMap<>();
configs.put(SslConfigs.SSL_ENDPOINT_IDENTIFICATION_ALGORITHM_CONFIG, "invalid");
restService.configure(configs);
}
private HostnameVerifier getHostnameVerifier(RestService restService) throws Exception {
Field hostnameVerifierField = RestService.class.getDeclaredField("hostnameVerifier");
boolean accessible = hostnameVerifierField.isAccessible();
hostnameVerifierField.setAccessible(true);
try {
return (HostnameVerifier) hostnameVerifierField.get(restService);
} finally {
hostnameVerifierField.setAccessible(accessible);
}
}
...
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment