10#include <mp/proxy-types.h>
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::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();
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(),
false);
67 foo_promise.set_value(std::move(foo_client));
68 disconnect_client = [&] { loop.sync([&] { connection_client.reset(); }); };
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));
74 connection_server->onDisconnect([&] { connection_server.reset(); });
77 std::unique_ptr<mp::ProxyClient<gen::FooInterface>> foo{foo_promise.get_future().get()};
83 COutPoint txout2{foo->passOutPoint(txout1)};
88 uni1.pushKV(
"s",
"two");
89 UniValue uni2{foo->passUniValue(uni1)};
95 mtx.
vin.emplace_back(txout1);
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()));
134 std::unique_ptr<interfaces::Init>
init{std::make_unique<TestInit>()};
136 std::promise<void> promise;
137 std::thread thread([&]() {
138 protocol->serve(fds[0],
"test-serve", *
init, [&] { promise.set_value(); });
140 promise.get_future().wait();
141 std::unique_ptr<interfaces::Init> remote_init{protocol->connect(fds[1],
"test-connect")};
142 std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
152 std::unique_ptr<interfaces::Init>
init{std::make_unique<TestInit>()};
156 std::string invalid_bind{
"invalid:"};
157 BOOST_CHECK_THROW(process->bind(datadir,
"test_bitcoin", invalid_bind), std::invalid_argument);
158 BOOST_CHECK_THROW(process->connect(datadir,
"test_bitcoin", invalid_bind), std::invalid_argument);
160 auto bind_and_listen{[&](
const std::string& bind_address) {
161 std::string address{bind_address};
162 int serve_fd = process->bind(datadir,
"test_bitcoin", address);
163 BOOST_CHECK_GE(serve_fd, 0);
165 protocol->listen(serve_fd,
"test-serve", *
init);
168 auto connect_and_test{[&](
const std::string& connect_address) {
169 std::string address{connect_address};
170 int connect_fd{process->connect(datadir,
"test_bitcoin", address)};
172 std::unique_ptr<interfaces::Init> remote_init{protocol->connect(connect_fd,
"test-connect")};
173 std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
181 std::vector<std::string> addresses{
187 for (
const auto& address : addresses) {
188 bind_and_listen(address);
192 for (
int i : {0, 1, 0, 0, 1}) {
193 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
std::string GetRejectReason() const
std::string GetDebugMessage() const
bool Invalid(Result result, const std::string &reject_reason="", const std::string &debug_message="")
Path class wrapper to block calls to the fs::path(std::string) implicit constructor and the fs::path:...
Initial interface created when a process is first started, and used to give and get access to other i...
static transaction_identifier FromUint256(const uint256 &id)
@ BLOCK_CHECKPOINT
the block failed to meet one of our checkpoints
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.
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::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