Test ipc::Process bind() and connect() methods connecting over a unix socket.
| 138 | |
| 139 | //! Test ipc::Process bind() and connect() methods connecting over a unix socket. |
| 140 | void IpcSocketTest(const fs::path& datadir) |
| 141 | { |
| 142 | std::unique_ptr<interfaces::Init> init{std::make_unique<TestInit>()}; |
| 143 | std::unique_ptr<ipc::Protocol> protocol{ipc::capnp::MakeCapnpProtocol()}; |
| 144 | std::unique_ptr<ipc::Process> process{ipc::MakeProcess()}; |
| 145 | |
| 146 | std::string invalid_bind{"invalid:"}; |
| 147 | BOOST_CHECK_THROW(process->bind(datadir, "test_bitcoin", invalid_bind), std::invalid_argument); |
| 148 | BOOST_CHECK_THROW(process->connect(datadir, "test_bitcoin", invalid_bind), std::invalid_argument); |
| 149 | |
| 150 | auto bind_and_listen{[&](const std::string& bind_address) { |
| 151 | std::string address{bind_address}; |
| 152 | int serve_fd = process->bind(datadir, "test_bitcoin", address); |
| 153 | BOOST_CHECK_GE(serve_fd, 0); |
| 154 | BOOST_CHECK_EQUAL(address, bind_address); |
| 155 | protocol->listen(serve_fd, "test-serve", *init); |
| 156 | }}; |
| 157 | |
| 158 | auto connect_and_test{[&](const std::string& connect_address) { |
| 159 | std::string address{connect_address}; |
| 160 | int connect_fd{process->connect(datadir, "test_bitcoin", address)}; |
| 161 | BOOST_CHECK_EQUAL(address, connect_address); |
| 162 | std::unique_ptr<interfaces::Init> remote_init{protocol->connect(connect_fd, "test-connect")}; |
| 163 | std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()}; |
| 164 | BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test"); |
| 165 | }}; |
| 166 | |
| 167 | // Need to specify explicit socket addresses outside the data directory, because the data |
| 168 | // directory path is so long that the default socket address and any other |
| 169 | // addresses in the data directory would fail with errors like: |
| 170 | // Address 'unix' path '"/tmp/test_common_Bitcoin Core/ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff/test_bitcoin.sock"' exceeded maximum socket path length |
| 171 | std::vector<std::string> addresses{ |
| 172 | strprintf("unix:%s", TempPath("bitcoin_sock0_XXXXXX")), |
| 173 | strprintf("unix:%s", TempPath("bitcoin_sock1_XXXXXX")), |
| 174 | }; |
| 175 | |
| 176 | // Bind and listen on multiple addresses |
| 177 | for (const auto& address : addresses) { |
| 178 | bind_and_listen(address); |
| 179 | } |
| 180 | |
| 181 | // Connect and test each address multiple times. |
| 182 | for (int i : {0, 1, 0, 0, 1}) { |
| 183 | connect_and_test(addresses[i]); |
| 184 | } |
| 185 | } |
no test coverage detected