|
15 | 15 | package io.netty.handler.codec.http2; |
16 | 16 |
|
17 | 17 | import io.netty.buffer.ByteBuf; |
| 18 | +import io.netty.buffer.CompositeByteBuf; |
18 | 19 | import io.netty.buffer.Unpooled; |
19 | 20 | import io.netty.channel.ChannelFuture; |
20 | 21 | import io.netty.channel.ChannelFutureListener; |
@@ -119,6 +120,11 @@ public void tearDown() throws Exception { |
119 | 120 | } |
120 | 121 |
|
121 | 122 | private void setUp(Http2FrameCodecBuilder frameCodecBuilder, Http2Settings initialRemoteSettings) throws Exception { |
| 123 | + setUp(frameCodecBuilder, initialRemoteSettings, true); |
| 124 | + } |
| 125 | + |
| 126 | + private void setUp(Http2FrameCodecBuilder frameCodecBuilder, Http2Settings initialRemoteSettings, boolean preface) |
| 127 | + throws Exception { |
122 | 128 | /* |
123 | 129 | * Some tests call this method twice. Once with JUnit's @Before and once directly to pass special settings. |
124 | 130 | * This call ensures that in case of two consecutive calls to setUp(), the previous channel is shutdown and |
@@ -142,18 +148,21 @@ private void setUp(Http2FrameCodecBuilder frameCodecBuilder, Http2Settings initi |
142 | 148 | // Handshake |
143 | 149 | verify(frameWriter).writeSettings(eqFrameCodecCtx(), anyHttp2Settings(), anyChannelPromise()); |
144 | 150 | verifyNoMoreInteractions(frameWriter); |
145 | | - channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf()); |
146 | 151 |
|
147 | | - frameInboundWriter.writeInboundSettings(initialRemoteSettings); |
| 152 | + if (preface) { |
| 153 | + channel.writeInbound(Http2CodecUtil.connectionPrefaceBuf()); |
148 | 154 |
|
149 | | - verify(frameWriter).writeSettingsAck(eqFrameCodecCtx(), anyChannelPromise()); |
| 155 | + frameInboundWriter.writeInboundSettings(initialRemoteSettings); |
150 | 156 |
|
151 | | - frameInboundWriter.writeInboundSettingsAck(); |
| 157 | + verify(frameWriter).writeSettingsAck(eqFrameCodecCtx(), anyChannelPromise()); |
152 | 158 |
|
153 | | - Http2SettingsFrame settingsFrame = inboundHandler.readInbound(); |
154 | | - assertNotNull(settingsFrame); |
155 | | - Http2SettingsAckFrame settingsAckFrame = inboundHandler.readInbound(); |
156 | | - assertNotNull(settingsAckFrame); |
| 159 | + frameInboundWriter.writeInboundSettingsAck(); |
| 160 | + |
| 161 | + Http2SettingsFrame settingsFrame = inboundHandler.readInbound(); |
| 162 | + assertNotNull(settingsFrame); |
| 163 | + Http2SettingsAckFrame settingsAckFrame = inboundHandler.readInbound(); |
| 164 | + assertNotNull(settingsAckFrame); |
| 165 | + } |
157 | 166 | } |
158 | 167 |
|
159 | 168 | @Test |
@@ -415,6 +424,43 @@ public void unknownFrameOnMissingStream() throws Exception { |
415 | 424 | assertTrue(channel.isActive()); |
416 | 425 | } |
417 | 426 |
|
| 427 | + @Test |
| 428 | + public void unknownFrameOnMissingStreamFirstPacket() throws Exception { |
| 429 | + setUp(Http2FrameCodecBuilder.forClient(), new Http2Settings(), false); |
| 430 | + |
| 431 | + // SETTINGS and UNKNOWN must come in on the same packet to trigger the bug |
| 432 | + channel.pipeline().addFirst("combine", new ChannelInboundHandlerAdapter() { |
| 433 | + CompositeByteBuf accumulate; |
| 434 | + |
| 435 | + @Override |
| 436 | + public void channelRead(ChannelHandlerContext ctx, Object msg) { |
| 437 | + if (accumulate == null) { |
| 438 | + accumulate = ctx.alloc().compositeBuffer(); |
| 439 | + } |
| 440 | + accumulate.addComponent(true, (ByteBuf) msg); |
| 441 | + } |
| 442 | + |
| 443 | + @Override |
| 444 | + public void handlerRemoved(ChannelHandlerContext ctx) { |
| 445 | + if (accumulate != null) { |
| 446 | + ctx.fireChannelRead(accumulate); |
| 447 | + ctx.fireChannelReadComplete(); |
| 448 | + } |
| 449 | + } |
| 450 | + }); |
| 451 | + |
| 452 | + frameInboundWriter.writeInboundSettings(new Http2Settings()); |
| 453 | + |
| 454 | + ByteBuf debugData = bb("debug"); |
| 455 | + frameInboundWriter.writeInboundFrame((byte) 0xb, 101, new Http2Flags(), debugData); |
| 456 | + |
| 457 | + channel.pipeline().remove("combine"); |
| 458 | + channel.flushInbound(); |
| 459 | + |
| 460 | + assertEquals(0, debugData.refCnt()); |
| 461 | + assertTrue(channel.isActive()); |
| 462 | + } |
| 463 | + |
418 | 464 | @Test |
419 | 465 | public void goAwayLastStreamIdOverflowed() throws Exception { |
420 | 466 | frameInboundWriter.writeInboundHeaders(5, request, 31, false); |
|