string_length_size_4.cpp 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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. }
  66. SECTION("bin 32 deserialization") {
  67. auto str = std::string(65536, '?');
  68. auto input = "\xc6\x00\x01\x00\x00"_s + str;
  69. auto err = deserializeMsgPack(doc, input);
  70. REQUIRE(err == DeserializationError::Ok);
  71. REQUIRE(doc.is<MsgPackBinary>());
  72. auto binary = doc.as<MsgPackBinary>();
  73. REQUIRE(binary.size() == 65536);
  74. REQUIRE(binary.data() != nullptr);
  75. REQUIRE(std::string(reinterpret_cast<const char*>(binary.data()),
  76. binary.size()) == str);
  77. }
  78. SECTION("bin 32 serialization") {
  79. auto str = std::string(65536, '?');
  80. doc.set(MsgPackBinary(str.data(), str.size()));
  81. std::string output;
  82. auto result = serializeMsgPack(doc, output);
  83. REQUIRE(result == 5 + str.size());
  84. REQUIRE(output == "\xc6\x00\x01\x00\x00"_s + str);
  85. }
  86. SECTION("ext 32 deserialization") {
  87. auto str = std::string(65536, '?');
  88. auto input = "\xc9\x00\x01\x00\x00\x2a"_s + str;
  89. auto err = deserializeMsgPack(doc, input);
  90. REQUIRE(err == DeserializationError::Ok);
  91. REQUIRE(doc.is<MsgPackExtension>());
  92. auto value = doc.as<MsgPackExtension>();
  93. REQUIRE(value.type() == 42);
  94. REQUIRE(value.size() == 65536);
  95. REQUIRE(value.data() != nullptr);
  96. REQUIRE(std::string(reinterpret_cast<const char*>(value.data()),
  97. value.size()) == str);
  98. }
  99. SECTION("ext 32 serialization") {
  100. auto str = std::string(65536, '?');
  101. doc.set(MsgPackExtension(42, str.data(), str.size()));
  102. std::string output;
  103. auto result = serializeMsgPack(doc, output);
  104. REQUIRE(result == 6 + str.size());
  105. REQUIRE(output == "\xc9\x00\x01\x00\x00\x2a"_s + str);
  106. }
  107. }