use of javax.net.ssl.SSLSocketFactory in project robovm by robovm.
the class URLConnectionTest method testHttpsWithCustomTrustManager.
public void testHttpsWithCustomTrustManager() throws Exception {
RecordingHostnameVerifier hostnameVerifier = new RecordingHostnameVerifier();
RecordingTrustManager trustManager = new RecordingTrustManager();
SSLContext sc = SSLContext.getInstance("TLS");
sc.init(null, new TrustManager[] { trustManager }, new java.security.SecureRandom());
HostnameVerifier defaultHostnameVerifier = HttpsURLConnection.getDefaultHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hostnameVerifier);
SSLSocketFactory defaultSSLSocketFactory = HttpsURLConnection.getDefaultSSLSocketFactory();
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
try {
TestSSLContext testSSLContext = TestSSLContext.create();
server.useHttps(testSSLContext.serverContext.getSocketFactory(), false);
server.enqueue(new MockResponse().setBody("ABC"));
server.enqueue(new MockResponse().setBody("DEF"));
server.enqueue(new MockResponse().setBody("GHI"));
server.play();
URL url = server.getUrl("/");
assertEquals("ABC", readAscii(url.openStream(), Integer.MAX_VALUE));
assertEquals("DEF", readAscii(url.openStream(), Integer.MAX_VALUE));
assertEquals("GHI", readAscii(url.openStream(), Integer.MAX_VALUE));
assertEquals(Arrays.asList("verify " + hostName), hostnameVerifier.calls);
assertEquals(Arrays.asList("checkServerTrusted [" + "CN=" + hostName + " 1, " + "CN=Test Intermediate Certificate Authority 1, " + "CN=Test Root Certificate Authority 1" + "] RSA"), trustManager.calls);
} finally {
HttpsURLConnection.setDefaultHostnameVerifier(defaultHostnameVerifier);
HttpsURLConnection.setDefaultSSLSocketFactory(defaultSSLSocketFactory);
}
}
use of javax.net.ssl.SSLSocketFactory in project robovm by robovm.
the class HttpsURLConnectionTest method testSetSSLSocketFactory.
/**
* Tests possibility to set up the SSLSocketFactory
* to be used by HttpsURLConnection.
*/
public void testSetSSLSocketFactory() throws Throwable {
// create the SSLServerSocket which will be used by server side
SSLContext ctx = getContext();
SSLServerSocket ss = (SSLServerSocket) ctx.getServerSocketFactory().createServerSocket(0);
// create the HostnameVerifier to check hostname verification
TestHostnameVerifier hnv = new TestHostnameVerifier();
HttpsURLConnection.setDefaultHostnameVerifier(hnv);
// create HttpsURLConnection to be tested
URL url = new URL("https://localhost:" + ss.getLocalPort());
HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
SSLSocketFactory socketFactory = (SSLSocketFactory) ctx.getSocketFactory();
connection.setSSLSocketFactory(socketFactory);
TestHostnameVerifier hnv_late = new TestHostnameVerifier();
// late initialization: should not be used for created connection
HttpsURLConnection.setDefaultHostnameVerifier(hnv_late);
// perform the interaction between the peers
SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss);
// check the connection state
checkConnectionStateParameters(connection, peerSocket);
// check the verification process
assertTrue("Hostname verification was not done", hnv.verified);
assertFalse("Hostname verification should not be done by this verifier", hnv_late.verified);
// check the used SSLSocketFactory
assertNotSame("Default SSLSocketFactory should not be used", HttpsURLConnection.getDefaultSSLSocketFactory(), connection.getSSLSocketFactory());
assertSame("Result differs from expected", socketFactory, connection.getSSLSocketFactory());
// should silently exit
connection.connect();
}
use of javax.net.ssl.SSLSocketFactory in project robovm by robovm.
the class SSLSocketFactoryTest method test_Constructor.
/**
* javax.net.ssl.SSLSocketFactory#SSLSocketFactory()
*/
public void test_Constructor() {
try {
SocketFactory sf = SSLSocketFactory.getDefault();
assertTrue(sf instanceof SSLSocketFactory);
} catch (Exception e) {
fail("Unexpected exception " + e.toString());
}
}
use of javax.net.ssl.SSLSocketFactory in project robovm by robovm.
the class SSLSocketFactoryTest method test_getSupportedCipherSuites.
/**
* javax.net.ssl.SSLSocketFactory#getSupportedCipherSuites()
*/
public void test_getSupportedCipherSuites() {
try {
SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
assertTrue("no supported cipher suites returned", sf.getSupportedCipherSuites().length > 0);
} catch (Exception e) {
fail("Unexpected exception " + e.toString());
}
}
use of javax.net.ssl.SSLSocketFactory in project robovm by robovm.
the class SSLSocketTest method test_SSLSocket_getEnabledProtocols.
public void test_SSLSocket_getEnabledProtocols() throws Exception {
SSLSocketFactory sf = (SSLSocketFactory) SSLSocketFactory.getDefault();
SSLSocket ssl = (SSLSocket) sf.createSocket();
String[] protocols = ssl.getEnabledProtocols();
StandardNames.assertValidProtocols(StandardNames.SSL_SOCKET_PROTOCOLS, protocols);
assertNotSame(protocols, ssl.getEnabledProtocols());
}
Aggregations