Added support for non zero-terminated strings (fixes #704)

This commit is contained in:
Benoit Blanchon
2018-05-14 17:12:59 +02:00
parent 4c9c047ddf
commit ccb54136a2
54 changed files with 2234 additions and 1401 deletions

View File

@ -3,13 +3,17 @@
# MIT License
add_executable(MsgPackTests
deserializationErrors.cpp
MsgPackError.cpp
deserializeArray.cpp
deserializeObject.cpp
deserializeVariant.cpp
deserializeStaticVariant.cpp
deserializeVariant.cpp
doubleToFloat.cpp
MsgPackError.cpp
incompleteInput.cpp
nestingLimit.cpp
notSupported.cpp
std_string.cpp
std_istream.cpp
)
target_link_libraries(MsgPackTests catch)

View File

@ -25,6 +25,7 @@ TEST_CASE("MsgPackError") {
TEST_STRINGIFICATION(NotSupported);
TEST_STRINGIFICATION(NoMemory);
TEST_STRINGIFICATION(TooDeep);
TEST_STRINGIFICATION(IncompleteInput);
}
SECTION("as boolean") {
@ -32,6 +33,7 @@ TEST_CASE("MsgPackError") {
TEST_BOOLIFICATION(NotSupported, true);
TEST_BOOLIFICATION(NoMemory, true);
TEST_BOOLIFICATION(TooDeep, true);
TEST_BOOLIFICATION(IncompleteInput, true);
}
SECTION("ostream") {

View File

@ -1,59 +0,0 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
static void check(const char* input, MsgPackError expected,
uint8_t nestingLimit = 10) {
DynamicJsonDocument doc;
doc.nestingLimit = nestingLimit;
MsgPackError error = deserializeMsgPack(doc, input);
REQUIRE(error == expected);
}
TEST_CASE("Errors returned by deserializeMsgPack()") {
SECTION("unsupported") {
check("\xc4", MsgPackError::NotSupported); // bin 8
check("\xc5", MsgPackError::NotSupported); // bin 16
check("\xc6", MsgPackError::NotSupported); // bin 32
check("\xc7", MsgPackError::NotSupported); // ext 8
check("\xc8", MsgPackError::NotSupported); // ext 16
check("\xc9", MsgPackError::NotSupported); // ext 32
check("\xd4", MsgPackError::NotSupported); // fixext 1
check("\xd5", MsgPackError::NotSupported); // fixext 2
check("\xd6", MsgPackError::NotSupported); // fixext 4
check("\xd7", MsgPackError::NotSupported); // fixext 8
check("\xd8", MsgPackError::NotSupported); // fixext 16
}
SECTION("unsupported in array") {
check("\x91\xc4", MsgPackError::NotSupported);
}
SECTION("unsupported in map") {
check("\x81\xc4\x00\xA1H", MsgPackError::NotSupported);
check("\x81\xA1H\xc4\x00", MsgPackError::NotSupported);
}
SECTION("integer as key") {
check("\x81\x01\xA1H", MsgPackError::NotSupported);
}
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:{}}
}
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); // [[]]
}
}

View File

@ -0,0 +1,106 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
MsgPackError 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);
while (--len) {
REQUIRE(deserialize(input, len) == MsgPackError::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);
}
SECTION("uint 64") {
checkAllSizes("\xCF\x00\x00\x00\x00\x00\x00\x00\x00", 9);
}
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);
}
SECTION("int 64") {
checkAllSizes("\xD3\x00\x00\x00\x00\x00\x00\x00\x00", 9);
}
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);
}
}

View File

@ -0,0 +1,31 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
static void check(const char* input, MsgPackError expected, uint8_t limit) {
DynamicJsonDocument doc;
doc.nestingLimit = limit;
MsgPackError 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:{}}
}
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); // [[]]
}
}

View File

@ -0,0 +1,73 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
static void checkNotSupported(const char* input) {
DynamicJsonDocument doc;
MsgPackError error = deserializeMsgPack(doc, input);
REQUIRE(error == MsgPackError::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");
}
}

View File

@ -0,0 +1,29 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("deserializeMsgPack(std::istream&)") {
DynamicJsonDocument doc;
SECTION("should accept a zero in input") {
std::istringstream input(std::string("\x92\x00\x02", 3));
MsgPackError err = deserializeMsgPack(doc, input);
REQUIRE(err == MsgPackError::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");
MsgPackError err = deserializeMsgPack(doc, input);
REQUIRE(err == MsgPackError::IncompleteInput);
}
}

View File

@ -0,0 +1,44 @@
// ArduinoJson - arduinojson.org
// Copyright Benoit Blanchon 2014-2018
// MIT License
#include <ArduinoJson.h>
#include <catch.hpp>
TEST_CASE("deserializeMsgPack(const std::string&)") {
DynamicJsonDocument doc;
SECTION("should accept const string") {
const std::string input("\x92\x01\x02");
MsgPackError err = deserializeMsgPack(doc, input);
REQUIRE(err == MsgPackError::Ok);
}
SECTION("should accept temporary string") {
MsgPackError err = deserializeMsgPack(doc, std::string("\x92\x01\x02"));
REQUIRE(err == MsgPackError::Ok);
}
SECTION("should duplicate content") {
std::string input("\x91\xA5hello");
MsgPackError 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(std::string("hello") == array[0]);
}
SECTION("should accept a zero in input") {
MsgPackError err = deserializeMsgPack(doc, std::string("\x92\x00\x02", 3));
REQUIRE(err == MsgPackError::Ok);
JsonArray& arr = doc.as<JsonArray>();
REQUIRE(arr[0] == 0);
REQUIRE(arr[1] == 2);
}
}