Search in sources :

Example 46 with SSLServerSocket

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

the class HttpsURLConnectionTest method testSetDefaultSSLSocketFactory.

/**
     * Tests possibility to set up the default SSLSocketFactory
     * to be used by HttpsURLConnection.
     */
public void testSetDefaultSSLSocketFactory() throws Throwable {
    // create the SSLServerSocket which will be used by server side
    SSLContext ctx = getContext();
    SSLServerSocket ss = (SSLServerSocket) ctx.getServerSocketFactory().createServerSocket(0);
    SSLSocketFactory socketFactory = (SSLSocketFactory) ctx.getSocketFactory();
    // set up the factory as default
    HttpsURLConnection.setDefaultSSLSocketFactory(socketFactory);
    // check the result
    assertSame("Default SSLSocketFactory differs from expected", socketFactory, HttpsURLConnection.getDefaultSSLSocketFactory());
    // 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();
    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
    assertSame("Default SSLSocketFactory should be used", HttpsURLConnection.getDefaultSSLSocketFactory(), 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 47 with SSLServerSocket

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

the class HttpsURLConnectionTest method testProxyAuthConnectionFailed.

/**
     * Tests HTTPS connection process made through the proxy server.
     * Proxy server needs authentication but client fails to authenticate
     * (Authenticator was not set up in the system).
     */
public void testProxyAuthConnectionFailed() 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
    try {
        doInteraction(connection, ss, AUTHENTICATION_REQUIRED_CODE, true);
    } catch (IOException e) {
        // SSL Tunnelling failed
        if (DO_LOG) {
            System.out.println("Got expected IOException: " + e.getMessage());
        }
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) IOException(java.io.IOException) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 48 with SSLServerSocket

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

the class HttpsURLConnectionTest method testProxyConnection_Not_Found_Response.

/**
     * Tests the behaviour of HTTPS connection in case of unavailability
     * of requested resource.
     */
public void testProxyConnection_Not_Found_Response() 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://localhost:" + ss.getLocalPort());
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("localhost", ss.getLocalPort())));
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    try {
        // NOT FOUND
        doInteraction(connection, ss, NOT_FOUND_CODE);
        fail("Expected exception was not thrown.");
    } catch (FileNotFoundException e) {
        if (DO_LOG) {
            System.out.println("Expected exception was thrown: " + e.getMessage());
        }
    }
}
Also used : Proxy(java.net.Proxy) InetSocketAddress(java.net.InetSocketAddress) FileNotFoundException(java.io.FileNotFoundException) ServerSocket(java.net.ServerSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 49 with SSLServerSocket

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

the class HttpsURLConnectionTest method testSetHostnameVerifier.

/**
     * Tests if setHostnameVerifier() method replaces default verifier.
     */
public void testSetHostnameVerifier() throws Throwable {
    // setting up the properties pointing to the key/trust stores
    setUpStoreProperties();
    // create the SSLServerSocket which will be used by server side
    SSLServerSocket ss = (SSLServerSocket) getContext().getServerSocketFactory().createServerSocket(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://localhost:" + ss.getLocalPort());
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    TestHostnameVerifier hnv_late = new TestHostnameVerifier();
    // replace default verifier
    connection.setHostnameVerifier(hnv_late);
    // perform the interaction between the peers and check the results
    SSLSocket peerSocket = (SSLSocket) doInteraction(connection, ss);
    assertTrue("Hostname verification was not done", hnv_late.verified);
    assertFalse("Hostname verification should not be done by this verifier", hnv.verified);
    checkConnectionStateParameters(connection, peerSocket);
    // should silently exit
    connection.connect();
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Example 50 with SSLServerSocket

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

the class HttpsURLConnectionTest method test_doOutput.

/**
     * Tests the behaviour in case of sending the data to the server.
     */
public void test_doOutput() throws Throwable {
    // setting up the properties pointing to the key/trust stores
    setUpStoreProperties();
    // create the SSLServerSocket which will be used by server side
    SSLServerSocket ss = (SSLServerSocket) getContext().getServerSocketFactory().createServerSocket(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://localhost:" + ss.getLocalPort());
    HttpsURLConnection connection = (HttpsURLConnection) url.openConnection();
    connection.setSSLSocketFactory(getContext().getSocketFactory());
    connection.setDoOutput(true);
    // 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 : SSLSocket(javax.net.ssl.SSLSocket) SSLServerSocket(javax.net.ssl.SSLServerSocket) URL(java.net.URL) HttpsURLConnection(javax.net.ssl.HttpsURLConnection)

Aggregations

SSLServerSocket (javax.net.ssl.SSLServerSocket)67 SSLContext (javax.net.ssl.SSLContext)24 SSLSocket (javax.net.ssl.SSLSocket)19 InetSocketAddress (java.net.InetSocketAddress)15 SSLServerSocketFactory (javax.net.ssl.SSLServerSocketFactory)14 IOException (java.io.IOException)13 ServerSocket (java.net.ServerSocket)12 URL (java.net.URL)10 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)10 SSLEngine (javax.net.ssl.SSLEngine)9 UnknownHostException (java.net.UnknownHostException)7 Proxy (java.net.Proxy)6 Test (org.junit.Test)6 InetAddress (java.net.InetAddress)5 Method (java.lang.reflect.Method)3 KeyManagementException (java.security.KeyManagementException)3 KeyStore (java.security.KeyStore)3 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)3 ArrayList (java.util.ArrayList)3 ServerSocketFactory (javax.net.ServerSocketFactory)3