|
21 | 21 | import io.netty.buffer.UnpooledByteBufAllocator; |
22 | 22 | import io.netty.channel.Channel; |
23 | 23 | import io.netty.channel.ChannelHandlerContext; |
| 24 | +import io.netty.channel.embedded.EmbeddedChannel; |
24 | 25 | import io.netty.handler.codec.DecoderException; |
25 | 26 | import io.netty.handler.codec.EncoderException; |
26 | 27 | import io.netty.handler.codec.TooLongFrameException; |
@@ -336,6 +337,78 @@ public void testPublishMessage() throws Exception { |
336 | 337 | validatePublishPayload(message.payload(), decodedMessage.payload()); |
337 | 338 | } |
338 | 339 |
|
| 340 | + @Test |
| 341 | + public void testPublishMessageIncompleteVariableHeaderDoesNotUseCumulationSizeForTooLongCheck() throws Exception { |
| 342 | + // The leading PUBLISH is hand-crafted rather than going through MqttEncoder because the |
| 343 | + // bug under test only triggers when variable-header decoding asks for REPLAY mid-message, |
| 344 | + // which in turn requires a deliberately malformed packet (topic-name length prefix larger |
| 345 | + // than the bytes we actually supply). MqttEncoder only produces well-formed messages. |
| 346 | + final int maxBytesInMessage = 16; |
| 347 | + // bytes after the fixed header; < 128 so it fits in a 1-byte Variable Byte Integer. |
| 348 | + final int currentPacketRemainingLength = 10; |
| 349 | + // > the bytes we write below, so the decoder must REPLAY mid-variable-header. |
| 350 | + final int claimedTopicNameLength = 32; |
| 351 | + final int followingPingReqPackets = 3; |
| 352 | + EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder(maxBytesInMessage)); |
| 353 | + ByteBuf byteBuf = ALLOCATOR.buffer(); |
| 354 | + // Leading PUBLISH packet (incomplete - missing most of the topic name): |
| 355 | + // Fixed header byte 1: PUBLISH (type 3), DUP=0, QoS=0, RETAIN=0. |
| 356 | + byteBuf.writeByte(0x30); |
| 357 | + // Fixed header remaining-length, encoded as a Variable Byte Integer (single byte for values < 128). |
| 358 | + byteBuf.writeByte(currentPacketRemainingLength); |
| 359 | + // Variable header: 2-byte topic-name length prefix. |
| 360 | + byteBuf.writeShort(claimedTopicNameLength); |
| 361 | + // ... + only 8 of the 32 topic-name bytes the prefix claims (so the decoder will ask for REPLAY). |
| 362 | + byteBuf.writeZero(currentPacketRemainingLength - 2); |
| 363 | + // Trailing PINGREQ packets - the cumulation bytes that the buggy size check used to look at. |
| 364 | + // Each PINGREQ is just a 2-byte fixed header: 0xC0 (type 12, flags 0) and remaining-length 0. |
| 365 | + for (int i = 0; i < followingPingReqPackets; i++) { |
| 366 | + byteBuf.writeByte(0xC0); |
| 367 | + byteBuf.writeByte(0); |
| 368 | + } |
| 369 | + |
| 370 | + try { |
| 371 | + assertFalse(channel.writeInbound(byteBuf)); |
| 372 | + assertNull(channel.readInbound()); |
| 373 | + } finally { |
| 374 | + channel.finishAndReleaseAll(); |
| 375 | + } |
| 376 | + } |
| 377 | + |
| 378 | + @Test |
| 379 | + public void testPublishMessageIncompleteVariableHeaderStillFailsWhenCurrentPacketTooLarge() throws Exception { |
| 380 | + // Same hand-crafting rationale as the test above: a malformed (incomplete-topic) PUBLISH |
| 381 | + // is needed so variable-header decoding asks for REPLAY, which is the code path under test. |
| 382 | + final int maxBytesInMessage = 16; |
| 383 | + // Declared packet size already exceeds the limit; the in-flight check must still flag it. |
| 384 | + final int currentPacketRemainingLength = maxBytesInMessage + 1; |
| 385 | + // > the bytes we write below, so the decoder still asks for REPLAY mid-variable-header. |
| 386 | + final int claimedTopicNameLength = 32; |
| 387 | + EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder(maxBytesInMessage)); |
| 388 | + ByteBuf byteBuf = ALLOCATOR.buffer(); |
| 389 | + // Fixed header byte 1: PUBLISH (type 3), all flags 0. |
| 390 | + byteBuf.writeByte(0x30); |
| 391 | + // Fixed header remaining-length Variable Byte Integer: 17 (still a single byte since < 128). |
| 392 | + byteBuf.writeByte(currentPacketRemainingLength); |
| 393 | + // Variable header: 2-byte topic-name length prefix claiming 32 bytes. |
| 394 | + byteBuf.writeShort(claimedTopicNameLength); |
| 395 | + // ... + 14 zero bytes - fewer than the 32 claimed, so the decoder will ask for REPLAY. |
| 396 | + byteBuf.writeZero(maxBytesInMessage - 2); |
| 397 | + |
| 398 | + try { |
| 399 | + assertTrue(channel.writeInbound(byteBuf)); |
| 400 | + MqttMessage decodedMessage = channel.readInbound(); |
| 401 | + try { |
| 402 | + assertTrue(decodedMessage.decoderResult().isFailure()); |
| 403 | + assertInstanceOf(TooLongFrameException.class, decodedMessage.decoderResult().cause()); |
| 404 | + } finally { |
| 405 | + ReferenceCountUtil.release(decodedMessage); |
| 406 | + } |
| 407 | + } finally { |
| 408 | + channel.finishAndReleaseAll(); |
| 409 | + } |
| 410 | + } |
| 411 | + |
339 | 412 | @Test |
340 | 413 | public void testPubAckMessage() throws Exception { |
341 | 414 | testMessageWithOnlyFixedHeaderAndMessageIdVariableHeader(MqttMessageType.PUBACK); |
|