|
2 | 2 | // Distributed under the MIT software license, see the accompanying
|
3 | 3 | // file COPYING or http://www.opensource.org/licenses/mit-license.php.
|
4 | 4 |
|
| 5 | +#include <interfaces/init.h> |
| 6 | +#include <ipc/capnp/protocol.h> |
| 7 | +#include <ipc/process.h> |
| 8 | +#include <ipc/protocol.h> |
5 | 9 | #include <logging.h>
|
6 | 10 | #include <mp/proxy-types.h>
|
7 | 11 | #include <test/ipc_test.capnp.h>
|
8 | 12 | #include <test/ipc_test.capnp.proxy.h>
|
9 | 13 | #include <test/ipc_test.h>
|
| 14 | +#include <tinyformat.h> |
10 | 15 |
|
11 | 16 | #include <future>
|
| 17 | +#include <thread> |
12 | 18 | #include <kj/common.h>
|
13 | 19 | #include <kj/memory.h>
|
14 | 20 | #include <kj/test.h>
|
| 21 | +#include <stdexcept> |
15 | 22 |
|
16 | 23 | #include <boost/test/unit_test.hpp>
|
17 | 24 |
|
| 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 | + |
18 | 45 | //! Unit test that tests execution of IPC calls without actually creating a
|
19 | 46 | //! separate process. This test is primarily intended to verify behavior of type
|
20 | 47 | //! conversion code that converts C++ objects to Cap'n Proto messages and vice
|
|
23 | 50 | //! The test creates a thread which creates a FooImplementation object (defined
|
24 | 51 | //! in ipc_test.h) and a two-way pipe accepting IPC requests which call methods
|
25 | 52 | //! on the object through FooInterface (defined in ipc_test.capnp).
|
26 |
| -void IpcTest() |
| 53 | +void IpcPipeTest() |
27 | 54 | {
|
28 | 55 | // Setup: create FooImplemention object and listen for FooInterface requests
|
29 | 56 | std::promise<std::unique_ptr<mp::ProxyClient<gen::FooInterface>>> foo_promise;
|
30 | 57 | std::function<void()> disconnect_client;
|
31 | 58 | 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); }); |
33 | 60 | auto pipe = loop.m_io_context.provider->newTwoWayPipe();
|
34 | 61 |
|
35 | 62 | auto connection_client = std::make_unique<mp::Connection>(loop, kj::mv(pipe.ends[0]));
|
@@ -65,3 +92,71 @@ void IpcTest()
|
65 | 92 | disconnect_client();
|
66 | 93 | thread.join();
|
67 | 94 | }
|
| 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 | +} |
0 commit comments