http: Introduce HTTPServer class and implement binding to listening socket

Introduce a new low-level socket managing class `HTTPServer`.

BindAndStartListening() was copied from CConnMan's BindListenPort()
in net.cpp and modernized.

Unit-test it with a new class `SocketTestingSetup` which mocks
`CreateSock()` and will enable mock client I/O in future commits.

Co-authored-by: Vasil Dimov <vd@FreeBSD.org>
This commit is contained in:
Matthew Zipkin
2025-06-04 13:52:56 -04:00
parent 9463e98781
commit f5bc018948
5 changed files with 178 additions and 1 deletions

View File

@@ -13,11 +13,12 @@
using http_bitcoin::HTTPHeaders;
using http_bitcoin::HTTPRequest;
using http_bitcoin::HTTPResponse;
using http_bitcoin::HTTPServer;
using http_bitcoin::MAX_BODY_SIZE;
using http_bitcoin::MAX_HEADERS_SIZE;
using util::LineReader;
BOOST_FIXTURE_TEST_SUITE(httpserver_tests, BasicTestingSetup)
BOOST_FIXTURE_TEST_SUITE(httpserver_tests, SocketTestingSetup)
BOOST_AUTO_TEST_CASE(test_query_parameters)
{
@@ -372,4 +373,27 @@ BOOST_AUTO_TEST_CASE(http_request_tests)
BOOST_CHECK(!req.LoadBody(reader));
}
}
BOOST_AUTO_TEST_CASE(http_server_socket_tests)
{
HTTPServer server;
{
// We can only bind to NET_IPV4 and NET_IPV6
CService onion_address{Lookup("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaam2dqd.onion", /*portDefault=*/0, /*fAllowLookup=*/false).value()};
auto result{server.BindAndStartListening(onion_address)};
BOOST_REQUIRE(!result);
BOOST_CHECK_EQUAL(result.error(), "Bind address family for aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaam2dqd.onion:0 not supported");
}
// This VALID address won't actually get used because we stubbed CreateSock()
CService addr_bind{Lookup("0.0.0.0", /*portDefault=*/0, /*fAllowLookup=*/false).value()};
// Init state
BOOST_REQUIRE_EQUAL(server.GetListeningSocketCount(), 0);
// Bind to mock Listening Socket
BOOST_REQUIRE(server.BindAndStartListening(addr_bind));
// We are bound and listening
BOOST_REQUIRE_EQUAL(server.GetListeningSocketCount(), 1);
}
BOOST_AUTO_TEST_SUITE_END()