|
23 | 23 | import org.junit.jupiter.api.AfterEach; |
24 | 24 | import org.junit.jupiter.api.BeforeEach; |
25 | 25 | import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.params.ParameterizedTest; |
| 27 | +import org.junit.jupiter.params.provider.ValueSource; |
26 | 28 |
|
27 | 29 | import static io.netty.handler.codec.stomp.StompTestConstants.*; |
28 | 30 | import static io.netty.util.CharsetUtil.*; |
@@ -590,4 +592,41 @@ void testContentLengthEqualToIntegerMaxValueIsAccepted() { |
590 | 592 | assertNull(channel.readInbound()); |
591 | 593 | assertTrue(channel.finishAndReleaseAll()); |
592 | 594 | } |
| 595 | + |
| 596 | + // Read sizes: byte by byte, a few bytes, about half of the frame, and the whole frame in one read. |
| 597 | + @ParameterizedTest(name = "read size {0}") |
| 598 | + @ValueSource(ints = { 1, 7, 90, Integer.MAX_VALUE }) |
| 599 | + void testMaxNumHeadersNotExceededWhenHeadersSplitAcrossReads(int readSize) { |
| 600 | + // 10 headers and a limit of 10: the frame must be accepted however it is split into reads. |
| 601 | + StompHeadersSubframe headersSubFrame = decodeHeaders(frameWithHeaders(10), readSize, 10); |
| 602 | + assertTrue(headersSubFrame.decoderResult().isSuccess(), String.valueOf(headersSubFrame.decoderResult())); |
| 603 | + assertEquals(10, headersSubFrame.headers().size()); |
| 604 | + } |
| 605 | + |
| 606 | + @ParameterizedTest(name = "read size {0}") |
| 607 | + @ValueSource(ints = { 1, 7, 90, Integer.MAX_VALUE }) |
| 608 | + void testMaxNumHeadersEnforcedWhenHeadersSplitAcrossReads(int readSize) { |
| 609 | + StompHeadersSubframe headersSubFrame = decodeHeaders(frameWithHeaders(11), readSize, 10); |
| 610 | + assertTrue(headersSubFrame.decoderResult().isFailure()); |
| 611 | + assertInstanceOf(TooLongFrameException.class, headersSubFrame.decoderResult().cause()); |
| 612 | + } |
| 613 | + |
| 614 | + private static byte[] frameWithHeaders(int numHeaders) { |
| 615 | + StringBuilder frame = new StringBuilder("SEND\ndestination:/queue/a\n"); |
| 616 | + for (int i = 1; i < numHeaders; i++) { |
| 617 | + frame.append("header").append(i).append(":value").append(i).append('\n'); |
| 618 | + } |
| 619 | + return frame.append("\nbody\0").toString().getBytes(UTF_8); |
| 620 | + } |
| 621 | + |
| 622 | + private static StompHeadersSubframe decodeHeaders(byte[] frame, int readSize, int maxNumHeaders) { |
| 623 | + EmbeddedChannel channel = new EmbeddedChannel(new StompSubframeDecoder(1024, 1024, maxNumHeaders, true)); |
| 624 | + for (int i = 0; i < frame.length; i += Math.min(readSize, frame.length - i)) { |
| 625 | + channel.writeInbound(Unpooled.wrappedBuffer(frame, i, Math.min(readSize, frame.length - i))); |
| 626 | + } |
| 627 | + StompHeadersSubframe headersSubFrame = channel.readInbound(); |
| 628 | + assertNotNull(headersSubFrame); |
| 629 | + channel.finishAndReleaseAll(); |
| 630 | + return headersSubFrame; |
| 631 | + } |
593 | 632 | } |