Java Code Examples for io.netty.buffer.UnpooledByteBufAllocator#buffer()

The following examples show how to use io.netty.buffer.UnpooledByteBufAllocator#buffer() . 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 testParser() throws Exception {
  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  CollectdParser parser = new CollectdParser(getContext(), false, null, false, null, CHARSET);
  byte[] bytes = Files.readAllBytes(SINGLE_PACKET.toPath());
  ByteBuf buf = allocator.buffer(bytes.length);
  buf.writeBytes(bytes);
  List<Record> records = parser.parse(buf, null, null);

  Assert.assertEquals(23, records.size()); // 23 Value parts

  Record record0 = records.get(0);
  UDPTestUtil.verifyCollectdRecord(UDPTestUtil.expectedRecord0, record0);

  Record record2 = records.get(2);
  UDPTestUtil.verifyCollectdRecord(UDPTestUtil.expectedRecord2, record2);

}
 
Example 2
Source File: TestCollectdParser.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testParserExcludeInterval() throws Exception {
  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  CollectdParser parser = new CollectdParser(getContext(), false, null, true, null, CHARSET);
  byte[] bytes = Files.readAllBytes(SINGLE_PACKET.toPath());
  ByteBuf buf = allocator.buffer(bytes.length);
  buf.writeBytes(bytes);
  List<Record> records = parser.parse(buf, null, null);

  Assert.assertEquals(23, records.size()); // 23 Value parts

  Record record0 = records.get(0);
  UDPTestUtil.verifyCollectdRecord(UDPTestUtil.expectedRecordNoInterval0, record0);

  Record record2 = records.get(2);
  UDPTestUtil.verifyCollectdRecord(UDPTestUtil.expectedRecordNoInterval2, record2);

}
 
Example 3
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 4
Source File: TestSyslogParser.java    From datacollector with Apache License 2.0 6 votes vote down vote up
@Test
public void testParseFailure() throws Exception {
  SyslogParser parser = new SyslogParser(getContext(), StandardCharsets.UTF_8);
  String msg = "<123>                    ";
  byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  ByteBuf buffer = allocator.buffer(bytes.length);
  buffer.writeBytes(bytes);
  try {
    parser.parse(
        buffer,
        InetSocketAddress.createUnresolved("localhost", 5000),
        InetSocketAddress.createUnresolved("localhost", 50000)
    );
    Assert.fail("Expected OnRecordErrorException");
  } catch (OnRecordErrorException ex) {
    Record record = ex.getRecord();
    Assert.assertEquals(msg, record.get().getValueAsString());
  }
}
 
Example 5
Source File: TestNetflowParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test(expected = OnRecordErrorException.class)
public void testInvalidVersion() throws Exception {
  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  NetflowParser netflowParser = makeNetflowParser();
  ByteBuf buf = allocator.buffer(4);
  buf.writeShort(0);
  buf.writeShort(0);
  netflowParser.parse(buf, null, null);
}
 
Example 6
Source File: TestNetflowParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test(expected = OnRecordErrorException.class)
public void testInvalidCountInvalidLength() throws Exception {
  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  NetflowParser netflowParser = makeNetflowParser();
  ByteBuf buf = allocator.buffer(4);
  buf.writeShort(5);
  buf.writeShort(1);
  netflowParser.parse(buf, null, null);
}
 
Example 7
Source File: TestNetflowParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test(expected = OnRecordErrorException.class)
public void testInvalidCountZero() throws Exception {
  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  NetflowParser netflowParser = makeNetflowParser();
  ByteBuf buf = allocator.buffer(4);
  buf.writeShort(5);
  buf.writeShort(0);
  netflowParser.parse(buf, null, null);
}
 
Example 8
Source File: TestNetflowParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test(expected = OnRecordErrorException.class)
public void testInvalidPacketTooShort1() throws Exception {
  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  NetflowParser netflowParser = makeNetflowParser();
  ByteBuf buf = allocator.buffer(0);
  netflowParser.parse(buf, null, null);
}
 
Example 9
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);
}
 
Example 10
Source File: TestNetflowParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testV5() throws Exception {
  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  NetflowParser netflowParser = makeNetflowParser();
  byte[] bytes = Resources.toByteArray(Resources.getResource(TEN_PACKETS));
  ByteBuf buf = allocator.buffer(bytes.length);
  buf.writeBytes(bytes);
  List<Record> records = netflowParser.parse(buf, null, null);
  NetflowTestUtil.assertRecordsForTenPackets(records);
}
 
Example 11
Source File: TestCollectdParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testSignedRecord() throws Exception {
  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  CollectdParser parser = new CollectdParser(getContext(), false, null, false, AUTH_FILE_PATH, CHARSET);
  byte[] bytes = Files.readAllBytes(SINGLE_SIGNED_PACKET.toPath());
  ByteBuf buf = allocator.buffer(bytes.length);
  buf.writeBytes(bytes);
  List<Record> records = parser.parse(buf, null, null);

  Assert.assertEquals(22, records.size()); // 22 value parts
  Record record15 = records.get(15);
  UDPTestUtil.verifyCollectdRecord(UDPTestUtil.signedRecord15, record15);
  LOG.info("Num records: {}", records.size());
}
 
Example 12
Source File: TestSyslogParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageParsing() throws Exception {
  SyslogParser parser = new SyslogParser(getContext(), StandardCharsets.UTF_8);
  List<String> messages = getTestMessageStrings();

  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  // test with default keepFields = false
  for (String msg : messages) {
    byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
    ByteBuf buffer = allocator.buffer(bytes.length);
    buffer.writeBytes(bytes);
    List<Record> records = parser.parse(
        buffer,
        InetSocketAddress.createUnresolved("localhost", 5000),
        InetSocketAddress.createUnresolved("localhost", 50000)
    );
    Assert.assertEquals(1, records.size());
    Assert.assertEquals("Failure to parse known-good syslog message",
      msg, records.get(0).get("/raw").getValueAsString());
    Assert.assertEquals("Failure to parse known-good syslog message",
      "localhost:5000", records.get(0).get("/receiverAddr").getValueAsString());
    Assert.assertEquals("Failure to parse known-good syslog message",
      "localhost:50000", records.get(0).get("/senderAddr").getValueAsString());
    Assert.assertNotNull("Failure to parse known-good syslog message",
      records.get(0).get("/host").getValueAsString());
  }
}
 
Example 13
Source File: TestSyslogParser.java    From datacollector with Apache License 2.0 5 votes vote down vote up
@Test
public void testMessageParsingIPv6() throws Exception {
  SyslogParser parser = new SyslogParser(getContext(), StandardCharsets.UTF_8);
  List<String> messages = getTestMessageStrings();

  UnpooledByteBufAllocator allocator = new UnpooledByteBufAllocator(false);
  // test with default keepFields = false
  for (String msg : messages) {
    byte[] bytes = msg.getBytes(StandardCharsets.UTF_8);
    ByteBuf buffer = allocator.buffer(bytes.length);
    buffer.writeBytes(bytes);
    List<Record> records = parser.parse(
        buffer,
        InetSocketAddress.createUnresolved("::1", 5000),
        InetSocketAddress.createUnresolved("2001:db8::ff00:42:8329", 50000)
    );
    Assert.assertEquals(1, records.size());
    Assert.assertEquals("Failure to parse known-good syslog message",
        msg, records.get(0).get("/raw").getValueAsString());
    Assert.assertEquals("Failure to parse known-good syslog message",
        "[::1]:5000", records.get(0).get("/receiverAddr").getValueAsString());
    Assert.assertEquals("Failure to parse known-good syslog message",
        "[2001:db8::ff00:42:8329]:50000", records.get(0).get("/senderAddr").getValueAsString());
    Assert.assertNotNull("Failure to parse known-good syslog message",
        records.get(0).get("/host").getValueAsString());
  }
}