io.netty.buffer.ByteBuf

Here are the examples of the java api class io.netty.buffer.ByteBuf taken from open source projects.

1. SocketSpdyEchoTest#createFrames()

Project: netty
File: SocketSpdyEchoTest.java
private static ByteBuf createFrames(int version) {
    ByteBuf frames = Unpooled.buffer(1174);
    // SPDY UNKNOWN Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(0xFFFF);
    frames.writeByte(0xFF);
    frames.writeMedium(4);
    frames.writeInt(random.nextInt());
    // SPDY NOOP Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(5);
    frames.writeInt(0);
    // SPDY Data Frame
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    frames.writeByte(0x01);
    frames.writeMedium(1024);
    for (int i = 0; i < 256; i++) {
        frames.writeInt(random.nextInt());
    }
    // SPDY SYN_STREAM Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(1);
    frames.writeByte(0x03);
    frames.writeMedium(10);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF);
    frames.writeShort(0x8000);
    if (version < 3) {
        frames.writeShort(0);
    }
    // SPDY SYN_REPLY Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(2);
    frames.writeByte(0x01);
    frames.writeMedium(4);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    if (version < 3) {
        frames.writeInt(0);
    }
    // SPDY RST_STREAM Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(3);
    frames.writeInt(8);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    frames.writeInt(random.nextInt() | 0x01);
    // SPDY SETTINGS Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(4);
    frames.writeByte(0x01);
    frames.writeMedium(12);
    frames.writeInt(1);
    frames.writeByte(0x03);
    frames.writeMedium(random.nextInt());
    frames.writeInt(random.nextInt());
    // SPDY PING Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(6);
    frames.writeInt(4);
    frames.writeInt(random.nextInt());
    // SPDY GOAWAY Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(7);
    frames.writeInt(8);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF);
    frames.writeInt(random.nextInt() | 0x01);
    // SPDY HEADERS Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(8);
    frames.writeByte(0x01);
    frames.writeMedium(4);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    // SPDY WINDOW_UPDATE Frame
    frames.writeByte(0x80);
    frames.writeByte(version);
    frames.writeShort(9);
    frames.writeInt(8);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    frames.writeInt(random.nextInt() & 0x7FFFFFFF | 0x01);
    return frames;
}

2. SpdyHeaderBlockZlibDecoderTest#testHeaderBlockExtraData()

Project: netty
File: SpdyHeaderBlockZlibDecoderTest.java
@Test(expected = SpdyProtocolException.class)
public void testHeaderBlockExtraData() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(37));
    headerBlock.writeBytes(zlibHeader);
    // Non-compressed block
    headerBlock.writeByte(0);
    // little-endian length (21)
    headerBlock.writeByte(0x15);
    // little-endian length (21)
    headerBlock.writeByte(0x00);
    // one's compliment of length
    headerBlock.writeByte(0xea);
    // one's compliment of length
    headerBlock.writeByte(0xff);
    // number of Name/Value pairs
    headerBlock.writeInt(1);
    // length of name
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    // length of value
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    // adler-32 checksum
    headerBlock.writeByte(0x19);
    // adler-32 checksum
    headerBlock.writeByte(0xa5);
    // adler-32 checksum
    headerBlock.writeByte(0x03);
    // adler-32 checksum
    headerBlock.writeByte(0xc9);
    // Data following zlib stream
    headerBlock.writeByte(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
}

3. TestDotNetInterop#testEncodeLong()

Project: TomP2P
File: TestDotNetInterop.java
@Ignore
@Test
public void testEncodeLong() throws Exception {
    ByteBuf buf = Unpooled.buffer();
    //-923372036854775808
    buf.writeLong(Long.MIN_VALUE);
    buf.writeLong(-256);
    buf.writeLong(-255);
    buf.writeLong(-128);
    buf.writeLong(-127);
    buf.writeLong(-1);
    buf.writeLong(0);
    buf.writeLong(1);
    buf.writeLong(127);
    buf.writeLong(128);
    buf.writeLong(255);
    buf.writeLong(256);
    // 923372036854775807
    buf.writeLong(Long.MAX_VALUE);
    byte[] bytes = buf.array();
    writeToFile(bytes);
}

4. TestDotNetInterop#testEncodeInt()

Project: TomP2P
File: TestDotNetInterop.java
@Ignore
@Test
public void testEncodeInt() throws Exception {
    ByteBuf buf = Unpooled.buffer();
    //-2147483648
    buf.writeInt(Integer.MIN_VALUE);
    buf.writeInt(-256);
    buf.writeInt(-255);
    buf.writeInt(-128);
    buf.writeInt(-127);
    buf.writeInt(-1);
    buf.writeInt(0);
    buf.writeInt(1);
    buf.writeInt(127);
    buf.writeInt(128);
    buf.writeInt(255);
    buf.writeInt(256);
    // 2147483647
    buf.writeInt(Integer.MAX_VALUE);
    byte[] bytes = buf.array();
    writeToFile(bytes);
}

5. SpdyHeaderBlockZlibDecoderTest#testHeaderBlock()

Project: netty
File: SpdyHeaderBlockZlibDecoderTest.java
@Test
public void testHeaderBlock() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(37));
    headerBlock.writeBytes(zlibHeader);
    // Non-compressed block
    headerBlock.writeByte(0);
    // little-endian length (21)
    headerBlock.writeByte(0x15);
    // little-endian length (21)
    headerBlock.writeByte(0x00);
    // one's compliment of length
    headerBlock.writeByte(0xea);
    // one's compliment of length
    headerBlock.writeByte(0xff);
    // number of Name/Value pairs
    headerBlock.writeInt(1);
    // length of name
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    // length of value
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeBytes(zlibSyncFlush);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertFalse(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(1, frame.headers().getAll(name).size());
    assertEquals(value, frame.headers().get(name));
}

6. SpdyHeaderBlockZlibDecoderTest#testLargeHeaderName()

Project: netty
File: SpdyHeaderBlockZlibDecoderTest.java
@Test
public void testLargeHeaderName() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(8220));
    headerBlock.writeBytes(zlibHeader);
    // Non-compressed block
    headerBlock.writeByte(0);
    // little-endian length (8204)
    headerBlock.writeByte(0x0c);
    // little-endian length (8204)
    headerBlock.writeByte(0x20);
    // one's compliment of length
    headerBlock.writeByte(0xf3);
    // one's compliment of length
    headerBlock.writeByte(0xdf);
    // number of Name/Value pairs
    headerBlock.writeInt(1);
    // length of name
    headerBlock.writeInt(8192);
    for (int i = 0; i < 8192; i++) {
        headerBlock.writeByte('n');
    }
    // length of value
    headerBlock.writeInt(0);
    headerBlock.writeBytes(zlibSyncFlush);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertFalse(frame.isInvalid());
    assertFalse(frame.isTruncated());
    assertEquals(1, frame.headers().names().size());
}

7. SpdyHeaderBlockRawDecoderTest#testMultipleNames()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testMultipleNames() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(38));
    headerBlock.writeInt(2);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(1, frame.headers().getAll(name).size());
    assertEquals(value, frame.headers().get(name));
}

8. SpdyHeaderBlockRawDecoderTest#testIllegalValueMultipleNulls()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testIllegalValueMultipleNulls() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(28));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(12);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeByte(0);
    headerBlock.writeByte(0);
    headerBlock.writeBytes(valueBytes);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

9. SpdyHeaderBlockRawDecoderTest#testMultipleValuesEndsWithNull()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testMultipleValuesEndsWithNull() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(28));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(12);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeByte(0);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeByte(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(1, frame.headers().getAll(name).size());
    assertEquals(value, frame.headers().get(name));
}

10. SpdyHeaderBlockZlibDecoderTest#testHeaderBlockInvalidDictionary()

Project: netty
File: SpdyHeaderBlockZlibDecoderTest.java
@Test(expected = SpdyProtocolException.class)
public void testHeaderBlockInvalidDictionary() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(7));
    headerBlock.writeByte(0x78);
    headerBlock.writeByte(0x3f);
    // Unknown dictionary
    headerBlock.writeByte(0x01);
    // Unknown dictionary
    headerBlock.writeByte(0x02);
    // Unknown dictionary
    headerBlock.writeByte(0x03);
    // Unknown dictionary
    headerBlock.writeByte(0x04);
    // Non-compressed block
    headerBlock.writeByte(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
}

11. SpdyHeaderBlockRawDecoderTest#testMultipleValues()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testMultipleValues() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(27));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(11);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeByte(0);
    headerBlock.writeBytes(valueBytes);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertFalse(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(2, frame.headers().getAll(name).size());
    assertEquals(value, frame.headers().getAll(name).get(0));
    assertEquals(value, frame.headers().getAll(name).get(1));
}

12. HAProxyMessageDecoderTest#testDetectProtocol()

Project: netty
File: HAProxyMessageDecoderTest.java
@Test
public void testDetectProtocol() {
    final ByteBuf validHeaderV1 = copiedBuffer("PROXY TCP4 192.168.0.1 192.168.0.11 56324 443\r\n", CharsetUtil.US_ASCII);
    ProtocolDetectionResult<HAProxyProtocolVersion> result = HAProxyMessageDecoder.detectProtocol(validHeaderV1);
    assertEquals(ProtocolDetectionState.DETECTED, result.state());
    assertEquals(HAProxyProtocolVersion.V1, result.detectedProtocol());
    validHeaderV1.release();
    final ByteBuf invalidHeader = copiedBuffer("Invalid header", CharsetUtil.US_ASCII);
    result = HAProxyMessageDecoder.detectProtocol(invalidHeader);
    assertEquals(ProtocolDetectionState.INVALID, result.state());
    assertNull(result.detectedProtocol());
    invalidHeader.release();
    final ByteBuf validHeaderV2 = buffer();
    validHeaderV2.writeByte(0x0D);
    validHeaderV2.writeByte(0x0A);
    validHeaderV2.writeByte(0x0D);
    validHeaderV2.writeByte(0x0A);
    validHeaderV2.writeByte(0x00);
    validHeaderV2.writeByte(0x0D);
    validHeaderV2.writeByte(0x0A);
    validHeaderV2.writeByte(0x51);
    validHeaderV2.writeByte(0x55);
    validHeaderV2.writeByte(0x49);
    validHeaderV2.writeByte(0x54);
    validHeaderV2.writeByte(0x0A);
    result = HAProxyMessageDecoder.detectProtocol(validHeaderV2);
    assertEquals(ProtocolDetectionState.DETECTED, result.state());
    assertEquals(HAProxyProtocolVersion.V2, result.detectedProtocol());
    validHeaderV2.release();
    final ByteBuf incompleteHeader = buffer();
    incompleteHeader.writeByte(0x0D);
    incompleteHeader.writeByte(0x0A);
    incompleteHeader.writeByte(0x0D);
    incompleteHeader.writeByte(0x0A);
    incompleteHeader.writeByte(0x00);
    incompleteHeader.writeByte(0x0D);
    incompleteHeader.writeByte(0x0A);
    result = HAProxyMessageDecoder.detectProtocol(incompleteHeader);
    assertEquals(ProtocolDetectionState.NEEDS_MORE_DATA, result.state());
    assertNull(result.detectedProtocol());
    incompleteHeader.release();
}

13. SpdyHeaderBlockRawDecoderTest#testExtraData()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testExtraData() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(22));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeByte(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(1, frame.headers().getAll(name).size());
    assertEquals(value, frame.headers().get(name));
}

14. SpdyHeaderBlockRawDecoderTest#testIllegalValueEndsWithNull()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testIllegalValueEndsWithNull() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(22));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(6);
    headerBlock.writeBytes(valueBytes);
    headerBlock.writeByte(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

15. SpdyHeaderBlockRawDecoderTest#testIllegalValueStartsWithNull()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testIllegalValueStartsWithNull() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(22));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(6);
    headerBlock.writeByte(0);
    headerBlock.writeBytes(valueBytes);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

16. SpdyHeaderBlockRawDecoderTest#testMultipleDecodes()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testMultipleDecodes() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(21));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    int readableBytes = headerBlock.readableBytes();
    for (int i = 0; i < readableBytes; i++) {
        ByteBuf headerBlockSegment = headerBlock.slice(i, 1);
        decoder.decode(ByteBufAllocator.DEFAULT, headerBlockSegment, frame);
        assertFalse(headerBlockSegment.isReadable());
    }
    decoder.endHeaderBlock(frame);
    assertFalse(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(1, frame.headers().getAll(name).size());
    assertEquals(value, frame.headers().get(name));
}

17. SpdyHeaderBlockRawDecoderTest#testMissingNextNameValuePair()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testMissingNextNameValuePair() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(21));
    headerBlock.writeInt(2);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(1, frame.headers().getAll(name).size());
    assertEquals(value, frame.headers().get(name));
}

18. SpdyHeaderBlockRawDecoderTest#testIllegalValueOnlyNull()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testIllegalValueOnlyNull() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(17));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(1);
    headerBlock.writeByte(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

19. SpdyHeaderBlockRawDecoderTest#testIllegalNameOnlyNull()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testIllegalNameOnlyNull() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(18));
    headerBlock.writeInt(1);
    headerBlock.writeInt(1);
    headerBlock.writeByte(0);
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

20. SpdyHeaderBlockRawDecoderTest#testOneNameValuePair()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testOneNameValuePair() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(21));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertFalse(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(1, frame.headers().getAll(name).size());
    assertEquals(value, frame.headers().get(name));
}

21. NtlmTest#testGenerateType3Msg()

Project: async-http-client
File: NtlmTest.java
@Test
public void testGenerateType3Msg() {
    ByteBuf buf = Unpooled.directBuffer();
    buf.writeBytes("NTLMSSP".getBytes(StandardCharsets.US_ASCII));
    buf.writeByte(0);
    // type 2 indicator
    buf.writeByte(2).writeByte(0).writeByte(0).writeByte(0);
    buf.writeLong(0);
    // flags
    // unicode support indicator
    buf.writeByte(1);
    buf.writeByte(0).writeByte(0).writeByte(0);
    // challenge
    buf.writeLong(1);
    NtlmEngine engine = new NtlmEngine();
    String type3Msg = engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.encode(ByteBufUtils.byteBuf2Bytes(buf)));
    assertEquals(type3Msg, "TlRMTVNTUAADAAAAGAAYAEgAAAAYABgAYAAAABIAEgB4AAAAEAAQAIoAAAAWABYAmgAAAAAAAACwAAAAAQAAAgUBKAoAAAAP1g6lqqN1HZ0wSSxeQ5riQkyh7/UexwVlCPQm0SHU2vsDQm2wM6NbT2zPonPzLJL0TABPAEMAQQBMAEgATwBTAFQAdQBzAGUAcgBuAGEAbQBlAFcATwBSAEsAUwBUAEEAVABJAE8ATgA=", "Incorrect type3 message generated");
}

22. NtlmTest#testGenerateType3MsgThrowsExceptionWhenUnicodeSupportNotIndicated()

Project: async-http-client
File: NtlmTest.java
@Test(expectedExceptions = NtlmEngineException.class)
public void testGenerateType3MsgThrowsExceptionWhenUnicodeSupportNotIndicated() {
    ByteBuf buf = Unpooled.directBuffer();
    buf.writeBytes("NTLMSSP".getBytes(StandardCharsets.US_ASCII));
    buf.writeByte(0);
    // type 2 indicator
    buf.writeByte(2).writeByte(0).writeByte(0).writeByte(0);
    buf.writeLong(1);
    // flags
    // unicode support indicator
    buf.writeByte(0);
    buf.writeByte(0).writeByte(0).writeByte(0);
    // challenge
    buf.writeLong(1);
    NtlmEngine engine = new NtlmEngine();
    engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.encode(ByteBufUtils.byteBuf2Bytes(buf)));
    fail("An NtlmEngineException must have occurred as unicode support is not indicated");
}

23. Bzip2DecoderTest#testStreamCrcErrorOfEmptyBlock()

Project: netty
File: Bzip2DecoderTest.java
@Test
public void testStreamCrcErrorOfEmptyBlock() throws Exception {
    expected.expect(DecompressionException.class);
    expected.expectMessage("stream CRC error");
    ByteBuf in = Unpooled.buffer();
    in.writeMedium(MAGIC_NUMBER);
    //block size
    in.writeByte('1');
    in.writeMedium(END_OF_STREAM_MAGIC_1);
    in.writeMedium(END_OF_STREAM_MAGIC_2);
    //wrong storedCombinedCRC
    in.writeInt(1);
    channel.writeInbound(in);
}

24. Bzip2DecoderTest#testBadBlockHeader()

Project: netty
File: Bzip2DecoderTest.java
@Test
public void testBadBlockHeader() throws Exception {
    expected.expect(DecompressionException.class);
    expected.expectMessage("bad block header");
    ByteBuf in = Unpooled.buffer();
    in.writeMedium(MAGIC_NUMBER);
    //block size
    in.writeByte('1');
    //incorrect block header
    in.writeMedium(11);
    //incorrect block header
    in.writeMedium(11);
    //block CRC
    in.writeInt(11111);
    channel.writeInbound(in);
}

25. SpdyHeaderBlockRawDecoderTest#testTruncatedHeaderValue()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testTruncatedHeaderValue() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(maxHeaderSize + 13));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(13);
    for (int i = 0; i < maxHeaderSize - 3; i++) {
        headerBlock.writeByte('a');
    }
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isTruncated());
    assertFalse(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

26. SpdyHeaderBlockRawDecoderTest#testTruncatedHeaderName()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testTruncatedHeaderName() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(maxHeaderSize + 18));
    headerBlock.writeInt(1);
    headerBlock.writeInt(maxHeaderSize + 1);
    for (int i = 0; i < maxHeaderSize + 1; i++) {
        headerBlock.writeByte('a');
    }
    headerBlock.writeInt(5);
    headerBlock.writeBytes(valueBytes);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isTruncated());
    assertFalse(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

27. SpdyHeaderBlockRawDecoderTest#testMissingValue()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testMissingValue() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(5);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

28. SpdyHeaderBlockRawDecoderTest#testNegativeValueLength()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testNegativeValueLength() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(-1);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

29. SpdyHeaderBlockRawDecoderTest#testZeroValueLength()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testZeroValueLength() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(16));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    headerBlock.writeInt(0);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertFalse(frame.isInvalid());
    assertEquals(1, frame.headers().names().size());
    assertTrue(frame.headers().contains(name));
    assertEquals(1, frame.headers().getAll(name).size());
    assertEquals("", frame.headers().get(name));
}

30. SpdyFrameDecoderTest#testProgressivelyDiscardUnknownEmptyFrame()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testProgressivelyDiscardUnknownEmptyFrame() throws Exception {
    short type = 5;
    byte flags = (byte) 0xFF;
    int segment = 4;
    int length = 2 * segment;
    ByteBuf header = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE));
    ByteBuf segment1 = Unpooled.buffer(segment);
    ByteBuf segment2 = Unpooled.buffer(segment);
    encodeControlFrameHeader(header, type, flags, length);
    segment1.writeInt(RANDOM.nextInt());
    segment2.writeInt(RANDOM.nextInt());
    replay(delegate);
    decoder.decode(header);
    decoder.decode(segment1);
    decoder.decode(segment2);
    verify(delegate);
    assertFalse(header.isReadable());
    assertFalse(segment1.isReadable());
    assertFalse(segment2.isReadable());
}

31. SpdyFrameDecoderTest#testIllegalSpdySynStreamFrameStreamId()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testIllegalSpdySynStreamFrameStreamId() throws Exception {
    short type = 1;
    byte flags = 0;
    int length = 10;
    // invalid stream identifier
    int streamId = 0;
    int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    byte priority = (byte) (RANDOM.nextInt() & 0x07);
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(associatedToStreamId);
    buf.writeByte(priority << 5);
    buf.writeByte(0);
    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

32. SpdyFrameDecoderTest#testReservedSpdySynStreamFrameBits()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testReservedSpdySynStreamFrameBits() throws Exception {
    short type = 1;
    byte flags = 0;
    int length = 10;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    byte priority = (byte) (RANDOM.nextInt() & 0x07);
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    // should ignore reserved bit
    buf.writeInt(streamId | 0x80000000);
    // should ignore reserved bit
    buf.writeInt(associatedToStreamId | 0x80000000);
    // should ignore reserved bits
    buf.writeByte(priority << 5 | 0x1F);
    // should ignore reserved bits
    buf.writeByte(0xFF);
    delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, false);
    delegate.readHeaderBlockEnd();
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

33. SpdyFrameDecoderTest#testUnknownSpdySynStreamFrameFlags()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testUnknownSpdySynStreamFrameFlags() throws Exception {
    short type = 1;
    // undefined flags
    byte flags = (byte) 0xFC;
    int length = 10;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    byte priority = (byte) (RANDOM.nextInt() & 0x07);
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(associatedToStreamId);
    buf.writeByte(priority << 5);
    buf.writeByte(0);
    delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, false);
    delegate.readHeaderBlockEnd();
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

34. SpdyFrameDecoderTest#testIndependentSpdySynStreamFrame()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testIndependentSpdySynStreamFrame() throws Exception {
    short type = 1;
    byte flags = 0;
    int length = 10;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    // independent of all other streams
    int associatedToStreamId = 0;
    byte priority = (byte) (RANDOM.nextInt() & 0x07);
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(associatedToStreamId);
    buf.writeByte(priority << 5);
    buf.writeByte(0);
    delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, false);
    delegate.readHeaderBlockEnd();
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

35. SpdyFrameDecoderTest#testUnidirectionalSpdySynStreamFrame()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testUnidirectionalSpdySynStreamFrame() throws Exception {
    short type = 1;
    // FLAG_UNIDIRECTIONAL
    byte flags = 0x02;
    int length = 10;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    byte priority = (byte) (RANDOM.nextInt() & 0x07);
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(associatedToStreamId);
    buf.writeByte(priority << 5);
    buf.writeByte(0);
    delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, true);
    delegate.readHeaderBlockEnd();
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

36. SpdyFrameDecoderTest#testLastSpdySynStreamFrame()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testLastSpdySynStreamFrame() throws Exception {
    short type = 1;
    // FLAG_FIN
    byte flags = 0x01;
    int length = 10;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    byte priority = (byte) (RANDOM.nextInt() & 0x07);
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(associatedToStreamId);
    buf.writeByte(priority << 5);
    buf.writeByte(0);
    delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, true, false);
    delegate.readHeaderBlockEnd();
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

37. SpdyFrameDecoderTest#testSpdySynStreamFrame()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testSpdySynStreamFrame() throws Exception {
    short type = 1;
    byte flags = 0;
    int length = 10;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    byte priority = (byte) (RANDOM.nextInt() & 0x07);
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(associatedToStreamId);
    buf.writeByte(priority << 5);
    buf.writeByte(0);
    delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, false, false);
    delegate.readHeaderBlockEnd();
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

38. SpdyFrameEncoder#encodeSynStreamFrame()

Project: netty
File: SpdyFrameEncoder.java
public ByteBuf encodeSynStreamFrame(ByteBufAllocator allocator, int streamId, int associatedToStreamId, byte priority, boolean last, boolean unidirectional, ByteBuf headerBlock) {
    int headerBlockLength = headerBlock.readableBytes();
    byte flags = last ? SPDY_FLAG_FIN : 0;
    if (unidirectional) {
        flags |= SPDY_FLAG_UNIDIRECTIONAL;
    }
    int length = 10 + headerBlockLength;
    ByteBuf frame = allocator.ioBuffer(SPDY_HEADER_SIZE + length).order(ByteOrder.BIG_ENDIAN);
    writeControlFrameHeader(frame, SPDY_SYN_STREAM_FRAME, flags, length);
    frame.writeInt(streamId);
    frame.writeInt(associatedToStreamId);
    frame.writeShort((priority & 0xFF) << 13);
    frame.writeBytes(headerBlock, headerBlock.readerIndex(), headerBlockLength);
    return frame;
}

39. HttpFrameDecoderTest#testHttpSettingsFrameWithMultiples()

Project: netty-http2
File: HttpFrameDecoderTest.java
@Test
public void testHttpSettingsFrameWithMultiples() throws Exception {
    int length = 12;
    byte flags = 0;
    // connection identifier
    int streamId = 0;
    int id = RANDOM.nextInt() & 0xFFFF;
    int value1 = RANDOM.nextInt();
    int value2 = RANDOM.nextInt();
    ByteBuf frame = settingsFrame(length, flags, streamId);
    frame.writeShort(id);
    frame.writeInt(value1);
    frame.writeShort(id);
    frame.writeInt(value2);
    decoder.decode(frame);
    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readSettingsFrame(false);
    inOrder.verify(delegate).readSetting(id, value1);
    inOrder.verify(delegate).readSetting(id, value2);
    inOrder.verify(delegate).readSettingsEnd();
    verifyNoMoreInteractions(delegate);
}

40. StompSubframeEncoderTest#testFrameAndContentEncoding()

Project: netty
File: StompSubframeEncoderTest.java
@Test
public void testFrameAndContentEncoding() {
    StompHeadersSubframe frame = new DefaultStompHeadersSubframe(StompCommand.CONNECT);
    StompHeaders headers = frame.headers();
    headers.set(StompHeaders.HOST, "stomp.github.org");
    headers.set(StompHeaders.ACCEPT_VERSION, "1.1,1.2");
    channel.writeOutbound(frame);
    channel.writeOutbound(LastStompContentSubframe.EMPTY_LAST_CONTENT);
    ByteBuf aggregatedBuffer = Unpooled.buffer();
    ByteBuf byteBuf = channel.readOutbound();
    assertNotNull(byteBuf);
    aggregatedBuffer.writeBytes(byteBuf);
    byteBuf = channel.readOutbound();
    assertNotNull(byteBuf);
    aggregatedBuffer.writeBytes(byteBuf);
    aggregatedBuffer.resetReaderIndex();
    String content = aggregatedBuffer.toString(CharsetUtil.UTF_8);
    assertEquals(StompTestConstants.CONNECT_FRAME, content);
}

41. LzfDecoderTest#testUnknownTypeOfChunk()

Project: netty
File: LzfDecoderTest.java
@Test
public void testUnknownTypeOfChunk() throws Exception {
    expected.expect(DecompressionException.class);
    expected.expectMessage("unknown type of chunk");
    ByteBuf in = Unpooled.buffer();
    in.writeByte(BYTE_Z);
    in.writeByte(BYTE_V);
    //random value
    in.writeByte(0xFF);
    in.writeInt(0);
    channel.writeInbound(in);
}

42. ByteToMessageDecoderTest#testInternalBufferClearReadPartly()

Project: netty
File: ByteToMessageDecoderTest.java
/**
     * Verifies that internal buffer of the ByteToMessageDecoder is released once decoder is removed from pipeline. In
     * this case input was not fully read.
     */
@Test
public void testInternalBufferClearReadPartly() {
    final ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer().writeBytes(new byte[] { 'a', 'b' }));
    EmbeddedChannel channel = new EmbeddedChannel(new ByteToMessageDecoder() {

        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            ByteBuf byteBuf = internalBuffer();
            assertEquals(1, byteBuf.refCnt());
            in.readByte();
            // Removal from pipeline should clear internal buffer
            ctx.pipeline().remove(this);
            assertEquals(0, byteBuf.refCnt());
        }
    });
    assertTrue(channel.writeInbound(buf));
    assertTrue(channel.finish());
    ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 'b' });
    ByteBuf b = channel.readInbound();
    assertEquals(expected, b);
    assertNull(channel.readInbound());
    expected.release();
    b.release();
}

43. PublishDecoderTest#preparePubclishWithQosFlags()

Project: moquette
File: PublishDecoderTest.java
private ByteBuf preparePubclishWithQosFlags(byte flags) {
    ByteBuf buff = Unpooled.buffer(14);
    ByteBuffer payload = ByteBuffer.allocate(3).put(new byte[] { 0x0A, 0x0B, 0x0C });
    ByteBuf tmp = Unpooled.buffer(4).writeBytes(Utils.encodeString("Fake Topic"));
    tmp.writeShort(MESSAGE_ID);
    tmp.writeBytes(payload);
    //set DUP=1 Qos to 11 => b1110
    buff.clear().writeByte(AbstractMessage.PUBLISH << 4 | flags).writeBytes(Utils.encodeRemainingLength(tmp.readableBytes()));
    //topic name
    buff.writeBytes(tmp);
    return buff;
}

44. PublishDecoderTest#generatePublishQoS0()

Project: moquette
File: PublishDecoderTest.java
private ByteBuf generatePublishQoS0(ByteBuf payload) throws IllegalAccessException {
    int size = payload.capacity();
    ByteBuf messageBody = Unpooled.buffer(size);
    messageBody.writeBytes(Utils.encodeString("/topic"));
    //ONLY for QoS > 1 Utils.writeWord(messageBody, messageID);
    messageBody.writeBytes(payload);
    ByteBuf completeMsg = Unpooled.buffer(size);
    //set Qos to 0
    completeMsg.clear().writeByte(AbstractMessage.PUBLISH << 4 | 0x00).writeBytes(Utils.encodeRemainingLength(messageBody.readableBytes()));
    completeMsg.writeBytes(messageBody);
    return completeMsg;
}

45. StompSubframeEncoder#encodeFrame()

Project: netty
File: StompSubframeEncoder.java
private static ByteBuf encodeFrame(StompHeadersSubframe frame, ChannelHandlerContext ctx) {
    ByteBuf buf = ctx.alloc().buffer();
    buf.writeBytes(frame.command().toString().getBytes(CharsetUtil.US_ASCII));
    buf.writeByte(StompConstants.LF);
    AsciiHeadersEncoder headersEncoder = new AsciiHeadersEncoder(buf, SeparatorType.COLON, NewlineType.LF);
    for (Entry<CharSequence, CharSequence> entry : frame.headers()) {
        headersEncoder.encode(entry);
    }
    buf.writeByte(StompConstants.LF);
    return buf;
}

46. RedisDecoderTest#shouldErrorOnReleasecontentOfArrayChildReferenceCounted()

Project: netty
File: RedisDecoderTest.java
@Test(expected = IllegalReferenceCountException.class)
public void shouldErrorOnReleasecontentOfArrayChildReferenceCounted() throws Exception {
    ByteBuf buf = Unpooled.buffer();
    buf.writeBytes(byteBufOf("*2\r\n"));
    buf.writeBytes(byteBufOf("$3\r\nFoo\r\n$3\r\nBar\r\n"));
    assertTrue(channel.writeInbound(buf));
    ArrayRedisMessage msg = channel.readInbound();
    List<RedisMessage> children = msg.children();
    ByteBuf childBuf = ((FullBulkStringRedisMessage) children.get(0)).content();
    ReferenceCountUtil.release(msg);
    ReferenceCountUtil.release(childBuf);
}

47. RedisDecoderTest#shouldErrorOnReleaseArrayChildReferenceCounted()

Project: netty
File: RedisDecoderTest.java
@Test(expected = IllegalReferenceCountException.class)
public void shouldErrorOnReleaseArrayChildReferenceCounted() throws Exception {
    ByteBuf buf = Unpooled.buffer();
    buf.writeBytes(byteBufOf("*2\r\n"));
    buf.writeBytes(byteBufOf("*3\r\n:1\r\n:2\r\n:3\r\n"));
    buf.writeBytes(byteBufOf("$3\r\nFoo\r\n"));
    assertTrue(channel.writeInbound(buf));
    ArrayRedisMessage msg = channel.readInbound();
    List<RedisMessage> children = msg.children();
    ReferenceCountUtil.release(msg);
    ReferenceCountUtil.release(children.get(1));
}

48. RedisDecoderTest#shouldErrorOnDoubleReleaseArrayReferenceCounted()

Project: netty
File: RedisDecoderTest.java
@Test(expected = IllegalReferenceCountException.class)
public void shouldErrorOnDoubleReleaseArrayReferenceCounted() throws Exception {
    ByteBuf buf = Unpooled.buffer();
    buf.writeBytes(byteBufOf("*2\r\n"));
    buf.writeBytes(byteBufOf("*3\r\n:1\r\n:2\r\n:3\r\n"));
    buf.writeBytes(byteBufOf("*2\r\n+Foo\r\n-Bar\r\n"));
    assertTrue(channel.writeInbound(buf));
    ArrayRedisMessage msg = channel.readInbound();
    ReferenceCountUtil.release(msg);
    ReferenceCountUtil.release(msg);
}

49. BinaryMemcacheEncoderTest#shouldEncodeKey()

Project: netty
File: BinaryMemcacheEncoderTest.java
@Test
public void shouldEncodeKey() {
    ByteBuf key = Unpooled.copiedBuffer("netty", CharsetUtil.UTF_8);
    int keyLength = key.readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(key);
    boolean result = channel.writeOutbound(request);
    assertThat(result, is(true));
    ByteBuf written = channel.readOutbound();
    assertThat(written.readableBytes(), is(DEFAULT_HEADER_SIZE + keyLength));
    written.readBytes(DEFAULT_HEADER_SIZE);
    assertThat(written.readBytes(keyLength).toString(CharsetUtil.UTF_8), equalTo("netty"));
    written.release();
}

50. BinaryMemcacheEncoderTest#shouldEncodeExtras()

Project: netty
File: BinaryMemcacheEncoderTest.java
@Test
public void shouldEncodeExtras() {
    String extrasContent = "netty<3memcache";
    ByteBuf extras = Unpooled.copiedBuffer(extrasContent, CharsetUtil.UTF_8);
    int extrasLength = extras.readableBytes();
    BinaryMemcacheRequest request = new DefaultBinaryMemcacheRequest(Unpooled.EMPTY_BUFFER, extras);
    boolean result = channel.writeOutbound(request);
    assertThat(result, is(true));
    ByteBuf written = channel.readOutbound();
    assertThat(written.readableBytes(), is(DEFAULT_HEADER_SIZE + extrasLength));
    written.readBytes(DEFAULT_HEADER_SIZE);
    assertThat(written.readBytes(extrasLength).toString(CharsetUtil.UTF_8), equalTo(extrasContent));
    written.release();
}

51. SpdyHeaderBlockRawDecoderTest#testMissingValueLength()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testMissingValueLength() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(12));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    headerBlock.writeBytes(nameBytes);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

52. Hash#hashToBase64()

Project: redisson
File: Hash.java
public static String hashToBase64(byte[] objectState) {
    long h1 = LongHashFunction.farmUo().hashBytes(objectState);
    long h2 = LongHashFunction.xx_r39().hashBytes(objectState);
    ByteBuf buf = Unpooled.buffer((2 * Long.SIZE) / Byte.SIZE).writeLong(h1).writeLong(h2);
    ByteBuf b = Base64.encode(buf);
    String s = b.toString(CharsetUtil.UTF_8);
    b.release();
    buf.release();
    return s.substring(0, s.length() - 2);
}

53. ZMTPWriterTest#testOneFrame()

Project: netty-zmtp
File: ZMTPWriterTest.java
@Test
public void testOneFrame() throws Exception {
    final ZMTPWriter writer = ZMTPWriter.create(ZMTP10);
    final ByteBuf buf = Unpooled.buffer();
    writer.reset(buf);
    ByteBuf frame = writer.frame(11, false);
    assertThat(frame, is(sameInstance(buf)));
    final ByteBuf content = copiedBuffer("hello world", UTF_8);
    frame.writeBytes(content.duplicate());
    final ZMTPFramingDecoder decoder = new ZMTPFramingDecoder(wireFormat(ZMTP10), new RawDecoder());
    decoder.decode(null, buf, out);
    assertThat(out, hasSize(1));
    assertThat(out, contains((Object) singletonList(content)));
}

54. HttpFrameDecoderTest#testHttpPushPromiseFrameMultipleContinuations()

Project: netty-http2
File: HttpFrameDecoderTest.java
@Test
public void testHttpPushPromiseFrameMultipleContinuations() throws Exception {
    int headerBlockLength = 16;
    int length = 4 + headerBlockLength;
    byte flags = 0;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int promisedStreamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    ByteBuf pushPromiseFrame = pushPromiseFrame(length, flags, streamId);
    pushPromiseFrame.writeInt(promisedStreamId);
    writeRandomData(pushPromiseFrame, headerBlockLength);
    ByteBuf continuationFrame1 = continuationFrame(0, (byte) 0x00, streamId);
    // END_HEADERS
    ByteBuf continuationFrame2 = continuationFrame(0, (byte) 0x04, streamId);
    decoder.decode(releaseLater(Unpooled.wrappedBuffer(pushPromiseFrame, continuationFrame1, continuationFrame2)));
    InOrder inOrder = inOrder(delegate);
    inOrder.verify(delegate).readPushPromiseFrame(streamId, promisedStreamId);
    inOrder.verify(delegate).readHeaderBlock(pushPromiseFrame.slice(HTTP_FRAME_HEADER_SIZE + 4, headerBlockLength));
    inOrder.verify(delegate).readHeaderBlockEnd();
    verifyNoMoreInteractions(delegate);
}

55. ReplayingDecoderTest#testRemoveItselfWriteBuffer()

Project: netty
File: ReplayingDecoderTest.java
@Test
public void testRemoveItselfWriteBuffer() {
    final ByteBuf buf = Unpooled.buffer().writeBytes(new byte[] { 'a', 'b', 'c' });
    EmbeddedChannel channel = new EmbeddedChannel(new ReplayingDecoder() {

        private boolean removed;

        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            assertFalse(removed);
            in.readByte();
            ctx.pipeline().remove(this);
            // This should not let it keep call decode
            buf.writeByte('d');
            removed = true;
        }
    });
    channel.writeInbound(buf.copy());
    ByteBuf b = channel.readInbound();
    assertEquals(b, Unpooled.wrappedBuffer(new byte[] { 'b', 'c' }));
    b.release();
    buf.release();
}

56. ReplayingDecoderTest#testRemoveItselfWithReplayError()

Project: netty
File: ReplayingDecoderTest.java
@Test
public void testRemoveItselfWithReplayError() {
    EmbeddedChannel channel = new EmbeddedChannel(new ReplayingDecoder() {

        private boolean removed;

        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            assertFalse(removed);
            ctx.pipeline().remove(this);
            in.readBytes(1000);
            removed = true;
        }
    });
    ByteBuf buf = Unpooled.wrappedBuffer(new byte[] { 'a', 'b', 'c' });
    channel.writeInbound(buf.copy());
    ByteBuf b = channel.readInbound();
    assertEquals("Expect to have still all bytes in the buffer", b, buf);
    b.release();
    buf.release();
}

57. ReplayingDecoderTest#testRemoveItself()

Project: netty
File: ReplayingDecoderTest.java
@Test
public void testRemoveItself() {
    EmbeddedChannel channel = new EmbeddedChannel(new ReplayingDecoder() {

        private boolean removed;

        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            assertFalse(removed);
            in.readByte();
            ctx.pipeline().remove(this);
            removed = true;
        }
    });
    ByteBuf buf = Unpooled.wrappedBuffer(new byte[] { 'a', 'b', 'c' });
    channel.writeInbound(buf.copy());
    ByteBuf b = channel.readInbound();
    assertEquals(b, buf.skipBytes(1));
    b.release();
    buf.release();
}

58. JsonObjectDecoderTest#testOneJsonObjectPerWrite()

Project: netty
File: JsonObjectDecoderTest.java
@Test
public void testOneJsonObjectPerWrite() {
    EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder());
    String object1 = "{\"key\" : \"value1\"}", object2 = "{\"key\" : \"value2\"}", object3 = "{\"key\" : \"value3\"}";
    ch.writeInbound(Unpooled.copiedBuffer(object1, CharsetUtil.UTF_8));
    ch.writeInbound(Unpooled.copiedBuffer(object2, CharsetUtil.UTF_8));
    ch.writeInbound(Unpooled.copiedBuffer(object3, CharsetUtil.UTF_8));
    ByteBuf res = ch.readInbound();
    assertEquals(object1, res.toString(CharsetUtil.UTF_8));
    res.release();
    res = ch.readInbound();
    assertEquals(object2, res.toString(CharsetUtil.UTF_8));
    res.release();
    res = ch.readInbound();
    assertEquals(object3, res.toString(CharsetUtil.UTF_8));
    res.release();
    assertFalse(ch.finish());
}

59. JsonObjectDecoderTest#testMultipleJsonObjectsInOneWrite()

Project: netty
File: JsonObjectDecoderTest.java
@Test
public void testMultipleJsonObjectsInOneWrite() {
    EmbeddedChannel ch = new EmbeddedChannel(new JsonObjectDecoder());
    String object1 = "{\"key\" : \"value1\"}", object2 = "{\"key\" : \"value2\"}", object3 = "{\"key\" : \"value3\"}";
    ch.writeInbound(Unpooled.copiedBuffer(object1 + object2 + object3, CharsetUtil.UTF_8));
    ByteBuf res = ch.readInbound();
    assertEquals(object1, res.toString(CharsetUtil.UTF_8));
    res.release();
    res = ch.readInbound();
    assertEquals(object2, res.toString(CharsetUtil.UTF_8));
    res.release();
    res = ch.readInbound();
    assertEquals(object3, res.toString(CharsetUtil.UTF_8));
    res.release();
    assertFalse(ch.finish());
}

60. LengthFieldPrependerTest#testPrependLengthInLittleEndian()

Project: netty
File: LengthFieldPrependerTest.java
@Test
public void testPrependLengthInLittleEndian() throws Exception {
    final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(ByteOrder.LITTLE_ENDIAN, 4, 0, false));
    ch.writeOutbound(msg);
    ByteBuf buf = ch.readOutbound();
    assertEquals(4, buf.readableBytes());
    byte[] writtenBytes = new byte[buf.readableBytes()];
    buf.getBytes(0, writtenBytes);
    assertEquals(1, writtenBytes[0]);
    assertEquals(0, writtenBytes[1]);
    assertEquals(0, writtenBytes[2]);
    assertEquals(0, writtenBytes[3]);
    buf.release();
    buf = ch.readOutbound();
    assertSame(buf, msg);
    buf.release();
    assertFalse("The channel must have been completely read", ch.finish());
}

61. LzfDecoderTest#testUnexpectedBlockIdentifier()

Project: netty
File: LzfDecoderTest.java
@Test
public void testUnexpectedBlockIdentifier() throws Exception {
    expected.expect(DecompressionException.class);
    expected.expectMessage("unexpected block identifier");
    ByteBuf in = Unpooled.buffer();
    //random value
    in.writeShort(0x1234);
    in.writeByte(BLOCK_TYPE_NON_COMPRESSED);
    in.writeShort(0);
    channel.writeInbound(in);
}

62. ByteToMessageDecoderTest#testRemoveItself()

Project: netty
File: ByteToMessageDecoderTest.java
@Test
public void testRemoveItself() {
    EmbeddedChannel channel = new EmbeddedChannel(new ByteToMessageDecoder() {

        private boolean removed;

        @Override
        protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {
            assertFalse(removed);
            in.readByte();
            ctx.pipeline().remove(this);
            removed = true;
        }
    });
    ByteBuf buf = Unpooled.wrappedBuffer(new byte[] { 'a', 'b', 'c' });
    channel.writeInbound(buf.copy());
    ByteBuf b = channel.readInbound();
    assertEquals(b, buf.skipBytes(1));
    b.release();
    buf.release();
}

63. CommandArgs#toString()

Project: lettuce
File: CommandArgs.java
@Override
public String toString() {
    final StringBuilder sb = new StringBuilder();
    sb.append(getClass().getSimpleName());
    ByteBuf buffer = UnpooledByteBufAllocator.DEFAULT.buffer(singularArguments.size() * 10);
    encode(buffer);
    buffer.resetReaderIndex();
    byte[] bytes = new byte[buffer.readableBytes()];
    buffer.readBytes(bytes);
    sb.append(" [buffer=").append(new String(bytes));
    sb.append(']');
    buffer.release();
    return sb.toString();
}

64. WebSocketEncoder#encode()

Project: blynk-server
File: WebSocketEncoder.java
@Override
protected void encode(ChannelHandlerContext ctx, MessageBase msg, List<Object> out) throws Exception {
    stats.mark(msg.command);
    ByteBuf bb;
    if (msg.command == Command.RESPONSE) {
        bb = ctx.alloc().ioBuffer(5);
    } else {
        bb = ctx.alloc().ioBuffer(5 + msg.length);
    }
    bb.writeByte(msg.command);
    bb.writeShort(msg.id);
    bb.writeShort(msg.length);
    final byte[] data = msg.getBytes();
    if (data != null) {
        bb.writeBytes(data);
    }
    out.add(new BinaryWebSocketFrame(bb));
}

65. NtlmTest#testGenerateType3MsgThworsExceptionWhenType2IndicatorNotPresent()

Project: async-http-client
File: NtlmTest.java
@Test(expectedExceptions = NtlmEngineException.class)
public void testGenerateType3MsgThworsExceptionWhenType2IndicatorNotPresent() {
    ByteBuf buf = Unpooled.directBuffer();
    buf.writeBytes("NTLMSSP".getBytes(StandardCharsets.US_ASCII));
    buf.writeByte(0);
    // type 2 indicator
    buf.writeByte(3).writeByte(0).writeByte(0).writeByte(0);
    buf.writeBytes("challenge".getBytes());
    NtlmEngine engine = new NtlmEngine();
    engine.generateType3Msg("username", "password", "localhost", "workstation", Base64.encode(ByteBufUtils.byteBuf2Bytes(buf)));
    fail("An NtlmEngineException must have occurred as type 2 indicator is incorrect");
}

66. SpdyHeaderBlockRawDecoderTest#testMissingName()

Project: netty
File: SpdyHeaderBlockRawDecoderTest.java
@Test
public void testMissingName() throws Exception {
    ByteBuf headerBlock = ReferenceCountUtil.releaseLater(Unpooled.buffer(8));
    headerBlock.writeInt(1);
    headerBlock.writeInt(4);
    decoder.decode(ByteBufAllocator.DEFAULT, headerBlock, frame);
    decoder.endHeaderBlock(frame);
    assertFalse(headerBlock.isReadable());
    assertTrue(frame.isInvalid());
    assertEquals(0, frame.headers().names().size());
}

67. SpdyFrameDecoderTest#testIllegalSpdyWindowUpdateFrameDeltaWindowSize()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testIllegalSpdyWindowUpdateFrameDeltaWindowSize() throws Exception {
    short type = 9;
    byte flags = 0;
    int length = 8;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF;
    // invalid delta window size
    int deltaWindowSize = 0;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(deltaWindowSize);
    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

68. SpdyFrameDecoderTest#testInvalidSpdyWindowUpdateFrameLength()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testInvalidSpdyWindowUpdateFrameLength() throws Exception {
    short type = 9;
    byte flags = 0;
    // invalid length
    int length = 12;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF;
    int deltaWindowSize = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(deltaWindowSize);
    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

69. SpdyFrameDecoderTest#testReservedSpdyWindowUpdateFrameBits()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testReservedSpdyWindowUpdateFrameBits() throws Exception {
    short type = 9;
    byte flags = 0;
    int length = 8;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF;
    int deltaWindowSize = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    // should ignore reserved bit
    buf.writeInt(streamId | 0x80000000);
    // should ignore reserved bit
    buf.writeInt(deltaWindowSize | 0x80000000);
    delegate.readWindowUpdateFrame(streamId, deltaWindowSize);
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

70. SpdyFrameDecoderTest#testUnknownSpdyWindowUpdateFrameFlags()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testUnknownSpdyWindowUpdateFrameFlags() throws Exception {
    short type = 9;
    // undefined flags
    byte flags = (byte) 0xFF;
    int length = 8;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF;
    int deltaWindowSize = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(deltaWindowSize);
    delegate.readWindowUpdateFrame(streamId, deltaWindowSize);
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

71. SpdyFrameDecoderTest#testSpdyWindowUpdateFrame()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testSpdyWindowUpdateFrame() throws Exception {
    short type = 9;
    byte flags = 0;
    int length = 8;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF;
    int deltaWindowSize = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length);
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(deltaWindowSize);
    delegate.readWindowUpdateFrame(streamId, deltaWindowSize);
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

72. SpdyFrameDecoderTest#testInvalidSpdyGoAwayFrameLength()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testInvalidSpdyGoAwayFrameLength() throws Exception {
    short type = 7;
    byte flags = 0;
    // invalid length
    int length = 12;
    int lastGoodStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    int statusCode = RANDOM.nextInt() | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(lastGoodStreamId);
    buf.writeInt(statusCode);
    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

73. SpdyFrameDecoderTest#testReservedSpdyGoAwayFrameBits()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testReservedSpdyGoAwayFrameBits() throws Exception {
    short type = 7;
    byte flags = 0;
    int length = 8;
    int lastGoodStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    int statusCode = RANDOM.nextInt() | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    // should ignore reserved bit
    buf.writeInt(lastGoodStreamId | 0x80000000);
    buf.writeInt(statusCode);
    delegate.readGoAwayFrame(lastGoodStreamId, statusCode);
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

74. SpdyFrameDecoderTest#testUnknownSpdyGoAwayFrameFlags()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testUnknownSpdyGoAwayFrameFlags() throws Exception {
    short type = 7;
    // undefined flags
    byte flags = (byte) 0xFF;
    int length = 8;
    int lastGoodStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    int statusCode = RANDOM.nextInt() | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(lastGoodStreamId);
    buf.writeInt(statusCode);
    delegate.readGoAwayFrame(lastGoodStreamId, statusCode);
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

75. SpdyFrameDecoderTest#testSpdyGoAwayFrame()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testSpdyGoAwayFrame() throws Exception {
    short type = 7;
    byte flags = 0;
    int length = 8;
    int lastGoodStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    int statusCode = RANDOM.nextInt() | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(lastGoodStreamId);
    buf.writeInt(statusCode);
    delegate.readGoAwayFrame(lastGoodStreamId, statusCode);
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

76. SpdyFrameDecoderTest#testIllegalSpdyRstStreamFrameStatusCode()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testIllegalSpdyRstStreamFrameStatusCode() throws Exception {
    short type = 3;
    byte flags = 0;
    int length = 8;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    // invalid status code
    int statusCode = 0;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(statusCode);
    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

77. SpdyFrameDecoderTest#testIllegalSpdyRstStreamFrameStreamId()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testIllegalSpdyRstStreamFrameStreamId() throws Exception {
    short type = 3;
    byte flags = 0;
    int length = 8;
    // invalid stream identifier
    int streamId = 0;
    int statusCode = RANDOM.nextInt() | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(statusCode);
    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

78. SpdyFrameDecoderTest#testInvalidSpdyRstStreamFrameLength()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testInvalidSpdyRstStreamFrameLength() throws Exception {
    short type = 3;
    byte flags = 0;
    // invalid length
    int length = 12;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int statusCode = RANDOM.nextInt() | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(statusCode);
    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

79. SpdyFrameDecoderTest#testInvalidSpdyRstStreamFrameFlags()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testInvalidSpdyRstStreamFrameFlags() throws Exception {
    short type = 3;
    // invalid flags
    byte flags = (byte) 0xFF;
    int length = 8;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int statusCode = RANDOM.nextInt() | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(statusCode);
    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

80. SpdyFrameDecoderTest#testReservedSpdyRstStreamFrameBits()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testReservedSpdyRstStreamFrameBits() throws Exception {
    short type = 3;
    byte flags = 0;
    int length = 8;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int statusCode = RANDOM.nextInt() | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    // should ignore reserved bit
    buf.writeInt(streamId | 0x80000000);
    buf.writeInt(statusCode);
    delegate.readRstStreamFrame(streamId, statusCode);
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

81. SpdyFrameDecoderTest#testSpdyRstStreamFrame()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testSpdyRstStreamFrame() throws Exception {
    short type = 3;
    byte flags = 0;
    int length = 8;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int statusCode = RANDOM.nextInt() | 0x01;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(statusCode);
    delegate.readRstStreamFrame(streamId, statusCode);
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

82. SpdyFrameDecoderTest#testInvalidSpdySynStreamFrameLength()

Project: netty
File: SpdyFrameDecoderTest.java
@Test
public void testInvalidSpdySynStreamFrameLength() throws Exception {
    short type = 1;
    byte flags = 0;
    // invalid length
    int length = 8;
    int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01;
    int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF;
    ByteBuf buf = ReferenceCountUtil.releaseLater(Unpooled.buffer(SPDY_HEADER_SIZE + length));
    encodeControlFrameHeader(buf, type, flags, length);
    buf.writeInt(streamId);
    buf.writeInt(associatedToStreamId);
    delegate.readFrameError((String) anyObject());
    replay(delegate);
    decoder.decode(buf);
    verify(delegate);
    assertFalse(buf.isReadable());
}

83. HttpPostRequestEncoderTest#getRequestBody()

Project: netty
File: HttpPostRequestEncoderTest.java
private static String getRequestBody(HttpPostRequestEncoder encoder) throws Exception {
    encoder.finalizeRequest();
    List<InterfaceHttpData> chunks = encoder.multipartHttpDatas;
    ByteBuf[] buffers = new ByteBuf[chunks.size()];
    for (int i = 0; i < buffers.length; i++) {
        InterfaceHttpData data = chunks.get(i);
        if (data instanceof InternalAttribute) {
            buffers[i] = ((InternalAttribute) data).toByteBuf();
        } else if (data instanceof HttpData) {
            buffers[i] = ((HttpData) data).getByteBuf();
        }
    }
    ByteBuf content = Unpooled.wrappedBuffer(buffers);
    String contentStr = content.toString(CharsetUtil.UTF_8);
    content.release();
    return contentStr;
}

84. CloseWebSocketFrame#reasonText()

Project: netty
File: CloseWebSocketFrame.java
/**
     * Returns the reason text as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a> If a reason
     * text is not supplied, an empty string is returned.
     */
public String reasonText() {
    ByteBuf binaryData = content();
    if (binaryData == null || binaryData.capacity() <= 2) {
        return "";
    }
    binaryData.readerIndex(2);
    String reasonText = binaryData.toString(CharsetUtil.UTF_8);
    binaryData.readerIndex(0);
    return reasonText;
}

85. CloseWebSocketFrame#statusCode()

Project: netty
File: CloseWebSocketFrame.java
/**
     * Returns the closing status code as per <a href="http://tools.ietf.org/html/rfc6455#section-7.4">RFC 6455</a>. If
     * a getStatus code is set, -1 is returned.
     */
public int statusCode() {
    ByteBuf binaryData = content();
    if (binaryData == null || binaryData.capacity() == 0) {
        return -1;
    }
    binaryData.readerIndex(0);
    int statusCode = binaryData.readShort();
    binaryData.readerIndex(0);
    return statusCode;
}

86. CloseWebSocketFrame#newBinaryData()

Project: netty
File: CloseWebSocketFrame.java
private static ByteBuf newBinaryData(int statusCode, String reasonText) {
    byte[] reasonBytes = EmptyArrays.EMPTY_BYTES;
    if (reasonText != null) {
        reasonBytes = reasonText.getBytes(CharsetUtil.UTF_8);
    }
    ByteBuf binaryData = Unpooled.buffer(2 + reasonBytes.length);
    binaryData.writeShort(statusCode);
    if (reasonBytes.length > 0) {
        binaryData.writeBytes(reasonBytes);
    }
    binaryData.readerIndex(0);
    return binaryData;
}

87. StringEncoderTest#testEncode()

Project: netty
File: StringEncoderTest.java
@Test
public void testEncode() {
    String msg = "Test";
    EmbeddedChannel channel = new EmbeddedChannel(new StringEncoder());
    Assert.assertTrue(channel.writeOutbound(msg));
    Assert.assertTrue(channel.finish());
    ByteBuf buf = channel.readOutbound();
    byte[] data = new byte[buf.readableBytes()];
    buf.readBytes(data);
    Assert.assertArrayEquals(msg.getBytes(CharsetUtil.UTF_8), data);
    Assert.assertNull(channel.readOutbound());
    buf.release();
}

88. LengthFieldPrependerTest#testPrependAdjustedLength()

Project: netty
File: LengthFieldPrependerTest.java
@Test
public void testPrependAdjustedLength() throws Exception {
    final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(4, -1));
    ch.writeOutbound(msg);
    ByteBuf buf = ch.readOutbound();
    assertEquals(4, buf.readableBytes());
    assertEquals(msg.readableBytes() - 1, buf.readInt());
    buf.release();
    buf = ch.readOutbound();
    assertSame(buf, msg);
    buf.release();
}

89. LengthFieldPrependerTest#testPrependLengthIncludesLengthFieldLength()

Project: netty
File: LengthFieldPrependerTest.java
@Test
public void testPrependLengthIncludesLengthFieldLength() throws Exception {
    final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(4, true));
    ch.writeOutbound(msg);
    ByteBuf buf = ch.readOutbound();
    assertEquals(4, buf.readableBytes());
    assertEquals(5, buf.readInt());
    buf.release();
    buf = ch.readOutbound();
    assertSame(buf, msg);
    buf.release();
}

90. LengthFieldPrependerTest#testPrependLength()

Project: netty
File: LengthFieldPrependerTest.java
@Test
public void testPrependLength() throws Exception {
    final EmbeddedChannel ch = new EmbeddedChannel(new LengthFieldPrepender(4));
    ch.writeOutbound(msg);
    ByteBuf buf = ch.readOutbound();
    assertEquals(4, buf.readableBytes());
    assertEquals(msg.readableBytes(), buf.readInt());
    buf.release();
    buf = ch.readOutbound();
    assertSame(buf, msg);
    buf.release();
}

91. SnappyTest#encodeShortTextIsLiteral()

Project: netty
File: SnappyTest.java
@Test
public void encodeShortTextIsLiteral() throws Exception {
    ByteBuf in = Unpooled.wrappedBuffer(new byte[] { 0x6e, 0x65, 0x74, 0x74, 0x79 });
    ByteBuf out = Unpooled.buffer(7);
    snappy.encode(in, out, 5);
    ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { // preamble length
    0x05, // literal tag + length
    0x04 << 2, // "netty"
    0x6e, // "netty"
    0x65, // "netty"
    0x74, // "netty"
    0x74, // "netty"
    0x79 });
    assertEquals("Encoded literal was invalid", expected, out);
}

92. SnappyTest#testDecodeCopyWith1ByteOffset()

Project: netty
File: SnappyTest.java
@Test
public void testDecodeCopyWith1ByteOffset() throws Exception {
    ByteBuf in = Unpooled.wrappedBuffer(new byte[] { // preamble length
    0x0a, // literal tag + length
    0x04 << 2, // "netty"
    0x6e, // "netty"
    0x65, // "netty"
    0x74, // "netty"
    0x74, // "netty"
    0x79, // copy with 1-byte offset + length
    0x01 << 2 | 0x01, // offset
    0x05 });
    ByteBuf out = Unpooled.buffer(10);
    snappy.decode(in, out);
    // "nettynetty" - we saved a whole byte :)
    ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 0x6e, 0x65, 0x74, 0x74, 0x79, 0x6e, 0x65, 0x74, 0x74, 0x79 });
    assertEquals("Copy was not decoded correctly", expected, out);
}

93. SnappyTest#testDecodeLiteral()

Project: netty
File: SnappyTest.java
@Test
public void testDecodeLiteral() throws Exception {
    ByteBuf in = Unpooled.wrappedBuffer(new byte[] { // preamble length
    0x05, // literal tag + length
    0x04 << 2, // "netty"
    0x6e, // "netty"
    0x65, // "netty"
    0x74, // "netty"
    0x74, // "netty"
    0x79 });
    ByteBuf out = Unpooled.buffer(5);
    snappy.decode(in, out);
    // "netty"
    ByteBuf expected = Unpooled.wrappedBuffer(new byte[] { 0x6e, 0x65, 0x74, 0x74, 0x79 });
    assertEquals("Literal was not decoded correctly", expected, out);
}

94. AbstractIntegrationTest#testIdentity()

Project: netty
File: AbstractIntegrationTest.java
protected void testIdentity(final byte[] data) {
    final ByteBuf in = Unpooled.wrappedBuffer(data);
    assertTrue(encoder.writeOutbound(in.retain()));
    assertTrue(encoder.finish());
    final CompositeByteBuf compressed = Unpooled.compositeBuffer();
    ByteBuf msg;
    while ((msg = encoder.readOutbound()) != null) {
        compressed.addComponent(true, msg);
    }
    assertThat(compressed, is(notNullValue()));
    decoder.writeInbound(compressed.retain());
    assertFalse(compressed.isReadable());
    final CompositeByteBuf decompressed = Unpooled.compositeBuffer();
    while ((msg = decoder.readInbound()) != null) {
        decompressed.addComponent(true, msg);
    }
    assertEquals(in.resetReaderIndex(), decompressed);
    compressed.release();
    decompressed.release();
    in.release();
}

95. AbstractEncoderTest#testCompressionOfBatchedFlow()

Project: netty
File: AbstractEncoderTest.java
protected void testCompressionOfBatchedFlow(final ByteBuf data) throws Exception {
    final int dataLength = data.readableBytes();
    int written = 0, length = rand.nextInt(100);
    while (written + length < dataLength) {
        ByteBuf in = data.retainedSlice(written, length);
        assertTrue(channel.writeOutbound(in));
        written += length;
        length = rand.nextInt(100);
    }
    ByteBuf in = data.retainedSlice(written, dataLength - written);
    assertTrue(channel.writeOutbound(in));
    assertTrue(channel.finish());
    ByteBuf decompressed = readDecompressed(dataLength);
    assertEquals(data, decompressed);
    decompressed.release();
    data.release();
}

96. AbstractDecoderTest#testDecompressionOfBatchedFlow()

Project: netty
File: AbstractDecoderTest.java
protected void testDecompressionOfBatchedFlow(final ByteBuf expected, final ByteBuf data) throws Exception {
    final int compressedLength = data.readableBytes();
    int written = 0, length = rand.nextInt(100);
    while (written + length < compressedLength) {
        ByteBuf compressedBuf = data.retainedSlice(written, length);
        channel.writeInbound(compressedBuf);
        written += length;
        length = rand.nextInt(100);
    }
    ByteBuf compressedBuf = data.slice(written, compressedLength - written);
    assertTrue(channel.writeInbound(compressedBuf.retain()));
    ByteBuf decompressedBuf = readDecompressed(channel);
    assertEquals(expected, decompressedBuf);
    decompressedBuf.release();
    data.release();
}

97. PublishDecoderTest#testBadClaimMoreData()

Project: moquette
File: PublishDecoderTest.java
@Test
public void testBadClaimMoreData() throws Exception {
    byte[] rawMessage = new byte[] { 0x30, 0x17, 0x00, 0x06, 0x2f, (byte) 0x74, 0x6f, (byte) 0x70, 0x69, 0x63, 0x54, 0x65, (byte) 0x73, 0x74, 0x20, 0x6d, (byte) 0x79, 0x20, (byte) 0x70, 0x61, (byte) 0x79, 0x6c, 0x6f, 0x61, 0x64 };
    ByteBuf msgBuf = Unpooled.buffer(25);
    msgBuf.writeBytes(rawMessage);
    //to simulate the reading of messageType done by MQTTDecoder dispatcher
    msgBuf.readByte();
    //Exercise
    m_msgdec.decode(m_attrMap, msgBuf, m_results);
    assertFalse(m_results.isEmpty());
    PublishMessage message = (PublishMessage) m_results.get(0);
    assertNotNull(message);
    assertEquals("/topic", message.getTopicName());
    //        assertEquals("Test my payload", new String(message.getPayload()));
    Buffer expectedPayload = ByteBuffer.allocate(15).put("Test my payload".getBytes()).flip();
    assertEquals(expectedPayload, message.getPayload());
}

98. CommandArgsTest#addKeyUsingByteCodec()

Project: lettuce
File: CommandArgsTest.java
@Test
public void addKeyUsingByteCodec() throws Exception {
    CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).addValue("one".getBytes());
    ByteBuf buffer = Unpooled.buffer();
    args.encode(buffer);
    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());
    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

99. CommandArgsTest#addValueUsingByteCodec()

Project: lettuce
File: CommandArgsTest.java
@Test
public void addValueUsingByteCodec() throws Exception {
    CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).addValue("one".getBytes());
    ByteBuf buffer = Unpooled.buffer();
    args.encode(buffer);
    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());
    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}

100. CommandArgsTest#addByteUsingByteCodec()

Project: lettuce
File: CommandArgsTest.java
@Test
public void addByteUsingByteCodec() throws Exception {
    CommandArgs<byte[], byte[]> args = new CommandArgs<>(ByteArrayCodec.INSTANCE).add("one".getBytes());
    ByteBuf buffer = Unpooled.buffer();
    args.encode(buffer);
    ByteBuf expected = Unpooled.buffer();
    expected.writeBytes(("$3\r\n" + "one\r\n").getBytes());
    assertThat(buffer.toString(LettuceCharsets.ASCII)).isEqualTo(expected.toString(LettuceCharsets.ASCII));
}