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