11#include <test/ipc_test.capnp.h>
12#include <test/ipc_test.capnp.proxy.h>
15#include <validation.h>
24#include <boost/test/unit_test.hpp>
34static std::string
TempPath(std::string_view pattern)
38 int fd{mkstemp(temp.data())};
39 BOOST_CHECK_GE(fd, 0);
41 temp.resize(temp.size() - 1);
57 std::promise<std::unique_ptr<mp::ProxyClient<gen::FooInterface>>> foo_promise;
58 std::thread thread([&]() {
59 mp::EventLoop loop(
"IpcPipeTest", [](
bool raise,
const std::string& log) {
LogInfo(
"LOG%i: %s", raise, log); });
62 auto connection_client = std::make_unique<mp::Connection>(loop, kj::mv(pipe.ends[0]));
63 auto foo_client = std::make_unique<mp::ProxyClient<gen::FooInterface>>(
64 connection_client->m_rpc_system->bootstrap(
mp::ServerVatId().vat_id).castAs<gen::FooInterface>(),
65 connection_client.get(),
true);
66 connection_client.release();
67 foo_promise.set_value(std::move(foo_client));
69 auto connection_server = std::make_unique<mp::Connection>(loop, kj::mv(pipe.ends[1]), [&](
mp::Connection& connection) {
70 auto foo_server = kj::heap<mp::ProxyServer<gen::FooInterface>>(std::make_shared<FooImplementation>(), connection);
71 return capnp::Capability::Client(kj::mv(foo_server));
73 connection_server->onDisconnect([&] { connection_server.reset(); });
76 std::unique_ptr<mp::ProxyClient<gen::FooInterface>> foo{foo_promise.get_future().get()};
82 COutPoint txout2{foo->passOutPoint(txout1)};
87 uni1.pushKV(
"s",
"two");
88 UniValue uni2{foo->passUniValue(uni1)};
94 mtx.
vin.emplace_back(txout1);
100 std::vector<char> vec1{
'H',
'e',
'l',
'l',
'o'};
101 std::vector<char> vec2{foo->passVectorChar(vec1)};
102 BOOST_CHECK_EQUAL(std::string_view(vec1.begin(), vec1.end()), std::string_view(vec2.begin(), vec2.end()));
105 auto script2{foo->passScript(script1)};
118 std::unique_ptr<interfaces::Init>
init{std::make_unique<TestInit>()};
120 std::promise<void> promise;
121 std::thread thread([&]() {
122 protocol->serve(fds[0],
"test-serve", *
init, [&] { promise.set_value(); });
124 promise.get_future().wait();
125 std::unique_ptr<interfaces::Init> remote_init{protocol->connect(fds[1],
"test-connect")};
126 std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
136 std::unique_ptr<interfaces::Init>
init{std::make_unique<TestInit>()};
140 std::string invalid_bind{
"invalid:"};
141 BOOST_CHECK_THROW(process->bind(datadir,
"test_bitcoin", invalid_bind), std::invalid_argument);
142 BOOST_CHECK_THROW(process->connect(datadir,
"test_bitcoin", invalid_bind), std::invalid_argument);
144 auto bind_and_listen{[&](
const std::string& bind_address) {
145 std::string address{bind_address};
146 int serve_fd = process->bind(datadir,
"test_bitcoin", address);
147 BOOST_CHECK_GE(serve_fd, 0);
149 protocol->listen(serve_fd,
"test-serve", *
init);
152 auto connect_and_test{[&](
const std::string& connect_address) {
153 std::string address{connect_address};
154 int connect_fd{process->connect(datadir,
"test_bitcoin", address)};
156 std::unique_ptr<interfaces::Init> remote_init{protocol->connect(connect_fd,
"test-connect")};
157 std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
165 std::vector<std::string> addresses{
171 for (
const auto& address : addresses) {
172 bind_and_listen(address);
176 for (
int i : {0, 1, 0, 0, 1}) {
177 connect_and_test(addresses[i]);
static constexpr CAmount COIN
The amount of satoshis in one BTC.
#define Assert(val)
Identity function.
An outpoint - a combination of a transaction hash and an index n into its vout.
Serialized script, used inside transaction inputs and outputs.
std::unique_ptr< interfaces::Echo > makeEcho() override
Initial interface created when a process is first started, and used to give and get access to other i...
Object holding network & rpc state associated with either an incoming server connection,...
Event loop implementation.
kj::AsyncIoContext m_io_context
Capnp IO context.
static transaction_identifier FromUint256(const uint256 &id)
static std::string PathToString(const path &path)
Convert path object to a byte string.
static path PathFromString(const std::string &string)
Convert byte string to path object.
std::string HexStr(const std::span< const uint8_t > s)
Convert a span of bytes to a lower-case hexadecimal string.
static std::string TempPath(std::string_view pattern)
Generate a temporary path with temp_directory_path and mkstemp.
void IpcSocketTest(const fs::path &datadir)
Test ipc::Process bind() and connect() methods connecting over a unix socket.
void IpcPipeTest()
Unit test that tests execution of IPC calls without actually creating a separate process.
void IpcSocketPairTest()
Test ipc::Protocol connect() and serve() methods connecting over a socketpair.
std::unique_ptr< Echo > MakeEcho()
Return implementation of Echo interface.
std::unique_ptr< Protocol > MakeCapnpProtocol()
std::unique_ptr< Process > MakeProcess()
Constructor for Process interface.
#define BOOST_CHECK_THROW(stmt, excMatch)
#define BOOST_CHECK_EQUAL(v1, v2)
#define BOOST_CHECK(expr)
static CTransactionRef MakeTransactionRef(Tx &&txIn)
std::shared_ptr< const CTransaction > CTransactionRef
A mutable version of CTransaction.
std::vector< CTxOut > vout
Vat id for server side of connection.