Fixed result of JsonVariant::set((char*)0) (fixes #1368)

This commit is contained in:
Benoit Blanchon
2020-09-05 17:33:47 +02:00
parent 05fc136915
commit d04669d0cc
7 changed files with 92 additions and 92 deletions

View File

@ -7,115 +7,150 @@
enum ErrorCode { ERROR_01 = 1, ERROR_10 = 10 };
TEST_CASE("JsonVariant and strings") {
TEST_CASE("JsonVariant::set() when there is enough memory") {
DynamicJsonDocument doc(4096);
JsonVariant variant = doc.to<JsonVariant>();
SECTION("stores const char* by reference") {
SECTION("const char*") {
char str[16];
strcpy(str, "hello");
variant.set(static_cast<const char *>(str));
bool result = variant.set(static_cast<const char *>(str));
strcpy(str, "world");
REQUIRE(variant == "world");
REQUIRE(result == true);
REQUIRE(variant == "world"); // stores by pointer
}
SECTION("stores char* by copy") {
char str[16];
SECTION("(const char*)0") {
bool result = variant.set(static_cast<const char *>(0));
strcpy(str, "hello");
variant.set(str);
strcpy(str, "world");
REQUIRE(variant == "hello");
REQUIRE(result == true);
REQUIRE(variant.isNull());
}
SECTION("stores unsigned char* by copy") {
SECTION("char*") {
char str[16];
strcpy(str, "hello");
variant.set(reinterpret_cast<unsigned char *>(str));
bool result = variant.set(str);
strcpy(str, "world");
REQUIRE(variant == "hello");
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
}
SECTION("stores signed char* by copy") {
SECTION("(char*)0") {
bool result = variant.set(static_cast<char *>(0));
REQUIRE(result == true);
REQUIRE(variant.isNull());
}
SECTION("unsigned char*") {
char str[16];
strcpy(str, "hello");
variant.set(reinterpret_cast<signed char *>(str));
bool result = variant.set(reinterpret_cast<unsigned char *>(str));
strcpy(str, "world");
REQUIRE(variant == "hello");
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
}
SECTION("signed char*") {
char str[16];
strcpy(str, "hello");
bool result = variant.set(reinterpret_cast<signed char *>(str));
strcpy(str, "world");
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
}
#ifdef HAS_VARIABLE_LENGTH_ARRAY
SECTION("stores VLA by copy") {
SECTION("VLA") {
int n = 16;
char str[n];
strcpy(str, "hello");
variant.set(str);
bool result = variant.set(str);
strcpy(str, "world");
REQUIRE(variant == "hello");
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
}
#endif
SECTION("stores std::string by copy") {
SECTION("std::string") {
std::string str;
str = "hello";
variant.set(str);
bool result = variant.set(str);
str.replace(0, 5, "world");
REQUIRE(variant == "hello");
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
}
SECTION("stores static JsonString by reference") {
SECTION("static JsonString") {
char str[16];
strcpy(str, "hello");
variant.set(JsonString(str, true));
bool result = variant.set(JsonString(str, true));
strcpy(str, "world");
REQUIRE(variant == "world");
REQUIRE(result == true);
REQUIRE(variant == "world"); // stores by pointer
}
SECTION("stores non-static JsonString by copy") {
SECTION("non-static JsonString") {
char str[16];
strcpy(str, "hello");
variant.set(JsonString(str, false));
bool result = variant.set(JsonString(str, false));
strcpy(str, "world");
REQUIRE(variant == "hello");
REQUIRE(result == true);
REQUIRE(variant == "hello"); // stores by copy
}
SECTION("stores an enum as an integer") {
SECTION("enum") {
ErrorCode code = ERROR_10;
variant.set(code);
bool result = variant.set(code);
REQUIRE(result == true);
REQUIRE(variant.is<int>() == true);
REQUIRE(variant.as<int>() == 10);
}
}
TEST_CASE("JsonVariant with not enough memory") {
TEST_CASE("JsonVariant::set() with not enough memory") {
StaticJsonDocument<1> doc;
JsonVariant v = doc.to<JsonVariant>();
SECTION("std::string") {
v.set(std::string("hello world!!"));
bool result = v.set(std::string("hello world!!"));
REQUIRE(result == false);
REQUIRE(v.isNull());
}
SECTION("Serialized<std::string>") {
v.set(serialized(std::string("hello world!!")));
bool result = v.set(serialized(std::string("hello world!!")));
REQUIRE(result == false);
REQUIRE(v.isNull());
}
SECTION("char*") {
char s[] = "hello world!!";
bool result = v.set(s);
REQUIRE(result == false);
REQUIRE(v.isNull());
}
}