string.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #define ARDUINOJSON_DECODE_UNICODE 1
  5. #include <ArduinoJson.h>
  6. #include <catch.hpp>
  7. TEST_CASE("Valid JSON strings value") {
  8. struct TestCase {
  9. const char* input;
  10. const char* expectedOutput;
  11. };
  12. TestCase testCases[] = {
  13. {"\"hello world\"", "hello world"},
  14. {"\'hello world\'", "hello world"},
  15. {"'\"'", "\""},
  16. {"'\\\\'", "\\"},
  17. {"'\\/'", "/"},
  18. {"'\\b'", "\b"},
  19. {"'\\f'", "\f"},
  20. {"'\\n'", "\n"},
  21. {"'\\r'", "\r"},
  22. {"'\\t'", "\t"},
  23. {"\"1\\\"2\\\\3\\/4\\b5\\f6\\n7\\r8\\t9\"", "1\"2\\3/4\b5\f6\n7\r8\t9"},
  24. {"'\\u0041'", "A"},
  25. {"'\\u00e4'", "\xc3\xa4"}, // ä
  26. {"'\\u00E4'", "\xc3\xa4"}, // ä
  27. {"'\\u3042'", "\xe3\x81\x82"}, // あ
  28. {"'\\ud83d\\udda4'", "\xf0\x9f\x96\xa4"}, // 🖤
  29. {"'\\uF053'", "\xef\x81\x93"}, // issue #1173
  30. {"'\\uF015'", "\xef\x80\x95"}, // issue #1173
  31. {"'\\uF054'", "\xef\x81\x94"}, // issue #1173
  32. };
  33. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  34. DynamicJsonDocument doc(4096);
  35. for (size_t i = 0; i < testCount; i++) {
  36. const TestCase& testCase = testCases[i];
  37. CAPTURE(testCase.input);
  38. DeserializationError err = deserializeJson(doc, testCase.input);
  39. CHECK(err == DeserializationError::Ok);
  40. CHECK(doc.as<std::string>() == testCase.expectedOutput);
  41. }
  42. }
  43. TEST_CASE("\\u0000") {
  44. StaticJsonDocument<200> doc;
  45. DeserializationError err = deserializeJson(doc, "\"wx\\u0000yz\"");
  46. REQUIRE(err == DeserializationError::Ok);
  47. const char* result = doc.as<const char*>();
  48. CHECK(result[0] == 'w');
  49. CHECK(result[1] == 'x');
  50. CHECK(result[2] == 0);
  51. CHECK(result[3] == 'y');
  52. CHECK(result[4] == 'z');
  53. CHECK(result[5] == 0);
  54. CHECK(doc.as<JsonString>().size() == 5);
  55. CHECK(doc.as<std::string>().size() == 5);
  56. }
  57. TEST_CASE("Truncated JSON string") {
  58. const char* testCases[] = {"\"hello", "\'hello", "'\\u", "'\\u00", "'\\u000"};
  59. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  60. DynamicJsonDocument doc(4096);
  61. for (size_t i = 0; i < testCount; i++) {
  62. const char* input = testCases[i];
  63. CAPTURE(input);
  64. REQUIRE(deserializeJson(doc, input) ==
  65. DeserializationError::IncompleteInput);
  66. }
  67. }
  68. TEST_CASE("Invalid JSON string") {
  69. const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'",
  70. "'\\u000G'", "'\\u000/'", "'\\x1234'"};
  71. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  72. DynamicJsonDocument doc(4096);
  73. for (size_t i = 0; i < testCount; i++) {
  74. const char* input = testCases[i];
  75. CAPTURE(input);
  76. REQUIRE(deserializeJson(doc, input) == DeserializationError::InvalidInput);
  77. }
  78. }
  79. TEST_CASE("Not enough room to save the key") {
  80. DynamicJsonDocument doc(JSON_OBJECT_SIZE(1) + 8);
  81. SECTION("Quoted string") {
  82. REQUIRE(deserializeJson(doc, "{\"example\":1}") ==
  83. DeserializationError::Ok);
  84. REQUIRE(deserializeJson(doc, "{\"accuracy\":1}") ==
  85. DeserializationError::NoMemory);
  86. REQUIRE(deserializeJson(doc, "{\"hello\":1,\"world\"}") ==
  87. DeserializationError::NoMemory); // fails in the second string
  88. }
  89. SECTION("Non-quoted string") {
  90. REQUIRE(deserializeJson(doc, "{example:1}") == DeserializationError::Ok);
  91. REQUIRE(deserializeJson(doc, "{accuracy:1}") ==
  92. DeserializationError::NoMemory);
  93. REQUIRE(deserializeJson(doc, "{hello:1,world}") ==
  94. DeserializationError::NoMemory); // fails in the second string
  95. }
  96. }
  97. TEST_CASE("Empty memory pool") {
  98. // NOLINTNEXTLINE(clang-analyzer-optin.portability.UnixAPI)
  99. DynamicJsonDocument doc(0);
  100. SECTION("Input is const char*") {
  101. REQUIRE(deserializeJson(doc, "\"hello\"") ==
  102. DeserializationError::NoMemory);
  103. REQUIRE(deserializeJson(doc, "\"\"") == DeserializationError::NoMemory);
  104. }
  105. SECTION("Input is const char*") {
  106. char hello[] = "\"hello\"";
  107. REQUIRE(deserializeJson(doc, hello) == DeserializationError::Ok);
  108. char empty[] = "\"hello\"";
  109. REQUIRE(deserializeJson(doc, empty) == DeserializationError::Ok);
  110. }
  111. }