mirror of
https://github.com/XRPLF/clio.git
synced 2026-04-29 15:37:53 +00:00
refactor: Use error code in make_address() calls (#3044)
Function `ip::make_address()` throws an exception on an invalid IP. Refactor to a better error handling without exceptions.
This commit is contained in:
@@ -81,7 +81,9 @@ struct RPCEngineTest : util::prometheus::WithPrometheus,
|
||||
|
||||
util::TagDecoratorFactory tagFactory{cfg};
|
||||
WorkQueue queue = WorkQueue::makeWorkQueue(cfg);
|
||||
web::dosguard::WhitelistHandler whitelistHandler{cfg};
|
||||
web::dosguard::WhitelistHandler whitelistHandler{
|
||||
web::dosguard::WhitelistHandler::create(cfg).value()
|
||||
};
|
||||
web::dosguard::Weights weights{1, {}};
|
||||
web::dosguard::DOSGuard dosGuard{cfg, whitelistHandler, weights};
|
||||
std::shared_ptr<MockHandlerProvider> handlerProvider = std::make_shared<MockHandlerProvider>();
|
||||
|
||||
@@ -150,13 +150,15 @@ struct WebServerTest : public virtual ::testing::Test {
|
||||
boost::asio::io_context ctxSync;
|
||||
std::string const port = std::to_string(tests::util::generateFreePort());
|
||||
ClioConfigDefinition cfg{getParseServerConfig(generateJSONWithDynamicPort(port))};
|
||||
dosguard::WhitelistHandler whitelistHandler{cfg};
|
||||
dosguard::WhitelistHandler whitelistHandler{dosguard::WhitelistHandler::create(cfg).value()};
|
||||
dosguard::Weights dosguardWeights{1, {}};
|
||||
dosguard::DOSGuard dosGuard{cfg, whitelistHandler, dosguardWeights};
|
||||
dosguard::IntervalSweepHandler sweepHandler{cfg, ctxSync, dosGuard};
|
||||
|
||||
ClioConfigDefinition cfgOverload{getParseServerConfig(generateJSONDataOverload(port))};
|
||||
dosguard::WhitelistHandler whitelistHandlerOverload{cfgOverload};
|
||||
dosguard::WhitelistHandler whitelistHandlerOverload{
|
||||
dosguard::WhitelistHandler::create(cfgOverload).value()
|
||||
};
|
||||
dosguard::DOSGuard dosGuardOverload{cfgOverload, whitelistHandlerOverload, dosguardWeights};
|
||||
dosguard::IntervalSweepHandler sweepHandlerOverload{cfgOverload, ctxSync, dosGuardOverload};
|
||||
// this ctx is for http server
|
||||
@@ -216,11 +218,26 @@ makeServerSync(
|
||||
std::reference_wrapper<data::LedgerCacheInterface const> cache
|
||||
)
|
||||
{
|
||||
return web::makeHttpServer(config, ioc, dosGuard, handler, cache);
|
||||
auto result = web::makeHttpServer(config, ioc, dosGuard, handler, cache);
|
||||
[&]() { ASSERT_TRUE(result.has_value()); }();
|
||||
return std::move(result).value();
|
||||
}
|
||||
|
||||
} // namespace
|
||||
|
||||
TEST_F(WebServerTest, InvalidIpAddress)
|
||||
{
|
||||
auto jsonConfig = generateJSONWithDynamicPort(port);
|
||||
jsonConfig.as_object()["server"].as_object()["ip"] = "not-an-ip";
|
||||
|
||||
auto cache = MockLedgerCache();
|
||||
auto const e = std::make_shared<EchoExecutor>();
|
||||
auto const result =
|
||||
web::makeHttpServer(getParseServerConfig(jsonConfig), ctx, dosGuard, e, cache);
|
||||
ASSERT_FALSE(result.has_value());
|
||||
EXPECT_THAT(result.error(), testing::HasSubstr("Invalid 'server.ip' config value"));
|
||||
}
|
||||
|
||||
TEST_F(WebServerTest, Http)
|
||||
{
|
||||
auto cache = MockLedgerCache();
|
||||
@@ -294,8 +311,9 @@ TEST_F(WebServerTest, IncompleteSslConfig)
|
||||
jsonConfig.as_object()["ssl_key_file"] = sslKeyFile.path;
|
||||
|
||||
auto cache = MockLedgerCache();
|
||||
auto const server = makeServerSync(getParseServerConfig(jsonConfig), ctx, dosGuard, e, cache);
|
||||
EXPECT_EQ(server, nullptr);
|
||||
auto const result =
|
||||
web::makeHttpServer(getParseServerConfig(jsonConfig), ctx, dosGuard, e, cache);
|
||||
EXPECT_FALSE(result.has_value());
|
||||
}
|
||||
|
||||
TEST_F(WebServerTest, WrongSslConfig)
|
||||
@@ -307,8 +325,9 @@ TEST_F(WebServerTest, WrongSslConfig)
|
||||
jsonConfig.as_object()["ssl_cert_file"] = "wrong_path";
|
||||
|
||||
auto cache = MockLedgerCache();
|
||||
auto const server = makeServerSync(getParseServerConfig(jsonConfig), ctx, dosGuard, e, cache);
|
||||
EXPECT_EQ(server, nullptr);
|
||||
auto const result =
|
||||
web::makeHttpServer(getParseServerConfig(jsonConfig), ctx, dosGuard, e, cache);
|
||||
EXPECT_FALSE(result.has_value());
|
||||
}
|
||||
|
||||
TEST_F(WebServerTest, Https)
|
||||
@@ -694,9 +713,8 @@ TEST_F(WebServerTest, AdminErrorCfgTestBothAdminPasswordAndLocalAdminSet)
|
||||
)};
|
||||
|
||||
MockLedgerCache cache;
|
||||
EXPECT_THROW(
|
||||
web::makeHttpServer(serverConfig, ctx, dosGuardOverload, e, cache), std::logic_error
|
||||
);
|
||||
auto const result = web::makeHttpServer(serverConfig, ctx, dosGuardOverload, e, cache);
|
||||
EXPECT_FALSE(result.has_value());
|
||||
}
|
||||
|
||||
TEST_F(WebServerTest, AdminErrorCfgTestBothAdminPasswordAndLocalAdminFalse)
|
||||
@@ -719,9 +737,8 @@ TEST_F(WebServerTest, AdminErrorCfgTestBothAdminPasswordAndLocalAdminFalse)
|
||||
)};
|
||||
|
||||
MockLedgerCache cache;
|
||||
EXPECT_THROW(
|
||||
web::makeHttpServer(serverConfig, ctx, dosGuardOverload, e, cache), std::logic_error
|
||||
);
|
||||
auto const result = web::makeHttpServer(serverConfig, ctx, dosGuardOverload, e, cache);
|
||||
EXPECT_FALSE(result.has_value());
|
||||
}
|
||||
|
||||
struct WebServerPrometheusTest : util::prometheus::WithPrometheus, WebServerTest {};
|
||||
|
||||
@@ -11,6 +11,7 @@
|
||||
#include <gmock/gmock.h>
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <expected>
|
||||
#include <string>
|
||||
#include <string_view>
|
||||
#include <vector>
|
||||
@@ -62,7 +63,9 @@ TEST_F(WhitelistHandlerTest, TestWhiteListIPV4)
|
||||
ClioConfigDefinition const cfg{
|
||||
getParseWhitelistHandlerConfig(boost::json::parse(kJSON_DATA_IP_V4))
|
||||
};
|
||||
WhitelistHandler const whitelistHandler{cfg, mockResolver};
|
||||
auto const result = WhitelistHandler::create(cfg, mockResolver);
|
||||
ASSERT_TRUE(result.has_value());
|
||||
auto const& whitelistHandler = *result;
|
||||
|
||||
EXPECT_TRUE(whitelistHandler.isWhiteListed("192.168.1.10"));
|
||||
EXPECT_FALSE(whitelistHandler.isWhiteListed("193.168.0.123"));
|
||||
@@ -86,7 +89,9 @@ TEST_F(WhitelistHandlerTest, TestWhiteListResolvesHostname)
|
||||
ClioConfigDefinition const cfg{
|
||||
getParseWhitelistHandlerConfig(boost::json::parse(kJSON_DATA_IP_V4))
|
||||
};
|
||||
WhitelistHandler const whitelistHandler{cfg};
|
||||
auto const result = WhitelistHandler::create(cfg);
|
||||
ASSERT_TRUE(result.has_value());
|
||||
auto const& whitelistHandler = *result;
|
||||
|
||||
EXPECT_TRUE(whitelistHandler.isWhiteListed("127.0.0.1"));
|
||||
EXPECT_FALSE(whitelistHandler.isWhiteListed("193.168.0.123"));
|
||||
@@ -110,10 +115,69 @@ TEST_F(WhitelistHandlerTest, TestWhiteListIPV6)
|
||||
ClioConfigDefinition const cfg{
|
||||
getParseWhitelistHandlerConfig(boost::json::parse(kJSON_DATA_IP_V6))
|
||||
};
|
||||
WhitelistHandler const whitelistHandler{cfg};
|
||||
auto const result = WhitelistHandler::create(cfg);
|
||||
ASSERT_TRUE(result.has_value());
|
||||
auto const& whitelistHandler = *result;
|
||||
|
||||
EXPECT_TRUE(whitelistHandler.isWhiteListed("2002:1dd8:85a7:0000:0000:8a6e:0000:1111"));
|
||||
EXPECT_FALSE(whitelistHandler.isWhiteListed("2002:1dd8:85a7:1101:0000:8a6e:0000:1111"));
|
||||
EXPECT_TRUE(whitelistHandler.isWhiteListed("2001:0db8:85a3:0000:0000:8a2e:0000:0000"));
|
||||
EXPECT_TRUE(whitelistHandler.isWhiteListed("2001:0db8:85a3:0000:1111:8a2e:0370:7334"));
|
||||
}
|
||||
|
||||
struct WhitelistTest : public virtual ::testing::Test {};
|
||||
|
||||
TEST_F(WhitelistTest, AddValidIPV4)
|
||||
{
|
||||
Whitelist whitelist;
|
||||
EXPECT_TRUE(whitelist.add("1.2.3.4").has_value());
|
||||
}
|
||||
|
||||
TEST_F(WhitelistTest, AddInvalidIP)
|
||||
{
|
||||
Whitelist whitelist;
|
||||
auto const result = whitelist.add("not-an-ip");
|
||||
ASSERT_FALSE(result.has_value());
|
||||
EXPECT_THAT(result.error(), testing::HasSubstr("not-an-ip"));
|
||||
}
|
||||
|
||||
TEST_F(WhitelistTest, AddInvalidNetwork)
|
||||
{
|
||||
Whitelist whitelist;
|
||||
auto const result = whitelist.add("not-a-net/24");
|
||||
ASSERT_FALSE(result.has_value());
|
||||
EXPECT_THAT(result.error(), testing::HasSubstr("not-a-net/24"));
|
||||
}
|
||||
|
||||
TEST_F(WhitelistTest, IsWhiteListedWithInvalidIP)
|
||||
{
|
||||
Whitelist whitelist;
|
||||
EXPECT_FALSE(whitelist.isWhiteListed("not-an-ip"));
|
||||
}
|
||||
|
||||
TEST_F(WhitelistHandlerTest, CreateWithInvalidIPFails)
|
||||
{
|
||||
struct MockResolver {
|
||||
MOCK_METHOD(std::vector<std::string>, resolve, (std::string_view, std::string_view));
|
||||
MOCK_METHOD(std::vector<std::string>, resolve, (std::string_view));
|
||||
};
|
||||
|
||||
static constexpr auto kJSON = R"JSON(
|
||||
{
|
||||
"dos_guard": {
|
||||
"whitelist": ["not-an-ip"]
|
||||
}
|
||||
}
|
||||
)JSON";
|
||||
|
||||
testing::StrictMock<MockResolver> mockResolver;
|
||||
EXPECT_CALL(mockResolver, resolve(testing::_))
|
||||
.WillOnce([](auto hostname) -> std::vector<std::string> {
|
||||
return {std::string{hostname}};
|
||||
});
|
||||
|
||||
ClioConfigDefinition const cfg{getParseWhitelistHandlerConfig(boost::json::parse(kJSON))};
|
||||
auto const result = WhitelistHandler::create(cfg, mockResolver);
|
||||
ASSERT_FALSE(result.has_value());
|
||||
EXPECT_THAT(result.error(), testing::HasSubstr("not-an-ip"));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user