nestingLimit.cpp 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. // ArduinoJson - https://arduinojson.org
  2. // Copyright © 2014-2022, Benoit BLANCHON
  3. // MIT License
  4. #include <ArduinoJson.h>
  5. #include <catch.hpp>
  6. #define SHOULD_WORK(expression) REQUIRE(DeserializationError::Ok == expression);
  7. #define SHOULD_FAIL(expression) \
  8. REQUIRE(DeserializationError::TooDeep == expression);
  9. TEST_CASE("JsonDeserializer nesting") {
  10. DynamicJsonDocument doc(4096);
  11. SECTION("Input = const char*") {
  12. SECTION("limit = 0") {
  13. DeserializationOption::NestingLimit nesting(0);
  14. SHOULD_WORK(deserializeMsgPack(doc, "\xA1H", nesting)); // "H"
  15. SHOULD_FAIL(deserializeMsgPack(doc, "\x90", nesting)); // []
  16. SHOULD_FAIL(deserializeMsgPack(doc, "\x80", nesting)); // {}
  17. }
  18. SECTION("limit = 1") {
  19. DeserializationOption::NestingLimit nesting(1);
  20. SHOULD_WORK(deserializeMsgPack(doc, "\x90", nesting)); // {}
  21. SHOULD_WORK(deserializeMsgPack(doc, "\x80", nesting)); // []
  22. SHOULD_FAIL(deserializeMsgPack(doc, "\x81\xA1H\x80", nesting)); // {H:{}}
  23. SHOULD_FAIL(deserializeMsgPack(doc, "\x91\x90", nesting)); // [[]]
  24. }
  25. }
  26. SECTION("char* and size_t") {
  27. SECTION("limit = 0") {
  28. DeserializationOption::NestingLimit nesting(0);
  29. SHOULD_WORK(deserializeMsgPack(doc, "\xA1H", 2, nesting));
  30. SHOULD_FAIL(deserializeMsgPack(doc, "\x90", 1, nesting));
  31. SHOULD_FAIL(deserializeMsgPack(doc, "\x80", 1, nesting));
  32. }
  33. SECTION("limit = 1") {
  34. DeserializationOption::NestingLimit nesting(1);
  35. SHOULD_WORK(deserializeMsgPack(doc, "\x90", 1, nesting));
  36. SHOULD_WORK(deserializeMsgPack(doc, "\x80", 1, nesting));
  37. SHOULD_FAIL(deserializeMsgPack(doc, "\x81\xA1H\x80", 4, nesting));
  38. SHOULD_FAIL(deserializeMsgPack(doc, "\x91\x90", 2, nesting));
  39. }
  40. }
  41. SECTION("Input = std::string") {
  42. using std::string;
  43. SECTION("limit = 0") {
  44. DeserializationOption::NestingLimit nesting(0);
  45. SHOULD_WORK(deserializeMsgPack(doc, string("\xA1H"), nesting));
  46. SHOULD_FAIL(deserializeMsgPack(doc, string("\x90"), nesting));
  47. SHOULD_FAIL(deserializeMsgPack(doc, string("\x80"), nesting));
  48. }
  49. SECTION("limit = 1") {
  50. DeserializationOption::NestingLimit nesting(1);
  51. SHOULD_WORK(deserializeMsgPack(doc, string("\x90"), nesting));
  52. SHOULD_WORK(deserializeMsgPack(doc, string("\x80"), nesting));
  53. SHOULD_FAIL(deserializeMsgPack(doc, string("\x81\xA1H\x80"), nesting));
  54. SHOULD_FAIL(deserializeMsgPack(doc, string("\x91\x90"), nesting));
  55. }
  56. }
  57. SECTION("Input = std::istream") {
  58. SECTION("limit = 0") {
  59. DeserializationOption::NestingLimit nesting(0);
  60. std::istringstream good("\xA1H"); // "H"
  61. std::istringstream bad("\x90"); // []
  62. SHOULD_WORK(deserializeMsgPack(doc, good, nesting));
  63. SHOULD_FAIL(deserializeMsgPack(doc, bad, nesting));
  64. }
  65. SECTION("limit = 1") {
  66. DeserializationOption::NestingLimit nesting(1);
  67. std::istringstream good("\x90"); // []
  68. std::istringstream bad("\x91\x90"); // [[]]
  69. SHOULD_WORK(deserializeMsgPack(doc, good, nesting));
  70. SHOULD_FAIL(deserializeMsgPack(doc, bad, nesting));
  71. }
  72. }
  73. }