forked from bblanchon/ArduinoJson
Replaced JsonDocument::nestingLimit with a param to deserializeJson()
This commit is contained in:
@ -5,28 +5,81 @@
|
||||
#include <ArduinoJson.h>
|
||||
#include <catch.hpp>
|
||||
|
||||
static void check(const char* input, DeserializationError expected,
|
||||
uint8_t limit) {
|
||||
#define SHOULD_WORK(expression) REQUIRE(DeserializationError::Ok == expression);
|
||||
#define SHOULD_FAIL(expression) \
|
||||
REQUIRE(DeserializationError::TooDeep == expression);
|
||||
|
||||
TEST_CASE("JsonDeserializer nesting") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
doc.nestingLimit = limit;
|
||||
|
||||
DeserializationError error = deserializeMsgPack(doc, input);
|
||||
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)); // {}
|
||||
}
|
||||
|
||||
REQUIRE(error == expected);
|
||||
}
|
||||
|
||||
TEST_CASE("Errors returned by deserializeMsgPack()") {
|
||||
SECTION("object too deep") {
|
||||
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("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("array too deep") {
|
||||
check("\x90", DeserializationError::TooDeep, 0); // []
|
||||
check("\x90", DeserializationError::Ok, 1); // []
|
||||
check("\x91\x90", DeserializationError::TooDeep, 1); // [[]]
|
||||
check("\x91\x90", DeserializationError::Ok, 2); // [[]]
|
||||
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));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user