Example usage for io.netty.buffer ByteBufAllocator DEFAULT

List of usage examples for io.netty.buffer ByteBufAllocator DEFAULT

Introduction

In this page you can find the example usage for io.netty.buffer ByteBufAllocator DEFAULT.

Prototype

ByteBufAllocator DEFAULT

To view the source code for io.netty.buffer ByteBufAllocator DEFAULT.

Click Source Link

Usage

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parserHeader_frameOkFragment() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);//from   w  w  w. j  ava 2  s  .co m
    in.writeIntLE(FRAME_MIN_SIZE);
    in.writeIntLE(6);
    ByteBuf in1 = in.readSlice(AltsTsiFrameProtector.getHeaderBytes() - 1);
    ByteBuf in2 = in.readSlice(1);

    unprotector.unprotect(in1, out, alloc);
    assertThat(in1.readableBytes()).isEqualTo(0);

    unprotector.unprotect(in2, out, alloc);
    assertThat(in2.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseHeader_frameFailFragment() throws GeneralSecurityException {
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf in = getDirectBuffer(AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes(),
            ref);/*from   w w w . j  a va2 s. co m*/
    in.writeIntLE(FRAME_MIN_SIZE - 1);
    in.writeIntLE(6);
    ByteBuf in1 = in.readSlice(AltsTsiFrameProtector.getHeaderBytes() - 1);
    ByteBuf in2 = in.readSlice(1);

    unprotector.unprotect(in1, out, alloc);
    assertThat(in1.readableBytes()).isEqualTo(0);

    try {
        unprotector.unprotect(in2, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too small");
    }

    assertThat(in2.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_oneFrameNoFragment() throws GeneralSecurityException {
    int payloadBytes = 1024;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf outFrame = getDirectBuffer(//from  ww w. j  a v a 2 s . c  o  m
            AltsTsiFrameProtector.getHeaderBytes() + payloadBytes + FakeChannelCrypter.getTagBytes(), ref);

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain = Collections.singletonList(plain);
    ByteBuf frameOut = writeSlice(outFrame, payloadBytes + FakeChannelCrypter.getTagBytes());
    crypter.encrypt(frameOut, framePlain);
    plain.readerIndex(0);

    unprotector.unprotect(outFrame, out, alloc);
    assertThat(outFrame.readableBytes()).isEqualTo(0);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesNoFragment() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf outFrame = getDirectBuffer(//from   w w w  .  java 2  s.c om
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes,
            ref);

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(outFrame, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    outFrame.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    outFrame.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(outFrame, payloadBytes2 + FakeChannelCrypter.getTagBytes());

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector.unprotect(outFrame, out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);
    assertThat(outFrame.refCnt()).isEqualTo(1);
    assertThat(outFrame.readableBytes()).isEqualTo(0);

    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesNoFragment_Leftover() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf protectedBuf = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes
                    + AltsTsiFrameProtector.getHeaderBytes(),
            ref);//from   w w w  .  j a  va  2 s .co  m

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(protectedBuf, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(protectedBuf, payloadBytes2 + FakeChannelCrypter.getTagBytes());
    // This is an invalid header length field, make sure it triggers an error
    // when the remainder of the header is given.
    protectedBuf.writeIntLE((byte) -1);

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector.unprotect(protectedBuf, out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain);

    // The protectedBuf is buffered inside the unprotector.
    assertThat(protectedBuf.readableBytes()).isEqualTo(0);
    assertThat(protectedBuf.refCnt()).isEqualTo(2);

    protectedBuf.writeIntLE(6);
    try {
        unprotector.unprotect(protectedBuf, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too small");
    }

    unprotector.destroy();

    // Make sure that unprotector does not hold onto buffered ByteBuf instance after destroy.
    assertThat(protectedBuf.refCnt()).isEqualTo(1);

    // Make sure that destroying twice does not throw.
    unprotector.destroy();
}

From source file:io.grpc.alts.internal.AltsTsiFrameProtectorTest.java

License:Apache License

@Test
public void parseFrame_twoFramesFragmentSecond() throws GeneralSecurityException {
    int payloadBytes = 1536;
    int payloadBytes1 = 1024;
    int payloadBytes2 = payloadBytes - payloadBytes1;
    ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
    List<Object> out = new ArrayList<>();
    FakeChannelCrypter crypter = new FakeChannelCrypter();
    AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);

    ByteBuf plain = getRandom(payloadBytes, ref);
    ByteBuf protectedBuf = getDirectBuffer(
            2 * (AltsTsiFrameProtector.getHeaderBytes() + FakeChannelCrypter.getTagBytes()) + payloadBytes
                    + AltsTsiFrameProtector.getHeaderBytes(),
            ref);/*from   ww w  .  j av  a2 s .c o m*/

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes1 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain1 = Collections.singletonList(plain.readSlice(payloadBytes1));
    ByteBuf frameOut1 = writeSlice(protectedBuf, payloadBytes1 + FakeChannelCrypter.getTagBytes());

    protectedBuf.writeIntLE(
            AltsTsiFrameProtector.getHeaderTypeFieldBytes() + payloadBytes2 + FakeChannelCrypter.getTagBytes());
    protectedBuf.writeIntLE(6);
    List<ByteBuf> framePlain2 = Collections.singletonList(plain);
    ByteBuf frameOut2 = writeSlice(protectedBuf, payloadBytes2 + FakeChannelCrypter.getTagBytes());

    crypter.encrypt(frameOut1, framePlain1);
    crypter.encrypt(frameOut2, framePlain2);
    plain.readerIndex(0);

    unprotector
            .unprotect(
                    protectedBuf.readSlice(payloadBytes + AltsTsiFrameProtector.getHeaderBytes()
                            + FakeChannelCrypter.getTagBytes() + AltsTsiFrameProtector.getHeaderBytes()),
                    out, alloc);
    assertThat(out.size()).isEqualTo(1);
    ByteBuf out1 = ref((ByteBuf) out.get(0));
    assertThat(out1).isEqualTo(plain.readSlice(payloadBytes1));
    assertThat(protectedBuf.refCnt()).isEqualTo(2);

    unprotector.unprotect(protectedBuf, out, alloc);
    assertThat(out.size()).isEqualTo(2);
    ByteBuf out2 = ref((ByteBuf) out.get(1));
    assertThat(out2).isEqualTo(plain);
    assertThat(protectedBuf.refCnt()).isEqualTo(1);

    unprotector.destroy();
}

From source file:io.grpc.netty.NettyHandlerTestBase.java

License:Apache License

protected ByteBuf grpcDataFrame(int streamId, boolean endStream, byte[] content) {
    final ByteBuf compressionFrame = Unpooled.buffer(content.length);
    MessageFramer framer = new MessageFramer(new MessageFramer.Sink() {
        @Override/* ww w. j  a  v a 2 s  . c o  m*/
        public void deliverFrame(WritableBuffer frame, boolean endOfStream, boolean flush, int numMessages) {
            if (frame != null) {
                ByteBuf bytebuf = ((NettyWritableBuffer) frame).bytebuf();
                compressionFrame.writeBytes(bytebuf);
            }
        }
    }, new NettyWritableBufferAllocator(ByteBufAllocator.DEFAULT), StatsTraceContext.NOOP);
    framer.writePayload(new ByteArrayInputStream(content));
    framer.flush();
    ChannelHandlerContext ctx = newMockContext();
    new DefaultHttp2FrameWriter().writeData(ctx, streamId, compressionFrame, 0, endStream, newPromise());
    return captureWrite(ctx);
}

From source file:io.lettuce.core.protocol.CommandHandlerTest.java

License:Apache License

@Before
public void before() throws Exception {

    when(context.channel()).thenReturn(channel);
    when(context.alloc()).thenReturn(ByteBufAllocator.DEFAULT);
    when(channel.pipeline()).thenReturn(pipeline);
    when(channel.eventLoop()).thenReturn(eventLoop);
    when(channel.remoteAddress()).thenReturn(new InetSocketAddress(Inet4Address.getLocalHost(), 1234));
    when(channel.localAddress()).thenReturn(new InetSocketAddress(Inet4Address.getLocalHost(), 1234));
    when(channel.config()).thenReturn(config);
    when(eventLoop.submit(any(Runnable.class))).thenAnswer(invocation -> {
        Runnable r = (Runnable) invocation.getArguments()[0];
        r.run();//from   w ww . ja va  2s  .co m
        return null;
    });

    when(latencyCollector.isEnabled()).thenReturn(true);
    when(clientResources.commandLatencyCollector()).thenReturn(latencyCollector);
    when(clientResources.tracing()).thenReturn(Tracing.disabled());

    sut = new CommandHandler(ClientOptions.create(), clientResources, endpoint);
    stack = (Queue) ReflectionTestUtils.getField(sut, "stack");
}

From source file:io.lettuce.core.pubsub.PubSubCommandHandlerTest.java

License:Apache License

@SuppressWarnings("unchecked")
@Before// ww w .  ja v  a 2s .c  o m
public void before() throws Exception {

    when(channel.config()).thenReturn(channelConfig);
    when(context.alloc()).thenReturn(ByteBufAllocator.DEFAULT);
    when(context.channel()).thenReturn(channel);
    when(channel.pipeline()).thenReturn(pipeline);
    when(channel.eventLoop()).thenReturn(eventLoop);
    when(eventLoop.submit(any(Runnable.class))).thenAnswer(invocation -> {
        Runnable r = (Runnable) invocation.getArguments()[0];
        r.run();
        return null;
    });

    when(clientResources.commandLatencyCollector())
            .thenReturn(new DefaultCommandLatencyCollector(DefaultCommandLatencyCollectorOptions.create()));
    when(clientResources.tracing()).thenReturn(Tracing.disabled());

    sut = new PubSubCommandHandler<>(ClientOptions.create(), clientResources, StringCodec.UTF8, endpoint);
    stack = (Queue) ReflectionTestUtils.getField(sut, "stack");
}

From source file:io.pravega.shared.protocol.netty.AppendEncodeDecodeTest.java

License:Open Source License

@Test(expected = InvalidMessageException.class)
public void testAppendWithoutSetup() throws Exception {
    int size = 10;
    @Cleanup("release")
    ByteBuf fakeNetwork = ByteBufAllocator.DEFAULT.buffer();
    append(streamName, connectionId, 0, 1, size, fakeNetwork);
}