Search in sources :

Example 16 with HandshakeCompletedListener

use of javax.net.ssl.HandshakeCompletedListener in project zookeeper by apache.

the class CnxManagerTest method testSSLSocketClosedWhenHandshakeTimeout.

/**
 * Test the SSLSocket is explicitly closed when there is IOException
 * happened during connect.
 */
@Test
public void testSSLSocketClosedWhenHandshakeTimeout() throws Exception {
    final CountDownLatch closeLatch = new CountDownLatch(1);
    QuorumX509Util mockedX509Util = new QuorumX509Util() {

        @Override
        public SSLSocket createSSLSocket() {
            return new SSLSocket() {

                @Override
                public void connect(SocketAddress endpoint, int timeout) {
                }

                @Override
                public void startHandshake() throws IOException {
                    throw new IOException();
                }

                @Override
                public void close() {
                    closeLatch.countDown();
                }

                public String[] getSupportedCipherSuites() {
                    throw new UnsupportedOperationException();
                }

                public String[] getEnabledCipherSuites() {
                    throw new UnsupportedOperationException();
                }

                public String[] getSupportedProtocols() {
                    throw new UnsupportedOperationException();
                }

                public String[] getEnabledProtocols() {
                    throw new UnsupportedOperationException();
                }

                public SSLSession getSession() {
                    throw new UnsupportedOperationException();
                }

                public void setEnabledCipherSuites(String[] suites) {
                }

                public void setEnabledProtocols(String[] protocols) {
                }

                public void addHandshakeCompletedListener(HandshakeCompletedListener listener) {
                }

                public void removeHandshakeCompletedListener(HandshakeCompletedListener listener) {
                }

                public void setUseClientMode(boolean mode) {
                }

                public boolean getUseClientMode() {
                    return true;
                }

                public void setNeedClientAuth(boolean need) {
                }

                public boolean getNeedClientAuth() {
                    return true;
                }

                public void setWantClientAuth(boolean want) {
                }

                public boolean getWantClientAuth() {
                    return true;
                }

                public void setEnableSessionCreation(boolean flag) {
                }

                public boolean getEnableSessionCreation() {
                    return true;
                }
            };
        }
    };
    QuorumPeer peer = new QuorumPeer(peers, peerTmpdir[0], peerTmpdir[0], peerClientPort[0], 3, 0, 2000, 2, 2, 2) {

        @Override
        public QuorumX509Util createX509Util() {
            return mockedX509Util;
        }
    };
    peer.setSslQuorum(true);
    QuorumCnxManager cnxManager = peer.createCnxnManager();
    cnxManager.connectOne(1, peers.get(1L).electionAddr);
    assertTrue(closeLatch.await(1, TimeUnit.SECONDS));
}
Also used : HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) QuorumX509Util(org.apache.zookeeper.common.QuorumX509Util) SSLSocket(javax.net.ssl.SSLSocket) IOException(java.io.IOException) CountDownLatch(java.util.concurrent.CountDownLatch) SocketAddress(java.net.SocketAddress) InetSocketAddress(java.net.InetSocketAddress) FLENewEpochTest(org.apache.zookeeper.test.FLENewEpochTest) Test(org.junit.jupiter.api.Test)

Example 17 with HandshakeCompletedListener

use of javax.net.ssl.HandshakeCompletedListener in project j2objc by google.

the class SSLSocketTest method j2objcNotImplemented_test_removeHandshakeCompletedListener.

/**
 * javax.net.ssl.SSLSocket#removeHandshakeCompletedListener(HandshakeCompletedListener listener)
 */
public void j2objcNotImplemented_test_removeHandshakeCompletedListener() throws IOException {
    SSLSocket ssl = getSSLSocket();
    HandshakeCompletedListener ls = new HandshakeCL();
    try {
        ssl.removeHandshakeCompletedListener(null);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    try {
        ssl.removeHandshakeCompletedListener(ls);
    } catch (IllegalArgumentException expected) {
    }
    ssl.addHandshakeCompletedListener(ls);
    ssl.removeHandshakeCompletedListener(ls);
    ssl.close();
}
Also used : HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) SSLSocket(javax.net.ssl.SSLSocket)

Example 18 with HandshakeCompletedListener

use of javax.net.ssl.HandshakeCompletedListener in project j2objc by google.

the class SSLSocketTest method j2objcNotImplemented_test_addHandshakeCompletedListener.

/**
 * javax.net.ssl.SSLSocket#addHandshakeCompletedListener(HandshakeCompletedListener listener)
 */
// AndroidOnly("RI doesn't throw the specified IAE")
public void j2objcNotImplemented_test_addHandshakeCompletedListener() throws IOException {
    SSLSocket ssl = getSSLSocket();
    HandshakeCompletedListener ls = new HandshakeCL();
    try {
        ssl.addHandshakeCompletedListener(null);
        fail();
    } catch (IllegalArgumentException expected) {
    }
    ssl.addHandshakeCompletedListener(ls);
    ssl.close();
}
Also used : HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) SSLSocket(javax.net.ssl.SSLSocket)

Example 19 with HandshakeCompletedListener

use of javax.net.ssl.HandshakeCompletedListener in project j2objc by google.

the class SSLSocketTest method test_SSLSocket_HandshakeCompletedListener_RuntimeException.

public void test_SSLSocket_HandshakeCompletedListener_RuntimeException() throws Exception {
    final Thread self = Thread.currentThread();
    final UncaughtExceptionHandler original = self.getUncaughtExceptionHandler();
    final RuntimeException expectedException = new RuntimeException("expected");
    final TestUncaughtExceptionHandler test = new TestUncaughtExceptionHandler();
    self.setUncaughtExceptionHandler(test);
    final TestSSLContext c = TestSSLContext.create();
    final SSLSocket client = (SSLSocket) c.clientContext.getSocketFactory().createSocket(c.host, c.port);
    final SSLSocket server = (SSLSocket) c.serverSocket.accept();
    ExecutorService executor = Executors.newSingleThreadExecutor();
    Future<Void> future = executor.submit(new Callable<Void>() {

        @Override
        public Void call() throws Exception {
            server.startHandshake();
            return null;
        }
    });
    executor.shutdown();
    client.addHandshakeCompletedListener(new HandshakeCompletedListener() {

        public void handshakeCompleted(HandshakeCompletedEvent event) {
            throw expectedException;
        }
    });
    client.startHandshake();
    future.get();
    client.close();
    server.close();
    c.close();
    assertSame(expectedException, test.actualException);
    self.setUncaughtExceptionHandler(original);
}
Also used : SSLSocket(javax.net.ssl.SSLSocket) SSLProtocolException(javax.net.ssl.SSLProtocolException) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) KeyManagementException(java.security.KeyManagementException) EOFException(java.io.EOFException) SSLException(javax.net.ssl.SSLException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) SocketException(java.net.SocketException) SocketTimeoutException(java.net.SocketTimeoutException) IOException(java.io.IOException) CertificateException(java.security.cert.CertificateException) SSLPeerUnverifiedException(javax.net.ssl.SSLPeerUnverifiedException) HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) ExecutorService(java.util.concurrent.ExecutorService) UncaughtExceptionHandler(java.lang.Thread.UncaughtExceptionHandler)

Example 20 with HandshakeCompletedListener

use of javax.net.ssl.HandshakeCompletedListener in project java-chassis by ServiceComb.

the class SSLManagerTest method testSSLManagerServerAndClient.

@Test
public void testSSLManagerServerAndClient(@Mocked final NetworkInterface nif) throws Exception {
    final InetAddress ia = Inet4Address.getByName("10.57.65.225");
    final Enumeration<NetworkInterface> interfaces = new Enumeration<NetworkInterface>() {

        int count = 1;

        int cur = 0;

        @Override
        public boolean hasMoreElements() {
            if (cur < count) {
                cur++;
                return true;
            }
            return false;
        }

        @Override
        public NetworkInterface nextElement() {
            return nif;
        }
    };
    final Enumeration<InetAddress> ias = new Enumeration<InetAddress>() {

        int count = 1;

        int cur = 0;

        @Override
        public boolean hasMoreElements() {
            if (cur < count) {
                cur++;
                return true;
            }
            return false;
        }

        @Override
        public InetAddress nextElement() {
            return ia;
        }
    };
    new Expectations() {

        @Mocked
        NetworkInterface nif;

        {
            NetworkInterface.getNetworkInterfaces();
            result = interfaces;
        }
    };
    new Expectations() {

        {
            nif.getInetAddresses();
            result = ias;
            ia.getHostAddress();
            result = "10.57.65.225";
        }
    };
    SSLOption option = SSLOption.build(DIR + "/server.ssl.properties");
    SSLCustom custom = new SSLCustom() {

        @Override
        public String getFullPath(String filename) {
            return DIR + "/ssl/" + filename;
        }

        @Override
        public char[] decode(char[] encrypted) {
            return encrypted;
        }
    };
    final SSLServerSocket serverSocket = SSLManager.createSSLServerSocket(option, custom);
    Assert.assertTrue(serverSocket.getNeedClientAuth());
    serverSocket.bind(new InetSocketAddress("127.0.0.1", 8886));
    String[] protos = serverSocket.getEnabledCipherSuites();
    String[] protosExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(protos, protosExpected);
    String[] ciphers = serverSocket.getEnabledCipherSuites();
    String[] ciphersExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(ciphers, ciphersExpected);
    Assert.assertEquals(serverSocket.getNeedClientAuth(), true);
    SSLOption clientoption = SSLOption.build(DIR + "/client.ssl.properties");
    SSLSocket clientsocket = SSLManager.createSSLSocket(clientoption, custom);
    String[] clientprotos = clientsocket.getEnabledCipherSuites();
    String[] clientprotosExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(clientprotos, clientprotosExpected);
    String[] clientciphers = clientsocket.getEnabledCipherSuites();
    String[] clientciphersExpected = "TLS_DHE_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_DSS_WITH_AES_128_CBC_SHA256,TLS_RSA_WITH_AES_128_CBC_SHA256,TLS_DHE_RSA_WITH_AES_128_CBC_SHA,TLS_DHE_DSS_WITH_AES_128_CBC_SHA,TLS_RSA_WITH_AES_128_CBC_SHA".split(",");
    Assert.assertArrayEquals(clientciphers, clientciphersExpected);
    Assert.assertEquals(clientsocket.getNeedClientAuth(), false);
    boolean validAssert = true;
    try {
        clientsocket.connect(new InetSocketAddress("127.0.0.1", 8886));
        new Thread() {

            public void run() {
                try {
                    SSLSocket s = (SSLSocket) serverSocket.accept();
                    s.addHandshakeCompletedListener(new HandshakeCompletedListener() {

                        @Override
                        public void handshakeCompleted(HandshakeCompletedEvent arg0) {
                        }
                    });
                    s.getOutputStream().write(new byte[] { 0, 1 });
                } catch (IOException e) {
                    e.printStackTrace();
                    // this should not happen, do a false assert
                    Assert.assertEquals(false, true);
                }
            }
        }.start();
        clientsocket.startHandshake();
        clientsocket.close();
        serverSocket.close();
    // socked successfully opened and closed
    } catch (Exception e) {
        e.printStackTrace();
        validAssert = false;
    }
    Assert.assertTrue(validAssert);
}
Also used : Expectations(mockit.Expectations) Enumeration(java.util.Enumeration) InetSocketAddress(java.net.InetSocketAddress) SSLSocket(javax.net.ssl.SSLSocket) NetworkInterface(java.net.NetworkInterface) IOException(java.io.IOException) SSLServerSocket(javax.net.ssl.SSLServerSocket) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) UnknownHostException(java.net.UnknownHostException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) HandshakeCompletedListener(javax.net.ssl.HandshakeCompletedListener) HandshakeCompletedEvent(javax.net.ssl.HandshakeCompletedEvent) InetAddress(java.net.InetAddress) Test(org.junit.Test)

Aggregations

HandshakeCompletedListener (javax.net.ssl.HandshakeCompletedListener)20 SSLSocket (javax.net.ssl.SSLSocket)19 HandshakeCompletedEvent (javax.net.ssl.HandshakeCompletedEvent)14 IOException (java.io.IOException)10 InetSocketAddress (java.net.InetSocketAddress)6 SocketException (java.net.SocketException)6 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)5 SSLServerSocket (javax.net.ssl.SSLServerSocket)5 KeyManagementException (java.security.KeyManagementException)4 ExecutorService (java.util.concurrent.ExecutorService)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)4 Test (org.junit.Test)4 InputStream (java.io.InputStream)3 OutputStream (java.io.OutputStream)3 InetAddress (java.net.InetAddress)3 NetworkInterface (java.net.NetworkInterface)3 SocketTimeoutException (java.net.SocketTimeoutException)3 UnknownHostException (java.net.UnknownHostException)3 CertificateException (java.security.cert.CertificateException)3 Enumeration (java.util.Enumeration)3