forked from bblanchon/ArduinoJson
Replaced JsonDocument::nestingLimit with a param to deserializeJson()
This commit is contained in:
@ -9,27 +9,93 @@
|
||||
#define SHOULD_FAIL(expression) \
|
||||
REQUIRE(DeserializationError::TooDeep == expression);
|
||||
|
||||
TEST_CASE("JsonDeserializer nestingLimit") {
|
||||
TEST_CASE("JsonDeserializer nesting") {
|
||||
DynamicJsonDocument doc(4096);
|
||||
|
||||
SECTION("limit = 0") {
|
||||
doc.nestingLimit = 0;
|
||||
SHOULD_WORK(deserializeJson(doc, "\"toto\""));
|
||||
SHOULD_WORK(deserializeJson(doc, "123"));
|
||||
SHOULD_WORK(deserializeJson(doc, "true"));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[]"));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{}"));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[\"toto\"]"));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":1}"));
|
||||
SECTION("Input = const char*") {
|
||||
SECTION("limit = 0") {
|
||||
DeserializationOption::NestingLimit nesting(0);
|
||||
SHOULD_WORK(deserializeJson(doc, "\"toto\"", nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "123", nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "true", nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[]", nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{}", nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[\"toto\"]", nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":1}", nesting));
|
||||
}
|
||||
|
||||
SECTION("limit = 1") {
|
||||
DeserializationOption::NestingLimit nesting(1);
|
||||
SHOULD_WORK(deserializeJson(doc, "[\"toto\"]", nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "{\"toto\":1}", nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":{}}", nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":[]}", nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[[\"toto\"]]", nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[{\"toto\":1}]", nesting));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("limit = 1") {
|
||||
doc.nestingLimit = 1;
|
||||
SHOULD_WORK(deserializeJson(doc, "[\"toto\"]"));
|
||||
SHOULD_WORK(deserializeJson(doc, "{\"toto\":1}"));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":{}}"));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":[]}"));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[[\"toto\"]]"));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[{\"toto\":1}]"));
|
||||
SECTION("char* and size_t") {
|
||||
SECTION("limit = 0") {
|
||||
DeserializationOption::NestingLimit nesting(0);
|
||||
SHOULD_WORK(deserializeJson(doc, "\"toto\"", 6, nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "123", 3, nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "true", 4, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[]", 2, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{}", 2, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[\"toto\"]", 8, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":1}", 10, nesting));
|
||||
}
|
||||
|
||||
SECTION("limit = 1") {
|
||||
DeserializationOption::NestingLimit nesting(1);
|
||||
SHOULD_WORK(deserializeJson(doc, "[\"toto\"]", 8, nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, "{\"toto\":1}", 10, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":{}}", 11, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "{\"toto\":[]}", 11, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[[\"toto\"]]", 10, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, "[{\"toto\":1}]", 12, nesting));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Input = std::string") {
|
||||
SECTION("limit = 0") {
|
||||
DeserializationOption::NestingLimit nesting(0);
|
||||
SHOULD_WORK(deserializeJson(doc, std::string("\"toto\""), nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, std::string("123"), nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, std::string("true"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("[]"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("{}"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("[\"toto\"]"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":1}"), nesting));
|
||||
}
|
||||
|
||||
SECTION("limit = 1") {
|
||||
DeserializationOption::NestingLimit nesting(1);
|
||||
SHOULD_WORK(deserializeJson(doc, std::string("[\"toto\"]"), nesting));
|
||||
SHOULD_WORK(deserializeJson(doc, std::string("{\"toto\":1}"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":{}}"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":[]}"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("[[\"toto\"]]"), nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, std::string("[{\"toto\":1}]"), nesting));
|
||||
}
|
||||
}
|
||||
|
||||
SECTION("Input = std::istream") {
|
||||
SECTION("limit = 0") {
|
||||
DeserializationOption::NestingLimit nesting(0);
|
||||
std::istringstream good("true");
|
||||
std::istringstream bad("[]");
|
||||
SHOULD_WORK(deserializeJson(doc, good, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, bad, nesting));
|
||||
}
|
||||
|
||||
SECTION("limit = 1") {
|
||||
DeserializationOption::NestingLimit nesting(1);
|
||||
std::istringstream good("[\"toto\"]");
|
||||
std::istringstream bad("{\"toto\":{}}");
|
||||
SHOULD_WORK(deserializeJson(doc, good, nesting));
|
||||
SHOULD_FAIL(deserializeJson(doc, bad, nesting));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -78,7 +78,6 @@ TEST_CASE("DynamicJsonDocument copies") {
|
||||
SECTION("Copy constructor") {
|
||||
DynamicJsonDocument doc1(1234);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
doc1.nestingLimit = 42;
|
||||
|
||||
DynamicJsonDocument doc2 = doc1;
|
||||
|
||||
@ -86,14 +85,12 @@ TEST_CASE("DynamicJsonDocument copies") {
|
||||
serializeJson(doc2, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
|
||||
REQUIRE(doc2.nestingLimit == 42);
|
||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||
}
|
||||
|
||||
SECTION("Copy assignment preserves the buffer when capacity is sufficient") {
|
||||
DynamicJsonDocument doc1(1234);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
doc1.nestingLimit = 42;
|
||||
|
||||
DynamicJsonDocument doc2(doc1.capacity());
|
||||
doc2 = doc1;
|
||||
@ -101,14 +98,12 @@ TEST_CASE("DynamicJsonDocument copies") {
|
||||
std::string json;
|
||||
serializeJson(doc2, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.nestingLimit == 42);
|
||||
REQUIRE(doc2.capacity() == doc1.capacity());
|
||||
}
|
||||
|
||||
SECTION("Copy assignment realloc the buffer when capacity is insufficient") {
|
||||
DynamicJsonDocument doc1(1234);
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
doc1.nestingLimit = 42;
|
||||
DynamicJsonDocument doc2(8);
|
||||
|
||||
REQUIRE(doc2.capacity() < doc1.memoryUsage());
|
||||
@ -118,20 +113,17 @@ TEST_CASE("DynamicJsonDocument copies") {
|
||||
std::string json;
|
||||
serializeJson(doc2, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.nestingLimit == 42);
|
||||
}
|
||||
|
||||
SECTION("Construct from StaticJsonDocument") {
|
||||
StaticJsonDocument<200> sdoc;
|
||||
deserializeJson(sdoc, "{\"hello\":\"world\"}");
|
||||
sdoc.nestingLimit = 42;
|
||||
|
||||
DynamicJsonDocument ddoc = sdoc;
|
||||
|
||||
std::string json;
|
||||
serializeJson(ddoc, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(ddoc.nestingLimit == 42);
|
||||
REQUIRE(ddoc.capacity() == sdoc.capacity());
|
||||
}
|
||||
|
||||
@ -141,13 +133,11 @@ TEST_CASE("DynamicJsonDocument copies") {
|
||||
|
||||
StaticJsonDocument<200> sdoc;
|
||||
deserializeJson(sdoc, "{\"hello\":\"world\"}");
|
||||
sdoc.nestingLimit = 42;
|
||||
|
||||
ddoc = sdoc;
|
||||
|
||||
std::string json;
|
||||
serializeJson(ddoc, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(ddoc.nestingLimit == 42);
|
||||
}
|
||||
}
|
||||
|
@ -33,27 +33,23 @@ TEST_CASE("StaticJsonDocument") {
|
||||
StaticJsonDocument<200> doc1, doc2;
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc2, "{\"hello\":\"world\"}");
|
||||
doc2.nestingLimit = 42;
|
||||
|
||||
doc1 = doc2;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc1, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc1.nestingLimit == 42);
|
||||
}
|
||||
|
||||
SECTION("Copy constructor") {
|
||||
StaticJsonDocument<200> doc1;
|
||||
deserializeJson(doc1, "{\"hello\":\"world\"}");
|
||||
doc1.nestingLimit = 42;
|
||||
|
||||
StaticJsonDocument<200> doc2 = doc1;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc2, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc2.nestingLimit == 42);
|
||||
}
|
||||
|
||||
SECTION("Assign from StaticJsonDocument of different capacity") {
|
||||
@ -61,14 +57,12 @@ TEST_CASE("StaticJsonDocument") {
|
||||
StaticJsonDocument<300> doc2;
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc2, "{\"hello\":\"world\"}");
|
||||
doc2.nestingLimit = 42;
|
||||
|
||||
doc1 = doc2;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc1, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc1.nestingLimit == 42);
|
||||
}
|
||||
|
||||
SECTION("Assign from DynamicJsonDocument") {
|
||||
@ -76,39 +70,33 @@ TEST_CASE("StaticJsonDocument") {
|
||||
DynamicJsonDocument doc2(4096);
|
||||
doc1.to<JsonVariant>().set(666);
|
||||
deserializeJson(doc2, "{\"hello\":\"world\"}");
|
||||
doc2.nestingLimit = 42;
|
||||
|
||||
doc1 = doc2;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc1, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc1.nestingLimit == 42);
|
||||
}
|
||||
|
||||
SECTION("Construct from StaticJsonDocument of different size") {
|
||||
StaticJsonDocument<300> doc2;
|
||||
deserializeJson(doc2, "{\"hello\":\"world\"}");
|
||||
doc2.nestingLimit = 42;
|
||||
|
||||
StaticJsonDocument<200> doc1 = doc2;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc1, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc1.nestingLimit == 42);
|
||||
}
|
||||
|
||||
SECTION("Construct from DynamicJsonDocument") {
|
||||
DynamicJsonDocument doc2(4096);
|
||||
deserializeJson(doc2, "{\"hello\":\"world\"}");
|
||||
doc2.nestingLimit = 42;
|
||||
|
||||
StaticJsonDocument<200> doc1 = doc2;
|
||||
|
||||
std::string json;
|
||||
serializeJson(doc1, json);
|
||||
REQUIRE(json == "{\"hello\":\"world\"}");
|
||||
REQUIRE(doc1.nestingLimit == 42);
|
||||
}
|
||||
}
|
||||
|
@ -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