Bitcoin Core 29.99.0
P2P Digital Currency
ipc_test.cpp
Go to the documentation of this file.
1// Copyright (c) 2023 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>
7#include <ipc/process.h>
8#include <ipc/protocol.h>
9#include <logging.h>
10#include <mp/proxy-types.h>
11#include <test/ipc_test.capnp.h>
12#include <test/ipc_test.capnp.proxy.h>
13#include <test/ipc_test.h>
14#include <tinyformat.h>
15#include <validation.h>
16
17#include <future>
18#include <thread>
19#include <kj/common.h>
20#include <kj/memory.h>
21#include <kj/test.h>
22#include <stdexcept>
23
24#include <boost/test/unit_test.hpp>
25
28{
29public:
30 std::unique_ptr<interfaces::Echo> makeEcho() override { return interfaces::MakeEcho(); }
31};
32
34static std::string TempPath(std::string_view pattern)
35{
36 std::string temp{fs::PathToString(fs::path{fs::temp_directory_path()} / fs::PathFromString(std::string{pattern}))};
37 temp.push_back('\0');
38 int fd{mkstemp(temp.data())};
39 BOOST_CHECK_GE(fd, 0);
40 BOOST_CHECK_EQUAL(close(fd), 0);
41 temp.resize(temp.size() - 1);
42 fs::remove(fs::PathFromString(temp));
43 return temp;
44}
45
55{
56 // Setup: create FooImplementation object and listen for FooInterface requests
57 std::promise<std::unique_ptr<mp::ProxyClient<gen::FooInterface>>> foo_promise;
58 std::function<void()> disconnect_client;
59 std::thread thread([&]() {
60 mp::EventLoop loop("IpcPipeTest", [](bool raise, const std::string& log) { LogPrintf("LOG%i: %s\n", raise, log); });
61 auto pipe = loop.m_io_context.provider->newTwoWayPipe();
62
63 auto connection_client = std::make_unique<mp::Connection>(loop, kj::mv(pipe.ends[0]));
64 auto foo_client = std::make_unique<mp::ProxyClient<gen::FooInterface>>(
65 connection_client->m_rpc_system->bootstrap(mp::ServerVatId().vat_id).castAs<gen::FooInterface>(),
66 connection_client.get(), /* destroy_connection= */ false);
67 foo_promise.set_value(std::move(foo_client));
68 disconnect_client = [&] { loop.sync([&] { connection_client.reset(); }); };
69
70 auto connection_server = std::make_unique<mp::Connection>(loop, kj::mv(pipe.ends[1]), [&](mp::Connection& connection) {
71 auto foo_server = kj::heap<mp::ProxyServer<gen::FooInterface>>(std::make_shared<FooImplementation>(), connection);
72 return capnp::Capability::Client(kj::mv(foo_server));
73 });
74 connection_server->onDisconnect([&] { connection_server.reset(); });
75 loop.loop();
76 });
77 std::unique_ptr<mp::ProxyClient<gen::FooInterface>> foo{foo_promise.get_future().get()};
78
79 // Test: make sure arguments were sent and return value is received
80 BOOST_CHECK_EQUAL(foo->add(1, 2), 3);
81
82 COutPoint txout1{Txid::FromUint256(uint256{100}), 200};
83 COutPoint txout2{foo->passOutPoint(txout1)};
84 BOOST_CHECK(txout1 == txout2);
85
87 uni1.pushKV("i", 1);
88 uni1.pushKV("s", "two");
89 UniValue uni2{foo->passUniValue(uni1)};
90 BOOST_CHECK_EQUAL(uni1.write(), uni2.write());
91
93 mtx.version = 2;
94 mtx.nLockTime = 3;
95 mtx.vin.emplace_back(txout1);
96 mtx.vout.emplace_back(COIN, CScript());
98 CTransactionRef tx2{foo->passTransaction(tx1)};
99 BOOST_CHECK(*Assert(tx1) == *Assert(tx2));
100
101 std::vector<char> vec1{'H', 'e', 'l', 'l', 'o'};
102 std::vector<char> vec2{foo->passVectorChar(vec1)};
103 BOOST_CHECK_EQUAL(std::string_view(vec1.begin(), vec1.end()), std::string_view(vec2.begin(), vec2.end()));
104
105 auto script1{CScript() << OP_11};
106 auto script2{foo->passScript(script1)};
107 BOOST_CHECK_EQUAL(HexStr(script1), HexStr(script2));
108
109 // Test cleanup: disconnect pipe and join thread
110 disconnect_client();
111 thread.join();
112}
113
116{
117 int fds[2];
118 BOOST_CHECK_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
119 std::unique_ptr<interfaces::Init> init{std::make_unique<TestInit>()};
120 std::unique_ptr<ipc::Protocol> protocol{ipc::capnp::MakeCapnpProtocol()};
121 std::promise<void> promise;
122 std::thread thread([&]() {
123 protocol->serve(fds[0], "test-serve", *init, [&] { promise.set_value(); });
124 });
125 promise.get_future().wait();
126 std::unique_ptr<interfaces::Init> remote_init{protocol->connect(fds[1], "test-connect")};
127 std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
128 BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test");
129 remote_echo.reset();
130 remote_init.reset();
131 thread.join();
132}
133
135void IpcSocketTest(const fs::path& datadir)
136{
137 std::unique_ptr<interfaces::Init> init{std::make_unique<TestInit>()};
138 std::unique_ptr<ipc::Protocol> protocol{ipc::capnp::MakeCapnpProtocol()};
139 std::unique_ptr<ipc::Process> process{ipc::MakeProcess()};
140
141 std::string invalid_bind{"invalid:"};
142 BOOST_CHECK_THROW(process->bind(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
143 BOOST_CHECK_THROW(process->connect(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
144
145 auto bind_and_listen{[&](const std::string& bind_address) {
146 std::string address{bind_address};
147 int serve_fd = process->bind(datadir, "test_bitcoin", address);
148 BOOST_CHECK_GE(serve_fd, 0);
149 BOOST_CHECK_EQUAL(address, bind_address);
150 protocol->listen(serve_fd, "test-serve", *init);
151 }};
152
153 auto connect_and_test{[&](const std::string& connect_address) {
154 std::string address{connect_address};
155 int connect_fd{process->connect(datadir, "test_bitcoin", address)};
156 BOOST_CHECK_EQUAL(address, connect_address);
157 std::unique_ptr<interfaces::Init> remote_init{protocol->connect(connect_fd, "test-connect")};
158 std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
159 BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test");
160 }};
161
162 // Need to specify explicit socket addresses outside the data directory, because the data
163 // directory path is so long that the default socket address and any other
164 // addresses in the data directory would fail with errors like:
165 // Address 'unix' path '"/tmp/test_common_Bitcoin Core/ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff/test_bitcoin.sock"' exceeded maximum socket path length
166 std::vector<std::string> addresses{
167 strprintf("unix:%s", TempPath("bitcoin_sock0_XXXXXX")),
168 strprintf("unix:%s", TempPath("bitcoin_sock1_XXXXXX")),
169 };
170
171 // Bind and listen on multiple addresses
172 for (const auto& address : addresses) {
173 bind_and_listen(address);
174 }
175
176 // Connect and test each address multiple times.
177 for (int i : {0, 1, 0, 0, 1}) {
178 connect_and_test(addresses[i]);
179 }
180}
static constexpr CAmount COIN
The amount of satoshis in one BTC.
Definition: amount.h:15
#define Assert(val)
Identity function.
Definition: check.h:106
An outpoint - a combination of a transaction hash and an index n into its vout.
Definition: transaction.h:29
Serialized script, used inside transaction inputs and outputs.
Definition: script.h:415
Remote init class.
Definition: ipc_test.cpp:28
std::unique_ptr< interfaces::Echo > makeEcho() override
Definition: ipc_test.cpp:30
@ VOBJ
Definition: univalue.h:24
Initial interface created when a process is first started, and used to give and get access to other i...
Definition: init.h:31
Object holding network & rpc state associated with either an incoming server connection,...
Definition: proxy-io.h:288
Event loop implementation.
Definition: proxy-io.h:134
kj::AsyncIoContext m_io_context
Capnp IO context.
Definition: proxy-io.h:219
static transaction_identifier FromUint256(const uint256 &id)
256-bit opaque blob.
Definition: uint256.h:196
static std::string PathToString(const path &path)
Convert path object to a byte string.
Definition: fs.h:151
static path PathFromString(const std::string &string)
Convert byte string to path object.
Definition: fs.h:174
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
Definition: hex_base.cpp:29
static std::string TempPath(std::string_view pattern)
Generate a temporary path with temp_directory_path and mkstemp.
Definition: ipc_test.cpp:34
void IpcSocketTest(const fs::path &datadir)
Test ipc::Process bind() and connect() methods connecting over a unix socket.
Definition: ipc_test.cpp:135
void IpcPipeTest()
Unit test that tests execution of IPC calls without actually creating a separate process.
Definition: ipc_test.cpp:54
void IpcSocketPairTest()
Test ipc::Protocol connect() and serve() methods connecting over a socketpair.
Definition: ipc_test.cpp:115
#define LogPrintf(...)
Definition: logging.h:266
std::unique_ptr< Echo > MakeEcho()
Return implementation of Echo interface.
Definition: interfaces.cpp:52
std::unique_ptr< Protocol > MakeCapnpProtocol()
Definition: protocol.cpp:102
std::unique_ptr< Process > MakeProcess()
Constructor for Process interface.
Definition: process.cpp:156
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
#define BOOST_CHECK(expr)
Definition: object.cpp:17
static CTransactionRef MakeTransactionRef(Tx &&txIn)
Definition: transaction.h:424
std::shared_ptr< const CTransaction > CTransactionRef
Definition: transaction.h:423
@ OP_11
Definition: script.h:94
A mutable version of CTransaction.
Definition: transaction.h:378
std::vector< CTxOut > vout
Definition: transaction.h:380
std::vector< CTxIn > vin
Definition: transaction.h:379
Vat id for server side of connection.
Definition: proxy-io.h:370
#define strprintf
Format arguments and return the string or write to given std::ostream (see tinyformat::format doc for...
Definition: tinyformat.h:1172