Java Code Examples for io.netty.buffer.ByteBufAllocator#DEFAULT

The following examples show how to use io.netty.buffer.ByteBufAllocator#DEFAULT . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AltsTsiFrameProtectorTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 2
Source File: AltsTsiFrameProtectorTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@Test
public void parserHeader_frameTooSmall() 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);
  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();
}
 
Example 3
Source File: AltsTsiFrameProtectorTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@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.getLimitMaxAllowedFrameBytes()
          - 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();
}
 
Example 4
Source File: AltsTsiFrameProtectorTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 5
Source File: AltsTsiFrameProtectorTest.java    From grpc-nebula-java with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 6
Source File: AltsTsiFrameProtectorTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@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);
  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();
}
 
Example 7
Source File: InboundEnvelopeDecoderTest.java    From stratosphere with Apache License 2.0 6 votes vote down vote up
private static ByteBuf encode(EmbeddedChannel ch, Envelope... envelopes) {
	for (Envelope env : envelopes) {
		ch.writeOutbound(env);

		if (env.getBuffer() != null) {
			verify(env.getBuffer(), times(1)).recycleBuffer();
		}
	}

	CompositeByteBuf encodedEnvelopes = new CompositeByteBuf(ByteBufAllocator.DEFAULT, false, envelopes.length);

	ByteBuf buf;
	while ((buf = (ByteBuf) ch.readOutbound()) != null) {
		encodedEnvelopes.addComponent(buf);
	}

	return encodedEnvelopes.writerIndex(encodedEnvelopes.capacity());
}
 
Example 8
Source File: AltsTsiFrameProtectorTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@Test
public void parserHeader_frameTooSmall() 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);
  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();
}
 
Example 9
Source File: AltsTsiFrameProtectorTest.java    From grpc-java with Apache License 2.0 6 votes vote down vote up
@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();
}
 
Example 10
Source File: GrpcMessageMarshallerTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void deserializeRequest_wrappedByteBuf() throws Exception {
    marshaller = new GrpcMessageMarshaller<>(
            ByteBufAllocator.DEFAULT,
            GrpcSerializationFormats.PROTO,
            TestServiceGrpc.getUnaryCallMethod(),
            DEFAULT_JSON_MARSHALLER,
            true);
    final ByteBuf buf = ByteBufAllocator.DEFAULT.buffer(GrpcTestUtil.REQUEST_MESSAGE.getSerializedSize());
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.writeBytes(GrpcTestUtil.REQUEST_MESSAGE.toByteArray());
    final SimpleRequest request = marshaller.deserializeRequest(new DeframedMessage(buf, 0));
    assertThat(request).isEqualTo(GrpcTestUtil.REQUEST_MESSAGE);
    assertThat(buf.refCnt()).isEqualTo(1);
    buf.release();
}
 
Example 11
Source File: AltsTsiFrameProtectorTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@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(
          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();
}
 
Example 12
Source File: HttpDecodedResponseTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void unpooledPayload_pooledDrain() {
    final HttpData payload = HttpData.wrap(PAYLOAD);
    final HttpResponse delegate = HttpResponse.of(RESPONSE_HEADERS, payload);
    final HttpResponse decoded = new HttpDecodedResponse(delegate, DECODERS, ByteBufAllocator.DEFAULT);
    final ByteBuf buf = responseBuf(decoded, true);

    assertThat(buf).isNotInstanceOf(UnpooledHeapByteBuf.class);
}
 
Example 13
Source File: ReferenceCountedOpenSslContext.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
/**
 * Return the pointer to a <a href="https://www.openssl.org/docs/crypto/BIO_get_mem_ptr.html">in-memory BIO</a>
 * or {@code 0} if the {@code key} is {@code null}. The BIO contains the content of the {@code key}.如果键为空,返回指向内存中的BIO或0的指针。BIO包含密钥的内容。
 */
static long toBIO(PrivateKey key) throws Exception {
    if (key == null) {
        return 0;
    }

    ByteBufAllocator allocator = ByteBufAllocator.DEFAULT;
    PemEncoded pem = PemPrivateKey.toPEM(allocator, true, key);
    try {
        return toBIO(allocator, pem.retain());
    } finally {
        pem.release();
    }
}
 
Example 14
Source File: HttpDecodedResponseTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void unpooledPayload_unpooledDrain() {
    final HttpData payload = HttpData.wrap(PAYLOAD);
    final HttpResponse delegate = HttpResponse.of(RESPONSE_HEADERS, payload);
    final HttpResponse decoded = new HttpDecodedResponse(delegate, DECODERS, ByteBufAllocator.DEFAULT);
    final ByteBuf buf = responseBuf(decoded, false);

    assertThat(buf).isInstanceOf(UnpooledHeapByteBuf.class);
}
 
Example 15
Source File: FrameHeaderFlyweightPerf.java    From rsocket-java with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup(Blackhole bh) {
  this.bh = bh;
  this.frameType = FrameType.REQUEST_RESPONSE;
  allocator = ByteBufAllocator.DEFAULT;
  frame = FrameHeaderCodec.encode(allocator, 123, FrameType.SETUP, 0);
}
 
Example 16
Source File: AltsTsiFrameProtectorTest.java    From grpc-nebula-java with Apache License 2.0 5 votes vote down vote up
@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();
}
 
Example 17
Source File: HttpDecodedResponseTest.java    From armeria with Apache License 2.0 5 votes vote down vote up
@Test
void pooledPayload_unpooledDrain() {
    final PooledHttpData payload = PooledHttpData.wrap(
            ByteBufAllocator.DEFAULT.buffer().writeBytes(PAYLOAD)).withEndOfStream();
    final HttpResponse delegate = HttpResponse.of(RESPONSE_HEADERS, payload);
    final HttpResponse decoded = new HttpDecodedResponse(delegate, DECODERS, ByteBufAllocator.DEFAULT);
    final ByteBuf buf = responseBuf(decoded, false);

    assertThat(buf).isInstanceOf(UnpooledHeapByteBuf.class);
    assertThat(payload.refCnt()).isZero();
}
 
Example 18
Source File: AltsTsiFrameProtectorTest.java    From grpc-java with Apache License 2.0 4 votes vote down vote up
@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();
}
 
Example 19
Source File: DriftNettyServerTransport.java    From drift with Apache License 2.0 4 votes vote down vote up
public DriftNettyServerTransport(ServerMethodInvoker methodInvoker, DriftNettyServerConfig config)
{
    this(methodInvoker, config, ByteBufAllocator.DEFAULT);
}
 
Example 20
Source File: DriftNettyServerTransportFactory.java    From drift with Apache License 2.0 4 votes vote down vote up
public DriftNettyServerTransportFactory(DriftNettyServerConfig config)
{
    this(config, ByteBufAllocator.DEFAULT);
}