testConnector(connector, true);
}
private void testConnector(IoConnector connector, boolean useLocalAddress)
throws Exception {
IoSession session = null;
if (!useLocalAddress) {
ConnectFuture future = connector.connect(new InetSocketAddress(
"127.0.0.1", port));
future.awaitUninterruptibly();
session = future.getSession();
} else {
int clientPort = port;
for (int i = 0; i < 65536; i++) {
clientPort = AvailablePortFinder
.getNextAvailable(clientPort + 1);
try {
ConnectFuture future = connector.connect(
new InetSocketAddress("127.0.0.1", port),
new InetSocketAddress(clientPort));
future.awaitUninterruptibly();
session = future.getSession();
break;
} catch (RuntimeIoException e) {
// Try again until we succeed to bind.
}
}
if (session == null) {
Assert.fail("Failed to find out an appropriate local address.");
}
}
// Run a basic connector test.
testConnector0(session);
// Send closeNotify to test TLS closure if it is TLS connection.
if (useSSL) {
connectorSSLFilter.stopSsl(session).awaitUninterruptibly();
System.out
.println("-------------------------------------------------------------------------------");
// Test again after we finished TLS session.
testConnector0(session);
System.out
.println("-------------------------------------------------------------------------------");
// Test if we can enter TLS mode again.
//// Send StartTLS request.
handler.readBuf.clear();
IoBuffer buf = IoBuffer.allocate(1);
buf.put((byte) '.');
buf.flip();
session.write(buf).awaitUninterruptibly();
//// Wait for StartTLS response.
waitForResponse(handler, 1);
handler.readBuf.flip();
Assert.assertEquals(1, handler.readBuf.remaining());
Assert.assertEquals((byte) '.', handler.readBuf.get());
// Now start TLS connection
Assert.assertTrue(connectorSSLFilter.startSsl(session));
testConnector0(session);
}
session.close(true).awaitUninterruptibly();
}