Update to ArduinoJson 7.0.4

This commit is contained in:
iranl
2024-04-19 14:44:01 +02:00
parent 1378732081
commit 81be0a689a
444 changed files with 10842 additions and 9422 deletions

View File

@@ -1,19 +1,17 @@
# ArduinoJson - https://arduinojson.org
# Copyright © 2014-2023, Benoit BLANCHON
# Copyright © 2014-2024, Benoit BLANCHON
# MIT License
add_executable(MsgPackDeserializerTests
deserializeArray.cpp
deserializeObject.cpp
deserializeStaticVariant.cpp
deserializeVariant.cpp
destination_types.cpp
doubleToFloat.cpp
errors.cpp
filter.cpp
incompleteInput.cpp
input_types.cpp
misc.cpp
nestingLimit.cpp
notSupported.cpp
)
add_test(MsgPackDeserializer MsgPackDeserializerTests)

View File

@@ -1,12 +1,12 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("deserialize MsgPack array") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
SECTION("fixarray") {
SECTION("empty") {

View File

@@ -1,12 +1,12 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("deserialize MsgPack object") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
SECTION("fixmap") {
SECTION("empty") {

View File

@@ -1,152 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
template <size_t Capacity>
static void check(const char* input, DeserializationError expected) {
StaticJsonDocument<Capacity> doc;
DeserializationError error = deserializeMsgPack(doc, input);
CAPTURE(input);
REQUIRE(error == expected);
}
template <size_t Size>
static void checkString(const char* input, DeserializationError expected) {
check<Size>(input, expected);
}
TEST_CASE("deserializeMsgPack(StaticJsonDocument&)") {
SECTION("single values always fit") {
check<0>("\xc0", DeserializationError::Ok); // nil
check<0>("\xc2", DeserializationError::Ok); // false
check<0>("\xc3", DeserializationError::Ok); // true
check<0>("\xcc\x00", DeserializationError::Ok); // uint 8
check<0>("\xcd\x30\x39", DeserializationError::Ok); // uint 16
check<0>("\xCE\x12\x34\x56\x78", DeserializationError::Ok); // uint 32
}
SECTION("fixstr") {
checkString<8>("\xA0", DeserializationError::Ok);
checkString<8>("\xA7ZZZZZZZ", DeserializationError::Ok);
checkString<8>("\xA8ZZZZZZZZ", DeserializationError::NoMemory);
checkString<16>("\xAFZZZZZZZZZZZZZZZ", DeserializationError::Ok);
checkString<16>("\xB0ZZZZZZZZZZZZZZZZ", DeserializationError::NoMemory);
}
SECTION("str 8") {
checkString<8>("\xD9\x00", DeserializationError::Ok);
checkString<8>("\xD9\x07ZZZZZZZ", DeserializationError::Ok);
checkString<8>("\xD9\x08ZZZZZZZZ", DeserializationError::NoMemory);
checkString<16>("\xD9\x0FZZZZZZZZZZZZZZZ", DeserializationError::Ok);
checkString<16>("\xD9\x10ZZZZZZZZZZZZZZZZ", DeserializationError::NoMemory);
}
SECTION("str 16") {
checkString<8>("\xDA\x00\x00", DeserializationError::Ok);
checkString<8>("\xDA\x00\x07ZZZZZZZ", DeserializationError::Ok);
checkString<8>("\xDA\x00\x08ZZZZZZZZ", DeserializationError::NoMemory);
checkString<16>("\xDA\x00\x0FZZZZZZZZZZZZZZZ", DeserializationError::Ok);
checkString<16>("\xDA\x00\x10ZZZZZZZZZZZZZZZZ",
DeserializationError::NoMemory);
}
SECTION("str 32") {
checkString<8>("\xDB\x00\x00\x00\x00", DeserializationError::Ok);
checkString<8>("\xDB\x00\x00\x00\x07ZZZZZZZ", DeserializationError::Ok);
checkString<8>("\xDB\x00\x00\x00\x08ZZZZZZZZ",
DeserializationError::NoMemory);
checkString<16>("\xDB\x00\x00\x00\x0FZZZZZZZZZZZZZZZ",
DeserializationError::Ok);
checkString<16>("\xDB\x00\x00\x00\x10ZZZZZZZZZZZZZZZZ",
DeserializationError::NoMemory);
}
SECTION("fixarray") {
check<JSON_ARRAY_SIZE(0)>("\x90", DeserializationError::Ok); // []
check<JSON_ARRAY_SIZE(0)>("\x91\x01",
DeserializationError::NoMemory); // [1]
check<JSON_ARRAY_SIZE(1)>("\x91\x01", DeserializationError::Ok); // [1]
check<JSON_ARRAY_SIZE(1)>("\x92\x01\x02",
DeserializationError::NoMemory); // [1,2]
}
SECTION("array 16") {
check<JSON_ARRAY_SIZE(0)>("\xDC\x00\x00", DeserializationError::Ok);
check<JSON_ARRAY_SIZE(0)>("\xDC\x00\x01\x01",
DeserializationError::NoMemory);
check<JSON_ARRAY_SIZE(1)>("\xDC\x00\x01\x01", DeserializationError::Ok);
check<JSON_ARRAY_SIZE(1)>("\xDC\x00\x02\x01\x02",
DeserializationError::NoMemory);
}
SECTION("array 32") {
check<JSON_ARRAY_SIZE(0)>("\xDD\x00\x00\x00\x00", DeserializationError::Ok);
check<JSON_ARRAY_SIZE(0)>("\xDD\x00\x00\x00\x01\x01",
DeserializationError::NoMemory);
check<JSON_ARRAY_SIZE(1)>("\xDD\x00\x00\x00\x01\x01",
DeserializationError::Ok);
check<JSON_ARRAY_SIZE(1)>("\xDD\x00\x00\x00\x02\x01\x02",
DeserializationError::NoMemory);
}
SECTION("fixmap") {
SECTION("{}") {
check<JSON_OBJECT_SIZE(0)>("\x80", DeserializationError::Ok);
}
SECTION("{H:1}") {
check<JSON_OBJECT_SIZE(0)>("\x81\xA1H\x01",
DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
"\x81\xA1H\x01", DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
"\x82\xA1H\x01\xA1W\x02", DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(2) + 2 * JSON_STRING_SIZE(2)>(
"\x82\xA1H\x01\xA1W\x02", DeserializationError::Ok);
}
}
SECTION("map 16") {
SECTION("{}") {
check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x00", DeserializationError::Ok);
}
SECTION("{H:1}") {
check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x01\xA1H\x01",
DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
"\xDE\x00\x01\xA1H\x01", DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
"\xDE\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(2) + 2 * JSON_OBJECT_SIZE(1)>(
"\xDE\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::Ok);
}
}
SECTION("map 32") {
SECTION("{}") {
check<JSON_OBJECT_SIZE(0)>("\xDF\x00\x00\x00\x00",
DeserializationError::Ok);
}
SECTION("{H:1}") {
check<JSON_OBJECT_SIZE(0)>("\xDF\x00\x00\x00\x01\xA1H\x01",
DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
"\xDF\x00\x00\x00\x01\xA1H\x01", DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
check<JSON_OBJECT_SIZE(1) + JSON_STRING_SIZE(2)>(
"\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::NoMemory);
check<JSON_OBJECT_SIZE(2) + 2 * JSON_OBJECT_SIZE(1)>(
"\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::Ok);
}
}
}

View File

@@ -1,13 +1,15 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
template <typename T, typename U>
static void check(const char* input, U expected) {
DynamicJsonDocument doc(4096);
#include "Allocators.hpp"
template <typename T>
static void checkValue(const char* input, T expected) {
JsonDocument doc;
DeserializationError error = deserializeMsgPack(doc, input);
@@ -16,132 +18,224 @@ static void check(const char* input, U expected) {
REQUIRE(doc.as<T>() == expected);
}
#if ARDUINOJSON_USE_LONG_LONG == 0
static void checkNotSupported(const char* input) {
DynamicJsonDocument doc(4096);
DeserializationError error = deserializeMsgPack(doc, input);
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.isNull());
}
#endif
static void checkIsNull(const char* input) {
DynamicJsonDocument doc(4096);
static void checkError(size_t timebombCountDown, const char* input,
DeserializationError expected) {
TimebombAllocator timebomb(timebombCountDown);
JsonDocument doc(&timebomb);
DeserializationError error = deserializeMsgPack(doc, input);
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.as<JsonVariant>().isNull());
CAPTURE(input);
REQUIRE(error == expected);
}
TEST_CASE("deserialize MsgPack value") {
SECTION("nil") {
checkIsNull("\xc0");
checkValue("\xc0", nullptr);
}
SECTION("bool") {
check<bool>("\xc2", false);
check<bool>("\xc3", true);
checkValue<bool>("\xc2", false);
checkValue<bool>("\xc3", true);
}
SECTION("positive fixint") {
check<int>("\x00", 0);
check<int>("\x7F", 127);
checkValue<int>("\x00", 0);
checkValue<int>("\x7F", 127);
}
SECTION("negative fixint") {
check<int>("\xe0", -32);
check<int>("\xff", -1);
checkValue<int>("\xe0", -32);
checkValue<int>("\xff", -1);
}
SECTION("uint 8") {
check<int>("\xcc\x00", 0);
check<int>("\xcc\xff", 255);
checkValue<int>("\xcc\x00", 0);
checkValue<int>("\xcc\xff", 255);
}
SECTION("uint 16") {
check<int>("\xcd\x00\x00", 0);
check<int>("\xcd\xFF\xFF", 65535);
check<int>("\xcd\x30\x39", 12345);
checkValue<int>("\xcd\x00\x00", 0);
checkValue<int>("\xcd\xFF\xFF", 65535);
checkValue<int>("\xcd\x30\x39", 12345);
}
SECTION("uint 32") {
check<uint32_t>("\xCE\x00\x00\x00\x00", 0x00000000U);
check<uint32_t>("\xCE\xFF\xFF\xFF\xFF", 0xFFFFFFFFU);
check<uint32_t>("\xCE\x12\x34\x56\x78", 0x12345678U);
checkValue<uint32_t>("\xCE\x00\x00\x00\x00", 0x00000000U);
checkValue<uint32_t>("\xCE\xFF\xFF\xFF\xFF", 0xFFFFFFFFU);
checkValue<uint32_t>("\xCE\x12\x34\x56\x78", 0x12345678U);
}
SECTION("uint 64") {
#if ARDUINOJSON_USE_LONG_LONG
check<uint64_t>("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
check<uint64_t>("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
0xFFFFFFFFFFFFFFFFU);
check<uint64_t>("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
0x123456789ABCDEF0U);
checkValue<uint64_t>("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 0U);
checkValue<uint64_t>("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
0xFFFFFFFFFFFFFFFFU);
checkValue<uint64_t>("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
0x123456789ABCDEF0U);
#else
checkNotSupported("\xCF\x00\x00\x00\x00\x00\x00\x00\x00");
checkNotSupported("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
checkNotSupported("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
checkValue("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", nullptr);
checkValue("\xCF\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", nullptr);
checkValue("\xCF\x12\x34\x56\x78\x9A\xBC\xDE\xF0", nullptr);
#endif
}
SECTION("int 8") {
check<int>("\xd0\x00", 0);
check<int>("\xd0\xff", -1);
checkValue<int>("\xd0\x00", 0);
checkValue<int>("\xd0\xff", -1);
}
SECTION("int 16") {
check<int>("\xD1\x00\x00", 0);
check<int>("\xD1\xFF\xFF", -1);
check<int>("\xD1\xCF\xC7", -12345);
checkValue<int>("\xD1\x00\x00", 0);
checkValue<int>("\xD1\xFF\xFF", -1);
checkValue<int>("\xD1\xCF\xC7", -12345);
}
SECTION("int 32") {
check<int>("\xD2\x00\x00\x00\x00", 0);
check<int>("\xD2\xFF\xFF\xFF\xFF", -1);
check<int>("\xD2\xB6\x69\xFD\x2E", -1234567890);
checkValue<int>("\xD2\x00\x00\x00\x00", 0);
checkValue<int>("\xD2\xFF\xFF\xFF\xFF", -1);
checkValue<int>("\xD2\xB6\x69\xFD\x2E", -1234567890);
}
SECTION("int 64") {
#if ARDUINOJSON_USE_LONG_LONG
check<int64_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", int64_t(0U));
check<int64_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
int64_t(0xFFFFFFFFFFFFFFFFU));
check<int64_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
int64_t(0x123456789ABCDEF0));
checkValue<int64_t>("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", int64_t(0U));
checkValue<int64_t>("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF",
int64_t(0xFFFFFFFFFFFFFFFFU));
checkValue<int64_t>("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0",
int64_t(0x123456789ABCDEF0));
#else
checkNotSupported("\xD3\x00\x00\x00\x00\x00\x00\x00\x00");
checkNotSupported("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF");
checkNotSupported("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0");
checkValue("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", nullptr);
checkValue("\xD3\xFF\xFF\xFF\xFF\xFF\xFF\xFF\xFF", nullptr);
checkValue("\xD3\x12\x34\x56\x78\x9A\xBC\xDE\xF0", nullptr);
#endif
}
SECTION("float 32") {
check<double>("\xCA\x00\x00\x00\x00", 0.0f);
check<double>("\xCA\x40\x48\xF5\xC3", 3.14f);
checkValue<double>("\xCA\x00\x00\x00\x00", 0.0f);
checkValue<double>("\xCA\x40\x48\xF5\xC3", 3.14f);
}
SECTION("float 64") {
check<double>("\xCB\x00\x00\x00\x00\x00\x00\x00\x00", 0.0);
check<double>("\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F", 3.1415);
checkValue<double>("\xCB\x00\x00\x00\x00\x00\x00\x00\x00", 0.0);
checkValue<double>("\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F", 3.1415);
}
SECTION("fixstr") {
check<const char*>("\xA0", std::string(""));
check<const char*>("\xABhello world", std::string("hello world"));
check<const char*>("\xBFhello world hello world hello !",
std::string("hello world hello world hello !"));
checkValue<std::string>("\xA0", std::string(""));
checkValue<std::string>("\xABhello world", std::string("hello world"));
checkValue<std::string>("\xBFhello world hello world hello !",
std::string("hello world hello world hello !"));
}
SECTION("str 8") {
check<const char*>("\xd9\x05hello", std::string("hello"));
checkValue<std::string>("\xd9\x05hello", std::string("hello"));
}
SECTION("str 16") {
check<const char*>("\xda\x00\x05hello", std::string("hello"));
checkValue<std::string>("\xda\x00\x05hello", std::string("hello"));
}
SECTION("str 32") {
check<const char*>("\xdb\x00\x00\x00\x05hello", std::string("hello"));
checkValue<std::string>("\xdb\x00\x00\x00\x05hello", std::string("hello"));
}
}
TEST_CASE("deserializeMsgPack() under memory constaints") {
SECTION("single values always fit") {
checkError(0, "\xc0", DeserializationError::Ok); // nil
checkError(0, "\xc2", DeserializationError::Ok); // false
checkError(0, "\xc3", DeserializationError::Ok); // true
checkError(0, "\xcc\x00", DeserializationError::Ok); // uint 8
checkError(0, "\xcd\x30\x39", DeserializationError::Ok); // uint 16
checkError(0, "\xCE\x12\x34\x56\x78",
DeserializationError::Ok); // uint 32
}
SECTION("fixstr") {
checkError(2, "\xA7ZZZZZZZ", DeserializationError::Ok);
checkError(0, "\xA7ZZZZZZZ", DeserializationError::NoMemory);
}
SECTION("str 8") {
checkError(2, "\xD9\x07ZZZZZZZ", DeserializationError::Ok);
checkError(0, "\xD9\x07ZZZZZZZ", DeserializationError::NoMemory);
}
SECTION("str 16") {
checkError(2, "\xDA\x00\x07ZZZZZZZ", DeserializationError::Ok);
checkError(0, "\xDA\x00\x07ZZZZZZZ", DeserializationError::NoMemory);
}
SECTION("str 32") {
checkError(2, "\xDB\x00\x00\x00\x07ZZZZZZZ", DeserializationError::Ok);
checkError(0, "\xDB\x00\x00\x00\x07ZZZZZZZ",
DeserializationError::NoMemory);
}
SECTION("fixarray") {
checkError(0, "\x90", DeserializationError::Ok); // []
checkError(0, "\x91\x01",
DeserializationError::NoMemory); // [1]
checkError(1, "\x91\x01",
DeserializationError::Ok); // [1]
}
SECTION("array 16") {
checkError(0, "\xDC\x00\x00", DeserializationError::Ok);
checkError(0, "\xDC\x00\x01\x01", DeserializationError::NoMemory);
checkError(1, "\xDC\x00\x01\x01", DeserializationError::Ok);
}
SECTION("array 32") {
checkError(0, "\xDD\x00\x00\x00\x00", DeserializationError::Ok);
checkError(0, "\xDD\x00\x00\x00\x01\x01", DeserializationError::NoMemory);
checkError(1, "\xDD\x00\x00\x00\x01\x01", DeserializationError::Ok);
}
SECTION("fixmap") {
SECTION("{}") {
checkError(0, "\x80", DeserializationError::Ok);
}
SECTION("{H:1}") {
checkError(0, "\x81\xA1H\x01", DeserializationError::NoMemory);
checkError(3, "\x81\xA1H\x01", DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
checkError(3, "\x82\xA1H\x01\xA1W\x02", DeserializationError::NoMemory);
checkError(5, "\x82\xA1H\x01\xA1W\x02", DeserializationError::Ok);
}
}
SECTION("map 16") {
SECTION("{}") {
checkError(0, "\xDE\x00\x00", DeserializationError::Ok);
}
SECTION("{H:1}") {
checkError(2, "\xDE\x00\x01\xA1H\x01", DeserializationError::NoMemory);
checkError(3, "\xDE\x00\x01\xA1H\x01", DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
checkError(3, "\xDE\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::NoMemory);
checkError(5, "\xDE\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::Ok);
}
}
SECTION("map 32") {
SECTION("{}") {
checkError(0, "\xDF\x00\x00\x00\x00", DeserializationError::Ok);
}
SECTION("{H:1}") {
checkError(2, "\xDF\x00\x00\x00\x01\xA1H\x01",
DeserializationError::NoMemory);
checkError(3, "\xDF\x00\x00\x00\x01\xA1H\x01", DeserializationError::Ok);
}
SECTION("{H:1,W:2}") {
checkError(3, "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::NoMemory);
checkError(5, "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::Ok);
}
}
}

View File

@@ -0,0 +1,108 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include "Allocators.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
TEST_CASE("deserializeMsgPack(JsonDocument&)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
spy.clearLog();
auto err = deserializeMsgPack(doc, "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[42]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofPool()),
Deallocate(sizeofString("hello")),
Allocate(sizeofPool()),
Reallocate(sizeofPool(), sizeofArray(1)),
});
}
TEST_CASE("deserializeMsgPack(JsonVariant)") {
SECTION("variant is bound") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
spy.clearLog();
JsonVariant variant = doc[0];
auto err = deserializeMsgPack(variant, "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("variant is unbound") {
JsonVariant variant;
auto err = deserializeMsgPack(variant, "\x91\x2A");
REQUIRE(err == DeserializationError::NoMemory);
}
}
TEST_CASE("deserializeMsgPack(ElementProxy)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add(std::string("hello"));
spy.clearLog();
SECTION("element already exists") {
auto err = deserializeMsgPack(doc[0], "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[[42]]");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("hello")),
});
}
SECTION("element must be created exists") {
auto err = deserializeMsgPack(doc[1], "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "[\"hello\",[42]]");
REQUIRE(spy.log() == AllocatorLog{});
}
}
TEST_CASE("deserializeMsgPack(MemberProxy)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc[std::string("hello")] = std::string("world");
spy.clearLog();
SECTION("member already exists") {
auto err = deserializeMsgPack(doc["hello"], "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":[42]}");
REQUIRE(spy.log() == AllocatorLog{
Deallocate(sizeofString("world")),
});
}
SECTION("member must be created") {
auto err = deserializeMsgPack(doc["value"], "\x91\x2A");
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.as<std::string>() == "{\"hello\":\"world\",\"value\":[42]}");
REQUIRE(spy.log() == AllocatorLog{});
}
}

View File

@@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>

View File

@@ -0,0 +1,265 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <sstream>
TEST_CASE("deserializeMsgPack() returns InvalidInput") {
JsonDocument doc;
SECTION("integer as key") {
auto err = deserializeMsgPack(doc, "\x81\x01\xA1H", 3);
REQUIRE(err == DeserializationError::InvalidInput);
}
}
TEST_CASE("deserializeMsgPack() returns EmptyInput") {
JsonDocument doc;
SECTION("from sized buffer") {
auto err = deserializeMsgPack(doc, "", 0);
REQUIRE(err == DeserializationError::EmptyInput);
}
SECTION("from stream") {
std::istringstream input("");
auto err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::EmptyInput);
}
}
static void testIncompleteInput(const char* input, size_t len) {
JsonDocument doc;
REQUIRE(deserializeMsgPack(doc, input, len) == DeserializationError::Ok);
while (--len) {
REQUIRE(deserializeMsgPack(doc, input, len) ==
DeserializationError::IncompleteInput);
}
}
TEST_CASE("deserializeMsgPack() returns IncompleteInput") {
SECTION("empty input") {
testIncompleteInput("\x00", 1);
}
SECTION("fixarray") {
testIncompleteInput("\x91\x01", 2);
}
SECTION("array 16") {
testIncompleteInput("\xDC\x00\x01\x01", 4);
}
SECTION("array 32") {
testIncompleteInput("\xDD\x00\x00\x00\x01\x01", 6);
}
SECTION("fixmap") {
testIncompleteInput("\x81\xA3one\x01", 6);
}
SECTION("map 16") {
testIncompleteInput("\xDE\x00\x01\xA3one\x01", 8);
}
SECTION("map 32") {
testIncompleteInput("\xDF\x00\x00\x00\x01\xA3one\x01", 10);
testIncompleteInput("\xDF\x00\x00\x00\x01\xd9\x03one\x01", 11);
}
SECTION("uint 8") {
testIncompleteInput("\xcc\x01", 2);
}
SECTION("uint 16") {
testIncompleteInput("\xcd\x00\x01", 3);
}
SECTION("uint 32") {
testIncompleteInput("\xCE\x00\x00\x00\x01", 5);
}
#if ARDUINOJSON_USE_LONG_LONG
SECTION("uint 64") {
testIncompleteInput("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 9);
}
#endif
SECTION("int 8") {
testIncompleteInput("\xD0\x01", 2);
}
SECTION("int 16") {
testIncompleteInput("\xD1\x00\x01", 3);
}
SECTION("int 32") {
testIncompleteInput("\xD2\x00\x00\x00\x01", 5);
}
#if ARDUINOJSON_USE_LONG_LONG
SECTION("int 64") {
testIncompleteInput("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 9);
}
#endif
SECTION("float 32") {
testIncompleteInput("\xCA\x40\x48\xF5\xC3", 5);
}
SECTION("float 64") {
testIncompleteInput("\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F", 9);
}
SECTION("fixstr") {
testIncompleteInput("\xABhello world", 12);
}
SECTION("str 8") {
testIncompleteInput("\xd9\x05hello", 7);
}
SECTION("str 16") {
testIncompleteInput("\xda\x00\x05hello", 8);
}
SECTION("str 32") {
testIncompleteInput("\xdb\x00\x00\x00\x05hello", 10);
}
SECTION("bin 8") {
testIncompleteInput("\xc4\x01X", 3);
}
SECTION("bin 16") {
testIncompleteInput("\xc5\x00\x01X", 4);
}
SECTION("bin 32") {
testIncompleteInput("\xc6\x00\x00\x00\x01X", 6);
}
SECTION("ext 8") {
testIncompleteInput("\xc7\x01\x01\x01", 4);
}
SECTION("ext 16") {
testIncompleteInput("\xc8\x00\x01\x01\x01", 5);
}
SECTION("ext 32") {
testIncompleteInput("\xc9\x00\x00\x00\x01\x01\x01", 7);
}
SECTION("fixext 1") {
testIncompleteInput("\xd4\x01\x01", 3);
}
SECTION("fixext 2") {
testIncompleteInput("\xd5\x01\x01\x02", 4);
}
SECTION("fixext 4") {
testIncompleteInput("\xd6\x01\x01\x02\x03\x04", 6);
}
SECTION("fixext 8") {
testIncompleteInput("\xd7\x01\x01\x02\x03\x04\x05\x06\x07\x08", 10);
}
SECTION("fixext 16") {
testIncompleteInput(
"\xd8\x01\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E"
"\x0F\x10",
18);
}
}
static std::string msgPackToJson(const char* input, size_t inputSize) {
JsonDocument doc;
auto err = deserializeMsgPack(doc, input, inputSize);
REQUIRE(err == DeserializationError::Ok);
return doc.as<std::string>();
}
TEST_CASE("deserializeMsgPack() replaces unsupported types by null") {
SECTION("bin 8") {
REQUIRE(msgPackToJson("\x92\xc4\x01X\x2A", 5) == "[null,42]");
}
SECTION("bin 16") {
REQUIRE(msgPackToJson("\x92\xc5\x00\x01X\x2A", 6) == "[null,42]");
}
SECTION("bin 32") {
REQUIRE(msgPackToJson("\x92\xc6\x00\x00\x00\x01X\x2A", 8) == "[null,42]");
}
SECTION("ext 8") {
REQUIRE(msgPackToJson("\x92\xc7\x01\x01\x01\x2A", 6) == "[null,42]");
}
SECTION("ext 16") {
REQUIRE(msgPackToJson("\x92\xc8\x00\x01\x01\x01\x2A", 7) == "[null,42]");
}
SECTION("ext 32") {
REQUIRE(msgPackToJson("\x92\xc9\x00\x00\x00\x01\x01\x01\x2A", 9) ==
"[null,42]");
}
SECTION("fixext 1") {
REQUIRE(msgPackToJson("\x92\xd4\x01\x01\x2A", 5) == "[null,42]");
}
SECTION("fixext 2") {
REQUIRE(msgPackToJson("\x92\xd5\x01\x01\x02\x2A", 6) == "[null,42]");
}
SECTION("fixext 4") {
REQUIRE(msgPackToJson("\x92\xd6\x01\x01\x02\x03\x04\x2A", 8) ==
"[null,42]");
}
SECTION("fixext 8") {
REQUIRE(msgPackToJson("\x92\xd7\x01\x01\x02\x03\x04\x05\x06\x07\x08\x2A",
12) == "[null,42]");
}
SECTION("fixext 16") {
REQUIRE(msgPackToJson("\x92\xd8\x01\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A"
"\x0B\x0C\x0D\x0E"
"\x0F\x10\x2A",
20) == "[null,42]");
}
}
TEST_CASE("deserializeMsgPack() returns NoMemory is string length overflows") {
JsonDocument doc;
auto maxLength = ArduinoJson::detail::StringNode::maxLength;
SECTION("max length should succeed") {
auto len = maxLength;
std::string prefix = {'\xdb', char(len >> 24), char(len >> 16),
char(len >> 8), char(len)};
auto err = deserializeMsgPack(doc, prefix + std::string(len, 'a'));
REQUIRE(err == DeserializationError::Ok);
}
SECTION("one above max length should fail") {
auto len = maxLength + 1;
std::string prefix = {'\xdb', char(len >> 24), char(len >> 16),
char(len >> 8), char(len)};
auto err = deserializeMsgPack(doc, prefix + std::string(len, 'a'));
REQUIRE(err == DeserializationError::NoMemory);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -1,158 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
DeserializationError deserialize(const char* input, size_t len) {
DynamicJsonDocument doc(4096);
return deserializeMsgPack(doc, input, len);
}
void checkAllSizes(const char* input, size_t len) {
REQUIRE(deserialize(input, len) == DeserializationError::Ok);
while (--len) {
REQUIRE(deserialize(input, len) == DeserializationError::IncompleteInput);
}
}
TEST_CASE("deserializeMsgPack() returns IncompleteInput") {
SECTION("empty input") {
checkAllSizes("\x00", 1);
}
SECTION("fixarray") {
checkAllSizes("\x91\x01", 2);
}
SECTION("array 16") {
checkAllSizes("\xDC\x00\x01\x01", 4);
}
SECTION("array 32") {
checkAllSizes("\xDD\x00\x00\x00\x01\x01", 6);
}
SECTION("fixmap") {
checkAllSizes("\x81\xA3one\x01", 6);
}
SECTION("map 16") {
checkAllSizes("\xDE\x00\x01\xA3one\x01", 8);
}
SECTION("map 32") {
checkAllSizes("\xDF\x00\x00\x00\x01\xA3one\x01", 10);
checkAllSizes("\xDF\x00\x00\x00\x01\xd9\x03one\x01", 11);
}
SECTION("uint 8") {
checkAllSizes("\xcc\x01", 2);
}
SECTION("uint 16") {
checkAllSizes("\xcd\x00\x01", 3);
}
SECTION("uint 32") {
checkAllSizes("\xCE\x00\x00\x00\x01", 5);
}
#if ARDUINOJSON_USE_LONG_LONG
SECTION("uint 64") {
checkAllSizes("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 9);
}
#endif
SECTION("int 8") {
checkAllSizes("\xD0\x01", 2);
}
SECTION("int 16") {
checkAllSizes("\xD1\x00\x01", 3);
}
SECTION("int 32") {
checkAllSizes("\xD2\x00\x00\x00\x01", 5);
}
#if ARDUINOJSON_USE_LONG_LONG
SECTION("int 64") {
checkAllSizes("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 9);
}
#endif
SECTION("float 32") {
checkAllSizes("\xCA\x40\x48\xF5\xC3", 5);
}
SECTION("float 64") {
checkAllSizes("\xCB\x40\x09\x21\xCA\xC0\x83\x12\x6F", 9);
}
SECTION("fixstr") {
checkAllSizes("\xABhello world", 12);
}
SECTION("str 8") {
checkAllSizes("\xd9\x05hello", 7);
}
SECTION("str 16") {
checkAllSizes("\xda\x00\x05hello", 8);
}
SECTION("str 32") {
checkAllSizes("\xdb\x00\x00\x00\x05hello", 10);
}
SECTION("bin 8") {
checkAllSizes("\xc4\x01X", 3);
}
SECTION("bin 16") {
checkAllSizes("\xc5\x00\x01X", 4);
}
SECTION("bin 32") {
checkAllSizes("\xc6\x00\x00\x00\x01X", 6);
}
SECTION("ext 8") {
checkAllSizes("\xc7\x01\x01\x01", 4);
}
SECTION("ext 16") {
checkAllSizes("\xc8\x00\x01\x01\x01", 5);
}
SECTION("ext 32") {
checkAllSizes("\xc9\x00\x00\x00\x01\x01\x01", 7);
}
SECTION("fixext 1") {
checkAllSizes("\xd4\x01\x01", 3);
}
SECTION("fixext 2") {
checkAllSizes("\xd5\x01\x01\x02", 4);
}
SECTION("fixext 4") {
checkAllSizes("\xd6\x01\x01\x02\x03\x04", 6);
}
SECTION("fixext 8") {
checkAllSizes("\xd7\x01\x01\x02\x03\x04\x05\x06\x07\x08", 10);
}
SECTION("fixext 16") {
checkAllSizes(
"\xd8\x01\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E"
"\x0F\x10",
18);
}
}

View File

@@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
@@ -7,8 +7,10 @@
#include "CustomReader.hpp"
using ArduinoJson::detail::sizeofObject;
TEST_CASE("deserializeMsgPack(const std::string&)") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
SECTION("should accept const string") {
const std::string input("\x92\x01\x02");
@@ -48,7 +50,7 @@ TEST_CASE("deserializeMsgPack(const std::string&)") {
}
TEST_CASE("deserializeMsgPack(std::istream&)") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
SECTION("should accept a zero in input") {
std::istringstream input(std::string("\x92\x00\x02", 3));
@@ -76,7 +78,7 @@ TEST_CASE("deserializeMsgPack(VLA)") {
char vla[i];
memcpy(vla, "\xDE\x00\x01\xA5Hello\xA5world", 15);
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
JsonDocument doc;
DeserializationError err = deserializeMsgPack(doc, vla);
REQUIRE(err == DeserializationError::Ok);
@@ -84,7 +86,7 @@ TEST_CASE("deserializeMsgPack(VLA)") {
#endif
TEST_CASE("deserializeMsgPack(CustomReader)") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
CustomReader reader("\x92\xA5Hello\xA5world");
DeserializationError err = deserializeMsgPack(doc, reader);

View File

@@ -1,26 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <sstream>
TEST_CASE("deserializeMsgPack() returns EmptyInput") {
StaticJsonDocument<100> doc;
SECTION("from sized buffer") {
DeserializationError err = deserializeMsgPack(doc, "", 0);
REQUIRE(err == DeserializationError::EmptyInput);
}
SECTION("from stream") {
std::istringstream input("");
DeserializationError err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::EmptyInput);
}
}

View File

@@ -1,5 +1,5 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// Copyright © 2014-2024, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
@@ -12,7 +12,7 @@
REQUIRE(DeserializationError::TooDeep == expression);
TEST_CASE("JsonDeserializer nesting") {
DynamicJsonDocument doc(4096);
JsonDocument doc;
SECTION("Input = const char*") {
SECTION("limit = 0") {

View File

@@ -1,82 +0,0 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2023, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
static void checkMsgPackDocument(const char* input, size_t inputSize,
const char* expectedJson) {
DynamicJsonDocument doc(4096);
DeserializationError error = deserializeMsgPack(doc, input, inputSize);
REQUIRE(error == DeserializationError::Ok);
std::string actualJson;
serializeJson(doc, actualJson);
REQUIRE(actualJson == expectedJson);
}
static void checkMsgPackError(const char* input, size_t inputSize,
DeserializationError expectedError) {
DynamicJsonDocument doc(4096);
DeserializationError error = deserializeMsgPack(doc, input, inputSize);
REQUIRE(error == expectedError);
}
TEST_CASE("deserializeMsgPack() return NotSupported") {
SECTION("bin 8") {
checkMsgPackDocument("\x92\xc4\x01X\x2A", 5, "[null,42]");
}
SECTION("bin 16") {
checkMsgPackDocument("\x92\xc5\x00\x01X\x2A", 6, "[null,42]");
}
SECTION("bin 32") {
checkMsgPackDocument("\x92\xc6\x00\x00\x00\x01X\x2A", 8, "[null,42]");
}
SECTION("ext 8") {
checkMsgPackDocument("\x92\xc7\x01\x01\x01\x2A", 6, "[null,42]");
}
SECTION("ext 16") {
checkMsgPackDocument("\x92\xc8\x00\x01\x01\x01\x2A", 7, "[null,42]");
}
SECTION("ext 32") {
checkMsgPackDocument("\x92\xc9\x00\x00\x00\x01\x01\x01\x2A", 9,
"[null,42]");
}
SECTION("fixext 1") {
checkMsgPackDocument("\x92\xd4\x01\x01\x2A", 5, "[null,42]");
}
SECTION("fixext 2") {
checkMsgPackDocument("\x92\xd5\x01\x01\x02\x2A", 6, "[null,42]");
}
SECTION("fixext 4") {
checkMsgPackDocument("\x92\xd6\x01\x01\x02\x03\x04\x2A", 8, "[null,42]");
}
SECTION("fixext 8") {
checkMsgPackDocument("\x92\xd7\x01\x01\x02\x03\x04\x05\x06\x07\x08\x2A", 12,
"[null,42]");
}
SECTION("fixext 16") {
checkMsgPackDocument(
"\x92\xd8\x01\x01\x02\x03\x04\x05\x06\x07\x08\x09\x0A\x0B\x0C\x0D\x0E"
"\x0F\x10\x2A",
20, "[null,42]");
}
SECTION("integer as key") {
checkMsgPackError("\x81\x01\xA1H", 3, DeserializationError::InvalidInput);
}
}