Add MsgPack bin8/bin16/bin32 support

Closes #2078
Closes #922
This commit is contained in:
Aubrey (Sanae)
2024-04-29 14:47:40 +02:00
committed by Benoit Blanchon
parent cd4bf33132
commit 18a9a5b590
16 changed files with 358 additions and 11 deletions

View File

@ -113,6 +113,42 @@ TEST_CASE("Compare JsonVariant with JsonVariant") {
CHECK_FALSE(a == b);
}
SECTION("MsgPackBinary('abc') vs MsgPackBinary('abc')") {
a.set(MsgPackBinary("abc", 4));
b.set(MsgPackBinary("abc", 4));
CHECK(a == b);
CHECK(a <= b);
CHECK(a >= b);
CHECK_FALSE(a != b);
CHECK_FALSE(a < b);
CHECK_FALSE(a > b);
}
SECTION("MsgPackBinary('abc') vs MsgPackBinary('bcd')") {
a.set(MsgPackBinary("abc", 4));
b.set(MsgPackBinary("bcd", 4));
CHECK(a != b);
CHECK(a < b);
CHECK(a <= b);
CHECK_FALSE(a == b);
CHECK_FALSE(a > b);
CHECK_FALSE(a >= b);
}
SECTION("MsgPackBinary('bcd') vs MsgPackBinary('abc')") {
a.set(MsgPackBinary("bcd", 4));
b.set(MsgPackBinary("abc", 4));
CHECK(a != b);
CHECK(a > b);
CHECK(a >= b);
CHECK_FALSE(a < b);
CHECK_FALSE(a <= b);
CHECK_FALSE(a == b);
}
SECTION("false vs true") {
a.set(false);
b.set(true);

View File

@ -21,6 +21,8 @@ TEST_CASE("Unbound JsonVariant") {
CHECK(variant.as<JsonObject>().isNull());
CHECK(variant.as<JsonObjectConst>().isNull());
CHECK(variant.as<JsonString>().isNull());
CHECK(variant.as<MsgPackBinary>().data() == nullptr);
CHECK(variant.as<MsgPackBinary>().size() == 0);
}
SECTION("is<T>()") {
@ -46,6 +48,7 @@ TEST_CASE("Unbound JsonVariant") {
CHECK_FALSE(variant.set(serialized("42")));
CHECK_FALSE(variant.set(serialized(std::string("42"))));
CHECK_FALSE(variant.set(true));
CHECK_FALSE(variant.set(MsgPackBinary("hello", 5)));
}
SECTION("add()") {

View File

@ -21,6 +21,22 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 1") {
REQUIRE(doc.overflowed() == true);
}
SECTION("set() returns true if binary has 255 characters") {
auto str = std::string(255, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("set() returns false if binary has 256 characters") {
auto str = std::string(256, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
SECTION("deserializeJson() returns Ok if string has 255 characters") {
auto input = "\"" + std::string(255, '?') + "\"";
@ -37,7 +53,7 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 1") {
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("deserializeMsgPack() returns Ok of string has 255 characters") {
SECTION("deserializeMsgPack() returns Ok if string has 255 characters") {
auto input = "\xd9\xff" + std::string(255, '?');
auto err = deserializeMsgPack(doc, input);
@ -46,11 +62,28 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 1") {
}
SECTION(
"deserializeMsgPack() returns NoMemory of string has 256 characters") {
"deserializeMsgPack() returns NoMemory if string has 256 characters") {
auto input = std::string("\xda\x01\x00", 3) + std::string(256, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("deserializeMsgPack() returns Ok if binary has 255 characters") {
auto input = "\xc4\xff" + std::string(255, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION(
"deserializeMsgPack() returns NoMemory if binary has 256 characters") {
auto input = std::string("\xc5\x01\x00", 3) + std::string(256, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
}

View File

@ -21,6 +21,22 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 2") {
REQUIRE(doc.overflowed() == true);
}
SECTION("set() returns true if string has 65535 characters") {
auto str = std::string(65535, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("set() returns false if string has 65536 characters") {
auto str = std::string(65536, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == false);
REQUIRE(doc.overflowed() == true);
}
SECTION("deserializeJson() returns Ok if string has 65535 characters") {
auto input = "\"" + std::string(65535, '?') + "\"";
@ -37,7 +53,7 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 2") {
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("deserializeMsgPack() returns Ok of string has 65535 characters") {
SECTION("deserializeMsgPack() returns Ok if string has 65535 characters") {
auto input = "\xda\xff\xff" + std::string(65535, '?');
auto err = deserializeMsgPack(doc, input);
@ -46,7 +62,7 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 2") {
}
SECTION(
"deserializeMsgPack() returns NoMemory of string has 65536 characters") {
"deserializeMsgPack() returns NoMemory if string has 65536 characters") {
auto input =
std::string("\xdb\x00\x01\x00\x00", 5) + std::string(65536, '?');
@ -54,4 +70,22 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 2") {
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("deserializeMsgPack() returns Ok if binary has 65535 characters") {
auto input = "\xc5\xff\xff" + std::string(65535, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION(
"deserializeMsgPack() returns NoMemory of binary has 65536 characters") {
auto input =
std::string("\xc6\x00\x01\x00\x00", 5) + std::string(65536, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::NoMemory);
}
}

View File

@ -14,6 +14,14 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
REQUIRE(doc.overflowed() == false);
}
SECTION("set() returns true if binary has 65536 characters") {
auto str = std::string(65536, '?');
auto result = doc.set(MsgPackBinary(str.data(), str.size()));
REQUIRE(result == true);
REQUIRE(doc.overflowed() == false);
}
SECTION("deserializeJson() returns Ok if string has 65536 characters") {
auto input = "\"" + std::string(65536, '?') + "\"";
@ -22,11 +30,45 @@ TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
REQUIRE(err == DeserializationError::Ok);
}
SECTION("deserializeMsgPack() returns Ok of string has 65536 characters") {
SECTION("deserializeMsgPack() returns Ok if string has 65536 characters") {
auto input = "\xda\xff\xff" + std::string(65536, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("deserializeMsgPack() returns Ok if binary has 65536 characters") {
auto input = "\xc5\xff\xff" + std::string(65536, '?');
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("bin 32 deserialization") {
auto str = std::string(65536, '?');
auto input = std::string("\xc6\x00\x01\x00\x00", 5) + str;
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackBinary>());
auto binary = doc.as<MsgPackBinary>();
REQUIRE(binary.size() == 65536);
REQUIRE(binary.data() != nullptr);
REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
binary.size()) == str);
}
SECTION("bin 32 serialization") {
auto str = std::string(65536, '?');
doc.set(MsgPackBinary(str.data(), str.size()));
std::string output;
auto result = serializeMsgPack(doc, output);
REQUIRE(result == 5 + str.size());
REQUIRE(output == std::string("\xc6\x00\x01\x00\x00", 5) + str);
}
}

View File

@ -3,6 +3,7 @@
// MIT License
#include <ArduinoJson.h>
#include <array>
#include <catch.hpp>
#include "Allocators.hpp"
@ -139,6 +140,36 @@ TEST_CASE("deserialize MsgPack value") {
SECTION("str 32") {
checkValue<std::string>("\xdb\x00\x00\x00\x05hello", std::string("hello"));
}
SECTION("bin 8") {
JsonDocument doc;
DeserializationError error = deserializeMsgPack(doc, "\xc4\x01?");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackBinary>());
auto binary = doc.as<MsgPackBinary>();
REQUIRE(binary.size() == 1);
REQUIRE(binary.data() != nullptr);
REQUIRE(reinterpret_cast<const char*>(binary.data())[0] == '?');
}
SECTION("bin 16") {
JsonDocument doc;
auto str = std::string(256, '?');
auto input = std::string("\xc5\x01\x00", 3) + str;
DeserializationError error = deserializeMsgPack(doc, input);
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackBinary>());
auto binary = doc.as<MsgPackBinary>();
REQUIRE(binary.size() == 0x100);
REQUIRE(binary.data() != nullptr);
REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
binary.size()) == str);
}
}
TEST_CASE("deserializeMsgPack() under memory constaints") {

View File

@ -3,6 +3,7 @@
// MIT License
#include <ArduinoJson.h>
#include <array>
#include <catch.hpp>
template <typename T>
@ -146,6 +147,17 @@ TEST_CASE("serialize MsgPack value") {
checkVariant(serialized("\xDB\x00\x01\x00\x00", 5), "\xDB\x00\x01\x00\x00");
}
SECTION("bin 8") {
auto str = std::string(1, 1);
checkVariant(MsgPackBinary(str.data(), str.size()), "\xC4\x01\x01");
}
SECTION("bin 16") {
auto str = std::string(256, 1);
checkVariant(MsgPackBinary(str.data(), str.size()),
std::string("\xC5\x01\x00", 3) + str);
}
SECTION("serialize round double as integer") { // Issue #1718
checkVariant(-32768.0, "\xD1\x80\x00");
checkVariant(-129.0, "\xD1\xFF\x7F");