refactor: Create interface for DOSGuard (#1602)

For #919.
This commit is contained in:
Sergey Kuznetsov
2024-08-13 17:26:47 +01:00
committed by GitHub
parent 49e9d5eda0
commit b7c8ed7e3a
26 changed files with 535 additions and 270 deletions

View File

@@ -12,7 +12,6 @@ target_sources(
data/cassandra/ExecutionStrategyTests.cpp
data/cassandra/RetryPolicyTests.cpp
data/cassandra/SettingsProviderTests.cpp
DOSGuardTests.cpp
# ETL
etl/AmendmentBlockHandlerTests.cpp
etl/CacheLoaderSettingsTests.cpp
@@ -127,11 +126,12 @@ target_sources(
util/TxUtilTests.cpp
# Webserver
web/AdminVerificationTests.cpp
web/dosguard/DOSGuardTests.cpp
web/dosguard/IntervalSweepHandlerTests.cpp
web/dosguard/WhitelistHandlerTests.cpp
web/impl/ServerSslContextTests.cpp
web/RPCServerHandlerTests.cpp
web/ServerTests.cpp
web/IntervalSweepHandlerTests.cpp
web/WhitelistHandlerTests.cpp
# New Config
util/newconfig/ArrayViewTests.cpp
util/newconfig/ObjectViewTests.cpp

View File

@@ -24,10 +24,11 @@
#include "util/config/Config.hpp"
#include "util/prometheus/Label.hpp"
#include "util/prometheus/Prometheus.hpp"
#include "web/DOSGuard.hpp"
#include "web/IntervalSweepHandler.hpp"
#include "web/Server.hpp"
#include "web/WhitelistHandler.hpp"
#include "web/dosguard/DOSGuard.hpp"
#include "web/dosguard/DOSGuardInterface.hpp"
#include "web/dosguard/IntervalSweepHandler.hpp"
#include "web/dosguard/WhitelistHandler.hpp"
#include "web/impl/AdminVerificationStrategy.hpp"
#include "web/interface/ConnectionBase.hpp"
@@ -127,14 +128,14 @@ struct WebServerTest : NoLoggerFixture {
boost::asio::io_context ctxSync;
std::string const port = std::to_string(tests::util::generateFreePort());
Config cfg{generateJSONWithDynamicPort(port)};
WhitelistHandler whitelistHandler{cfg};
DOSGuard dosGuard{cfg, whitelistHandler};
IntervalSweepHandler sweepHandler{cfg, ctxSync, dosGuard};
dosguard::WhitelistHandler whitelistHandler{cfg};
dosguard::DOSGuard dosGuard{cfg, whitelistHandler};
dosguard::IntervalSweepHandler sweepHandler{cfg, ctxSync, dosGuard};
Config cfgOverload{generateJSONDataOverload(port)};
WhitelistHandler whitelistHandlerOverload{cfgOverload};
DOSGuard dosGuardOverload{cfgOverload, whitelistHandlerOverload};
IntervalSweepHandler sweepHandlerOverload{cfgOverload, ctxSync, dosGuardOverload};
dosguard::WhitelistHandler whitelistHandlerOverload{cfgOverload};
dosguard::DOSGuard dosGuardOverload{cfgOverload, whitelistHandlerOverload};
dosguard::IntervalSweepHandler sweepHandlerOverload{cfgOverload, ctxSync, dosGuardOverload};
// this ctx is for http server
boost::asio::io_context ctx;
@@ -178,7 +179,7 @@ std::shared_ptr<web::HttpServer<Executor>>
makeServerSync(
util::Config const& config,
boost::asio::io_context& ioc,
web::DOSGuard& dosGuard,
web::dosguard::DOSGuardInterface& dosGuard,
std::shared_ptr<Executor> const& handler
)
{

View File

@@ -19,7 +19,8 @@
#include "util/LoggerFixtures.hpp"
#include "util/config/Config.hpp"
#include "web/DOSGuard.hpp"
#include "web/dosguard/DOSGuard.hpp"
#include "web/dosguard/WhitelistHandlerInterface.hpp"
#include <boost/json/parse.hpp>
#include <gmock/gmock.h>
@@ -30,11 +31,11 @@
using namespace testing;
using namespace util;
using namespace std;
using namespace web;
using namespace web::dosguard;
namespace json = boost::json;
namespace {
constexpr auto JSONData = R"JSON(
struct DOSGuardTest : NoLoggerFixture {
static constexpr auto JSONData = R"JSON(
{
"dos_guard": {
"max_fetches": 100,
@@ -47,20 +48,15 @@ constexpr auto JSONData = R"JSON(
}
)JSON";
constexpr auto IP = "127.0.0.2";
static constexpr auto IP = "127.0.0.2";
struct MockWhitelistHandler {
MOCK_METHOD(bool, isWhiteListed, (std::string_view ip), (const));
};
struct MockWhitelistHandler : WhitelistHandlerInterface {
MOCK_METHOD(bool, isWhiteListed, (std::string_view ip), (const));
};
using MockWhitelistHandlerType = NiceMock<MockWhitelistHandler>;
}; // namespace
class DOSGuardTest : public NoLoggerFixture {
protected:
Config cfg{json::parse(JSONData)};
MockWhitelistHandlerType whitelistHandler;
BasicDOSGuard<MockWhitelistHandlerType> guard{cfg, whitelistHandler};
NiceMock<MockWhitelistHandler> whitelistHandler;
DOSGuard guard{cfg, whitelistHandler};
};
TEST_F(DOSGuardTest, Whitelisting)

View File

@@ -19,8 +19,8 @@
#include "util/AsioContextTestFixture.hpp"
#include "util/config/Config.hpp"
#include "web/DOSGuard.hpp"
#include "web/IntervalSweepHandler.hpp"
#include "web/dosguard/DOSGuardInterface.hpp"
#include "web/dosguard/IntervalSweepHandler.hpp"
#include <boost/json/parse.hpp>
#include <gmock/gmock.h>
@@ -28,7 +28,7 @@
#include <chrono>
using namespace web;
using namespace web::dosguard;
struct IntervalSweepHandlerTest : SyncAsioContextTest {
protected:

View File

@@ -18,7 +18,7 @@
//==============================================================================
#include "util/LoggerFixtures.hpp"
#include "util/config/Config.hpp"
#include "web/WhitelistHandler.hpp"
#include "web/dosguard/WhitelistHandler.hpp"
#include <boost/json/parse.hpp>
#include <gmock/gmock.h>
@@ -29,7 +29,7 @@
#include <vector>
using namespace util;
using namespace web;
using namespace web::dosguard;
struct WhitelistHandlerTest : NoLoggerFixture {};