From b371fc9b9129a1415412f759d763838c9ee9fc80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jo=C3=ABl=20G=C3=A4hwiler?= Date: Thu, 7 Jun 2018 10:55:22 +0200 Subject: [PATCH] fixed wrong connect flags discovered by @mattiabertorello: https://github.com/256dpi/arduino-mqtt/pull/108 --- src/packet.c | 4 +-- tests/packet.cpp | 74 ++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 76 insertions(+), 2 deletions(-) diff --git a/src/packet.c b/src/packet.c index 5ed579f..512b44d 100644 --- a/src/packet.c +++ b/src/packet.c @@ -132,11 +132,11 @@ lwmqtt_err_t lwmqtt_encode_connect(uint8_t *buf, size_t buf_len, size_t *len, lw // set username flag if present if (options.username.len > 0) { - lwmqtt_write_bits(&flags, 1, 6, 1); + lwmqtt_write_bits(&flags, 1, 7, 1); // set password flag if present if (options.password.len > 0) { - lwmqtt_write_bits(&flags, 1, 7, 1); + lwmqtt_write_bits(&flags, 1, 6, 1); } } diff --git a/tests/packet.cpp b/tests/packet.cpp index 191c6c9..ecd127a 100644 --- a/tests/packet.cpp +++ b/tests/packet.cpp @@ -174,6 +174,80 @@ TEST(ConnectTest, Encode2) { EXPECT_ARRAY_EQ(pkt, buf, len); } +TEST(ConnectTest, Encode3) { + uint8_t pkt[50] = { + LWMQTT_CONNECT_PACKET << 4, + 48, + 0, // Protocol String MSB + 4, // Protocol String LSB + 'M', + 'Q', + 'T', + 'T', + 4, // Protocol level 4 + 140, // Connect Flags + 0, // Keep Alive MSB + 10, // Keep Alive LSB + 0, // Client ID MSB + 7, // Client ID LSB + 's', + 'u', + 'r', + 'g', + 'e', + 'm', + 'q', + 0, // Will Topic MSB + 4, // Will Topic LSB + 'w', + 'i', + 'l', + 'l', + 0, // Will Message MSB + 12, // Will Message LSB + 's', + 'e', + 'n', + 'd', + ' ', + 'm', + 'e', + ' ', + 'h', + 'o', + 'm', + 'e', + 0, // Username ID MSB + 7, // Username ID LSB + 's', + 'u', + 'r', + 'g', + 'e', + 'm', + 'q', + }; + + uint8_t buf[62]; + + lwmqtt_will_t will = lwmqtt_default_will; + will.topic = lwmqtt_string("will"); + will.payload = lwmqtt_string("send me home"); + will.qos = LWMQTT_QOS1; + + lwmqtt_options_t opts = lwmqtt_default_options; + opts.clean_session = false; + opts.keep_alive = 10; + opts.client_id = lwmqtt_string("surgemq"); + opts.username = lwmqtt_string("surgemq"); + + size_t len; + lwmqtt_err_t err = lwmqtt_encode_connect(buf, 62, &len, opts, &will); + + EXPECT_EQ(err, LWMQTT_SUCCESS); + EXPECT_ARRAY_EQ(pkt, buf, len); +} + TEST(ConnectTest, EncodeError1) { uint8_t buf[4]; // <- too small buffer