Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 18 additions & 19 deletions src/ipc/test/ipc_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,7 @@
#include <kj/test.h>
#include <stdexcept>

#include <boost/test/unit_test.hpp>

#include <test/util/framework.hpp>
static_assert(ipc::capnp::messages::MAX_MONEY == MAX_MONEY);
static_assert(ipc::capnp::messages::MAX_DOUBLE == std::numeric_limits<double>::max());
static_assert(ipc::capnp::messages::DEFAULT_BLOCK_RESERVED_WEIGHT == DEFAULT_BLOCK_RESERVED_WEIGHT);
Expand All @@ -47,8 +46,8 @@ static std::string TempPath(std::string_view pattern)
std::string temp{fs::PathToString(fs::path{fs::temp_directory_path()} / fs::PathFromString(std::string{pattern}))};
temp.push_back('\0');
int fd{mkstemp(temp.data())};
BOOST_CHECK_GE(fd, 0);
BOOST_CHECK_EQUAL(close(fd), 0);
CHECK(fd >= 0);
CHECK(close(fd) == 0);
temp.resize(temp.size() - 1);
fs::remove(fs::PathFromString(temp));
return temp;
Expand Down Expand Up @@ -87,17 +86,17 @@ void IpcPipeTest()
std::unique_ptr<mp::ProxyClient<gen::FooInterface>> foo{foo_promise.get_future().get()};

// Test: make sure arguments were sent and return value is received
BOOST_CHECK_EQUAL(foo->add(1, 2), 3);
CHECK(foo->add(1, 2) == 3);

COutPoint txout1{Txid::FromUint256(uint256{100}), 200};
COutPoint txout2{foo->passOutPoint(txout1)};
BOOST_CHECK(txout1 == txout2);
CHECK((txout1 == txout2));

UniValue uni1{UniValue::VOBJ};
uni1.pushKV("i", 1);
uni1.pushKV("s", "two");
UniValue uni2{foo->passUniValue(uni1)};
BOOST_CHECK_EQUAL(uni1.write(), uni2.write());
CHECK(uni1.write() == uni2.write());

CMutableTransaction mtx;
mtx.version = 2;
Expand All @@ -106,15 +105,15 @@ void IpcPipeTest()
mtx.vout.emplace_back(COIN, CScript());
CTransactionRef tx1{MakeTransactionRef(mtx)};
CTransactionRef tx2{foo->passTransaction(tx1)};
BOOST_CHECK(*Assert(tx1) == *Assert(tx2));
CHECK((*Assert(tx1) == *Assert(tx2)));

std::vector<char> vec1{'H', 'e', 'l', 'l', 'o'};
std::vector<char> vec2{foo->passVectorChar(vec1)};
BOOST_CHECK_EQUAL(std::string_view(vec1.begin(), vec1.end()), std::string_view(vec2.begin(), vec2.end()));
CHECK(std::string_view(vec1.begin(), vec1.end()) == std::string_view(vec2.begin(), vec2.end()));

auto script1{CScript() << OP_11};
auto script2{foo->passScript(script1)};
BOOST_CHECK_EQUAL(HexStr(script1), HexStr(script2));
CHECK(HexStr(script1) == HexStr(script2));

// Test cleanup: disconnect and join thread
foo.reset();
Expand All @@ -125,7 +124,7 @@ void IpcPipeTest()
void IpcSocketPairTest()
{
int fds[2];
BOOST_CHECK_EQUAL(socketpair(AF_UNIX, SOCK_STREAM, 0, fds), 0);
CHECK(socketpair(AF_UNIX, SOCK_STREAM, 0, fds) == 0);
std::unique_ptr<interfaces::Init> init{std::make_unique<TestInit>()};
std::unique_ptr<ipc::Protocol> protocol{ipc::capnp::MakeCapnpProtocol()};
std::promise<void> promise;
Expand All @@ -135,10 +134,10 @@ void IpcSocketPairTest()
promise.get_future().wait();
std::unique_ptr<interfaces::Init> remote_init{protocol->connect(fds[1], "test-connect")};
std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test");
CHECK(remote_echo->echo("echo test") == "echo test");
remote_echo.reset();
remote_init->stop();
BOOST_CHECK(static_cast<TestInit*>(init.get())->stop_called.load());
CHECK(static_cast<TestInit*>(init.get())->stop_called.load());
remote_init.reset();
thread.join();
}
Expand All @@ -151,24 +150,24 @@ void IpcSocketTest(const fs::path& datadir)
std::unique_ptr<ipc::Process> process{ipc::MakeProcess()};

std::string invalid_bind{"invalid:"};
BOOST_CHECK_THROW(process->bind(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
BOOST_CHECK_THROW(process->connect(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
CHECK_THROWS_AS(process->bind(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);
CHECK_THROWS_AS(process->connect(datadir, "test_bitcoin", invalid_bind), std::invalid_argument);

auto bind_and_listen{[&](const std::string& bind_address) {
std::string address{bind_address};
int serve_fd = process->bind(datadir, "test_bitcoin", address);
BOOST_CHECK_GE(serve_fd, 0);
BOOST_CHECK_EQUAL(address, bind_address);
CHECK(serve_fd >= 0);
CHECK(address == bind_address);
protocol->listen(serve_fd, "test-serve", *init);
}};

auto connect_and_test{[&](const std::string& connect_address) {
std::string address{connect_address};
int connect_fd{process->connect(datadir, "test_bitcoin", address)};
BOOST_CHECK_EQUAL(address, connect_address);
CHECK(address == connect_address);
std::unique_ptr<interfaces::Init> remote_init{protocol->connect(connect_fd, "test-connect")};
std::unique_ptr<interfaces::Echo> remote_echo{remote_init->makeEcho()};
BOOST_CHECK_EQUAL(remote_echo->echo("echo test"), "echo test");
CHECK(remote_echo->echo("echo test") == "echo test");
}};

// Need to specify explicit socket addresses outside the data directory, because the data
Expand Down
17 changes: 8 additions & 9 deletions src/ipc/test/ipc_tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,28 @@

#include <test/util/common.h>
#include <test/util/setup_common.h>
#include <boost/test/unit_test.hpp>

BOOST_FIXTURE_TEST_SUITE(ipc_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(ipc_tests)
#include <test/util/framework.hpp>
TEST_SUITE_BEGIN("ipc_tests")
FIXTURE_TEST_CASE("ipc_tests", BasicTestingSetup)
{
IpcPipeTest();
IpcSocketPairTest();
IpcSocketTest(m_args.GetDataDirNet());
}

// Test address parsing.
BOOST_AUTO_TEST_CASE(parse_address_test)
FIXTURE_TEST_CASE("parse_address_test", BasicTestingSetup)
{
std::unique_ptr<ipc::Process> process{ipc::MakeProcess()};
fs::path datadir{"/var/empty/notexist"};
auto check_notexist{[](const std::system_error& e) { return e.code() == std::errc::no_such_file_or_directory; }};
auto check_address{[&](std::string address, std::string expect_address, std::string expect_error) {
if (expect_error.empty()) {
BOOST_CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::system_error, check_notexist);
CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::system_error, check_notexist);
} else {
BOOST_CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::invalid_argument, HasReason(expect_error));
CHECK_EXCEPTION(process->connect(datadir, "test_bitcoin", address), std::invalid_argument, HasReason(expect_error));
}
BOOST_CHECK_EQUAL(address, expect_address);
CHECK(address == expect_address);
}};
check_address("unix", "unix:/var/empty/notexist/test_bitcoin.sock", "");
check_address("unix:", "unix:/var/empty/notexist/test_bitcoin.sock", "");
Expand All @@ -40,4 +39,4 @@ BOOST_AUTO_TEST_CASE(parse_address_test)
check_address("invalid", "invalid", "Unrecognized address 'invalid'");
}

BOOST_AUTO_TEST_SUITE_END()
TEST_SUITE_END()
13 changes: 6 additions & 7 deletions src/test/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -160,22 +160,21 @@ target_link_libraries(test_bitcoin

add_subdirectory(${PROJECT_SOURCE_DIR}/src/ipc/test ipc)

function(add_boost_test source_file)
function(register_test_suite source_file)
if(NOT EXISTS ${source_file})
return()
endif()

file(READ "${source_file}" source_file_content)
string(REGEX
MATCHALL "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(([A-Za-z0-9_]+)"
MATCHALL "TEST_SUITE_BEGIN\\(\"[A-Za-z0-9_]+\""
test_suite_macro "${source_file_content}"
)
list(TRANSFORM test_suite_macro
REPLACE "(BOOST_FIXTURE_TEST_SUITE|BOOST_AUTO_TEST_SUITE)\\(" ""
)
list(TRANSFORM test_suite_macro REPLACE "^TEST_SUITE_BEGIN\\(\"" "")
list(TRANSFORM test_suite_macro REPLACE "\"$" "")
foreach(test_suite_name IN LISTS test_suite_macro)
add_test(NAME ${test_suite_name}
COMMAND test_bitcoin --run_test=${test_suite_name} --catch_system_error=no --log_level=test_suite -- -printtoconsole=1
COMMAND test_bitcoin --run_test=${test_suite_name} -- -printtoconsole=1
)
set_property(TEST ${test_suite_name} PROPERTY
SKIP_REGULAR_EXPRESSION
Expand All @@ -194,7 +193,7 @@ function(add_all_test_targets)
if(result)
cmake_path(APPEND test_source_dir ${test_source} OUTPUT_VARIABLE test_source)
endif()
add_boost_test(${test_source})
register_test_suite(${test_source})
endforeach()
endfunction()

Expand Down
Loading
Loading