From a24e87d4d00fa8fd62aaff1707356450dc7ff32c Mon Sep 17 00:00:00 2001 From: Jason Rhinelander Date: Fri, 15 May 2020 01:33:02 -0300 Subject: [PATCH] Fix sodium linking and call sodium_init() We call libsodium functions which require a sodium_init() call; this is usually a no-op (zmq will have already called it for us), but in case zmq is built with tweetnacl instead of sodium we need to call it before we call it directly in the LokiMQ ctor and the test suite. --- lokimq/lokimq.cpp | 3 +++ tests/CMakeLists.txt | 4 +--- tests/test_connect.cpp | 3 +++ 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lokimq/lokimq.cpp b/lokimq/lokimq.cpp index 5fd2190..1c40c27 100644 --- a/lokimq/lokimq.cpp +++ b/lokimq/lokimq.cpp @@ -201,6 +201,9 @@ LokiMQ::LokiMQ( LMQ_TRACE("Constructing LokiMQ, id=", object_id, ", this=", this); + if (sodium_init() == -1) + throw std::runtime_error{"libsodium initialization failed"}; + if (pubkey.empty() != privkey.empty()) { throw std::invalid_argument("LokiMQ construction failed: one (and only one) of pubkey/privkey is empty. Both must be specified, or both empty to generate a key."); } else if (pubkey.empty()) { diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 2791387..f792fc0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -16,10 +16,8 @@ set(LMQ_TEST_SRC add_executable(tests ${LMQ_TEST_SRC}) find_package(Threads) -find_package(PkgConfig REQUIRED) -pkg_check_modules(SODIUM REQUIRED libsodium) -target_link_libraries(tests Catch2::Catch2 lokimq ${SODIUM_LIBRARIES} Threads::Threads) +target_link_libraries(tests Catch2::Catch2 lokimq Threads::Threads) set_target_properties(tests PROPERTIES CXX_STANDARD 17 diff --git a/tests/test_connect.cpp b/tests/test_connect.cpp index 0ea0084..851c36b 100644 --- a/tests/test_connect.cpp +++ b/tests/test_connect.cpp @@ -53,6 +53,7 @@ TEST_CASE("self-connection SN optimization", "[connect][self]") { std::string pubkey, privkey; pubkey.resize(crypto_box_PUBLICKEYBYTES); privkey.resize(crypto_box_SECRETKEYBYTES); + REQUIRE(sodium_init() != -1); crypto_box_keypair(reinterpret_cast(&pubkey[0]), reinterpret_cast(&privkey[0])); LokiMQ sn{ pubkey, privkey, @@ -188,6 +189,7 @@ TEST_CASE("SN disconnections", "[connect][disconnect]") { std::vector> lmq; std::vector pubkey, privkey; std::unordered_map conn; + REQUIRE(sodium_init() != -1); for (int i = 0; i < 3; i++) { pubkey.emplace_back(); privkey.emplace_back(); @@ -234,6 +236,7 @@ TEST_CASE("SN auth checks", "[sandwich][auth]") { std::string pubkey, privkey; pubkey.resize(crypto_box_PUBLICKEYBYTES); privkey.resize(crypto_box_SECRETKEYBYTES); + REQUIRE(sodium_init() != -1); crypto_box_keypair(reinterpret_cast(&pubkey[0]), reinterpret_cast(&privkey[0])); LokiMQ server{ pubkey, privkey,