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();
}
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();
}
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();
}
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();
}
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();
}
Aggregations