| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162 |
- // ArduinoJson - https://arduinojson.org
- // Copyright © 2014-2025, Benoit BLANCHON
- // MIT License
- #define ARDUINOJSON_DECODE_UNICODE 1
- #include <ArduinoJson.h>
- #include <catch.hpp>
- #include "Allocators.hpp"
- TEST_CASE("deserializeJson() returns IncompleteInput") {
- const char* testCases[] = {
- // strings
- "\"\\",
- "\"hello",
- "\'hello",
- // unicode
- "'\\u",
- "'\\u00",
- "'\\u000",
- // false
- "f",
- "fa",
- "fal",
- "fals",
- // true
- "t",
- "tr",
- "tru",
- // null
- "n",
- "nu",
- "nul",
- // object
- "{",
- "{a",
- "{a:",
- "{a:1",
- "{a:1,",
- "{a:1,b",
- "{a:1,b:",
- };
- for (auto input : testCases) {
- SECTION(input) {
- JsonDocument doc;
- REQUIRE(deserializeJson(doc, input) ==
- DeserializationError::IncompleteInput);
- }
- }
- }
- TEST_CASE("deserializeJson() returns InvalidInput") {
- const char* testCases[] = {
- // unicode
- "'\\u'", "'\\u000g'", "'\\u000'", "'\\u000G'", "'\\u000/'", "\\x1234",
- // numbers
- "6a9", "1,", "2]", "3}",
- // constants
- "nulL", "tru3", "fals3",
- // garbage
- "%*$£¤"};
- for (auto input : testCases) {
- SECTION(input) {
- JsonDocument doc;
- REQUIRE(deserializeJson(doc, input) ==
- DeserializationError::InvalidInput);
- }
- }
- }
- TEST_CASE("deserializeJson() oversees some edge cases") {
- const char* testCases[] = {
- "'\\ud83d'", // leading surrogate without a trailing surrogate
- "'\\udda4'", // trailing surrogate without a leading surrogate
- "'\\ud83d\\ud83d'", // two leading surrogates
- };
- for (auto input : testCases) {
- SECTION(input) {
- JsonDocument doc;
- REQUIRE(deserializeJson(doc, input) == DeserializationError::Ok);
- }
- }
- }
- TEST_CASE("deserializeJson() returns EmptyInput") {
- JsonDocument doc;
- SECTION("null") {
- auto err = deserializeJson(doc, static_cast<const char*>(0));
- REQUIRE(err == DeserializationError::EmptyInput);
- }
- SECTION("Empty string") {
- auto err = deserializeJson(doc, "");
- REQUIRE(err == DeserializationError::EmptyInput);
- }
- SECTION("Only spaces") {
- auto err = deserializeJson(doc, " \t\n\r");
- REQUIRE(err == DeserializationError::EmptyInput);
- }
- }
- TEST_CASE("deserializeJson() returns NoMemory if string length overflows") {
- JsonDocument doc;
- auto maxLength = ArduinoJson::detail::StringNode::maxLength;
- SECTION("max length should succeed") {
- auto err = deserializeJson(doc, "\"" + std::string(maxLength, 'a') + "\"");
- REQUIRE(err == DeserializationError::Ok);
- }
- SECTION("one above max length should fail") {
- auto err =
- deserializeJson(doc, "\"" + std::string(maxLength + 1, 'a') + "\"");
- REQUIRE(err == DeserializationError::NoMemory);
- }
- }
- TEST_CASE("deserializeJson() returns NoMemory if 8-bit slot allocation fails") {
- JsonDocument doc(FailingAllocator::instance());
- SECTION("uint32_t should pass") {
- auto err = deserializeJson(doc, "4294967295");
- REQUIRE(err == DeserializationError::Ok);
- }
- SECTION("uint64_t should fail") {
- auto err = deserializeJson(doc, "18446744073709551615");
- REQUIRE(err == DeserializationError::NoMemory);
- }
- SECTION("int32_t should pass") {
- auto err = deserializeJson(doc, "-2147483648");
- REQUIRE(err == DeserializationError::Ok);
- }
- SECTION("int64_t should fail") {
- auto err = deserializeJson(doc, "-9223372036854775808");
- REQUIRE(err == DeserializationError::NoMemory);
- }
- SECTION("float should pass") {
- auto err = deserializeJson(doc, "3.402823e38");
- REQUIRE(err == DeserializationError::Ok);
- }
- SECTION("double should fail") {
- auto err = deserializeJson(doc, "1.7976931348623157e308");
- REQUIRE(err == DeserializationError::NoMemory);
- }
- }
|