nestingLimit.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // ArduinoJson - arduinojson.org
  2. // Copyright Benoit Blanchon 2014-2019
  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(deserializeJson(doc, "\"toto\"", nesting));
  15. SHOULD_WORK(deserializeJson(doc, "123", nesting));
  16. SHOULD_WORK(deserializeJson(doc, "true", nesting));
  17. SHOULD_FAIL(deserializeJson(doc, "[]", nesting));
  18. SHOULD_FAIL(deserializeJson(doc, "{}", nesting));
  19. SHOULD_FAIL(deserializeJson(doc, "[\"toto\"]", nesting));
  20. SHOULD_FAIL(deserializeJson(doc, "{\"toto\":1}", nesting));
  21. }
  22. SECTION("limit = 1") {
  23. DeserializationOption::NestingLimit nesting(1);
  24. SHOULD_WORK(deserializeJson(doc, "[\"toto\"]", nesting));
  25. SHOULD_WORK(deserializeJson(doc, "{\"toto\":1}", nesting));
  26. SHOULD_FAIL(deserializeJson(doc, "{\"toto\":{}}", nesting));
  27. SHOULD_FAIL(deserializeJson(doc, "{\"toto\":[]}", nesting));
  28. SHOULD_FAIL(deserializeJson(doc, "[[\"toto\"]]", nesting));
  29. SHOULD_FAIL(deserializeJson(doc, "[{\"toto\":1}]", nesting));
  30. }
  31. }
  32. SECTION("char* and size_t") {
  33. SECTION("limit = 0") {
  34. DeserializationOption::NestingLimit nesting(0);
  35. SHOULD_WORK(deserializeJson(doc, "\"toto\"", 6, nesting));
  36. SHOULD_WORK(deserializeJson(doc, "123", 3, nesting));
  37. SHOULD_WORK(deserializeJson(doc, "true", 4, nesting));
  38. SHOULD_FAIL(deserializeJson(doc, "[]", 2, nesting));
  39. SHOULD_FAIL(deserializeJson(doc, "{}", 2, nesting));
  40. SHOULD_FAIL(deserializeJson(doc, "[\"toto\"]", 8, nesting));
  41. SHOULD_FAIL(deserializeJson(doc, "{\"toto\":1}", 10, nesting));
  42. }
  43. SECTION("limit = 1") {
  44. DeserializationOption::NestingLimit nesting(1);
  45. SHOULD_WORK(deserializeJson(doc, "[\"toto\"]", 8, nesting));
  46. SHOULD_WORK(deserializeJson(doc, "{\"toto\":1}", 10, nesting));
  47. SHOULD_FAIL(deserializeJson(doc, "{\"toto\":{}}", 11, nesting));
  48. SHOULD_FAIL(deserializeJson(doc, "{\"toto\":[]}", 11, nesting));
  49. SHOULD_FAIL(deserializeJson(doc, "[[\"toto\"]]", 10, nesting));
  50. SHOULD_FAIL(deserializeJson(doc, "[{\"toto\":1}]", 12, nesting));
  51. }
  52. }
  53. SECTION("Input = std::string") {
  54. SECTION("limit = 0") {
  55. DeserializationOption::NestingLimit nesting(0);
  56. SHOULD_WORK(deserializeJson(doc, std::string("\"toto\""), nesting));
  57. SHOULD_WORK(deserializeJson(doc, std::string("123"), nesting));
  58. SHOULD_WORK(deserializeJson(doc, std::string("true"), nesting));
  59. SHOULD_FAIL(deserializeJson(doc, std::string("[]"), nesting));
  60. SHOULD_FAIL(deserializeJson(doc, std::string("{}"), nesting));
  61. SHOULD_FAIL(deserializeJson(doc, std::string("[\"toto\"]"), nesting));
  62. SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":1}"), nesting));
  63. }
  64. SECTION("limit = 1") {
  65. DeserializationOption::NestingLimit nesting(1);
  66. SHOULD_WORK(deserializeJson(doc, std::string("[\"toto\"]"), nesting));
  67. SHOULD_WORK(deserializeJson(doc, std::string("{\"toto\":1}"), nesting));
  68. SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":{}}"), nesting));
  69. SHOULD_FAIL(deserializeJson(doc, std::string("{\"toto\":[]}"), nesting));
  70. SHOULD_FAIL(deserializeJson(doc, std::string("[[\"toto\"]]"), nesting));
  71. SHOULD_FAIL(deserializeJson(doc, std::string("[{\"toto\":1}]"), nesting));
  72. }
  73. }
  74. SECTION("Input = std::istream") {
  75. SECTION("limit = 0") {
  76. DeserializationOption::NestingLimit nesting(0);
  77. std::istringstream good("true");
  78. std::istringstream bad("[]");
  79. SHOULD_WORK(deserializeJson(doc, good, nesting));
  80. SHOULD_FAIL(deserializeJson(doc, bad, nesting));
  81. }
  82. SECTION("limit = 1") {
  83. DeserializationOption::NestingLimit nesting(1);
  84. std::istringstream good("[\"toto\"]");
  85. std::istringstream bad("{\"toto\":{}}");
  86. SHOULD_WORK(deserializeJson(doc, good, nesting));
  87. SHOULD_FAIL(deserializeJson(doc, bad, nesting));
  88. }
  89. }
  90. }