Initial commit of Arduino libraries

This commit is contained in:
Sam
2025-05-23 10:47:41 +10:00
commit 5bfce5fc3e
2476 changed files with 1108481 additions and 0 deletions

View File

@@ -0,0 +1,22 @@
# ArduinoJson - https://arduinojson.org
# Copyright © 2014-2025, Benoit BLANCHON
# MIT License
add_executable(MsgPackDeserializerTests
deserializeArray.cpp
deserializeObject.cpp
deserializeVariant.cpp
destination_types.cpp
doubleToFloat.cpp
errors.cpp
filter.cpp
input_types.cpp
nestingLimit.cpp
)
add_test(MsgPackDeserializer MsgPackDeserializerTests)
set_tests_properties(MsgPackDeserializer
PROPERTIES
LABELS "Catch"
)

View File

@@ -0,0 +1,104 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
TEST_CASE("deserialize MsgPack array") {
SpyingAllocator spy;
JsonDocument doc(&spy);
SECTION("fixarray") {
SECTION("empty") {
const char* input = "\x90";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 0);
}
SECTION("two integers") {
const char* input = "\x92\x01\x02";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 2);
REQUIRE(array[0] == 1);
REQUIRE(array[1] == 2);
}
SECTION("tiny strings") {
DeserializationError error =
deserializeMsgPack(doc, "\x92\xA3xxx\xA3yyy");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonArray>());
REQUIRE(doc.size() == 2);
REQUIRE(doc[0] == "xxx");
REQUIRE(doc[1] == "yyy");
REQUIRE(spy.log() == AllocatorLog{
Allocate(sizeofPool()),
Allocate(sizeofString("xxx")),
// Buffer is reused for the next string
Deallocate(sizeofString("xxx")),
Reallocate(sizeofPool(), sizeofPool(2)),
});
}
}
SECTION("array 16") {
SECTION("empty") {
const char* input = "\xDC\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 0);
}
SECTION("two strings") {
const char* input = "\xDC\x00\x02\xA5hello\xA5world";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 2);
REQUIRE(array[0] == "hello");
REQUIRE(array[1] == "world");
}
}
SECTION("array 32") {
SECTION("empty") {
const char* input = "\xDD\x00\x00\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 0);
}
SECTION("two floats") {
const char* input =
"\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3";
DeserializationError error = deserializeMsgPack(doc, input);
JsonArray array = doc.as<JsonArray>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(array.size() == 2);
REQUIRE(array[0] == 0.0f);
REQUIRE(array[1] == 3.14f);
}
}
}

View File

@@ -0,0 +1,130 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("deserialize MsgPack object") {
JsonDocument doc;
SECTION("fixmap") {
SECTION("empty") {
const char* input = "\x80";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0);
}
SECTION("two integers") {
const char* input = "\x82\xA3one\x01\xA3two\x02";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["one"] == 1);
REQUIRE(obj["two"] == 2);
}
SECTION("key is str 8") {
const char* input = "\x82\xd9\x03one\x01\xd9\x03two\x02";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["one"] == 1);
REQUIRE(obj["two"] == 2);
}
SECTION("key is str 16") {
const char* input = "\x82\xda\x00\x03one\x01\xda\x00\x03two\x02";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["one"] == 1);
REQUIRE(obj["two"] == 2);
}
SECTION("key is str 32") {
const char* input =
"\x82\xdb\x00\x00\x00\x03one\x01\xdb\x00\x00\x00\x03two\x02";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["one"] == 1);
REQUIRE(obj["two"] == 2);
}
}
SECTION("map 16") {
SECTION("empty") {
const char* input = "\xDE\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0);
}
SECTION("two strings") {
const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["H"] == "hello");
REQUIRE(obj["W"] == "world");
}
}
SECTION("map 32") {
SECTION("empty") {
const char* input = "\xDF\x00\x00\x00\x00";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 0);
}
SECTION("two floats") {
const char* input =
"\xDF\x00\x00\x00\x02\xA4zero\xCA\x00\x00\x00\x00\xA2pi\xCA\x40\x48"
"\xF5\xC3";
DeserializationError error = deserializeMsgPack(doc, input);
JsonObject obj = doc.as<JsonObject>();
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<JsonObject>());
REQUIRE(obj.size() == 2);
REQUIRE(obj["zero"] == 0.0f);
REQUIRE(obj["pi"] == 3.14f);
}
}
}

View File

@@ -0,0 +1,395 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "Allocators.hpp"
#include "Literals.hpp"
template <typename T>
static void checkValue(const char* input, T expected) {
JsonDocument doc;
DeserializationError error = deserializeMsgPack(doc, input);
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<T>());
REQUIRE(doc.as<T>() == expected);
}
static void checkError(size_t timebombCountDown, const char* input,
DeserializationError expected) {
TimebombAllocator timebomb(timebombCountDown);
JsonDocument doc(&timebomb);
DeserializationError error = deserializeMsgPack(doc, input);
CAPTURE(input);
REQUIRE(error == expected);
}
TEST_CASE("deserialize MsgPack value") {
SECTION("nil") {
checkValue("\xc0", nullptr);
}
SECTION("bool") {
checkValue<bool>("\xc2", false);
checkValue<bool>("\xc3", true);
}
SECTION("positive fixint") {
checkValue<int>("\x00", 0);
checkValue<int>("\x7F", 127);
}
SECTION("negative fixint") {
checkValue<int>("\xe0", -32);
checkValue<int>("\xff", -1);
}
SECTION("uint 8") {
checkValue<int>("\xcc\x00", 0);
checkValue<int>("\xcc\xff", 255);
}
SECTION("uint 16") {
checkValue<int>("\xcd\x00\x00", 0);
checkValue<int>("\xcd\xFF\xFF", 65535);
checkValue<int>("\xcd\x30\x39", 12345);
}
SECTION("uint 32") {
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
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
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") {
checkValue<int>("\xd0\x00", 0);
checkValue<int>("\xd0\xff", -1);
}
SECTION("int 16") {
checkValue<int>("\xD1\x00\x00", 0);
checkValue<int>("\xD1\xFF\xFF", -1);
checkValue<int>("\xD1\xCF\xC7", -12345);
}
SECTION("int 32") {
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
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
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") {
checkValue<double>("\xCA\x00\x00\x00\x00", 0.0f);
checkValue<double>("\xCA\x40\x48\xF5\xC3", 3.14f);
}
SECTION("float 64") {
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") {
checkValue<std::string>("\xA0", std::string(""));
checkValue<std::string>("\xABhello world", "hello world"_s);
checkValue<std::string>("\xBFhello world hello world hello !",
"hello world hello world hello !"_s);
}
SECTION("str 8") {
checkValue<std::string>("\xd9\x05hello", "hello"_s);
}
SECTION("str 16") {
checkValue<std::string>("\xda\x00\x05hello", "hello"_s);
}
SECTION("str 32") {
checkValue<std::string>("\xdb\x00\x00\x00\x05hello", "hello"_s);
}
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 = "\xc5\x01\x00"_s + 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);
}
SECTION("fixext 1") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xd4\x01\x02");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 1);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 2);
}
SECTION("fixext 2") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xd5\x01\x02\x03");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 2);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 2);
REQUIRE(data[1] == 3);
}
SECTION("fixext 4") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xd6\x01\x02\x03\x04\x05");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 4);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 2);
REQUIRE(data[1] == 3);
REQUIRE(data[2] == 4);
REQUIRE(data[3] == 5);
}
SECTION("fixext 8") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xd7\x01????????");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 8);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == '?');
REQUIRE(data[7] == '?');
}
SECTION("fixext 16") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xd8\x01?????????????????");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 16);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == '?');
REQUIRE(data[15] == '?');
}
SECTION("ext 8") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xc7\x02\x01\x03\x04");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 2);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 3);
REQUIRE(data[1] == 4);
}
SECTION("ext 16") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xc8\x00\x02\x01\x03\x04");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 2);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 3);
REQUIRE(data[1] == 4);
}
SECTION("ext 32") {
JsonDocument doc;
auto error = deserializeMsgPack(doc, "\xc9\x00\x00\x00\x02\x01\x03\x04");
REQUIRE(error == DeserializationError::Ok);
REQUIRE(doc.is<MsgPackExtension>());
auto ext = doc.as<MsgPackExtension>();
REQUIRE(ext.type() == 1);
REQUIRE(ext.size() == 2);
auto data = reinterpret_cast<const uint8_t*>(ext.data());
REQUIRE(data[0] == 3);
REQUIRE(data[1] == 4);
}
}
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("{Hello:1}") {
checkError(1, "\x81\xA5Hello\x01", DeserializationError::NoMemory);
checkError(2, "\x81\xA5Hello\x01", DeserializationError::Ok);
}
SECTION("{Hello:1,World:2}") {
checkError(2, "\x82\xA5Hello\x01\xA5World\x02",
DeserializationError::NoMemory);
checkError(3, "\x82\xA5Hello\x01\xA5World\x02", DeserializationError::Ok);
}
}
SECTION("map 16") {
SECTION("{}") {
checkError(0, "\xDE\x00\x00", DeserializationError::Ok);
}
SECTION("{Hello:1}") {
checkError(1, "\xDE\x00\x01\xA5Hello\x01",
DeserializationError::NoMemory);
checkError(2, "\xDE\x00\x01\xA5Hello\x01", DeserializationError::Ok);
}
SECTION("{Hello:1,World:2}") {
checkError(2, "\xDE\x00\x02\xA5Hello\x01\xA5World\x02",
DeserializationError::NoMemory);
checkError(3, "\xDE\x00\x02\xA5Hello\x01\xA5World\x02",
DeserializationError::Ok);
}
}
SECTION("map 32") {
SECTION("{}") {
checkError(0, "\xDF\x00\x00\x00\x00", DeserializationError::Ok);
}
SECTION("{H:1}") {
checkError(1, "\xDF\x00\x00\x00\x01\xA1H\x01",
DeserializationError::NoMemory);
checkError(2, "\xDF\x00\x00\x00\x01\xA1H\x01", DeserializationError::Ok);
}
SECTION("{Hello:1,World:2}") {
checkError(2, "\xDF\x00\x00\x00\x02\xA5Hello\x01\xA5World\x02",
DeserializationError::NoMemory);
checkError(3, "\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
DeserializationError::Ok);
}
}
}

View File

@@ -0,0 +1,109 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <string>
#include "Allocators.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofArray;
using ArduinoJson::detail::sizeofObject;
TEST_CASE("deserializeMsgPack(JsonDocument&)") {
SpyingAllocator spy;
JsonDocument doc(&spy);
doc.add("hello"_s);
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("hello"_s);
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("hello"_s);
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["hello"_s] = "world"_s;
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

@@ -0,0 +1,25 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
using namespace ArduinoJson::detail;
template <typename T>
static void check(const char* input, T expected) {
T actual;
uint8_t* f = reinterpret_cast<uint8_t*>(&actual);
const uint8_t* d = reinterpret_cast<const uint8_t*>(input);
doubleToFloat(d, f);
fixEndianness(actual);
CHECK(actual == expected);
}
TEST_CASE("doubleToFloat()") {
check("\x40\x09\x21\xCA\xC0\x83\x12\x6F", 3.1415f);
check("\x00\x00\x00\x00\x00\x00\x00\x00", 0.0f);
check("\x80\x00\x00\x00\x00\x00\x00\x00", -0.0f);
check("\xC0\x5E\xDC\xCC\xCC\xCC\xCC\xCD", -123.45f);
}

View File

@@ -0,0 +1,242 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <sstream>
#include "Allocators.hpp"
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);
}
}
TEST_CASE(
"deserializeMsgPack() returns NoMemory when string allocation fails") {
TimebombAllocator allocator(0);
JsonDocument doc(&allocator);
SECTION("fixstr") {
DeserializationError err = deserializeMsgPack(doc, "\xA5hello", 9);
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("bin 8") {
DeserializationError err = deserializeMsgPack(doc, "\xC4\x01X", 3);
REQUIRE(err == DeserializationError::NoMemory);
}
}
TEST_CASE(
"deserializeMsgPack() returns NoMemory if extension allocation fails") {
JsonDocument doc(FailingAllocator::instance());
SECTION("uint32_t should pass") {
auto err = deserializeMsgPack(doc, "\xceXXXX");
REQUIRE(err == DeserializationError::Ok);
}
SECTION("uint64_t should fail") {
auto err = deserializeMsgPack(doc, "\xcfXXXXXXXX");
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("int32_t should pass") {
auto err = deserializeMsgPack(doc, "\xd2XXXX");
REQUIRE(err == DeserializationError::Ok);
}
SECTION("int64_t should fail") {
auto err = deserializeMsgPack(doc, "\xd3XXXXXXXX");
REQUIRE(err == DeserializationError::NoMemory);
}
SECTION("float should pass") {
auto err = deserializeMsgPack(doc, "\xcaXXXX");
REQUIRE(err == DeserializationError::Ok);
}
SECTION("double should fail") {
auto err = deserializeMsgPack(doc, "\xcbXXXXXXXX");
REQUIRE(err == DeserializationError::NoMemory);
}
}

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,96 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include "CustomReader.hpp"
#include "Literals.hpp"
using ArduinoJson::detail::sizeofObject;
TEST_CASE("deserializeMsgPack(const std::string&)") {
JsonDocument doc;
SECTION("should accept const string") {
const std::string input("\x92\x01\x02");
DeserializationError err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("should accept temporary string") {
DeserializationError err = deserializeMsgPack(doc, "\x92\x01\x02"_s);
REQUIRE(err == DeserializationError::Ok);
}
SECTION("should duplicate content") {
std::string input("\x91\xA5hello");
DeserializationError err = deserializeMsgPack(doc, input);
input[2] = 'X'; // alter the string tomake sure we made a copy
JsonArray array = doc.as<JsonArray>();
REQUIRE(err == DeserializationError::Ok);
REQUIRE("hello"_s == array[0]);
}
SECTION("should accept a zero in input") {
DeserializationError err = deserializeMsgPack(doc, "\x92\x00\x02"_s);
REQUIRE(err == DeserializationError::Ok);
JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr[0] == 0);
REQUIRE(arr[1] == 2);
}
}
TEST_CASE("deserializeMsgPack(std::istream&)") {
JsonDocument doc;
SECTION("should accept a zero in input") {
std::istringstream input("\x92\x00\x02"_s);
DeserializationError err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::Ok);
JsonArray arr = doc.as<JsonArray>();
REQUIRE(arr[0] == 0);
REQUIRE(arr[1] == 2);
}
SECTION("should detect incomplete input") {
std::istringstream input("\x92\x00\x02");
DeserializationError err = deserializeMsgPack(doc, input);
REQUIRE(err == DeserializationError::IncompleteInput);
}
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
TEST_CASE("deserializeMsgPack(VLA)") {
size_t i = 16;
char vla[i];
memcpy(vla, "\xDE\x00\x01\xA5Hello\xA5world", 15);
JsonDocument doc;
DeserializationError err = deserializeMsgPack(doc, vla);
REQUIRE(err == DeserializationError::Ok);
}
#endif
TEST_CASE("deserializeMsgPack(CustomReader)") {
JsonDocument doc;
CustomReader reader("\x92\xA5Hello\xA5world");
DeserializationError err = deserializeMsgPack(doc, reader);
REQUIRE(err == DeserializationError::Ok);
REQUIRE(doc.size() == 2);
REQUIRE(doc[0] == "Hello");
REQUIRE(doc[1] == "world");
}

View File

@@ -0,0 +1,87 @@
// ArduinoJson - https://arduinojson.org
// Copyright © 2014-2025, Benoit BLANCHON
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
#include <sstream>
#define SHOULD_WORK(expression) REQUIRE(DeserializationError::Ok == expression);
#define SHOULD_FAIL(expression) \
REQUIRE(DeserializationError::TooDeep == expression);
TEST_CASE("JsonDeserializer nesting") {
JsonDocument doc;
SECTION("Input = const char*") {
SECTION("limit = 0") {
DeserializationOption::NestingLimit nesting(0);
SHOULD_WORK(deserializeMsgPack(doc, "\xA1H", nesting)); // "H"
SHOULD_FAIL(deserializeMsgPack(doc, "\x90", nesting)); // []
SHOULD_FAIL(deserializeMsgPack(doc, "\x80", nesting)); // {}
}
SECTION("limit = 1") {
DeserializationOption::NestingLimit nesting(1);
SHOULD_WORK(deserializeMsgPack(doc, "\x90", nesting)); // {}
SHOULD_WORK(deserializeMsgPack(doc, "\x80", nesting)); // []
SHOULD_FAIL(deserializeMsgPack(doc, "\x81\xA1H\x80", nesting)); // {H:{}}
SHOULD_FAIL(deserializeMsgPack(doc, "\x91\x90", nesting)); // [[]]
}
}
SECTION("char* and size_t") {
SECTION("limit = 0") {
DeserializationOption::NestingLimit nesting(0);
SHOULD_WORK(deserializeMsgPack(doc, "\xA1H", 2, nesting));
SHOULD_FAIL(deserializeMsgPack(doc, "\x90", 1, nesting));
SHOULD_FAIL(deserializeMsgPack(doc, "\x80", 1, nesting));
}
SECTION("limit = 1") {
DeserializationOption::NestingLimit nesting(1);
SHOULD_WORK(deserializeMsgPack(doc, "\x90", 1, nesting));
SHOULD_WORK(deserializeMsgPack(doc, "\x80", 1, nesting));
SHOULD_FAIL(deserializeMsgPack(doc, "\x81\xA1H\x80", 4, nesting));
SHOULD_FAIL(deserializeMsgPack(doc, "\x91\x90", 2, nesting));
}
}
SECTION("Input = std::string") {
using std::string;
SECTION("limit = 0") {
DeserializationOption::NestingLimit nesting(0);
SHOULD_WORK(deserializeMsgPack(doc, string("\xA1H"), nesting));
SHOULD_FAIL(deserializeMsgPack(doc, string("\x90"), nesting));
SHOULD_FAIL(deserializeMsgPack(doc, string("\x80"), nesting));
}
SECTION("limit = 1") {
DeserializationOption::NestingLimit nesting(1);
SHOULD_WORK(deserializeMsgPack(doc, string("\x90"), nesting));
SHOULD_WORK(deserializeMsgPack(doc, string("\x80"), nesting));
SHOULD_FAIL(deserializeMsgPack(doc, string("\x81\xA1H\x80"), nesting));
SHOULD_FAIL(deserializeMsgPack(doc, string("\x91\x90"), nesting));
}
}
SECTION("Input = std::istream") {
SECTION("limit = 0") {
DeserializationOption::NestingLimit nesting(0);
std::istringstream good("\xA1H"); // "H"
std::istringstream bad("\x90"); // []
SHOULD_WORK(deserializeMsgPack(doc, good, nesting));
SHOULD_FAIL(deserializeMsgPack(doc, bad, nesting));
}
SECTION("limit = 1") {
DeserializationOption::NestingLimit nesting(1);
std::istringstream good("\x90"); // []
std::istringstream bad("\x91\x90"); // [[]]
SHOULD_WORK(deserializeMsgPack(doc, good, nesting));
SHOULD_FAIL(deserializeMsgPack(doc, bad, nesting));
}
}
}