@Override protected SocketAddress localAddress0() { return isActive()? LOCAL_ADDRESS : null; }
@Override protected SocketAddress remoteAddress0() { return isActive()? REMOTE_ADDRESS : null; }
@Override protected SocketAddress remoteAddress0() { return isActive()? REMOTE_ADDRESS : null; }
@Override protected SocketAddress localAddress0() { return isActive()? LOCAL_ADDRESS : null; }
@Override protected SocketAddress localAddress0() { return isActive()? LOCAL_ADDRESS : null; }
@Override protected SocketAddress remoteAddress0() { return isActive()? REMOTE_ADDRESS : null; }
@Test public void shouldFallbackToNoProtocolIfNoMatch() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When ByteBuf input = Unpooled.wrappedBuffer( // create handshake data new byte[]{(byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17}, // preamble new byte[]{0, 0, 0, 0}, // first choice - no protocol new byte[]{0, 0, 0, 2}, // second choise - protocol 1 new byte[]{0, 0, 0, 3}, // third choice - no protocol new byte[]{0, 0, 0, 4} ); // fourth choice - no protocol channel.writeInbound( input ); // Then assertEquals( 1, channel.outboundMessages().size() ); assertByteBufEquals( Unpooled.buffer().writeInt( 0 ), channel.readOutbound() ); assertFalse( channel.isActive() ); verify( protocol, never() ).install(); }
@Test public void shouldRejectIfWrongPreamble() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When ByteBuf input = Unpooled.wrappedBuffer( // create handshake data new byte[]{(byte) 0xDE, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF}, // preamble new byte[]{0, 0, 0, 1}, // first choice - no protocol new byte[]{0, 0, 0, 2}, // second choise - protocol 1 new byte[]{0, 0, 0, 3}, // third choice - no protocol new byte[]{0, 0, 0, 4} ); // fourth choice - no protocol channel.writeInbound( input ); // Then assertEquals( 0, channel.outboundMessages().size() ); assertFalse( channel.isActive() ); verify( protocol, never() ).install(); }
@Test public void shouldRejectIfInsecureWhenEncryptionRequired() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, true, false ) ); // When ByteBuf input = Unpooled.wrappedBuffer( // create handshake data new byte[]{(byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17}, // preamble new byte[]{0, 0, 0, 1}, // first choice - no protocol new byte[]{0, 0, 0, 2}, // second choise - protocol 1 new byte[]{0, 0, 0, 3}, // third choice - no protocol new byte[]{0, 0, 0, 4} ); // fourth choice - no protocol channel.writeInbound( input ); // Then assertEquals( 0, channel.outboundMessages().size() ); assertFalse( channel.isActive() ); verify( protocol, never() ).install(); }
@Test public void shouldHandleFragmentedMessage() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{(byte) 0x60, (byte) 0x60, (byte) 0xB0} ) ); assertEquals( 0, channel.outboundMessages().size() ); channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{(byte) 0x17, 0, 0, 0} ) ); assertEquals( 0, channel.outboundMessages().size() ); channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{0, 0, 0} ) ); assertEquals( 0, channel.outboundMessages().size() ); channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{0, 1, 0, 0, 0} ) ); assertEquals( 0, channel.outboundMessages().size() ); channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{0, 0, 0} ) ); assertEquals( 0, channel.outboundMessages().size() ); channel.writeInbound( Unpooled.wrappedBuffer( new byte[]{0, 0} ) ); // Then assertEquals( 1, channel.outboundMessages().size() ); assertByteBufEquals( Unpooled.buffer().writeInt( 1 ), channel.readOutbound() ); thrown.expect( NoSuchElementException.class ); channel.pipeline().remove( ProtocolHandshaker.class ); assertTrue( channel.isActive() ); verify( protocol ).install(); }
@Test public void shouldChooseFirstAvailableProtocol() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When ByteBuf input = Unpooled.wrappedBuffer( // create handshake data new byte[]{(byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17}, // preamble new byte[]{0, 0, 0, 0}, // first choice - no protocol new byte[]{0, 0, 0, 1}, // second choise - protocol 1 new byte[]{0, 0, 0, 0}, // third choice - no protocol new byte[]{0, 0, 0, 0} ); // fourth choice - no protocol channel.writeInbound( input ); // Then assertEquals( 1, channel.outboundMessages().size() ); assertByteBufEquals( Unpooled.buffer().writeInt( 1 ), channel.readOutbound() ); thrown.expect( NoSuchElementException.class ); channel.pipeline().remove( ProtocolHandshaker.class ); assertTrue( channel.isActive() ); verify( protocol ).install(); }
@Test public void shouldHandleHandshakeFollowedImmediatelyByMessage() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When ByteBuf input = Unpooled.wrappedBuffer( // create handshake data new byte[]{(byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17}, // preamble new byte[]{0, 0, 0, 0}, // first choice - no protocol new byte[]{0, 0, 0, 1}, // second choise - protocol 1 new byte[]{0, 0, 0, 0}, // third choice - no protocol new byte[]{0, 0, 0, 0}, // fourth choice - no protocol new byte[]{1, 2, 3, 4} ); // this is a message channel.writeInbound( input ); // Then assertEquals( 1, channel.outboundMessages().size() ); assertByteBufEquals( Unpooled.buffer().writeInt( 1 ), channel.readOutbound() ); assertEquals( 1, channel.inboundMessages().size() ); assertByteBufEquals( Unpooled.wrappedBuffer( new byte[]{1, 2, 3, 4} ), channel.readInbound() ); thrown.expect( NoSuchElementException.class ); channel.pipeline().remove( ProtocolHandshaker.class ); assertTrue( channel.isActive() ); verify( protocol ).install(); }
@Test public void shouldRejectIfHttp() { // Given BoltProtocol protocol = newBoltProtocol( 1 ); BoltProtocolFactory handlerFactory = newProtocolFactory( 1, protocol ); EmbeddedChannel channel = new EmbeddedChannel( new ProtocolHandshaker( handlerFactory, boltChannel, logProvider, false, true ) ); // When FullHttpRequest request = new DefaultFullHttpRequest( HttpVersion.HTTP_1_1, HttpMethod.POST, "http://hello_world:10000" ); request.headers().setInt( HttpHeaderNames.CONTENT_LENGTH, 0 ); channel.writeInbound( request ); // Then assertEquals( 0, channel.outboundMessages().size() ); assertFalse( channel.isActive() ); verify( protocol, never() ).install(); logProvider.assertExactly( AssertableLogProvider.inLog( ProtocolHandshaker.class ).warn( "Unsupported connection type: 'HTTP'. Bolt protocol only operates over a TCP connection or WebSocket." ) ); }
@Override protected SocketAddress localAddress0() { return isActive()? LOCAL_ADDRESS : null; }
@Override protected SocketAddress localAddress0() { return isActive()? LOCAL_ADDRESS : null; }
@Override protected SocketAddress remoteAddress0() { return isActive()? REMOTE_ADDRESS : null; }
@Override protected SocketAddress localAddress0() { return isActive()? LOCAL_ADDRESS : null; }
@Override protected SocketAddress remoteAddress0() { return isActive()? REMOTE_ADDRESS : null; }
@Override protected SocketAddress remoteAddress0() { return isActive()? REMOTE_ADDRESS : null; }
@Test public void testAllowedRule() throws UnknownHostException { List<Http1DeterministicRuleEngineConfig.Rule> blacklist = new ArrayList<>(); HashMultimap<String, String> headers = HashMultimap.create(); headers.put("User-Agent", "Bad-actor: 1.0"); Http1DeterministicRuleEngineConfig.Rule bad = new Http1DeterministicRuleEngineConfig.Rule( HttpMethod.POST, "/path/to/failure", HttpVersion.HTTP_1_1, headers); blacklist.add(bad); Http1Filter http1Filter = new Http1Filter(new Http1FilterConfig(ImmutableList.copyOf(blacklist))); EmbeddedChannel chAllow = new EmbeddedChannel(http1Filter); DefaultHttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_0, HttpMethod.GET, "/path/to/failure"); request.headers().set("User-Agent", "Bad-actor: 1.0"); chAllow.writeInbound(request); assertTrue(chAllow.isActive()); assertTrue(chAllow.isOpen()); } }