diff --git a/src/DotNetty.Codecs.Mqtt/MqttDecoder.cs b/src/DotNetty.Codecs.Mqtt/MqttDecoder.cs index 25414e89e..617c82027 100644 --- a/src/DotNetty.Codecs.Mqtt/MqttDecoder.cs +++ b/src/DotNetty.Codecs.Mqtt/MqttDecoder.cs @@ -337,7 +337,11 @@ static void DecodePublishPacket(IByteBuffer buffer, PublishPacket packet, ref in static void DecodePacketIdVariableHeader(IByteBuffer buffer, PacketWithId packet, ref int remainingLength) { - packet.PacketId = DecodeUnsignedShort(buffer, ref remainingLength); + int packetId = packet.PacketId = DecodeUnsignedShort(buffer, ref remainingLength); + if (packetId == 0) + { + throw new DecoderException("[MQTT-2.3.1-1]"); + } } static void DecodeSubscribePayload(IByteBuffer buffer, SubscribePacket packet, ref int remainingLength) diff --git a/test/DotNetty.Codecs.Mqtt.Tests/MqttCodecTests.cs b/test/DotNetty.Codecs.Mqtt.Tests/MqttCodecTests.cs index af412fce3..ffa3f511b 100644 --- a/test/DotNetty.Codecs.Mqtt.Tests/MqttCodecTests.cs +++ b/test/DotNetty.Codecs.Mqtt.Tests/MqttCodecTests.cs @@ -103,7 +103,7 @@ public void TestConnAckMessage(bool sessionPresent, ConnectReturnCode returnCode } [Theory] - [InlineData(0, new[] { "+", "+/+", "//", "/#", "+//+" }, new[] { QualityOfService.ExactlyOnce, QualityOfService.AtLeastOnce, QualityOfService.AtMostOnce, QualityOfService.ExactlyOnce, QualityOfService.AtMostOnce })] + [InlineData(1, new[] { "+", "+/+", "//", "/#", "+//+" }, new[] { QualityOfService.ExactlyOnce, QualityOfService.AtLeastOnce, QualityOfService.AtMostOnce, QualityOfService.ExactlyOnce, QualityOfService.AtMostOnce })] [InlineData(ushort.MaxValue, new[] { "a" }, new[] { QualityOfService.AtLeastOnce })] public void TestSubscribeMessage(int packetId, string[] topicFilters, QualityOfService[] requestedQosValues) { @@ -117,7 +117,7 @@ public void TestSubscribeMessage(int packetId, string[] topicFilters, QualityOfS } [Theory] - [InlineData(0, new[] { QualityOfService.ExactlyOnce, QualityOfService.AtLeastOnce, QualityOfService.AtMostOnce, QualityOfService.Failure, QualityOfService.AtMostOnce })] + [InlineData(1, new[] { QualityOfService.ExactlyOnce, QualityOfService.AtLeastOnce, QualityOfService.AtMostOnce, QualityOfService.Failure, QualityOfService.AtMostOnce })] [InlineData(ushort.MaxValue, new[] { QualityOfService.AtLeastOnce })] public void TestSubAckMessage(int packetId, QualityOfService[] qosValues) { @@ -133,7 +133,7 @@ public void TestSubAckMessage(int packetId, QualityOfService[] qosValues) } [Theory] - [InlineData(0, new[] { "+", "+/+", "//", "/#", "+//+" })] + [InlineData(1, new[] { "+", "+/+", "//", "/#", "+//+" })] [InlineData(ushort.MaxValue, new[] { "a" })] public void TestUnsubscribeMessage(int packetId, string[] topicFilters) { @@ -147,10 +147,10 @@ public void TestUnsubscribeMessage(int packetId, string[] topicFilters) } [Theory] - [InlineData(QualityOfService.AtMostOnce, false, false, 0, "a", null)] + [InlineData(QualityOfService.AtMostOnce, false, false, 1, "a", null)] [InlineData(QualityOfService.ExactlyOnce, true, false, ushort.MaxValue, "/", new byte[0])] - [InlineData(QualityOfService.AtLeastOnce, false, true, 0, "a/b", new byte[] { 1, 2, 3 })] - [InlineData(QualityOfService.ExactlyOnce, true, true, 1, "topic/name/that/is/longer/than/256/characters/topic/name/that/is/longer/than/256/characters/topic/name/that/is/longer/than/256/characters/topic/name/that/is/longer/than/256/characters/topic/name/that/is/longer/than/256/characters/topic/name/that/is/longer/than/256/characters/", new byte[] { 1 })] + [InlineData(QualityOfService.AtLeastOnce, false, true, 129, "a/b", new byte[] { 1, 2, 3 })] + [InlineData(QualityOfService.ExactlyOnce, true, true, ushort.MaxValue - 1, "topic/name/that/is/longer/than/256/characters/topic/name/that/is/longer/than/256/characters/topic/name/that/is/longer/than/256/characters/topic/name/that/is/longer/than/256/characters/topic/name/that/is/longer/than/256/characters/topic/name/that/is/longer/than/256/characters/", new byte[] { 1 })] public void TestPublishMessage(QualityOfService qos, bool dup, bool retain, int packetId, string topicName, byte[] payload) { var packet = new PublishPacket(qos, dup, retain); @@ -173,8 +173,8 @@ public void TestPublishMessage(QualityOfService qos, bool dup, bool retain, int } [Theory] - [InlineData(0)] [InlineData(1)] + [InlineData(127)] [InlineData(128)] [InlineData(256)] [InlineData(257)]