From 7209d677a40672177834377ae9aa63043e0622fa Mon Sep 17 00:00:00 2001 From: Ziver Koc Date: Tue, 6 Jan 2026 01:50:10 +0100 Subject: [PATCH] Fixed packet ID incorrect parsing --- .../net/mqtt/packet/MqttPacketPublish.java | 7 +++-- test/zutil/net/mqtt/MqttBrokerTest.java | 4 +-- .../mqtt/packet/MqttPacketPublishTest.java | 30 +++++++++++++++++-- 3 files changed, 33 insertions(+), 8 deletions(-) diff --git a/src/zutil/net/mqtt/packet/MqttPacketPublish.java b/src/zutil/net/mqtt/packet/MqttPacketPublish.java index c67e026..f109b82 100755 --- a/src/zutil/net/mqtt/packet/MqttPacketPublish.java +++ b/src/zutil/net/mqtt/packet/MqttPacketPublish.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2020 Ziver Koc + * Copyright (c) 2020-2026 Ziver Koc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -167,9 +167,10 @@ public class MqttPacketPublish extends MqttPacketHeader { public Object read(InputStream in, BinaryFieldData field, Object parentObject) throws IOException { MqttPacketPublish publish = (MqttPacketPublish) parentObject; - if (0 < publish.getFlagQoS()) { + if (publish.getFlagQoS() > 0) { byte[] b = new byte[2]; - in.read(b); + b[1] = (byte) in.read(); + b[0] = (byte) in.read(); return Converter.toInt(b); } diff --git a/test/zutil/net/mqtt/MqttBrokerTest.java b/test/zutil/net/mqtt/MqttBrokerTest.java index 208a832..9610502 100644 --- a/test/zutil/net/mqtt/MqttBrokerTest.java +++ b/test/zutil/net/mqtt/MqttBrokerTest.java @@ -1,7 +1,7 @@ /* * The MIT License (MIT) * - * Copyright (c) 2020-2025 Ziver Koc + * Copyright (c) 2020-2026 Ziver Koc * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal @@ -423,7 +423,7 @@ public class MqttBrokerTest { // Setup publish message MqttPacketPublish publish = new MqttPacketPublish(); publish.topicName = "test/topic"; - publish.setFlagQoS(3); + publish.setFlagQoS(2); // Test illegal qos assertThrows(UnsupportedOperationException.class, () -> { diff --git a/test/zutil/net/mqtt/packet/MqttPacketPublishTest.java b/test/zutil/net/mqtt/packet/MqttPacketPublishTest.java index 0de495e..34e7719 100644 --- a/test/zutil/net/mqtt/packet/MqttPacketPublishTest.java +++ b/test/zutil/net/mqtt/packet/MqttPacketPublishTest.java @@ -1,3 +1,27 @@ +/* + * The MIT License (MIT) + * + * Copyright (c) 2026 Ziver Koc + * + * Permission is hereby granted, free of charge, to any person obtaining a copy + * of this software and associated documentation files (the "Software"), to deal + * in the Software without restriction, including without limitation the rights + * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell + * copies of the Software, and to permit persons to whom the Software is + * furnished to do so, subject to the following conditions: + * + * The above copyright notice and this permission notice shall be included in + * all copies or substantial portions of the Software. + * + * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE + * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, + * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN + * THE SOFTWARE. + */ + package zutil.net.mqtt.packet; import org.junit.Test; @@ -172,15 +196,15 @@ public class MqttPacketPublishTest { 0xFF & 2, // length 'a', // Topic Name 'b', // Topic Name - 0b0000_0000, // Packet ID - 0b0000_0101 // Packet ID + 0b0110_1101, // Packet ID 28140 (0x6D) + 0b1110_1100 // Packet ID 28140 (0xEC) // Payload }; MqttPacketPublish obj = new MqttPacketPublish(); obj.setFlagQoS(MqttPacketPublish.PUBLISH_QOS_2); obj.topicName = "ab"; - obj.packetId = 5; + obj.packetId = 28140; ByteArrayOutputStream buffer = new ByteArrayOutputStream(); BinaryStructOutputStream binOut = new BinaryStructOutputStream(buffer);