Search in sources :

Example 71 with SSLSocket

use of javax.net.ssl.SSLSocket 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();
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLContext(javax.net.ssl.SSLContext) SSLServerSocket(javax.net.ssl.SSLServerSocket) SSLSocketFactory(javax.net.ssl.SSLSocketFactory) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 72 with SSLSocket

use of javax.net.ssl.SSLSocket in project robovm by robovm.

the class HttpsURLConnectionTest method testProxyConnection.

/**
     * Tests HTTPS connection process made through the proxy server.
     */
public void testProxyConnection() throws Throwable {
    // setting up the properties pointing to the key/trust stores
    setUpStoreProperties();
    // create the SSLServerSocket which will be used by server side
    ServerSocket ss = new ServerSocket(0);
    // create the HostnameVerifier to check that Hostname verification
    // is done
    TestHostnameVerifier hnv = new TestHostnameVerifier();
    HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    // create HttpsURLConnection to be tested
    URL url = new URL("https://requested.host:55556/requested.data");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    // perform the interaction between the peers and check the results
    SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss);
    checkConnectionStateParameters(connection, peerSocket);
    // should silently exit
    connection.connect();
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 73 with SSLSocket

use of javax.net.ssl.SSLSocket in project robovm by robovm.

the class HttpsURLConnectionTest method testProxyAuthConnection_doOutput.

/**
     * Tests HTTPS connection process made through the proxy server.
     * Proxy server needs authentication.
     * Client sends data to the server.
     */
public void testProxyAuthConnection_doOutput() throws Throwable {
    // setting up the properties pointing to the key/trust stores
    setUpStoreProperties();
    // create the SSLServerSocket which will be used by server side
    ServerSocket ss = new ServerSocket(0);
    // create the HostnameVerifier to check that Hostname verification
    // is done
    TestHostnameVerifier hnv = new TestHostnameVerifier();
    HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    Authenticator.setDefault(new Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user", "password".toCharArray());
        }
    });
    // create HttpsURLConnection to be tested
    URL url = new URL("https://requested.host:55554/requested.data");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    connection.setDoOutput(true);
    // perform the interaction between the peers and check the results
    SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss, OK_CODE, true);
    checkConnectionStateParameters(connection, peerSocket);
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) Authenticator(java.net.Authenticator) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Example 74 with SSLSocket

use of javax.net.ssl.SSLSocket in project robovm by robovm.

the class HttpsURLConnectionTest method testConsequentProxyConnection.

/**
     * Tests HTTPS connection process made through the proxy server.
     * 2 HTTPS connections are opened for one URL. For the first time
     * the connection is opened through one proxy,
     * for the second time through another.
     */
public void testConsequentProxyConnection() throws Throwable {
    // setting up the properties pointing to the key/trust stores
    setUpStoreProperties();
    // create the SSLServerSocket which will be used by server side
    ServerSocket ss = new ServerSocket(0);
    // create the HostnameVerifier to check that Hostname verification
    // is done
    TestHostnameVerifier hnv = new TestHostnameVerifier();
    HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    // create HttpsURLConnection to be tested
    URL url = new URL("https://requested.host:55555/requested.data");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    // perform the interaction between the peers and check the results
    SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss);
    checkConnectionStateParameters(connection, peerSocket);
    // create another SSLServerSocket which will be used by server side
    ss = new ServerSocket(0);
    connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    // perform the interaction between the peers and check the results
    peerSocket = (SSLSocket) doInteraction(connection, ss);
    checkConnectionStateParameters(connection, peerSocket);
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 75 with SSLSocket

use of javax.net.ssl.SSLSocket in project robovm by robovm.

the class HttpsURLConnectionTest method testProxyAuthConnection.

/**
     * Tests HTTPS connection process made through the proxy server.
     * Proxy server needs authentication.
     */
public void testProxyAuthConnection() throws Throwable {
    // setting up the properties pointing to the key/trust stores
    setUpStoreProperties();
    // create the SSLServerSocket which will be used by server side
    ServerSocket ss = new ServerSocket(0);
    // create the HostnameVerifier to check that Hostname verification
    // is done
    TestHostnameVerifier hnv = new TestHostnameVerifier();
    HttpsURLConnection.setDefaultHostnameVerifier(hnv);
    Authenticator.setDefault(new Authenticator() {

        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication("user", "password".toCharArray());
        }
    });
    // create HttpsURLConnection to be tested
    URL url = new URL("https://requested.host:55555/requested.data");
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    // perform the interaction between the peers and check the results
    SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss);
    checkConnectionStateParameters(connection, peerSocket);
    // should silently exit
    connection.connect();
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) Authenticator(java.net.Authenticator) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection) PasswordAuthentication(java.net.PasswordAuthentication)

Aggregations

SSLSocket (javax.net.ssl.SSLSocket)326 IOException (java.io.IOException)101 Test (org.junit.Test)62 SSLContext (javax.net.ssl.SSLContext)59 SSLSocketFactory (javax.net.ssl.SSLSocketFactory)59 Socket (java.net.Socket)57 OutputStream (java.io.OutputStream)50 InetSocketAddress (java.net.InetSocketAddress)39 CertificateException (java.security.cert.CertificateException)33 SSLException (javax.net.ssl.SSLException)32 SSLSession (javax.net.ssl.SSLSession)31 InputStream (java.io.InputStream)30 SSLPeerUnverifiedException (javax.net.ssl.SSLPeerUnverifiedException)30 SSLServerSocket (javax.net.ssl.SSLServerSocket)27 SocketTimeoutException (java.net.SocketTimeoutException)24 SocketException (java.net.SocketException)23 ServerSocket (java.net.ServerSocket)22 UnknownHostException (java.net.UnknownHostException)21 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)21 InputStreamReader (java.io.InputStreamReader)19