Search in sources :

Example 6 with BoltProtocolFactory

use of org.neo4j.bolt.transport.BoltProtocolFactory in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldFallbackToNoProtocolIfNoMatch.

@Test
void shouldFallbackToNoProtocolIfNoMatch() {
    // Given
    BoltProtocol protocol = newBoltProtocol(1, 0);
    BoltProtocolFactory handlerFactory = newProtocolFactory(1, 0, protocol);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // When
    ByteBuf input = // create handshake data
    Unpooled.wrappedBuffer(// preamble
    new byte[] { (byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17 }, // first choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // second choice - protocol 1
    new byte[] { 0, 0, 0, 2 }, // third choice - no protocol
    new byte[] { 0, 0, 0, 3 }, // fourth choice - no protocol
    new byte[] { 0, 0, 0, 4 });
    channel.writeInbound(input);
    // Then
    assertEquals(1, channel.outboundMessages().size());
    assertByteBufEquals(Unpooled.buffer().writeInt(0), channel.readOutbound());
    assertFalse(channel.isActive());
    verify(protocol, never()).install();
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 7 with BoltProtocolFactory

use of org.neo4j.bolt.transport.BoltProtocolFactory in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldRejectIfWrongPreamble.

@Test
void shouldRejectIfWrongPreamble() {
    // Given
    BoltProtocol protocol = newBoltProtocol(1, 0);
    BoltProtocolFactory handlerFactory = newProtocolFactory(1, 0, protocol);
    var memoryTracker = mock(MemoryTracker.class);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // When
    ByteBuf input = // create handshake data
    Unpooled.wrappedBuffer(// preamble
    new byte[] { (byte) 0xDE, (byte) 0xAB, (byte) 0xCD, (byte) 0xEF }, // first choice - no protocol
    new byte[] { 0, 0, 0, 1 }, // second choice - protocol 1
    new byte[] { 0, 0, 0, 2 }, // third choice - no protocol
    new byte[] { 0, 0, 0, 3 }, // fourth choice - no protocol
    new byte[] { 0, 0, 0, 4 });
    channel.writeInbound(input);
    // Then
    assertEquals(0, channel.outboundMessages().size());
    assertFalse(channel.isActive());
    verify(protocol, never()).install();
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 8 with BoltProtocolFactory

use of org.neo4j.bolt.transport.BoltProtocolFactory in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldRejectIfHttp.

@Test
void shouldRejectIfHttp() {
    // Given
    BoltProtocol protocol = newBoltProtocol(1, 0);
    BoltProtocolFactory handlerFactory = newProtocolFactory(1, 0, protocol);
    var memoryTracker = mock(MemoryTracker.class);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // 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();
    assertThat(logProvider).forClass(ProtocolHandshaker.class).forLevel(WARN).containsMessages("Unsupported connection type: 'HTTP'. Bolt protocol only operates over a TCP connection or WebSocket.");
}
Also used : DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) FullHttpRequest(io.netty.handler.codec.http.FullHttpRequest) BoltProtocol(org.neo4j.bolt.BoltProtocol) DefaultFullHttpRequest(io.netty.handler.codec.http.DefaultFullHttpRequest) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 9 with BoltProtocolFactory

use of org.neo4j.bolt.transport.BoltProtocolFactory in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldHandleMaxVersionNumber.

@Test
void shouldHandleMaxVersionNumber() {
    int maxVersionNumber = 255;
    // Given
    BoltProtocol protocol = newBoltProtocol(maxVersionNumber, maxVersionNumber);
    BoltProtocolFactory handlerFactory = newProtocolFactory(maxVersionNumber, maxVersionNumber, protocol);
    var memoryTracker = mock(MemoryTracker.class, RETURNS_MOCKS);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, false, true, mock(ChannelProtector.class), memoryTracker));
    // When
    ByteBuf input = // create handshake data
    Unpooled.wrappedBuffer(// preamble
    new byte[] { (byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17 }, // first choice - no protocol
    new byte[] { (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF }, // second choice - protocol 1
    new byte[] { 0, 0, 0, 0 }, // third choice - no protocol
    new byte[] { 0, 0, 0, 0 }, // fourth choice - no protocol
    new byte[] { 0, 0, 0, 0 });
    channel.writeInbound(input);
    // Then
    assertEquals(1, channel.outboundMessages().size());
    assertByteBufEquals(Unpooled.buffer().writeInt(new BoltProtocolVersion(maxVersionNumber, maxVersionNumber).toInt()), channel.readOutbound());
    assertThrows(NoSuchElementException.class, () -> channel.pipeline().remove(ProtocolHandshaker.class));
    assertTrue(channel.isActive());
    verify(protocol).install();
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) BoltProtocolVersion(org.neo4j.bolt.BoltProtocolVersion) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 10 with BoltProtocolFactory

use of org.neo4j.bolt.transport.BoltProtocolFactory in project neo4j by neo4j.

the class ProtocolHandshakerTest method shouldRejectIfInsecureWhenEncryptionRequired.

@Test
void shouldRejectIfInsecureWhenEncryptionRequired() {
    // Given
    BoltProtocol protocol = newBoltProtocol(1, 0);
    BoltProtocolFactory handlerFactory = newProtocolFactory(1, 0, protocol);
    var memoryTracker = mock(MemoryTracker.class);
    EmbeddedChannel channel = new EmbeddedChannel(new ProtocolHandshaker(handlerFactory, boltChannel, logProvider, true, false, mock(ChannelProtector.class), memoryTracker));
    // When
    ByteBuf input = // create handshake data
    Unpooled.wrappedBuffer(// preamble
    new byte[] { (byte) 0x60, (byte) 0x60, (byte) 0xB0, (byte) 0x17 }, // first choice - no protocol
    new byte[] { 0, 0, 0, 1 }, // second choice - protocol 1
    new byte[] { 0, 0, 0, 2 }, // third choice - no protocol
    new byte[] { 0, 0, 0, 3 }, // fourth choice - no protocol
    new byte[] { 0, 0, 0, 4 });
    channel.writeInbound(input);
    // Then
    assertEquals(0, channel.outboundMessages().size());
    assertFalse(channel.isActive());
    verify(protocol, never()).install();
}
Also used : BoltProtocol(org.neo4j.bolt.BoltProtocol) EmbeddedChannel(io.netty.channel.embedded.EmbeddedChannel) ByteBuf(io.netty.buffer.ByteBuf) BoltProtocolFactory(org.neo4j.bolt.transport.BoltProtocolFactory) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

BoltProtocolFactory (org.neo4j.bolt.transport.BoltProtocolFactory)11 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)10 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)10 BoltProtocol (org.neo4j.bolt.BoltProtocol)10 ByteBuf (io.netty.buffer.ByteBuf)8 Test (org.junit.jupiter.api.Test)8 MethodSource (org.junit.jupiter.params.provider.MethodSource)2 BoltProtocolVersion (org.neo4j.bolt.BoltProtocolVersion)2 ByteBufAllocator (io.netty.buffer.ByteBufAllocator)1 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)1 DefaultFullHttpRequest (io.netty.handler.codec.http.DefaultFullHttpRequest)1 FullHttpRequest (io.netty.handler.codec.http.FullHttpRequest)1 BoltConnectionFactory (org.neo4j.bolt.runtime.BoltConnectionFactory)1 DefaultBoltConnectionFactory (org.neo4j.bolt.runtime.DefaultBoltConnectionFactory)1 BoltSchedulerProvider (org.neo4j.bolt.runtime.scheduling.BoltSchedulerProvider)1 CachedThreadPoolExecutorFactory (org.neo4j.bolt.runtime.scheduling.CachedThreadPoolExecutorFactory)1 ExecutorBoltSchedulerProvider (org.neo4j.bolt.runtime.scheduling.ExecutorBoltSchedulerProvider)1 NettyThreadFactory (org.neo4j.bolt.runtime.scheduling.NettyThreadFactory)1 BoltStateMachineFactory (org.neo4j.bolt.runtime.statemachine.BoltStateMachineFactory)1 BoltMemoryPool (org.neo4j.bolt.transport.BoltMemoryPool)1