|
51 | 51 |
|
52 | 52 | import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.AUTHENTICATION_DATA; |
53 | 53 | import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.AUTHENTICATION_METHOD; |
| 54 | +import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.CONTENT_TYPE; |
54 | 55 | import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.MAXIMUM_PACKET_SIZE; |
55 | 56 | import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.MAXIMUM_QOS; |
56 | 57 | import static io.netty.handler.codec.mqtt.MqttProperties.MqttPropertyType.PAYLOAD_FORMAT_INDICATOR; |
@@ -937,6 +938,74 @@ public void testPublishMqtt5SingleBytePropertiesLengthIsUnaffected() throws Exce |
937 | 938 | } |
938 | 939 | } |
939 | 940 |
|
| 941 | + @Test |
| 942 | + public void testPublishMqtt5PropertyLengthOverreadIsRejected() throws Exception { |
| 943 | + EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder()); |
| 944 | + try { |
| 945 | + assertTrue(channel.writeInbound(newMqtt5Connect())); |
| 946 | + ReferenceCountUtil.release(channel.readInbound()); |
| 947 | + |
| 948 | + // 30 0a 00 01 61 02 03 00 03 78 79 7a |
| 949 | + ByteBuf malformed = channel.alloc().buffer(); |
| 950 | + malformed.writeByte(0x30); // PUBLISH, DUP=0, QoS=0, RETAIN=0 |
| 951 | + malformed.writeByte(0x0a); // Remaining Length = 10 |
| 952 | + writeMqttUtf8String(malformed, "a"); // topic name "a" |
| 953 | + malformed.writeByte(0x02); // Property Length = 2 (too short for what follows) |
| 954 | + malformed.writeByte(CONTENT_TYPE.value()); // 0x03: starts a Content Type string property |
| 955 | + malformed.writeByte(0x00); // string length prefix, msb -- Properties section ends here |
| 956 | + malformed.writeByte(0x03); // string length prefix lsb -- actually outside Property Length |
| 957 | + malformed.writeByte('x'); |
| 958 | + malformed.writeByte('y'); |
| 959 | + malformed.writeByte('z'); |
| 960 | + |
| 961 | + assertTrue(channel.writeInbound(malformed)); |
| 962 | + |
| 963 | + MqttMessage decoded = channel.readInbound(); |
| 964 | + try { |
| 965 | + assertTrue(decoded.decoderResult().isFailure()); |
| 966 | + assertInstanceOf(DecoderException.class, decoded.decoderResult().cause()); |
| 967 | + } finally { |
| 968 | + ReferenceCountUtil.release(decoded); |
| 969 | + } |
| 970 | + } finally { |
| 971 | + channel.finishAndReleaseAll(); |
| 972 | + } |
| 973 | + } |
| 974 | + |
| 975 | + @Test |
| 976 | + public void testPublishMqtt5PropertyLengthCoveringFullStringIsAccepted() throws Exception { |
| 977 | + EmbeddedChannel channel = new EmbeddedChannel(new MqttDecoder()); |
| 978 | + try { |
| 979 | + assertTrue(channel.writeInbound(newMqtt5Connect())); |
| 980 | + ReferenceCountUtil.release(channel.readInbound()); |
| 981 | + |
| 982 | + // 30 0a 00 01 61 06 03 00 03 78 79 7a |
| 983 | + ByteBuf wellFormed = channel.alloc().buffer(); |
| 984 | + wellFormed.writeByte(0x30); |
| 985 | + wellFormed.writeByte(0x0a); |
| 986 | + writeMqttUtf8String(wellFormed, "a"); |
| 987 | + wellFormed.writeByte(0x06); // Property Length = 6, covers the full Content Type string |
| 988 | + wellFormed.writeByte(CONTENT_TYPE.value()); |
| 989 | + writeMqttUtf8String(wellFormed, "xyz"); |
| 990 | + |
| 991 | + assertTrue(channel.writeInbound(wellFormed)); |
| 992 | + |
| 993 | + MqttPublishMessage decoded = channel.readInbound(); |
| 994 | + try { |
| 995 | + assertFalse(decoded.decoderResult().isFailure()); |
| 996 | + assertEquals("a", decoded.variableHeader().topicName()); |
| 997 | + assertEquals("xyz", |
| 998 | + ((MqttProperties.StringProperty) decoded.variableHeader() |
| 999 | + .properties().getProperty(CONTENT_TYPE.value())).value); |
| 1000 | + assertEquals(0, decoded.payload().readableBytes()); |
| 1001 | + } finally { |
| 1002 | + ReferenceCountUtil.release(decoded); |
| 1003 | + } |
| 1004 | + } finally { |
| 1005 | + channel.finishAndReleaseAll(); |
| 1006 | + } |
| 1007 | + } |
| 1008 | + |
940 | 1009 | // Minimal well-formed MQTT 5 CONNECT (empty client id) whose only purpose is to make the decoder |
941 | 1010 | // record MQTT 5 for the channel so subsequent packets have their properties decoded. |
942 | 1011 | private static ByteBuf newMqtt5Connect() { |
|