private void testConnector( IoConnector connector, boolean useLocalAddress ) throws Exception
{
EchoConnectorHandler handler = new EchoConnectorHandler();
IoSession session = null;
if( !useLocalAddress )
{
ConnectFuture future = connector.connect(
new InetSocketAddress( "localhost", port ),
handler );
future.join();
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( "localhost", port ),
new InetSocketAddress( clientPort ),
handler );
future.join();
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 ).join();
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();
ByteBuffer buf = ByteBuffer.allocate( 1 );
buf.put( ( byte ) '.' );
buf.flip();
session.write( buf ).join();
//// 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().join();
}