Search in sources :

Example 1 with InvalidPacketDetectedEvent

use of com.couchbase.client.core.cnc.events.io.InvalidPacketDetectedEvent in project couchbase-jvm-clients by couchbase.

the class MemcacheProtocolVerificationHandler method handleEventAndCloseChannel.

/**
 * Helper method to send the event into the event bus and then close the
 * channel.
 *
 * @param ctx the context to use.
 * @param msg the msg to export.
 */
private void handleEventAndCloseChannel(final ChannelHandlerContext ctx, final ByteBuf msg) {
    final IoContext ioContext = new IoContext(endpointContext, ctx.channel().localAddress(), ctx.channel().remoteAddress(), endpointContext.bucket());
    byte[] packet = ByteBufUtil.getBytes(msg);
    ReferenceCountUtil.release(msg);
    endpointContext.environment().eventBus().publish(new InvalidPacketDetectedEvent(ioContext, packet));
    ctx.close();
}
Also used : IoContext(com.couchbase.client.core.io.IoContext) InvalidPacketDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidPacketDetectedEvent)

Example 2 with InvalidPacketDetectedEvent

use of com.couchbase.client.core.cnc.events.io.InvalidPacketDetectedEvent in project couchbase-jvm-clients by couchbase.

the class MemcacheProtocolVerificationHandlerTest method shouldCloseOnInvalidResponses.

/**
 * Verifies that invalid responses are not let through.
 *
 * @param inputHolder the bad input packets.
 */
@ParameterizedTest(name = "{0}")
@MethodSource
void shouldCloseOnInvalidResponses(final InputHolder inputHolder) {
    SimpleEventBus eventBus = new SimpleEventBus(true);
    CoreEnvironment env = mock(CoreEnvironment.class);
    EndpointContext ctx = mock(EndpointContext.class);
    when(ctx.environment()).thenReturn(env);
    when(env.eventBus()).thenReturn(eventBus);
    final EmbeddedChannel channel = new EmbeddedChannel(new MemcacheProtocolVerificationHandler(ctx));
    try {
        channel.writeInbound(inputHolder.input);
        assertFalse(channel.isOpen());
        InvalidPacketDetectedEvent event = (InvalidPacketDetectedEvent) eventBus.publishedEvents().get(0);
        assertEquals(Event.Severity.ERROR, event.severity());
        assertEquals(Event.Category.IO.path(), event.category());
        assertTrue(event.description().contains("Invalid Packet detected:"));
    } finally {
        channel.finishAndReleaseAll();
    }
}
Also used : CoreEnvironment(com.couchbase.client.core.env.CoreEnvironment) EndpointContext(com.couchbase.client.core.endpoint.EndpointContext) SimpleEventBus(com.couchbase.client.core.cnc.SimpleEventBus) EmbeddedChannel(com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel) InvalidPacketDetectedEvent(com.couchbase.client.core.cnc.events.io.InvalidPacketDetectedEvent) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Aggregations

InvalidPacketDetectedEvent (com.couchbase.client.core.cnc.events.io.InvalidPacketDetectedEvent)2 SimpleEventBus (com.couchbase.client.core.cnc.SimpleEventBus)1 EmbeddedChannel (com.couchbase.client.core.deps.io.netty.channel.embedded.EmbeddedChannel)1 EndpointContext (com.couchbase.client.core.endpoint.EndpointContext)1 CoreEnvironment (com.couchbase.client.core.env.CoreEnvironment)1 IoContext (com.couchbase.client.core.io.IoContext)1 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)1 MethodSource (org.junit.jupiter.params.provider.MethodSource)1