refactor: Change config section and key string literals into constants (#7095)

Co-authored-by: Bart <11445373+bthomee@users.noreply.github.com>
This commit is contained in:
Bart
2026-06-09 10:58:21 -04:00
committed by GitHub
parent c9769d1add
commit c552eb333f
83 changed files with 1262 additions and 1029 deletions

View File

@@ -8,6 +8,7 @@
#include <xrpl/basics/base64.h>
#include <xrpl/beast/test/yield_to.h>
#include <xrpl/beast/unit_test/suite.h>
#include <xrpl/config/Constants.h>
#include <xrpl/json/json_reader.h>
#include <xrpl/json/json_value.h>
#include <xrpl/json/to_string.h>
@@ -55,22 +56,23 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
static auto
makeConfig(std::string const& proto, bool admin = true, bool credentials = false)
{
auto const sectionName = boost::starts_with(proto, "h") ? "port_rpc" : "port_ws";
auto const sectionName =
boost::starts_with(proto, "h") ? Sections::kPortRpc : Sections::kPortWs;
auto p = jtx::envconfig();
p->overwrite(sectionName, "protocol", proto);
p->overwrite(sectionName, Keys::kProtocol, proto);
if (!admin)
p->overwrite(sectionName, "admin", "");
p->overwrite(sectionName, Keys::kAdmin, "");
if (credentials)
{
(*p)[sectionName].set("admin_password", "p");
(*p)[sectionName].set("admin_user", "u");
(*p)[sectionName].set(Keys::kAdminPassword, "p");
(*p)[sectionName].set(Keys::kAdminUser, "u");
}
p->overwrite(
boost::starts_with(proto, "h") ? "port_ws" : "port_rpc",
"protocol",
boost::starts_with(proto, "h") ? Sections::kPortWs : Sections::kPortRpc,
Keys::kProtocol,
boost::starts_with(proto, "h") ? "ws" : "http");
if (proto == "https")
@@ -78,11 +80,11 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
// this port is here to allow the env to create its internal client,
// which requires an http endpoint to talk to. In the connection
// failure test, this endpoint should never be used
(*p)["server"].append("port_alt");
(*p)["port_alt"].set("ip", getEnvLocalhostAddr());
(*p)["port_alt"].set("port", "7099");
(*p)["port_alt"].set("protocol", "http");
(*p)["port_alt"].set("admin", getEnvLocalhostAddr());
(*p)[Sections::kServer].append("port_alt");
(*p)["port_alt"].set(Keys::kIp, getEnvLocalhostAddr());
(*p)["port_alt"].set(Keys::kPort, "7099");
(*p)["port_alt"].set(Keys::kProtocol, "http");
(*p)["port_alt"].set(Keys::kAdmin, getEnvLocalhostAddr());
}
return p;
@@ -212,8 +214,8 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
boost::beast::http::response<boost::beast::http::string_body>& resp,
boost::system::error_code& ec)
{
auto const port = env.app().config()["port_ws"].get<std::uint16_t>("port");
auto ip = env.app().config()["port_ws"].get<std::string>("ip");
auto const port = env.app().config()[Sections::kPortWs].get<std::uint16_t>(Keys::kPort);
auto ip = env.app().config()[Sections::kPortWs].get<std::string>(Keys::kIp);
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
doRequest(yield, makeWSUpgrade(*ip, *port), *ip, *port, secure, resp, ec);
return;
@@ -229,8 +231,8 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
std::string const& body = "",
MyFields const& fields = {})
{
auto const port = env.app().config()["port_rpc"].get<std::uint16_t>("port");
auto const ip = env.app().config()["port_rpc"].get<std::string>("ip");
auto const port = env.app().config()[Sections::kPortRpc].get<std::uint16_t>(Keys::kPort);
auto const ip = env.app().config()[Sections::kPortRpc].get<std::string>(Keys::kIp);
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
doRequest(yield, makeHTTPRequest(*ip, *port, body, fields), *ip, *port, secure, resp, ec);
return;
@@ -298,12 +300,13 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
if (admin && credentials)
{
auto const user =
env.app().config()[protoWs ? "port_ws" : "port_rpc"].get<std::string>("admin_user");
auto const user = env.app()
.config()[protoWs ? Sections::kPortWs : Sections::kPortRpc]
.get<std::string>(Keys::kAdminUser);
auto const password =
env.app().config()[protoWs ? "port_ws" : "port_rpc"].get<std::string>(
"admin_password");
auto const password = env.app()
.config()[protoWs ? Sections::kPortWs : Sections::kPortRpc]
.get<std::string>(Keys::kAdminPassword);
// 1 - FAILS with wrong pass
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
@@ -368,7 +371,7 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
testcase("WS client to http server fails");
using namespace jtx;
Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
cfg->section("port_ws").set("protocol", "http,https");
cfg->section(Sections::kPortWs).set(Keys::kProtocol, "http,https");
return cfg;
})};
@@ -399,8 +402,8 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
testcase("Status request");
using namespace jtx;
Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
cfg->section("port_rpc").set("protocol", "ws2,wss2");
cfg->section("port_ws").set("protocol", "http");
cfg->section(Sections::kPortRpc).set(Keys::kProtocol, "ws2,wss2");
cfg->section(Sections::kPortWs).set(Keys::kProtocol, "http");
return cfg;
})};
@@ -433,12 +436,12 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
using namespace boost::asio;
using namespace boost::beast::http;
Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
cfg->section("port_ws").set("protocol", "ws2");
cfg->section(Sections::kPortWs).set(Keys::kProtocol, "ws2");
return cfg;
})};
auto const port = env.app().config()["port_ws"].get<std::uint16_t>("port");
auto const ip = env.app().config()["port_ws"].get<std::string>("ip");
auto const port = env.app().config()[Sections::kPortWs].get<std::uint16_t>(Keys::kPort);
auto const ip = env.app().config()[Sections::kPortWs].get<std::string>(Keys::kIp);
boost::system::error_code ec;
response<string_body> resp;
@@ -505,11 +508,11 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
using namespace test::jtx;
Env env{*this, envconfig([secure](std::unique_ptr<Config> cfg) {
(*cfg)["port_rpc"].set("user", "me");
(*cfg)["port_rpc"].set("password", "secret");
(*cfg)["port_rpc"].set("protocol", secure ? "https" : "http");
(*cfg)[Sections::kPortRpc].set(Keys::kUser, "me");
(*cfg)[Sections::kPortRpc].set(Keys::kPassword, "secret");
(*cfg)[Sections::kPortRpc].set(Keys::kProtocol, secure ? "https" : "http");
if (secure)
(*cfg)["port_ws"].set("protocol", "http,ws");
(*cfg)[Sections::kPortWs].set(Keys::kProtocol, "http,ws");
return cfg;
})};
@@ -533,11 +536,11 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
doHTTPRequest(env, yield, secure, resp, ec, to_string(jr), auth);
BEAST_EXPECT(resp.result() == boost::beast::http::status::forbidden);
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
auto const user = env.app().config().section("port_rpc").get<std::string>("user").value();
auto const pass =
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
env.app().config().section("port_rpc").get<std::string>("password").value();
auto const section = env.app().config().section(Sections::kPortRpc);
// NOLINTBEGIN(bugprone-unchecked-optional-access)
auto const user = section.get<std::string>(Keys::kUser).value();
auto const pass = section.get<std::string>(Keys::kPassword).value();
// NOLINTEND(bugprone-unchecked-optional-access)
// try with the correct user/pass, but not encoded
auth.set("Authorization", "Basic " + user + ":" + pass);
@@ -560,15 +563,15 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
using namespace boost::asio;
using namespace boost::beast::http;
Env env{*this, envconfig([&](std::unique_ptr<Config> cfg) {
(*cfg)["port_rpc"].set("limit", std::to_string(limit));
(*cfg)[Sections::kPortRpc].set(Keys::kLimit, std::to_string(limit));
return cfg;
})};
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
auto const port = env.app().config()["port_rpc"].get<std::uint16_t>("port").value();
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
auto const ip = env.app().config()["port_rpc"].get<std::string>("ip").value();
auto const section = env.app().config().section(Sections::kPortRpc);
// NOLINTBEGIN(bugprone-unchecked-optional-access)
auto const port = section.get<std::uint16_t>(Keys::kPort).value();
auto const ip = section.get<std::string>(Keys::kIp).value();
// NOLINTEND(bugprone-unchecked-optional-access)
boost::system::error_code ec;
io_context& ios = getIoContext();
@@ -620,14 +623,15 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
using namespace test::jtx;
Env env{*this, envconfig([](std::unique_ptr<Config> cfg) {
(*cfg)["port_ws"].set("protocol", "wss");
(*cfg)[Sections::kPortWs].set(Keys::kProtocol, "wss");
return cfg;
})};
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
auto const port = env.app().config()["port_ws"].get<std::uint16_t>("port").value();
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
auto const ip = env.app().config()["port_ws"].get<std::string>("ip").value();
auto const section = env.app().config().section(Sections::kPortWs);
// NOLINTBEGIN(bugprone-unchecked-optional-access)
auto const port = section.get<std::uint16_t>(Keys::kPort).value();
auto const ip = section.get<std::string>(Keys::kIp).value();
// NOLINTEND(bugprone-unchecked-optional-access)
boost::beast::http::response<boost::beast::http::string_body> resp;
boost::system::error_code ec;
doRequest(yield, makeWSUpgrade(ip, port), ip, port, true, resp, ec);
@@ -644,10 +648,11 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
using namespace test::jtx;
Env env{*this};
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
auto const port = env.app().config()["port_ws"].get<std::uint16_t>("port").value();
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
auto const ip = env.app().config()["port_ws"].get<std::string>("ip").value();
auto const section = env.app().config().section(Sections::kPortWs);
// NOLINTBEGIN(bugprone-unchecked-optional-access)
auto const port = section.get<std::uint16_t>(Keys::kPort).value();
auto const ip = section.get<std::string>(Keys::kIp).value();
// NOLINTEND(bugprone-unchecked-optional-access)
boost::beast::http::response<boost::beast::http::string_body> resp;
boost::system::error_code ec;
// body content is required here to avoid being
@@ -667,10 +672,11 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
using namespace boost::beast::http;
Env env{*this};
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
auto const port = env.app().config()["port_ws"].get<std::uint16_t>("port").value();
// NOLINTNEXTLINE(bugprone-unchecked-optional-access)
auto const ip = env.app().config()["port_ws"].get<std::string>("ip").value();
auto const section = env.app().config().section(Sections::kPortWs);
// NOLINTBEGIN(bugprone-unchecked-optional-access)
auto const port = section.get<std::uint16_t>(Keys::kPort).value();
auto const ip = section.get<std::string>(Keys::kIp).value();
// NOLINTEND(bugprone-unchecked-optional-access)
boost::system::error_code ec;
io_context& ios = getIoContext();
@@ -746,7 +752,7 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
*this,
validator(
envconfig([](std::unique_ptr<Config> cfg) {
cfg->section("port_rpc").set("protocol", "http");
cfg->section(Sections::kPortRpc).set(Keys::kProtocol, "http");
return cfg;
}),
"")};
@@ -774,8 +780,8 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
BEAST_EXPECT(env.app().getOPs().getConsensusInfo()["validating"] == true);
BEAST_EXPECT(!si[jss::state].isMember(jss::warnings));
auto const portWs = env.app().config()["port_ws"].get<std::uint16_t>("port");
auto const ipWs = env.app().config()["port_ws"].get<std::string>("ip");
auto const portWs = env.app().config()[Sections::kPortWs].get<std::uint16_t>(Keys::kPort);
auto const ipWs = env.app().config()[Sections::kPortWs].get<std::string>(Keys::kIp);
boost::system::error_code ec;
response<string_body> resp;
@@ -874,7 +880,7 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
*this,
validator(
envconfig([](std::unique_ptr<Config> cfg) {
cfg->section("port_rpc").set("protocol", "http");
cfg->section(Sections::kPortRpc).set(Keys::kProtocol, "http");
return cfg;
}),
"")};
@@ -902,8 +908,8 @@ class ServerStatus_test : public beast::unit_test::Suite, public beast::test::En
BEAST_EXPECT(env.app().getOPs().getConsensusInfo()["validating"] == true);
BEAST_EXPECT(!si[jss::state].isMember(jss::warnings));
auto const portWs = env.app().config()["port_ws"].get<std::uint16_t>("port");
auto const ipWs = env.app().config()["port_ws"].get<std::string>("ip");
auto const portWs = env.app().config()[Sections::kPortWs].get<std::uint16_t>(Keys::kPort);
auto const ipWs = env.app().config()[Sections::kPortWs].get<std::string>(Keys::kIp);
boost::system::error_code ec;
response<string_body> resp;

View File

@@ -4,11 +4,11 @@
#include <test/unit_test/SuiteJournal.h>
#include <xrpld/core/Config.h>
#include <xrpld/core/ConfigSections.h>
#include <xrpl/beast/rfc2616.h>
#include <xrpl/beast/unit_test/suite.h>
#include <xrpl/beast/utility/Journal.h>
#include <xrpl/config/Constants.h>
#include <xrpl/server/Handoff.h>
#include <xrpl/server/Port.h>
#include <xrpl/server/Server.h>
@@ -396,7 +396,7 @@ public:
Env const env{
*this,
envconfig([](std::unique_ptr<Config> cfg) {
(*cfg).deprecatedClearSection("port_rpc");
(*cfg).deprecatedClearSection(Sections::kPortRpc);
return cfg;
}),
std::make_unique<CaptureLogs>(&messages)};
@@ -407,8 +407,8 @@ public:
Env const env{
*this,
envconfig([](std::unique_ptr<Config> cfg) {
(*cfg).deprecatedClearSection("port_rpc");
(*cfg)["port_rpc"].set("ip", getEnvLocalhostAddr());
(*cfg).deprecatedClearSection(Sections::kPortRpc);
(*cfg)[Sections::kPortRpc].set(Keys::kIp, getEnvLocalhostAddr());
return cfg;
}),
std::make_unique<CaptureLogs>(&messages)};
@@ -419,9 +419,9 @@ public:
Env const env{
*this,
envconfig([](std::unique_ptr<Config> cfg) {
(*cfg).deprecatedClearSection("port_rpc");
(*cfg)["port_rpc"].set("ip", getEnvLocalhostAddr());
(*cfg)["port_rpc"].set("port", "0");
(*cfg).deprecatedClearSection(Sections::kPortRpc);
(*cfg)[Sections::kPortRpc].set(Keys::kIp, getEnvLocalhostAddr());
(*cfg)[Sections::kPortRpc].set(Keys::kPort, "0");
return cfg;
}),
std::make_unique<CaptureLogs>(&messages)};
@@ -433,7 +433,7 @@ public:
Env const env{
*this,
envconfig([](std::unique_ptr<Config> cfg) {
(*cfg)["server"].set("port", "0");
(*cfg)[Sections::kServer].set(Keys::kPort, "0");
return cfg;
}),
std::make_unique<CaptureLogs>(&messages)};
@@ -445,10 +445,10 @@ public:
Env const env{
*this,
envconfig([](std::unique_ptr<Config> cfg) {
(*cfg).deprecatedClearSection("port_rpc");
(*cfg)["port_rpc"].set("ip", getEnvLocalhostAddr());
(*cfg)["port_rpc"].set("port", "8081");
(*cfg)["port_rpc"].set("protocol", "");
(*cfg).deprecatedClearSection(Sections::kPortRpc);
(*cfg)[Sections::kPortRpc].set(Keys::kIp, getEnvLocalhostAddr());
(*cfg)[Sections::kPortRpc].set(Keys::kPort, "8081");
(*cfg)[Sections::kPortRpc].set(Keys::kProtocol, "");
return cfg;
}),
std::make_unique<CaptureLogs>(&messages)};
@@ -462,22 +462,22 @@ public:
*this,
envconfig([](std::unique_ptr<Config> cfg) {
cfg = std::make_unique<Config>();
cfg->overwrite(ConfigSection::nodeDatabase(), "type", "memory");
cfg->overwrite(ConfigSection::nodeDatabase(), "path", "main");
cfg->deprecatedClearSection(ConfigSection::importNodeDatabase());
cfg->legacy("database_path", "");
cfg->overwrite(Sections::kNodeDatabase, Keys::kType, "memory");
cfg->overwrite(Sections::kNodeDatabase, Keys::kPath, "main");
cfg->deprecatedClearSection(Sections::kImportNodeDatabase);
cfg->legacy(Sections::kDatabasePath, "");
cfg->setupControl(true, true, true);
(*cfg)["port_peer"].set("ip", getEnvLocalhostAddr());
(*cfg)["port_peer"].set("port", "8080");
(*cfg)["port_peer"].set("protocol", "peer");
(*cfg)["port_rpc"].set("ip", getEnvLocalhostAddr());
(*cfg)["port_rpc"].set("port", "8081");
(*cfg)["port_rpc"].set("protocol", "http,ws2");
(*cfg)["port_rpc"].set("admin", getEnvLocalhostAddr());
(*cfg)["port_ws"].set("ip", getEnvLocalhostAddr());
(*cfg)["port_ws"].set("port", "8082");
(*cfg)["port_ws"].set("protocol", "ws");
(*cfg)["port_ws"].set("admin", getEnvLocalhostAddr());
(*cfg)[Sections::kPortPeer].set(Keys::kIp, getEnvLocalhostAddr());
(*cfg)[Sections::kPortPeer].set(Keys::kPort, "8080");
(*cfg)[Sections::kPortPeer].set(Keys::kProtocol, "peer");
(*cfg)[Sections::kPortRpc].set(Keys::kIp, getEnvLocalhostAddr());
(*cfg)[Sections::kPortRpc].set(Keys::kPort, "8081");
(*cfg)[Sections::kPortRpc].set(Keys::kProtocol, "http,ws2");
(*cfg)[Sections::kPortRpc].set(Keys::kAdmin, getEnvLocalhostAddr());
(*cfg)[Sections::kPortWs].set(Keys::kIp, getEnvLocalhostAddr());
(*cfg)[Sections::kPortWs].set(Keys::kPort, "8082");
(*cfg)[Sections::kPortWs].set(Keys::kProtocol, "ws");
(*cfg)[Sections::kPortWs].set(Keys::kAdmin, getEnvLocalhostAddr());
return cfg;
}),
std::make_unique<CaptureLogs>(&messages)};
@@ -491,14 +491,14 @@ public:
*this,
envconfig([](std::unique_ptr<Config> cfg) {
cfg = std::make_unique<Config>();
cfg->overwrite(ConfigSection::nodeDatabase(), "type", "memory");
cfg->overwrite(ConfigSection::nodeDatabase(), "path", "main");
cfg->deprecatedClearSection(ConfigSection::importNodeDatabase());
cfg->legacy("database_path", "");
cfg->overwrite(Sections::kNodeDatabase, Keys::kType, "memory");
cfg->overwrite(Sections::kNodeDatabase, Keys::kPath, "main");
cfg->deprecatedClearSection(Sections::kImportNodeDatabase);
cfg->legacy(Sections::kDatabasePath, "");
cfg->setupControl(true, true, true);
(*cfg)["server"].append("port_peer");
(*cfg)["server"].append("port_rpc");
(*cfg)["server"].append("port_ws");
(*cfg)[Sections::kServer].append(Sections::kPortPeer);
(*cfg)[Sections::kServer].append(Sections::kPortRpc);
(*cfg)[Sections::kServer].append(Sections::kPortWs);
return cfg;
}),
std::make_unique<CaptureLogs>(&messages)};