io.netty.buffer.UnpooledByteBufAllocator Java Examples

The following examples show how to use io.netty.buffer.UnpooledByteBufAllocator. 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: TestCollectdParser.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testEncryptedRecord() throws Exception {
  // If unlimited strength encryption is not available, we cant run this test.
  Assume.assumeFalse(Cipher.getMaxAllowedKeyLength("AES") < 256);

  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  CollectdParser parser = new CollectdParser(getContext(), false, null, false, AUTH_FILE_PATH, CHARSET);
  byte[] bytes = Files.readAllBytes(SINGLE_ENCRYPTED_PACKET.toPath());
  ByteBuf buf = allocator.buffer(bytes.length);
  buf.writeBytes(bytes);
  List<Record> records = parser.parse(buf, null, null);

  Assert.assertEquals(24, records.size()); // 24 value parts
  Record record14 = records.get(14);
  UDPTestUtil.verifyCollectdRecord(UDPTestUtil.encryptedRecord14, record14);
  LOG.info("Num records: {}", records.size());
}
 
Example #2
Source File: JT808MessageDecoder.java    From jt808-server with Apache License 2.0 6 votes vote down vote up
/**
 * 反转义
 */
@Override
public ByteBuf unEscape(ByteBuf source) {
    int low = source.readerIndex();
    int high = source.writerIndex();

    int mark = source.indexOf(low, high, (byte) 0x7d);
    if (mark == -1)
        return source;

    List<ByteBuf> bufList = new ArrayList<>(3);

    int len;
    do {

        len = mark + 2 - low;
        bufList.add(slice(source, low, len));
        low += len;

        mark = source.indexOf(low, high, (byte) 0x7d);
    } while (mark > 0);

    bufList.add(source.slice(low, high - low));

    return new CompositeByteBuf(UnpooledByteBufAllocator.DEFAULT, false, bufList.size(), bufList);
}
 
Example #3
Source File: ByteBufTest.java    From x-pipe with Apache License 2.0 6 votes vote down vote up
@Test
public void testByteBuf() {
    ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer();
    System.out.println("readable bytes: " + byteBuf.readableBytes());

    byteBuf.writeBytes(new byte[]{'+', 'P', 'R', 'O', 'X', 'Y'});

    System.out.println("reader index: " + byteBuf.readerIndex());
    System.out.println("readable bytes: " + byteBuf.readableBytes());

    byteBuf.readByte();
    byteBuf.readByte();

    System.out.println("reader index: " + byteBuf.readerIndex());
    System.out.println("readable bytes: " + byteBuf.readableBytes());
}
 
Example #4
Source File: RxMovieServer.java    From ribbon with Apache License 2.0 6 votes vote down vote up
private Observable<Void> handleRecommendationsByUserId(HttpServerRequest<ByteBuf> request, HttpServerResponse<ByteBuf> response) {
    System.out.println("HTTP request -> recommendations by user id request: " + request.getPath());
    final String userId = userIdFromPath(request.getPath());
    if (userId == null) {
        response.setStatus(HttpResponseStatus.BAD_REQUEST);
        return response.close();
    }
    if (!userRecommendations.containsKey(userId)) {
        response.setStatus(HttpResponseStatus.NOT_FOUND);
        return response.close();
    }

    StringBuilder builder = new StringBuilder();
    for (String movieId : userRecommendations.get(userId)) {
        System.out.println("    returning: " + movies.get(movieId));
        builder.append(movies.get(movieId)).append('\n');
    }

    ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer();
    byteBuf.writeBytes(builder.toString().getBytes(Charset.defaultCharset()));

    response.write(byteBuf);
    return response.close();
}
 
Example #5
Source File: DataBufferFactoryWrapperTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void usingNettyDataBufferFactory_HttpData() {
    final DataBufferFactoryWrapper<?> wrapper =
            new DataBufferFactoryWrapper<>(new NettyDataBufferFactory(UnpooledByteBufAllocator.DEFAULT));

    final HttpData httpData1 = HttpData.ofUtf8("abc");

    final DataBuffer buffer = wrapper.toDataBuffer(httpData1);
    assertThat(buffer).isInstanceOf(NettyDataBuffer.class);
    assertThat(((NettyDataBuffer) buffer).getNativeBuffer().refCnt()).isOne();

    final HttpData httpData2 = wrapper.toHttpData(buffer);
    assertThat(httpData2).isInstanceOf(PooledHttpData.class);
    assertThat(((PooledHttpData) httpData2).content())
            .isEqualTo(((NettyDataBuffer) buffer).getNativeBuffer());
    assertThat(((PooledHttpData) httpData2).refCnt()).isOne();
}
 
Example #6
Source File: GrpcRequestHandlerTest.java    From xio with Apache License 2.0 6 votes vote down vote up
private ByteBuf bufferFor(
    com.google.protobuf.GeneratedMessageV3 protoObject, boolean compressed) {
  byte[] dataBytes = protoObject.toByteArray();
  int length = dataBytes.length;
  byte[] lengthByteBuffer = ByteBuffer.allocate(4).putInt(length).array();
  int compressedFlag = compressed ? 1 : 0;
  byte[] compressedByteBuffer = ByteBuffer.allocate(1).put((byte) compressedFlag).array();

  ByteBuf grpcRequestBuffer = UnpooledByteBufAllocator.DEFAULT.buffer(length + 5, length + 5);

  grpcRequestBuffer.writeBytes(compressedByteBuffer);
  grpcRequestBuffer.writeBytes(lengthByteBuffer);
  grpcRequestBuffer.writeBytes(dataBytes);

  return grpcRequestBuffer;
}
 
Example #7
Source File: RedisEncoderBenchmark.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Setup(Level.Trial)
public void setup() {
    byte[] bytes = new byte[256];
    content = Unpooled.buffer(bytes.length);
    content.writeBytes(bytes);
    ByteBuf testContent = Unpooled.unreleasableBuffer(content.asReadOnly());

    List<RedisMessage> rList = new ArrayList<RedisMessage>(arraySize);
    for (int i = 0; i < arraySize; ++i) {
        rList.add(new FullBulkStringRedisMessage(testContent));
    }
    redisArray = new ArrayRedisMessage(rList);
    encoder = new RedisEncoder();
    context = new EmbeddedChannelWriteReleaseHandlerContext(pooledAllocator ? PooledByteBufAllocator.DEFAULT :
            UnpooledByteBufAllocator.DEFAULT, encoder) {
        @Override
        protected void handleException(Throwable t) {
            handleUnexpectedException(t);
        }
    };
}
 
Example #8
Source File: Http1ClientCodecUnitTest.java    From xio with Apache License 2.0 6 votes vote down vote up
@Test
public void testFullRequestWithBody() throws Exception {
  outputReceived = new CountDownLatch(1);
  ByteBuf body = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "body");
  FullRequest requestIn = RequestBuilders.newPost("/").body(body).build();

  channel.writeOutbound(requestIn);

  channel.runPendingTasks(); // blocks

  Uninterruptibles.awaitUninterruptibly(outputReceived);

  FullHttpRequest requestOut = (FullHttpRequest) requests.remove(0);

  assertTrue(requestOut != null);
  assertEquals(HTTP_1_1, requestOut.protocolVersion());
  assertEquals(HttpMethod.POST, requestOut.method());
  assertEquals("/", requestOut.uri());
  assertFalse(requestOut.content() == null);
  assertEquals(body, requestOut.content());
}
 
Example #9
Source File: SSLEngineTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testCloseInboundAfterBeginHandshake() throws Exception {
    SelfSignedCertificate cert = new SelfSignedCertificate();

    clientSslCtx = SslContextBuilder
            .forClient()
            .sslProvider(sslClientProvider())
            .build();
    SSLEngine client = clientSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);

    serverSslCtx = SslContextBuilder
            .forServer(cert.certificate(), cert.privateKey())
            .sslProvider(sslServerProvider())
            .build();
    SSLEngine server = serverSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);

    try {
        testCloseInboundAfterBeginHandshake(client);
        testCloseInboundAfterBeginHandshake(server);
    } finally {
        cleanupClientSslEngine(client);
        cleanupServerSslEngine(server);
        cert.delete();
    }
}
 
Example #10
Source File: SSLEngineTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBeginHandshakeCloseOutbound() throws Exception {
    SelfSignedCertificate cert = new SelfSignedCertificate();

    clientSslCtx = SslContextBuilder
            .forClient()
            .sslProvider(sslClientProvider())
            .build();
    SSLEngine client = clientSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);

    serverSslCtx = SslContextBuilder
            .forServer(cert.certificate(), cert.privateKey())
            .sslProvider(sslServerProvider())
            .build();
    SSLEngine server = serverSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);

    try {
        testBeginHandshakeCloseOutbound(client);
        testBeginHandshakeCloseOutbound(server);
    } finally {
        cleanupClientSslEngine(client);
        cleanupServerSslEngine(server);
        cert.delete();
    }
}
 
Example #11
Source File: SSLEngineTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testBeginHandshakeAfterEngineClosed() throws SSLException {
    clientSslCtx = SslContextBuilder
            .forClient()
            .sslProvider(sslClientProvider())
            .build();
    SSLEngine client = clientSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);

    try {
        client.closeInbound();
        client.closeOutbound();
        try {
            client.beginHandshake();
            fail();
        } catch (SSLException expected) {
            // expected
        }
    } finally {
        cleanupClientSslEngine(client);
    }
}
 
Example #12
Source File: SSLEngineTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
private void testProtocol(String[] clientProtocols, String[] serverProtocols) throws Exception {
    SelfSignedCertificate cert = new SelfSignedCertificate();

    clientSslCtx = SslContextBuilder
            .forClient()
            .trustManager(cert.cert())
            .sslProvider(sslClientProvider())
            .protocols(clientProtocols)
            .build();
    SSLEngine client = clientSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);

    serverSslCtx = SslContextBuilder
            .forServer(cert.certificate(), cert.privateKey())
            .sslProvider(sslServerProvider())
            .protocols(serverProtocols)
            .build();
    SSLEngine server = serverSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);

    try {
        handshake(client, server);
    } finally {
        cleanupClientSslEngine(client);
        cleanupServerSslEngine(server);
        cert.delete();
    }
}
 
Example #13
Source File: DataBufferFactoryWrapperTest.java    From armeria with Apache License 2.0 6 votes vote down vote up
@Test
public void usingNettyDataBufferFactory_PooledHttpData() {
    final DataBufferFactoryWrapper<?> wrapper =
            new DataBufferFactoryWrapper<>(new NettyDataBufferFactory(UnpooledByteBufAllocator.DEFAULT));

    final PooledHttpData httpData1 =
            PooledHttpData.wrap(Unpooled.wrappedBuffer("abc".getBytes()));

    final DataBuffer buffer = wrapper.toDataBuffer(httpData1);
    assertThat(buffer).isInstanceOf(NettyDataBuffer.class);
    assertThat(((NettyDataBuffer) buffer).getNativeBuffer().refCnt()).isOne();

    final HttpData httpData2 = wrapper.toHttpData(buffer);
    assertThat(httpData2).isInstanceOf(PooledHttpData.class);
    assertThat(((PooledHttpData) httpData2).content())
            .isEqualTo(((NettyDataBuffer) buffer).getNativeBuffer());
    assertThat(((PooledHttpData) httpData2).refCnt()).isOne();
}
 
Example #14
Source File: CodecTestSupport.java    From r2dbc-mysql with Apache License 2.0 6 votes vote down vote up
@Test
default void stringify() {
    Codec<T> codec = getCodec(UnpooledByteBufAllocator.DEFAULT);
    T[] origin = originParameters();
    Object[] strings = stringifyParameters();

    assertEquals(origin.length, strings.length);

    for (int i = 0; i < origin.length; ++i) {
        ParameterWriter writer = ParameterWriterHelper.get(1);
        codec.encode(origin[i], context())
            .publishText(writer)
            .as(StepVerifier::create)
            .verifyComplete();
        assertEquals(ParameterWriterHelper.toSql(writer), strings[i].toString());
    }
}
 
Example #15
Source File: PersistentMessageFinderTest.java    From pulsar with Apache License 2.0 6 votes vote down vote up
public static byte[] createMessageWrittenToLedger(String msg) throws Exception {
    PulsarApi.MessageMetadata.Builder messageMetadataBuilder = PulsarApi.MessageMetadata.newBuilder();
    messageMetadataBuilder.setPublishTime(System.currentTimeMillis());
    messageMetadataBuilder.setProducerName("createMessageWrittenToLedger");
    messageMetadataBuilder.setSequenceId(1);
    PulsarApi.MessageMetadata messageMetadata = messageMetadataBuilder.build();
    ByteBuf data = UnpooledByteBufAllocator.DEFAULT.heapBuffer().writeBytes(msg.getBytes());

    int msgMetadataSize = messageMetadata.getSerializedSize();
    int payloadSize = data.readableBytes();
    int totalSize = 4 + msgMetadataSize + payloadSize;

    ByteBuf headers = PulsarByteBufAllocator.DEFAULT.heapBuffer(totalSize, totalSize);
    ByteBufCodedOutputStream outStream = ByteBufCodedOutputStream.get(headers);
    headers.writeInt(msgMetadataSize);
    messageMetadata.writeTo(outStream);
    ByteBuf headersAndPayload = ByteBufPair.coalesce(ByteBufPair.get(headers, data));
    byte[] byteMessage = headersAndPayload.nioBuffer().array();
    headersAndPayload.release();
    return byteMessage;
}
 
Example #16
Source File: OpenSslEngineTest.java    From netty-4.1.22 with Apache License 2.0 6 votes vote down vote up
@Test
public void testSNIMatchersDoesNotThrow() throws Exception {
    assumeTrue(PlatformDependent.javaVersion() >= 8);
    SelfSignedCertificate ssc = new SelfSignedCertificate();
    serverSslCtx = SslContextBuilder.forServer(ssc.certificate(), ssc.privateKey())
            .sslProvider(sslServerProvider())
            .build();

    SSLEngine engine = serverSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
    try {
        SSLParameters parameters = new SSLParameters();
        Java8SslTestUtils.setSNIMatcher(parameters);
        engine.setSSLParameters(parameters);
    } finally {
        cleanupServerSslEngine(engine);
        ssc.delete();
    }
}
 
Example #17
Source File: Http1ServerCodecUnitTest.java    From xio with Apache License 2.0 5 votes vote down vote up
@Test
public void testFullResponse() throws Exception {
  outputReceived = new CountDownLatch(2);
  ByteBuf body = ByteBufUtil.writeUtf8(UnpooledByteBufAllocator.DEFAULT, "response");

  FullHttpRequest requestIn = new DefaultFullHttpRequest(HTTP_1_1, GET, "/");
  FullResponse responseIn = ResponseBuilders.newOk().body(body).build();

  channel.writeInbound(requestIn);
  channel.runPendingTasks(); // blocks
  channel.writeOutbound(responseIn);

  channel.runPendingTasks(); // blocks

  Uninterruptibles.awaitUninterruptibly(outputReceived);

  HttpResponse responseOut = (HttpResponse) responses.remove(0);

  assertTrue(responseOut != null);
  assertTrue(responseOut instanceof FullHttpResponse);
  assertEquals(HTTP_1_1, responseOut.protocolVersion());
  assertEquals(OK, responseOut.status());
  assertFalse(((FullHttpResponse) responseOut).content() == null);
  assertEquals(body, ((FullHttpResponse) responseOut).content());

  // https://tools.ietf.org/html/rfc7230#section-3.3.2
  assertNotNull(responseOut.headers().get(HttpHeaderNames.CONTENT_LENGTH));
  assertNull(responseOut.headers().get(HttpHeaderNames.TRANSFER_ENCODING));
}
 
Example #18
Source File: HystrixMetricsStreamHandler.java    From ReactiveLab with Apache License 2.0 5 votes vote down vote up
@SuppressWarnings("unchecked")
private void writeMetric(String json, HttpServerResponse<O> response) {
    byte[] bytes = json.getBytes(Charset.defaultCharset());
    ByteBuf byteBuf = UnpooledByteBufAllocator.DEFAULT.buffer(bytes.length + EXTRA_SPACE);
    byteBuf.writeBytes(HEADER);
    byteBuf.writeBytes(bytes);
    byteBuf.writeBytes(FOOTER);
    response.writeAndFlush((O) byteBuf);
}
 
Example #19
Source File: CoreMetrics.java    From styx with Apache License 2.0 5 votes vote down vote up
private static void registerJvmMetrics(MetricRegistry metricRegistry) {
    RuntimeMXBean runtimeMxBean = getRuntimeMXBean();

    MetricRegistry scoped = metricRegistry.scope("jvm");
    scoped.register("bufferpool", new BufferPoolMetricSet(getPlatformMBeanServer()));
    scoped.register("memory", new MemoryUsageGaugeSet());
    scoped.register("thread", new ThreadStatesGaugeSet());
    scoped.register("gc", new GarbageCollectorMetricSet());
    scoped.register("uptime", (Gauge<Long>) runtimeMxBean::getUptime);
    scoped.register("uptime.formatted", (Gauge<String>) () -> formatTime(runtimeMxBean.getUptime()));
    scoped.register("netty", new NettyAllocatorMetricSet("pooled-allocator", PooledByteBufAllocator.DEFAULT.metric()));
    scoped.register("netty", new NettyAllocatorMetricSet("unpooled-allocator", UnpooledByteBufAllocator.DEFAULT.metric()));
}
 
Example #20
Source File: CopyByteBufHandlerTest.java    From servicetalk with Apache License 2.0 5 votes vote down vote up
@Test
public void doesNotProcessByteBufHolder() {
    CopyByteBufHandler handler = new CopyByteBufHandler(UnpooledByteBufAllocator.DEFAULT);
    ChannelHandlerContext ctx = mock(ChannelHandlerContext.class);
    ByteBuf buf = mock(ByteBuf.class);

    IllegalArgumentException ex = assertThrows(IllegalArgumentException.class,
            // Use DatagramPacket as a ByteBufHolder implementation:
            () -> handler.channelRead(ctx, new DatagramPacket(buf, mock(InetSocketAddress.class))));
    assertThat(ex.getMessage(), startsWith("Unexpected ReferenceCounted msg"));

    verify(ctx, never()).fireChannelRead(any());
    verify(buf).release();
}
 
Example #21
Source File: AbstractDataBufferAllocatingTestCase.java    From java-technology-stack with MIT License 5 votes vote down vote up
@Parameterized.Parameters(name = "{0}")
public static Object[][] dataBufferFactories() {
	return new Object[][] {
			{new NettyDataBufferFactory(new UnpooledByteBufAllocator(true))},
			{new NettyDataBufferFactory(new UnpooledByteBufAllocator(false))},
			// disable caching for reliable leak detection, see https://github.com/netty/netty/issues/5275
			{new NettyDataBufferFactory(new PooledByteBufAllocator(true, 1, 1, 8192, 11, 0, 0, 0, true))},
			{new NettyDataBufferFactory(new PooledByteBufAllocator(false, 1, 1, 8192, 11, 0, 0, 0, true))},
			{new DefaultDataBufferFactory(true)},
			{new DefaultDataBufferFactory(false)}

	};
}
 
Example #22
Source File: SSLEngineTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetCreationTime() throws Exception {
    clientSslCtx = SslContextBuilder.forClient()
            .sslProvider(sslClientProvider())
            .sslContextProvider(clientSslContextProvider()).build();
    SSLEngine engine = null;
    try {
        engine = clientSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
        assertTrue(engine.getSession().getCreationTime() <= System.currentTimeMillis());
    } finally {
        cleanupClientSslEngine(engine);
    }
}
 
Example #23
Source File: DelegatingSslContextTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitEngineOnNewSslHandler() throws Exception {
    SslContext delegating = newDelegatingSslContext();

    SslHandler handler = delegating.newHandler(UnpooledByteBufAllocator.DEFAULT);
    Assert.assertArrayEquals(EXPECTED_PROTOCOLS, handler.engine().getEnabledProtocols());

    handler = delegating.newHandler(UnpooledByteBufAllocator.DEFAULT, "localhost", 9090);
    Assert.assertArrayEquals(EXPECTED_PROTOCOLS, handler.engine().getEnabledProtocols());
}
 
Example #24
Source File: SslContextGMBuilderTest.java    From julongchain with Apache License 2.0 5 votes vote down vote up
@Test
public void testServerContext() throws Exception {
    SslContextGMBuilder builder = SslContextGMBuilder.forServer(ENC_CERT, ENC_KEY, SIGN_CERT, SIGN_KEY, null)
                                                 .trustManager(TRUST_CERT)
                                                 .clientAuth(ClientAuth.REQUIRE);
    SslContext context = builder.build();
    SSLEngine engine = context.newEngine(UnpooledByteBufAllocator.DEFAULT);
    assertFalse(engine.getWantClientAuth());
    assertTrue(engine.getNeedClientAuth());
    engine.closeInbound();
    engine.closeOutbound();
}
 
Example #25
Source File: DelegatingSslContextTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
@Test
public void testInitEngineOnNewEngine() throws Exception {
    SslContext delegating = newDelegatingSslContext();

    SSLEngine engine = delegating.newEngine(UnpooledByteBufAllocator.DEFAULT);
    Assert.assertArrayEquals(EXPECTED_PROTOCOLS, engine.getEnabledProtocols());

    engine = delegating.newEngine(UnpooledByteBufAllocator.DEFAULT, "localhost", 9090);
    Assert.assertArrayEquals(EXPECTED_PROTOCOLS, engine.getEnabledProtocols());
}
 
Example #26
Source File: OpenSslEngineTest.java    From netty-4.1.22 with Apache License 2.0 5 votes vote down vote up
private void testWrapWithDifferentSizes(String protocol, String cipher) throws Exception {
    assumeTrue(OpenSsl.SUPPORTED_PROTOCOLS_SET.contains(protocol));
    if (!OpenSsl.isCipherSuiteAvailable(cipher)) {
        return;
    }

    SSLEngine clientEngine = null;
    SSLEngine serverEngine = null;
    try {
        clientEngine = clientSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
        serverEngine = serverSslCtx.newEngine(UnpooledByteBufAllocator.DEFAULT);
        clientEngine.setEnabledCipherSuites(new String[] { cipher });
        clientEngine.setEnabledProtocols(new String[] { protocol });
        serverEngine.setEnabledCipherSuites(new String[] { cipher });
        serverEngine.setEnabledProtocols(new String[] { protocol });

        try {
            handshake(clientEngine, serverEngine);
        } catch (SSLException e) {
            if (e.getMessage().contains("unsupported protocol")) {
                Assume.assumeNoException(protocol + " not supported with cipher " + cipher, e);
            }
            throw e;
        }

        int srcLen = 64;
        do {
            testWrapDstBigEnough(clientEngine, srcLen);
            srcLen += 64;
        } while (srcLen < MAX_PLAINTEXT_LENGTH);

        testWrapDstBigEnough(clientEngine, MAX_PLAINTEXT_LENGTH);
    } finally {
        cleanupClientSslEngine(clientEngine);
        cleanupServerSslEngine(serverEngine);
    }
}
 
Example #27
Source File: GrpcRequestHandlerTest.java    From xio with Apache License 2.0 5 votes vote down vote up
private <T extends GeneratedMessageV3> T protoObjectFor(
    ByteBuf originalBuffer, GrpcRequestParser<T> parser) {
  int size = originalBuffer.slice(1, 4).readInt();
  ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer(size, size);
  buffer.writeBytes(originalBuffer.slice(5, size));

  try {
    return parser.parse(buffer.nioBuffer());
  } catch (InvalidProtocolBufferException e) {
    e.printStackTrace();
    throw new RuntimeException(e);
  }
}
 
Example #28
Source File: NettyServerStreamTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void closeAfterClientHalfCloseShouldSucceed() throws Exception {
  ListMultimap<CharSequence, CharSequence> expectedHeaders =
      ImmutableListMultimap.copyOf(new DefaultHttp2Headers()
          .status(new AsciiString("200"))
          .set(new AsciiString("content-type"), new AsciiString("application/grpc"))
          .set(new AsciiString("grpc-status"), new AsciiString("0")));

  // Client half-closes. Listener gets halfClosed()
  stream().transportState()
      .inboundDataReceived(new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT), true);

  verify(serverListener).halfClosed();

  // Server closes. Status sent
  stream().close(Status.OK, trailers);
  assertNull("no message expected", listenerMessageQueue.poll());

  ArgumentCaptor<SendResponseHeadersCommand> cmdCap =
      ArgumentCaptor.forClass(SendResponseHeadersCommand.class);
  verify(writeQueue).enqueue(cmdCap.capture(), eq(true));
  SendResponseHeadersCommand cmd = cmdCap.getValue();
  assertThat(cmd.stream()).isSameInstanceAs(stream.transportState());
  assertThat(ImmutableListMultimap.copyOf(cmd.headers()))
      .containsExactlyEntriesIn(expectedHeaders);
  assertThat(cmd.endOfStream()).isTrue();

  // Sending and receiving complete. Listener gets closed()
  stream().transportState().complete();
  verify(serverListener).closed(Status.OK);
  assertNull("no message expected", listenerMessageQueue.poll());
}
 
Example #29
Source File: NettyServerStreamTest.java    From grpc-java with Apache License 2.0 5 votes vote down vote up
@Test
public void abortStreamAfterClientHalfCloseShouldCallClose() {
  Status status = Status.INTERNAL.withCause(new Throwable());
  // Client half-closes. Listener gets halfClosed()
  stream().transportState().inboundDataReceived(
      new EmptyByteBuf(UnpooledByteBufAllocator.DEFAULT), true);
  verify(serverListener).halfClosed();
  // Abort from the transport layer
  stream().transportState().transportReportStatus(status);
  verify(serverListener).closed(same(status));
  assertNull("no message expected", listenerMessageQueue.poll());
}
 
Example #30
Source File: TestNetflowParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test(expected = OnRecordErrorException.class)
public void testInvalidPacketTooShort2() throws Exception {
  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  NetflowParser netflowParser = makeNetflowParser();
  ByteBuf buf = allocator.buffer(2);
  buf.writeShort(5);
  netflowParser.parse(buf, null, null);
}