From 9c7f3b6b8ecf25c481931cde3200bc4275afeb33 Mon Sep 17 00:00:00 2001 From: hsaturn Date: Mon, 9 Aug 2021 10:35:49 +0200 Subject: [PATCH] Fix of decode length --- library.json | 2 +- library.properties | 2 +- src/TinyMqtt.cpp | 7 +++- src/TinyMqtt.h | 2 +- tests/length-tests/Makefile | 8 +++++ tests/length-tests/length-tests.ino | 53 +++++++++++++++++++++++++++++ 6 files changed, 70 insertions(+), 4 deletions(-) create mode 100644 tests/length-tests/Makefile create mode 100644 tests/length-tests/length-tests.ino diff --git a/library.json b/library.json index 48eccd7..4ca9c7e 100644 --- a/library.json +++ b/library.json @@ -6,7 +6,7 @@ "type": "git", "url": "https://github.com/hsaturn/TinyMqtt.git" }, - "version": "0.7.6", + "version": "0.7.7", "exclude": "", "examples": "examples/*/*.ino", "frameworks": "arduino", diff --git a/library.properties b/library.properties index 7604e7c..0157b3b 100644 --- a/library.properties +++ b/library.properties @@ -1,5 +1,5 @@ name=TinyMqtt -version=0.7.6 +version=0.7.7 author=Francois BIOT, HSaturn, maintainer=Francois BIOT, HSaturn, sentence=A tiny broker and client library for MQTT messaging. diff --git a/src/TinyMqtt.cpp b/src/TinyMqtt.cpp index 4ed0c22..a61245d 100644 --- a/src/TinyMqtt.cpp +++ b/src/TinyMqtt.cpp @@ -665,7 +665,12 @@ void MqttMessage::incoming(char in_byte) state = Length; break; case Length: - size = (size<<7) + (in_byte & 0x7F); + if (size==0) + size = in_byte & 0x7F; + else if (size<128) + size += static_cast(in_byte & 0x7F)<<7; + else + state = Error; // Really don't want to handle msg with length > 16k if (size > MaxBufferLength) { state = Error; diff --git a/src/TinyMqtt.h b/src/TinyMqtt.h index b1a1cd3..27487dd 100644 --- a/src/TinyMqtt.h +++ b/src/TinyMqtt.h @@ -64,7 +64,7 @@ class Topic : public IndexedString class MqttClient; class MqttMessage { - const uint16_t MaxBufferLength = 255; + const uint16_t MaxBufferLength = 4096; //hard limit: 16k public: enum Type { diff --git a/tests/length-tests/Makefile b/tests/length-tests/Makefile new file mode 100644 index 0000000..f79de70 --- /dev/null +++ b/tests/length-tests/Makefile @@ -0,0 +1,8 @@ +# See https://github.com/bxparks/EpoxyDuino for documentation about this +# Makefile to compile and run Arduino programs natively on Linux or MacOS. + +APP_NAME := length-tests +ARDUINO_LIBS := AUnit AceCommon AceTime TinyMqtt EspMock ESP8266WiFi ESPAsyncTCP +ARDUINO_LIB_DIRS := ../../../EspMock/libraries +EPOXY_CORE := EPOXY_CORE_ESP8266 +include ../../../EpoxyDuino/EpoxyDuino.mk diff --git a/tests/length-tests/length-tests.ino b/tests/length-tests/length-tests.ino new file mode 100644 index 0000000..53b63b9 --- /dev/null +++ b/tests/length-tests/length-tests.ino @@ -0,0 +1,53 @@ +#include +#include +#include + +/** + * TinyMqtt local unit tests. + * + * Clients are connected to pseudo remote broker + * The remote should be 127.0.0.1:1883 <--- But this does not work due to Esp network limitations + * We are using 127.0.0.1 because this is simpler to test with a single ESP + * Also, this will allow to mock and thus run Action on github + **/ + +using namespace std; + +MqttBroker broker(1883); + +std::map> published; // map[client_id] => map[topic] = count + +const char* lastPayload; +size_t lastLength; + +void onPublish(const MqttClient* srce, const Topic& topic, const char* payload, size_t length) +{ + if (srce) + published[srce->id()][topic]++; + lastPayload = payload; + lastLength = length; +} + +test(length_decode_greater_than_127) +{ + // TODO WRITE THIS TEST + // The test should verify than a mqtt message with more than 127 bytes is correctly decoded + assertEqual(1,2); +} + + +//---------------------------------------------------------------------------- +// setup() and loop() +void setup() { + delay(1000); + Serial.begin(115200); + while(!Serial); + + Serial.println("=============[ NO WIFI CONNECTION TinyMqtt TESTS ]========================"); +} + +void loop() { + aunit::TestRunner::run(); + + if (Serial.available()) ESP.reset(); +}