Assert.assertArrayEquals(HexUtil.toByteArray("4100"), BufferHelper.createByteArray(buffer0));
}
@Test
public void readTlv() throws Exception {
Tlv tlv0 = null;
ChannelBuffer buffer0 = null;
// a single byte TLV
buffer0 = BufferHelper.createBuffer("0210000134");
tlv0 = ChannelBufferUtil.readTlv(buffer0);
Assert.assertEquals(0, buffer0.readableBytes());
Assert.assertEquals((short)0x0210, tlv0.getTag());
Assert.assertEquals((short)0x01, tlv0.getLength());
Assert.assertArrayEquals(new byte[] {0x34}, tlv0.getValue());
// a C-string TLV
buffer0 = BufferHelper.createBuffer("140200056331657400");
tlv0 = ChannelBufferUtil.readTlv(buffer0);
Assert.assertEquals(0, buffer0.readableBytes());
Assert.assertEquals((short)0x1402, tlv0.getTag());
Assert.assertEquals((short)0x05, tlv0.getLength());
Assert.assertArrayEquals(HexUtil.toByteArray("6331657400"), tlv0.getValue());
// a short or just 2 byte TLV
buffer0 = BufferHelper.createBuffer("02040002ce34");
tlv0 = ChannelBufferUtil.readTlv(buffer0);
Assert.assertEquals(0, buffer0.readableBytes());
Assert.assertEquals((short)0x0204, tlv0.getTag());
Assert.assertEquals((short)0x02, tlv0.getLength());
Assert.assertArrayEquals(HexUtil.toByteArray("ce34"), tlv0.getValue());
// a sample message payload TLV
buffer0 = BufferHelper.createBuffer("0424002f4f4d4720492077616e7420746f207365652022546865204372617a69657322206c6f6f6b73207369636b21203d5d20");
tlv0 = ChannelBufferUtil.readTlv(buffer0);
// OMG I want to see "The Crazies" looks sick! =]
Assert.assertEquals(0, buffer0.readableBytes());
Assert.assertEquals((short)0x0424, tlv0.getTag());
Assert.assertEquals((short)0x2f, tlv0.getLength());
// convert bytes to actual chars
Assert.assertEquals("OMG I want to see \"The Crazies\" looks sick! =] ", new String(tlv0.getValue()));
// multiple TLVs in a row
buffer0 = BufferHelper.createBuffer("000e000101000600010104240001");
tlv0 = ChannelBufferUtil.readTlv(buffer0);
Assert.assertEquals(9, buffer0.readableBytes());
Assert.assertEquals((short)0x0e, tlv0.getTag());
Assert.assertEquals((short)0x01, tlv0.getLength());
Assert.assertArrayEquals(HexUtil.toByteArray("01"), tlv0.getValue());
tlv0 = ChannelBufferUtil.readTlv(buffer0);
Assert.assertEquals(4, buffer0.readableBytes());
Assert.assertEquals((short)0x06, tlv0.getTag());
Assert.assertEquals((short)0x01, tlv0.getLength());
Assert.assertArrayEquals(HexUtil.toByteArray("01"), tlv0.getValue());
try {
// this should error out since we don't have enough bytes
tlv0 = ChannelBufferUtil.readTlv(buffer0);
Assert.fail();
} catch (NotEnoughDataInBufferException e) {
// correct behavior
Assert.assertEquals(0, buffer0.readableBytes());
}
// a TLV with an unsigned short length (1 above 15-bit integer)
StringBuilder buf = new StringBuilder(40000);
buf.append("FFFF8000");
for (int i = 0; i < 0x8000; i++) {
buf.append("01");
}
buffer0 = BufferHelper.createBuffer(buf.toString());
tlv0 = ChannelBufferUtil.readTlv(buffer0);
Assert.assertEquals(0, buffer0.readableBytes());
Assert.assertEquals((short)0xffff, tlv0.getTag());
Assert.assertEquals(32768, tlv0.getUnsignedLength());
Assert.assertEquals(-32768, tlv0.getLength()); // the "signed" version of the length
// a TLV with an unsigned short length (1 above 15-bit integer)
buf = new StringBuilder(70000);
buf.append("FFFFFFFF");
for (int i = 0; i < 0xFFFF; i++) {
buf.append("02");
}
buffer0 = BufferHelper.createBuffer(buf.toString());
tlv0 = ChannelBufferUtil.readTlv(buffer0);
Assert.assertEquals(0, buffer0.readableBytes());
Assert.assertEquals((short)0xffff, tlv0.getTag());
Assert.assertEquals(-1, tlv0.getLength()); // the "signed" version of the length
}