Branch data Line data Source code
1 : : // Copyright (c) 2023-present The Bitcoin Core developers
2 : : // Distributed under the MIT software license, see the accompanying
3 : : // file COPYING or http://www.opensource.org/licenses/mit-license.php.
4 : :
5 : : #include <interfaces/init.h>
6 : : #include <ipc/capnp/mining.capnp.h>
7 : : #include <ipc/capnp/protocol.h>
8 : : #include <ipc/process.h>
9 : : #include <ipc/protocol.h>
10 : : #include <ipc/test/ipc_test.capnp.h>
11 : : #include <ipc/test/ipc_test.capnp.proxy.h>
12 : : #include <ipc/test/ipc_test.h>
13 : : #include <mp/proxy-types.h>
14 : : #include <test/util/common.h>
15 : : #include <test/util/setup_common.h>
16 : : #include <tinyformat.h>
17 : : #include <util/log.h>
18 : : #include <validation.h>
19 : :
20 : : #include <future>
21 : : #include <thread>
22 : : #include <kj/common.h>
23 : : #include <kj/memory.h>
24 : : #include <kj/test.h>
25 : : #include <stdexcept>
26 : :
27 : : #include <boost/test/unit_test.hpp>
28 : :
29 : : static_assert(ipc::capnp::messages::MAX_MONEY == MAX_MONEY);
30 : : static_assert(ipc::capnp::messages::MAX_DOUBLE == std::numeric_limits<double>::max());
31 : : static_assert(ipc::capnp::messages::DEFAULT_BLOCK_RESERVED_WEIGHT == DEFAULT_BLOCK_RESERVED_WEIGHT);
32 : : static_assert(ipc::capnp::messages::DEFAULT_COINBASE_OUTPUT_MAX_ADDITIONAL_SIGOPS == DEFAULT_COINBASE_OUTPUT_MAX_ADDITIONAL_SIGOPS);
33 : :
34 : : //! Remote init class.
35 : 2 : class TestInit : public interfaces::Init
36 : : {
37 : : public:
38 : 6 : std::unique_ptr<interfaces::Echo> makeEcho() override { return interfaces::MakeEcho(); }
39 : : };
40 : :
41 : : //! Generate a temporary path with temp_directory_path and mkstemp
42 : 2 : static std::string TempPath(std::string_view pattern)
43 : : {
44 [ + - + - : 16 : std::string temp{fs::PathToString(fs::path{fs::temp_directory_path()} / fs::PathFromString(std::string{pattern}))};
+ - - + ]
45 [ + - ]: 2 : temp.push_back('\0');
46 [ + - ]: 2 : int fd{mkstemp(temp.data())};
47 [ + - + - ]: 2 : BOOST_CHECK_GE(fd, 0);
48 [ + - + - : 2 : BOOST_CHECK_EQUAL(close(fd), 0);
+ - ]
49 [ - + + - ]: 2 : temp.resize(temp.size() - 1);
50 [ + - + - ]: 2 : fs::remove(fs::PathFromString(temp));
51 : 2 : return temp;
52 : 0 : }
53 : :
54 : : //! Unit test that tests execution of IPC calls without actually creating a
55 : : //! separate process. This test is primarily intended to verify behavior of type
56 : : //! conversion code that converts C++ objects to Cap'n Proto messages and vice
57 : : //! versa.
58 : : //!
59 : : //! The test creates a thread which creates a FooImplementation object (defined
60 : : //! in ipc_test.h) and a two-way pipe accepting IPC requests which call methods
61 : : //! on the object through FooInterface (defined in ipc_test.capnp).
62 : 1 : void IpcPipeTest()
63 : : {
64 : : // Setup: create FooImplementation object and listen for FooInterface requests
65 : 1 : std::promise<std::unique_ptr<mp::ProxyClient<gen::FooInterface>>> foo_promise;
66 : 2 : std::thread thread([&]() {
67 [ + - ]: 68 : mp::EventLoop loop("IpcPipeTest", [](bool raise, const std::string& log) { LogInfo("LOG%i: %s", raise, log); });
68 [ + - ]: 1 : auto pipe = loop.m_io_context.provider->newTwoWayPipe();
69 : :
70 [ + - ]: 1 : auto connection_client = std::make_unique<mp::Connection>(loop, kj::mv(pipe.ends[0]));
71 : 1 : auto foo_client = std::make_unique<mp::ProxyClient<gen::FooInterface>>(
72 [ + - + - : 1 : connection_client->m_rpc_system->bootstrap(mp::ServerVatId().vat_id).castAs<gen::FooInterface>(),
+ - + - ]
73 [ + - + - ]: 2 : connection_client.get(), /* destroy_connection= */ true);
74 [ + - ]: 1 : (void)connection_client.release();
75 [ + - ]: 1 : foo_promise.set_value(std::move(foo_client));
76 : :
77 : 2 : auto connection_server = std::make_unique<mp::Connection>(loop, kj::mv(pipe.ends[1]), [&](mp::Connection& connection) {
78 [ + - ]: 1 : auto foo_server = kj::heap<mp::ProxyServer<gen::FooInterface>>(std::make_shared<FooImplementation>(), connection);
79 [ + - + - ]: 1 : return capnp::Capability::Client(kj::mv(foo_server));
80 [ + - ]: 2 : });
81 [ + - + - ]: 2 : connection_server->onDisconnect([&] { connection_server.reset(); });
82 [ + - ]: 1 : loop.loop();
83 [ + - + - ]: 2 : });
84 [ + - + - ]: 2 : std::unique_ptr<mp::ProxyClient<gen::FooInterface>> foo{foo_promise.get_future().get()};
85 : :
86 : : // Test: make sure arguments were sent and return value is received
87 [ + - + - : 1 : BOOST_CHECK_EQUAL(foo->add(1, 2), 3);
+ - ]
88 : :
89 [ + - ]: 1 : COutPoint txout1{Txid::FromUint256(uint256{100}), 200};
90 [ + - ]: 1 : COutPoint txout2{foo->passOutPoint(txout1)};
91 [ + - + - ]: 2 : BOOST_CHECK(txout1 == txout2);
92 : :
93 : 1 : UniValue uni1{UniValue::VOBJ};
94 [ + - + - : 2 : uni1.pushKV("i", 1);
+ - ]
95 [ + - + - : 2 : uni1.pushKV("s", "two");
+ - ]
96 [ + - + - ]: 1 : UniValue uni2{foo->passUniValue(uni1)};
97 [ + - + - : 1 : BOOST_CHECK_EQUAL(uni1.write(), uni2.write());
+ - + - ]
98 : :
99 [ + - ]: 1 : CMutableTransaction mtx;
100 : 1 : mtx.version = 2;
101 : 1 : mtx.nLockTime = 3;
102 [ + - ]: 1 : mtx.vin.emplace_back(txout1);
103 [ + - ]: 1 : mtx.vout.emplace_back(COIN, CScript());
104 [ + - ]: 1 : CTransactionRef tx1{MakeTransactionRef(mtx)};
105 [ + - + - ]: 2 : CTransactionRef tx2{foo->passTransaction(tx1)};
106 [ + - - + : 2 : BOOST_CHECK(*Assert(tx1) == *Assert(tx2));
- + + - +
- ]
107 : :
108 : 1 : std::vector<CTransactionRef> txs1;
109 [ + - ]: 1 : txs1.push_back(tx1);
110 [ + - - + ]: 1 : txs1.push_back(nullptr);
111 [ + - + - ]: 1 : std::vector<CTransactionRef> txs2(foo->passTransactions(txs1));
112 [ + - - + : 1 : BOOST_CHECK_EQUAL(txs2.size(), 2);
+ - ]
113 [ + - - + : 2 : BOOST_CHECK(*Assert(txs1[0]) == *Assert(txs2[0]));
- + + - +
- ]
114 [ + - + - : 2 : BOOST_CHECK(!txs2[1]);
+ - ]
115 : :
116 [ + - ]: 1 : std::vector<char> vec1{'H', 'e', 'l', 'l', 'o'};
117 [ + - + - ]: 1 : std::vector<char> vec2{foo->passVectorChar(vec1)};
118 [ + - + - ]: 1 : BOOST_CHECK_EQUAL(std::string_view(vec1.begin(), vec1.end()), std::string_view(vec2.begin(), vec2.end()));
119 : :
120 [ + - ]: 1 : auto script1{CScript() << OP_11};
121 [ + - ]: 1 : auto script2{foo->passScript(script1)};
122 [ + - - + : 3 : BOOST_CHECK_EQUAL(HexStr(script1), HexStr(script2));
+ - + - +
- ]
123 : :
124 : : // Test cleanup: disconnect and join thread
125 [ + - ]: 1 : foo.reset();
126 [ + - ]: 1 : thread.join();
127 [ + - + - ]: 4 : }
128 : :
129 : : //! Test ipc::Protocol connect() and serve() methods connecting over a socketpair.
130 : 1 : void IpcSocketPairTest()
131 : : {
132 : 1 : std::unique_ptr<interfaces::Init> init{std::make_unique<TestInit>()};
133 [ + - ]: 1 : std::unique_ptr<ipc::Protocol> protocol{ipc::capnp::MakeCapnpProtocol("IpcSocketPairTest")};
134 : 1 : mp::Stream client_stream;
135 [ + - ]: 1 : std::promise<void> promise;
136 : 2 : std::thread thread([&]() {
137 [ + - ]: 1 : protocol->serve(*init, [&] {
138 : 1 : auto pair{mp::SocketPair()};
139 : 1 : client_stream = protocol->makeStream(pair[0]);
140 : 1 : promise.set_value();
141 : 1 : return protocol->makeStream(pair[1]);
142 : : });
143 [ + - ]: 2 : });
144 [ + - + - ]: 2 : promise.get_future().wait();
145 [ + - ]: 1 : std::unique_ptr<interfaces::Init> remote_init{protocol->connect(std::move(client_stream))};
146 [ + - ]: 1 : std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
147 [ + - + - : 1 : BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test");
+ - + - ]
148 [ + - ]: 1 : remote_echo.reset();
149 [ + - ]: 1 : remote_init.reset();
150 [ + - ]: 1 : thread.join();
151 : 1 : }
152 : :
153 : : //! Test ipc::Process bind() and connect() methods connecting over a unix socket.
154 : 1 : void IpcSocketTest(const fs::path& datadir)
155 : : {
156 : 1 : std::unique_ptr<interfaces::Init> init{std::make_unique<TestInit>()};
157 [ + - ]: 1 : std::unique_ptr<ipc::Protocol> protocol{ipc::capnp::MakeCapnpProtocol("IpcSocketTest")};
158 [ + - ]: 1 : std::unique_ptr<ipc::Process> process{ipc::MakeProcess()};
159 : :
160 [ + - ]: 1 : std::string invalid_bind{"invalid:"};
161 [ + - + - : 3 : BOOST_CHECK_THROW(process->bind(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
- + - - -
- - + + -
+ - ]
162 [ + - + - : 3 : BOOST_CHECK_THROW(process->connect(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
- + - - -
- - + + -
+ - ]
163 : :
164 : 3 : auto bind_and_listen{[&](const std::string& bind_address) {
165 [ - + ]: 2 : std::string address{bind_address};
166 [ + - + - ]: 2 : mp::SocketId serve_fd = process->bind(datadir, "test_bitcoin", address);
167 [ + - + - ]: 2 : BOOST_CHECK_NE(serve_fd, mp::SocketError);
168 [ + - + - ]: 2 : BOOST_CHECK_EQUAL(address, bind_address);
169 [ + - ]: 2 : protocol->listen(serve_fd, *init);
170 : 2 : }};
171 : :
172 : 6 : auto connect_and_test{[&](const std::string& connect_address) {
173 [ - + ]: 5 : std::string address{connect_address};
174 [ + - + - ]: 5 : mp::SocketId connect_fd{process->connect(datadir, "test_bitcoin", address)};
175 [ + - + - ]: 5 : BOOST_CHECK_EQUAL(address, connect_address);
176 [ + - + - ]: 5 : std::unique_ptr<interfaces::Init> remote_init{protocol->connect(protocol->makeStream(connect_fd))};
177 [ + - ]: 5 : std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
178 [ + - + - : 5 : BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test");
+ - + - ]
179 : 5 : }};
180 : :
181 : : // Need to specify explicit socket addresses outside the data directory, because the data
182 : : // directory path is so long that the default socket address and any other
183 : : // addresses in the data directory would fail with errors like:
184 : : // Address 'unix' path '"/tmp/test_common_Bitcoin Core/ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff/test_bitcoin.sock"' exceeded maximum socket path length
185 : 1 : std::vector<std::string> addresses{
186 [ + - ]: 1 : strprintf("unix:%s", TempPath("bitcoin_sock0_XXXXXX")),
187 [ + - ]: 2 : strprintf("unix:%s", TempPath("bitcoin_sock1_XXXXXX")),
188 [ - + + + : 3 : };
- - ]
189 : :
190 : : // Bind and listen on multiple addresses
191 [ + + ]: 3 : for (const auto& address : addresses) {
192 [ + - ]: 2 : bind_and_listen(address);
193 : : }
194 : :
195 : : // Connect and test each address multiple times.
196 [ + + ]: 6 : for (int i : {0, 1, 0, 0, 1}) {
197 [ + - ]: 5 : connect_and_test(addresses[i]);
198 : : }
199 [ + - + - : 3 : }
- - ]
200 : :
201 : : BOOST_FIXTURE_TEST_SUITE(ipc_tests, BasicTestingSetup)
202 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(ipc_tests)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
203 : : {
204 : 1 : IpcPipeTest();
205 : 1 : IpcSocketPairTest();
206 [ + - ]: 1 : IpcSocketTest(m_args.GetDataDirNet());
207 : 1 : }
208 : :
209 : : // Test address parsing.
210 [ + - + - : 7 : BOOST_AUTO_TEST_CASE(parse_address_test)
+ - + - -
+ + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- + - + -
+ - + - +
- + - - +
+ - + - +
- + - + -
+ - - + +
- ]
211 : : {
212 : 1 : std::unique_ptr<ipc::Process> process{ipc::MakeProcess()};
213 [ + - ]: 1 : fs::path datadir{"/var/empty/notexist"};
214 : 4 : auto check_notexist{[](const std::system_error& e) { return e.code() == std::errc::no_such_file_or_directory; }};
215 : 6 : auto check_address{[&](std::string address, std::string expect_address, std::string expect_error) {
216 [ + + ]: 5 : if (expect_error.empty()) {
217 [ + - + - : 9 : BOOST_CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::system_error, check_notexist);
- + - - -
- - + + -
+ - ]
218 : : } else {
219 [ + - + - : 6 : BOOST_CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::invalid_argument, HasReason(expect_error));
- + - - -
- - + + -
- + + - +
- ]
220 : : }
221 [ + - ]: 5 : BOOST_CHECK_EQUAL(address, expect_address);
222 : 6 : }};
223 [ + - + - : 4 : std::string prefix{fs::PathToString(datadir / "")};
- + ]
224 [ + - + - : 3 : check_address("unix", "unix:" + prefix + "test_bitcoin.sock", "");
+ - + - ]
225 [ + - + - : 3 : check_address("unix:", "unix:" + prefix + "test_bitcoin.sock", "");
+ - + - ]
226 [ + - + - : 3 : check_address("unix:path.sock", "unix:" + prefix + "path.sock", "");
+ - + - ]
227 [ + - ]: 2 : check_address("unix:0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.sock",
228 [ + - ]: 2 : "unix:" + prefix + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.sock",
229 [ + - + - ]: 3 : "Unix address path \"" + prefix + "0000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.sock\" exceeded maximum socket path length");
230 [ + - + - : 2 : check_address("invalid", "invalid", "Unrecognized address 'invalid'");
+ - + - ]
231 : 2 : }
232 : :
233 : : BOOST_AUTO_TEST_SUITE_END()
|