SpdyFrameDecoder { public void decode(ByteBuf buffer) { boolean last; int statusCode; while (true) { switch(state) { case READ_COMMON_HEADER: if (buffer.readableBytes() < SPDY_HEADER_SIZE) { return; } int frameOffset = buffer.readerIndex(); int flagsOffset = frameOffset + SPDY_HEADER_FLAGS_OFFSET; int lengthOffset = frameOffset + SPDY_HEADER_LENGTH_OFFSET; buffer.skipBytes(SPDY_HEADER_SIZE); boolean control = (buffer.getByte(frameOffset) & 0x80) != 0; int version; int type; if (control) { version = getUnsignedShort(buffer, frameOffset) & 0x7FFF; type = getUnsignedShort(buffer, frameOffset + SPDY_HEADER_TYPE_OFFSET); streamId = 0; } else { version = spdyVersion; type = SPDY_DATA_FRAME; streamId = getUnsignedInt(buffer, frameOffset); } flags = buffer.getByte(flagsOffset); length = getUnsignedMedium(buffer, lengthOffset); if (version != spdyVersion) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid SPDY Version"); } else if (!isValidFrameHeader(streamId, type, flags, length)) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid Frame Error"); } else { state = getNextState(type, length); } break; case READ_DATA_FRAME: if (length == 0) { state = State.READ_COMMON_HEADER; delegate.readDataFrame(streamId, hasFlag(flags, SPDY_DATA_FLAG_FIN), Unpooled.buffer(0)); break; } int dataLength = Math.min(maxChunkSize, length); if (buffer.readableBytes() < dataLength) { return; } ByteBuf data = buffer.alloc().buffer(dataLength); data.writeBytes(buffer, dataLength); length -= dataLength; if (length == 0) { state = State.READ_COMMON_HEADER; } last = length == 0 && hasFlag(flags, SPDY_DATA_FLAG_FIN); delegate.readDataFrame(streamId, last, data); break; case READ_SYN_STREAM_FRAME: if (buffer.readableBytes() < 10) { return; } int offset = buffer.readerIndex(); streamId = getUnsignedInt(buffer, offset); int associatedToStreamId = getUnsignedInt(buffer, offset + 4); byte priority = (byte) (buffer.getByte(offset + 8) >> 5 & 0x07); last = hasFlag(flags, SPDY_FLAG_FIN); boolean unidirectional = hasFlag(flags, SPDY_FLAG_UNIDIRECTIONAL); buffer.skipBytes(10); length -= 10; if (streamId == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid SYN_STREAM Frame"); } else { state = State.READ_HEADER_BLOCK; delegate.readSynStreamFrame(streamId, associatedToStreamId, priority, last, unidirectional); } break; case READ_SYN_REPLY_FRAME: if (buffer.readableBytes() < 4) { return; } streamId = getUnsignedInt(buffer, buffer.readerIndex()); last = hasFlag(flags, SPDY_FLAG_FIN); buffer.skipBytes(4); length -= 4; if (streamId == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid SYN_REPLY Frame"); } else { state = State.READ_HEADER_BLOCK; delegate.readSynReplyFrame(streamId, last); } break; case READ_RST_STREAM_FRAME: if (buffer.readableBytes() < 8) { return; } streamId = getUnsignedInt(buffer, buffer.readerIndex()); statusCode = getSignedInt(buffer, buffer.readerIndex() + 4); buffer.skipBytes(8); if (streamId == 0 || statusCode == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid RST_STREAM Frame"); } else { state = State.READ_COMMON_HEADER; delegate.readRstStreamFrame(streamId, statusCode); } break; case READ_SETTINGS_FRAME: if (buffer.readableBytes() < 4) { return; } boolean clear = hasFlag(flags, SPDY_SETTINGS_CLEAR); numSettings = getUnsignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); length -= 4; if ((length & 0x07) != 0 || length >> 3 != numSettings) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid SETTINGS Frame"); } else { state = State.READ_SETTING; delegate.readSettingsFrame(clear); } break; case READ_SETTING: if (numSettings == 0) { state = State.READ_COMMON_HEADER; delegate.readSettingsEnd(); break; } if (buffer.readableBytes() < 8) { return; } byte settingsFlags = buffer.getByte(buffer.readerIndex()); int id = getUnsignedMedium(buffer, buffer.readerIndex() + 1); int value = getSignedInt(buffer, buffer.readerIndex() + 4); boolean persistValue = hasFlag(settingsFlags, SPDY_SETTINGS_PERSIST_VALUE); boolean persisted = hasFlag(settingsFlags, SPDY_SETTINGS_PERSISTED); buffer.skipBytes(8); --numSettings; delegate.readSetting(id, value, persistValue, persisted); break; case READ_PING_FRAME: if (buffer.readableBytes() < 4) { return; } int pingId = getSignedInt(buffer, buffer.readerIndex()); buffer.skipBytes(4); state = State.READ_COMMON_HEADER; delegate.readPingFrame(pingId); break; case READ_GOAWAY_FRAME: if (buffer.readableBytes() < 8) { return; } int lastGoodStreamId = getUnsignedInt(buffer, buffer.readerIndex()); statusCode = getSignedInt(buffer, buffer.readerIndex() + 4); buffer.skipBytes(8); state = State.READ_COMMON_HEADER; delegate.readGoAwayFrame(lastGoodStreamId, statusCode); break; case READ_HEADERS_FRAME: if (buffer.readableBytes() < 4) { return; } streamId = getUnsignedInt(buffer, buffer.readerIndex()); last = hasFlag(flags, SPDY_FLAG_FIN); buffer.skipBytes(4); length -= 4; if (streamId == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid HEADERS Frame"); } else { state = State.READ_HEADER_BLOCK; delegate.readHeadersFrame(streamId, last); } break; case READ_WINDOW_UPDATE_FRAME: if (buffer.readableBytes() < 8) { return; } streamId = getUnsignedInt(buffer, buffer.readerIndex()); int deltaWindowSize = getUnsignedInt(buffer, buffer.readerIndex() + 4); buffer.skipBytes(8); if (deltaWindowSize == 0) { state = State.FRAME_ERROR; delegate.readFrameError("Invalid WINDOW_UPDATE Frame"); } else { state = State.READ_COMMON_HEADER; delegate.readWindowUpdateFrame(streamId, deltaWindowSize); } break; case READ_HEADER_BLOCK: if (length == 0) { state = State.READ_COMMON_HEADER; delegate.readHeaderBlockEnd(); break; } if (!buffer.isReadable()) { return; } int compressedBytes = Math.min(buffer.readableBytes(), length); ByteBuf headerBlock = buffer.alloc().buffer(compressedBytes); headerBlock.writeBytes(buffer, compressedBytes); length -= compressedBytes; delegate.readHeaderBlock(headerBlock); break; case DISCARD_FRAME: int numBytes = Math.min(buffer.readableBytes(), length); buffer.skipBytes(numBytes); length -= numBytes; if (length == 0) { state = State.READ_COMMON_HEADER; break; } return; case FRAME_ERROR: buffer.skipBytes(buffer.readableBytes()); return; default: throw new Error("Shouldn't reach here."); } } } SpdyFrameDecoder(SpdyVersion spdyVersion, SpdyFrameDecoderDelegate delegate);  SpdyFrameDecoder(SpdyVersion spdyVersion, SpdyFrameDecoderDelegate delegate, int maxChunkSize); void decode(ByteBuf buffer);  }
@Test public void testSpdySettingsFrame() throws Exception { short type = 4; byte flags = 0; int numSettings = 2; int length = 8 * numSettings + 4; byte idFlags = 0; int id = RANDOM.nextInt() & 0x00FFFFFF; int value = RANDOM.nextInt(); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); for (int i = 0; i < numSettings; i++) { buf.writeByte(idFlags); buf.writeMedium(id); buf.writeInt(value); } delegate.readSettingsEnd(); decoder.decode(buf); verify(delegate).readSettingsFrame(false); verify(delegate, times(numSettings)).readSetting(id, value, false, false); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testEmptySpdySettingsFrame() throws Exception { short type = 4; byte flags = 0; int numSettings = 0; int length = 8 * numSettings + 4; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); decoder.decode(buf); verify(delegate).readSettingsFrame(false); verify(delegate).readSettingsEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testSpdySettingsFrameClearFlag() throws Exception { short type = 4; byte flags = 0x01; int numSettings = 0; int length = 8 * numSettings + 4; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); decoder.decode(buf); verify(delegate).readSettingsFrame(true); verify(delegate).readSettingsEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testSpdySettingsPersistValues() throws Exception { short type = 4; byte flags = 0; int numSettings = 1; int length = 8 * numSettings + 4; byte idFlags = 0x01; int id = RANDOM.nextInt() & 0x00FFFFFF; int value = RANDOM.nextInt(); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); for (int i = 0; i < numSettings; i++) { buf.writeByte(idFlags); buf.writeMedium(id); buf.writeInt(value); } delegate.readSettingsEnd(); decoder.decode(buf); verify(delegate).readSettingsFrame(false); verify(delegate, times(numSettings)).readSetting(id, value, true, false); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testSpdySettingsPersistedValues() throws Exception { short type = 4; byte flags = 0; int numSettings = 1; int length = 8 * numSettings + 4; byte idFlags = 0x02; int id = RANDOM.nextInt() & 0x00FFFFFF; int value = RANDOM.nextInt(); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); for (int i = 0; i < numSettings; i++) { buf.writeByte(idFlags); buf.writeMedium(id); buf.writeInt(value); } delegate.readSettingsEnd(); decoder.decode(buf); verify(delegate).readSettingsFrame(false); verify(delegate, times(numSettings)).readSetting(id, value, false, true); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testUnknownSpdySettingsFrameFlags() throws Exception { short type = 4; byte flags = (byte) 0xFE; int numSettings = 0; int length = 8 * numSettings + 4; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); decoder.decode(buf); verify(delegate).readSettingsFrame(false); verify(delegate).readSettingsEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testUnknownSpdySettingsFlags() throws Exception { short type = 4; byte flags = 0; int numSettings = 1; int length = 8 * numSettings + 4; byte idFlags = (byte) 0xFC; int id = RANDOM.nextInt() & 0x00FFFFFF; int value = RANDOM.nextInt(); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); for (int i = 0; i < numSettings; i++) { buf.writeByte(idFlags); buf.writeMedium(id); buf.writeInt(value); } delegate.readSettingsEnd(); decoder.decode(buf); verify(delegate).readSettingsFrame(false); verify(delegate, times(numSettings)).readSetting(id, value, false, false); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdySettingsFrameLength() throws Exception { short type = 4; byte flags = 0; int numSettings = 2; int length = 8 * numSettings + 8; byte idFlags = 0; int id = RANDOM.nextInt() & 0x00FFFFFF; int value = RANDOM.nextInt(); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(numSettings); for (int i = 0; i < numSettings; i++) { buf.writeByte(idFlags); buf.writeMedium(id); buf.writeInt(value); } decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdySettingsFrameNumSettings() throws Exception { short type = 4; byte flags = 0; int numSettings = 2; int length = 8 * numSettings + 4; byte idFlags = 0; int id = RANDOM.nextInt() & 0x00FFFFFF; int value = RANDOM.nextInt(); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(0); for (int i = 0; i < numSettings; i++) { buf.writeByte(idFlags); buf.writeMedium(id); buf.writeInt(value); } decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testDiscardUnknownFrame() throws Exception { short type = 5; byte flags = (byte) 0xFF; int length = 8; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeLong(RANDOM.nextLong()); decoder.decode(buf); verifyZeroInteractions(delegate); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testDiscardUnknownEmptyFrame() throws Exception { short type = 5; byte flags = (byte) 0xFF; int length = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); decoder.decode(buf); verifyZeroInteractions(delegate); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testProgressivelyDiscardUnknownEmptyFrame() throws Exception { short type = 5; byte flags = (byte) 0xFF; int segment = 4; int length = 2 * segment; ByteBuf header = 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()); decoder.decode(header); decoder.decode(segment1); decoder.decode(segment2); verifyZeroInteractions(delegate); assertFalse(header.isReadable()); assertFalse(segment1.isReadable()); assertFalse(segment2.isReadable()); header.release(); segment1.release(); segment2.release(); }
@Test public void testSpdyPingFrame() throws Exception { short type = 6; byte flags = 0; int length = 4; int id = RANDOM.nextInt(); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(id); decoder.decode(buf); verify(delegate).readPingFrame(id); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testUnknownSpdyPingFrameFlags() throws Exception { short type = 6; byte flags = (byte) 0xFF; int length = 4; int id = RANDOM.nextInt(); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(id); decoder.decode(buf); verify(delegate).readPingFrame(id); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdyPingFrameLength() throws Exception { short type = 6; byte flags = 0; int length = 8; int id = RANDOM.nextInt(); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(id); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@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 = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(lastGoodStreamId); buf.writeInt(statusCode); decoder.decode(buf); verify(delegate).readGoAwayFrame(lastGoodStreamId, statusCode); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testUnknownSpdyGoAwayFrameFlags() throws Exception { short type = 7; byte flags = (byte) 0xFF; int length = 8; int lastGoodStreamId = RANDOM.nextInt() & 0x7FFFFFFF; int statusCode = RANDOM.nextInt() | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(lastGoodStreamId); buf.writeInt(statusCode); decoder.decode(buf); verify(delegate).readGoAwayFrame(lastGoodStreamId, statusCode); assertFalse(buf.isReadable()); buf.release(); }
@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 = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(lastGoodStreamId | 0x80000000); buf.writeInt(statusCode); decoder.decode(buf); verify(delegate).readGoAwayFrame(lastGoodStreamId, statusCode); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdyGoAwayFrameLength() throws Exception { short type = 7; byte flags = 0; int length = 12; int lastGoodStreamId = RANDOM.nextInt() & 0x7FFFFFFF; int statusCode = RANDOM.nextInt() | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(lastGoodStreamId); buf.writeInt(statusCode); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testSpdyHeadersFrame() throws Exception { short type = 8; byte flags = 0; int length = 4; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); decoder.decode(buf); verify(delegate).readHeadersFrame(streamId, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testLastSpdyHeadersFrame() throws Exception { short type = 8; byte flags = 0x01; int length = 4; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); decoder.decode(buf); verify(delegate).readHeadersFrame(streamId, true); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testUnknownSpdyHeadersFrameFlags() throws Exception { short type = 8; byte flags = (byte) 0xFE; int length = 4; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); decoder.decode(buf); verify(delegate).readHeadersFrame(streamId, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testReservedSpdyHeadersFrameBits() throws Exception { short type = 8; byte flags = 0; int length = 4; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId | 0x80000000); decoder.decode(buf); verify(delegate).readHeadersFrame(streamId, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdyHeadersFrameLength() throws Exception { short type = 8; byte flags = 0; int length = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdyHeadersFrameStreamId() throws Exception { short type = 8; byte flags = 0; int length = 4; int streamId = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testSpdyHeadersFrameHeaderBlock() throws Exception { short type = 8; byte flags = 0; int length = 4; int headerBlockLength = 1024; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length + headerBlockLength); buf.writeInt(streamId); ByteBuf headerBlock = Unpooled.buffer(headerBlockLength); for (int i = 0; i < 256; i ++) { headerBlock.writeInt(RANDOM.nextInt()); } decoder.decode(buf); decoder.decode(headerBlock); verify(delegate).readHeadersFrame(streamId, false); verify(delegate).readHeaderBlock(headerBlock.slice(0, headerBlock.writerIndex())); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); assertFalse(headerBlock.isReadable()); buf.release(); headerBlock.release(); }
@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); decoder.decode(buf); verify(delegate).readWindowUpdateFrame(streamId, deltaWindowSize); assertFalse(buf.isReadable()); }
@Test public void testUnknownSpdyWindowUpdateFrameFlags() throws Exception { short type = 9; byte flags = (byte) 0xFF; 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); decoder.decode(buf); verify(delegate).readWindowUpdateFrame(streamId, deltaWindowSize); assertFalse(buf.isReadable()); buf.release(); }
@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 = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId | 0x80000000); buf.writeInt(deltaWindowSize | 0x80000000); decoder.decode(buf); verify(delegate).readWindowUpdateFrame(streamId, deltaWindowSize); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdyWindowUpdateFrameLength() throws Exception { short type = 9; byte flags = 0; int length = 12; 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); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testIllegalSpdyWindowUpdateFrameDeltaWindowSize() throws Exception { short type = 9; byte flags = 0; int length = 8; int streamId = RANDOM.nextInt() & 0x7FFFFFFF; int deltaWindowSize = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(deltaWindowSize); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testSpdyDataFrame() throws Exception { int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; byte flags = 0; int length = 1024; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeDataFrameHeader(buf, streamId, flags, length); for (int i = 0; i < 256; i ++) { buf.writeInt(RANDOM.nextInt()); } decoder.decode(buf); verify(delegate).readDataFrame(streamId, false, buf.slice(SPDY_HEADER_SIZE, length)); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testEmptySpdyDataFrame() throws Exception { int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; byte flags = 0; int length = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeDataFrameHeader(buf, streamId, flags, length); decoder.decode(buf); verify(delegate).readDataFrame(streamId, false, Unpooled.EMPTY_BUFFER); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testLastSpdyDataFrame() throws Exception { int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; byte flags = 0x01; int length = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeDataFrameHeader(buf, streamId, flags, length); decoder.decode(buf); verify(delegate).readDataFrame(streamId, true, Unpooled.EMPTY_BUFFER); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testUnknownSpdyDataFrameFlags() throws Exception { int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; byte flags = (byte) 0xFE; int length = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeDataFrameHeader(buf, streamId, flags, length); decoder.decode(buf); verify(delegate).readDataFrame(streamId, false, Unpooled.EMPTY_BUFFER); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testIllegalSpdyDataFrameStreamId() throws Exception { int streamId = 0; byte flags = 0; int length = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeDataFrameHeader(buf, streamId, flags, length); decoder.decode(buf); verify(delegate).readFrameError((String) any()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testPipelinedSpdyDataFrames() throws Exception { int streamId1 = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; int streamId2 = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; byte flags = 0; int length = 0; ByteBuf buf = Unpooled.buffer(2 * (SPDY_HEADER_SIZE + length)); encodeDataFrameHeader(buf, streamId1, flags, length); encodeDataFrameHeader(buf, streamId2, flags, length); decoder.decode(buf); verify(delegate).readDataFrame(streamId1, false, Unpooled.EMPTY_BUFFER); verify(delegate).readDataFrame(streamId2, false, Unpooled.EMPTY_BUFFER); assertFalse(buf.isReadable()); buf.release(); }
@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 = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(associatedToStreamId); buf.writeByte(priority << 5); buf.writeByte(0); decoder.decode(buf); verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testLastSpdySynStreamFrame() throws Exception { short type = 1; 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 = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(associatedToStreamId); buf.writeByte(priority << 5); buf.writeByte(0); decoder.decode(buf); verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, true, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testUnidirectionalSpdySynStreamFrame() throws Exception { short type = 1; 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 = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(associatedToStreamId); buf.writeByte(priority << 5); buf.writeByte(0); decoder.decode(buf); verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, true); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testIndependentSpdySynStreamFrame() throws Exception { short type = 1; byte flags = 0; int length = 10; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; int associatedToStreamId = 0; byte priority = (byte) (RANDOM.nextInt() & 0x07); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(associatedToStreamId); buf.writeByte(priority << 5); buf.writeByte(0); decoder.decode(buf); verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testUnknownSpdySynStreamFrameFlags() throws Exception { short type = 1; 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 = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(associatedToStreamId); buf.writeByte(priority << 5); buf.writeByte(0); decoder.decode(buf); verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@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 = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId | 0x80000000); buf.writeInt(associatedToStreamId | 0x80000000); buf.writeByte(priority << 5 | 0x1F); buf.writeByte(0xFF); decoder.decode(buf); verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdySynStreamFrameLength() throws Exception { short type = 1; byte flags = 0; int length = 8; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(associatedToStreamId); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testIllegalSpdySynStreamFrameStreamId() throws Exception { short type = 1; byte flags = 0; int length = 10; int streamId = 0; int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF; byte priority = (byte) (RANDOM.nextInt() & 0x07); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(associatedToStreamId); buf.writeByte(priority << 5); buf.writeByte(0); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testSpdySynStreamFrameHeaderBlock() throws Exception { short type = 1; byte flags = 0; int length = 10; int headerBlockLength = 1024; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; int associatedToStreamId = RANDOM.nextInt() & 0x7FFFFFFF; byte priority = (byte) (RANDOM.nextInt() & 0x07); ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length + headerBlockLength); encodeControlFrameHeader(buf, type, flags, length + headerBlockLength); buf.writeInt(streamId); buf.writeInt(associatedToStreamId); buf.writeByte(priority << 5); buf.writeByte(0); ByteBuf headerBlock = Unpooled.buffer(headerBlockLength); for (int i = 0; i < 256; i ++) { headerBlock.writeInt(RANDOM.nextInt()); } decoder.decode(buf); decoder.decode(headerBlock); verify(delegate).readSynStreamFrame(streamId, associatedToStreamId, priority, false, false); verify(delegate).readHeaderBlock(headerBlock.slice(0, headerBlock.writerIndex())); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); assertFalse(headerBlock.isReadable()); buf.release(); headerBlock.release(); }
@Test public void testSpdySynReplyFrame() throws Exception { short type = 2; byte flags = 0; int length = 4; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); decoder.decode(buf); verify(delegate).readSynReplyFrame(streamId, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testLastSpdySynReplyFrame() throws Exception { short type = 2; byte flags = 0x01; int length = 4; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); decoder.decode(buf); verify(delegate).readSynReplyFrame(streamId, true); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testUnknownSpdySynReplyFrameFlags() throws Exception { short type = 2; byte flags = (byte) 0xFE; int length = 4; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); decoder.decode(buf); verify(delegate).readSynReplyFrame(streamId, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testReservedSpdySynReplyFrameBits() throws Exception { short type = 2; byte flags = 0; int length = 4; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId | 0x80000000); decoder.decode(buf); verify(delegate).readSynReplyFrame(streamId, false); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdySynReplyFrameLength() throws Exception { short type = 2; byte flags = 0; int length = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testIllegalSpdySynReplyFrameStreamId() throws Exception { short type = 2; byte flags = 0; int length = 4; int streamId = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testSpdySynReplyFrameHeaderBlock() throws Exception { short type = 2; byte flags = 0; int length = 4; int headerBlockLength = 1024; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length + headerBlockLength); encodeControlFrameHeader(buf, type, flags, length + headerBlockLength); buf.writeInt(streamId); ByteBuf headerBlock = Unpooled.buffer(headerBlockLength); for (int i = 0; i < 256; i ++) { headerBlock.writeInt(RANDOM.nextInt()); } decoder.decode(buf); decoder.decode(headerBlock); verify(delegate).readSynReplyFrame(streamId, false); verify(delegate).readHeaderBlock(headerBlock.slice(0, headerBlock.writerIndex())); verify(delegate).readHeaderBlockEnd(); assertFalse(buf.isReadable()); assertFalse(headerBlock.isReadable()); buf.release(); headerBlock.release(); }
@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 = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(statusCode); decoder.decode(buf); verify(delegate).readRstStreamFrame(streamId, statusCode); assertFalse(buf.isReadable()); buf.release(); }
@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 = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId | 0x80000000); buf.writeInt(statusCode); decoder.decode(buf); verify(delegate).readRstStreamFrame(streamId, statusCode); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdyRstStreamFrameFlags() throws Exception { short type = 3; byte flags = (byte) 0xFF; int length = 8; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; int statusCode = RANDOM.nextInt() | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(statusCode); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testInvalidSpdyRstStreamFrameLength() throws Exception { short type = 3; byte flags = 0; int length = 12; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; int statusCode = RANDOM.nextInt() | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(statusCode); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testIllegalSpdyRstStreamFrameStreamId() throws Exception { short type = 3; byte flags = 0; int length = 8; int streamId = 0; int statusCode = RANDOM.nextInt() | 0x01; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(statusCode); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }
@Test public void testIllegalSpdyRstStreamFrameStatusCode() throws Exception { short type = 3; byte flags = 0; int length = 8; int streamId = RANDOM.nextInt() & 0x7FFFFFFF | 0x01; int statusCode = 0; ByteBuf buf = Unpooled.buffer(SPDY_HEADER_SIZE + length); encodeControlFrameHeader(buf, type, flags, length); buf.writeInt(streamId); buf.writeInt(statusCode); decoder.decode(buf); verify(delegate).readFrameError(anyString()); assertFalse(buf.isReadable()); buf.release(); }