From 8571c82cbc0275b7d28202cc329107511a66bc09 Mon Sep 17 00:00:00 2001 From: Bart Thomee <11445373+bthomee@users.noreply.github.com> Date: Thu, 16 Oct 2025 16:00:15 -0400 Subject: [PATCH] Fix tests, remove unused functions --- src/test/jtx/envconfig.h | 23 +--------- src/test/jtx/impl/JSONRPCClient.cpp | 14 +++++- src/test/jtx/impl/envconfig.cpp | 40 ++++++----------- src/test/server/ServerStatus_test.cpp | 7 ++- src/test/server/Server_test.cpp | 62 +++++++++++++-------------- 5 files changed, 61 insertions(+), 85 deletions(-) diff --git a/src/test/jtx/envconfig.h b/src/test/jtx/envconfig.h index 4005b6e34d..7e74b6b833 100644 --- a/src/test/jtx/envconfig.h +++ b/src/test/jtx/envconfig.h @@ -45,7 +45,7 @@ getRandomIPv4Loopback() // (except 127.0.0.0 and 127.255.255.255 that are reserved), which we should // use as the majority of our tests use IPv4. On macOS only 127.0.0.1 is // available as loopback address, unless the loopback interface is aliased - // to more addresses, e.g. `sudo ifconfig lo0 alias 127.0.0.* up`. + // to more addresses, e.g. `sudo ifconfig lo0 alias 127.0.0.2 up` etc. #if defined(__clang__) && defined(__APPLE__) return 0x7F000001; // 127.0.0.1 #else @@ -142,27 +142,6 @@ std::unique_ptr secure_gateway_localnet(std::unique_ptr); std::unique_ptr validator(std::unique_ptr, std::string const&); -/// @brief add a grpc address and port to config -/// -/// This is intended for use with envconfig, for tests that require a grpc -/// server. If this function is not called, grpc server will not start -/// -/// -/// @param cfg config instance to be modified -std::unique_ptr addGrpcConfig(std::unique_ptr); - -/// @brief add a grpc address, port and secure_gateway to config -/// -/// This is intended for use with envconfig, for tests that require a grpc -/// server. If this function is not called, grpc server will not start -/// -/// -/// @param cfg config instance to be modified -std::unique_ptr -addGrpcConfigWithSecureGateway( - std::unique_ptr, - std::string const& secureGateway); - std::unique_ptr makeConfig( std::map extraTxQ = {}, diff --git a/src/test/jtx/impl/JSONRPCClient.cpp b/src/test/jtx/impl/JSONRPCClient.cpp index a4c5817788..0376b26c30 100644 --- a/src/test/jtx/impl/JSONRPCClient.cpp +++ b/src/test/jtx/impl/JSONRPCClient.cpp @@ -18,6 +18,7 @@ //============================================================================== #include +#include #include #include @@ -54,8 +55,17 @@ class JSONRPCClient : public AbstractClient continue; using namespace boost::asio::ip; if (pp.ip && pp.ip->is_unspecified()) - *pp.ip = pp.ip->is_v6() ? address{address_v6::loopback()} - : address{address_v4::loopback()}; + { + if (pp.ip->is_v6()) + { + *pp.ip = address{address_v6::loopback()}; + } + else + { + auto const loopback_v4 = getRandomIPv4Loopback(); + *pp.ip = address{address_v4(loopback_v4)}; + } + } if (!pp.port) Throw("Use fixConfigPorts with auto ports"); diff --git a/src/test/jtx/impl/envconfig.cpp b/src/test/jtx/impl/envconfig.cpp index 624036196d..0d924c08e4 100644 --- a/src/test/jtx/impl/envconfig.cpp +++ b/src/test/jtx/impl/envconfig.cpp @@ -31,6 +31,10 @@ void setupConfigForUnitTests(Config& cfg) { using namespace jtx; + + // Use the loopback address for IPv4 or IPv6, as appropriate. + auto const& localhost = getEnvLocalhostAddr(); + // Default fees to old values, so tests don't have to worry about changes in // Config.h cfg.FEES.reference_fee = UNIT_TEST_REFERENCE_FEE; @@ -46,7 +50,7 @@ setupConfigForUnitTests(Config& cfg) cfg.legacy("database_path", ""); cfg.setupControl(true, true, true); cfg["server"].append(PORT_PEER); - cfg[PORT_PEER].set("ip", getEnvLocalhostAddr()); + cfg[PORT_PEER].set("ip", localhost); // Using port 0 asks the operating system to allocate an unused port, which // can be obtained after a "bind" call. @@ -57,14 +61,14 @@ setupConfigForUnitTests(Config& cfg) cfg[PORT_PEER].set("protocol", "peer"); cfg["server"].append(PORT_RPC); - cfg[PORT_RPC].set("ip", getEnvLocalhostAddr()); - cfg[PORT_RPC].set("admin", getEnvLocalhostAddr()); + cfg[PORT_RPC].set("ip", localhost); + cfg[PORT_RPC].set("admin", localhost); cfg[PORT_RPC].set("port", "0"); cfg[PORT_RPC].set("protocol", "http,ws2"); cfg["server"].append(PORT_WS); - cfg[PORT_WS].set("ip", getEnvLocalhostAddr()); - cfg[PORT_WS].set("admin", getEnvLocalhostAddr()); + cfg[PORT_WS].set("ip", localhost); + cfg[PORT_WS].set("admin", localhost); cfg[PORT_WS].set("port", "0"); cfg[PORT_WS].set("protocol", "ws"); cfg.SSL_VERIFY = false; @@ -83,9 +87,11 @@ no_admin(std::unique_ptr cfg) std::unique_ptr secure_gateway(std::unique_ptr cfg) { + // Use the same IP address as assigned to the other IP fields. + auto const localhost = (*cfg)[PORT_PEER].get("ip"); (*cfg)[PORT_RPC].set("admin", ""); (*cfg)[PORT_WS].set("admin", ""); - (*cfg)[PORT_RPC].set("secure_gateway", getEnvLocalhostAddr()); + (*cfg)[PORT_RPC].set("secure_gateway", *localhost); return cfg; } @@ -118,28 +124,6 @@ validator(std::unique_ptr cfg, std::string const& seed) return cfg; } -std::unique_ptr -addGrpcConfig(std::unique_ptr cfg) -{ - (*cfg)[SECTION_PORT_GRPC].set("ip", getEnvLocalhostAddr()); - (*cfg)[SECTION_PORT_GRPC].set("port", "0"); - return cfg; -} - -std::unique_ptr -addGrpcConfigWithSecureGateway( - std::unique_ptr cfg, - std::string const& secureGateway) -{ - (*cfg)[SECTION_PORT_GRPC].set("ip", getEnvLocalhostAddr()); - - // Check https://man7.org/linux/man-pages/man7/ip.7.html - // "ip_local_port_range" section for using 0 ports - (*cfg)[SECTION_PORT_GRPC].set("port", "0"); - (*cfg)[SECTION_PORT_GRPC].set("secure_gateway", secureGateway); - return cfg; -} - std::unique_ptr makeConfig( std::map extraTxQ, diff --git a/src/test/server/ServerStatus_test.cpp b/src/test/server/ServerStatus_test.cpp index 8bbad2cd99..0ddd5f16f5 100644 --- a/src/test/server/ServerStatus_test.cpp +++ b/src/test/server/ServerStatus_test.cpp @@ -80,14 +80,17 @@ class ServerStatus_test : public beast::unit_test::suite, if (proto == "https") { + // Use the same IP address as assigned to the other IP address + // fields. + auto const localhost = (*p)[PORT_PEER].get("ip"); // 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("ip", *localhost); (*p)["port_alt"].set("port", "7099"); (*p)["port_alt"].set("protocol", "http"); - (*p)["port_alt"].set("admin", getEnvLocalhostAddr()); + (*p)["port_alt"].set("admin", *localhost); } return p; diff --git a/src/test/server/Server_test.cpp b/src/test/server/Server_test.cpp index 874558f428..41391f00ed 100644 --- a/src/test/server/Server_test.cpp +++ b/src/test/server/Server_test.cpp @@ -466,37 +466,37 @@ public: messages.find("Missing 'protocol' in [port_rpc]") != std::string::npos); - except( - [&] // this creates a standard test config without the server - // section - { - Env env{ - *this, - envconfig([](std::unique_ptr cfg) { - cfg = std::make_unique(); - cfg->overwrite( - ConfigSection::nodeDatabase(), "type", "memory"); - cfg->overwrite( - ConfigSection::nodeDatabase(), "path", "main"); - cfg->deprecatedClearSection( - ConfigSection::importNodeDatabase()); - cfg->legacy("database_path", ""); - 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()); - return cfg; - }), - std::make_unique(&messages)}; - }); + except([&] // this creates a standard test config without the server + // section + { + Env env{ + *this, + envconfig([](std::unique_ptr cfg) { + auto const& localhost = getEnvLocalhostAddr(); + cfg = std::make_unique(); + cfg->overwrite( + ConfigSection::nodeDatabase(), "type", "memory"); + cfg->overwrite( + ConfigSection::nodeDatabase(), "path", "main"); + cfg->deprecatedClearSection( + ConfigSection::importNodeDatabase()); + cfg->legacy("database_path", ""); + cfg->setupControl(true, true, true); + (*cfg)["port_peer"].set("ip", localhost); + (*cfg)["port_peer"].set("port", "8080"); + (*cfg)["port_peer"].set("protocol", "peer"); + (*cfg)["port_rpc"].set("ip", localhost); + (*cfg)["port_rpc"].set("port", "8081"); + (*cfg)["port_rpc"].set("protocol", "http,ws2"); + (*cfg)["port_rpc"].set("admin", localhost); + (*cfg)["port_ws"].set("ip", localhost); + (*cfg)["port_ws"].set("port", "8082"); + (*cfg)["port_ws"].set("protocol", "ws"); + (*cfg)["port_ws"].set("admin", localhost); + return cfg; + }), + std::make_unique(&messages)}; + }); BEAST_EXPECT( messages.find("Required section [server] is missing") != std::string::npos);