string_length_size_2.cpp 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #define ARDUINOJSON_STRING_LENGTH_SIZE 2
  2. #include <ArduinoJson.h>
  3. #include <catch.hpp>
  4. #include <string>
  5. TEST_CASE("ARDUINOJSON_STRING_LENGTH_SIZE == 2") {
  6. JsonDocument doc;
  7. SECTION("set(std::string)") {
  8. SECTION("returns true if len <= 65535") {
  9. auto result = doc.set(std::string(65535, '?'));
  10. REQUIRE(result == true);
  11. REQUIRE(doc.overflowed() == false);
  12. }
  13. SECTION("returns false if len >= 65536") {
  14. auto result = doc.set(std::string(65536, '?'));
  15. REQUIRE(result == false);
  16. REQUIRE(doc.overflowed() == true);
  17. }
  18. }
  19. SECTION("set(MsgPackBinary)") {
  20. SECTION("returns true if size <= 65532") {
  21. auto str = std::string(65532, '?');
  22. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  23. REQUIRE(result == true);
  24. REQUIRE(doc.overflowed() == false);
  25. }
  26. SECTION("returns false if size >= 65533") {
  27. auto str = std::string(65533, '?');
  28. auto result = doc.set(MsgPackBinary(str.data(), str.size()));
  29. REQUIRE(result == false);
  30. REQUIRE(doc.overflowed() == true);
  31. }
  32. }
  33. SECTION("deserializeJson()") {
  34. SECTION("returns Ok if string length <= 65535") {
  35. auto input = "\"" + std::string(65535, '?') + "\"";
  36. auto err = deserializeJson(doc, input);
  37. REQUIRE(err == DeserializationError::Ok);
  38. }
  39. SECTION("returns NoMemory if string length >= 65536") {
  40. auto input = "\"" + std::string(65536, '?') + "\"";
  41. auto err = deserializeJson(doc, input);
  42. REQUIRE(err == DeserializationError::NoMemory);
  43. }
  44. }
  45. SECTION("deserializeMsgPack()") {
  46. SECTION("returns Ok if string length <= 65535") {
  47. auto input = "\xda\xff\xff" + std::string(65535, '?');
  48. auto err = deserializeMsgPack(doc, input);
  49. REQUIRE(err == DeserializationError::Ok);
  50. }
  51. SECTION("returns NoMemory if string length >= 65536") {
  52. auto input =
  53. std::string("\xdb\x00\x01\x00\x00", 5) + std::string(65536, '?');
  54. auto err = deserializeMsgPack(doc, input);
  55. REQUIRE(err == DeserializationError::NoMemory);
  56. }
  57. SECTION("returns Ok if binary size <= 65532") {
  58. auto input = "\xc5\xff\xfc" + std::string(65532, '?');
  59. auto err = deserializeMsgPack(doc, input);
  60. REQUIRE(err == DeserializationError::Ok);
  61. }
  62. SECTION("returns NoMemory if binary size >= 65534") {
  63. auto input = "\xc5\xff\xfd" + std::string(65534, '?');
  64. auto err = deserializeMsgPack(doc, input);
  65. REQUIRE(err == DeserializationError::NoMemory);
  66. }
  67. }
  68. }