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));
}
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();
}
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();
}
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);
}
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);
}
Aggregations