From 09b77d316621c064de07136e85d5ac88b6283f83 Mon Sep 17 00:00:00 2001
From: Davide Pianca <davidepianca98@gmail.com>
Date: Fri, 5 Jul 2024 11:04:56 +0200
Subject: [PATCH] Fix WillQos field in ConnectFlags deserialization

---
 .../kotlin/mqtt/packets/ConnectFlags.kt       |  2 +-
 .../mqtt/packets/mqttv5/MQTTConnectTest.kt    | 67 ++++++++++++++++---
 2 files changed, 60 insertions(+), 9 deletions(-)

diff --git a/kmqtt-common/src/commonMain/kotlin/mqtt/packets/ConnectFlags.kt b/kmqtt-common/src/commonMain/kotlin/mqtt/packets/ConnectFlags.kt
index 527474b..afe23bf 100644
--- a/kmqtt-common/src/commonMain/kotlin/mqtt/packets/ConnectFlags.kt
+++ b/kmqtt-common/src/commonMain/kotlin/mqtt/packets/ConnectFlags.kt
@@ -29,7 +29,7 @@ public data class ConnectFlags(
             if (reserved)
                 throw MQTTException(ReasonCode.MALFORMED_PACKET)
             val willFlag = ((byte shr 2) and 1) == 1
-            val willQos = ((byte shr 4) and 1) or ((byte shl 3) and 1)
+            val willQos = ((byte shr 3) and 3)
             val willRetain = ((byte shr 5) and 1) == 1
             if (willFlag) {
                 if (willQos == 3)
diff --git a/kmqtt-common/src/commonTest/kotlin/mqtt/packets/mqttv5/MQTTConnectTest.kt b/kmqtt-common/src/commonTest/kotlin/mqtt/packets/mqttv5/MQTTConnectTest.kt
index a9bbe64..cda2de1 100644
--- a/kmqtt-common/src/commonTest/kotlin/mqtt/packets/mqttv5/MQTTConnectTest.kt
+++ b/kmqtt-common/src/commonTest/kotlin/mqtt/packets/mqttv5/MQTTConnectTest.kt
@@ -11,20 +11,64 @@ class MQTTConnectTest {
 
     private val array = ubyteArrayOf(
         0x10u,
-        0x0Du,
+        0x39u,
         0x00u,
         0x04u,
-        0x4Du,
+        0x4du,
         0x51u,
         0x54u,
         0x54u,
         0x05u,
-        0x02u,
+        0x16u,
+        0x00u,
+        0x3cu,
+        0x05u,
+        0x11u,
+        0x00u,
+        0x00u,
+        0x00u,
+        0x00u,
+        0x00u,
+        0x0eu,
+        0x6du,
+        0x71u,
+        0x74u,
+        0x74u,
+        0x78u,
+        0x5fu,
+        0x66u,
+        0x35u,
+        0x61u,
+        0x63u,
+        0x30u,
+        0x31u,
+        0x37u,
+        0x31u,
+        0x06u,
+        0x03u,
         0x00u,
-        0x3Cu,
         0x00u,
+        0x08u,
         0x00u,
-        0x00u
+        0x00u,
+        0x00u,
+        0x0au,
+        0x2fu,
+        0x74u,
+        0x65u,
+        0x73u,
+        0x74u,
+        0x2fu,
+        0x77u,
+        0x69u,
+        0x6cu,
+        0x6cu,
+        0x00u,
+        0x04u,
+        0x61u,
+        0x62u,
+        0x63u,
+        0x64u
     )
     private val packet = MQTT5Connect(
         "MQTT",
@@ -32,12 +76,17 @@ class MQTTConnectTest {
             false,
             false,
             false,
-            Qos.AT_MOST_ONCE,
-            false,
+            Qos.EXACTLY_ONCE,
+            true,
             true,
             false
         ),
-        60
+        60,
+        clientID = "mqttx_f5ac0171",
+        properties = MQTT5Properties(sessionExpiryInterval = 0u),
+        willTopic = "/test/will",
+        willPayload = "abcd".encodeToByteArray().toUByteArray(),
+        willProperties = MQTT5Properties(contentType = "", responseTopic = "")
     )
 
     @Test
@@ -51,5 +100,7 @@ class MQTTConnectTest {
         assertEquals(packet.protocolName, result.protocolName)
         assertEquals(packet.protocolVersion, result.protocolVersion)
         assertEquals(packet.keepAlive, result.keepAlive)
+        assertEquals(packet.connectFlags.willFlag, true)
+        assertEquals(packet.connectFlags.willQos, Qos.EXACTLY_ONCE)
     }
 }
-- 
GitLab