diff --git a/tests/common.h b/tests/common.h index adef968..9a3ce2b 100644 --- a/tests/common.h +++ b/tests/common.h @@ -6,6 +6,16 @@ using namespace lokimq; static auto startup = std::chrono::steady_clock::now(); +/// Returns a localhost connection string to listen on. It can be considered random, though in +/// practice in the current implementation is sequential starting at 4500. +inline std::string random_localhost() { + static uint16_t last = 4499; + last++; + assert(last); // We should never call this enough to overflow + return "tcp://127.0.0.1:" + std::to_string(last); +} + + /// Waits up to 100ms for something to happen. template inline void wait_for(Func f) { diff --git a/tests/test_commands.cpp b/tests/test_commands.cpp index 03e1495..70b7f36 100644 --- a/tests/test_commands.cpp +++ b/tests/test_commands.cpp @@ -6,7 +6,7 @@ using namespace lokimq; TEST_CASE("basic commands", "[commands]") { - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -76,7 +76,7 @@ TEST_CASE("basic commands", "[commands]") { } TEST_CASE("outgoing auth level", "[commands][auth]") { - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -158,7 +158,7 @@ TEST_CASE("deferred replies on incoming connections", "[commands][hey google]") // Tests that the ConnectionID from a Message can be stored and reused later to contact the // original node. - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -270,7 +270,7 @@ TEST_CASE("deferred replies on incoming connections", "[commands][hey google]") } TEST_CASE("send failure callbacks", "[commands][queue_full]") { - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -364,7 +364,7 @@ TEST_CASE("send failure callbacks", "[commands][queue_full]") { } TEST_CASE("data parts", "[send][data_parts]") { - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node diff --git a/tests/test_connect.cpp b/tests/test_connect.cpp index 73161e9..b9f30d8 100644 --- a/tests/test_connect.cpp +++ b/tests/test_connect.cpp @@ -6,7 +6,7 @@ extern "C" { TEST_CASE("connections with curve authentication", "[curve][connect]") { - std::string listen = "tcp://127.0.0.1:4455"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -53,16 +53,17 @@ TEST_CASE("self-connection SN optimization", "[connect][self]") { pubkey.resize(crypto_box_PUBLICKEYBYTES); privkey.resize(crypto_box_SECRETKEYBYTES); REQUIRE(sodium_init() != -1); + auto listen_addr = random_localhost(); crypto_box_keypair(reinterpret_cast(&pubkey[0]), reinterpret_cast(&privkey[0])); LokiMQ sn{ pubkey, privkey, true, - [&](auto pk) { if (pk == pubkey) return "tcp://127.0.0.1:5544"; else return ""; }, + [&](auto pk) { if (pk == pubkey) return listen_addr; else return ""s; }, get_logger("S» "), LogLevel::trace }; - sn.listen_curve("tcp://127.0.0.1:5544", [&](auto ip, auto pk, auto sn) { + sn.listen_curve(listen_addr, [&](auto ip, auto pk, auto sn) { auto lock = catch_lock(); REQUIRE(ip == "127.0.0.1"); REQUIRE(sn == (pk == pubkey)); @@ -90,7 +91,7 @@ TEST_CASE("self-connection SN optimization", "[connect][self]") { } TEST_CASE("plain-text connections", "[plaintext][connect]") { - std::string listen = "tcp://127.0.0.1:4455"; + std::string listen = random_localhost(); LokiMQ server{get_logger("S» "), LogLevel::trace}; server.add_category("public", Access{AuthLevel::none}); @@ -129,7 +130,7 @@ TEST_CASE("plain-text connections", "[plaintext][connect]") { } TEST_CASE("unique connection IDs", "[connect][id]") { - std::string listen = "tcp://127.0.0.1:4455"; + std::string listen = random_localhost(); LokiMQ server{get_logger("S» "), LogLevel::trace}; ConnectionID first, second; @@ -195,7 +196,7 @@ TEST_CASE("SN disconnections", "[connect][disconnect]") { pubkey[i].resize(crypto_box_PUBLICKEYBYTES); privkey[i].resize(crypto_box_SECRETKEYBYTES); crypto_box_keypair(reinterpret_cast(&pubkey[i][0]), reinterpret_cast(&privkey[i][0])); - conn.emplace(pubkey[i], "tcp://127.0.0.1:" + std::to_string(4450 + i)); + conn.emplace(pubkey[i], random_localhost()); } std::atomic his{0}; for (int i = 0; i < pubkey.size(); i++) { @@ -231,7 +232,7 @@ TEST_CASE("SN auth checks", "[sandwich][auth]") { // isn't recognized as a SN but tries to invoke a SN command it'll be told to disconnect; if it // tries to send again it should reconnect and reauthenticate. This test is meant to test this // pattern where the reconnection/reauthentication now authenticates it as a SN. - std::string listen = "tcp://127.0.0.1:4455"; + std::string listen = random_localhost(); std::string pubkey, privkey; pubkey.resize(crypto_box_PUBLICKEYBYTES); privkey.resize(crypto_box_SECRETKEYBYTES); @@ -350,7 +351,7 @@ TEST_CASE("SN auth checks", "[sandwich][auth]") { TEST_CASE("SN single worker test", "[connect][worker]") { // Tests a failure case that could trigger when all workers are allocated (here we make that // simpler by just having one worker). - std::string listen = "tcp://127.0.0.1:4455"; + std::string listen = random_localhost(); LokiMQ server{ "", "", false, // service node diff --git a/tests/test_failures.cpp b/tests/test_failures.cpp index efab861..1701b28 100644 --- a/tests/test_failures.cpp +++ b/tests/test_failures.cpp @@ -6,7 +6,7 @@ using namespace lokimq; TEST_CASE("failure responses - UNKNOWNCOMMAND", "[failure][UNKNOWNCOMMAND]") { - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -47,7 +47,7 @@ TEST_CASE("failure responses - UNKNOWNCOMMAND", "[failure][UNKNOWNCOMMAND]") { } TEST_CASE("failure responses - NO_REPLY_TAG", "[failure][NO_REPLY_TAG]") { - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -108,7 +108,7 @@ TEST_CASE("failure responses - NO_REPLY_TAG", "[failure][NO_REPLY_TAG]") { } TEST_CASE("failure responses - FORBIDDEN", "[failure][FORBIDDEN]") { - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -191,7 +191,7 @@ TEST_CASE("failure responses - FORBIDDEN", "[failure][FORBIDDEN]") { } TEST_CASE("failure responses - NOT_A_SERVICE_NODE", "[failure][NOT_A_SERVICE_NODE]") { - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -258,7 +258,7 @@ TEST_CASE("failure responses - NOT_A_SERVICE_NODE", "[failure][NOT_A_SERVICE_NOD } TEST_CASE("failure responses - FORBIDDEN_SN", "[failure][FORBIDDEN_SN]") { - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node diff --git a/tests/test_inject.cpp b/tests/test_inject.cpp index 168e02f..cffd20a 100644 --- a/tests/test_inject.cpp +++ b/tests/test_inject.cpp @@ -3,7 +3,7 @@ using namespace lokimq; TEST_CASE("injected external commands", "[injected]") { - std::string listen = "tcp://127.0.0.1:4567"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node diff --git a/tests/test_requests.cpp b/tests/test_requests.cpp index 789b3b9..af31e9e 100644 --- a/tests/test_requests.cpp +++ b/tests/test_requests.cpp @@ -4,7 +4,7 @@ using namespace lokimq; TEST_CASE("basic requests", "[requests]") { - std::string listen = "tcp://127.0.0.1:5678"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -61,7 +61,7 @@ TEST_CASE("basic requests", "[requests]") { } TEST_CASE("request from server to client", "[requests]") { - std::string listen = "tcp://127.0.0.1:5678"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node @@ -124,7 +124,7 @@ TEST_CASE("request from server to client", "[requests]") { } TEST_CASE("request timeouts", "[requests][timeout]") { - std::string listen = "tcp://127.0.0.1:5678"; + std::string listen = random_localhost(); LokiMQ server{ "", "", // generate ephemeral keys false, // not a service node