Skip to content

Commit 73fe7d7

Browse files
committed
multiprocess: Add unit tests for connect, serve, and listen functions
1 parent 955d407 commit 73fe7d7

File tree

6 files changed

+142
-8
lines changed

6 files changed

+142
-8
lines changed

src/ipc/capnp/protocol.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,11 +61,12 @@ class CapnpProtocol : public Protocol
6161
}
6262
mp::ListenConnections<messages::Init>(*m_loop, listen_fd, init);
6363
}
64-
void serve(int fd, const char* exe_name, interfaces::Init& init) override
64+
void serve(int fd, const char* exe_name, interfaces::Init& init, const std::function<void()>& ready_fn = {}) override
6565
{
6666
assert(!m_loop);
6767
mp::g_thread_context.thread_name = mp::ThreadName(exe_name);
6868
m_loop.emplace(exe_name, &IpcLogFn, &m_context);
69+
if (ready_fn) ready_fn();
6970
mp::ServeStream<messages::Init>(*m_loop, fd, init);
7071
m_loop->loop();
7172
m_loop.reset();

src/ipc/protocol.h

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,13 @@ class Protocol
5050
//! created by them. This isn't really a problem because serve() is only
5151
//! called by spawned child processes that call it immediately to
5252
//! communicate back with parent processes.
53-
virtual void serve(int fd, const char* exe_name, interfaces::Init& init) = 0;
53+
//
54+
//! The optional `ready_fn` callback will be called after the event loop is
55+
//! created but before it is started. This can be useful in tests to trigger
56+
//! client connections from another thread as soon as the event loop is
57+
//! available, but should not be neccessary in normal code which starts
58+
//! clients and servers independently.
59+
virtual void serve(int fd, const char* exe_name, interfaces::Init& init, const std::function<void()>& ready_fn = {}) = 0;
5460

5561
//! Add cleanup callback to interface that will run when the interface is
5662
//! deleted.

src/test/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,7 @@ if(WITH_MULTIPROCESS)
177177
PRIVATE
178178
ipc_tests.cpp
179179
)
180-
target_link_libraries(test_bitcoin bitcoin_ipc_test)
180+
target_link_libraries(test_bitcoin bitcoin_ipc_test bitcoin_ipc)
181181
endif()
182182

183183
function(add_boost_test source_file)

src/test/ipc_test.cpp

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,46 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <interfaces/init.h>
6+
#include <ipc/capnp/protocol.h>
7+
#include <ipc/process.h>
8+
#include <ipc/protocol.h>
59
#include <logging.h>
610
#include <mp/proxy-types.h>
711
#include <test/ipc_test.capnp.h>
812
#include <test/ipc_test.capnp.proxy.h>
913
#include <test/ipc_test.h>
14+
#include <tinyformat.h>
1015

1116
#include <future>
17+
#include <thread>
1218
#include <kj/common.h>
1319
#include <kj/memory.h>
1420
#include <kj/test.h>
21+
#include <stdexcept>
1522

1623
#include <boost/test/unit_test.hpp>
1724

25+
//! Remote init class.
26+
class TestInit : public interfaces::Init
27+
{
28+
public:
29+
std::unique_ptr<interfaces::Echo> makeEcho() override { return interfaces::MakeEcho(); }
30+
};
31+
32+
//! Generate a temporary path with temp_directory_path and mkstemp
33+
static std::string TempPath(std::string_view pattern)
34+
{
35+
std::string temp{fs::PathToString(fs::path{fs::temp_directory_path()} / fs::PathFromString(std::string{pattern}))};
36+
temp.push_back('\0');
37+
int fd{mkstemp(temp.data())};
38+
BOOST_CHECK_GE(fd, 0);
39+
BOOST_CHECK_EQUAL(close(fd), 0);
40+
temp.resize(temp.size() - 1);
41+
fs::remove(fs::PathFromString(temp));
42+
return temp;
43+
}
44+
1845
//! Unit test that tests execution of IPC calls without actually creating a
1946
//! separate process. This test is primarily intended to verify behavior of type
2047
//! conversion code that converts C++ objects to Cap'n Proto messages and vice
@@ -23,13 +50,13 @@
2350
//! The test creates a thread which creates a FooImplementation object (defined
2451
//! in ipc_test.h) and a two-way pipe accepting IPC requests which call methods
2552
//! on the object through FooInterface (defined in ipc_test.capnp).
26-
void IpcTest()
53+
void IpcPipeTest()
2754
{
2855
// Setup: create FooImplemention object and listen for FooInterface requests
2956
std::promise<std::unique_ptr<mp::ProxyClient<gen::FooInterface>>> foo_promise;
3057
std::function<void()> disconnect_client;
3158
std::thread thread([&]() {
32-
mp::EventLoop loop("IpcTest", [](bool raise, const std::string& log) { LogPrintf("LOG%i: %s\n", raise, log); });
59+
mp::EventLoop loop("IpcPipeTest", [](bool raise, const std::string& log) { LogPrintf("LOG%i: %s\n", raise, log); });
3360
auto pipe = loop.m_io_context.provider->newTwoWayPipe();
3461

3562
auto connection_client = std::make_unique<mp::Connection>(loop, kj::mv(pipe.ends[0]));
@@ -65,3 +92,71 @@ void IpcTest()
6592
disconnect_client();
6693
thread.join();
6794
}
95+
96+
//! Test ipc::Protocol connect() and serve() methods connecting over a socketpair.
97+
void IpcSocketPairTest()
98+
{
99+
int fds[2];
100+
BOOST_CHECK_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
101+
std::unique_ptr<interfaces::Init> init{std::make_unique<TestInit>()};
102+
std::unique_ptr<ipc::Protocol> protocol{ipc::capnp::MakeCapnpProtocol()};
103+
std::promise<void> promise;
104+
std::thread thread([&]() {
105+
protocol->serve(fds[0], "test-serve", *init, [&] { promise.set_value(); });
106+
});
107+
promise.get_future().wait();
108+
std::unique_ptr<interfaces::Init> remote_init{protocol->connect(fds[1], "test-connect")};
109+
std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
110+
BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test");
111+
remote_echo.reset();
112+
remote_init.reset();
113+
thread.join();
114+
}
115+
116+
//! Test ipc::Process bind() and connect() methods connecting over a unix socket.
117+
void IpcSocketTest(const fs::path& datadir)
118+
{
119+
std::unique_ptr<interfaces::Init> init{std::make_unique<TestInit>()};
120+
std::unique_ptr<ipc::Protocol> protocol{ipc::capnp::MakeCapnpProtocol()};
121+
std::unique_ptr<ipc::Process> process{ipc::MakeProcess()};
122+
123+
std::string invalid_bind{"invalid:"};
124+
BOOST_CHECK_THROW(process->bind(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
125+
BOOST_CHECK_THROW(process->connect(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
126+
127+
auto bind_and_listen{[&](const std::string& bind_address) {
128+
std::string address{bind_address};
129+
int serve_fd = process->bind(datadir, "test_bitcoin", address);
130+
BOOST_CHECK_GE(serve_fd, 0);
131+
BOOST_CHECK_EQUAL(address, bind_address);
132+
protocol->listen(serve_fd, "test-serve", *init);
133+
}};
134+
135+
auto connect_and_test{[&](const std::string& connect_address) {
136+
std::string address{connect_address};
137+
int connect_fd{process->connect(datadir, "test_bitcoin", address)};
138+
BOOST_CHECK_EQUAL(address, connect_address);
139+
std::unique_ptr<interfaces::Init> remote_init{protocol->connect(connect_fd, "test-connect")};
140+
std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
141+
BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test");
142+
}};
143+
144+
// Need to specify explicit socket addresses outside the data directory, because the data
145+
// directory path is so long that the default socket address and any other
146+
// addresses in the data directory would fail with errors like:
147+
// Address 'unix' path '"/tmp/test_common_Bitcoin Core/ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff/test_bitcoin.sock"' exceeded maximum socket path length
148+
std::vector<std::string> addresses{
149+
strprintf("unix:%s", TempPath("bitcoin_sock0_XXXXXX")),
150+
strprintf("unix:%s", TempPath("bitcoin_sock1_XXXXXX")),
151+
};
152+
153+
// Bind and listen on multiple addresses
154+
for (const auto& address : addresses) {
155+
bind_and_listen(address);
156+
}
157+
158+
// Connect and test each address multiple times.
159+
for (int i : {0, 1, 0, 0, 1}) {
160+
connect_and_test(addresses[i]);
161+
}
162+
}

src/test/ipc_test.h

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
#include <primitives/transaction.h>
99
#include <univalue.h>
10+
#include <util/fs.h>
1011

1112
class FooImplementation
1213
{
@@ -16,6 +17,8 @@ class FooImplementation
1617
UniValue passUniValue(UniValue v) { return v; }
1718
};
1819

19-
void IpcTest();
20+
void IpcPipeTest();
21+
void IpcSocketPairTest();
22+
void IpcSocketTest(const fs::path& datadir);
2023

2124
#endif // BITCOIN_TEST_IPC_TEST_H

src/test/ipc_tests.cpp

Lines changed: 31 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,41 @@
22
// Distributed under the MIT software license, see the accompanying
33
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
44

5+
#include <ipc/process.h>
56
#include <test/ipc_test.h>
7+
8+
#include <test/util/setup_common.h>
69
#include <boost/test/unit_test.hpp>
710

8-
BOOST_AUTO_TEST_SUITE(ipc_tests)
11+
BOOST_FIXTURE_TEST_SUITE(ipc_tests, BasicTestingSetup)
912
BOOST_AUTO_TEST_CASE(ipc_tests)
1013
{
11-
IpcTest();
14+
IpcPipeTest();
15+
IpcSocketPairTest();
16+
IpcSocketTest(m_args.GetDataDirNet());
1217
}
18+
19+
// Test address parsing.
20+
BOOST_AUTO_TEST_CASE(parse_address_test)
21+
{
22+
std::unique_ptr<ipc::Process> process{ipc::MakeProcess()};
23+
fs::path datadir{"/var/empty/notexist"};
24+
auto check_notexist{[](const std::system_error& e) { return e.code() == std::errc::no_such_file_or_directory; }};
25+
auto check_address{[&](std::string address, std::string expect_address, std::string expect_error) {
26+
if (expect_error.empty()) {
27+
BOOST_CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::system_error, check_notexist);
28+
} else {
29+
BOOST_CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::invalid_argument, HasReason(expect_error));
30+
}
31+
BOOST_CHECK_EQUAL(address, expect_address);
32+
}};
33+
check_address("unix", "unix:/var/empty/notexist/test_bitcoin.sock", "");
34+
check_address("unix:", "unix:/var/empty/notexist/test_bitcoin.sock", "");
35+
check_address("unix:path.sock", "unix:/var/empty/notexist/path.sock", "");
36+
check_address("unix:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.sock",
37+
"unix:/var/empty/notexist/0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.sock",
38+
"Unix address path \"/var/empty/notexist/0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.sock\" exceeded maximum socket path length");
39+
check_address("invalid", "invalid", "Unrecognized address 'invalid'");
40+
}
41+
1342
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)