Search in sources :

Example 31 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.

the class AltsTsiFrameProtectorTest method parserHeader_frameLengthNegativeFails.

@Test
public void parserHeader_frameLengthNegativeFails() 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(), ref);
    in.writeIntLE(-1);
    in.writeIntLE(6);
    try {
        unprotector.unprotect(in, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too small");
    }
    unprotector.destroy();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 32 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.

the class AltsTsiFrameProtectorTest method parseHeader_frameFailFragment.

@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);
    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();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 33 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.

the class AltsTsiFrameProtectorTest method parseFrame_twoFramesNoFragment_Leftover.

@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);
    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();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 34 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.

the class AltsTsiFrameProtectorTest method parserHeader_frameTypeInvalid.

@Test
public void parserHeader_frameTypeInvalid() 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);
    in.writeIntLE(FRAME_MIN_SIZE);
    in.writeIntLE(5);
    try {
        unprotector.unprotect(in, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame type");
    }
    unprotector.destroy();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Example 35 with ByteBufAllocator

use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.

the class AltsTsiFrameProtectorTest method parserHeader_frameTooLarge.

@Test
public void parserHeader_frameTooLarge() 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);
    in.writeIntLE(AltsTsiFrameProtector.getLimitMaxAllowedFrameSize() - AltsTsiFrameProtector.getHeaderLenFieldBytes() + 1);
    in.writeIntLE(6);
    try {
        unprotector.unprotect(in, out, alloc);
        fail("Exception expected");
    } catch (IllegalArgumentException ex) {
        assertThat(ex).hasMessageThat().contains("Invalid header field: frame size too large");
    }
    unprotector.destroy();
}
Also used : ByteBufAllocator(io.netty.buffer.ByteBufAllocator) ArrayList(java.util.ArrayList) ByteBuf(io.netty.buffer.ByteBuf) Test(org.junit.Test)

Aggregations

ByteBufAllocator (io.netty.buffer.ByteBufAllocator)79 ByteBuf (io.netty.buffer.ByteBuf)48 ArrayList (java.util.ArrayList)17 Test (org.junit.Test)17 Test (org.junit.jupiter.api.Test)11 PooledByteBufAllocator (io.netty.buffer.PooledByteBufAllocator)10 Channel (io.netty.channel.Channel)9 EmbeddedChannel (io.netty.channel.embedded.EmbeddedChannel)8 IOException (java.io.IOException)8 UnpooledByteBufAllocator (io.netty.buffer.UnpooledByteBufAllocator)7 ChannelHandlerContext (io.netty.channel.ChannelHandlerContext)7 ChannelFuture (io.netty.channel.ChannelFuture)6 Mono (reactor.core.publisher.Mono)6 CompositeByteBuf (io.netty.buffer.CompositeByteBuf)4 InetSocketAddress (java.net.InetSocketAddress)4 StandardCharsets (java.nio.charset.StandardCharsets)4 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)4 NettyDataBufferFactory (org.springframework.core.io.buffer.NettyDataBufferFactory)4 RecvByteBufAllocator (io.netty.channel.RecvByteBufAllocator)3 ByteBuffer (java.nio.ByteBuffer)3