Search in sources :

Example 61 with SSLSocket

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

the class HandshakeCompletedEventTest method test_getLocalPrincipal.

/**
     * @throws IOException
     * javax.net.ssl.HandshakeCompletedEvent#getLocalPrincipal()
     */
public final void test_getLocalPrincipal() throws Exception {
    mySSLSession session = new mySSLSession("localhost", 1080, null);
    SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
    HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, session);
    assertNull(event.getLocalPrincipal());
}
Also used : HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) SSLSocket(javax.net.ssl.SSLSocket) org.apache.harmony.xnet.tests.support.mySSLSession(org.apache.harmony.xnet.tests.support.mySSLSession)

Example 62 with SSLSocket

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

the class HandshakeCompletedEventTest method test_getLocalCertificates.

/**
     * @throws IOException
     * javax.net.ssl.HandshakeCompletedEvent#getLocalCertificates()
     */
public final void test_getLocalCertificates() throws Exception {
    mySSLSession session = new mySSLSession("localhost", 1080, null);
    SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
    HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, session);
    assertNull(event.getLocalCertificates());
}
Also used : HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) SSLSocket(javax.net.ssl.SSLSocket) org.apache.harmony.xnet.tests.support.mySSLSession(org.apache.harmony.xnet.tests.support.mySSLSession)

Example 63 with SSLSocket

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

the class HandshakeCompletedEventTest method test_getSocket.

/**
     * @throws IOException
     * javax.net.ssl.HandshakeCompletedEvent#getSocket()
     */
public final void test_getSocket() throws IOException {
    SSLSocket socket = (SSLSocket) SSLSocketFactory.getDefault().createSocket();
    HandshakeCompletedEvent event = new HandshakeCompletedEvent(socket, null);
    SSLSocket ss = event.getSocket();
    assertNotNull(ss);
    assertEquals(socket, ss);
}
Also used : HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) SSLSocket(javax.net.ssl.SSLSocket)

Example 64 with SSLSocket

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

the class MockWebServer method serveConnection.

private void serveConnection(final Socket raw) {
    String name = "MockWebServer-" + raw.getRemoteSocketAddress();
    executor.execute(namedRunnable(name, new Runnable() {

        int sequenceNumber = 0;

        public void run() {
            try {
                processConnection();
            } catch (Exception e) {
                logger.log(Level.WARNING, "MockWebServer connection failed", e);
            }
        }

        public void processConnection() throws Exception {
            Socket socket;
            if (sslSocketFactory != null) {
                if (tunnelProxy) {
                    createTunnel();
                }
                socket = sslSocketFactory.createSocket(raw, raw.getInetAddress().getHostAddress(), raw.getPort(), true);
                ((SSLSocket) socket).setUseClientMode(false);
                openClientSockets.add(socket);
                openClientSockets.remove(raw);
            } else {
                socket = raw;
            }
            InputStream in = new BufferedInputStream(socket.getInputStream());
            OutputStream out = new BufferedOutputStream(socket.getOutputStream());
            while (!responseQueue.isEmpty() && processOneRequest(in, out, socket)) {
            }
            if (sequenceNumber == 0) {
                logger.warning("MockWebServer connection didn't make a request");
            }
            in.close();
            out.close();
            socket.close();
            if (responseQueue.isEmpty()) {
                shutdown();
            }
            openClientSockets.remove(socket);
        }

        /**
             * Respond to CONNECT requests until a SWITCH_TO_SSL_AT_END response
             * is dispatched.
             */
        private void createTunnel() throws IOException, InterruptedException {
            while (true) {
                MockResponse connect = responseQueue.peek();
                if (!processOneRequest(raw.getInputStream(), raw.getOutputStream(), raw)) {
                    throw new IllegalStateException("Tunnel without any CONNECT!");
                }
                if (connect.getSocketPolicy() == SocketPolicy.UPGRADE_TO_SSL_AT_END) {
                    return;
                }
            }
        }

        /**
             * Reads a request and writes its response. Returns true if a request
             * was processed.
             */
        private boolean processOneRequest(InputStream in, OutputStream out, Socket socket) throws IOException, InterruptedException {
            RecordedRequest request = readRequest(in, sequenceNumber);
            if (request == null) {
                return false;
            }
            MockResponse response = dispatch(request);
            writeResponse(out, response);
            if (response.getSocketPolicy() == SocketPolicy.DISCONNECT_AT_END) {
                in.close();
                out.close();
            } else if (response.getSocketPolicy() == SocketPolicy.SHUTDOWN_INPUT_AT_END) {
                socket.shutdownInput();
            } else if (response.getSocketPolicy() == SocketPolicy.SHUTDOWN_OUTPUT_AT_END) {
                socket.shutdownOutput();
            }
            sequenceNumber++;
            return true;
        }
    }));
}
Also used : BufferedInputStream(java.io.BufferedInputStream) BufferedInputStream(java.io.BufferedInputStream) InputStream(java.io.InputStream) SSLSocket(javax.net.ssl.SSLSocket) ByteArrayOutputStream(java.io.ByteArrayOutputStream) BufferedOutputStream(java.io.BufferedOutputStream) OutputStream(java.io.OutputStream) BufferedOutputStream(java.io.BufferedOutputStream) SocketException(java.net.SocketException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) Socket(java.net.Socket) SSLSocket(javax.net.ssl.SSLSocket) ServerSocket(java.net.ServerSocket)

Example 65 with SSLSocket

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

the class TestSSLSocketPair method connect.

/**
     * Create a new connected server/client socket pair within a
     * existing SSLContext. Optionally specify clientCipherSuites to
     * allow forcing new SSLSession to test SSLSessionContext
     * caching. Optionally specify serverCipherSuites for testing
     * cipher suite negotiation.
     */
public static SSLSocket[] connect(final TestSSLContext context, final String[] clientCipherSuites, final String[] serverCipherSuites) {
    try {
        final SSLSocket client = (SSLSocket) context.clientContext.getSocketFactory().createSocket(context.host, context.port);
        final SSLSocket server = (SSLSocket) context.serverSocket.accept();
        ExecutorService executor = Executors.newFixedThreadPool(2);
        Future s = executor.submit(new Callable<Void>() {

            public Void call() throws Exception {
                if (serverCipherSuites != null) {
                    server.setEnabledCipherSuites(serverCipherSuites);
                }
                server.startHandshake();
                return null;
            }
        });
        Future c = executor.submit(new Callable<Void>() {

            public Void call() throws Exception {
                if (clientCipherSuites != null) {
                    client.setEnabledCipherSuites(clientCipherSuites);
                }
                client.startHandshake();
                return null;
            }
        });
        executor.shutdown();
        // catch client and server exceptions separately so we can
        // potentially log both.
        Exception serverException;
        try {
            s.get(30, TimeUnit.SECONDS);
            serverException = null;
        } catch (Exception e) {
            serverException = e;
            e.printStackTrace();
        }
        Exception clientException;
        try {
            c.get(30, TimeUnit.SECONDS);
            clientException = null;
        } catch (Exception e) {
            clientException = e;
            e.printStackTrace();
        }
        if (serverException != null) {
            throw serverException;
        }
        if (clientException != null) {
            throw clientException;
        }
        return new SSLSocket[] { server, client };
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) ExecutorService(java.util.concurrent.ExecutorService) Future(java.util.concurrent.Future) ExecutionException(java.util.concurrent.ExecutionException)

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