forked from bblanchon/ArduinoJson
Merged MsgPackError and JsonError into DeserializationError.
Return NotSupported if the JSON input contains "\u".
This commit is contained in:
@ -3,7 +3,6 @@
|
||||
# MIT License
|
||||
|
||||
add_executable(MsgPackTests
|
||||
MsgPackError.cpp
|
||||
deserializeArray.cpp
|
||||
deserializeObject.cpp
|
||||
deserializeStaticVariant.cpp
|
||||
|
@ -1,44 +0,0 @@
|
||||
// ArduinoJson - arduinojson.org
|
||||
// Copyright Benoit Blanchon 2014-2018
|
||||
// MIT License
|
||||
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
void testStringification(MsgPackError error, std::string expected) {
|
||||
REQUIRE(error.c_str() == expected);
|
||||
}
|
||||
|
||||
void testBoolification(MsgPackError error, bool expected) {
|
||||
CHECK(error == expected);
|
||||
}
|
||||
|
||||
#define TEST_STRINGIFICATION(symbol) \
|
||||
testStringification(MsgPackError::symbol, #symbol)
|
||||
|
||||
#define TEST_BOOLIFICATION(symbol, expected) \
|
||||
testBoolification(MsgPackError::symbol, expected)
|
||||
|
||||
TEST_CASE("MsgPackError") {
|
||||
SECTION("c_str()") {
|
||||
TEST_STRINGIFICATION(Ok);
|
||||
TEST_STRINGIFICATION(NotSupported);
|
||||
TEST_STRINGIFICATION(NoMemory);
|
||||
TEST_STRINGIFICATION(TooDeep);
|
||||
TEST_STRINGIFICATION(IncompleteInput);
|
||||
}
|
||||
|
||||
SECTION("as boolean") {
|
||||
TEST_BOOLIFICATION(Ok, false);
|
||||
TEST_BOOLIFICATION(NotSupported, true);
|
||||
TEST_BOOLIFICATION(NoMemory, true);
|
||||
TEST_BOOLIFICATION(TooDeep, true);
|
||||
TEST_BOOLIFICATION(IncompleteInput, true);
|
||||
}
|
||||
|
||||
SECTION("ostream") {
|
||||
std::stringstream s;
|
||||
s << MsgPackError::NotSupported;
|
||||
REQUIRE(s.str() == "NotSupported");
|
||||
}
|
||||
}
|
@ -12,20 +12,20 @@ TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\x90";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two integers") {
|
||||
const char* input = "\x92\x01\x02";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
REQUIRE(array[0] == 1);
|
||||
REQUIRE(array[1] == 2);
|
||||
@ -36,20 +36,20 @@ TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDC\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
}
|
||||
|
||||
SECTION("two strings") {
|
||||
const char* input = "\xDC\x00\x02\xA5hello\xA5world";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
REQUIRE(array[0] == "hello");
|
||||
REQUIRE(array[1] == "world");
|
||||
@ -60,10 +60,10 @@ TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDD\x00\x00\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 0);
|
||||
}
|
||||
|
||||
@ -71,10 +71,10 @@ TEST_CASE("deserializeMsgPack(JsonArray&)") {
|
||||
const char* input =
|
||||
"\xDD\x00\x00\x00\x02\xCA\x00\x00\x00\x00\xCA\x40\x48\xF5\xC3";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(array.size() == 2);
|
||||
REQUIRE(array[0] == 0.0f);
|
||||
REQUIRE(array[1] == 3.14f);
|
||||
|
@ -12,10 +12,10 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\x80";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
@ -23,10 +23,10 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
SECTION("two integers") {
|
||||
const char* input = "\x82\xA3one\x01\xA3two\x02";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["one"] == 1);
|
||||
@ -38,10 +38,10 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDE\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
@ -49,10 +49,10 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
SECTION("two strings") {
|
||||
const char* input = "\xDE\x00\x02\xA1H\xA5hello\xA1W\xA5world";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["H"] == "hello");
|
||||
@ -64,10 +64,10 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
SECTION("empty") {
|
||||
const char* input = "\xDF\x00\x00\x00\x00";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 0);
|
||||
}
|
||||
@ -77,10 +77,10 @@ TEST_CASE("deserialize MsgPack object") {
|
||||
"\xDF\x00\x00\x00\x02\xA4zero\xCA\x00\x00\x00\x00\xA2pi\xCA\x40\x48"
|
||||
"\xF5\xC3";
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
JsonObject& obj = doc.as<JsonObject>();
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(doc.is<JsonObject>());
|
||||
REQUIRE(obj.size() == 2);
|
||||
REQUIRE(obj["zero"] == 0.0f);
|
||||
|
@ -8,124 +8,133 @@
|
||||
static const size_t epsilon = sizeof(void*);
|
||||
|
||||
template <size_t Capacity>
|
||||
static void check(const char* input, MsgPackError expected) {
|
||||
static void check(const char* input, DeserializationError expected) {
|
||||
StaticJsonDocument<Capacity> variant;
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
DeserializationError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack(StaticJsonDocument&)") {
|
||||
SECTION("single values always fit") {
|
||||
check<0>("\xc0", MsgPackError::Ok); // nil
|
||||
check<0>("\xc2", MsgPackError::Ok); // false
|
||||
check<0>("\xc3", MsgPackError::Ok); // true
|
||||
check<0>("\xcc\x00", MsgPackError::Ok); // uint 8
|
||||
check<0>("\xcd\x30\x39", MsgPackError::Ok); // uint 16
|
||||
check<0>("\xCE\x12\x34\x56\x78", MsgPackError::Ok); // uint 32
|
||||
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") {
|
||||
check<0>("\xA0", MsgPackError::Ok);
|
||||
check<0>("\xA1H", MsgPackError::NoMemory);
|
||||
check<4>("\xA1H", MsgPackError::Ok);
|
||||
check<4>("\xA5Hello", MsgPackError::NoMemory);
|
||||
check<0>("\xA0", DeserializationError::Ok);
|
||||
check<0>("\xA1H", DeserializationError::NoMemory);
|
||||
check<4>("\xA1H", DeserializationError::Ok);
|
||||
check<4>("\xA5Hello", DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("str 8") {
|
||||
check<0>("\xD9\x00", MsgPackError::Ok);
|
||||
check<0>("\xD9\x01H", MsgPackError::NoMemory);
|
||||
check<4>("\xD9\x01H", MsgPackError::Ok);
|
||||
check<4>("\xD9\x05Hello", MsgPackError::NoMemory);
|
||||
check<0>("\xD9\x00", DeserializationError::Ok);
|
||||
check<0>("\xD9\x01H", DeserializationError::NoMemory);
|
||||
check<4>("\xD9\x01H", DeserializationError::Ok);
|
||||
check<4>("\xD9\x05Hello", DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("str 16") {
|
||||
check<0>("\xDA\x00\x00", MsgPackError::Ok);
|
||||
check<0>("\xDA\x00\x01H", MsgPackError::NoMemory);
|
||||
check<4>("\xDA\x00\x01H", MsgPackError::Ok);
|
||||
check<4>("\xDA\x00\x05Hello", MsgPackError::NoMemory);
|
||||
check<0>("\xDA\x00\x00", DeserializationError::Ok);
|
||||
check<0>("\xDA\x00\x01H", DeserializationError::NoMemory);
|
||||
check<4>("\xDA\x00\x01H", DeserializationError::Ok);
|
||||
check<4>("\xDA\x00\x05Hello", DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("str 32") {
|
||||
check<0>("\xDB\x00\x00\x00\x00", MsgPackError::Ok);
|
||||
check<0>("\xDB\x00\x00\x00\x01H", MsgPackError::NoMemory);
|
||||
check<4>("\xDB\x00\x00\x00\x01H", MsgPackError::Ok);
|
||||
check<4>("\xDB\x00\x00\x00\x05Hello", MsgPackError::NoMemory);
|
||||
check<0>("\xDB\x00\x00\x00\x00", DeserializationError::Ok);
|
||||
check<0>("\xDB\x00\x00\x00\x01H", DeserializationError::NoMemory);
|
||||
check<4>("\xDB\x00\x00\x00\x01H", DeserializationError::Ok);
|
||||
check<4>("\xDB\x00\x00\x00\x05Hello", DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("fixarray") {
|
||||
check<JSON_ARRAY_SIZE(0)>("\x90", MsgPackError::Ok); // []
|
||||
check<JSON_ARRAY_SIZE(0)>("\x91\x01", MsgPackError::NoMemory); // [1]
|
||||
check<JSON_ARRAY_SIZE(1)>("\x91\x01", MsgPackError::Ok); // [1]
|
||||
check<JSON_ARRAY_SIZE(1)>("\x92\x01\x02", MsgPackError::NoMemory); // [1,2]
|
||||
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", MsgPackError::Ok);
|
||||
check<JSON_ARRAY_SIZE(0)>("\xDC\x00\x01\x01", MsgPackError::NoMemory);
|
||||
check<JSON_ARRAY_SIZE(1)>("\xDC\x00\x01\x01", MsgPackError::Ok);
|
||||
check<JSON_ARRAY_SIZE(1)>("\xDC\x00\x02\x01\x02", MsgPackError::NoMemory);
|
||||
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", MsgPackError::Ok);
|
||||
check<JSON_ARRAY_SIZE(0)>("\xDD\x00\x00\x00\x00", DeserializationError::Ok);
|
||||
check<JSON_ARRAY_SIZE(0)>("\xDD\x00\x00\x00\x01\x01",
|
||||
MsgPackError::NoMemory);
|
||||
check<JSON_ARRAY_SIZE(1)>("\xDD\x00\x00\x00\x01\x01", MsgPackError::Ok);
|
||||
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",
|
||||
MsgPackError::NoMemory);
|
||||
DeserializationError::NoMemory);
|
||||
}
|
||||
|
||||
SECTION("fixmap") {
|
||||
SECTION("{}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\x80", MsgPackError::Ok);
|
||||
check<JSON_OBJECT_SIZE(0)>("\x80", DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{H:1}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\x81\xA1H\x01", MsgPackError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\x81\xA1H\x01", MsgPackError::Ok);
|
||||
check<JSON_OBJECT_SIZE(0)>("\x81\xA1H\x01",
|
||||
DeserializationError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\x81\xA1H\x01",
|
||||
DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\x82\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::NoMemory);
|
||||
DeserializationError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2 * epsilon>("\x82\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::Ok);
|
||||
DeserializationError::Ok);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("map 16") {
|
||||
SECTION("{}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x00", MsgPackError::Ok);
|
||||
check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x00", DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{H:1}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\xDE\x00\x01\xA1H\x01",
|
||||
MsgPackError::NoMemory);
|
||||
DeserializationError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDE\x00\x01\xA1H\x01",
|
||||
MsgPackError::Ok);
|
||||
DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDE\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::NoMemory);
|
||||
DeserializationError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2 * epsilon>("\xDE\x00\x02\xA1H\x01\xA1W\x02",
|
||||
MsgPackError::Ok);
|
||||
DeserializationError::Ok);
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("map 32") {
|
||||
SECTION("{}") {
|
||||
check<JSON_OBJECT_SIZE(0)>("\xDF\x00\x00\x00\x00", MsgPackError::Ok);
|
||||
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",
|
||||
MsgPackError::NoMemory);
|
||||
DeserializationError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>("\xDF\x00\x00\x00\x01\xA1H\x01",
|
||||
MsgPackError::Ok);
|
||||
DeserializationError::Ok);
|
||||
}
|
||||
SECTION("{H:1,W:2}") {
|
||||
check<JSON_OBJECT_SIZE(1) + epsilon>(
|
||||
"\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02", MsgPackError::NoMemory);
|
||||
"\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02",
|
||||
DeserializationError::NoMemory);
|
||||
check<JSON_OBJECT_SIZE(2) + 2 * epsilon>(
|
||||
"\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02", MsgPackError::Ok);
|
||||
"\xDF\x00\x00\x00\x02\xA1H\x01\xA1W\x02", DeserializationError::Ok);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -9,9 +9,9 @@ template <typename T, typename U>
|
||||
static void check(const char* input, U expected) {
|
||||
DynamicJsonDocument variant;
|
||||
|
||||
MsgPackError error = deserializeMsgPack(variant, input);
|
||||
DeserializationError error = deserializeMsgPack(variant, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::Ok);
|
||||
REQUIRE(error == DeserializationError::Ok);
|
||||
REQUIRE(variant.is<T>());
|
||||
REQUIRE(variant.as<T>() == expected);
|
||||
}
|
||||
|
@ -5,17 +5,17 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
MsgPackError deserialize(const char* input, size_t len) {
|
||||
DeserializationError deserialize(const char* input, size_t len) {
|
||||
DynamicJsonDocument doc;
|
||||
|
||||
return deserializeMsgPack(doc, input, len);
|
||||
}
|
||||
|
||||
void checkAllSizes(const char* input, size_t len) {
|
||||
REQUIRE(deserialize(input, len) == MsgPackError::Ok);
|
||||
REQUIRE(deserialize(input, len) == DeserializationError::Ok);
|
||||
|
||||
while (--len) {
|
||||
REQUIRE(deserialize(input, len) == MsgPackError::IncompleteInput);
|
||||
REQUIRE(deserialize(input, len) == DeserializationError::IncompleteInput);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -5,27 +5,28 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(const char* input, MsgPackError expected, uint8_t limit) {
|
||||
static void check(const char* input, DeserializationError expected,
|
||||
uint8_t limit) {
|
||||
DynamicJsonDocument doc;
|
||||
doc.nestingLimit = limit;
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(error == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("Errors returned by deserializeMsgPack()") {
|
||||
SECTION("object too deep") {
|
||||
check("\x80", MsgPackError::TooDeep, 0); // {}
|
||||
check("\x80", MsgPackError::Ok, 1); // {}
|
||||
check("\x81\xA1H\x80", MsgPackError::TooDeep, 1); // {H:{}}
|
||||
check("\x81\xA1H\x80", MsgPackError::Ok, 2); // {H:{}}
|
||||
check("\x80", DeserializationError::TooDeep, 0); // {}
|
||||
check("\x80", DeserializationError::Ok, 1); // {}
|
||||
check("\x81\xA1H\x80", DeserializationError::TooDeep, 1); // {H:{}}
|
||||
check("\x81\xA1H\x80", DeserializationError::Ok, 2); // {H:{}}
|
||||
}
|
||||
|
||||
SECTION("array too deep") {
|
||||
check("\x90", MsgPackError::TooDeep, 0); // []
|
||||
check("\x90", MsgPackError::Ok, 1); // []
|
||||
check("\x91\x90", MsgPackError::TooDeep, 1); // [[]]
|
||||
check("\x91\x90", MsgPackError::Ok, 2); // [[]]
|
||||
check("\x90", DeserializationError::TooDeep, 0); // []
|
||||
check("\x90", DeserializationError::Ok, 1); // []
|
||||
check("\x91\x90", DeserializationError::TooDeep, 1); // [[]]
|
||||
check("\x91\x90", DeserializationError::Ok, 2); // [[]]
|
||||
}
|
||||
}
|
||||
|
@ -8,9 +8,9 @@
|
||||
static void checkNotSupported(const char* input) {
|
||||
DynamicJsonDocument doc;
|
||||
|
||||
MsgPackError error = deserializeMsgPack(doc, input);
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(error == MsgPackError::NotSupported);
|
||||
REQUIRE(error == DeserializationError::NotSupported);
|
||||
}
|
||||
|
||||
TEST_CASE("deserializeMsgPack() return NotSupported") {
|
||||
|
@ -11,9 +11,9 @@ TEST_CASE("deserializeMsgPack(std::istream&)") {
|
||||
SECTION("should accept a zero in input") {
|
||||
std::istringstream input(std::string("\x92\x00\x02", 3));
|
||||
|
||||
MsgPackError err = deserializeMsgPack(doc, input);
|
||||
DeserializationError err = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(err == MsgPackError::Ok);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
REQUIRE(arr[0] == 0);
|
||||
REQUIRE(arr[1] == 2);
|
||||
@ -22,8 +22,8 @@ TEST_CASE("deserializeMsgPack(std::istream&)") {
|
||||
SECTION("should detect incomplete input") {
|
||||
std::istringstream input("\x92\x00\x02");
|
||||
|
||||
MsgPackError err = deserializeMsgPack(doc, input);
|
||||
DeserializationError err = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(err == MsgPackError::IncompleteInput);
|
||||
REQUIRE(err == DeserializationError::IncompleteInput);
|
||||
}
|
||||
}
|
||||
|
@ -11,32 +11,34 @@ TEST_CASE("deserializeMsgPack(const std::string&)") {
|
||||
SECTION("should accept const string") {
|
||||
const std::string input("\x92\x01\x02");
|
||||
|
||||
MsgPackError err = deserializeMsgPack(doc, input);
|
||||
DeserializationError err = deserializeMsgPack(doc, input);
|
||||
|
||||
REQUIRE(err == MsgPackError::Ok);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("should accept temporary string") {
|
||||
MsgPackError err = deserializeMsgPack(doc, std::string("\x92\x01\x02"));
|
||||
DeserializationError err =
|
||||
deserializeMsgPack(doc, std::string("\x92\x01\x02"));
|
||||
|
||||
REQUIRE(err == MsgPackError::Ok);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
}
|
||||
|
||||
SECTION("should duplicate content") {
|
||||
std::string input("\x91\xA5hello");
|
||||
|
||||
MsgPackError err = deserializeMsgPack(doc, input);
|
||||
DeserializationError err = deserializeMsgPack(doc, input);
|
||||
input[2] = 'X'; // alter the string tomake sure we made a copy
|
||||
|
||||
JsonArray& array = doc.as<JsonArray>();
|
||||
REQUIRE(err == MsgPackError::Ok);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
REQUIRE(std::string("hello") == array[0]);
|
||||
}
|
||||
|
||||
SECTION("should accept a zero in input") {
|
||||
MsgPackError err = deserializeMsgPack(doc, std::string("\x92\x00\x02", 3));
|
||||
DeserializationError err =
|
||||
deserializeMsgPack(doc, std::string("\x92\x00\x02", 3));
|
||||
|
||||
REQUIRE(err == MsgPackError::Ok);
|
||||
REQUIRE(err == DeserializationError::Ok);
|
||||
JsonArray& arr = doc.as<JsonArray>();
|
||||
REQUIRE(arr[0] == 0);
|
||||
REQUIRE(arr[1] == 2);
|
||||
|
Reference in New Issue
Block a user