string_length_size_4.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #define ARDUINOJSON_STRING_LENGTH_SIZE 4
  2. #include <ArduinoJson.h>
  3. #include <catch.hpp>
  4. #include <string>
  5. #include "Literals.hpp"
  6. TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 4") {
  7. JsonDocument doc;
  8. SECTION("set(std::string)") {
  9. SECTION("returns true if string length >= 65536") {
  10. auto result = doc.set(std::string(65536, '?'));
  11. REQUIRE(result == true);
  12. REQUIRE(doc.overflowed() == false);
  13. }
  14. }
  15. SECTION("set(MsgPackBinary)") {
  16. SECTION("returns true if size >= 65536") {
  17. auto str = std::string(65536, '?');
  18. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  19. REQUIRE(result == true);
  20. REQUIRE(doc.overflowed() == false);
  21. }
  22. }
  23. SECTION("set(MsgPackExtension)") {
  24. SECTION("returns true if size >= 65532") {
  25. auto str = std::string(65532, '?');
  26. auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
  27. REQUIRE(result == true);
  28. REQUIRE(doc.overflowed() == false);
  29. }
  30. }
  31. SECTION("deserializeJson()") {
  32. SECTION("returns Ok if string length >= 65536") {
  33. auto input = "\"" + std::string(65536, '?') + "\"";
  34. auto err = deserializeJson(doc, input);
  35. REQUIRE(err == DeserializationError::Ok);
  36. }
  37. }
  38. SECTION("deserializeMsgPack()") {
  39. SECTION("returns Ok if string size >= 65536") {
  40. auto input = "\xda\xff\xff" + std::string(65536, '?');
  41. auto err = deserializeMsgPack(doc, input);
  42. REQUIRE(err == DeserializationError::Ok);
  43. }
  44. SECTION("returns Ok if binary size >= 65536") {
  45. auto input = "\xc5\xff\xff" + std::string(65536, '?');
  46. auto err = deserializeMsgPack(doc, input);
  47. REQUIRE(err == DeserializationError::Ok);
  48. }
  49. SECTION("returns Ok if extension size >= 65532") {
  50. auto input = "\xc8\xff\xfb\x01" + std::string(65532, '?');
  51. auto err = deserializeMsgPack(doc, input);
  52. REQUIRE(err == DeserializationError::Ok);
  53. }
  54. // https://oss-fuzz.com/testcase?key=5354792971993088
  55. SECTION("doesn't overflow if binary size == 0xFFFFFFFF") {
  56. auto input = "\xc6\xff\xff\xff\xff"_s;
  57. auto err = deserializeMsgPack(doc, input);
  58. REQUIRE(err == DeserializationError::NoMemory);
  59. }
  60. SECTION("doesn't overflow if string size == 0xFFFFFFFF") {
  61. auto input = "\xdb\xff\xff\xff\xff???????????????????"_s;
  62. auto err = deserializeMsgPack(doc, input);
  63. REQUIRE(err != DeserializationError::Ok);
  64. }
  65. SECTION("bin 32") {
  66. auto str = std::string(65536, '?');
  67. auto input = "\xc6\x00\x01\x00\x00"_s + str;
  68. auto err = deserializeMsgPack(doc, input);
  69. REQUIRE(err == DeserializationError::Ok);
  70. REQUIRE(doc.is<MsgPackBinary>());
  71. auto binary = doc.as<MsgPackBinary>();
  72. REQUIRE(binary.size() == 65536);
  73. REQUIRE(binary.data() != nullptr);
  74. REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
  75. binary.size()) == str);
  76. }
  77. SECTION("ext 32 deserialization") {
  78. auto str = std::string(65536, '?');
  79. auto input = "\xc9\x00\x01\x00\x00\x2a"_s + str;
  80. auto err = deserializeMsgPack(doc, input);
  81. REQUIRE(err == DeserializationError::Ok);
  82. REQUIRE(doc.is<MsgPackExtension>());
  83. auto value = doc.as<MsgPackExtension>();
  84. REQUIRE(value.type() == 42);
  85. REQUIRE(value.size() == 65536);
  86. REQUIRE(value.data() != nullptr);
  87. REQUIRE(std::string(reinterpret_cast<const char*>(value.data()),
  88. value.size()) == str);
  89. }
  90. }
  91. SECTION("serializeMsgPack()") {
  92. SECTION("bin 32 serialization") {
  93. auto str = std::string(65536, '?');
  94. doc.set(MsgPackBinary(str.data(), str.size()));
  95. std::string output;
  96. auto result = serializeMsgPack(doc, output);
  97. REQUIRE(result == 5 + str.size());
  98. REQUIRE(output == "\xc6\x00\x01\x00\x00"_s + str);
  99. }
  100. SECTION("ext 32 serialization") {
  101. auto str = std::string(65536, '?');
  102. doc.set(MsgPackExtension(42, str.data(), str.size()));
  103. std::string output;
  104. auto result = serializeMsgPack(doc, output);
  105. REQUIRE(result == 6 + str.size());
  106. REQUIRE(output == "\xc9\x00\x01\x00\x00\x2a"_s + str);
  107. }
  108. SECTION("str 32 serialization") {
  109. auto str = std::string(65536, '?');
  110. doc.set(str);
  111. std::string output;
  112. auto result = serializeMsgPack(doc, output);
  113. REQUIRE(result == 5 + str.size());
  114. REQUIRE(output == "\xDB\x00\x01\x00\x00"_s + str);
  115. }
  116. }
  117. }