|
48 | 48 |
|
49 | 49 | import static io.netty.handler.codec.mqtt.MqttProperties.AUTHENTICATION_DATA; |
50 | 50 | import static io.netty.handler.codec.mqtt.MqttProperties.AUTHENTICATION_METHOD; |
| 51 | +import static io.netty.handler.codec.mqtt.MqttProperties.CONTENT_TYPE; |
51 | 52 | import static io.netty.handler.codec.mqtt.MqttProperties.MAXIMUM_PACKET_SIZE; |
52 | 53 | import static io.netty.handler.codec.mqtt.MqttProperties.MAXIMUM_QOS; |
53 | 54 | import static io.netty.handler.codec.mqtt.MqttProperties.PAYLOAD_FORMAT_INDICATOR; |
@@ -900,6 +901,74 @@ public void testPublishMqtt5SingleBytePropertiesLengthIsUnaffected() throws Exce |
900 | 901 | } |
901 | 902 | } |
902 | 903 |
|
| 904 | + @Test |
| 905 | + public void testPublishMqtt5PropertyLengthOverreadIsRejected() throws Exception { |
| 906 | + EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder()); |
| 907 | + try { |
| 908 | + assertTrue(channel.writeInbound(newMqtt5Connect())); |
| 909 | + ReferenceCountUtil.release(channel.readInbound()); |
| 910 | + |
| 911 | + // 30 0a 00 01 61 02 03 00 03 78 79 7a |
| 912 | + ByteBuf malformed = channel.alloc().buffer(); |
| 913 | + malformed.writeByte(0x30); // PUBLISH, DUP=0, QoS=0, RETAIN=0 |
| 914 | + malformed.writeByte(0x0a); // Remaining Length = 10 |
| 915 | + writeMqttUtf8String(malformed, "a"); // topic name "a" |
| 916 | + malformed.writeByte(0x02); // Property Length = 2 (too short for what follows) |
| 917 | + malformed.writeByte(CONTENT_TYPE); // 0x03: starts a Content Type string property |
| 918 | + malformed.writeByte(0x00); // string length prefix, msb -- Properties section ends here |
| 919 | + malformed.writeByte(0x03); // string length prefix lsb -- actually outside Property Length |
| 920 | + malformed.writeByte('x'); |
| 921 | + malformed.writeByte('y'); |
| 922 | + malformed.writeByte('z'); |
| 923 | + |
| 924 | + assertTrue(channel.writeInbound(malformed)); |
| 925 | + |
| 926 | + MqttMessage decoded = channel.readInbound(); |
| 927 | + try { |
| 928 | + assertTrue(decoded.decoderResult().isFailure()); |
| 929 | + assertInstanceOf(DecoderException.class, decoded.decoderResult().cause()); |
| 930 | + } finally { |
| 931 | + ReferenceCountUtil.release(decoded); |
| 932 | + } |
| 933 | + } finally { |
| 934 | + channel.finishAndReleaseAll(); |
| 935 | + } |
| 936 | + } |
| 937 | + |
| 938 | + @Test |
| 939 | + public void testPublishMqtt5PropertyLengthCoveringFullStringIsAccepted() throws Exception { |
| 940 | + EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder()); |
| 941 | + try { |
| 942 | + assertTrue(channel.writeInbound(newMqtt5Connect())); |
| 943 | + ReferenceCountUtil.release(channel.readInbound()); |
| 944 | + |
| 945 | + // 30 0a 00 01 61 06 03 00 03 78 79 7a |
| 946 | + ByteBuf wellFormed = channel.alloc().buffer(); |
| 947 | + wellFormed.writeByte(0x30); |
| 948 | + wellFormed.writeByte(0x0a); |
| 949 | + writeMqttUtf8String(wellFormed, "a"); |
| 950 | + wellFormed.writeByte(0x06); // Property Length = 6, covers the full Content Type string |
| 951 | + wellFormed.writeByte(CONTENT_TYPE); |
| 952 | + writeMqttUtf8String(wellFormed, "xyz"); |
| 953 | + |
| 954 | + assertTrue(channel.writeInbound(wellFormed)); |
| 955 | + |
| 956 | + MqttPublishMessage decoded = channel.readInbound(); |
| 957 | + try { |
| 958 | + assertFalse(decoded.decoderResult().isFailure()); |
| 959 | + assertEquals("a", decoded.variableHeader().topicName()); |
| 960 | + assertEquals("xyz", |
| 961 | + ((MqttProperties.StringProperty) decoded.variableHeader() |
| 962 | + .properties().getProperty(CONTENT_TYPE)).value); |
| 963 | + assertEquals(0, decoded.payload().readableBytes()); |
| 964 | + } finally { |
| 965 | + ReferenceCountUtil.release(decoded); |
| 966 | + } |
| 967 | + } finally { |
| 968 | + channel.finishAndReleaseAll(); |
| 969 | + } |
| 970 | + } |
| 971 | + |
903 | 972 | // Minimal well-formed MQTT 5 CONNECT (empty client id) whose only purpose is to make the decoder |
904 | 973 | // record MQTT 5 for the channel so subsequent packets have their properties decoded. |
905 | 974 | private static ByteBuf newMqtt5Connect() { |
|