Fixed null values that could be pass to strcmp() (closes #745)

This commit is contained in:
Benoit Blanchon
2018-06-01 09:08:38 +02:00
parent 3523296e3d
commit 7c0af91844
9 changed files with 75 additions and 16 deletions

View File

@ -149,4 +149,15 @@ TEST_CASE("JsonObject::operator[]") {
const size_t expectedSize = JSON_OBJECT_SIZE(1) + 12;
REQUIRE(expectedSize <= _jsonBuffer.size());
}
SECTION("should ignore null key") {
// object must have a value to make a call to strcmp()
_object["dummy"] = 42;
const char* null = 0;
_object[null] = 666;
REQUIRE(_object.size() == 1);
REQUIRE(_object[null] == 0);
}
}

View File

@ -5,6 +5,8 @@
#include <ArduinoJson.h>
#include <catch.hpp>
static const char* null = 0;
template <typename T>
void checkEquals(JsonVariant a, T b) {
REQUIRE(b == a);
@ -96,38 +98,69 @@ TEST_CASE("JsonVariant comparisons") {
checkComparisons<unsigned short>(122, 123, 124);
}
SECTION("null") {
JsonVariant variant = null;
REQUIRE(variant == variant);
REQUIRE_FALSE(variant != variant);
REQUIRE(variant == null);
REQUIRE_FALSE(variant != null);
REQUIRE(variant != "null");
REQUIRE_FALSE(variant == "null");
}
SECTION("StringLiteral") {
DynamicJsonBuffer jsonBuffer;
JsonVariant variant = jsonBuffer.parse("\"hello\"");
REQUIRE(variant == variant);
REQUIRE_FALSE(variant != variant);
REQUIRE(variant == "hello");
REQUIRE_FALSE(variant != "hello");
REQUIRE(variant != "world");
REQUIRE_FALSE(variant == "world");
REQUIRE(variant != null);
REQUIRE_FALSE(variant == null);
REQUIRE("hello" == variant);
REQUIRE_FALSE("hello" != variant);
REQUIRE("world" != variant);
REQUIRE_FALSE("world" == variant);
REQUIRE(null != variant);
REQUIRE_FALSE(null == variant);
}
SECTION("String") {
DynamicJsonBuffer jsonBuffer;
JsonVariant variant = jsonBuffer.parse("\"hello\"");
REQUIRE(variant == variant);
REQUIRE_FALSE(variant != variant);
REQUIRE(variant == std::string("hello"));
REQUIRE_FALSE(variant != std::string("hello"));
REQUIRE(variant != std::string("world"));
REQUIRE_FALSE(variant == std::string("world"));
REQUIRE(variant != null);
REQUIRE_FALSE(variant == null);
REQUIRE(std::string("hello") == variant);
REQUIRE_FALSE(std::string("hello") != variant);
REQUIRE(std::string("world") != variant);
REQUIRE_FALSE(std::string("world") == variant);
REQUIRE(null != variant);
REQUIRE_FALSE(null == variant);
}
SECTION("IntegerInVariant") {