mirror of
https://github.com/XRPLF/clio.git
synced 2025-12-06 17:27:58 +00:00
feat: Validate unexpected config values (#2457)
This commit is contained in:
@@ -81,7 +81,8 @@ class SettingsProviderTest : public NoLoggerFixture {};
|
||||
|
||||
TEST_F(SettingsProviderTest, Defaults)
|
||||
{
|
||||
auto const cfg = getParseSettingsConfig(json::parse(R"JSON({"contact_points": "127.0.0.1"})JSON"));
|
||||
auto const cfg =
|
||||
getParseSettingsConfig(json::parse(R"JSON({"database.cassandra.contact_points": "127.0.0.1"})JSON"));
|
||||
SettingsProvider const provider{cfg.getObject("database.cassandra")};
|
||||
|
||||
auto const settings = provider.getSettings();
|
||||
|
||||
@@ -160,7 +160,7 @@ TEST_F(LoadBalancerConstructorTests, construct)
|
||||
TEST_F(LoadBalancerConstructorTests, forwardingTimeoutPassedToSourceFactory)
|
||||
{
|
||||
auto const forwardingTimeout = 10;
|
||||
configJson_.as_object()["forwarding"] = boost::json::object{{"timeout", float{forwardingTimeout}}};
|
||||
configJson_.as_object()["forwarding"] = boost::json::object{{"cache_timeout", float{forwardingTimeout}}};
|
||||
EXPECT_CALL(
|
||||
sourceFactory_,
|
||||
makeSource(
|
||||
|
||||
@@ -182,7 +182,7 @@ TEST_F(LoadBalancerConstructorNgTests, construct)
|
||||
TEST_F(LoadBalancerConstructorNgTests, forwardingTimeoutPassedToSourceFactory)
|
||||
{
|
||||
auto const forwardingTimeout = 10;
|
||||
configJson_.as_object()["forwarding"] = boost::json::object{{"timeout", float{forwardingTimeout}}};
|
||||
configJson_.as_object()["forwarding"] = boost::json::object{{"cache_timeout", float{forwardingTimeout}}};
|
||||
EXPECT_CALL(
|
||||
sourceFactory_,
|
||||
makeSource(
|
||||
|
||||
@@ -465,3 +465,35 @@ TEST_F(ClioConfigDefinitionParseArrayTest, missingAllRequiredFields)
|
||||
EXPECT_EQ(result->size(), 1);
|
||||
EXPECT_THAT(result->at(0).error, testing::StartsWith("array.[].int"));
|
||||
}
|
||||
|
||||
TEST(ClioConfigDefinitionParse, unexpectedFields)
|
||||
{
|
||||
ClioConfigDefinition config{
|
||||
{"expected", ConfigValue{ConfigType::String}.optional()},
|
||||
};
|
||||
|
||||
auto const configJson = boost::json::parse(R"JSON({
|
||||
"expected": "present",
|
||||
"unexpected_string": "",
|
||||
"unexpected_non_empty_array": [
|
||||
{"string": ""},
|
||||
{"string": ""}
|
||||
],
|
||||
"unexpected_empty_array": [],
|
||||
"unexpected_object": {
|
||||
"string": ""
|
||||
}
|
||||
})JSON")
|
||||
.as_object();
|
||||
|
||||
auto const configFile = ConfigFileJson{configJson};
|
||||
auto result = config.parse(configFile);
|
||||
std::ranges::sort(*result, [](auto const& lhs, auto const& rhs) { return lhs.error < rhs.error; });
|
||||
ASSERT_TRUE(result.has_value());
|
||||
ASSERT_EQ(result->size(), 4);
|
||||
|
||||
EXPECT_EQ(result->at(0).error, "Unknown key: unexpected_empty_array.[]");
|
||||
EXPECT_EQ(result->at(1).error, "Unknown key: unexpected_non_empty_array.[].string");
|
||||
EXPECT_EQ(result->at(2).error, "Unknown key: unexpected_object.string");
|
||||
EXPECT_EQ(result->at(3).error, "Unknown key: unexpected_string");
|
||||
}
|
||||
|
||||
@@ -31,6 +31,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <cstdint>
|
||||
#include <ranges>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
#include <variant>
|
||||
@@ -488,6 +489,31 @@ TEST_F(ConfigFileJsonTest, containsKey)
|
||||
EXPECT_FALSE(jsonFileObj.containsKey("array_of_objects.[].object"));
|
||||
}
|
||||
|
||||
TEST_F(ConfigFileJsonTest, getAllKeys)
|
||||
{
|
||||
auto const jsonStr = R"JSON({
|
||||
"int": 42,
|
||||
"object": { "string": "some string", "array": [1, 2, 3] },
|
||||
"array2": [1, 2, 3],
|
||||
"array_of_objects": [ {"int": 42}, {"string": "some string"} ]
|
||||
})JSON";
|
||||
auto const jsonFileObj = ConfigFileJson{boost::json::parse(jsonStr).as_object()};
|
||||
|
||||
auto allKeys = jsonFileObj.getAllKeys();
|
||||
std::ranges::sort(allKeys);
|
||||
EXPECT_EQ(allKeys.size(), 6);
|
||||
|
||||
std::vector<std::string> const expectedKeys{
|
||||
{"array2.[]",
|
||||
"array_of_objects.[].int",
|
||||
"array_of_objects.[].string",
|
||||
"int",
|
||||
"object.array.[]",
|
||||
"object.string"}
|
||||
};
|
||||
EXPECT_EQ(allKeys, expectedKeys);
|
||||
}
|
||||
|
||||
struct ConfigFileJsonMakeTest : ConfigFileJsonTest {};
|
||||
|
||||
TEST_F(ConfigFileJsonMakeTest, invalidFile)
|
||||
|
||||
Reference in New Issue
Block a user