NettyChannelInitializer extends ChannelInitializer<Channel> { @Override protected void initChannel( Channel channel ) { if ( securityPlan.requiresEncryption() ) { SslHandler sslHandler = createSslHandler(); channel.pipeline().addFirst( sslHandler ); } updateChannelAttributes( channel ); } NettyChannelInitializer( BoltServerAddress address, SecurityPlan securityPlan, int connectTimeoutMillis,
            Clock clock, Logging logging );   }
@Test void shouldAddSslHandlerWhenRequiresEncryption() throws Exception { SecurityPlan security = trustAllCertificates(); NettyChannelInitializer initializer = newInitializer( security ); initializer.initChannel( channel ); assertNotNull( channel.pipeline().get( SslHandler.class ) ); }
@Test void shouldNotAddSslHandlerWhenDoesNotRequireEncryption() { SecurityPlan security = SecurityPlanImpl.insecure(); NettyChannelInitializer initializer = newInitializer( security ); initializer.initChannel( channel ); assertNull( channel.pipeline().get( SslHandler.class ) ); }
@Test void shouldAddSslHandlerWithHandshakeTimeout() throws Exception { int timeoutMillis = 424242; SecurityPlan security = trustAllCertificates(); NettyChannelInitializer initializer = newInitializer( security, timeoutMillis ); initializer.initChannel( channel ); SslHandler sslHandler = channel.pipeline().get( SslHandler.class ); assertNotNull( sslHandler ); assertEquals( timeoutMillis, sslHandler.getHandshakeTimeoutMillis() ); }
@Test void shouldUpdateChannelAttributes() { Clock clock = mock( Clock.class ); when( clock.millis() ).thenReturn( 42L ); SecurityPlan security = SecurityPlanImpl.insecure(); NettyChannelInitializer initializer = newInitializer( security, Integer.MAX_VALUE, clock ); initializer.initChannel( channel ); assertEquals( LOCAL_DEFAULT, serverAddress( channel ) ); assertEquals( 42L, creationTimestamp( channel ) ); assertNotNull( messageDispatcher( channel ) ); }
@Test void shouldIncludeSniHostName() throws Exception { BoltServerAddress address = new BoltServerAddress( "database.neo4j.com", 8989 ); NettyChannelInitializer initializer = new NettyChannelInitializer( address, trustAllCertificates(), 10000, Clock.SYSTEM, DEV_NULL_LOGGING ); initializer.initChannel( channel ); SslHandler sslHandler = channel.pipeline().get( SslHandler.class ); SSLEngine sslEngine = sslHandler.engine(); SSLParameters sslParameters = sslEngine.getSSLParameters(); List<SNIServerName> sniServerNames = sslParameters.getServerNames(); assertThat( sniServerNames, hasSize( 1 ) ); assertThat( sniServerNames.get( 0 ), instanceOf( SNIHostName.class ) ); assertThat( ((SNIHostName) sniServerNames.get( 0 )).getAsciiName(), equalTo( address.host() ) ); }