use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.
the class AltsTsiFrameProtectorTest method parserHeader_EmptyUnprotectNoRetain.
@Test
public void parserHeader_EmptyUnprotectNoRetain() throws GeneralSecurityException {
ByteBufAllocator alloc = ByteBufAllocator.DEFAULT;
List<Object> out = new ArrayList<>();
FakeChannelCrypter crypter = new FakeChannelCrypter();
AltsTsiFrameProtector.Unprotector unprotector = new AltsTsiFrameProtector.Unprotector(crypter, alloc);
ByteBuf emptyBuf = getDirectBuffer(0, ref);
unprotector.unprotect(emptyBuf, out, alloc);
assertThat(emptyBuf.refCnt()).isEqualTo(1);
unprotector.destroy();
}
use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.
the class AltsTsiFrameProtectorTest method parseFrame_twoFramesNoFragment.
@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(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();
}
use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.
the class AltsTsiFrameProtectorTest method parseFrame_twoFramesFragmentSecond.
@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);
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();
}
use of io.netty.buffer.ByteBufAllocator in project grpc-java by grpc.
the class AltsTsiFrameProtectorTest method parserHeader_frameZeroOk.
@Test
public void parserHeader_frameZeroOk() 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(6);
unprotector.unprotect(in, out, alloc);
assertThat(in.readableBytes()).isEqualTo(0);
unprotector.destroy();
}
use of io.netty.buffer.ByteBufAllocator in project baseio by generallycloud.
the class TestNettyByteBuf method test_exp.
static void test_exp() {
ByteBufAllocator a = ByteBufAllocator.DEFAULT;
ByteBuf buf = a.buffer(512);
System.out.println(buf);
buf.writerIndex(508);
buf.writeLong(1L);
System.out.println(buf);
}
Aggregations