errors.cpp 2.8 KB

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