invalid_input.cpp 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2020
  3. // MIT License
  4. #define ARDUINOJSON_DECODE_UNICODE 1
  5. #include <ArduinoJson.h>
  6. #include <catch.hpp>
  7. TEST_CASE("Invalid JSON input") {
  8. const char* testCases[] = {"'\\u'", "'\\u000g'", "'\\u000'", "'\\u000G'",
  9. "'\\u000/'", "\\x1234", "6a9", "1,",
  10. "2]", "3}"};
  11. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  12. DynamicJsonDocument doc(4096);
  13. for (size_t i = 0; i < testCount; i++) {
  14. const char* input = testCases[i];
  15. CAPTURE(input);
  16. REQUIRE(deserializeJson(doc, input) == DeserializationError::InvalidInput);
  17. }
  18. }
  19. TEST_CASE("Invalid JSON input that should pass") {
  20. const char* testCases[] = {
  21. "nulL",
  22. "tru3",
  23. "fals3",
  24. "'\\ud83d'", // leading surrogate without a trailing surrogate
  25. "'\\udda4'", // trailing surrogate without a leading surrogate
  26. "'\\ud83d\\ud83d'", // two leading surrogates
  27. };
  28. const size_t testCount = sizeof(testCases) / sizeof(testCases[0]);
  29. DynamicJsonDocument doc(4096);
  30. for (size_t i = 0; i < testCount; i++) {
  31. const char* input = testCases[i];
  32. CAPTURE(input);
  33. REQUIRE(deserializeJson(doc, input) == DeserializationError::Ok);
  34. }
  35. }