string_length_size_2.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. #define ARDUINOJSON_STRING_LENGTH_SIZE 2
  2. #include <ArduinoJson.h>
  3. #include <catch.hpp>
  4. #include <string>
  5. #include "Literals.hpp"
  6. TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 2") {
  7. JsonDocument doc;
  8. SECTION("set(std::string)") {
  9. SECTION("returns true if len <= 65535") {
  10. auto result = doc.set(std::string(65535, '?'));
  11. REQUIRE(result == true);
  12. REQUIRE(doc.overflowed() == false);
  13. }
  14. SECTION("returns false if len >= 65536") {
  15. auto result = doc.set(std::string(65536, '?'));
  16. REQUIRE(result == false);
  17. REQUIRE(doc.overflowed() == true);
  18. }
  19. }
  20. SECTION("set(MsgPackBinary)") {
  21. SECTION("returns true if size <= 65532") {
  22. auto str = std::string(65532, '?');
  23. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  24. REQUIRE(result == true);
  25. REQUIRE(doc.overflowed() == false);
  26. }
  27. SECTION("returns false if size >= 65533") {
  28. auto str = std::string(65533, '?');
  29. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  30. REQUIRE(result == false);
  31. REQUIRE(doc.overflowed() == true);
  32. }
  33. }
  34. SECTION("set(MsgPackExtension)") {
  35. SECTION("returns true if size <= 65531") {
  36. auto str = std::string(65531, '?');
  37. auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
  38. REQUIRE(result == true);
  39. REQUIRE(doc.overflowed() == false);
  40. }
  41. SECTION("returns false if size >= 65532") {
  42. auto str = std::string(65532, '?');
  43. auto result = doc.set(MsgPackExtension(1, str.data(), str.size()));
  44. REQUIRE(result == false);
  45. REQUIRE(doc.overflowed() == true);
  46. }
  47. }
  48. SECTION("deserializeJson()") {
  49. SECTION("returns Ok if string length <= 65535") {
  50. auto input = "\"" + std::string(65535, '?') + "\"";
  51. auto err = deserializeJson(doc, input);
  52. REQUIRE(err == DeserializationError::Ok);
  53. }
  54. SECTION("returns NoMemory if string length >= 65536") {
  55. auto input = "\"" + std::string(65536, '?') + "\"";
  56. auto err = deserializeJson(doc, input);
  57. REQUIRE(err == DeserializationError::NoMemory);
  58. }
  59. }
  60. SECTION("deserializeMsgPack()") {
  61. SECTION("returns Ok if string length <= 65535") {
  62. auto input = "\xda\xff\xff" + std::string(65535, '?');
  63. auto err = deserializeMsgPack(doc, input);
  64. REQUIRE(err == DeserializationError::Ok);
  65. }
  66. SECTION("returns NoMemory if string length >= 65536") {
  67. auto input = "\xdb\x00\x01\x00\x00"_s + std::string(65536, '?');
  68. auto err = deserializeMsgPack(doc, input);
  69. REQUIRE(err == DeserializationError::NoMemory);
  70. }
  71. SECTION("returns Ok if binary size <= 65532") {
  72. auto input = "\xc5\xff\xfc" + std::string(65532, '?');
  73. auto err = deserializeMsgPack(doc, input);
  74. REQUIRE(err == DeserializationError::Ok);
  75. }
  76. SECTION("returns NoMemory if binary size >= 65534") {
  77. auto input = "\xc5\xff\xfd" + std::string(65534, '?');
  78. auto err = deserializeMsgPack(doc, input);
  79. REQUIRE(err == DeserializationError::NoMemory);
  80. }
  81. // https://oss-fuzz.com/testcase?key=5354792971993088
  82. SECTION("doesn't overflow if binary size == 0xFFFF") {
  83. auto input = "\xc5\xff\xff"_s;
  84. auto err = deserializeMsgPack(doc, input);
  85. REQUIRE(err == DeserializationError::NoMemory);
  86. }
  87. SECTION("returns Ok if extension size <= 65531") {
  88. auto input = "\xc8\xff\xfb\x01" + std::string(65531, '?');
  89. auto err = deserializeMsgPack(doc, input);
  90. REQUIRE(err == DeserializationError::Ok);
  91. }
  92. SECTION("returns NoMemory if extension size >= 65532") {
  93. auto input = "\xc8\xff\xfc\x01" + std::string(65532, '?');
  94. auto err = deserializeMsgPack(doc, input);
  95. REQUIRE(err == DeserializationError::NoMemory);
  96. }
  97. }
  98. }