string.cpp 4.2 KB

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