errors.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2025, Benoit BLANCHON
  3. // MIT License
  4. #define ARDUINOJSON_DECODE_UNICODE 1
  5. #include <ArduinoJson.h>
  6. #include <catch.hpp>
  7. #include "Allocators.hpp"
  8. TEST_CASE("deserializeJson() returns IncompleteInput") {
  9. const char* testCases[] = {
  10. // strings
  11. "\"\\",
  12. "\"hello",
  13. "\'hello",
  14. // unicode
  15. "'\\u",
  16. "'\\u00",
  17. "'\\u000",
  18. // false
  19. "f",
  20. "fa",
  21. "fal",
  22. "fals",
  23. // true
  24. "t",
  25. "tr",
  26. "tru",
  27. // null
  28. "n",
  29. "nu",
  30. "nul",
  31. // object
  32. "{",
  33. "{a",
  34. "{a:",
  35. "{a:1",
  36. "{a:1,",
  37. "{a:1,b",
  38. "{a:1,b:",
  39. };
  40. for (auto input : testCases) {
  41. SECTION(input) {
  42. JsonDocument doc;
  43. REQUIRE(deserializeJson(doc, input) ==
  44. DeserializationError::IncompleteInput);
  45. }
  46. }
  47. }
  48. TEST_CASE("deserializeJson() returns InvalidInput") {
  49. const char* testCases[] = {
  50. // unicode
  51. "'\\u'", "'\\u000g'", "'\\u000'", "'\\u000G'", "'\\u000/'", "\\x1234",
  52. // numbers
  53. "6a9", "1,", "2]", "3}",
  54. // constants
  55. "nulL", "tru3", "fals3",
  56. // garbage
  57. "%*$£¤"};
  58. for (auto input : testCases) {
  59. SECTION(input) {
  60. JsonDocument doc;
  61. REQUIRE(deserializeJson(doc, input) ==
  62. DeserializationError::InvalidInput);
  63. }
  64. }
  65. }
  66. TEST_CASE("deserializeJson() oversees some edge cases") {
  67. const char* testCases[] = {
  68. "'\\ud83d'", // leading surrogate without a trailing surrogate
  69. "'\\udda4'", // trailing surrogate without a leading surrogate
  70. "'\\ud83d\\ud83d'", // two leading surrogates
  71. };
  72. for (auto input : testCases) {
  73. SECTION(input) {
  74. JsonDocument doc;
  75. REQUIRE(deserializeJson(doc, input) == DeserializationError::Ok);
  76. }
  77. }
  78. }
  79. TEST_CASE("deserializeJson() returns EmptyInput") {
  80. JsonDocument doc;
  81. SECTION("null") {
  82. auto err = deserializeJson(doc, static_cast<const char*>(0));
  83. REQUIRE(err == DeserializationError::EmptyInput);
  84. }
  85. SECTION("Empty string") {
  86. auto err = deserializeJson(doc, "");
  87. REQUIRE(err == DeserializationError::EmptyInput);
  88. }
  89. SECTION("Only spaces") {
  90. auto err = deserializeJson(doc, " \t\n\r");
  91. REQUIRE(err == DeserializationError::EmptyInput);
  92. }
  93. }
  94. TEST_CASE("deserializeJson() returns NoMemory if string length overflows") {
  95. JsonDocument doc;
  96. auto maxLength = ArduinoJson::detail::StringNode::maxLength;
  97. SECTION("max length should succeed") {
  98. auto err = deserializeJson(doc, "\"" + std::string(maxLength, 'a') + "\"");
  99. REQUIRE(err == DeserializationError::Ok);
  100. }
  101. SECTION("one above max length should fail") {
  102. auto err =
  103. deserializeJson(doc, "\"" + std::string(maxLength + 1, 'a') + "\"");
  104. REQUIRE(err == DeserializationError::NoMemory);
  105. }
  106. }
  107. TEST_CASE("deserializeJson() returns NoMemory if 8-bit slot allocation fails") {
  108. JsonDocument doc(FailingAllocator::instance());
  109. SECTION("uint32_t should pass") {
  110. auto err = deserializeJson(doc, "4294967295");
  111. REQUIRE(err == DeserializationError::Ok);
  112. }
  113. SECTION("uint64_t should fail") {
  114. auto err = deserializeJson(doc, "18446744073709551615");
  115. REQUIRE(err == DeserializationError::NoMemory);
  116. }
  117. SECTION("int32_t should pass") {
  118. auto err = deserializeJson(doc, "-2147483648");
  119. REQUIRE(err == DeserializationError::Ok);
  120. }
  121. SECTION("int64_t should fail") {
  122. auto err = deserializeJson(doc, "-9223372036854775808");
  123. REQUIRE(err == DeserializationError::NoMemory);
  124. }
  125. SECTION("float should pass") {
  126. auto err = deserializeJson(doc, "3.402823e38");
  127. REQUIRE(err == DeserializationError::Ok);
  128. }
  129. SECTION("double should fail") {
  130. auto err = deserializeJson(doc, "1.7976931348623157e308");
  131. REQUIRE(err == DeserializationError::NoMemory);
  132. }
  133. }