2121import io .netty .buffer .ByteBuf ;
2222import io .netty .buffer .Unpooled ;
2323import io .netty .channel .embedded .EmbeddedChannel ;
24+ import io .netty .handler .codec .DecoderException ;
2425import io .netty .handler .logging .LoggingHandler ;
2526import org .graylog2 .jackson .TypeReferences ;
2627import org .graylog2 .shared .bindings .providers .ObjectMapperProvider ;
3233import java .util .zip .Deflater ;
3334
3435import static org .assertj .core .api .Assertions .assertThat ;
36+ import static org .assertj .core .api .Assertions .assertThatThrownBy ;
3537
3638public class BeatsFrameDecoderTest {
3739 private final ObjectMapper objectMapper = new ObjectMapperProvider ().get ();
@@ -238,4 +240,46 @@ private long extractSequenceNumber(ByteBuf buffer) {
238240 assertThat (buffer .readableBytes ()).isEqualTo (0 );
239241 return seqNum ;
240242 }
241- }
243+
244+ @ Test
245+ public void rejectsJsonFrameExceedingLengthLimit () {
246+ final byte [] oversizedJson = ("{\" message\" :\" " + "X" .repeat (16 * 1024 * 1024 ) + "\" }" )
247+ .getBytes (StandardCharsets .UTF_8 );
248+
249+ final ByteBuf frame = buildJsonFrame (oversizedJson , 0 );
250+
251+ assertThatThrownBy (() -> {
252+ channel .writeInbound (frame );
253+ channel .checkException ();
254+ }).isInstanceOf (DecoderException .class );
255+ }
256+
257+ @ Test
258+ public void rejectsDataFrameExceedingPairLimit () {
259+ final ByteBuf frame = Unpooled .buffer ();
260+ frame .writeByte ('2' );
261+ frame .writeByte ('D' );
262+ frame .writeInt (0 );
263+ frame .writeInt (1025 );
264+
265+ assertThatThrownBy (() -> {
266+ channel .writeInbound (frame );
267+ channel .checkException ();
268+ }).isInstanceOf (DecoderException .class );
269+ }
270+
271+ @ Test
272+ public void rejectsDataItemExceedingLengthLimit () {
273+ final ByteBuf frame = Unpooled .buffer ();
274+ frame .writeByte ('2' );
275+ frame .writeByte ('D' );
276+ frame .writeInt (0 );
277+ frame .writeInt (1 );
278+ frame .writeInt (1024 * 1024 + 1 );
279+
280+ assertThatThrownBy (() -> {
281+ channel .writeInbound (frame );
282+ channel .checkException ();
283+ }).isInstanceOf (DecoderException .class );
284+ }
285+ }
0 commit comments