mirror of
https://github.com/bblanchon/ArduinoJson.git
synced 2025-07-29 10:17:39 +02:00
Moved ancillary files to extras/
(fixes #1011)
This commit is contained in:
18
extras/tests/MsgPackDeserializer/CMakeLists.txt
Normal file
18
extras/tests/MsgPackDeserializer/CMakeLists.txt
Normal file
@ -0,0 +1,18 @@
|
||||
# ArduinoJson - arduinojson.org
|
||||
# Copyright Benoit Blanchon 2014-2019
|
||||
# MIT License
|
||||
|
||||
add_executable(MsgPackDeserializerTests
|
||||
deserializeArray.cpp
|
||||
deserializeObject.cpp
|
||||
deserializeStaticVariant.cpp
|
||||
deserializeVariant.cpp
|
||||
doubleToFloat.cpp
|
||||
incompleteInput.cpp
|
||||
input_types.cpp
|
||||
nestingLimit.cpp
|
||||
notSupported.cpp
|
||||
)
|
||||
|
||||
target_link_libraries(MsgPackDeserializerTests catch)
|
||||
add_test(MsgPackDeserializer MsgPackDeserializerTests)
|
83
extras/tests/MsgPackDeserializer/deserializeArray.cpp
Normal file
83
extras/tests/MsgPackDeserializer/deserializeArray.cpp
Normal file
@ -0,0 +1,83 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserialize MsgPack array") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
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("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);
|
||||
}
|
||||
}
|
||||
}
|
130
extras/tests/MsgPackDeserializer/deserializeObject.cpp
Normal file
130
extras/tests/MsgPackDeserializer/deserializeObject.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserialize MsgPack object") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
||||
}
|
152
extras/tests/MsgPackDeserializer/deserializeStaticVariant.cpp
Normal file
152
extras/tests/MsgPackDeserializer/deserializeStaticVariant.cpp
Normal file
@ -0,0 +1,152 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
template <size_t Capacity>
|
||||
static void check(const char* input, DeserializationError expected) {
|
||||
StaticJsonDocument<Capacity> variant;
|
||||
|
||||
DeserializationError error = deserializeMsgPack(variant, input);
|
||||
|
||||
CAPTURE(input);
|
||||
REQUIRE(error == expected);
|
||||
}
|
||||
|
||||
template <size_t Size>
|
||||
static void checkString(const char* input, DeserializationError expected) {
|
||||
check<JSON_STRING_SIZE(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);
|
||||
}
|
||||
}
|
||||
}
|
146
extras/tests/MsgPackDeserializer/deserializeVariant.cpp
Normal file
146
extras/tests/MsgPackDeserializer/deserializeVariant.cpp
Normal file
@ -0,0 +1,146 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
template <typename T, typename U>
|
||||
static void check(const char* input, U expected) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<T>());
|
||||
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::NotSupported);
|
||||
}
|
||||
#endif
|
||||
|
||||
static void checkIsNull(const char* input) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.as<JsonVariant>().isNull());
|
||||
}
|
||||
|
||||
TEST_CASE("deserialize MsgPack value") {
|
||||
SECTION("nil") {
|
||||
checkIsNull("\xc0");
|
||||
}
|
||||
|
||||
SECTION("bool") {
|
||||
check<bool>("\xc2", false);
|
||||
check<bool>("\xc3", true);
|
||||
}
|
||||
|
||||
SECTION("positive fixint") {
|
||||
check<int>("\x00", 0);
|
||||
check<int>("\x7F", 127);
|
||||
}
|
||||
|
||||
SECTION("negative fixint") {
|
||||
check<int>("\xe0", -32);
|
||||
check<int>("\xff", -1);
|
||||
}
|
||||
|
||||
SECTION("uint 8") {
|
||||
check<int>("\xcc\x00", 0);
|
||||
check<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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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);
|
||||
#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");
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("int 8") {
|
||||
check<int>("\xd0\x00", 0);
|
||||
check<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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
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));
|
||||
#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");
|
||||
#endif
|
||||
}
|
||||
|
||||
SECTION("float 32") {
|
||||
check<double>("\xCA\x00\x00\x00\x00", 0.0f);
|
||||
check<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);
|
||||
}
|
||||
|
||||
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 !"));
|
||||
}
|
||||
|
||||
SECTION("str 8") {
|
||||
check<const char*>("\xd9\x05hello", std::string("hello"));
|
||||
}
|
||||
|
||||
SECTION("str 16") {
|
||||
check<const char*>("\xda\x00\x05hello", std::string("hello"));
|
||||
}
|
||||
|
||||
SECTION("str 32") {
|
||||
check<const char*>("\xdb\x00\x00\x00\x05hello", std::string("hello"));
|
||||
}
|
||||
}
|
25
extras/tests/MsgPackDeserializer/doubleToFloat.cpp
Normal file
25
extras/tests/MsgPackDeserializer/doubleToFloat.cpp
Normal file
@ -0,0 +1,25 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
using namespace ARDUINOJSON_NAMESPACE;
|
||||
|
||||
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);
|
||||
fixEndianess(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);
|
||||
}
|
110
extras/tests/MsgPackDeserializer/incompleteInput.cpp
Normal file
110
extras/tests/MsgPackDeserializer/incompleteInput.cpp
Normal file
@ -0,0 +1,110 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// 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);
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
}
|
82
extras/tests/MsgPackDeserializer/input_types.cpp
Normal file
82
extras/tests/MsgPackDeserializer/input_types.cpp
Normal file
@ -0,0 +1,82 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
TEST_CASE("deserializeMsgPack(const std::string&)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
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, std::string("\x92\x01\x02"));
|
||||
|
||||
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(std::string("hello") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("should accept a zero in input") {
|
||||
DeserializationError err =
|
||||
deserializeMsgPack(doc, std::string("\x92\x00\x02", 3));
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
JsonArray arr = doc.as<JsonArray>();
|
||||
REQUIRE(arr[0] == 0);
|
||||
REQUIRE(arr[1] == 2);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack(std::istream&)") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("should accept a zero in input") {
|
||||
std::istringstream input(std::string("\x92\x00\x02", 3));
|
||||
|
||||
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)") {
|
||||
int i = 16;
|
||||
char vla[i];
|
||||
memcpy(vla, "\xDE\x00\x01\xA5Hello\xA5world", 15);
|
||||
|
||||
StaticJsonDocument<JSON_OBJECT_SIZE(1)> doc;
|
||||
DeserializationError err = deserializeMsgPack(doc, vla);
|
||||
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
#endif
|
85
extras/tests/MsgPackDeserializer/nestingLimit.cpp
Normal file
85
extras/tests/MsgPackDeserializer/nestingLimit.cpp
Normal file
@ -0,0 +1,85 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
#define SHOULD_WORK(expression) REQUIRE(DeserializationError::Ok == expression);
|
||||
#define SHOULD_FAIL(expression) \
|
||||
REQUIRE(DeserializationError::TooDeep == expression);
|
||||
|
||||
TEST_CASE("JsonDeserializer nesting") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
73
extras/tests/MsgPackDeserializer/notSupported.cpp
Normal file
73
extras/tests/MsgPackDeserializer/notSupported.cpp
Normal file
@ -0,0 +1,73 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2019
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void checkNotSupported(const char* input) {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(error == DeserializationError::NotSupported);
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack() return NotSupported") {
|
||||
SECTION("bin 8") {
|
||||
checkNotSupported("\xc4");
|
||||
}
|
||||
|
||||
SECTION("bin 16") {
|
||||
checkNotSupported("\xc5");
|
||||
}
|
||||
|
||||
SECTION("bin 32") {
|
||||
checkNotSupported("\xc6");
|
||||
}
|
||||
|
||||
SECTION("ext 8") {
|
||||
checkNotSupported("\xc7");
|
||||
}
|
||||
|
||||
SECTION("ext 16") {
|
||||
checkNotSupported("\xc8");
|
||||
}
|
||||
|
||||
SECTION("ext 32") {
|
||||
checkNotSupported("\xc9");
|
||||
}
|
||||
|
||||
SECTION("fixext 1") {
|
||||
checkNotSupported("\xd4");
|
||||
}
|
||||
|
||||
SECTION("fixext 2") {
|
||||
checkNotSupported("\xd5");
|
||||
}
|
||||
|
||||
SECTION("fixext 4") {
|
||||
checkNotSupported("\xd6");
|
||||
}
|
||||
|
||||
SECTION("fixext 8") {
|
||||
checkNotSupported("\xd7");
|
||||
}
|
||||
|
||||
SECTION("fixext 16") {
|
||||
checkNotSupported("\xd8");
|
||||
}
|
||||
|
||||
SECTION("unsupported in array") {
|
||||
checkNotSupported("\x91\xc4");
|
||||
}
|
||||
|
||||
SECTION("unsupported in map") {
|
||||
checkNotSupported("\x81\xc4\x00\xA1H");
|
||||
checkNotSupported("\x81\xA1H\xc4\x00");
|
||||
}
|
||||
|
||||
SECTION("integer as key") {
|
||||
checkNotSupported("\x81\x01\xA1H");
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user