|
48 | 48 | import static org.junit.jupiter.api.Assertions.assertFalse; |
49 | 49 | import static org.junit.jupiter.api.Assertions.assertInstanceOf; |
50 | 50 | import static org.junit.jupiter.api.Assertions.assertThrows; |
| 51 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
51 | 52 | import static org.mockito.Mockito.*; |
52 | 53 |
|
53 | 54 | /** |
@@ -351,6 +352,72 @@ public void writeFrameHasPayload() throws Exception { |
351 | 352 | assertEquals(expectedOutbound, outbound); |
352 | 353 | } |
353 | 354 |
|
| 355 | + @Test |
| 356 | + public void writeFrameExceedingMaxFrameSizeFailsPromiseAndReleasesPayload() throws Exception { |
| 357 | + frameWriter.maxFrameSize(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND); |
| 358 | + ByteBuf payload = Unpooled.buffer().writeZero(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND + 1); |
| 359 | + |
| 360 | + ChannelFuture future = frameWriter.writeFrame(ctx, (byte) 0xf, 0, new Http2Flags(), payload, promise); |
| 361 | + |
| 362 | + assertTrue(future.isDone()); |
| 363 | + assertFalse(future.isSuccess()); |
| 364 | + assertInstanceOf(IllegalArgumentException.class, future.cause()); |
| 365 | + assertTrue(future.cause().getMessage().contains("0xf")); |
| 366 | + // Nothing must have been written and the payload must have been released |
| 367 | + assertEquals(0, outbound.readableBytes()); |
| 368 | + assertEquals(0, payload.refCnt()); |
| 369 | + verify(ctx, never()).write(any(), any(ChannelPromise.class)); |
| 370 | + } |
| 371 | + |
| 372 | + @Test |
| 373 | + public void writeFrameAtMaxFrameSizeSucceeds() throws Exception { |
| 374 | + frameWriter.maxFrameSize(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND); |
| 375 | + ByteBuf payload = Unpooled.buffer().writeZero(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND); |
| 376 | + |
| 377 | + ChannelFuture future = frameWriter.writeFrame(ctx, (byte) 0xf, 0, new Http2Flags(), payload, promise); |
| 378 | + |
| 379 | + // The mocked context never completes the writes, so only verify that the write was not rejected |
| 380 | + assertFalse(future.isDone() && !future.isSuccess()); |
| 381 | + assertEquals(Http2CodecUtil.FRAME_HEADER_LENGTH + Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND, |
| 382 | + outbound.readableBytes()); |
| 383 | + // 24 bit payload length field must hold exactly the payload size |
| 384 | + assertEquals(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND, outbound.getUnsignedMedium(0)); |
| 385 | + } |
| 386 | + |
| 387 | + @Test |
| 388 | + public void writeSettingsExceedingMaxFrameSizeFailsPromise() throws Exception { |
| 389 | + frameWriter.maxFrameSize(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND); |
| 390 | + Http2Settings settings = new Http2Settings(); |
| 391 | + // Each setting takes 6 bytes, so this is one entry more than fits into a single frame |
| 392 | + int entries = Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND / Http2CodecUtil.SETTING_ENTRY_LENGTH + 1; |
| 393 | + for (int i = 0; i < entries; i++) { |
| 394 | + settings.put((char) (0x100 + i), Long.valueOf(0)); |
| 395 | + } |
| 396 | + |
| 397 | + ChannelFuture future = frameWriter.writeSettings(ctx, settings, promise); |
| 398 | + |
| 399 | + assertTrue(future.isDone()); |
| 400 | + assertFalse(future.isSuccess()); |
| 401 | + assertInstanceOf(IllegalArgumentException.class, future.cause()); |
| 402 | + assertTrue(future.cause().getMessage().contains("0x4")); |
| 403 | + assertEquals(0, outbound.readableBytes()); |
| 404 | + } |
| 405 | + |
| 406 | + @Test |
| 407 | + public void writeGoAwayTruncatesDebugDataExceedingMaxFrameSize() throws Exception { |
| 408 | + frameWriter.maxFrameSize(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND); |
| 409 | + ByteBuf debugData = Unpooled.buffer().writeZero(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND); |
| 410 | + |
| 411 | + frameWriter.writeGoAway(ctx, 0, Http2Error.NO_ERROR.code(), debugData, promise); |
| 412 | + |
| 413 | + // 8 bytes of last stream id and error code plus the truncated debug data fill exactly one frame |
| 414 | + assertEquals(Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND, outbound.getUnsignedMedium(0)); |
| 415 | + assertEquals(Http2CodecUtil.FRAME_HEADER_LENGTH + Http2CodecUtil.MAX_FRAME_SIZE_LOWER_BOUND, |
| 416 | + outbound.readableBytes()); |
| 417 | + // The original debug data buffer must have been released |
| 418 | + assertEquals(0, debugData.refCnt()); |
| 419 | + } |
| 420 | + |
354 | 421 | @Test |
355 | 422 | public void writePriority() { |
356 | 423 | frameWriter.writePriority( |
|