From ece91e87fc1031763d560640abbcaba18fdc685e Mon Sep 17 00:00:00 2001 From: lyyn Date: Tue, 2 Mar 2021 00:07:32 +0300 Subject: [PATCH] Migrate tests from gtest to catch2 --- .drone.jsonnet | 2 - test/CMakeLists.txt | 119 ++- test/check_main.cpp | 42 +- test/config/test_llarp_config_ini.cpp | 90 +- test/crypto/mock_crypto.cpp | 1 - test/crypto/mock_crypto.hpp | 85 +- test/crypto/test_llarp_crypto.cpp | 77 +- test/crypto/test_llarp_crypto_types.cpp | 196 ++-- test/crypto/test_llarp_key_manager.cpp | 89 +- test/dht/mock_context.cpp | 1 - test/dht/mock_context.hpp | 117 ++- test/dht/test_llarp_dht_bucket.cpp | 213 +++-- test/dht/test_llarp_dht_explorenetworkjob.cpp | 35 +- test/dht/test_llarp_dht_kademlia.cpp | 34 +- test/dht/test_llarp_dht_key.cpp | 134 ++- test/dht/test_llarp_dht_node.cpp | 96 +- test/dht/test_llarp_dht_tx.cpp | 46 +- test/dht/test_llarp_dht_txowner.cpp | 79 +- test/llarp_test.cpp | 1 - test/llarp_test.hpp | 18 +- test/net/test_llarp_net.cpp | 131 +-- test/router/test_llarp_router_version.cpp | 74 +- .../llarp_routing_transfer_traffic.cpp | 24 - .../test_llarp_routing_obtainexitmessage.cpp | 32 +- .../test_llarp_routing_transfer_traffic.cpp | 24 + test/service/test_llarp_service_address.cpp | 111 ++- test/test_llarp_encrypted_frame.cpp | 27 +- test/util/meta/test_llarp_util_memfn.cpp | 42 +- test/util/meta/test_llarp_util_traits.cpp | 114 +-- test/util/test_llarp_util_aligned.cpp | 372 ++++---- test/util/test_llarp_util_bencode.cpp | 393 ++++---- .../util/test_llarp_util_decaying_hashset.cpp | 18 +- test/util/test_llarp_util_log_level.cpp | 79 +- test/util/thread/test_llarp_util_queue.cpp | 269 +++--- .../thread/test_llarp_util_queue_manager.cpp | 843 ++++++++---------- win32-setup/lokinet-win32.iss | 1 - 36 files changed, 1857 insertions(+), 2172 deletions(-) delete mode 100644 test/crypto/mock_crypto.cpp delete mode 100644 test/dht/mock_context.cpp delete mode 100644 test/llarp_test.cpp delete mode 100644 test/routing/llarp_routing_transfer_traffic.cpp create mode 100644 test/routing/test_llarp_routing_transfer_traffic.cpp diff --git a/.drone.jsonnet b/.drone.jsonnet index b3d530f39..06bf9e00e 100644 --- a/.drone.jsonnet +++ b/.drone.jsonnet @@ -56,7 +56,6 @@ local debian_pipeline(name, image, '-DWITH_LTO=' + (if lto then 'ON ' else 'OFF ') + cmake_extra, 'ninja -v', - '../contrib/ci/drone-gdb.sh ./test/testAll --gtest_color=yes', '../contrib/ci/drone-gdb.sh ./test/catchAll --use-colour yes', ] + extra_cmds, } @@ -200,7 +199,6 @@ local mac_builder(name, build_type='Release', werror=true, cmake_extra='', extra 'cmake .. -G Ninja -DCMAKE_CXX_FLAGS=-fcolor-diagnostics -DCMAKE_BUILD_TYPE='+build_type+' ' + (if werror then '-DWARNINGS_AS_ERRORS=ON ' else '') + cmake_extra, 'ninja -v', - './test/testAll --gtest_color=yes', './test/catchAll --use-colour yes', ] + extra_cmds, } diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 42ec51e78..2134cb6d2 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -8,84 +8,71 @@ if (WITH_HIVE) hive_build) endif() -# Old gtest-based tests; new tests should use Catch2, instead. -add_executable(testAll - # helpers - main.cpp - crypto/mock_crypto.cpp - dht/mock_context.cpp - test_util.cpp - # actual test cases - config/test_llarp_config_ini.cpp - crypto/test_llarp_crypto_types.cpp - crypto/test_llarp_crypto.cpp - crypto/test_llarp_key_manager.cpp - dht/test_llarp_dht_bucket.cpp - dht/test_llarp_dht_explorenetworkjob.cpp - dht/test_llarp_dht_kademlia.cpp - dht/test_llarp_dht_key.cpp - dht/test_llarp_dht_node.cpp - dht/test_llarp_dht_tx.cpp - dht/test_llarp_dht_txowner.cpp - llarp_test.cpp - net/test_llarp_net.cpp - router/test_llarp_router_version.cpp - routing/llarp_routing_transfer_traffic.cpp - routing/test_llarp_routing_obtainexitmessage.cpp - service/test_llarp_service_address.cpp - test_llarp_encrypted_frame.cpp - util/meta/test_llarp_util_memfn.cpp - util/meta/test_llarp_util_traits.cpp - util/test_llarp_util_aligned.cpp - util/test_llarp_util_bencode.cpp - util/test_llarp_util_log_level.cpp - util/thread/test_llarp_util_queue_manager.cpp - util/thread/test_llarp_util_queue.cpp -) - -target_link_libraries(testAll PUBLIC gmock gtest liblokinet) -target_include_directories(testAll PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) - -if(WIN32) - target_sources(testAll PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/win32/test.rc") - target_link_libraries(testAll PUBLIC ws2_32 iphlpapi shlwapi) -endif() - -if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") - target_link_directories(testAll PRIVATE /usr/local/lib) -endif() - add_subdirectory(Catch2) add_executable(catchAll - nodedb/test_nodedb.cpp - path/test_path.cpp - dns/test_llarp_dns_dns.cpp - regress/2020-06-08-key-backup-bug.cpp - util/test_llarp_util_bits.cpp - util/test_llarp_util_printer.cpp - util/test_llarp_util_str.cpp - util/test_llarp_util_decaying_hashset.cpp - peerstats/test_peer_db.cpp - peerstats/test_peer_types.cpp + # helpers + check_main.cpp + test_util.cpp + # actual test cases config/test_llarp_config_definition.cpp + config/test_llarp_config_ini.cpp config/test_llarp_config_output.cpp - net/test_ip_address.cpp - net/test_sock_addr.cpp - service/test_llarp_service_name.cpp + crypto/test_llarp_crypto_types.cpp + crypto/test_llarp_crypto.cpp + crypto/test_llarp_key_manager.cpp + dht/test_llarp_dht_bucket.cpp + dht/test_llarp_dht_explorenetworkjob.cpp + dht/test_llarp_dht_kademlia.cpp + dht/test_llarp_dht_key.cpp + dht/test_llarp_dht_node.cpp + dht/test_llarp_dht_tx.cpp + dht/test_llarp_dht_txowner.cpp + dns/test_llarp_dns_dns.cpp exit/test_llarp_exit_context.cpp iwp/test_iwp_session.cpp + net/test_ip_address.cpp + net/test_llarp_net.cpp + net/test_sock_addr.cpp + nodedb/test_nodedb.cpp + path/test_path.cpp + peerstats/test_peer_db.cpp + peerstats/test_peer_types.cpp + regress/2020-06-08-key-backup-bug.cpp + router/test_llarp_router_version.cpp + routing/test_llarp_routing_transfer_traffic.cpp + routing/test_llarp_routing_obtainexitmessage.cpp + service/test_llarp_service_address.cpp service/test_llarp_service_identity.cpp - test_util.cpp - test_llarp_router_contact.cpp - check_main.cpp) + service/test_llarp_service_name.cpp + util/meta/test_llarp_util_memfn.cpp + util/meta/test_llarp_util_traits.cpp + util/thread/test_llarp_util_queue_manager.cpp + util/thread/test_llarp_util_queue.cpp + util/test_llarp_util_aligned.cpp + util/test_llarp_util_bencode.cpp + util/test_llarp_util_bits.cpp + util/test_llarp_util_decaying_hashset.cpp + util/test_llarp_util_log_level.cpp + util/test_llarp_util_printer.cpp + util/test_llarp_util_str.cpp + test_llarp_encrypted_frame.cpp + test_llarp_router_contact.cpp) -target_link_libraries(catchAll PUBLIC liblokinet Catch2::Catch2) +target_link_libraries(catchAll PUBLIC gmock liblokinet Catch2::Catch2) target_include_directories(catchAll PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}) +if(WIN32) + target_sources(catchAll PUBLIC "${CMAKE_CURRENT_SOURCE_DIR}/win32/test.rc") + target_link_libraries(catchAll PUBLIC ws2_32 iphlpapi shlwapi) +endif() + +if(${CMAKE_SYSTEM_NAME} MATCHES "FreeBSD") + target_link_directories(catchAll PRIVATE /usr/local/lib) +endif() + # Custom targets to invoke the different test suites: add_custom_target(catch COMMAND catchAll) -add_custom_target(rungtest COMMAND testAll) # Add a custom "check" target that runs all the test suites: -add_custom_target(check DEPENDS rungtest catch) +add_custom_target(check DEPENDS catch) diff --git a/test/check_main.cpp b/test/check_main.cpp index 4ed06df1f..4e320cd30 100644 --- a/test/check_main.cpp +++ b/test/check_main.cpp @@ -1,2 +1,42 @@ -#define CATCH_CONFIG_MAIN +#define CATCH_CONFIG_RUNNER #include + +#include + +#include + +#ifdef _WIN32 +#include +int +startWinsock() +{ + WSADATA wsockd; + int err; + err = ::WSAStartup(MAKEWORD(2, 2), &wsockd); + if (err) + { + perror("Failed to start Windows Sockets"); + return err; + } + return 0; +} +#endif + +int +main(int argc, char* argv[]) +{ + llarp::LogSilencer shutup; + + ::testing::InitGoogleMock(&argc, argv); + +#ifdef _WIN32 + if (startWinsock()) + return -1; +#endif + + int result = Catch::Session().run(argc, argv); +#ifdef _WIN32 + WSACleanup(); +#endif + return result; +} diff --git a/test/config/test_llarp_config_ini.cpp b/test/config/test_llarp_config_ini.cpp index fdba409bc..06c187e2c 100644 --- a/test/config/test_llarp_config_ini.cpp +++ b/test/config/test_llarp_config_ini.cpp @@ -1,60 +1,56 @@ -#include - #include -struct TestINIParser : public ::testing::Test +#include + +TEST_CASE("ConfigParser", "[config]") { llarp::ConfigParser parser; - void - TearDown() + SECTION("Parse empty") { - parser.Clear(); + REQUIRE(parser.LoadFromStr("")); } -}; -TEST_F(TestINIParser, TestParseEmpty) -{ - ASSERT_TRUE(parser.LoadFromStr("")); -} + SECTION("Parse one section") + { + llarp::ConfigParser::SectionValues_t sect; + // this is an anti pattern don't write this kind of code with configpaser + auto assertVisit = [§](const auto& section) -> bool { + sect = section; + return true; + }; + REQUIRE(parser.LoadFromStr("[test]\nkey=val \n")); + REQUIRE(parser.VisitSection("test", assertVisit)); + auto itr = sect.find("notfound"); + REQUIRE(itr == sect.end()); + itr = sect.find("key"); + REQUIRE(itr != sect.end()); + REQUIRE(itr->second == "val"); + } -TEST_F(TestINIParser, TestParseOneSection) -{ - llarp::ConfigParser::SectionValues_t sect; - // this is an anti pattern don't write this kind of code with configpaser - auto assertVisit = [§](const auto& section) -> bool { - sect = section; - return true; - }; - ASSERT_TRUE(parser.LoadFromStr("[test]\nkey=val \n")); - ASSERT_TRUE(parser.VisitSection("test", assertVisit)); - auto itr = sect.find("notfound"); - ASSERT_EQ(itr, sect.end()); - itr = sect.find("key"); - ASSERT_NE(itr, sect.end()); - ASSERT_EQ(itr->second, "val"); -} + SECTION("Parse section duplicate keys") + { + REQUIRE(parser.LoadFromStr("[test]\nkey1=val1\nkey1=val2")); + size_t num = 0; + auto visit = [&num](const auto& section) -> bool { + num = section.count("key1"); + return true; + }; + REQUIRE(parser.VisitSection("test", visit)); + REQUIRE(num == size_t(2)); + } -TEST_F(TestINIParser, TestParseSectionDuplicateKeys) -{ - ASSERT_TRUE(parser.LoadFromStr("[test]\nkey1=val1\nkey1=val2")); - size_t num = 0; - auto visit = [&num](const auto& section) -> bool { - num = section.count("key1"); - return true; - }; - ASSERT_TRUE(parser.VisitSection("test", visit)); - ASSERT_EQ(num, size_t(2)); -} + SECTION("No key") + { + REQUIRE_FALSE(parser.LoadFromStr("[test]\n=1090\n")); + } -TEST_F(TestINIParser, TestNoKey) -{ - ASSERT_FALSE(parser.LoadFromStr("[test]\n=1090\n")); -} + SECTION("Parse invalid") + { + REQUIRE_FALSE( + parser.LoadFromStr("srged5ghe5\nf34wtge5\nw34tgfs4ygsd5yg=4;\n#" + "g4syhgd5\n")); + } -TEST_F(TestINIParser, TestParseInvalid) -{ - ASSERT_FALSE( - parser.LoadFromStr("srged5ghe5\nf34wtge5\nw34tgfs4ygsd5yg=4;\n#" - "g4syhgd5\n")); + parser.Clear(); } diff --git a/test/crypto/mock_crypto.cpp b/test/crypto/mock_crypto.cpp deleted file mode 100644 index ea7d94129..000000000 --- a/test/crypto/mock_crypto.cpp +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/test/crypto/mock_crypto.hpp b/test/crypto/mock_crypto.hpp index ad2bf0323..672145280 100644 --- a/test/crypto/mock_crypto.hpp +++ b/test/crypto/mock_crypto.hpp @@ -9,76 +9,67 @@ namespace llarp { namespace test { - struct MockCrypto final : public Crypto + struct MockCrypto : public Crypto { - MOCK_METHOD3(maybe_decrypt_name,std::optional>(std::string_view, llarp::SymmNonce, std::string_view)); - - - MOCK_METHOD3(xchacha20, - bool(const llarp_buffer_t &, const SharedSecret &, - const TunnelNonce &)); + MOCK_METHOD3( + maybe_decrypt_name, + std::optional>(std::string_view, llarp::SymmNonce, std::string_view)); - MOCK_METHOD4(xchacha20_alt, - bool(const llarp_buffer_t &, const llarp_buffer_t &, - const SharedSecret &, const byte_t *)); + MOCK_METHOD3(xchacha20, bool(const llarp_buffer_t&, const SharedSecret&, const TunnelNonce&)); - MOCK_METHOD4(dh_client, - bool(SharedSecret &, const PubKey &, const SecretKey &, - const TunnelNonce &)); + MOCK_METHOD4( + xchacha20_alt, + bool(const llarp_buffer_t&, const llarp_buffer_t&, const SharedSecret&, const byte_t*)); - MOCK_METHOD4(dh_server, - bool(SharedSecret &, const PubKey &, const SecretKey &, - const TunnelNonce &)); + MOCK_METHOD4( + dh_client, bool(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&)); - MOCK_METHOD4(transport_dh_client, - bool(SharedSecret &, const PubKey &, const SecretKey &, - const TunnelNonce &)); + MOCK_METHOD4( + dh_server, bool(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&)); - MOCK_METHOD4(transport_dh_server, - bool(SharedSecret &, const PubKey &, const SecretKey &, - const TunnelNonce &)); + MOCK_METHOD4( + transport_dh_client, + bool(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&)); - MOCK_METHOD2(hash, bool(byte_t *, const llarp_buffer_t &)); + MOCK_METHOD4( + transport_dh_server, + bool(SharedSecret&, const PubKey&, const SecretKey&, const TunnelNonce&)); - MOCK_METHOD2(shorthash, bool(ShortHash &, const llarp_buffer_t &)); + MOCK_METHOD2(hash, bool(byte_t*, const llarp_buffer_t&)); - MOCK_METHOD3(hmac, - bool(byte_t *, const llarp_buffer_t &, - const SharedSecret &)); + MOCK_METHOD2(shorthash, bool(ShortHash&, const llarp_buffer_t&)); - MOCK_METHOD4(derive_subkey, bool(PubKey &, const PubKey &, uint64_t, const AlignedBuffer<32> *)); + MOCK_METHOD3(hmac, bool(byte_t*, const llarp_buffer_t&, const SharedSecret&)); - MOCK_METHOD4(derive_subkey_private, - bool(PrivateKey &, const SecretKey &, uint64_t, const AlignedBuffer<32> *)); + MOCK_METHOD4(derive_subkey, bool(PubKey&, const PubKey&, uint64_t, const AlignedBuffer<32>*)); - MOCK_METHOD(bool, sign, (Signature &, const SecretKey &, const llarp_buffer_t &)); + MOCK_METHOD4( + derive_subkey_private, + bool(PrivateKey&, const SecretKey&, uint64_t, const AlignedBuffer<32>*)); - MOCK_METHOD(bool, sign, (Signature &, const PrivateKey &, const llarp_buffer_t &)); + MOCK_METHOD(bool, sign, (Signature&, const SecretKey&, const llarp_buffer_t&)); - MOCK_METHOD3(verify, - bool(const PubKey &, const llarp_buffer_t &, - const Signature &)); + MOCK_METHOD(bool, sign, (Signature&, const PrivateKey&, const llarp_buffer_t&)); - MOCK_METHOD2(seed_to_secretkey, - bool(llarp::SecretKey &, const llarp::IdentitySecret &)); + MOCK_METHOD3(verify, bool(const PubKey&, const llarp_buffer_t&, const Signature&)); - MOCK_METHOD1(randomize, void(const llarp_buffer_t &)); + MOCK_METHOD2(seed_to_secretkey, bool(llarp::SecretKey&, const llarp::IdentitySecret&)); - MOCK_METHOD2(randbytes, void(byte_t *, size_t)); + MOCK_METHOD1(randomize, void(const llarp_buffer_t&)); - MOCK_METHOD1(identity_keygen, void(SecretKey &)); + MOCK_METHOD2(randbytes, void(byte_t*, size_t)); - MOCK_METHOD1(encryption_keygen, void(SecretKey &)); + MOCK_METHOD1(identity_keygen, void(SecretKey&)); - MOCK_METHOD1(pqe_keygen, void(PQKeyPair &)); + MOCK_METHOD1(encryption_keygen, void(SecretKey&)); - MOCK_METHOD3(pqe_decrypt, - bool(const PQCipherBlock &, SharedSecret &, const byte_t *)); + MOCK_METHOD1(pqe_keygen, void(PQKeyPair&)); - MOCK_METHOD3(pqe_encrypt, - bool(PQCipherBlock &, SharedSecret &, const PQPubKey &)); + MOCK_METHOD3(pqe_decrypt, bool(const PQCipherBlock&, SharedSecret&, const byte_t*)); - MOCK_METHOD1(check_identity_privkey, bool(const SecretKey &)); + MOCK_METHOD3(pqe_encrypt, bool(PQCipherBlock&, SharedSecret&, const PQPubKey&)); + + MOCK_METHOD1(check_identity_privkey, bool(const SecretKey&)); }; } // namespace test } // namespace llarp diff --git a/test/crypto/test_llarp_crypto.cpp b/test/crypto/test_llarp_crypto.cpp index 44009e74b..34747e53d 100644 --- a/test/crypto/test_llarp_crypto.cpp +++ b/test/crypto/test_llarp_crypto.cpp @@ -2,69 +2,48 @@ #include -#include +#include -namespace llarp +using namespace llarp; + +TEST_CASE("Identity key") { - struct IdentityKeyTest : public ::testing::Test - { - llarp::sodium::CryptoLibSodium crypto; + llarp::sodium::CryptoLibSodium crypto; + SecretKey secret; + crypto.identity_keygen(secret); - IdentityKeyTest() - { - } - }; - - TEST_F(IdentityKeyTest, TestKeyGen) + SECTION("Keygen") { - SecretKey secret; - crypto.identity_keygen(secret); - ASSERT_FALSE(secret.IsZero()); + REQUIRE_FALSE(secret.IsZero()); } - TEST_F(IdentityKeyTest, TestSignVerify) + SECTION("Sign-verify") { - SecretKey secret; - crypto.identity_keygen(secret); - AlignedBuffer< 128 > random; + AlignedBuffer<128> random; random.Randomize(); Signature sig; const PubKey pk = secret.toPublic(); const llarp_buffer_t buf(random.data(), random.size()); - ASSERT_TRUE(crypto.sign(sig, secret, buf)); - ASSERT_TRUE(crypto.verify(pk, buf, sig)); + REQUIRE(crypto.sign(sig, secret, buf)); + REQUIRE(crypto.verify(pk, buf, sig)); random.Randomize(); // mangle body - ASSERT_FALSE(crypto.verify(pk, buf, sig)); + REQUIRE_FALSE(crypto.verify(pk, buf, sig)); } +} - struct PQCryptoTest : public ::testing::Test - { - llarp::sodium::CryptoLibSodium crypto; - PQKeyPair keys; +TEST_CASE("PQ crypto") +{ + llarp::sodium::CryptoLibSodium crypto; + PQKeyPair keys; + crypto.pqe_keygen(keys); + PQCipherBlock block; + SharedSecret shared, otherShared; + auto c = &crypto; - PQCryptoTest() - { - } - - void - SetUp() - { - crypto.pqe_keygen(keys); - } - }; - - TEST_F(PQCryptoTest, TestCrypto) - { - PQCipherBlock block; - SharedSecret shared, otherShared; - auto c = &crypto; - - ASSERT_TRUE(keys.size() == PQ_KEYPAIRSIZE); - ASSERT_TRUE( - c->pqe_encrypt(block, shared, PQPubKey(pq_keypair_to_public(keys)))); - ASSERT_TRUE(c->pqe_decrypt(block, otherShared, pq_keypair_to_secret(keys))); - ASSERT_TRUE(otherShared == shared); - } -} // namespace llarp + REQUIRE(keys.size() == PQ_KEYPAIRSIZE); + REQUIRE(c->pqe_encrypt(block, shared, PQPubKey(pq_keypair_to_public(keys)))); + REQUIRE(c->pqe_decrypt(block, otherShared, pq_keypair_to_secret(keys))); + REQUIRE(otherShared == shared); +} diff --git a/test/crypto/test_llarp_crypto_types.cpp b/test/crypto/test_llarp_crypto_types.cpp index 65c8c69d1..0110fa216 100644 --- a/test/crypto/test_llarp_crypto_types.cpp +++ b/test/crypto/test_llarp_crypto_types.cpp @@ -4,7 +4,7 @@ #include #include -#include +#include // This used to be implied via the headers above *shrug* #ifdef _WIN32 @@ -17,43 +17,37 @@ struct ToStringData std::string output; }; -struct PubKeyString : public ::testing::TestWithParam< ToStringData > -{ -}; - -TEST_P(PubKeyString, tostring) -{ - auto d = GetParam(); - llarp::PubKey key(d.input); - - ASSERT_EQ(key.ToString(), d.output); -} - -TEST_P(PubKeyString, fromstring) -{ - auto d = GetParam(); - - llarp::PubKey key; - - ASSERT_TRUE(key.FromString(d.output)); - ASSERT_EQ(key, llarp::PubKey(d.input)); -} - llarp::PubKey::Data empty = {}; -llarp::PubKey::Data full = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; +llarp::PubKey::Data full = {{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; // clang-format off -ToStringData toStringData[] = { +std::vector toStringData{ {empty, "0000000000000000000000000000000000000000000000000000000000000000"}, {full, "ffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffffff"}, }; // clang-format on -INSTANTIATE_TEST_SUITE_P(TestCryptoTypes, PubKeyString, - ::testing::ValuesIn(toStringData)); +TEST_CASE("PubKey-string conversion") +{ + auto d = GENERATE(from_range(toStringData)); + + SECTION("To string") + { + llarp::PubKey key(d.input); + + REQUIRE(key.ToString() == d.output); + } + + SECTION("From string") + { + llarp::PubKey key; + + REQUIRE(key.FromString(d.output)); + REQUIRE(key == llarp::PubKey(d.input)); + } +} // Concerns // - file missing @@ -63,32 +57,31 @@ INSTANTIATE_TEST_SUITE_P(TestCryptoTypes, PubKeyString, // - raw buffer // - bencoded -struct TestCryptoTypesSecret : public ::testing::Test +struct TestCryptoTypesSecret { std::string filename; fs::path p; TestCryptoTypesSecret() : filename(llarp::test::randFilename()), p(filename) - { - } + {} }; -TEST_F(TestCryptoTypesSecret, secret_key_from_file_missing) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_from_file_missing") { // Verify loading an empty file fails cleanly. - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); llarp::SecretKey key; - ASSERT_FALSE(key.LoadFromFile(filename.c_str())); + REQUIRE_FALSE(key.LoadFromFile(filename.c_str())); // Verify we didn't create a file - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); } -TEST_F(TestCryptoTypesSecret, secret_key_from_file_empty) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_from_file_empty") { // Verify loading an empty file fails cleanly. - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); // Create empty file std::fstream f; @@ -98,156 +91,154 @@ TEST_F(TestCryptoTypesSecret, secret_key_from_file_empty) llarp::test::FileGuard guard(p); llarp::SecretKey key; - ASSERT_FALSE(key.LoadFromFile(filename.c_str())); + REQUIRE_FALSE(key.LoadFromFile(filename.c_str())); // Verify we didn't delete the file - ASSERT_TRUE(fs::exists(fs::status(fs::path(filename)))); + REQUIRE(fs::exists(fs::status(fs::path(filename)))); } -TEST_F(TestCryptoTypesSecret, secret_key_from_file_smaller) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_from_file_smaller") { // Verify loading a file which is too small fails cleanly. - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); // Create empty file std::fstream f; f.open(filename, std::ios::out | std::ios::binary); - std::fill_n(std::ostream_iterator< byte_t >(f), llarp::SecretKey::SIZE / 2, - 0xAA); + std::fill_n(std::ostream_iterator(f), llarp::SecretKey::SIZE / 2, 0xAA); f.close(); llarp::test::FileGuard guard(p); llarp::SecretKey key; - ASSERT_FALSE(key.LoadFromFile(filename.c_str())); + REQUIRE_FALSE(key.LoadFromFile(filename.c_str())); // Verify we didn't delete the file - ASSERT_TRUE(fs::exists(fs::status(fs::path(filename)))); + REQUIRE(fs::exists(fs::status(fs::path(filename)))); } -TEST_F(TestCryptoTypesSecret, secret_key_from_file_smaller_bencode) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_from_file_smaller_bencode") { // Verify loading a file which is too small fails cleanly. - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); // Create empty file std::fstream f; f.open(filename, std::ios::out | std::ios::binary); f.write("32:", 3); - std::fill_n(std::ostream_iterator< byte_t >(f), 32, 0xAA); + std::fill_n(std::ostream_iterator(f), 32, 0xAA); f.close(); llarp::test::FileGuard guard(p); llarp::SecretKey key; - ASSERT_FALSE(key.LoadFromFile(filename.c_str())); + REQUIRE_FALSE(key.LoadFromFile(filename.c_str())); // Verify we didn't delete the file - ASSERT_TRUE(fs::exists(fs::status(fs::path(filename)))); + REQUIRE(fs::exists(fs::status(fs::path(filename)))); } -TEST_F(TestCryptoTypesSecret, secret_key_from_file_smaller_corrupt_bencode) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_from_file_smaller_corrupt_bencode") { // Verify loading a file which is too small + corrupt fails cleanly. - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); // Create empty file std::fstream f; f.open(filename, std::ios::out | std::ios::binary); f.write("256:", 4); - std::fill_n(std::ostream_iterator< byte_t >(f), 32, 0xAA); + std::fill_n(std::ostream_iterator(f), 32, 0xAA); f.close(); llarp::test::FileGuard guard(p); llarp::SecretKey key; - ASSERT_FALSE(key.LoadFromFile(filename.c_str())); + REQUIRE_FALSE(key.LoadFromFile(filename.c_str())); // Verify we didn't delete the file - ASSERT_TRUE(fs::exists(fs::status(fs::path(filename)))); + REQUIRE(fs::exists(fs::status(fs::path(filename)))); } -TEST_F(TestCryptoTypesSecret, secret_key_from_file_larger) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_from_file_larger") { // Verify loading a file which is too large fails cleanly. - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); // Create empty file std::fstream f; f.open(filename, std::ios::out | std::ios::binary); - std::fill_n(std::ostream_iterator< byte_t >(f), llarp::SecretKey::SIZE * 2, - 0xAA); + std::fill_n(std::ostream_iterator(f), llarp::SecretKey::SIZE * 2, 0xAA); f.close(); llarp::test::FileGuard guard(p); llarp::SecretKey key; - ASSERT_FALSE(key.LoadFromFile(filename.c_str())); + REQUIRE_FALSE(key.LoadFromFile(filename.c_str())); // Verify we didn't delete the file - ASSERT_TRUE(fs::exists(fs::status(fs::path(filename)))); + REQUIRE(fs::exists(fs::status(fs::path(filename)))); } -TEST_F(TestCryptoTypesSecret, secret_key_from_file_larger_bencode) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_from_file_larger_bencode") { // Verify loading a file which is too large fails cleanly. - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); // Create empty file std::fstream f; f.open(filename, std::ios::out | std::ios::binary); f.write("256:", 4); - std::fill_n(std::ostream_iterator< byte_t >(f), 256, 0xAA); + std::fill_n(std::ostream_iterator(f), 256, 0xAA); f.close(); llarp::test::FileGuard guard(p); llarp::SecretKey key; - ASSERT_FALSE(key.LoadFromFile(filename.c_str())); + REQUIRE_FALSE(key.LoadFromFile(filename.c_str())); // Verify we didn't delete the file - ASSERT_TRUE(fs::exists(fs::status(fs::path(filename)))); + REQUIRE(fs::exists(fs::status(fs::path(filename)))); } -TEST_F(TestCryptoTypesSecret, secret_key_from_file_happy_raw) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_from_file_happy_raw") { // Verify loading a valid raw file succeeds. - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); // Create empty file std::fstream f; f.open(filename, std::ios::out | std::ios::binary); - std::fill_n(std::ostream_iterator< byte_t >(f), llarp::SecretKey::SIZE, 0xAA); + std::fill_n(std::ostream_iterator(f), llarp::SecretKey::SIZE, 0xAA); f.close(); llarp::test::FileGuard guard(p); llarp::SecretKey key; - ASSERT_TRUE(key.LoadFromFile(filename.c_str())); + REQUIRE(key.LoadFromFile(filename.c_str())); // Verify we didn't delete the file - ASSERT_TRUE(fs::exists(fs::status(fs::path(filename)))); + REQUIRE(fs::exists(fs::status(fs::path(filename)))); } -TEST_F(TestCryptoTypesSecret, secret_key_from_file_happy_bencode) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_from_file_happy_bencode") { // Verify loading a valid bencoded file succeeds. - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); // Create empty file std::fstream f; f.open(filename, std::ios::out | std::ios::binary); f.write("64:", 4); - std::fill_n(std::ostream_iterator< byte_t >(f), llarp::SecretKey::SIZE, 0xAA); + std::fill_n(std::ostream_iterator(f), llarp::SecretKey::SIZE, 0xAA); f.close(); llarp::test::FileGuard guard(p); llarp::SecretKey key; - ASSERT_TRUE(key.LoadFromFile(filename.c_str())); + REQUIRE(key.LoadFromFile(filename.c_str())); // Verify we didn't delete the file - ASSERT_TRUE(fs::exists(fs::status(fs::path(filename)))); + REQUIRE(fs::exists(fs::status(fs::path(filename)))); } // Save to file @@ -261,15 +252,24 @@ TEST_F(TestCryptoTypesSecret, secret_key_from_file_happy_bencode) BOOL IsRunAsAdmin() { - BOOL fIsRunAsAdmin = FALSE; - DWORD dwError = ERROR_SUCCESS; + BOOL fIsRunAsAdmin = FALSE; + DWORD dwError = ERROR_SUCCESS; PSID pAdministratorsGroup = NULL; // Allocate and initialize a SID of the administrators group. SID_IDENTIFIER_AUTHORITY NtAuthority = SECURITY_NT_AUTHORITY; - if(!AllocateAndInitializeSid(&NtAuthority, 2, SECURITY_BUILTIN_DOMAIN_RID, - DOMAIN_ALIAS_RID_ADMINS, 0, 0, 0, 0, 0, 0, - &pAdministratorsGroup)) + if (!AllocateAndInitializeSid( + &NtAuthority, + 2, + SECURITY_BUILTIN_DOMAIN_RID, + DOMAIN_ALIAS_RID_ADMINS, + 0, + 0, + 0, + 0, + 0, + 0, + &pAdministratorsGroup)) { dwError = GetLastError(); goto Cleanup; @@ -277,7 +277,7 @@ IsRunAsAdmin() // Determine whether the SID of administrators group is enabled in // the primary access token of the process. - if(!CheckTokenMembership(NULL, pAdministratorsGroup, &fIsRunAsAdmin)) + if (!CheckTokenMembership(NULL, pAdministratorsGroup, &fIsRunAsAdmin)) { dwError = GetLastError(); goto Cleanup; @@ -285,14 +285,14 @@ IsRunAsAdmin() Cleanup: // Centralized cleanup for all allocated resources. - if(pAdministratorsGroup) + if (pAdministratorsGroup) { FreeSid(pAdministratorsGroup); pAdministratorsGroup = NULL; } // Throw the error if something failed in the function. - if(ERROR_SUCCESS != dwError) + if (ERROR_SUCCESS != dwError) { throw dwError; } @@ -301,46 +301,46 @@ Cleanup: } #endif -TEST_F(TestCryptoTypesSecret, secret_key_to_missing_file) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_to_missing_file") { // Verify writing to an unwritable file fails. // Assume we're not running as root, so can't write to [C:]/ // if we are root just skip this test #ifndef _WIN32 - if(getuid() == 0) + if (getuid() == 0) return; #else - if(IsRunAsAdmin()) + if (IsRunAsAdmin()) return; #endif filename = "/" + filename; - p = filename; - ASSERT_FALSE(fs::exists(fs::status(p))); + p = filename; + REQUIRE_FALSE(fs::exists(fs::status(p))); llarp::test::FileGuard guard(p); llarp::SecretKey key; - ASSERT_FALSE(key.SaveToFile(filename.c_str())); + REQUIRE_FALSE(key.SaveToFile(filename.c_str())); // Verify we didn't create the file - ASSERT_FALSE(fs::exists(fs::status(fs::path(filename)))); + REQUIRE_FALSE(fs::exists(fs::status(fs::path(filename)))); } -TEST_F(TestCryptoTypesSecret, secret_key_to_file) +TEST_CASE_METHOD(TestCryptoTypesSecret, "secret_key_to_file") { - ASSERT_FALSE(fs::exists(fs::status(p))); + REQUIRE_FALSE(fs::exists(fs::status(p))); llarp::test::FileGuard guard(p); llarp::SecretKey key; key.Randomize(); - ASSERT_TRUE(key.SaveToFile(filename.c_str())); + REQUIRE(key.SaveToFile(filename.c_str())); // Verify we created the file - ASSERT_TRUE(fs::exists(fs::status(fs::path(filename)))); + REQUIRE(fs::exists(fs::status(fs::path(filename)))); llarp::SecretKey other; other.LoadFromFile(filename.c_str()); - ASSERT_EQ(other, key); + REQUIRE(other == key); } diff --git a/test/crypto/test_llarp_key_manager.cpp b/test/crypto/test_llarp_key_manager.cpp index 527f2cca8..d508c205e 100644 --- a/test/crypto/test_llarp_key_manager.cpp +++ b/test/crypto/test_llarp_key_manager.cpp @@ -9,12 +9,12 @@ #include #include -#include +#include using namespace ::llarp; using namespace ::testing; -struct KeyManagerTest : public test::LlarpTest< llarp::sodium::CryptoLibSodium > +struct KeyManagerTest : public test::LlarpTest { // paranoid file guards for anything KeyManager might touch test::FileGuard m_rcFileGuard; @@ -27,8 +27,7 @@ struct KeyManagerTest : public test::LlarpTest< llarp::sodium::CryptoLibSodium > , m_encFileGuard(our_enc_key_filename) , m_transportFileGuard(our_transport_key_filename) , m_identFileGuard(our_identity_filename) - { - } + {} /// generate a valid "rc.signed" file bool @@ -39,10 +38,10 @@ struct KeyManagerTest : public test::LlarpTest< llarp::sodium::CryptoLibSodium > } }; -TEST_F(KeyManagerTest, TestBackupFileByMoving_MovesExistingFiles) +TEST_CASE_METHOD(KeyManagerTest, "Backup file by moving moves existing files") { fs::path p = test::randFilename(); - ASSERT_FALSE(fs::exists(p)); + REQUIRE_FALSE(fs::exists(p)); // touch file std::fstream f; @@ -51,33 +50,33 @@ TEST_F(KeyManagerTest, TestBackupFileByMoving_MovesExistingFiles) KeyManager::backupFileByMoving(p.string()); - ASSERT_FALSE(fs::exists(p)); + REQUIRE_FALSE(fs::exists(p)); fs::path moved = p.string() + ".0.bak"; - ASSERT_TRUE(fs::exists(moved)); + REQUIRE(fs::exists(moved)); test::FileGuard guard(moved); }; -TEST_F(KeyManagerTest, TestBackupFileByMoving_DoesntTouchNonExistentFiles) +TEST_CASE_METHOD(KeyManagerTest, "Backup file by moving doesnt touch non existent files") { fs::path p = test::randFilename(); - ASSERT_FALSE(fs::exists(p)); + REQUIRE_FALSE(fs::exists(p)); KeyManager::backupFileByMoving(p.string()); - ASSERT_FALSE(fs::exists(p)); + REQUIRE_FALSE(fs::exists(p)); fs::path moved = p.string() + ".0.bak"; - ASSERT_FALSE(fs::exists(moved)); + REQUIRE_FALSE(fs::exists(moved)); } -TEST_F(KeyManagerTest, TestBackupFileByMoving_FailsIfBackupNamesAreExausted) +TEST_CASE_METHOD(KeyManagerTest, "Backup file by moving fails if backup names are exausted") { fs::path base = test::randFilename(); - ASSERT_FALSE(fs::exists(base)); + REQUIRE_FALSE(fs::exists(base)); // touch file { @@ -93,9 +92,9 @@ TEST_F(KeyManagerTest, TestBackupFileByMoving_FailsIfBackupNamesAreExausted) guards.reserve(numBackupNames); // generate backup files foo.0.bak through foo.9.bak - for (uint32_t i=0; i \ No newline at end of file diff --git a/test/dht/mock_context.hpp b/test/dht/mock_context.hpp index dac797fb0..bfd4a4dc8 100644 --- a/test/dht/mock_context.hpp +++ b/test/dht/mock_context.hpp @@ -9,71 +9,99 @@ namespace llarp { namespace test { - struct MockContext final : public dht::AbstractContext + struct MockContext : public dht::AbstractContext { MOCK_CONST_METHOD1(StoreRC, void(const RouterContact)); MOCK_METHOD2(LookupRouter, bool(const RouterID&, RouterLookupHandler)); - MOCK_METHOD5(LookupRouterRecursive, - void(const RouterID&, const dht::Key_t&, uint64_t, - const dht::Key_t&, RouterLookupHandler)); + MOCK_METHOD5( + LookupRouterRecursive, + void( + const RouterID&, + const dht::Key_t&, + uint64_t, + const dht::Key_t&, + RouterLookupHandler)); - MOCK_METHOD6(LookupIntroSetRelayed, - void(const dht::Key_t&, const dht::Key_t&, uint64_t, - const dht::Key_t&, uint64_t, - service::EncryptedIntroSetLookupHandler)); + MOCK_METHOD6( + LookupIntroSetRelayed, + void( + const dht::Key_t&, + const dht::Key_t&, + uint64_t, + const dht::Key_t&, + uint64_t, + service::EncryptedIntroSetLookupHandler)); - MOCK_METHOD5(LookupIntroSetDirect, - void(const dht::Key_t&, const dht::Key_t&, uint64_t, - const dht::Key_t&, - service::EncryptedIntroSetLookupHandler)); + MOCK_METHOD5( + LookupIntroSetDirect, + void( + const dht::Key_t&, + const dht::Key_t&, + uint64_t, + const dht::Key_t&, + service::EncryptedIntroSetLookupHandler)); MOCK_CONST_METHOD1(HasRouterLookup, bool(const RouterID& target)); - MOCK_METHOD4(LookupRouterForPath, - void(const RouterID& target, uint64_t txid, - const PathID_t& path, const dht::Key_t& askpeer)); + MOCK_METHOD4( + LookupRouterForPath, + void( + const RouterID& target, + uint64_t txid, + const PathID_t& path, + const dht::Key_t& askpeer)); - MOCK_METHOD5(LookupIntroSetForPath, - void(const dht::Key_t&, uint64_t, const PathID_t&, - const dht::Key_t&, uint64_t)); + MOCK_METHOD5( + LookupIntroSetForPath, + void(const dht::Key_t&, uint64_t, const PathID_t&, const dht::Key_t&, uint64_t)); MOCK_METHOD3(DHTSendTo, void(const RouterID&, dht::IMessage*, bool)); MOCK_METHOD4( HandleExploritoryRouterLookup, - bool(const dht::Key_t& requester, uint64_t txid, - const RouterID& target, - std::vector< std::unique_ptr< dht::IMessage > >& reply)); + bool( + const dht::Key_t& requester, + uint64_t txid, + const RouterID& target, + std::vector>& reply)); MOCK_METHOD5( LookupRouterRelayed, - void(const dht::Key_t& requester, uint64_t txid, - const dht::Key_t& target, bool recursive, - std::vector< std::unique_ptr< dht::IMessage > >& replies)); + void( + const dht::Key_t& requester, + uint64_t txid, + const dht::Key_t& target, + bool recursive, + std::vector>& replies)); - MOCK_METHOD2(RelayRequestForPath, - bool(const PathID_t& localPath, const dht::IMessage& msg)); + MOCK_METHOD2(RelayRequestForPath, bool(const PathID_t& localPath, const dht::IMessage& msg)); - MOCK_CONST_METHOD2(GetRCFromNodeDB, - bool(const dht::Key_t& k, RouterContact& rc)); + MOCK_CONST_METHOD2(GetRCFromNodeDB, bool(const dht::Key_t& k, RouterContact& rc)); - MOCK_METHOD5(PropagateIntroSetTo, - void(const dht::Key_t& source, uint64_t sourceTX, - const service::EncryptedIntroSet& introset, - const dht::Key_t& peer, uint64_t relayOrder)); - MOCK_METHOD5(PropagateLocalIntroSet, - void(const PathID_t& source, uint64_t sourceTX, - const service::EncryptedIntroSet& introset, - const dht::Key_t& peer, uint64_t relayOrder)); + MOCK_METHOD5( + PropagateIntroSetTo, + void( + const dht::Key_t& source, + uint64_t sourceTX, + const service::EncryptedIntroSet& introset, + const dht::Key_t& peer, + uint64_t relayOrder)); + MOCK_METHOD5( + PropagateLocalIntroSet, + void( + const PathID_t& source, + uint64_t sourceTX, + const service::EncryptedIntroSet& introset, + const dht::Key_t& peer, + uint64_t relayOrder)); - MOCK_METHOD2(Init, - void(const dht::Key_t&, AbstractRouter*)); + MOCK_METHOD2(Init, void(const dht::Key_t&, AbstractRouter*)); - MOCK_CONST_METHOD1(GetIntroSetByLocation, - std::optional< llarp::service::EncryptedIntroSet >( - const llarp::dht::Key_t&)); + MOCK_CONST_METHOD1( + GetIntroSetByLocation, + std::optional(const llarp::dht::Key_t&)); MOCK_CONST_METHOD0(ExtractStatus, util::StatusObject()); @@ -85,8 +113,7 @@ namespace llarp MOCK_CONST_METHOD0(OurKey, const dht::Key_t&()); - MOCK_CONST_METHOD0(pendingIntrosetLookups, - const PendingIntrosetLookups&()); + MOCK_CONST_METHOD0(pendingIntrosetLookups, const PendingIntrosetLookups&()); MOCK_METHOD0(pendingIntrosetLookups, PendingIntrosetLookups&()); MOCK_METHOD0(pendingRouterLookups, PendingRouterLookups&()); @@ -97,12 +124,12 @@ namespace llarp MOCK_CONST_METHOD0(pendingExploreLookups, const PendingExploreLookups&()); - MOCK_METHOD0(services, dht::Bucket< dht::ISNode >*()); + MOCK_METHOD0(services, dht::Bucket*()); MOCK_CONST_METHOD0(AllowTransit, const bool&()); MOCK_METHOD0(AllowTransit, bool&()); - MOCK_CONST_METHOD0(Nodes, dht::Bucket< dht::RCNode >*()); + MOCK_CONST_METHOD0(Nodes, dht::Bucket*()); MOCK_METHOD1(PutRCNodeAsync, void(const dht::RCNode& val)); MOCK_METHOD1(DelRCNodeAsync, void(const dht::Key_t& val)); diff --git a/test/dht/test_llarp_dht_bucket.cpp b/test/dht/test_llarp_dht_bucket.cpp index eadd42c13..2150ecef0 100644 --- a/test/dht/test_llarp_dht_bucket.cpp +++ b/test/dht/test_llarp_dht_bucket.cpp @@ -2,22 +2,22 @@ #include #include -#include +#include -using Key_t = llarp::dht::Key_t; -using Value_t = llarp::dht::RCNode; -using Bucket_t = llarp::dht::Bucket< Value_t >; +using Key_t = llarp::dht::Key_t; +using Value_t = llarp::dht::RCNode; +using Bucket_t = llarp::dht::Bucket; -class TestDhtBucket : public ::testing::Test +class TestDhtBucket { public: TestDhtBucket() : randInt(0) { us.Fill(16); - nodes = std::make_unique< Bucket_t >(us, [&]() { return randInt++; }); + nodes = std::make_unique(us, [&]() { return randInt++; }); size_t numNodes = 10; - byte_t fill = 1; - while(numNodes) + byte_t fill = 1; + while (numNodes) { Value_t n; n.ID.Fill(fill); @@ -30,10 +30,10 @@ class TestDhtBucket : public ::testing::Test uint64_t randInt; llarp::dht::Key_t us; - std::unique_ptr< Bucket_t > nodes; + std::unique_ptr nodes; }; -TEST_F(TestDhtBucket, simple_cycle) +TEST_CASE_METHOD(TestDhtBucket, "Simple cycle", "[dht]") { // Empty the current bucket. nodes->Clear(); @@ -45,27 +45,27 @@ TEST_F(TestDhtBucket, simple_cycle) nodes->PutNode(val); // Verify the value is in the bucket - ASSERT_TRUE(nodes->HasNode(val.ID)); - ASSERT_EQ(1u, nodes->size()); + REQUIRE(nodes->HasNode(val.ID)); + REQUIRE(1u == nodes->size()); // Verify after deletion, the value is no longer in the bucket nodes->DelNode(val.ID); - ASSERT_FALSE(nodes->HasNode(val.ID)); + REQUIRE_FALSE(nodes->HasNode(val.ID)); // Verify deleting again succeeds; nodes->DelNode(val.ID); - ASSERT_FALSE(nodes->HasNode(val.ID)); + REQUIRE_FALSE(nodes->HasNode(val.ID)); } -TEST_F(TestDhtBucket, get_random_node_excluding) +TEST_CASE_METHOD(TestDhtBucket, "get_random_node_excluding") { // Empty the current bucket. nodes->Clear(); // We expect not to find anything Key_t result; - std::set< Key_t > excludeSet; - ASSERT_FALSE(nodes->GetRandomNodeExcluding(result, excludeSet)); + std::set excludeSet; + REQUIRE_FALSE(nodes->GetRandomNodeExcluding(result, excludeSet)); // Create a simple value. Value_t val; @@ -73,24 +73,24 @@ TEST_F(TestDhtBucket, get_random_node_excluding) // Add the simple value to the exclude set excludeSet.insert(val.ID); - ASSERT_FALSE(nodes->GetRandomNodeExcluding(result, excludeSet)); + REQUIRE_FALSE(nodes->GetRandomNodeExcluding(result, excludeSet)); // Add the simple value to the bucket nodes->PutNode(val); - ASSERT_FALSE(nodes->GetRandomNodeExcluding(result, excludeSet)); + REQUIRE_FALSE(nodes->GetRandomNodeExcluding(result, excludeSet)); excludeSet.clear(); - ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet)); - ASSERT_EQ(val.ID, result); + REQUIRE(nodes->GetRandomNodeExcluding(result, excludeSet)); + REQUIRE(val.ID == result); // Add an element to the exclude set which isn't the bucket. Key_t other; other.Fill(0xff); excludeSet.insert(other); - ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet)); - ASSERT_EQ(val.ID, result); + REQUIRE(nodes->GetRandomNodeExcluding(result, excludeSet)); + REQUIRE(val.ID == result); // Add a node which is in both bucket and excludeSet Value_t nextVal; @@ -98,28 +98,28 @@ TEST_F(TestDhtBucket, get_random_node_excluding) excludeSet.insert(nextVal.ID); nodes->PutNode(nextVal); - ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet)); - ASSERT_EQ(val.ID, result); + REQUIRE(nodes->GetRandomNodeExcluding(result, excludeSet)); + REQUIRE(val.ID == result); // Clear the excludeSet - we should still have 2 nodes in the bucket excludeSet.clear(); randInt = 0; - ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet)); - ASSERT_EQ(val.ID, result); + REQUIRE(nodes->GetRandomNodeExcluding(result, excludeSet)); + REQUIRE(val.ID == result); // Set the random value to be 1, we should get the other node. randInt = 1; - ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet)); - ASSERT_EQ(nextVal.ID, result); + REQUIRE(nodes->GetRandomNodeExcluding(result, excludeSet)); + REQUIRE(nextVal.ID == result); // Set the random value to be 100, we should get the first node. randInt = 100; - ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet)); - ASSERT_EQ(val.ID, result); + REQUIRE(nodes->GetRandomNodeExcluding(result, excludeSet)); + REQUIRE(val.ID == result); } -TEST_F(TestDhtBucket, find_closest) +TEST_CASE_METHOD(TestDhtBucket, "find_closest", "[dht]") { // Empty the current bucket. nodes->Clear(); @@ -129,182 +129,182 @@ TEST_F(TestDhtBucket, find_closest) target.Fill(0xF0); Key_t result; - ASSERT_FALSE(nodes->FindClosest(target, result)); + REQUIRE_FALSE(nodes->FindClosest(target, result)); // Add a node to the bucket Value_t first; first.ID.Zero(); nodes->PutNode(first); - ASSERT_TRUE(nodes->FindClosest(target, result)); - ASSERT_EQ(result, first.ID); + REQUIRE(nodes->FindClosest(target, result)); + REQUIRE(result == first.ID); // Add another node to the bucket, closer to the target Value_t second; second.ID.Fill(0x10); nodes->PutNode(second); - ASSERT_TRUE(nodes->FindClosest(target, result)); - ASSERT_EQ(result, second.ID); + REQUIRE(nodes->FindClosest(target, result)); + REQUIRE(result == second.ID); // Add a third node to the bucket, closer to the target Value_t third; third.ID.Fill(0x20); nodes->PutNode(third); - ASSERT_TRUE(nodes->FindClosest(target, result)); - ASSERT_EQ(result, third.ID); + REQUIRE(nodes->FindClosest(target, result)); + REQUIRE(result == third.ID); // Add a fourth node to the bucket, greater than the target Value_t fourth; fourth.ID.Fill(0xF1); nodes->PutNode(fourth); - ASSERT_TRUE(nodes->FindClosest(target, result)); - ASSERT_EQ(result, fourth.ID); + REQUIRE(nodes->FindClosest(target, result)); + REQUIRE(result == fourth.ID); // Add a fifth node to the bucket, equal to the target Value_t fifth; fifth.ID.Fill(0xF0); nodes->PutNode(fifth); - ASSERT_TRUE(nodes->FindClosest(target, result)); - ASSERT_EQ(result, fifth.ID); + REQUIRE(nodes->FindClosest(target, result)); + REQUIRE(result == fifth.ID); } -TEST_F(TestDhtBucket, get_many_random) +TEST_CASE_METHOD(TestDhtBucket, "get_many_random", "[dht]") { // Empty the current bucket. nodes->Clear(); // Verify behaviour with empty node set - std::set< Key_t > result; - ASSERT_FALSE(nodes->GetManyRandom(result, 0)); - ASSERT_FALSE(nodes->GetManyRandom(result, 1)); + std::set result; + REQUIRE_FALSE(nodes->GetManyRandom(result, 0)); + REQUIRE_FALSE(nodes->GetManyRandom(result, 1)); // Add 5 nodes to the bucket - std::set< Value_t > curValues; - std::set< Key_t > curKeys; - for(byte_t i = 0x00; i < 0x05; ++i) + std::set curValues; + std::set curKeys; + for (byte_t i = 0x00; i < 0x05; ++i) { Value_t v; v.ID.Fill(i); - ASSERT_TRUE(curKeys.insert(v.ID).second); + REQUIRE(curKeys.insert(v.ID).second); nodes->PutNode(v); } // Fetching more than the current size fails - ASSERT_EQ(5u, nodes->size()); - ASSERT_FALSE(nodes->GetManyRandom(result, nodes->size() + 1)); + REQUIRE(5u == nodes->size()); + REQUIRE_FALSE(nodes->GetManyRandom(result, nodes->size() + 1)); // Fetching the current size succeeds - ASSERT_TRUE(nodes->GetManyRandom(result, nodes->size())); - ASSERT_EQ(curKeys, result); + REQUIRE(nodes->GetManyRandom(result, nodes->size())); + REQUIRE(curKeys == result); // Fetching a subset succeeds. // Note we hack this by "fixing" the random number generator result.clear(); - ASSERT_TRUE(nodes->GetManyRandom(result, 1u)); - ASSERT_EQ(1u, result.size()); - ASSERT_EQ(*curKeys.begin(), *result.begin()); + REQUIRE(nodes->GetManyRandom(result, 1u)); + REQUIRE(1u == result.size()); + REQUIRE(*curKeys.begin() == *result.begin()); randInt = 0; result.clear(); - ASSERT_TRUE(nodes->GetManyRandom(result, nodes->size() - 1)); - ASSERT_EQ(nodes->size() - 1, result.size()); - ASSERT_EQ(std::set< Key_t >(++curKeys.rbegin(), curKeys.rend()), result); + REQUIRE(nodes->GetManyRandom(result, nodes->size() - 1)); + REQUIRE(nodes->size() - 1 == result.size()); + REQUIRE(std::set(++curKeys.rbegin(), curKeys.rend()) == result); } -TEST_F(TestDhtBucket, find_close_excluding) +TEST_CASE_METHOD(TestDhtBucket, "find_close_excluding", "[dht]") { // Empty the current bucket. nodes->Clear(); Key_t target; target.Zero(); - std::set< Key_t > exclude; + std::set exclude; Key_t result; // Empty node + exclude set fails - ASSERT_FALSE(nodes->FindCloseExcluding(target, result, exclude)); + REQUIRE_FALSE(nodes->FindCloseExcluding(target, result, exclude)); Value_t first; first.ID.Fill(0xF0); exclude.insert(first.ID); // Empty nodes fails - ASSERT_FALSE(nodes->FindCloseExcluding(target, result, exclude)); + REQUIRE_FALSE(nodes->FindCloseExcluding(target, result, exclude)); // Nodes and exclude set match nodes->PutNode(first); - ASSERT_FALSE(nodes->FindCloseExcluding(target, result, exclude)); + REQUIRE_FALSE(nodes->FindCloseExcluding(target, result, exclude)); // Exclude set empty exclude.clear(); - ASSERT_TRUE(nodes->FindCloseExcluding(target, result, exclude)); + REQUIRE(nodes->FindCloseExcluding(target, result, exclude)); result = first.ID; Value_t second; second.ID.Fill(0x01); nodes->PutNode(second); - ASSERT_TRUE(nodes->FindCloseExcluding(target, result, exclude)); + REQUIRE(nodes->FindCloseExcluding(target, result, exclude)); result = second.ID; exclude.insert(second.ID); - ASSERT_TRUE(nodes->FindCloseExcluding(target, result, exclude)); + REQUIRE(nodes->FindCloseExcluding(target, result, exclude)); result = first.ID; } -TEST_F(TestDhtBucket, find_many_near_excluding) +TEST_CASE_METHOD(TestDhtBucket, "find_many_near_excluding", "[dht]") { // Empty the current bucket. nodes->Clear(); Key_t target; target.Zero(); - std::set< Key_t > exclude; - std::set< Key_t > result; + std::set exclude; + std::set result; // Empty node + exclude set, with size 0 succeeds - ASSERT_TRUE(nodes->GetManyNearExcluding(target, result, 0, exclude)); - ASSERT_EQ(0u, result.size()); + REQUIRE(nodes->GetManyNearExcluding(target, result, 0, exclude)); + REQUIRE(0u == result.size()); // Empty node + exclude set fails - ASSERT_FALSE(nodes->GetManyNearExcluding(target, result, 1, exclude)); + REQUIRE_FALSE(nodes->GetManyNearExcluding(target, result, 1, exclude)); Value_t first; first.ID.Fill(0xF0); exclude.insert(first.ID); // Empty nodes fails - ASSERT_FALSE(nodes->GetManyNearExcluding(target, result, 1, exclude)); + REQUIRE_FALSE(nodes->GetManyNearExcluding(target, result, 1, exclude)); // Nodes and exclude set match nodes->PutNode(first); - ASSERT_FALSE(nodes->GetManyNearExcluding(target, result, 1, exclude)); + REQUIRE_FALSE(nodes->GetManyNearExcluding(target, result, 1, exclude)); // Single node succeeds exclude.clear(); - ASSERT_TRUE(nodes->GetManyNearExcluding(target, result, 1, exclude)); - ASSERT_EQ(result, std::set< Key_t >({first.ID})); + REQUIRE(nodes->GetManyNearExcluding(target, result, 1, exclude)); + REQUIRE(result == std::set({first.ID})); // Trying to grab 2 nodes from a 1 node set fails result.clear(); - ASSERT_FALSE(nodes->GetManyNearExcluding(target, result, 2, exclude)); + REQUIRE_FALSE(nodes->GetManyNearExcluding(target, result, 2, exclude)); // two nodes finds closest Value_t second; second.ID.Fill(0x01); nodes->PutNode(second); result.clear(); - ASSERT_TRUE(nodes->GetManyNearExcluding(target, result, 1, exclude)); - ASSERT_EQ(result, std::set< Key_t >({second.ID})); + REQUIRE(nodes->GetManyNearExcluding(target, result, 1, exclude)); + REQUIRE(result == std::set({second.ID})); // 3 nodes finds 2 closest Value_t third; third.ID.Fill(0x02); nodes->PutNode(third); result.clear(); - ASSERT_TRUE(nodes->GetManyNearExcluding(target, result, 2, exclude)); - ASSERT_EQ(result, std::set< Key_t >({second.ID, third.ID})); + REQUIRE(nodes->GetManyNearExcluding(target, result, 2, exclude)); + REQUIRE(result == std::set({second.ID, third.ID})); // 4 nodes, one in exclude set finds 2 closest Value_t fourth; @@ -312,27 +312,27 @@ TEST_F(TestDhtBucket, find_many_near_excluding) nodes->PutNode(fourth); exclude.insert(third.ID); result.clear(); - ASSERT_TRUE(nodes->GetManyNearExcluding(target, result, 2, exclude)); - ASSERT_EQ(result, std::set< Key_t >({second.ID, fourth.ID})); + REQUIRE(nodes->GetManyNearExcluding(target, result, 2, exclude)); + REQUIRE(result == std::set({second.ID, fourth.ID})); } -TEST_F(TestDhtBucket, TestBucketFindClosest) +TEST_CASE_METHOD(TestDhtBucket, "Bucket: FindClosest", "[dht]") { llarp::dht::Key_t result; llarp::dht::Key_t target; target.Fill(5); - ASSERT_TRUE(nodes->FindClosest(target, result)); - ASSERT_EQ(target, result); + REQUIRE(nodes->FindClosest(target, result)); + REQUIRE(target == result); const llarp::dht::Key_t oldResult = result; target.Fill(0xf5); - ASSERT_TRUE(nodes->FindClosest(target, result)); - ASSERT_EQ(oldResult, result); + REQUIRE(nodes->FindClosest(target, result)); + REQUIRE(oldResult == result); } -TEST_F(TestDhtBucket, TestBucketRandomized_1000) +TEST_CASE_METHOD(TestDhtBucket, "Bucket: randomized 1000", "[dht]") { size_t moreNodes = 100; - while(moreNodes--) + while (moreNodes--) { llarp::dht::RCNode n; n.ID.Fill(randInt); @@ -340,31 +340,30 @@ TEST_F(TestDhtBucket, TestBucketRandomized_1000) nodes->PutNode(n); } const size_t count = 1000; - size_t left = count; - while(left--) + size_t left = count; + while (left--) { llarp::dht::Key_t result; llarp::dht::Key_t target; target.Randomize(); const llarp::dht::Key_t expect = target; - ASSERT_TRUE(nodes->FindClosest(target, result)); - if(target == result) + REQUIRE(nodes->FindClosest(target, result)); + if (target == result) { - ASSERT_GE(result ^ target, expect ^ target); - ASSERT_EQ(result ^ target, expect ^ target); - ASSERT_EQ(result ^ target, expect ^ target); + REQUIRE((result ^ target) >= (expect ^ target)); + REQUIRE((result ^ target) == (expect ^ target)); + REQUIRE((result ^ target) == (expect ^ target)); } else { - Key_t dist = result ^ target; - Key_t oldDist = expect ^ target; - ASSERT_NE(result ^ target, expect ^ target); + Key_t dist = (result ^ target); + Key_t oldDist = (expect ^ target); + REQUIRE((result ^ target) != (expect ^ target)); - ASSERT_GE(result ^ target, expect ^ target) - << "result=" << result << "expect=" << expect << std::endl - << dist << ">=" << oldDist << "iteration=" << (count - left); + INFO(dist << ">=" << oldDist << "iteration=" << (count - left)); + REQUIRE((result ^ target) >= (expect ^ target)); - ASSERT_NE(result ^ target, expect ^ target); + REQUIRE((result ^ target) != (expect ^ target)); } } } diff --git a/test/dht/test_llarp_dht_explorenetworkjob.cpp b/test/dht/test_llarp_dht_explorenetworkjob.cpp index 2fc683afe..8b05cc299 100644 --- a/test/dht/test_llarp_dht_explorenetworkjob.cpp +++ b/test/dht/test_llarp_dht_explorenetworkjob.cpp @@ -4,38 +4,42 @@ #include #include -#include +#include +#include using namespace llarp; using namespace ::testing; using test::makeBuf; -struct TestDhtExploreNetworkJob : public ::testing::Test +struct TestDhtExploreNetworkJob { RouterID peer; test::MockContext context; dht::ExploreNetworkJob exploreNetworkJob; - TestDhtExploreNetworkJob() - : peer(makeBuf< RouterID >(0x01)), exploreNetworkJob(peer, &context) + TestDhtExploreNetworkJob() : peer(makeBuf(0x01)), exploreNetworkJob(peer, &context) + {} + + ~TestDhtExploreNetworkJob() { + CHECK(Mock::VerifyAndClearExpectations(&context)); } }; -TEST_F(TestDhtExploreNetworkJob, validate) +TEST_CASE_METHOD(TestDhtExploreNetworkJob, "validate", "[dht]") { - const RouterID other = makeBuf< RouterID >(0x02); - ASSERT_TRUE(exploreNetworkJob.Validate(other)); + const RouterID other = makeBuf(0x02); + REQUIRE(exploreNetworkJob.Validate(other)); } -TEST_F(TestDhtExploreNetworkJob, start) +TEST_CASE_METHOD(TestDhtExploreNetworkJob, "start", "[dht]") { // Verify input arguments are passed correctly. // The actual logic is inside the `dht::AbstractContext` implementation. - const auto txKey = makeBuf< dht::Key_t >(0x02); - uint64_t txId = 4; + const auto txKey = makeBuf(0x02); + uint64_t txId = 4; dht::TXOwner txOwner(txKey, txId); @@ -47,10 +51,11 @@ TEST_F(TestDhtExploreNetworkJob, start) ).Times(1); // clang-format off - ASSERT_NO_THROW(exploreNetworkJob.Start(txOwner)); + REQUIRE_NOTHROW(exploreNetworkJob.Start(txOwner)); } -TEST_F(TestDhtExploreNetworkJob, send_reply) +// TODO: sections? +TEST_CASE_METHOD(TestDhtExploreNetworkJob, "send_reply", "[dht]") { // Concerns: // - Empty collection @@ -62,7 +67,7 @@ TEST_F(TestDhtExploreNetworkJob, send_reply) EXPECT_CALL(context, LookupRouter(_, _)).Times(0); EXPECT_CALL(context, GetRouter()).WillOnce(Return(nullptr)); - ASSERT_NO_THROW(exploreNetworkJob.SendReply()); + REQUIRE_NOTHROW(exploreNetworkJob.SendReply()); } { @@ -75,7 +80,7 @@ TEST_F(TestDhtExploreNetworkJob, send_reply) EXPECT_CALL(context, LookupRouter(Ne(makeBuf(0x01)), _)).Times(2).WillRepeatedly(Return(true)); EXPECT_CALL(context, LookupRouter(Eq(makeBuf(0x01)), _)).WillOnce(Return(false)); - ASSERT_NO_THROW(exploreNetworkJob.SendReply()); + REQUIRE_NOTHROW(exploreNetworkJob.SendReply()); } { @@ -87,6 +92,6 @@ TEST_F(TestDhtExploreNetworkJob, send_reply) EXPECT_CALL(context, GetRouter()).WillOnce(Return(nullptr)); EXPECT_CALL(context, LookupRouter(_, _)).Times(3).WillRepeatedly(Return(true)); - ASSERT_NO_THROW(exploreNetworkJob.SendReply()); + REQUIRE_NOTHROW(exploreNetworkJob.SendReply()); } } diff --git a/test/dht/test_llarp_dht_kademlia.cpp b/test/dht/test_llarp_dht_kademlia.cpp index b1a1be842..6d907769b 100644 --- a/test/dht/test_llarp_dht_kademlia.cpp +++ b/test/dht/test_llarp_dht_kademlia.cpp @@ -1,10 +1,10 @@ #include -#include +#include using llarp::dht::Key_t; -using Array = std::array< byte_t, Key_t::SIZE >; +using Array = std::array; struct XorMetricData { @@ -15,34 +15,21 @@ struct XorMetricData XorMetricData(const Array& u, const Array& l, const Array& r, bool res) : us(u), left(l), right(r), result(res) - { - } + {} }; std::ostream& operator<<(std::ostream& stream, const XorMetricData& x) { - stream << int(x.us[0]) << " " << int(x.left[0]) << " " << int(x.right[0]) - << " " << std::boolalpha << x.result; + stream << int(x.us[0]) << " " << int(x.left[0]) << " " << int(x.right[0]) << " " << std::boolalpha + << x.result; return stream; } - -struct XorMetric : public ::testing::TestWithParam< XorMetricData > -{ -}; - -TEST_P(XorMetric, test) -{ - auto d = GetParam(); - ASSERT_EQ(llarp::dht::XorMetric{Key_t{d.us}}(Key_t{d.left}, Key_t{d.right}), - d.result); -} - -std::vector< XorMetricData > +std::vector makeData() { - std::vector< XorMetricData > result; + std::vector result; Array zero; zero.fill(0); @@ -84,5 +71,8 @@ makeData() return result; } -INSTANTIATE_TEST_SUITE_P(TestDhtXorMetric, XorMetric, - ::testing::ValuesIn(makeData())); +TEST_CASE("XorMetric", "[dht]") +{ + auto d = GENERATE(from_range(makeData())); + REQUIRE(llarp::dht::XorMetric{Key_t{d.us}}(Key_t{d.left}, Key_t{d.right}) == d.result); +} diff --git a/test/dht/test_llarp_dht_key.cpp b/test/dht/test_llarp_dht_key.cpp index 54db9df06..59b13c6f4 100644 --- a/test/dht/test_llarp_dht_key.cpp +++ b/test/dht/test_llarp_dht_key.cpp @@ -1,105 +1,91 @@ -#include +#include #include using namespace llarp; -using Array = std::array< byte_t, dht::Key_t::SIZE >; +using Array = std::array; -struct DHT : public ::testing::TestWithParam< Array > -{ -}; +static constexpr Array emptyArray{{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, + 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; -TEST_P(DHT, constructor) +static constexpr Array fullArray{{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, + 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; + +static constexpr Array seqArray{{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, + 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, + 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}}; + +std::vector data{emptyArray, fullArray, seqArray}; + +TEST_CASE("DHT key constructor", "[dht]") { - auto d = GetParam(); + auto d = GENERATE(from_range(data)); dht::Key_t a(d); dht::Key_t b(d.data()); dht::Key_t c; - ASSERT_EQ(a, b); + REQUIRE(a == b); - if(a.IsZero()) + if (a.IsZero()) { - ASSERT_EQ(a, c); + REQUIRE(a == c); } else { - ASSERT_NE(a, c); + REQUIRE(a != c); } } -static constexpr Array emptyArray{ - {0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, - 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}}; - -static constexpr Array fullArray{ - {0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, - 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}}; - -static constexpr Array seqArray{ - {0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A, - 0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15, - 0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}}; - -static const Array data[] = {emptyArray, fullArray, seqArray}; - -INSTANTIATE_TEST_SUITE_P(TestDhtKey, DHT, ::testing::ValuesIn(data)); - -TEST(TestDhtKey, eq) +TEST_CASE("DHT key ==", "[dht]") { - ASSERT_EQ(dht::Key_t(emptyArray), dht::Key_t(emptyArray)); - ASSERT_EQ(dht::Key_t(fullArray), dht::Key_t(fullArray)); - ASSERT_EQ(dht::Key_t(seqArray), dht::Key_t(seqArray)); -} -TEST(TestDhtKey, ne) -{ - ASSERT_NE(dht::Key_t(emptyArray), dht::Key_t(fullArray)); - ASSERT_NE(dht::Key_t(emptyArray), dht::Key_t(seqArray)); - ASSERT_NE(dht::Key_t(fullArray), dht::Key_t(seqArray)); + REQUIRE(dht::Key_t(emptyArray) == dht::Key_t(emptyArray)); + REQUIRE(dht::Key_t(fullArray) == dht::Key_t(fullArray)); + REQUIRE(dht::Key_t(seqArray) == dht::Key_t(seqArray)); } -TEST(TestDhtKey, lt) +TEST_CASE("DHT key !=", "[dht]") { - ASSERT_LT(dht::Key_t(emptyArray), dht::Key_t(fullArray)); - ASSERT_LT(dht::Key_t(emptyArray), dht::Key_t(seqArray)); - ASSERT_LT(dht::Key_t(seqArray), dht::Key_t(fullArray)); + REQUIRE(dht::Key_t(emptyArray) != dht::Key_t(fullArray)); + REQUIRE(dht::Key_t(emptyArray) != dht::Key_t(seqArray)); + REQUIRE(dht::Key_t(fullArray) != dht::Key_t(seqArray)); } -TEST(TestDhtKey, gt) +TEST_CASE("DHT key <", "[dht]") { - ASSERT_GT(dht::Key_t(fullArray), dht::Key_t(emptyArray)); - ASSERT_GT(dht::Key_t(seqArray), dht::Key_t(emptyArray)); - ASSERT_GT(dht::Key_t(fullArray), dht::Key_t(seqArray)); + REQUIRE(dht::Key_t(emptyArray) < dht::Key_t(fullArray)); + REQUIRE(dht::Key_t(emptyArray) < dht::Key_t(seqArray)); + REQUIRE(dht::Key_t(seqArray) < dht::Key_t(fullArray)); } -TEST(TestDhtKey, XOR) +TEST_CASE("DHT key >", "[dht]") { - ASSERT_EQ(dht::Key_t(emptyArray), - dht::Key_t(emptyArray) ^ dht::Key_t(emptyArray)); + REQUIRE(dht::Key_t(fullArray) > dht::Key_t(emptyArray)); + REQUIRE(dht::Key_t(seqArray) > dht::Key_t(emptyArray)); + REQUIRE(dht::Key_t(fullArray) > dht::Key_t(seqArray)); +} - ASSERT_EQ(dht::Key_t(seqArray), - dht::Key_t(emptyArray) ^ dht::Key_t(seqArray)); +TEST_CASE("DHT key ^", "[dht]") +{ + REQUIRE(dht::Key_t(emptyArray) == (dht::Key_t(emptyArray) ^ dht::Key_t(emptyArray))); - ASSERT_EQ(dht::Key_t(fullArray), - dht::Key_t(emptyArray) ^ dht::Key_t(fullArray)); + REQUIRE(dht::Key_t(seqArray) == (dht::Key_t(emptyArray) ^ dht::Key_t(seqArray))); - ASSERT_EQ(dht::Key_t(emptyArray), - dht::Key_t(fullArray) ^ dht::Key_t(fullArray)); + REQUIRE(dht::Key_t(fullArray) == (dht::Key_t(emptyArray) ^ dht::Key_t(fullArray))); - ASSERT_EQ(dht::Key_t(emptyArray), - dht::Key_t(seqArray) ^ dht::Key_t(seqArray)); + REQUIRE(dht::Key_t(emptyArray) == (dht::Key_t(fullArray) ^ dht::Key_t(fullArray))); + + REQUIRE(dht::Key_t(emptyArray) == (dht::Key_t(seqArray) ^ dht::Key_t(seqArray))); Array xorResult; std::iota(xorResult.rbegin(), xorResult.rend(), 0xE0); - ASSERT_EQ(dht::Key_t(xorResult), - dht::Key_t(seqArray) ^ dht::Key_t(fullArray)); + REQUIRE(dht::Key_t(xorResult) == (dht::Key_t(seqArray) ^ dht::Key_t(fullArray))); } -TEST(TestDhtKey, TestBucketOperators) +TEST_CASE("DHT key: test bucket operators", "[dht]") { dht::Key_t zero; dht::Key_t one; @@ -108,16 +94,16 @@ TEST(TestDhtKey, TestBucketOperators) zero.Zero(); one.Fill(1); three.Fill(3); - ASSERT_LT(zero, one); - ASSERT_LT(zero, three); - ASSERT_FALSE(zero > one); - ASSERT_FALSE(zero > three); - ASSERT_NE(zero, three); - ASSERT_FALSE(zero == three); - ASSERT_EQ(zero ^ one, one); - ASSERT_LT(one, three); - ASSERT_GT(three, one); - ASSERT_NE(one, three); - ASSERT_FALSE(one == three); - ASSERT_EQ(one ^ three, three ^ one); + REQUIRE(zero < one); + REQUIRE(zero < three); + REQUIRE_FALSE(zero > one); + REQUIRE_FALSE(zero > three); + REQUIRE(zero != three); + REQUIRE_FALSE(zero == three); + REQUIRE((zero ^ one) == one); + REQUIRE(one < three); + REQUIRE(three > one); + REQUIRE(one != three); + REQUIRE_FALSE(one == three); + REQUIRE((one ^ three) == (three ^ one)); } diff --git a/test/dht/test_llarp_dht_node.cpp b/test/dht/test_llarp_dht_node.cpp index 8eedbe874..eac7e7178 100644 --- a/test/dht/test_llarp_dht_node.cpp +++ b/test/dht/test_llarp_dht_node.cpp @@ -2,115 +2,105 @@ #include -#include -#include +#include using namespace llarp; -using namespace ::testing; using test::makeBuf; -struct TestDhtRCNode : public ::testing::Test -{ -}; - -TEST_F(TestDhtRCNode, construct) +TEST_CASE("dht::RCNode construct", "[dht]") { dht::RCNode node; - ASSERT_THAT(node.ID, Property(&dht::Key_t::IsZero, true)); + REQUIRE(node.ID.IsZero()); node.ID.Fill(0xCA); node.rc.last_updated = 101s; dht::RCNode other{node}; - ASSERT_EQ(node.ID, other.ID); - ASSERT_EQ(node.rc, other.rc); + REQUIRE(node.ID == other.ID); + REQUIRE(node.rc == other.rc); RouterContact contact; contact.pubkey.Randomize(); dht::RCNode fromContact{contact}; - ASSERT_EQ(fromContact.ID.as_array(), contact.pubkey.as_array()); + REQUIRE(fromContact.ID.as_array() == contact.pubkey.as_array()); } -TEST_F(TestDhtRCNode, lt) +TEST_CASE("dht::RCNode <", "[dht]") { dht::RCNode one; dht::RCNode two; dht::RCNode three; dht::RCNode eqThree; - one.rc.last_updated = 1s; - two.rc.last_updated = 2s; - three.rc.last_updated = 3s; + one.rc.last_updated = 1s; + two.rc.last_updated = 2s; + three.rc.last_updated = 3s; eqThree.rc.last_updated = 3s; // LT cases - ASSERT_THAT(one, Lt(two)); - ASSERT_THAT(one, Lt(three)); - ASSERT_THAT(one, Lt(eqThree)); - ASSERT_THAT(two, Lt(three)); - ASSERT_THAT(two, Lt(eqThree)); + REQUIRE(one < two); + REQUIRE(one < three); + REQUIRE(one < eqThree); + REQUIRE(two < three); + REQUIRE(two < eqThree); // !LT cases - ASSERT_THAT(one, Not(Lt(one))); - ASSERT_THAT(two, Not(Lt(one))); - ASSERT_THAT(two, Not(Lt(two))); - ASSERT_THAT(three, Not(Lt(one))); - ASSERT_THAT(three, Not(Lt(two))); - ASSERT_THAT(three, Not(Lt(three))); - ASSERT_THAT(three, Not(Lt(eqThree))); + REQUIRE(!(one < one)); + REQUIRE(!(two < one)); + REQUIRE(!(two < two)); + REQUIRE(!(three < one)); + REQUIRE(!(three < two)); + REQUIRE(!(three < three)); + REQUIRE(!(three < eqThree)); } -struct TestDhtISNode : public ::testing::Test -{ -}; - -TEST_F(TestDhtISNode, construct) +TEST_CASE("dht::ISNode construct", "[dht]") { dht::ISNode node; - ASSERT_THAT(node.ID, Property(&dht::Key_t::IsZero, true)); + REQUIRE(node.ID.IsZero()); node.ID.Fill(0xCA); node.introset.derivedSigningKey.Fill(0xDB); dht::ISNode other{node}; - ASSERT_EQ(node.ID, other.ID); - ASSERT_EQ(node.introset, other.introset); + REQUIRE(node.ID == other.ID); + REQUIRE(node.introset == other.introset); service::EncryptedIntroSet introSet; introSet.derivedSigningKey.Randomize(); dht::ISNode fromIntro{introSet}; - ASSERT_EQ(fromIntro.ID.as_array(), introSet.derivedSigningKey); + REQUIRE(fromIntro.ID.as_array() == introSet.derivedSigningKey); } -TEST_F(TestDhtISNode, lt) +TEST_CASE("dht::ISNode <", "[dht]") { dht::ISNode one; dht::ISNode two; dht::ISNode three; dht::ISNode eqThree; - one.introset.signedAt = 1s; - two.introset.signedAt = 2s; - three.introset.signedAt = 3s; + one.introset.signedAt = 1s; + two.introset.signedAt = 2s; + three.introset.signedAt = 3s; eqThree.introset.signedAt = 3s; // LT cases - ASSERT_THAT(one, Lt(two)); - ASSERT_THAT(one, Lt(three)); - ASSERT_THAT(one, Lt(eqThree)); - ASSERT_THAT(two, Lt(three)); - ASSERT_THAT(two, Lt(eqThree)); + REQUIRE(one < two); + REQUIRE(one < three); + REQUIRE(one < eqThree); + REQUIRE(two < three); + REQUIRE(two < eqThree); // !LT cases - ASSERT_THAT(one, Not(Lt(one))); - ASSERT_THAT(two, Not(Lt(one))); - ASSERT_THAT(two, Not(Lt(two))); - ASSERT_THAT(three, Not(Lt(one))); - ASSERT_THAT(three, Not(Lt(two))); - ASSERT_THAT(three, Not(Lt(three))); - ASSERT_THAT(three, Not(Lt(eqThree))); + REQUIRE(!(one < one)); + REQUIRE(!(two < one)); + REQUIRE(!(two < two)); + REQUIRE(!(three < one)); + REQUIRE(!(three < two)); + REQUIRE(!(three < three)); + REQUIRE(!(three < eqThree)); } diff --git a/test/dht/test_llarp_dht_tx.cpp b/test/dht/test_llarp_dht_tx.cpp index cf88085c6..504ccddb9 100644 --- a/test/dht/test_llarp_dht_tx.cpp +++ b/test/dht/test_llarp_dht_tx.cpp @@ -2,7 +2,7 @@ #include #include -#include +#include #include using namespace llarp; @@ -13,13 +13,11 @@ using llarp::test::makeBuf; using Val_t = llarp::service::Tag; // Mock implementation of TX. -struct TestTx final : public dht::TX< dht::Key_t, Val_t > +struct TestTx : public dht::TX { - TestTx(const dht::TXOwner& asker, const dht::Key_t& k, - dht::AbstractContext* p) - : dht::TX< dht::Key_t, Val_t >(asker, k, p) - { - } + TestTx(const dht::TXOwner& asker, const dht::Key_t& k, dht::AbstractContext* p) + : dht::TX(asker, k, p) + {} MOCK_CONST_METHOD1(Validate, bool(const Val_t&)); @@ -28,18 +26,23 @@ struct TestTx final : public dht::TX< dht::Key_t, Val_t > MOCK_METHOD0(SendReply, void()); }; -struct TestDhtTx : public Test +struct TestDhtTx { dht::TXOwner asker; dht::Key_t m_key; TestTx tx; TestDhtTx() : tx(asker, m_key, nullptr) + {} + + ~TestDhtTx() { + CHECK(Mock::VerifyAndClearExpectations(&tx)); } }; -TEST_F(TestDhtTx, on_found) +// TODO: sections? +TEST_CASE_METHOD(TestDhtTx, "on_found", "[dht]") { // Concerns // - Validate returns true @@ -48,7 +51,7 @@ TEST_F(TestDhtTx, on_found) // - Repeated call on failure // - Repeated call on success after failure - const auto key = makeBuf< dht::Key_t >(0x00); + const auto key = makeBuf(0x00); Val_t val("good value"); // Validate returns true @@ -57,19 +60,19 @@ TEST_F(TestDhtTx, on_found) tx.OnFound(key, val); - ASSERT_THAT(tx.peersAsked, Contains(key)); - ASSERT_THAT(tx.valuesFound, Contains(val)); + REQUIRE(tx.peersAsked.count(key) > 0); + REQUIRE_THAT(tx.valuesFound, Catch::VectorContains(val)); } // Repeated call on success { EXPECT_CALL(tx, Validate(val)).WillOnce(Return(true)); tx.OnFound(key, val); - ASSERT_THAT(tx.peersAsked, Contains(key)); - ASSERT_THAT(tx.valuesFound, Contains(val)); + REQUIRE(tx.peersAsked.count(key) > 0); + REQUIRE_THAT(tx.valuesFound, Catch::VectorContains(val)); } - const auto key1 = makeBuf< dht::Key_t >(0x01); + const auto key1 = makeBuf(0x01); Val_t badVal("bad value"); // Validate returns false @@ -78,8 +81,8 @@ TEST_F(TestDhtTx, on_found) tx.OnFound(key1, badVal); - ASSERT_THAT(tx.peersAsked, Contains(key1)); - ASSERT_THAT(tx.valuesFound, Not(Contains(badVal))); + REQUIRE(tx.peersAsked.count(key1) > 0); + REQUIRE_THAT(tx.valuesFound, !Catch::VectorContains(badVal)); } // Repeated call on failure @@ -88,8 +91,8 @@ TEST_F(TestDhtTx, on_found) tx.OnFound(key1, badVal); - ASSERT_THAT(tx.peersAsked, Contains(key1)); - ASSERT_THAT(tx.valuesFound, Not(Contains(badVal))); + REQUIRE(tx.peersAsked.count(key1) > 0); + REQUIRE_THAT(tx.valuesFound, !Catch::VectorContains(badVal)); } // Repeated call on success after failure @@ -98,8 +101,7 @@ TEST_F(TestDhtTx, on_found) tx.OnFound(key1, badVal); - ASSERT_THAT(tx.peersAsked, Contains(key1)); - ASSERT_THAT(tx.valuesFound, Contains(badVal)); + REQUIRE(tx.peersAsked.count(key1) > 0); + REQUIRE_THAT(tx.valuesFound, Catch::VectorContains(badVal)); } } - diff --git a/test/dht/test_llarp_dht_txowner.cpp b/test/dht/test_llarp_dht_txowner.cpp index 49891fb19..585b9db60 100644 --- a/test/dht/test_llarp_dht_txowner.cpp +++ b/test/dht/test_llarp_dht_txowner.cpp @@ -1,6 +1,6 @@ #include -#include +#include namespace { @@ -13,37 +13,22 @@ namespace uint64_t id; size_t expectedHash; - TxOwnerData(const Key_t& k, uint64_t i, size_t h) - : node(k), id(i), expectedHash(h) - { - } + TxOwnerData(const Key_t& k, uint64_t i, size_t h) : node(k), id(i), expectedHash(h) + {} }; - struct TxOwner : public ::testing::TestWithParam< TxOwnerData > - { - }; - - TEST_F(TxOwner, default_construct) + TEST_CASE("TxOwner default construct", "[dht]") { TXOwner dc; - ASSERT_TRUE(dc.node.IsZero()); - ASSERT_EQ(0u, dc.txid); - ASSERT_EQ(0u, TXOwner::Hash()(dc)); + REQUIRE(dc.node.IsZero()); + REQUIRE(0u == dc.txid); + REQUIRE(0u == TXOwner::Hash()(dc)); } - TEST_P(TxOwner, hash) - { - // test single interactions (constructor and hash) - auto d = GetParam(); - TXOwner constructor(d.node, d.id); - - ASSERT_EQ(d.expectedHash, TXOwner::Hash()(constructor)); - } - - std::vector< TxOwnerData > + std::vector makeData() { - std::vector< TxOwnerData > result; + std::vector result; Key_t zero; zero.Zero(); @@ -52,7 +37,7 @@ namespace Key_t two; two.Fill(0x02); - uint64_t max = std::numeric_limits< uint64_t >::max(); + uint64_t max = std::numeric_limits::max(); result.emplace_back(zero, 0, 0ull); result.emplace_back(zero, 1, 1ull); @@ -67,6 +52,15 @@ namespace return result; } + TEST_CASE("TxOwner hash", "[dht]") + { + // test single interactions (constructor and hash) + auto d = GENERATE(from_range(makeData())); + TXOwner constructor(d.node, d.id); + + REQUIRE(d.expectedHash == TXOwner::Hash()(constructor)); + } + struct TxOwnerCmpData { TXOwner lhs; @@ -76,27 +70,13 @@ namespace TxOwnerCmpData(const TXOwner& l, const TXOwner& r, bool e, bool ls) : lhs(l), rhs(r), equal(e), less(ls) - { - } + {} }; - struct TxOwnerOps : public ::testing::TestWithParam< TxOwnerCmpData > - { - }; - - TEST_P(TxOwnerOps, operators) - { - // test single interactions (constructor and hash) - auto d = GetParam(); - - ASSERT_EQ(d.lhs == d.rhs, d.equal); - ASSERT_EQ(d.lhs < d.rhs, d.less); - } - - std::vector< TxOwnerCmpData > + std::vector makeCmpData() { - std::vector< TxOwnerCmpData > result; + std::vector result; Key_t zero; zero.Fill(0x00); @@ -114,10 +94,13 @@ namespace return result; } + + TEST_CASE("TxOwner ops", "[dht]") + { + // test single interactions (constructor and hash) + auto d = GENERATE(from_range(makeCmpData())); + + REQUIRE((d.lhs == d.rhs) == d.equal); + REQUIRE((d.lhs < d.rhs) == d.less); + } } // namespace - -INSTANTIATE_TEST_SUITE_P(TestDhtTxOwner, TxOwner, - ::testing::ValuesIn(makeData())); - -INSTANTIATE_TEST_SUITE_P(TestDhtTxOwner, TxOwnerOps, - ::testing::ValuesIn(makeCmpData())); diff --git a/test/llarp_test.cpp b/test/llarp_test.cpp deleted file mode 100644 index 9fb89d970..000000000 --- a/test/llarp_test.cpp +++ /dev/null @@ -1 +0,0 @@ -#include diff --git a/test/llarp_test.hpp b/test/llarp_test.hpp index 123e14df2..90ac3bb37 100644 --- a/test/llarp_test.hpp +++ b/test/llarp_test.hpp @@ -1,15 +1,16 @@ #ifndef LLARP_TEST #define LLARP_TEST -#include #include +#include +#include namespace llarp { namespace test { - template < typename CryptoImpl = MockCrypto > - class LlarpTest : public ::testing::Test + template + class LlarpTest { protected: CryptoImpl m_crypto; @@ -17,9 +18,18 @@ namespace llarp LlarpTest() : cm(&m_crypto) { - static_assert(std::is_base_of< Crypto, CryptoImpl >::value, ""); + static_assert(std::is_base_of::value, ""); } + + ~LlarpTest() + {} }; + + template <> + inline LlarpTest::~LlarpTest() + { + CHECK(::testing::Mock::VerifyAndClearExpectations(&m_crypto)); + } } // namespace test } // namespace llarp diff --git a/test/net/test_llarp_net.cpp b/test/net/test_llarp_net.cpp index 2ce6f8918..9c504e83b 100644 --- a/test/net/test_llarp_net.cpp +++ b/test/net/test_llarp_net.cpp @@ -1,99 +1,104 @@ -#include - #include #include #include #include -struct TestNet : public ::testing::Test -{ -}; +#include -TEST_F(TestNet, TestIn6AddrFromString) +TEST_CASE("In6Addr") { llarp::huint128_t ip; - ASSERT_TRUE(ip.FromString("fc00::1")); + + SECTION("From string") + { + REQUIRE(ip.FromString("fc00::1")); + } + + SECTION("From string fail") + { + REQUIRE_FALSE(ip.FromString("10.1.1.1")); + } } -TEST_F(TestNet, TestIn6AddrFromStringFail) -{ - llarp::huint128_t ip; - ASSERT_FALSE(ip.FromString("10.1.1.1")); -} - -TEST_F(TestNet, TestIn6AddrToHUIntLoopback) +TEST_CASE("In6AddrToHUIntLoopback") { llarp::huint128_t loopback = {0}; - ASSERT_TRUE(loopback.FromString("::1")); + REQUIRE(loopback.FromString("::1")); in6_addr addr = IN6ADDR_LOOPBACK_INIT; auto huint = llarp::net::In6ToHUInt(addr); - ASSERT_EQ(huint, loopback); + REQUIRE(huint == loopback); } -TEST_F(TestNet, TestIn6AddrToHUInt) +TEST_CASE("In6AddrToHUInt") { llarp::huint128_t huint_parsed = {0}; - ASSERT_TRUE(huint_parsed.FromString("fd00::1")); + REQUIRE(huint_parsed.FromString("fd00::1")); in6_addr addr = {{{0xfd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}}}; auto huint = llarp::net::In6ToHUInt(addr); - ASSERT_EQ(huint, huint_parsed); + REQUIRE(huint == huint_parsed); huint_parsed.h++; - ASSERT_NE(huint, huint_parsed); + REQUIRE(huint != huint_parsed); } -TEST_F(TestNet, TestRangeContains8) +TEST_CASE("Range") { - ASSERT_TRUE( - llarp::IPRange::FromIPv4(10, 0, 0, 1, 8).Contains(llarp::ipaddr_ipv4_bits(10, 40, 11, 6))); + SECTION("Contains 8") + { + REQUIRE( + llarp::IPRange::FromIPv4(10, 0, 0, 1, 8).Contains(llarp::ipaddr_ipv4_bits(10, 40, 11, 6))); + } + + SECTION("Contains 24") + { + REQUIRE(llarp::IPRange::FromIPv4(10, 200, 0, 1, 24) + .Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253))); + } + + SECTION("Contains fail") + { + REQUIRE(!llarp::IPRange::FromIPv4(192, 168, 0, 1, 24) + .Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253))); + } } -TEST_F(TestNet, TestRangeContains24) +TEST_CASE("IPv4 netmask") { - ASSERT_TRUE(llarp::IPRange::FromIPv4(10, 200, 0, 1, 24) - .Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253))); + REQUIRE(llarp::netmask_ipv4_bits(8) == llarp::huint32_t{0xFF000000}); + REQUIRE(llarp::netmask_ipv4_bits(24) == llarp::huint32_t{0xFFFFFF00}); } -TEST_F(TestNet, TestRangeContainsFail) +TEST_CASE("Bogon") { - ASSERT_TRUE(!llarp::IPRange::FromIPv4(192, 168, 0, 1, 24) - .Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253))); -} + SECTION("Bogon_10_8") + { + REQUIRE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(10, 40, 11, 6))); + } -TEST_F(TestNet, TestIPv4Netmask) -{ - ASSERT_TRUE(llarp::netmask_ipv4_bits(8) == llarp::huint32_t{0xFF000000}); - ASSERT_TRUE(llarp::netmask_ipv4_bits(24) == llarp::huint32_t{0xFFFFFF00}); -} + SECTION("Bogon_192_168_16") + { + REQUIRE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(192, 168, 1, 111))); + } -TEST_F(TestNet, TestBogon_10_8) -{ - ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(10, 40, 11, 6))); -} + SECTION("Bogon_DoD_8") + { + REQUIRE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(21, 3, 37, 70))); + } -TEST_F(TestNet, TestBogon_192_168_16) -{ - ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(192, 168, 1, 111))); -} + SECTION("Bogon_127_8") + { + REQUIRE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(127, 0, 0, 1))); + } -TEST_F(TestNet, TestBogon_DoD_8) -{ - ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(21, 3, 37, 70))); -} + SECTION("Bogon_0_8") + { + REQUIRE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(0, 0, 0, 0))); + } -TEST_F(TestNet, TestBogon_127_8) -{ - ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(127, 0, 0, 1))); -} - -TEST_F(TestNet, TestBogon_0_8) -{ - ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(0, 0, 0, 0))); -} - -TEST_F(TestNet, TestBogon_NonBogon) -{ - ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(1, 1, 1, 1))); - ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(8, 8, 6, 6))); - ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(141, 55, 12, 99))); - ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(79, 12, 3, 4))); + SECTION("Non-bogon") + { + REQUIRE_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(1, 1, 1, 1))); + REQUIRE_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(8, 8, 6, 6))); + REQUIRE_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(141, 55, 12, 99))); + REQUIRE_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(79, 12, 3, 4))); + } } diff --git a/test/router/test_llarp_router_version.cpp b/test/router/test_llarp_router_version.cpp index d9a496b8a..01adc3cdf 100644 --- a/test/router/test_llarp_router_version.cpp +++ b/test/router/test_llarp_router_version.cpp @@ -1,91 +1,87 @@ -#include #include #include "router/router.hpp" -class TestRouterVersion : public ::testing::Test -{ -}; +#include -TEST_F(TestRouterVersion, TestCompatibilityWhenProtocolEqual) -{ - llarp::RouterVersion v1( {0, 1, 2}, 1); - llarp::RouterVersion v2( {0, 1, 2}, 1); +using Catch::Matchers::Equals; - EXPECT_TRUE(v1.IsCompatableWith(v2)); +TEST_CASE("Compatibility when protocol equal", "[RouterVersion]") +{ + llarp::RouterVersion v1({0, 1, 2}, 1); + llarp::RouterVersion v2({0, 1, 2}, 1); + + CHECK(v1.IsCompatableWith(v2)); } -TEST_F(TestRouterVersion, TestCompatibilityWhenProtocolUnequal) +TEST_CASE("Compatibility when protocol unequal", "[RouterVersion]") { - llarp::RouterVersion older( {0, 1, 2}, 1); - llarp::RouterVersion newer( {0, 1, 2}, 2); + llarp::RouterVersion older({0, 1, 2}, 1); + llarp::RouterVersion newer({0, 1, 2}, 2); - EXPECT_FALSE(older.IsCompatableWith(newer)); - EXPECT_FALSE(newer.IsCompatableWith(older)); + CHECK_FALSE(older.IsCompatableWith(newer)); + CHECK_FALSE(newer.IsCompatableWith(older)); } -TEST_F(TestRouterVersion, TestEmptyCompatibility) +TEST_CASE("Empty compatibility", "[RouterVersion]") { - llarp::RouterVersion v1( {0, 0, 1}, LLARP_PROTO_VERSION); + llarp::RouterVersion v1({0, 0, 1}, LLARP_PROTO_VERSION); - EXPECT_FALSE(v1.IsCompatableWith(llarp::emptyRouterVersion)); + CHECK_FALSE(v1.IsCompatableWith(llarp::emptyRouterVersion)); } -TEST_F(TestRouterVersion, TestIsEmpty) +TEST_CASE("IsEmpty", "[RouterVersion]") { - llarp::RouterVersion notEmpty( {0, 0, 1}, LLARP_PROTO_VERSION); - EXPECT_FALSE(notEmpty.IsEmpty()); + llarp::RouterVersion notEmpty({0, 0, 1}, LLARP_PROTO_VERSION); + CHECK_FALSE(notEmpty.IsEmpty()); - EXPECT_TRUE(llarp::emptyRouterVersion.IsEmpty()); + CHECK(llarp::emptyRouterVersion.IsEmpty()); } -TEST_F(TestRouterVersion, TestClear) +TEST_CASE("Clear", "[RouterVersion]") { - llarp::RouterVersion version( {0, 0, 1}, LLARP_PROTO_VERSION); - EXPECT_FALSE(version.IsEmpty()); + llarp::RouterVersion version({0, 0, 1}, LLARP_PROTO_VERSION); + CHECK_FALSE(version.IsEmpty()); version.Clear(); - EXPECT_TRUE(version.IsEmpty()); + CHECK(version.IsEmpty()); } -TEST_F(TestRouterVersion, TestBEncode) +TEST_CASE("BEncode", "[RouterVersion]") { - llarp::RouterVersion v1235( {1, 2, 3}, 5); + llarp::RouterVersion v1235({1, 2, 3}, 5); - std::array< byte_t, 128 > tmp{}; + std::array tmp{}; llarp_buffer_t buf(tmp); - EXPECT_TRUE(v1235.BEncode(&buf)); + CHECK(v1235.BEncode(&buf)); std::string s((const char*)buf.begin(), (buf.end() - buf.begin())); LogInfo("bencoded: ", buf.begin()); - EXPECT_STREQ((const char*)buf.begin(), "li5ei1ei2ei3ee"); - + CHECK_THAT((const char*)buf.begin(), Equals("li5ei1ei2ei3ee")); } -TEST_F(TestRouterVersion, TestBDecode) +TEST_CASE("BDecode", "[RouterVersion]") { llarp::RouterVersion version; version.Clear(); const std::string bString("li9ei3ei2ei1ee"); llarp_buffer_t buf(bString.data(), bString.size()); - EXPECT_TRUE(version.BDecode(&buf)); + CHECK(version.BDecode(&buf)); - llarp::RouterVersion expected( {3, 2, 1}, 9); - - EXPECT_EQ(expected, version); + llarp::RouterVersion expected({3, 2, 1}, 9); + CHECK(expected == version); } -TEST_F(TestRouterVersion, TestDecodeLongVersionArray) +TEST_CASE("Decode long version array", "[RouterVersion]") { llarp::RouterVersion version; version.Clear(); const std::string bString("li9ei3ei2ei1ei2ei3ei4ei5ei6ei7ei8ei9ee"); llarp_buffer_t buf(bString.data(), bString.size()); - EXPECT_FALSE(version.BDecode(&buf)); - + CHECK_FALSE(version.BDecode(&buf)); } diff --git a/test/routing/llarp_routing_transfer_traffic.cpp b/test/routing/llarp_routing_transfer_traffic.cpp deleted file mode 100644 index 3e70494a0..000000000 --- a/test/routing/llarp_routing_transfer_traffic.cpp +++ /dev/null @@ -1,24 +0,0 @@ -#include -#include - -using TransferTrafficMessage = llarp::routing::TransferTrafficMessage; - -class TransferTrafficTest : public ::testing::Test -{ -}; - -TEST_F(TransferTrafficTest, TestPutBufferOverflow) -{ - TransferTrafficMessage msg; - std::array< byte_t, llarp::routing::MaxExitMTU* 2 > tmp = {{0}}; - llarp_buffer_t buf(tmp); - ASSERT_FALSE(msg.PutBuffer(buf, 1)); -} - -TEST_F(TransferTrafficTest, TestPutBuffer) -{ - TransferTrafficMessage msg; - std::array< byte_t, llarp::routing::MaxExitMTU > tmp = {{0}}; - llarp_buffer_t buf(tmp); - ASSERT_TRUE(msg.PutBuffer(buf, 1)); -} diff --git a/test/routing/test_llarp_routing_obtainexitmessage.cpp b/test/routing/test_llarp_routing_obtainexitmessage.cpp index a6decdf53..f78cca32d 100644 --- a/test/routing/test_llarp_routing_obtainexitmessage.cpp +++ b/test/routing/test_llarp_routing_obtainexitmessage.cpp @@ -2,45 +2,37 @@ #include #include +#include #include -#include #include +#include + using namespace ::testing; using namespace ::llarp; +using namespace ::llarp::test; using ObtainExitMessage = routing::ObtainExitMessage; -class ObtainExitTest : public test::LlarpTest<> -{ - public: - SecretKey alice; - - ObtainExitTest() - { - // m_crypto.identity_keygen(alice); - } -}; - void fill(Signature& s) { s.Fill(0xFF); } -TEST_F(ObtainExitTest, TestSignVerify) +TEST_CASE_METHOD(LlarpTest<>, "Sign-verify") { - EXPECT_CALL(m_crypto, sign(_, alice, _)) - .WillOnce(DoAll(WithArg< 0 >(Invoke(&fill)), Return(true))); + SecretKey alice; + EXPECT_CALL(m_crypto, sign(_, alice, _)).WillOnce(DoAll(WithArg<0>(Invoke(&fill)), Return(true))); EXPECT_CALL(m_crypto, verify(_, _, _)).WillOnce(Return(true)); ObtainExitMessage msg; msg.Z.Zero(); msg.S = randint(); msg.T = randint(); - EXPECT_TRUE(msg.Sign(alice)); - EXPECT_TRUE(msg.Verify()); - EXPECT_TRUE(msg.I == PubKey(seckey_topublic(alice))); - EXPECT_FALSE(msg.version != LLARP_PROTO_VERSION); - EXPECT_FALSE(msg.Z.IsZero()); + CHECK(msg.Sign(alice)); + CHECK(msg.Verify()); + CHECK(msg.I == PubKey(seckey_topublic(alice))); + CHECK(msg.version == LLARP_PROTO_VERSION); + CHECK_FALSE(msg.Z.IsZero()); } diff --git a/test/routing/test_llarp_routing_transfer_traffic.cpp b/test/routing/test_llarp_routing_transfer_traffic.cpp new file mode 100644 index 000000000..915aecef0 --- /dev/null +++ b/test/routing/test_llarp_routing_transfer_traffic.cpp @@ -0,0 +1,24 @@ +#include + +#include + +using TransferTrafficMessage = llarp::routing::TransferTrafficMessage; + +TEST_CASE("TransferTrafficMessage", "[TransferTrafficMessage]") +{ + TransferTrafficMessage msg; + + SECTION("Put buffer overflow") + { + std::array tmp = {{0}}; + llarp_buffer_t buf(tmp); + REQUIRE_FALSE(msg.PutBuffer(buf, 1)); + } + + SECTION("Put buffer") + { + std::array tmp = {{0}}; + llarp_buffer_t buf(tmp); + REQUIRE(msg.PutBuffer(buf, 1)); + } +} diff --git a/test/service/test_llarp_service_address.cpp b/test/service/test_llarp_service_address.cpp index 01d0ba526..f95e398c9 100644 --- a/test/service/test_llarp_service_address.cpp +++ b/test/service/test_llarp_service_address.cpp @@ -1,67 +1,58 @@ #include -#include +#include -struct ServiceAddressTest : public ::testing::Test +TEST_CASE("Address", "[Address]") { - const std::string snode = - "8zfiwpgonsu5zpddpxwdurxyb19x6r96xy4qbikff99jwsziws9y.snode"; - const std::string loki = - "7okic5x5do3uh3usttnqz9ek3uuoemdrwzto1hciwim9f947or6y.loki"; + const std::string snode = "8zfiwpgonsu5zpddpxwdurxyb19x6r96xy4qbikff99jwsziws9y.snode"; + const std::string loki = "7okic5x5do3uh3usttnqz9ek3uuoemdrwzto1hciwim9f947or6y.loki"; const std::string sub = "lokinet.test"; - const std::string invalid = - "7okic5x5do3uh3usttnqz9ek3uuoemdrwzto1hciwim9f947or6y.net"; -}; - -TEST_F(ServiceAddressTest, TestParseBadTLD) -{ + const std::string invalid = "7okic5x5do3uh3usttnqz9ek3uuoemdrwzto1hciwim9f947or6y.net"; llarp::service::Address addr; - ASSERT_FALSE(addr.FromString(snode, ".net")); - ASSERT_FALSE(addr.FromString(invalid, ".net")); + + SECTION("Parse bad TLD") + { + REQUIRE_FALSE(addr.FromString(snode, ".net")); + REQUIRE_FALSE(addr.FromString(invalid, ".net")); + } + + SECTION("Parse bad TLD appened on end") + { + const std::string bad = loki + ".net"; + REQUIRE_FALSE(addr.FromString(bad, ".net")); + } + + SECTION("Parse bad TLD appened on end with subdomain") + { + const std::string bad = sub + "." + loki + ".net"; + REQUIRE_FALSE(addr.FromString(bad, ".net")); + } + + SECTION("Parse SNode not Loki") + { + REQUIRE(addr.FromString(snode, ".snode")); + REQUIRE_FALSE(addr.FromString(snode, ".loki")); + } + + SECTION("Parse Loki not SNode") + { + REQUIRE_FALSE(addr.FromString(loki, ".snode")); + REQUIRE(addr.FromString(loki, ".loki")); + } + + SECTION("Parse Loki with subdomain") + { + const std::string addr_str = sub + "." + loki; + REQUIRE(addr.FromString(addr_str, ".loki")); + REQUIRE(addr.subdomain == sub); + REQUIRE(addr.ToString() == addr_str); + }; + + SECTION("Parse SNode with subdomain") + { + const std::string addr_str = sub + "." + snode; + REQUIRE(addr.FromString(addr_str, ".snode")); + REQUIRE(addr.subdomain == sub); + REQUIRE(addr.ToString(".snode") == addr_str); + } } - -TEST_F(ServiceAddressTest, TestParseBadTLDAppenedOnEnd) -{ - llarp::service::Address addr; - const std::string bad = loki + ".net"; - ASSERT_FALSE(addr.FromString(bad, ".net")); -} - -TEST_F(ServiceAddressTest, TestParseBadTLDAppenedOnEndWithSubdomain) -{ - llarp::service::Address addr; - const std::string bad = sub + "." + loki + ".net"; - ASSERT_FALSE(addr.FromString(bad, ".net")); -} - -TEST_F(ServiceAddressTest, TestParseSNodeNotLoki) -{ - llarp::service::Address addr; - ASSERT_TRUE(addr.FromString(snode, ".snode")); - ASSERT_FALSE(addr.FromString(snode, ".loki")); -} - -TEST_F(ServiceAddressTest, TestParseLokiNotSNode) -{ - llarp::service::Address addr; - ASSERT_FALSE(addr.FromString(loki, ".snode")); - ASSERT_TRUE(addr.FromString(loki, ".loki")); -} - -TEST_F(ServiceAddressTest, TestParseLokiWithSubdomain) -{ - llarp::service::Address addr; - const std::string addr_str = sub + "." + loki; - ASSERT_TRUE(addr.FromString(addr_str, ".loki")); - ASSERT_EQ(addr.subdomain, sub); - ASSERT_EQ(addr.ToString(), addr_str); -}; - -TEST_F(ServiceAddressTest, TestParseSnodeWithSubdomain) -{ - llarp::service::Address addr; - const std::string addr_str = sub + "." + snode; - ASSERT_TRUE(addr.FromString(addr_str, ".snode")); - ASSERT_EQ(addr.subdomain, sub); - ASSERT_EQ(addr.ToString(".snode"), addr_str); -}; diff --git a/test/test_llarp_encrypted_frame.cpp b/test/test_llarp_encrypted_frame.cpp index 1c3f1206c..039f8046e 100644 --- a/test/test_llarp_encrypted_frame.cpp +++ b/test/test_llarp_encrypted_frame.cpp @@ -7,15 +7,15 @@ #include -#include +#include using namespace ::llarp; using namespace ::testing; using EncryptedFrame = EncryptedFrame; -using SecretKey = SecretKey; -using PubKey = PubKey; -using LRCR = LR_CommitRecord; +using SecretKey = SecretKey; +using PubKey = PubKey; +using LRCR = LR_CommitRecord; class FrameTest : public test::LlarpTest<> { @@ -23,7 +23,7 @@ class FrameTest : public test::LlarpTest<> SecretKey alice, bob; }; -TEST_F(FrameTest, TestFrameCrypto) +TEST_CASE_METHOD(FrameTest, "Frame crypto") { EncryptedFrame f(256); f.Fill(0); @@ -36,28 +36,25 @@ TEST_F(FrameTest, TestFrameCrypto) auto buf = f.Buffer(); buf->cur = buf->base + EncryptedFrameOverheadSize; - ASSERT_TRUE(record.BEncode(buf)); + REQUIRE(record.BEncode(buf)); - EXPECT_CALL(m_crypto, randbytes(_, _)) - .WillOnce(Invoke(&test::randbytes_impl)); + EXPECT_CALL(m_crypto, randbytes(_, _)).WillOnce(Invoke(&test::randbytes_impl)); EXPECT_CALL(m_crypto, dh_client(_, _, alice, _)).WillOnce(Return(true)); - EXPECT_CALL(m_crypto, xchacha20(_, _, _)) - .Times(2) - .WillRepeatedly(Return(true)); + EXPECT_CALL(m_crypto, xchacha20(_, _, _)).Times(2).WillRepeatedly(Return(true)); EXPECT_CALL(m_crypto, hmac(_, _, _)).Times(2).WillRepeatedly(Return(true)); // rewind buffer buf->cur = buf->base + EncryptedFrameOverheadSize; // encrypt to alice - ASSERT_TRUE(f.EncryptInPlace(alice, bob.toPublic())); + REQUIRE(f.EncryptInPlace(alice, bob.toPublic())); EXPECT_CALL(m_crypto, dh_server(_, _, _, _)).WillOnce(Return(true)); // decrypt from alice - ASSERT_TRUE(f.DecryptInPlace(bob)); + REQUIRE(f.DecryptInPlace(bob)); LRCR otherRecord; - ASSERT_TRUE(otherRecord.BDecode(buf)); - ASSERT_TRUE(otherRecord == record); + REQUIRE(otherRecord.BDecode(buf)); + REQUIRE(otherRecord == record); } diff --git a/test/util/meta/test_llarp_util_memfn.cpp b/test/util/meta/test_llarp_util_memfn.cpp index 085776da5..165115570 100644 --- a/test/util/meta/test_llarp_util_memfn.cpp +++ b/test/util/meta/test_llarp_util_memfn.cpp @@ -1,7 +1,6 @@ #include -#include -#include +#include using namespace llarp; @@ -32,36 +31,25 @@ struct Foo } }; -TEST(MemFn, call) +TEST_CASE("memFn call") { Foo foo; - ASSERT_FALSE(util::memFn(&Foo::empty, &foo)()); - ASSERT_TRUE(util::memFn(&Foo::constEmpty, &foo)()); - ASSERT_EQ(11, util::memFn(&Foo::arg, &foo)(10)); - ASSERT_EQ(9, util::memFn(&Foo::constArg, &foo)(10)); + REQUIRE_FALSE(util::memFn(&Foo::empty, &foo)()); + REQUIRE(util::memFn(&Foo::constEmpty, &foo)()); + REQUIRE(11 == util::memFn(&Foo::arg, &foo)(10)); + REQUIRE(9 == util::memFn(&Foo::constArg, &foo)(10)); - ASSERT_TRUE(util::memFn(&Foo::constEmpty, &foo)()); - ASSERT_EQ(9, util::memFn(&Foo::constArg, &foo)(10)); + REQUIRE(util::memFn(&Foo::constEmpty, &foo)()); + REQUIRE(9 == util::memFn(&Foo::constArg, &foo)(10)); } -template < typename T > -class MemFnType : public ::testing::Test -{ -}; - -TYPED_TEST_SUITE_P(MemFnType); - -TYPED_TEST_P(MemFnType, Smoke) -{ - TypeParam foo{}; - ASSERT_TRUE(util::memFn(&Foo::constEmpty, &foo)()); -} - -REGISTER_TYPED_TEST_SUITE_P(MemFnType, Smoke); - // clang-format off -using MemFnTypes = ::testing::Types< +using MemFnTypes = std::tuple< Foo, const Foo>; - -INSTANTIATE_TYPED_TEST_SUITE_P(MemFn, MemFnType, MemFnTypes); // clang-format on + +TEMPLATE_LIST_TEST_CASE("memFn type smoke test", "", MemFnTypes) +{ + TestType foo{}; + REQUIRE(util::memFn(&Foo::constEmpty, &foo)()); +} diff --git a/test/util/meta/test_llarp_util_traits.cpp b/test/util/meta/test_llarp_util_traits.cpp index 64c2cd141..4386de340 100644 --- a/test/util/meta/test_llarp_util_traits.cpp +++ b/test/util/meta/test_llarp_util_traits.cpp @@ -2,37 +2,19 @@ #include -#include -#include +#include using namespace llarp; -TEST(traits_bottom, Smoke) +TEST_CASE("traits::Bottom smoke test") { traits::Bottom bottom; (void)bottom; SUCCEED(); } -template < typename T > -class IsContainer : public ::testing::Test -{ -}; - -TYPED_TEST_SUITE_P(IsContainer); - -TYPED_TEST_P(IsContainer, Smoke) -{ - bool expected = std::tuple_element_t< 1, TypeParam >::value; - bool result = - traits::is_container< std::tuple_element_t< 0, TypeParam > >::value; - ASSERT_EQ(expected, result); -} - -REGISTER_TYPED_TEST_SUITE_P(IsContainer, Smoke); - // clang-format off -using ContainerTypes = ::testing::Types< +using ContainerTypes = std::tuple< std::tuple< std::vector< int >, std::integral_constant< bool, true > >, std::tuple< std::vector< std::string >, std::integral_constant< bool, true > >, std::tuple< std::list< std::string >, std::integral_constant< bool, true > >, @@ -41,8 +23,16 @@ using ContainerTypes = ::testing::Types< std::tuple< std::tuple, std::integral_constant< bool, false > >, std::tuple< int, std::integral_constant< bool, false > > >; -INSTANTIATE_TYPED_TEST_SUITE_P(traits, IsContainer, ContainerTypes); +// clang-format on +TEMPLATE_LIST_TEST_CASE("is_container smoke test", "", ContainerTypes) +{ + bool expected = std::tuple_element_t<1, TestType>::value; + bool result = traits::is_container>::value; + REQUIRE(expected == result); +} + +// clang-format off struct A { }; struct B { }; struct C { }; @@ -65,29 +55,11 @@ char f(H) { return 'H'; } char f(I) { return 'I'; } char f(J) { return 'J'; } char f(traits::Bottom) { return '0'; } - // clang-format on -template < typename T > -class TestSwitch : public ::testing::Test -{ -}; - -TYPED_TEST_SUITE_P(TestSwitch); - -TYPED_TEST_P(TestSwitch, Smoke) -{ - char expected = std::tuple_element_t< 0, TypeParam >::value; - using InputType = typename std::tuple_element_t< 1, TypeParam >::Type; - char result = f(InputType()); - ASSERT_EQ(expected, result); -} - -REGISTER_TYPED_TEST_SUITE_P(TestSwitch, Smoke); - // clang-format off using namespace traits::Switch; -using SwitchTypes = ::testing::Types< +using SwitchTypes = std::tuple< std::tuple, Switch< 0, A, B, C, D, E, F, G, H, I, J > >, std::tuple, Switch< 1, A, B, C, D, E, F, G, H, I, J > >, std::tuple, Switch< 2, A, B, C, D, E, F, G, H, I, J > >, @@ -102,8 +74,15 @@ using SwitchTypes = ::testing::Types< std::tuple, Switch< 6, C, C, C, C, C, C, C, C, C, J > >, std::tuple, Switch< 10, A, B, C, D, E, F, G, H, I, J > > >; +// clang-format off -INSTANTIATE_TYPED_TEST_SUITE_P(traits, TestSwitch, SwitchTypes); +TEMPLATE_LIST_TEST_CASE("Switch smoke test", "", SwitchTypes) +{ + char expected = std::tuple_element_t<0, TestType>::value; + using InputType = typename std::tuple_element_t<1, TestType>::Type; + char result = f(InputType()); + REQUIRE(expected == result); +} template using is_bool = std::is_same; @@ -126,56 +105,35 @@ selectCase() return dispatch(Selection()); } -template < typename T > -class Select : public ::testing::Test -{ -}; - -TYPED_TEST_SUITE_P(Select); - -TYPED_TEST_P(Select, Smoke) -{ - char expected = std::tuple_element_t< 0, TypeParam >::value; - char result = selectCase >(); - ASSERT_EQ(expected, result); -} - -REGISTER_TYPED_TEST_SUITE_P(Select, Smoke); - -using SelectTypes = ::testing::Types< +// clang-format off +using SelectTypes = std::tuple< std::tuple, double >, std::tuple, bool >, std::tuple, char >, std::tuple, std::string > >; - -INSTANTIATE_TYPED_TEST_SUITE_P(traits, Select, SelectTypes); - // clang-format on -template < typename T > -class IsPointy : public ::testing::Test +TEMPLATE_LIST_TEST_CASE("selectCase smoke test", "", SelectTypes) { -}; - -TYPED_TEST_SUITE_P(IsPointy); - -TYPED_TEST_P(IsPointy, Smoke) -{ - bool expected = std::tuple_element_t< 1, TypeParam >::value; - bool result = - traits::is_pointy< std::tuple_element_t< 0, TypeParam > >::value; - ASSERT_EQ(expected, result); + char expected = std::tuple_element_t<0, TestType>::value; + char result = selectCase>(); + REQUIRE(expected == result); } - -REGISTER_TYPED_TEST_SUITE_P(IsPointy, Smoke); +// clang-format on // clang-format off -using PointerTypes = ::testing::Types< +using PointerTypes = std::tuple< std::tuple< int *, std::true_type >, std::tuple< int, std::integral_constant< bool, false > >, std::tuple< std::shared_ptr, std::true_type >, std::tuple< std::unique_ptr, std::true_type > >; -INSTANTIATE_TYPED_TEST_SUITE_P(traits, IsPointy, PointerTypes); // clang-format on + +TEMPLATE_LIST_TEST_CASE("is_pointy smoke test", "", PointerTypes) +{ + bool expected = std::tuple_element_t<1, TestType>::value; + bool result = traits::is_pointy>::value; + REQUIRE(expected == result); +} diff --git a/test/util/test_llarp_util_aligned.cpp b/test/util/test_llarp_util_aligned.cpp index 3f8782143..b84fa40a9 100644 --- a/test/util/test_llarp_util_aligned.cpp +++ b/test/util/test_llarp_util_aligned.cpp @@ -1,4 +1,4 @@ -#include +#include #include @@ -7,218 +7,176 @@ #include #include -using TestSizes = ::testing::Types< std::integral_constant< std::size_t, 8 >, - std::integral_constant< std::size_t, 12 >, - std::integral_constant< std::size_t, 16 >, - std::integral_constant< std::size_t, 32 >, - std::integral_constant< std::size_t, 64 >, - std::integral_constant< std::size_t, 77 >, - std::integral_constant< std::size_t, 1024 >, - std::integral_constant< std::size_t, 3333 > >; +using TestSizes = std::tuple< + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant, + std::integral_constant>; -template < typename T > -struct AlignedBufferTest : public ::testing::Test +TEMPLATE_LIST_TEST_CASE("AlignedBuffer", "[AlignedBuffer]", TestSizes) { -}; - -TYPED_TEST_CASE(AlignedBufferTest, TestSizes, ); - -TYPED_TEST(AlignedBufferTest, Constructor) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; + using Buffer = llarp::AlignedBuffer; Buffer b; - EXPECT_TRUE(b.IsZero()); - EXPECT_EQ(b.size(), TypeParam::value); -} + CHECK(b.IsZero()); -TYPED_TEST(AlignedBufferTest, CopyConstructor) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; - - Buffer b; - EXPECT_TRUE(b.IsZero()); - - Buffer c = b; - EXPECT_TRUE(c.IsZero()); - - c.Fill(1); - EXPECT_FALSE(c.IsZero()); - - Buffer d = c; - EXPECT_FALSE(d.IsZero()); -} - -TYPED_TEST(AlignedBufferTest, AltConstructors) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; - - Buffer b; - EXPECT_TRUE(b.IsZero()); - b.Fill(2); - - Buffer c(b.as_array()); - EXPECT_FALSE(c.IsZero()); - - Buffer d(c.data()); - EXPECT_FALSE(d.IsZero()); -} - -TYPED_TEST(AlignedBufferTest, Assignment) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; - - Buffer b; - EXPECT_TRUE(b.IsZero()); - - Buffer c; - c = b; - EXPECT_TRUE(c.IsZero()); - - c.Fill(1); - EXPECT_FALSE(c.IsZero()); - - Buffer d; - d = c; - EXPECT_FALSE(d.IsZero()); -} - -TYPED_TEST(AlignedBufferTest, StreamOut) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; - - Buffer b; - EXPECT_TRUE(b.IsZero()); - - std::stringstream stream; - - stream << b; - - EXPECT_EQ(stream.str(), std::string(TypeParam::value * 2, '0')); - - stream.str(""); - - b.Fill(255); - stream << b; - - EXPECT_EQ(stream.str(), std::string(TypeParam::value * 2, 'f')); -} - -TYPED_TEST(AlignedBufferTest, BitwiseNot) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; - - Buffer b; - EXPECT_TRUE(b.IsZero()); - - Buffer c = ~b; - EXPECT_FALSE(c.IsZero()); - - for(auto val : c.as_array()) + SECTION("Constructor") { - EXPECT_EQ(255, val); + CHECK(b.size() == TestType::value); } - Buffer d = ~c; - EXPECT_TRUE(d.IsZero()); -} - -TYPED_TEST(AlignedBufferTest, Operators) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; - - Buffer b; - EXPECT_TRUE(b.IsZero()); - - Buffer c = b; - EXPECT_EQ(b, c); - EXPECT_GE(b, c); - EXPECT_LE(b, c); - EXPECT_GE(c, b); - EXPECT_LE(c, b); - - c.Fill(1); - EXPECT_NE(b, c); - EXPECT_LT(b, c); - EXPECT_GT(c, b); -} - -TYPED_TEST(AlignedBufferTest, Xor) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; - - Buffer b; - Buffer c; - b.Fill(255); - c.Fill(255); - EXPECT_FALSE(b.IsZero()); - EXPECT_FALSE(c.IsZero()); - - Buffer d = b ^ c; - // 1 ^ 1 = 0 - EXPECT_TRUE(d.IsZero()); - // Verify unchanged - EXPECT_FALSE(b.IsZero()); - EXPECT_FALSE(c.IsZero()); - - Buffer e, f; - e.Fill(255); - Buffer g = e ^ f; - // 1 ^ 0 = 1 - EXPECT_FALSE(g.IsZero()); - - Buffer h, i; - i.Fill(255); - Buffer j = h ^ i; - // 0 ^ 1 = 1 - EXPECT_FALSE(j.IsZero()); -} - -TYPED_TEST(AlignedBufferTest, XorAssign) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; - - Buffer b, c; - b.Fill(255); - c.Fill(255); - EXPECT_FALSE(b.IsZero()); - EXPECT_FALSE(c.IsZero()); - - b ^= c; - EXPECT_TRUE(b.IsZero()); -} - -TYPED_TEST(AlignedBufferTest, Zero) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; - - Buffer b; - EXPECT_TRUE(b.IsZero()); - - b.Fill(127); - EXPECT_FALSE(b.IsZero()); - - b.Zero(); - EXPECT_TRUE(b.IsZero()); -} - -TYPED_TEST(AlignedBufferTest, TestHash) -{ - using Buffer = llarp::AlignedBuffer< TypeParam::value >; - using Map_t = std::unordered_map< Buffer, int, typename Buffer::Hash >; - - Buffer k, other_k; - k.Randomize(); - other_k.Randomize(); - Map_t m; - EXPECT_TRUE(m.empty()); - EXPECT_TRUE(m.emplace(k, 1).second); - EXPECT_TRUE(m.find(k) != m.end()); - EXPECT_TRUE(m[k] == 1); - EXPECT_FALSE(m.find(other_k) != m.end()); - EXPECT_TRUE(m.size() == 1); - Buffer k_copy = k; - EXPECT_FALSE(m.emplace(k_copy, 2).second); - EXPECT_FALSE(m[k_copy] == 2); - EXPECT_TRUE(m[k_copy] == 1); + SECTION("CopyConstructor") + { + Buffer c = b; + CHECK(c.IsZero()); + + c.Fill(1); + CHECK_FALSE(c.IsZero()); + + Buffer d = c; + CHECK_FALSE(d.IsZero()); + } + + SECTION("AltConstructors") + { + b.Fill(2); + + Buffer c(b.as_array()); + CHECK_FALSE(c.IsZero()); + + Buffer d(c.data()); + CHECK_FALSE(d.IsZero()); + } + + SECTION("Assignment") + { + Buffer c; + c = b; + CHECK(c.IsZero()); + + c.Fill(1); + CHECK_FALSE(c.IsZero()); + + Buffer d; + d = c; + CHECK_FALSE(d.IsZero()); + } + + SECTION("StreamOut") + { + std::stringstream stream; + + stream << b; + + CHECK(stream.str() == std::string(TestType::value * 2, '0')); + + stream.str(""); + + b.Fill(255); + stream << b; + + CHECK(stream.str() == std::string(TestType::value * 2, 'f')); + } + + SECTION("BitwiseNot") + { + Buffer c = ~b; + CHECK_FALSE(c.IsZero()); + + for (auto val : c.as_array()) + { + CHECK(255 == val); + } + + Buffer d = ~c; + CHECK(d.IsZero()); + } + + SECTION("Operators") + { + Buffer c = b; + CHECK(b == c); + CHECK(b >= c); + CHECK(b <= c); + CHECK(c >= b); + CHECK(c <= b); + + c.Fill(1); + CHECK(b != c); + CHECK(b < c); + CHECK(c > b); + } + + SECTION("Xor") + { + Buffer c; + b.Fill(255); + c.Fill(255); + CHECK_FALSE(b.IsZero()); + CHECK_FALSE(c.IsZero()); + + Buffer d = b ^ c; + // 1 ^ 1 = 0 + CHECK(d.IsZero()); + // Verify unchanged + CHECK_FALSE(b.IsZero()); + CHECK_FALSE(c.IsZero()); + + Buffer e, f; + e.Fill(255); + Buffer g = e ^ f; + // 1 ^ 0 = 1 + CHECK_FALSE(g.IsZero()); + + Buffer h, i; + i.Fill(255); + Buffer j = h ^ i; + // 0 ^ 1 = 1 + CHECK_FALSE(j.IsZero()); + } + + SECTION("XorAssign") + { + Buffer c; + b.Fill(255); + c.Fill(255); + CHECK_FALSE(b.IsZero()); + CHECK_FALSE(c.IsZero()); + + b ^= c; + CHECK(b.IsZero()); + } + + SECTION("Zero") + { + b.Fill(127); + CHECK_FALSE(b.IsZero()); + + b.Zero(); + CHECK(b.IsZero()); + } + + SECTION("TestHash") + { + using Map_t = std::unordered_map; + + Buffer k, other_k; + k.Randomize(); + other_k.Randomize(); + Map_t m; + CHECK(m.empty()); + CHECK(m.emplace(k, 1).second); + CHECK(m.find(k) != m.end()); + CHECK(m[k] == 1); + CHECK_FALSE(m.find(other_k) != m.end()); + CHECK(m.size() == 1); + Buffer k_copy = k; + CHECK_FALSE(m.emplace(k_copy, 2).second); + CHECK_FALSE(m[k_copy] == 2); + CHECK(m[k_copy] == 1); + } } diff --git a/test/util/test_llarp_util_bencode.cpp b/test/util/test_llarp_util_bencode.cpp index fbacd99ef..890ddce30 100644 --- a/test/util/test_llarp_util_bencode.cpp +++ b/test/util/test_llarp_util_bencode.cpp @@ -6,11 +6,11 @@ #include #include -#include +#include -using TestBuffer = std::vector< byte_t >; +using TestBuffer = std::vector; -template < typename Result > +template struct TestReadData { TestBuffer buffer; @@ -18,15 +18,15 @@ struct TestReadData Result result; }; -using TestReadInt = TestReadData< uint64_t >; -using TestReadString = TestReadData< std::string >; +using TestReadInt = TestReadData; +using TestReadString = TestReadData; -template < typename Result > +template std::ostream& -operator<<(std::ostream& os, const TestReadData< Result >& d) +operator<<(std::ostream& os, const TestReadData& d) { os << "buf = [ "; - for(auto x : d.buffer) + for (auto x : d.buffer) { os << x << " "; } @@ -37,33 +37,16 @@ operator<<(std::ostream& os, const TestReadData< Result >& d) return os; } -struct ReadInt : public ::testing::TestWithParam< TestReadInt > -{ -}; - -TEST_P(ReadInt, readInt) -{ - auto d = GetParam(); - - llarp_buffer_t buffer(d.buffer); - - uint64_t result = 0; - bool rc = bencode_read_integer(&buffer, &result); - - EXPECT_EQ(rc, d.rc); - EXPECT_EQ(result, d.result); -} - -static constexpr byte_t i = 'i'; -static constexpr byte_t e = 'e'; -static constexpr byte_t zero = '0'; -static constexpr byte_t one = '1'; -static constexpr byte_t two = '2'; -static constexpr byte_t f = 'f'; -static constexpr byte_t z = 'z'; +static constexpr byte_t i = 'i'; +static constexpr byte_t e = 'e'; +static constexpr byte_t zero = '0'; +static constexpr byte_t one = '1'; +static constexpr byte_t two = '2'; +static constexpr byte_t f = 'f'; +static constexpr byte_t z = 'z'; static constexpr byte_t colon = ':'; -static const TestReadInt testReadInt[] = { +std::vector testReadInt{ // good cases {{i, 0, e}, true, 0}, {{i, zero, e}, true, 0}, @@ -80,28 +63,20 @@ static const TestReadInt testReadInt[] = { {{z}, false, 0}, }; -INSTANTIATE_TEST_SUITE_P(TestBencode, ReadInt, - ::testing::ValuesIn(testReadInt)); - -struct ReadStr : public ::testing::TestWithParam< TestReadString > +TEST_CASE("Read int", "[bencode]") { -}; - -TEST_P(ReadStr, readStr) -{ - auto d = GetParam(); + auto d = GENERATE(from_range(testReadInt)); llarp_buffer_t buffer(d.buffer); - llarp_buffer_t result; - bool rc = bencode_read_string(&buffer, &result); + uint64_t result = 0; + bool rc = bencode_read_integer(&buffer, &result); - EXPECT_EQ(rc, d.rc); - EXPECT_EQ(result.sz, d.result.size()); - EXPECT_EQ(std::string(result.base, result.base + result.sz), d.result); + CHECK(rc == d.rc); + CHECK(result == d.result); } -static const TestReadString testReadStr[] = { +std::vector testReadStr{ // good cases {{one, colon, 'a'}, true, "a"}, {{one, colon, 'b'}, true, "b"}, @@ -118,10 +93,21 @@ static const TestReadString testReadStr[] = { {{colon, colon}, false, ""}, }; -INSTANTIATE_TEST_SUITE_P(TestBencode, ReadStr, - ::testing::ValuesIn(testReadStr)); +TEST_CASE("Read str", "[bencode]") +{ + auto d = GENERATE(from_range(testReadStr)); -template < typename Input > + llarp_buffer_t buffer(d.buffer); + + llarp_buffer_t result; + bool rc = bencode_read_string(&buffer, &result); + + CHECK(rc == d.rc); + CHECK(result.sz == d.result.size()); + CHECK(std::string(result.base, result.base + result.sz) == d.result); +} + +template struct TestWriteData { Input input; @@ -130,62 +116,39 @@ struct TestWriteData std::string output; }; -using TestWriteByteString = TestWriteData< std::string >; -using TestWriteInt = TestWriteData< uint64_t >; +using TestWriteByteString = TestWriteData; +using TestWriteInt = TestWriteData; -struct WriteByteStr : public ::testing::TestWithParam< TestWriteByteString > -{ -}; +static constexpr size_t MAX_1 = static_cast(std::numeric_limits::max()) + 1; -TEST_P(WriteByteStr, writeByte) -{ - auto d = GetParam(); - - std::vector< byte_t > backingBuffer(d.bufferSize, 0); - llarp_buffer_t buffer(backingBuffer); - - bool rc = bencode_write_bytestring(&buffer, d.input.data(), d.input.size()); - - ASSERT_EQ(rc, d.rc); - ASSERT_EQ(std::string(buffer.base, buffer.cur), d.output); -} - -static constexpr size_t MAX_1 = - static_cast< size_t >(std::numeric_limits< int16_t >::max()) + 1; - -static const TestWriteByteString testWriteByteString[] = { +std::vector testWriteByteString{ // good cases {"abacus", 100, true, "6:abacus"}, {" abacus", 100, true, "8: abacus"}, {"", 100, true, "0:"}, {std::string("\0\0\0", 3), 100, true, std::string("3:\0\0\0", 5)}, - {std::string(MAX_1, 'a'), MAX_1 + 100, true, + {std::string(MAX_1, 'a'), + MAX_1 + 100, + true, std::to_string(MAX_1) + std::string(":") + std::string(MAX_1, 'a')}, // bad cases {"a", 1, false, ""}, }; -INSTANTIATE_TEST_SUITE_P(TestBencode, WriteByteStr, - ::testing::ValuesIn(testWriteByteString)); - -struct WriteInt : public ::testing::TestWithParam< TestWriteInt > +TEST_CASE("Write byte str", "[bencode]") { -}; + auto d = GENERATE(from_range(testWriteByteString)); -TEST_P(WriteInt, writeInt) -{ - auto d = GetParam(); - - std::vector< byte_t > backingBuffer(d.bufferSize, 0); + std::vector backingBuffer(d.bufferSize, 0); llarp_buffer_t buffer(backingBuffer); - bool rc = bencode_write_uint64(&buffer, d.input); + bool rc = bencode_write_bytestring(&buffer, d.input.data(), d.input.size()); - ASSERT_EQ(rc, d.rc); - ASSERT_EQ(std::string(buffer.base, buffer.cur), d.output); + REQUIRE(rc == d.rc); + REQUIRE(std::string(buffer.base, buffer.cur) == d.output); } -static const TestWriteInt testWriteInt[] = { +std::vector testWriteInt{ // Good cases {0, 100, true, "i0e"}, {1234, 100, true, "i1234e"}, @@ -194,59 +157,62 @@ static const TestWriteInt testWriteInt[] = { {1234567, 3, false, ""}, }; -INSTANTIATE_TEST_SUITE_P(TestBencode, WriteInt, - ::testing::ValuesIn(testWriteInt)); - -struct WriteIntValues : public ::testing::TestWithParam< uint64_t > +TEST_CASE("Write int", "[bencode]") { -}; + auto d = GENERATE(from_range(testWriteInt)); -TEST_P(WriteIntValues, anyvalue) + std::vector backingBuffer(d.bufferSize, 0); + llarp_buffer_t buffer(backingBuffer); + + bool rc = bencode_write_uint64(&buffer, d.input); + + REQUIRE(rc == d.rc); + REQUIRE(std::string(buffer.base, buffer.cur) == d.output); +} + +TEST_CASE("Write int values", "[bencode]") { // test we can encode any uint64_t into a buffer. - uint64_t val = GetParam(); + uint64_t val = GENERATE( + std::numeric_limits::min(), + std::numeric_limits::max(), + std::numeric_limits::max() / 2, + std::numeric_limits::max() / 3); - std::vector< byte_t > backingBuffer(100, 0); + std::vector backingBuffer(100, 0); { llarp_buffer_t buffer(backingBuffer); bool rc = bencode_write_uint64(&buffer, val); - ASSERT_TRUE(rc); + REQUIRE(rc); } { uint64_t result = 0; llarp_buffer_t buffer(backingBuffer); bool rc = bencode_read_integer(&buffer, &result); - ASSERT_TRUE(rc); - ASSERT_EQ(result, val); + REQUIRE(rc); + REQUIRE(result == val); } } -INSTANTIATE_TEST_SUITE_P( - TestBencode, WriteIntValues, - ::testing::Values(std::numeric_limits< uint64_t >::min(), - std::numeric_limits< uint64_t >::max(), - std::numeric_limits< uint64_t >::max() / 2, - std::numeric_limits< uint64_t >::max() / 3)); - -TEST(TestBencode, good_uint64_entry) +TEST_CASE("Bencode: good uint64 entry", "[bencode]") { - std::vector< byte_t > backingBuffer(100, 0); + std::vector backingBuffer(100, 0); llarp_buffer_t buffer(backingBuffer); - ASSERT_TRUE(bencode_write_uint64_entry(&buffer, "v", 1, 0)); + REQUIRE(bencode_write_uint64_entry(&buffer, "v", 1, 0)); - ASSERT_EQ(std::string(buffer.base, buffer.cur), "1:vi0e"); + REQUIRE(std::string(buffer.base, buffer.cur) == "1:vi0e"); } -TEST(TestBencode, bad_uint64_entry) +TEST_CASE("Bencode: bad uint64 entry", "[bencode]") { - std::vector< byte_t > otherBuffer(1, 0); + std::vector otherBuffer(1, 0); llarp_buffer_t buffer(otherBuffer); - ASSERT_FALSE(bencode_write_uint64_entry(&buffer, "v", 1, 0)); + REQUIRE_FALSE(bencode_write_uint64_entry(&buffer, "v", 1, 0)); } struct ValueData @@ -259,172 +225,139 @@ struct ValueData struct ListTestData { - std::vector< ValueData > list; + std::vector list; size_t bufferSize; std::string result; }; -struct ListTest : public ::testing::TestWithParam< ListTestData > -{ -}; - -TEST_P(ListTest, list) -{ - auto d = GetParam(); - - std::vector< byte_t > backingBuffer(d.bufferSize, 0); - llarp_buffer_t buffer(backingBuffer); - - ASSERT_TRUE(bencode_start_list(&buffer)); - - for(const auto& x : d.list) - { - if(x.isString) - { - ASSERT_TRUE(bencode_write_bytestring(&buffer, x.theString.data(), - x.theString.size())); - } - else - { - ASSERT_TRUE(bencode_write_uint64(&buffer, x.theInt)); - } - } - - ASSERT_TRUE(bencode_end(&buffer)); - - ASSERT_EQ(std::string(buffer.base, buffer.cur), d.result); -} - -ListTestData listTestData[] = { +std::vector listTestData{ {{}, 100, "le"}, {{{"", 0, true}}, 100, "l0:e"}, {{{"", 0, false}}, 100, "li0ee"}, {{{"", 0, false}, {"", 0, true}}, 100, "li0e0:e"}, {{{"", 123, false}, {"abc", 0, true}}, 100, "li123e3:abce"}, - {{{"", 123, false}, {"abc", 0, true}, {"abc", 0, true}}, - 100, - "li123e3:abc3:abce"}, + {{{"", 123, false}, {"abc", 0, true}, {"abc", 0, true}}, 100, "li123e3:abc3:abce"}, }; -INSTANTIATE_TEST_SUITE_P(TestBencode, ListTest, - ::testing::ValuesIn(listTestData)); +TEST_CASE("List test", "[bencode]") +{ + auto d = GENERATE(from_range(listTestData)); + + std::vector backingBuffer(d.bufferSize, 0); + llarp_buffer_t buffer(backingBuffer); + + REQUIRE(bencode_start_list(&buffer)); + + for (const auto& x : d.list) + { + if (x.isString) + { + REQUIRE(bencode_write_bytestring(&buffer, x.theString.data(), x.theString.size())); + } + else + { + REQUIRE(bencode_write_uint64(&buffer, x.theInt)); + } + } + + REQUIRE(bencode_end(&buffer)); + + REQUIRE(std::string(buffer.base, buffer.cur) == d.result); +} struct DictTestData { - std::vector< std::pair< char, ValueData > > list; + std::vector> list; size_t bufferSize; std::string result; }; -struct DictTest : public ::testing::TestWithParam< DictTestData > -{ -}; - -TEST_P(DictTest, dict) -{ - auto d = GetParam(); - - std::vector< byte_t > backingBuffer(d.bufferSize, 0); - llarp_buffer_t buffer(backingBuffer); - - ASSERT_TRUE(bencode_start_dict(&buffer)); - - for(const auto& x : d.list) - { - ASSERT_TRUE(bencode_write_bytestring(&buffer, &x.first, 1)); - if(x.second.isString) - { - ASSERT_TRUE(bencode_write_bytestring(&buffer, x.second.theString.data(), - x.second.theString.size())); - } - else - { - ASSERT_TRUE(bencode_write_uint64(&buffer, x.second.theInt)); - } - } - - ASSERT_TRUE(bencode_end(&buffer)); - - ASSERT_EQ(std::string(buffer.base, buffer.cur), d.result); -} - -DictTestData dictTestData[] = { +std::vector dictTestData{ {{}, 100, "de"}, {{{'a', {"", 0, true}}}, 100, "d1:a0:e"}, {{{'b', {"", 0, false}}}, 100, "d1:bi0ee"}, {{{'c', {"", 0, false}}, {'d', {"", 0, true}}}, 100, "d1:ci0e1:d0:e"}, - {{{'e', {"", 123, false}}, {'f', {"abc", 0, true}}}, - 100, - "d1:ei123e1:f3:abce"}, - {{{'a', {"", 123, false}}, - {'b', {"abc", 0, true}}, - {'c', {"abc", 0, true}}}, + {{{'e', {"", 123, false}}, {'f', {"abc", 0, true}}}, 100, "d1:ei123e1:f3:abce"}, + {{{'a', {"", 123, false}}, {'b', {"abc", 0, true}}, {'c', {"abc", 0, true}}}, 100, "d1:ai123e1:b3:abc1:c3:abce"}, }; -INSTANTIATE_TEST_SUITE_P(TestBencode, DictTest, - ::testing::ValuesIn(dictTestData)); +TEST_CASE("Dict test", "[bencode]") +{ + auto d = GENERATE(from_range(dictTestData)); + + std::vector backingBuffer(d.bufferSize, 0); + llarp_buffer_t buffer(backingBuffer); + + REQUIRE(bencode_start_dict(&buffer)); + + for (const auto& x : d.list) + { + REQUIRE(bencode_write_bytestring(&buffer, &x.first, 1)); + if (x.second.isString) + { + REQUIRE( + bencode_write_bytestring(&buffer, x.second.theString.data(), x.second.theString.size())); + } + else + { + REQUIRE(bencode_write_uint64(&buffer, x.second.theInt)); + } + } + + REQUIRE(bencode_end(&buffer)); + + REQUIRE(std::string(buffer.base, buffer.cur) == d.result); +} struct ReadData { std::string input; - std::vector< std::string > output; + std::vector output; }; -struct DictReadTest : public ::testing::TestWithParam< ReadData > -{ -}; +std::vector dictReadData{ + {"de", {}}, {"d1:a0:e", {"a", ""}}, {"d1:be", {"b"}}, {"d1:b2:23e", {"b", "23"}}}; -TEST_P(DictReadTest, readtest) +TEST_CASE("Read dict", "[bencode]") { - auto d = GetParam(); + auto d = GENERATE(from_range(dictReadData)); - byte_t* input = - const_cast< byte_t* >(reinterpret_cast< const byte_t* >(d.input.data())); + byte_t* input = const_cast(reinterpret_cast(d.input.data())); llarp_buffer_t buffer(input, input, d.input.size()); - std::vector< std::string > result; + std::vector result; - ASSERT_TRUE(llarp::bencode_read_dict( + REQUIRE(llarp::bencode_read_dict( [&](llarp_buffer_t*, llarp_buffer_t* key) { - if(key) + if (key) { result.emplace_back(key->base, key->base + key->sz); } return true; }, &buffer)); - ASSERT_EQ(result, d.output); + REQUIRE(result == d.output); } -ReadData dictReadData[] = {{"de", {}}, - {"d1:a0:e", {"a", ""}}, - {"d1:be", {"b"}}, - {"d1:b2:23e", {"b", "23"}}}; +std::vector listReadData{ + {"le", {}}, {"l1:ae", {"a"}}, {"l1:be", {"b"}}, {"l1:b2:23e", {"b", "23"}}}; -INSTANTIATE_TEST_SUITE_P(TestBencode, DictReadTest, - ::testing::ValuesIn(dictReadData)); - -struct ListReadTest : public ::testing::TestWithParam< ReadData > +TEST_CASE("Read list", "[bencode]") { -}; + auto d = GENERATE(from_range(listReadData)); -TEST_P(ListReadTest, readtest) -{ - auto d = GetParam(); - - byte_t* input = - const_cast< byte_t* >(reinterpret_cast< const byte_t* >(d.input.data())); + byte_t* input = const_cast(reinterpret_cast(d.input.data())); llarp_buffer_t buffer(input, input, d.input.size()); - std::vector< std::string > result; + std::vector result; - ASSERT_TRUE(llarp::bencode_read_list( + REQUIRE(llarp::bencode_read_list( [&](llarp_buffer_t* b, bool cont) { - if(cont) + if (cont) { llarp_buffer_t tmp; bencode_read_string(b, &tmp); @@ -433,18 +366,12 @@ TEST_P(ListReadTest, readtest) return true; }, &buffer)); - ASSERT_EQ(result, d.output); + REQUIRE(result == d.output); } -ReadData listReadData[] = { - {"le", {}}, {"l1:ae", {"a"}}, {"l1:be", {"b"}}, {"l1:b2:23e", {"b", "23"}}}; - -INSTANTIATE_TEST_SUITE_P(TestBencode, ListReadTest, - ::testing::ValuesIn(listReadData)); - -TEST(TestBencode, ReadDictEmptyBuffer) +TEST_CASE("Read dict to empty buffer", "[bencode]") { llarp_buffer_t buf((byte_t*)nullptr, 0); - ASSERT_FALSE(llarp::bencode_read_dict( - [](llarp_buffer_t*, llarp_buffer_t*) { return true; }, &buf)); + REQUIRE_FALSE( + llarp::bencode_read_dict([](llarp_buffer_t*, llarp_buffer_t*) { return true; }, &buf)); } diff --git a/test/util/test_llarp_util_decaying_hashset.cpp b/test/util/test_llarp_util_decaying_hashset.cpp index 3267b276f..eb8cbc83b 100644 --- a/test/util/test_llarp_util_decaying_hashset.cpp +++ b/test/util/test_llarp_util_decaying_hashset.cpp @@ -6,8 +6,8 @@ TEST_CASE("DecayingHashSet test decay static time", "[decaying-hashset]") { static constexpr auto timeout = 5s; static constexpr auto now = 1s; - llarp::util::DecayingHashSet hashset(timeout); - const llarp::RouterID zero; + llarp::util::DecayingHashSet hashset{timeout}; + const llarp::RouterID zero{}; REQUIRE(zero.IsZero()); REQUIRE(not hashset.Contains(zero)); REQUIRE(hashset.Insert(zero, now)); @@ -20,20 +20,20 @@ TEST_CASE("DecayingHashSet test decay static time", "[decaying-hashset]") REQUIRE(not hashset.Contains(zero)); } -TEST_CASE("DecayingHashSet tset decay dynamic time", "[decaying-hashset]") +TEST_CASE("DecayingHashSet test decay dynamic time", "[decaying-hashset]") { static constexpr llarp_time_t timeout = 5s; - const llarp_time_t now = llarp::time_now_ms(); - llarp::util::DecayingHashSet hashset(timeout); - const llarp::RouterID zero; + constexpr auto now = llarp::time_now_ms; + llarp::util::DecayingHashSet hashset{timeout}; + const llarp::RouterID zero{}; REQUIRE(zero.IsZero()); REQUIRE(not hashset.Contains(zero)); REQUIRE(hashset.Insert(zero)); REQUIRE(hashset.Contains(zero)); - hashset.Decay(now + 1s); + hashset.Decay(now() + 1s); REQUIRE(hashset.Contains(zero)); - hashset.Decay(now + timeout); + hashset.Decay(now() + timeout); REQUIRE(not hashset.Contains(zero)); - hashset.Decay(now + timeout + 1s); + hashset.Decay(now() + timeout + 1s); REQUIRE(not hashset.Contains(zero)); } diff --git a/test/util/test_llarp_util_log_level.cpp b/test/util/test_llarp_util_log_level.cpp index 527212ba4..b69929a7f 100644 --- a/test/util/test_llarp_util_log_level.cpp +++ b/test/util/test_llarp_util_log_level.cpp @@ -1,4 +1,4 @@ -#include +#include #include #include #include @@ -8,58 +8,49 @@ using TestString = std::string; struct TestParseLog { TestString input; - std::optional< llarp::LogLevel > level; + std::optional level; }; -struct LogLevelTest : public ::testing::TestWithParam< TestParseLog > -{ -}; +std::vector testParseLog{// bad cases + {"bogus", {}}, + {"BOGUS", {}}, + {"", {}}, + {" ", {}}, + // good cases + {"info", llarp::eLogInfo}, + {"infO", llarp::eLogInfo}, + {"iNfO", llarp::eLogInfo}, + {"InfO", llarp::eLogInfo}, + {"INFO", llarp::eLogInfo}, + {"debug", llarp::eLogDebug}, + {"warn", llarp::eLogWarn}, + {"error", llarp::eLogError}, + {"none", llarp::eLogNone}}; -TEST_P(LogLevelTest, parseLevel) +TEST_CASE("parseLevel") { - const auto data = GetParam(); + const auto data = GENERATE(from_range(testParseLog)); const auto maybe = llarp::LogLevelFromString(data.input); - EXPECT_EQ(maybe, data.level); + CHECK(maybe == data.level); } -static const TestParseLog testParseLog[] = { - // bad cases - {"bogus", {}}, - {"BOGUS", {}}, - {"", {}}, - {" ", {}}, - // good cases - {"info", llarp::eLogInfo}, - {"infO", llarp::eLogInfo}, - {"iNfO", llarp::eLogInfo}, - {"InfO", llarp::eLogInfo}, - {"INFO", llarp::eLogInfo}, - {"debug", llarp::eLogDebug}, - {"warn", llarp::eLogWarn}, - {"error", llarp::eLogError}, - {"none", llarp::eLogNone}}; - -INSTANTIATE_TEST_SUITE_P(TestLogConfig, LogLevelTest, - ::testing::ValuesIn(testParseLog)); - -TEST_F(LogLevelTest, TestLogLevelToName) +TEST_CASE("TestLogLevelToName") { - EXPECT_EQ("Trace", LogLevelToName(llarp::eLogTrace)); - EXPECT_EQ("Debug", LogLevelToName(llarp::eLogDebug)); - EXPECT_EQ("Info", LogLevelToName(llarp::eLogInfo)); - EXPECT_EQ("Warn", LogLevelToName(llarp::eLogWarn)); - EXPECT_EQ("Error", LogLevelToName(llarp::eLogError)); - EXPECT_EQ("None", LogLevelToName(llarp::eLogNone)); - EXPECT_EQ("???", LogLevelToName( (llarp::LogLevel)99999 )); + CHECK("Trace" == LogLevelToName(llarp::eLogTrace)); + CHECK("Debug" == LogLevelToName(llarp::eLogDebug)); + CHECK("Info" == LogLevelToName(llarp::eLogInfo)); + CHECK("Warn" == LogLevelToName(llarp::eLogWarn)); + CHECK("Error" == LogLevelToName(llarp::eLogError)); + CHECK("None" == LogLevelToName(llarp::eLogNone)); + CHECK("???" == LogLevelToName((llarp::LogLevel)99999)); } -TEST_F(LogLevelTest, TestLogLevelToString) +TEST_CASE("TestLogLevelToString") { - EXPECT_EQ("TRC", LogLevelToString(llarp::eLogTrace)); - EXPECT_EQ("DBG", LogLevelToString(llarp::eLogDebug)); - EXPECT_EQ("NFO", LogLevelToString(llarp::eLogInfo)); - EXPECT_EQ("WRN", LogLevelToString(llarp::eLogWarn)); - EXPECT_EQ("ERR", LogLevelToString(llarp::eLogError)); - EXPECT_EQ("???", LogLevelToString(llarp::eLogNone)); + CHECK("TRC" == LogLevelToString(llarp::eLogTrace)); + CHECK("DBG" == LogLevelToString(llarp::eLogDebug)); + CHECK("NFO" == LogLevelToString(llarp::eLogInfo)); + CHECK("WRN" == LogLevelToString(llarp::eLogWarn)); + CHECK("ERR" == LogLevelToString(llarp::eLogError)); + CHECK("???" == LogLevelToString(llarp::eLogNone)); } - diff --git a/test/util/thread/test_llarp_util_queue.cpp b/test/util/thread/test_llarp_util_queue.cpp index 022e67ea1..4410b5ab7 100644 --- a/test/util/thread/test_llarp_util_queue.cpp +++ b/test/util/thread/test_llarp_util_queue.cpp @@ -7,7 +7,7 @@ #include #include -#include +#include using namespace llarp; using namespace llarp::thread; @@ -24,8 +24,7 @@ class Element public: Element(double d, bool _stop = false) : data(d), shouldStop(_stop) - { - } + {} double val() const @@ -46,7 +45,7 @@ operator==(const Element& lhs, const Element& rhs) return lhs.val() == rhs.val(); } -using ObjQueue = Queue< Element >; +using ObjQueue = Queue; class Args { @@ -68,14 +67,8 @@ class Args volatile size_t endSignal; Args(size_t _iterations, size_t size = 20 * 1000) - : queue(size) - , iterations(_iterations) - , count(0) - , startSignal(0) - , runSignal(0) - , endSignal(0) - { - } + : queue(size), iterations(_iterations), count(0), startSignal(0), runSignal(0), endSignal(0) + {} bool signal() const @@ -93,10 +86,10 @@ popFrontTester(Args& args) args.cv.wait(lock, [&] { return args.signal(); }); } - for(;;) + for (;;) { Element e = args.queue.popFront(); - if(e.stop()) + if (e.stop()) { break; } @@ -112,9 +105,9 @@ pushBackTester(Args& args) args.cv.wait(lock, [&] { return args.signal(); }); } - for(size_t i = 0; i < args.iterations; ++i) + for (size_t i = 0; i < args.iterations; ++i) { - Element e{static_cast< double >(i)}; + Element e{static_cast(i)}; args.queue.pushBack(e); } @@ -122,38 +115,34 @@ pushBackTester(Args& args) } void -abaThread(char* firstValue, char* lastValue, Queue< char* >& queue, - util::Barrier& barrier) +abaThread(char* firstValue, char* lastValue, Queue& queue, util::Barrier& barrier) { barrier.Block(); - for(char* val = firstValue; val <= lastValue; ++val) + for (char* val = firstValue; val <= lastValue; ++val) { queue.pushBack(val); } } struct Exception : public std::exception -{ -}; +{}; struct ExceptionTester { - static std::atomic< std::thread::id > throwFrom; + static std::atomic throwFrom; void test() { - if(throwFrom != std::thread::id() - && std::this_thread::get_id() == throwFrom) + if (throwFrom != std::thread::id() && std::this_thread::get_id() == throwFrom) { throw Exception(); } } ExceptionTester() - { - } + {} ExceptionTester(const ExceptionTester&) { @@ -168,7 +157,7 @@ struct ExceptionTester } }; -std::atomic< std::thread::id > ExceptionTester::throwFrom = {std::thread::id()}; +std::atomic ExceptionTester::throwFrom = {std::thread::id()}; void sleepNWait(std::chrono::microseconds microseconds, util::Barrier& barrier) @@ -178,18 +167,18 @@ sleepNWait(std::chrono::microseconds microseconds, util::Barrier& barrier) } void -exceptionProducer(Queue< ExceptionTester >& queue, util::Semaphore& semaphore, - std::atomic_size_t& caught) +exceptionProducer( + Queue& queue, util::Semaphore& semaphore, std::atomic_size_t& caught) { static constexpr size_t iterations = 3; - for(size_t i = 0; i < iterations; ++i) + for (size_t i = 0; i < iterations; ++i) { try { queue.pushBack(ExceptionTester()); } - catch(const Exception&) + catch (const Exception&) { ++caught; } @@ -204,18 +193,15 @@ struct MoveTester size_t& moveCounter; size_t value; - explicit MoveTester(size_t& counter, size_t val) - : moved(false), moveCounter(counter), value(val) - { - } + explicit MoveTester(size_t& counter, size_t val) : moved(false), moveCounter(counter), value(val) + {} explicit MoveTester(const MoveTester& rhs) = delete; MoveTester& operator=(const MoveTester& rhs) = delete; - MoveTester(MoveTester&& rhs) - : moved(false), moveCounter(rhs.moveCounter), value(rhs.value) + MoveTester(MoveTester&& rhs) : moved(false), moveCounter(rhs.moveCounter), value(rhs.value) { rhs.moved = true; moveCounter++; @@ -224,8 +210,8 @@ struct MoveTester MoveTester& operator=(MoveTester&& rhs) { - value = rhs.value; - rhs.moved = true; + value = rhs.value; + rhs.moved = true; moveCounter = rhs.moveCounter; moveCounter++; @@ -234,22 +220,22 @@ struct MoveTester } }; -TEST(TestQueue, single) +TEST_CASE("single") { ObjQueue queue(1u); - ASSERT_EQ(0u, queue.size()); - ASSERT_EQ(1u, queue.capacity()); + REQUIRE(0u == queue.size()); + REQUIRE(1u == queue.capacity()); } -TEST(TestQueue, breathing) +TEST_CASE("breathing") { static constexpr size_t DEFAULT_CAP = 10 * 1000; ObjQueue queue(DEFAULT_CAP); - ASSERT_EQ(0u, queue.size()); - ASSERT_EQ(DEFAULT_CAP, queue.capacity()); + REQUIRE(0u == queue.size()); + REQUIRE(DEFAULT_CAP == queue.capacity()); Element e1(1.0); Element e2(2.0); @@ -263,260 +249,261 @@ TEST(TestQueue, breathing) Element p2 = queue.popFront(); Element p3 = queue.popFront(); - ASSERT_EQ(e1, p1); - ASSERT_EQ(e2, p2); - ASSERT_EQ(e3, p3); + REQUIRE(e1 == p1); + REQUIRE(e2 == p2); + REQUIRE(e3 == p3); } -TEST(TestQueue, singleProducerManyConsumer) +TEST_CASE("Single producer many consumer") { static constexpr size_t iterations = 100 * 1000; static constexpr size_t numThreads = 5; - std::array< std::thread, numThreads > threads; + std::array threads; Args args{iterations}; { LockGuard lock(args.mutex); - for(size_t i = 0; i < threads.size(); ++i) + for (size_t i = 0; i < threads.size(); ++i) { threads[i] = std::thread(std::bind(&popFrontTester, std::ref(args))); - args.cv.wait(lock, [&] { return args.count != i+1; }); + args.cv.wait(lock, [&] { return args.count != i + 1; }); } args.runSignal++; } - for(size_t i = 0; i < iterations; ++i) + for (size_t i = 0; i < iterations; ++i) { - Element e{static_cast< double >(i)}; + Element e{static_cast(i)}; args.queue.pushBack(e); } - for(size_t i = 0; i < numThreads; ++i) + for (size_t i = 0; i < numThreads; ++i) { Element e{0.0, true}; args.queue.pushBack(e); } - for(size_t i = 0; i < numThreads; ++i) + for (size_t i = 0; i < numThreads; ++i) { threads[i].join(); } - ASSERT_EQ(0u, args.queue.size()); + REQUIRE(0u == args.queue.size()); } -TEST(TestQueue, manyProducerManyConsumer) +TEST_CASE("Many producer many consumer") { static constexpr size_t iterations = 100 * 1000; static constexpr size_t numThreads = 5; - std::array< std::thread, numThreads * 2 > threads; + std::array threads; Args args{iterations}; { LockGuard lock(args.mutex); - for(size_t i = 0; i < numThreads; ++i) + for (size_t i = 0; i < numThreads; ++i) { threads[i] = std::thread(std::bind(&popFrontTester, std::ref(args))); - args.cv.wait(lock, [&] { return args.count != i+1; }); + args.cv.wait(lock, [&] { return args.count != i + 1; }); } - for(size_t i = 0; i < numThreads; ++i) + for (size_t i = 0; i < numThreads; ++i) { - threads[i + numThreads] = - std::thread(std::bind(&pushBackTester, std::ref(args))); - args.cv.wait(lock, [&] { return args.count != numThreads+i+1; }); + threads[i + numThreads] = std::thread(std::bind(&pushBackTester, std::ref(args))); + args.cv.wait(lock, [&] { return args.count != numThreads + i + 1; }); } args.runSignal++; } - for(auto& thread : threads) + for (auto& thread : threads) { thread.join(); } - ASSERT_EQ(0u, args.queue.size()); + REQUIRE(0u == args.queue.size()); } -TEST(TestQueue, ABAEmpty) +TEST_CASE("ABA empty") { // Verify we avoid the ABA problem, where multiple threads try to push an // object to the same "empty" position in the queue. - static constexpr size_t numThreads = 50; - static constexpr size_t numValues = 6; + static constexpr size_t numThreads = 50; + static constexpr size_t numValues = 6; static constexpr size_t numIterations = 1000; - static constexpr size_t numEntries = numThreads * numValues; + static constexpr size_t numEntries = numThreads * numValues; char block[numEntries]; - for(size_t i = 0; i < numIterations; ++i) + for (size_t i = 0; i < numIterations; ++i) { util::Barrier barrier{numThreads + 1}; - Queue< char* > queue{numEntries + 1}; + Queue queue{numEntries + 1}; - std::array< std::thread, numThreads + 1 > threads; + std::array threads; char* nextValue[numThreads]; char* lastValue[numThreads]; - for(size_t j = 0; j < numThreads; ++j) + for (size_t j = 0; j < numThreads; ++j) { nextValue[j] = block + (numValues * j); lastValue[j] = block + (numValues * (j + 1)) - 1; - threads[j] = std::thread([&, n=nextValue[j], l=lastValue[j]] { abaThread(n, l, queue, barrier); }); + threads[j] = + std::thread([&, n = nextValue[j], l = lastValue[j]] { abaThread(n, l, queue, barrier); }); } threads[numThreads] = std::thread([&] { - std::this_thread::sleep_for(100us); - barrier.Block(); + std::this_thread::sleep_for(100us); + barrier.Block(); }); - for(size_t j = 0; j < numEntries; ++j) + for (size_t j = 0; j < numEntries; ++j) { char* val = queue.popFront(); size_t k = 0; - for(k = 0; k < numThreads; ++k) + for (k = 0; k < numThreads; ++k) { - if(val == nextValue[k]) + if (val == nextValue[k]) { nextValue[k] += (val == lastValue[k] ? 0 : 1); - ASSERT_LE(nextValue[k], lastValue[k]); + REQUIRE(nextValue[k] <= lastValue[k]); break; } } - ASSERT_LT(k, numThreads); + REQUIRE(k < numThreads); } - for(auto& thread : threads) + for (auto& thread : threads) { thread.join(); } - ASSERT_EQ(0u, queue.size()); + REQUIRE(0u == queue.size()); } } -TEST(TestQueue, generationCount) +TEST_CASE("Generation count") { // Verify functionality after running through a full cycle (and invoking the // generation rollover logic). // For a queue of size 3, this is currently 508 cycles, implying we need to go // through at least 3048 objects (3 * 508 * 2) to trigger this logic twice. static constexpr size_t numThreads = 6; - static constexpr size_t queueSize = 3; + static constexpr size_t queueSize = 3; static constexpr size_t numEntries = 3060; - static constexpr size_t numValues = numEntries / numThreads; + static constexpr size_t numValues = numEntries / numThreads; char block[numEntries]; util::Barrier barrier{numThreads + 1}; - Queue< char* > queue{queueSize}; + Queue queue{queueSize}; - std::array< std::thread, numThreads + 1 > threads; + std::array threads; char* nextValue[numThreads]; char* lastValue[numThreads]; - for(size_t j = 0; j < numThreads; ++j) + for (size_t j = 0; j < numThreads; ++j) { nextValue[j] = block + (numValues * j); lastValue[j] = block + (numValues * (j + 1)) - 1; - threads[j] = std::thread([&, n=nextValue[j], l=lastValue[j]] { abaThread(n, l, queue, barrier); }); + threads[j] = + std::thread([&, n = nextValue[j], l = lastValue[j]] { abaThread(n, l, queue, barrier); }); } threads[numThreads] = std::thread([&] { - std::this_thread::sleep_for(100ms); - barrier.Block(); + std::this_thread::sleep_for(100ms); + barrier.Block(); }); - for(size_t j = 0; j < numEntries; ++j) + for (size_t j = 0; j < numEntries; ++j) { char* val = queue.popFront(); size_t k = 0; - for(k = 0; k < numThreads; ++k) + for (k = 0; k < numThreads; ++k) { - if(val == nextValue[k]) + if (val == nextValue[k]) { nextValue[k] += (val == lastValue[k] ? 0 : 1); - ASSERT_LE(nextValue[k], lastValue[k]); + REQUIRE(nextValue[k] <= lastValue[k]); break; } } - ASSERT_LT(k, numThreads); + REQUIRE(k < numThreads); } - for(auto& thread : threads) + for (auto& thread : threads) { thread.join(); } - ASSERT_EQ(0u, queue.size()); + REQUIRE(0u == queue.size()); } -TEST(TestQueue, basicExceptionSafety) +TEST_CASE("Basic exception safety") { ExceptionTester::throwFrom = std::this_thread::get_id(); - Queue< ExceptionTester > queue{1}; + Queue queue{1}; - ASSERT_THROW(queue.pushBack(ExceptionTester()), Exception); + REQUIRE_THROWS_AS(queue.pushBack(ExceptionTester()), Exception); ExceptionTester::throwFrom = std::thread::id(); } -TEST(TestQueue, exceptionSafety) +TEST_CASE("Exception safety") { - ExceptionTester::throwFrom = std::thread::id(); + ExceptionTester::throwFrom = std::thread::id(); static constexpr size_t queueSize = 3; - Queue< ExceptionTester > queue{queueSize}; + Queue queue{queueSize}; - ASSERT_EQ(QueueReturn::Success, queue.pushBack(ExceptionTester())); - ASSERT_EQ(QueueReturn::Success, queue.pushBack(ExceptionTester())); - ASSERT_EQ(QueueReturn::Success, queue.pushBack(ExceptionTester())); - ASSERT_NE(QueueReturn::Success, queue.tryPushBack(ExceptionTester())); + REQUIRE(QueueReturn::Success == queue.pushBack(ExceptionTester())); + REQUIRE(QueueReturn::Success == queue.pushBack(ExceptionTester())); + REQUIRE(QueueReturn::Success == queue.pushBack(ExceptionTester())); + REQUIRE(QueueReturn::Success != queue.tryPushBack(ExceptionTester())); util::Semaphore semaphore{0}; std::atomic_size_t caught = {0}; - std::thread producer(std::bind(&exceptionProducer, std::ref(queue), - std::ref(semaphore), std::ref(caught))); + std::thread producer( + std::bind(&exceptionProducer, std::ref(queue), std::ref(semaphore), std::ref(caught))); ExceptionTester::throwFrom = std::this_thread::get_id(); - ASSERT_THROW({ (void)queue.popFront(); }, Exception); + REQUIRE_THROWS_AS(queue.popFront(), Exception); using namespace std::literals; // Now the queue is not full, and the producer thread can start adding items. - ASSERT_TRUE(semaphore.waitFor(1s)); + REQUIRE(semaphore.waitFor(1s)); - ASSERT_EQ(queueSize, queue.size()); + REQUIRE(queueSize == queue.size()); - ASSERT_THROW({ (void)queue.popFront(); }, Exception); + REQUIRE_THROWS_AS(queue.popFront(), Exception); // Now the queue is not full, and the producer thread can start adding items. - ASSERT_TRUE(semaphore.waitFor(1s)); + REQUIRE(semaphore.waitFor(1s)); - ASSERT_EQ(queueSize, queue.size()); + REQUIRE(queueSize == queue.size()); // Pushing into the queue with exception empties the queue. ExceptionTester::throwFrom = producer.get_id(); @@ -524,61 +511,61 @@ TEST(TestQueue, exceptionSafety) // pop an item to unblock the pusher (void)queue.popFront(); - ASSERT_TRUE(semaphore.waitFor(1s)); + REQUIRE(semaphore.waitFor(1s)); - ASSERT_EQ(1u, caught); + REQUIRE(1u == caught); - ASSERT_EQ(0u, queue.size()); - ASSERT_TRUE(queue.empty()); + REQUIRE(0u == queue.size()); + REQUIRE(queue.empty()); // after throwing, the queue works fine. - ASSERT_EQ(QueueReturn::Success, queue.pushBack(ExceptionTester())); - ASSERT_EQ(QueueReturn::Success, queue.pushBack(ExceptionTester())); - ASSERT_EQ(QueueReturn::Success, queue.pushBack(ExceptionTester())); - ASSERT_NE(QueueReturn::Success, queue.tryPushBack(ExceptionTester())); + REQUIRE(QueueReturn::Success == queue.pushBack(ExceptionTester())); + REQUIRE(QueueReturn::Success == queue.pushBack(ExceptionTester())); + REQUIRE(QueueReturn::Success == queue.pushBack(ExceptionTester())); + REQUIRE(QueueReturn::Success != queue.tryPushBack(ExceptionTester())); ExceptionTester::throwFrom = std::thread::id(); producer.join(); } -TEST(TestQueue, moveIt) +TEST_CASE("Move it") { static constexpr size_t queueSize = 40; - Queue< MoveTester > queue{queueSize}; + Queue queue{queueSize}; size_t counter = 0; queue.pushBack(MoveTester{counter, 0}); - ASSERT_EQ(1u, counter); + REQUIRE(1u == counter); MoveTester tester2(counter, 2); queue.pushBack(std::move(tester2)); - ASSERT_TRUE(tester2.moved); - ASSERT_EQ(2u, counter); + REQUIRE(tester2.moved); + REQUIRE(2u == counter); - ASSERT_EQ(QueueReturn::Success, queue.tryPushBack(MoveTester{counter, 3})); - ASSERT_EQ(3u, counter); + REQUIRE(QueueReturn::Success == queue.tryPushBack(MoveTester{counter, 3})); + REQUIRE(3u == counter); MoveTester tester4(counter, 4); - ASSERT_EQ(QueueReturn::Success, queue.tryPushBack(std::move(tester4))); + REQUIRE(QueueReturn::Success == queue.tryPushBack(std::move(tester4))); - ASSERT_TRUE(tester4.moved); - ASSERT_EQ(4u, counter); + REQUIRE(tester4.moved); + REQUIRE(4u == counter); MoveTester popped = queue.popFront(); (void)popped; - ASSERT_EQ(5u, counter); + REQUIRE(5u == counter); - std::optional< MoveTester > optPopped = queue.tryPopFront(); + std::optional optPopped = queue.tryPopFront(); - ASSERT_TRUE(optPopped); + REQUIRE(optPopped); // Moved twice here to construct the optional. - ASSERT_EQ(6u, counter); + REQUIRE(6u == counter); } diff --git a/test/util/thread/test_llarp_util_queue_manager.cpp b/test/util/thread/test_llarp_util_queue_manager.cpp index 7776130e2..526c6b289 100644 --- a/test/util/thread/test_llarp_util_queue_manager.cpp +++ b/test/util/thread/test_llarp_util_queue_manager.cpp @@ -2,19 +2,19 @@ #include #include -#include +#include using namespace llarp::thread; void generation(QueueManager& manager, uint32_t pushIndex, uint32_t popIndex) { - ASSERT_GE(pushIndex, popIndex); - ASSERT_LE(pushIndex - popIndex, manager.capacity()); + REQUIRE(pushIndex >= popIndex); + REQUIRE(pushIndex - popIndex <= manager.capacity()); - for(uint32_t i = 0; i < popIndex; ++i) + for (uint32_t i = 0; i < popIndex; ++i) { - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; (void)manager.reservePushIndex(gen, index); @@ -22,20 +22,20 @@ generation(QueueManager& manager, uint32_t pushIndex, uint32_t popIndex) auto result = manager.reservePopIndex(gen, index); - ASSERT_EQ(result, QueueReturn::Success); - ASSERT_EQ(index, i % manager.capacity()); + REQUIRE(result == QueueReturn::Success); + REQUIRE(index == i % manager.capacity()); manager.commitPopIndex(gen, index); } - for(uint32_t i = popIndex; i < pushIndex; ++i) + for (uint32_t i = popIndex; i < pushIndex; ++i) { - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; auto result = manager.reservePushIndex(gen, index); - ASSERT_EQ(result, QueueReturn::Success); - ASSERT_EQ(index, i % manager.capacity()); + REQUIRE(result == QueueReturn::Success); + REQUIRE(index == i % manager.capacity()); manager.commitPushIndex(gen, index); } @@ -46,24 +46,23 @@ class IntQueue private: QueueManager manager; - std::vector< int > data; + std::vector data; public: IntQueue(const IntQueue&) = delete; explicit IntQueue(size_t capacity) : manager(capacity), data(capacity, 0) - { - } + {} ~IntQueue() = default; bool tryPushBack(int value) { - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; - if(manager.reservePushIndex(gen, index) == QueueReturn::Success) + if (manager.reservePushIndex(gen, index) == QueueReturn::Success) { data[index] = value; manager.commitPushIndex(gen, index); @@ -75,13 +74,13 @@ class IntQueue } } - std::optional< int > + std::optional tryPopFront() { - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; - if(manager.reservePopIndex(gen, index) == QueueReturn::Success) + if (manager.reservePopIndex(gen, index) == QueueReturn::Success) { int result = data[index]; manager.commitPopIndex(gen, index); @@ -114,14 +113,12 @@ struct QueueData QueueManager::AtomicIndex m_pushIndex; // Index in the buffer that the next // element will be added to. - char m_pushPadding[QueueManager::Alignment - - sizeof(QueueManager::AtomicIndex)]; + char m_pushPadding[QueueManager::Alignment - sizeof(QueueManager::AtomicIndex)]; QueueManager::AtomicIndex m_popIndex; // Index in the buffer that the next // element will be removed from. - char - m_popPadding[QueueManager::Alignment - sizeof(QueueManager::AtomicIndex)]; + char m_popPadding[QueueManager::Alignment - sizeof(QueueManager::AtomicIndex)]; const size_t m_capacity; // max size of the manager. @@ -133,11 +130,10 @@ struct QueueData std::uint32_t* m_states; // Array of index states. }; -static_assert(sizeof(QueueData) == sizeof(QueueManager), - "QueueData not updated"); +static_assert(sizeof(QueueData) == sizeof(QueueManager), "QueueData not updated"); static constexpr uint32_t GENERATION_COUNT_SHIFT = 0x2; -static constexpr uint32_t ELEMENT_STATE_MASK = 0x3; +static constexpr uint32_t ELEMENT_STATE_MASK = 0x3; struct QueueIntrospection { @@ -146,9 +142,8 @@ struct QueueIntrospection public: QueueIntrospection(const QueueManager& manager) - : data(reinterpret_cast< const QueueData* >(&manager)) - { - } + : data(reinterpret_cast(&manager)) + {} uint32_t pushIndex() const @@ -183,8 +178,7 @@ struct QueueIntrospection ElementState elementState(uint32_t index) const { - return static_cast< ElementState >(data->m_states[index] - & ELEMENT_STATE_MASK); + return static_cast(data->m_states[index] & ELEMENT_STATE_MASK); } uint32_t @@ -209,176 +203,162 @@ struct QueueIntrospection void adjustGeneration(QueueManager& manager, uint32_t gen) { - QueueData* data = reinterpret_cast< QueueData* >(&manager); + QueueData* data = reinterpret_cast(&manager); auto capacity = manager.capacity(); - for(size_t i = 0; i < capacity; ++i) + for (size_t i = 0; i < capacity; ++i) { data->m_states[i] = gen << GENERATION_COUNT_SHIFT; } - *reinterpret_cast< QueueManager::AtomicIndex* >(&data->m_pushIndex) = - (gen * capacity); - *reinterpret_cast< QueueManager::AtomicIndex* >(&data->m_popIndex) = - (gen * capacity); + *reinterpret_cast(&data->m_pushIndex) = (gen * capacity); + *reinterpret_cast(&data->m_popIndex) = (gen * capacity); } void -dirtyGenerate(QueueManager& manager, uint32_t pushCombinedIndex, - uint32_t popCombinedIndex) +dirtyGenerate(QueueManager& manager, uint32_t pushCombinedIndex, uint32_t popCombinedIndex) { - ASSERT_GE(pushCombinedIndex, popCombinedIndex); - ASSERT_LE(pushCombinedIndex - popCombinedIndex, manager.capacity()); + REQUIRE(pushCombinedIndex >= popCombinedIndex); + REQUIRE(pushCombinedIndex - popCombinedIndex <= manager.capacity()); uint32_t capacity = manager.capacity(); - uint32_t start = - static_cast< uint32_t >(popCombinedIndex / manager.capacity()); + uint32_t start = static_cast(popCombinedIndex / manager.capacity()); adjustGeneration(manager, start); - generation(manager, pushCombinedIndex - (start * capacity), - popCombinedIndex - (start * capacity)); + generation( + manager, pushCombinedIndex - (start * capacity), popCombinedIndex - (start * capacity)); } -TEST(TestQueueManager, SimpleUsage) +TEST_CASE("Simple usage") { IntQueue queue(2); bool rc = queue.tryPushBack(1); - ASSERT_TRUE(rc); + REQUIRE(rc); rc = queue.tryPushBack(2); - ASSERT_TRUE(rc); + REQUIRE(rc); rc = queue.tryPushBack(3); - ASSERT_FALSE(rc); + REQUIRE_FALSE(rc); - ASSERT_EQ(2u, queue.size()); + REQUIRE(2u == queue.size()); auto result = queue.tryPopFront(); - ASSERT_TRUE(result); - ASSERT_EQ(1, *result); + REQUIRE(result); + REQUIRE(1 == *result); } -class BasicFunctionality : public ::testing::TestWithParam< uint32_t > +TEST_CASE("Push") { -}; - -TEST_P(BasicFunctionality, Push) -{ - uint32_t val = GetParam(); + uint32_t val = GENERATE(range(1u, 100u)); QueueManager manager(val); - ASSERT_EQ(0u, manager.size()); + REQUIRE(0u == manager.size()); - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; - for(uint32_t i = 0; i < val; ++i) + for (uint32_t i = 0; i < val; ++i) { - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); - ASSERT_EQ(i, index); - ASSERT_EQ(0u, gen); - ASSERT_EQ(i, manager.size() - 1); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); + REQUIRE(i == index); + REQUIRE(0u == gen); + REQUIRE(i == manager.size() - 1); manager.commitPushIndex(gen, index); } - ASSERT_EQ(QueueReturn::QueueFull, manager.reservePushIndex(gen, index)); - ASSERT_EQ(val, manager.size()); + REQUIRE(QueueReturn::QueueFull == manager.reservePushIndex(gen, index)); + REQUIRE(val == manager.size()); } -TEST_P(BasicFunctionality, AcquiringPopIndex) +TEST_CASE("Basic functionality, acquiringPopIndex") { - uint32_t capacity = GetParam(); + uint32_t capacity = GENERATE(range(1u, 100u)); QueueManager manager(capacity); - ASSERT_EQ(0u, manager.size()); + REQUIRE(0u == manager.size()); - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; - for(uint32_t g = 0; g < 3; ++g) + for (uint32_t g = 0; g < 3; ++g) { - for(uint32_t idx = 0; idx < capacity; ++idx) + for (uint32_t idx = 0; idx < capacity; ++idx) { - ASSERT_EQ(QueueReturn::QueueEmpty, manager.reservePopIndex(gen, index)); + REQUIRE(QueueReturn::QueueEmpty == manager.reservePopIndex(gen, index)); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); - ASSERT_EQ(g, gen); - ASSERT_EQ(index, idx); - ASSERT_EQ(1u, manager.size()); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); + REQUIRE(g == gen); + REQUIRE(index == idx); + REQUIRE(1u == manager.size()); manager.commitPushIndex(gen, index); - ASSERT_EQ(1u, manager.size()); + REQUIRE(1u == manager.size()); - ASSERT_EQ(QueueReturn::Success, manager.reservePopIndex(gen, index)); - ASSERT_EQ(g, gen); - ASSERT_EQ(index, idx); - ASSERT_EQ(0u, manager.size()); + REQUIRE(QueueReturn::Success == manager.reservePopIndex(gen, index)); + REQUIRE(g == gen); + REQUIRE(index == idx); + REQUIRE(0u == manager.size()); manager.commitPopIndex(gen, index); - ASSERT_EQ(0u, manager.size()); + REQUIRE(0u == manager.size()); } } } -TEST_P(BasicFunctionality, pushIndex) +TEST_CASE("Basic functionality, pushIndex") { - uint32_t capacity = GetParam(); + uint32_t capacity = GENERATE(range(1u, 100u)); QueueManager manager(capacity); - ASSERT_EQ(0u, manager.size()); + REQUIRE(0u == manager.size()); uint32_t generation = 0; - uint32_t index = 0; + uint32_t index = 0; // Fill the queue - for(uint32_t idx = 0; idx < capacity; ++idx) + for (uint32_t idx = 0; idx < capacity; ++idx) { manager.reservePushIndex(generation, index); manager.commitPushIndex(generation, index); } - ASSERT_EQ(capacity, manager.size()); + REQUIRE(capacity == manager.size()); - for(uint32_t gen = 0; gen < 3; ++gen) + for (uint32_t gen = 0; gen < 3; ++gen) { - for(uint32_t idx = 0; idx < capacity; ++idx) + for (uint32_t idx = 0; idx < capacity; ++idx) { - ASSERT_EQ(QueueReturn::QueueFull, - manager.reservePushIndex(generation, index)); + REQUIRE(QueueReturn::QueueFull == manager.reservePushIndex(generation, index)); - ASSERT_EQ(QueueReturn::Success, - manager.reservePopIndex(generation, index)); + REQUIRE(QueueReturn::Success == manager.reservePopIndex(generation, index)); - ASSERT_EQ(generation, gen); - ASSERT_EQ(index, idx); - ASSERT_EQ(capacity - 1, manager.size()); + REQUIRE(generation == gen); + REQUIRE(index == idx); + REQUIRE(capacity - 1 == manager.size()); manager.commitPopIndex(generation, index); - ASSERT_EQ(capacity - 1, manager.size()); + REQUIRE(capacity - 1 == manager.size()); - ASSERT_EQ(QueueReturn::Success, - manager.reservePushIndex(generation, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(generation, index)); - ASSERT_EQ(generation, gen + 1); - ASSERT_EQ(index, idx); - ASSERT_EQ(manager.size(), capacity); + REQUIRE(generation == gen + 1); + REQUIRE(index == idx); + REQUIRE(manager.size() == capacity); manager.commitPushIndex(generation, index); - ASSERT_EQ(manager.size(), capacity); + REQUIRE(manager.size() == capacity); } } } -INSTANTIATE_TEST_SUITE_P(TestQueueManagerBasic, BasicFunctionality, - ::testing::Range(1u, 100u)); - // Potential issues: // - That pushing an element at the max combined index will push the next // element at index 0 @@ -389,51 +369,49 @@ INSTANTIATE_TEST_SUITE_P(TestQueueManagerBasic, BasicFunctionality, // - That reservePopIndexForClear and abortPushIndexReservation clear the // correct element and increment push/pop -TEST(TestQueueManagerMaxCombinedIndex, PushAtMax) +TEST_CASE("Push at max") { QueueManager manager(1); QueueIntrospection state{manager}; - const uint32_t MAX_COMBINED_INDEX = - std::numeric_limits< uint32_t >::max() >> 2; - const uint32_t MAX_GENERATION = std::numeric_limits< uint32_t >::max() >> 2; + const uint32_t MAX_COMBINED_INDEX = std::numeric_limits::max() >> 2; + const uint32_t MAX_GENERATION = std::numeric_limits::max() >> 2; const uint32_t maxGeneration = QueueIntrospection(manager).maxGen(); - const uint32_t maxCombinedIndex = - QueueIntrospection(manager).maxCombinedIndex(); + const uint32_t maxCombinedIndex = QueueIntrospection(manager).maxCombinedIndex(); - ASSERT_EQ(maxGeneration, MAX_GENERATION); - ASSERT_EQ(maxCombinedIndex, MAX_COMBINED_INDEX); + REQUIRE(maxGeneration == MAX_GENERATION); + REQUIRE(maxCombinedIndex == MAX_COMBINED_INDEX); dirtyGenerate(manager, MAX_COMBINED_INDEX, MAX_COMBINED_INDEX); - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; - ASSERT_EQ(0u, manager.size()); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); - ASSERT_EQ(MAX_GENERATION, gen); - ASSERT_EQ(0u, index); + REQUIRE(0u == manager.size()); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); + REQUIRE(MAX_GENERATION == gen); + REQUIRE(0u == index); manager.commitPushIndex(gen, index); - ASSERT_EQ(QueueReturn::Success, manager.reservePopIndex(gen, index)); - ASSERT_EQ(MAX_GENERATION, gen); - ASSERT_EQ(0u, index); + REQUIRE(QueueReturn::Success == manager.reservePopIndex(gen, index)); + REQUIRE(MAX_GENERATION == gen); + REQUIRE(0u == index); manager.commitPopIndex(gen, index); - ASSERT_EQ(0u, manager.size()); + REQUIRE(0u == manager.size()); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); - ASSERT_EQ(0u, gen); - ASSERT_EQ(0u, index); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); + REQUIRE(0u == gen); + REQUIRE(0u == index); manager.commitPushIndex(gen, index); - ASSERT_EQ(1u, manager.size()); + REQUIRE(1u == manager.size()); - ASSERT_EQ(QueueReturn::Success, manager.reservePopIndex(gen, index)); - ASSERT_EQ(0u, gen); - ASSERT_EQ(0u, index); + REQUIRE(QueueReturn::Success == manager.reservePopIndex(gen, index)); + REQUIRE(0u == gen); + REQUIRE(0u == index); manager.commitPopIndex(gen, index); - ASSERT_EQ(0u, manager.size()); + REQUIRE(0u == manager.size()); } struct CombinedIndexData @@ -451,13 +429,43 @@ operator<<(std::ostream& os, CombinedIndexData d) return os; } -class PopAtMax : public ::testing::TestWithParam< CombinedIndexData > -{ -}; +std::vector PopAtMaxData{// Capacity 2 queues for a couple generations + {2, 1, 0}, + {2, 2, 0}, + {2, 2, 1}, + {2, 2, 2}, + {2, 3, 1}, + {2, 3, 2}, + {2, 3, 3}, + {2, 4, 2}, + {2, 4, 3}, + {2, 4, 4}, -TEST_P(PopAtMax, PopAtMax) + // Capacity 3 queues for a couple generations + {3, 2, 0}, + {3, 3, 0}, + {3, 3, 1}, + {3, 3, 2}, + {3, 3, 3}, + {3, 4, 1}, + {3, 4, 2}, + {3, 4, 3}, + {3, 4, 4}, + {3, 5, 2}, + {3, 5, 3}, + {3, 5, 4}, + {3, 5, 5}, + + // Capacity 7 queue + {7, 6, 0}, + {7, 7, 0}, + {7, 7, 6}, + {7, 13, 7}, + {7, 14, 7}}; + +TEST_CASE("Pop at max") { - const auto& d = GetParam(); + const auto& d = GENERATE(from_range(PopAtMaxData)); QueueManager manager(d.capacity); @@ -466,144 +474,62 @@ TEST_P(PopAtMax, PopAtMax) adjustGeneration(manager, MAX_GEN); - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; // Push and pop elements up until the pop-index. - for(size_t j = 0; j < d.popIndex; ++j) + for (size_t j = 0; j < d.popIndex; ++j) { uint32_t INDEX = j % d.capacity; - uint32_t GEN = (MAX_GEN + j / d.capacity) % NUM_GEN; + uint32_t GEN = (MAX_GEN + j / d.capacity) % NUM_GEN; - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); - ASSERT_EQ(INDEX, index); - ASSERT_EQ(GEN, gen); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); + REQUIRE(INDEX == index); + REQUIRE(GEN == gen); manager.commitPushIndex(gen, index); - ASSERT_EQ(1u, manager.size()); + REQUIRE(1u == manager.size()); - ASSERT_EQ(QueueReturn::Success, manager.reservePopIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePopIndex(gen, index)); - ASSERT_EQ(INDEX, index); - ASSERT_EQ(GEN, gen); + REQUIRE(INDEX == index); + REQUIRE(GEN == gen); manager.commitPopIndex(gen, index); - ASSERT_EQ(0u, manager.size()); + REQUIRE(0u == manager.size()); } // Push elements up to the push index - for(size_t j = d.popIndex; j < d.pushIndex; ++j) + for (size_t j = d.popIndex; j < d.pushIndex; ++j) { uint32_t INDEX = j % d.capacity; - uint32_t GEN = (MAX_GEN + j / d.capacity) % NUM_GEN; + uint32_t GEN = (MAX_GEN + j / d.capacity) % NUM_GEN; - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); - ASSERT_EQ(INDEX, index); - ASSERT_EQ(GEN, gen); + REQUIRE(INDEX == index); + REQUIRE(GEN == gen); manager.commitPushIndex(gen, index); - ASSERT_EQ(j - d.popIndex + 1, manager.size()); + REQUIRE(j - d.popIndex + 1 == manager.size()); } // Pop elements until the queue is empty. - for(size_t j = d.popIndex; j < d.pushIndex; ++j) + for (size_t j = d.popIndex; j < d.pushIndex; ++j) { uint32_t INDEX = j % d.capacity; - uint32_t GEN = (MAX_GEN + j / d.capacity) % NUM_GEN; + uint32_t GEN = (MAX_GEN + j / d.capacity) % NUM_GEN; - ASSERT_EQ(QueueReturn::Success, manager.reservePopIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePopIndex(gen, index)); - ASSERT_EQ(INDEX, index); - ASSERT_EQ(GEN, gen); + REQUIRE(INDEX == index); + REQUIRE(GEN == gen); manager.commitPopIndex(gen, index); - ASSERT_EQ(d.pushIndex - j - 1, manager.size()); + REQUIRE(d.pushIndex - j - 1 == manager.size()); } } -CombinedIndexData PopAtMaxData[] = - { // Capacity 2 queues for a couple generations - {2, 1, 0}, - {2, 2, 0}, - {2, 2, 1}, - {2, 2, 2}, - {2, 3, 1}, - {2, 3, 2}, - {2, 3, 3}, - {2, 4, 2}, - {2, 4, 3}, - {2, 4, 4}, - - // Capacity 3 queues for a couple generations - {3, 2, 0}, - {3, 3, 0}, - {3, 3, 1}, - {3, 3, 2}, - {3, 3, 3}, - {3, 4, 1}, - {3, 4, 2}, - {3, 4, 3}, - {3, 4, 4}, - {3, 5, 2}, - {3, 5, 3}, - {3, 5, 4}, - {3, 5, 5}, - - // Capacity 7 queue - {7, 6, 0}, - {7, 7, 0}, - {7, 7, 6}, - {7, 13, 7}, - {7, 14, 7}}; - -INSTANTIATE_TEST_SUITE_P(TestQueueManagerMaxCombinedIndex, PopAtMax, - ::testing::ValuesIn(PopAtMaxData)); - -class ReservePop : public ::testing::TestWithParam< CombinedIndexData > -{ -}; - -TEST_P(ReservePop, ReservePopIndexForClear) -{ - const auto& d = GetParam(); - - QueueManager manager(d.capacity); - const uint32_t NUM_GEN = QueueManager::numGenerations(d.capacity); - const uint32_t MAX_GEN = NUM_GEN - 1; - - adjustGeneration(manager, MAX_GEN); - - generation(manager, d.pushIndex, d.popIndex); - - // Pop elements until the queue is empty - - uint32_t endGeneration = 0; - uint32_t endIndex = 0; - uint32_t gen = 0; - uint32_t index = 0; - - ASSERT_EQ(QueueReturn::Success, - manager.reservePushIndex(endGeneration, endIndex)); - - for(uint32_t j = d.popIndex; j < d.pushIndex; ++j) - { - uint32_t INDEX = j % d.capacity; - uint32_t GEN = (MAX_GEN + j / d.capacity) % NUM_GEN; - - ASSERT_TRUE( - manager.reservePopForClear(gen, index, endGeneration, endIndex)); - - ASSERT_EQ(INDEX, index); - ASSERT_EQ(GEN, gen); - manager.commitPopIndex(gen, index); - } - - ASSERT_FALSE(manager.reservePopForClear(gen, index, endGeneration, endIndex)); - manager.abortPushIndexReservation(endGeneration, endIndex); - ASSERT_EQ(0u, manager.size()); -} - -CombinedIndexData ReservePopIndexForClearData[] = { +std::vector ReservePopIndexForClearData{ // Capacity 2 queues for a couple generations {2, 1, 0}, {2, 2, 1}, @@ -631,8 +557,43 @@ CombinedIndexData ReservePopIndexForClearData[] = { {7, 13, 7}, }; -INSTANTIATE_TEST_SUITE_P(TestQueueManagerMaxCombinedIndex, ReservePop, - ::testing::ValuesIn(ReservePopIndexForClearData)); +TEST_CASE("Reserve pop index for clear") +{ + const auto& d = GENERATE(from_range(ReservePopIndexForClearData)); + + QueueManager manager(d.capacity); + const uint32_t NUM_GEN = QueueManager::numGenerations(d.capacity); + const uint32_t MAX_GEN = NUM_GEN - 1; + + adjustGeneration(manager, MAX_GEN); + + generation(manager, d.pushIndex, d.popIndex); + + // Pop elements until the queue is empty + + uint32_t endGeneration = 0; + uint32_t endIndex = 0; + uint32_t gen = 0; + uint32_t index = 0; + + REQUIRE(QueueReturn::Success == manager.reservePushIndex(endGeneration, endIndex)); + + for (uint32_t j = d.popIndex; j < d.pushIndex; ++j) + { + uint32_t INDEX = j % d.capacity; + uint32_t GEN = (MAX_GEN + j / d.capacity) % NUM_GEN; + + REQUIRE(manager.reservePopForClear(gen, index, endGeneration, endIndex)); + + REQUIRE(INDEX == index); + REQUIRE(GEN == gen); + manager.commitPopIndex(gen, index); + } + + REQUIRE_FALSE(manager.reservePopForClear(gen, index, endGeneration, endIndex)); + manager.abortPushIndexReservation(endGeneration, endIndex); + REQUIRE(0u == manager.size()); +} struct CircularDifferenceData { @@ -646,30 +607,15 @@ std::ostream& operator<<(std::ostream& os, CircularDifferenceData d) { os << "[ minuend = " << d.minuend << " subtrahend = " << d.subtrahend - << " maxSize = " << d.maxSize << " expectedValue = " << d.expectedValue - << " ]"; + << " maxSize = " << d.maxSize << " expectedValue = " << d.expectedValue << " ]"; return os; } -class CircularDifference - : public ::testing::TestWithParam< CircularDifferenceData > -{ -}; - -TEST_P(CircularDifference, difference) -{ - const auto& data = GetParam(); - - ASSERT_EQ(data.expectedValue, - QueueManager::circularDifference(data.minuend, data.subtrahend, - data.maxSize)); -} - -constexpr uint32_t OUR_INT32_MAX = std::numeric_limits< int32_t >::max(); -constexpr uint32_t OUR_INT32_MAX_1 = OUR_INT32_MAX + 1; +constexpr uint32_t OUR_INT32_MAX = std::numeric_limits::max(); +constexpr uint32_t OUR_INT32_MAX_1 = OUR_INT32_MAX + 1; constexpr int32_t OUR_INT32_MAX_DIV = OUR_INT32_MAX_1 / 2; -CircularDifferenceData circularDifferenceData[] = { +std::vector circularDifferenceData{ // capacity 1 {0, 0, 1, 0}, @@ -715,62 +661,53 @@ CircularDifferenceData circularDifferenceData[] = { {0, 180, 360, -180}, }; -INSTANTIATE_TEST_SUITE_P(TestQueueManagerMaxCombinedIndex, CircularDifference, - ::testing::ValuesIn(circularDifferenceData)); - -class NumGenerations : public ::testing::TestWithParam< uint32_t > +TEST_CASE("Circular difference") { -}; + const auto& data = GENERATE(from_range(circularDifferenceData)); -TEST_P(NumGenerations, generations) -{ - uint32_t capacity = GetParam(); - uint32_t numGen = QueueManager::numGenerations(capacity); - - static const uint32_t MAX_ELEMENT_STATE_GEN = - std::numeric_limits< uint32_t >::max() >> 2; - - static const uint32_t MAX_COMBINED_INDEX = - std::numeric_limits< uint32_t >::max() >> 1; - - ASSERT_GE(numGen, 2u); - ASSERT_TRUE(MAX_ELEMENT_STATE_GEN == numGen - 1 - || ((numGen * capacity - 1 <= MAX_COMBINED_INDEX) - && ((numGen + 1) * capacity - 1 > MAX_COMBINED_INDEX))); + REQUIRE( + data.expectedValue + == QueueManager::circularDifference(data.minuend, data.subtrahend, data.maxSize)); } -uint32_t GenerationData[] = {1, - 2, - 3, - 4, - 15, - 16, - 17, - QueueManager::MAX_CAPACITY - 1, - QueueManager::MAX_CAPACITY}; +std::vector GenerationData{ + 1, 2, 3, 4, 15, 16, 17, QueueManager::MAX_CAPACITY - 1, QueueManager::MAX_CAPACITY}; -INSTANTIATE_TEST_SUITE_P(TestQueueManagerMaxCombinedIndex, NumGenerations, - ::testing::ValuesIn(GenerationData)); - -TEST(TestQueueManager, abortPushIndexReservation) +TEST_CASE("Num generations") { - uint32_t genA = 0; - uint32_t genB = 0; + uint32_t capacity = GENERATE(from_range(GenerationData)); + uint32_t numGen = QueueManager::numGenerations(capacity); + + static const uint32_t MAX_ELEMENT_STATE_GEN = std::numeric_limits::max() >> 2; + + static const uint32_t MAX_COMBINED_INDEX = std::numeric_limits::max() >> 1; + + REQUIRE(numGen >= 2u); + REQUIRE( + ((MAX_ELEMENT_STATE_GEN == numGen - 1) + || ((numGen * capacity - 1 <= MAX_COMBINED_INDEX) + && ((numGen + 1) * capacity - 1 > MAX_COMBINED_INDEX)))); +} + +TEST_CASE("Abort push index reservation") +{ + uint32_t genA = 0; + uint32_t genB = 0; uint32_t indexA = 0; uint32_t indexB = 0; QueueManager manager(1); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(genA, indexA)); - ASSERT_NE(QueueReturn::Success, manager.reservePushIndex(genA, indexA)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(genA, indexA)); + REQUIRE(QueueReturn::Success != manager.reservePushIndex(genA, indexA)); manager.abortPushIndexReservation(genA, indexA); - ASSERT_EQ(0u, manager.size()); + REQUIRE(0u == manager.size()); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(genB, indexB)); - ASSERT_EQ(genA + 1, genB); - ASSERT_EQ(indexA, indexB); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(genB, indexB)); + REQUIRE(genA + 1 == genB); + REQUIRE(indexA == indexB); } struct AbortData @@ -785,66 +722,11 @@ std::ostream& operator<<(std::ostream& os, AbortData d) { os << "[ capacity = " << d.capacity << " pushIndex = " << d.pushIndex - << " popIndex = " << d.popIndex << " expectedClears = " << d.expectedClears - << " ]"; + << " popIndex = " << d.popIndex << " expectedClears = " << d.expectedClears << " ]"; return os; } -class AbortPush : public ::testing::TestWithParam< AbortData > -{ -}; - -TEST_P(AbortPush, abortPush) -{ - const auto& data = GetParam(); - - QueueManager manager(data.capacity); - - generation(manager, data.pushIndex, data.popIndex); - - const uint32_t END_GENERATION = data.pushIndex / data.capacity; - const uint32_t END_INDEX = data.pushIndex % data.capacity; - - uint32_t gen = 0; - uint32_t index = 0; - - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); - ASSERT_EQ(END_GENERATION, gen); - ASSERT_EQ(END_INDEX, index); - - for(uint32_t i = 0; i < data.expectedClears; ++i) - { - ASSERT_TRUE( - manager.reservePopForClear(gen, index, END_GENERATION, END_INDEX)); - - ASSERT_EQ((data.popIndex + i) / data.capacity, gen); - ASSERT_EQ((data.popIndex + i) % data.capacity, index); - - manager.commitPopIndex(gen, index); - } - - ASSERT_FALSE( - manager.reservePopForClear(gen, index, END_GENERATION, END_INDEX)); - - manager.abortPushIndexReservation(END_GENERATION, END_INDEX); - - // Verify the queue is now empty, and the current push index has changed - - ASSERT_EQ(0u, manager.size()); - for(uint32_t i = 0; i < data.capacity; ++i) - { - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); - ASSERT_EQ(i + 1, manager.size()); - - ASSERT_EQ(END_GENERATION * data.capacity + END_INDEX + i + 1, - gen * data.capacity + index); - } - - ASSERT_NE(QueueReturn::Success, manager.reservePushIndex(gen, index)); - ASSERT_EQ(data.capacity, manager.size()); -} - -AbortData abortData[] = { +std::vector abortData{ {1, 0, 0, 0}, // Capacity 2 queues for a couple generations @@ -879,8 +761,52 @@ AbortData abortData[] = { {7, 24, 18, 6}, }; -INSTANTIATE_TEST_SUITE_P(TestQueueManagerMaxCombinedIndex, AbortPush, - ::testing::ValuesIn(abortData)); +TEST_CASE("Abort push") +{ + const auto& data = GENERATE(from_range(abortData)); + + QueueManager manager(data.capacity); + + generation(manager, data.pushIndex, data.popIndex); + + const uint32_t END_GENERATION = data.pushIndex / data.capacity; + const uint32_t END_INDEX = data.pushIndex % data.capacity; + + uint32_t gen = 0; + uint32_t index = 0; + + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); + REQUIRE(END_GENERATION == gen); + REQUIRE(END_INDEX == index); + + for (uint32_t i = 0; i < data.expectedClears; ++i) + { + REQUIRE(manager.reservePopForClear(gen, index, END_GENERATION, END_INDEX)); + + REQUIRE((data.popIndex + i) / data.capacity == gen); + REQUIRE((data.popIndex + i) % data.capacity == index); + + manager.commitPopIndex(gen, index); + } + + REQUIRE_FALSE(manager.reservePopForClear(gen, index, END_GENERATION, END_INDEX)); + + manager.abortPushIndexReservation(END_GENERATION, END_INDEX); + + // Verify the queue is now empty, and the current push index has changed + + REQUIRE(0u == manager.size()); + for (uint32_t i = 0; i < data.capacity; ++i) + { + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); + REQUIRE(i + 1 == manager.size()); + + REQUIRE(END_GENERATION * data.capacity + END_INDEX + i + 1 == gen * data.capacity + index); + } + + REQUIRE(QueueReturn::Success != manager.reservePushIndex(gen, index)); + REQUIRE(data.capacity == manager.size()); +} // Testing reservePopForClear // - Failure is returned when the head of the queue is the same as the given end @@ -889,85 +815,85 @@ INSTANTIATE_TEST_SUITE_P(TestQueueManagerMaxCombinedIndex, AbortPush, // not the given end generation and index // - We do not clear an index reserved for popping -TEST(TestQueueManagerReserve, Capacity1) +TEST_CASE("Capacity 1") { // It is not possible to clear a pop index when the capacity is 1. - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; // Random values to verify we didn't change them. - uint32_t resultGen = 1024; + uint32_t resultGen = 1024; uint32_t resultIndex = 1023; QueueManager manager(1); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); - ASSERT_FALSE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); + REQUIRE_FALSE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); - ASSERT_EQ(1024u, resultGen); - ASSERT_EQ(1023u, resultIndex); + REQUIRE(1024u == resultGen); + REQUIRE(1023u == resultIndex); - ASSERT_EQ(1u, manager.size()); + REQUIRE(1u == manager.size()); } -TEST(TestQueueManagerReserve, Capacity2) +TEST_CASE("Capacity 2") { - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; // Random values to verify we didn't change them. - uint32_t resultGen = 1024; + uint32_t resultGen = 1024; uint32_t resultIndex = 1023; QueueManager manager(2); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); - ASSERT_FALSE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); + REQUIRE_FALSE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); - ASSERT_EQ(1024u, resultGen); - ASSERT_EQ(1023u, resultIndex); + REQUIRE(1024u == resultGen); + REQUIRE(1023u == resultIndex); manager.commitPushIndex(gen, index); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); - ASSERT_TRUE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); - ASSERT_EQ(0u, resultGen); - ASSERT_EQ(0u, resultIndex); + REQUIRE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); + REQUIRE(0u == resultGen); + REQUIRE(0u == resultIndex); manager.commitPopIndex(resultGen, resultIndex); manager.commitPushIndex(gen, index); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); - ASSERT_TRUE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); - ASSERT_EQ(0u, resultGen); - ASSERT_EQ(1u, resultIndex); + REQUIRE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); + REQUIRE(0u == resultGen); + REQUIRE(1u == resultIndex); manager.commitPopIndex(resultGen, resultIndex); manager.commitPushIndex(gen, index); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); - ASSERT_TRUE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); - ASSERT_EQ(1u, resultGen); - ASSERT_EQ(0u, resultIndex); + REQUIRE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); + REQUIRE(1u == resultGen); + REQUIRE(0u == resultIndex); manager.commitPopIndex(resultGen, resultIndex); manager.commitPushIndex(gen, index); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); - ASSERT_TRUE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); - ASSERT_EQ(1u, resultGen); - ASSERT_EQ(1u, resultIndex); + REQUIRE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); + REQUIRE(1u == resultGen); + REQUIRE(1u == resultIndex); manager.commitPopIndex(resultGen, resultIndex); manager.commitPushIndex(gen, index); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); - ASSERT_TRUE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); - ASSERT_EQ(2u, resultGen); - ASSERT_EQ(0u, resultIndex); + REQUIRE(manager.reservePopForClear(resultGen, resultIndex, gen, index)); + REQUIRE(2u == resultGen); + REQUIRE(0u == resultIndex); manager.commitPopIndex(resultGen, resultIndex); manager.commitPushIndex(gen, index); } @@ -984,43 +910,11 @@ std::ostream& operator<<(std::ostream& os, ReserveData d) { os << "[ capacity = " << d.capacity << " pushIndex = " << d.pushIndex - << " popIndex = " << d.popIndex << " expectedClears = " << d.expectedClears - << " ]"; + << " popIndex = " << d.popIndex << " expectedClears = " << d.expectedClears << " ]"; return os; } -class Reserve : public ::testing::TestWithParam< ReserveData > -{ -}; -TEST_P(Reserve, clear) -{ - const auto& data = GetParam(); - QueueManager manager(data.capacity); - - generation(manager, data.pushIndex, data.popIndex); - - const uint32_t endGen = data.pushIndex / data.capacity; - const uint32_t endIdx = data.pushIndex % data.capacity; - - uint32_t gen = 0; - uint32_t index = 0; - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); - ASSERT_EQ(endGen, gen); - ASSERT_EQ(endIdx, index); - - for(unsigned int j = 0; j < data.expectedClears; ++j) - { - ASSERT_TRUE(manager.reservePopForClear(gen, index, endGen, endIdx)); - ASSERT_EQ((data.popIndex + j) / data.capacity, gen); - ASSERT_EQ((data.popIndex + j) % data.capacity, index); - manager.commitPopIndex(gen, index); - } - ASSERT_FALSE(manager.reservePopForClear(gen, index, endGen, endIdx)); - manager.commitPushIndex(endGen, endIdx); - ASSERT_EQ(1u, manager.size()); -} - -ReserveData reserveData[] = { +std::vector reserveData{ {1, 0, 0, 0}, // Capacity 2 queues for a couple generations @@ -1055,72 +949,97 @@ ReserveData reserveData[] = { {7, 24, 18, 6}, }; -INSTANTIATE_TEST_SUITE_P(TestQueueManagerReserve, Reserve, - ::testing::ValuesIn(reserveData)); +TEST_CASE("Reserve, clear") +{ + const auto& data = GENERATE(from_range(reserveData)); + QueueManager manager(data.capacity); -TEST(TestQueueManager, Enabled) + generation(manager, data.pushIndex, data.popIndex); + + const uint32_t endGen = data.pushIndex / data.capacity; + const uint32_t endIdx = data.pushIndex % data.capacity; + + uint32_t gen = 0; + uint32_t index = 0; + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); + REQUIRE(endGen == gen); + REQUIRE(endIdx == index); + + for (unsigned int j = 0; j < data.expectedClears; ++j) + { + REQUIRE(manager.reservePopForClear(gen, index, endGen, endIdx)); + REQUIRE((data.popIndex + j) / data.capacity == gen); + REQUIRE((data.popIndex + j) % data.capacity == index); + manager.commitPopIndex(gen, index); + } + REQUIRE_FALSE(manager.reservePopForClear(gen, index, endGen, endIdx)); + manager.commitPushIndex(endGen, endIdx); + REQUIRE(1u == manager.size()); +} + +TEST_CASE("Enabled") { QueueManager manager(3); - ASSERT_TRUE(manager.enabled()); + REQUIRE(manager.enabled()); - uint32_t gen = 0; + uint32_t gen = 0; uint32_t index = 0; // Insert 2 elements. - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); manager.commitPushIndex(gen, index); - ASSERT_EQ(1u, manager.size()); + REQUIRE(1u == manager.size()); - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); manager.commitPushIndex(gen, index); - ASSERT_EQ(2u, manager.size()); + REQUIRE(2u == manager.size()); // Disable the queue. manager.disable(); - ASSERT_FALSE(manager.enabled()); + REQUIRE_FALSE(manager.enabled()); // Test that attempting to push fails. - ASSERT_EQ(QueueReturn::QueueDisabled, manager.reservePushIndex(gen, index)); - ASSERT_EQ(2u, manager.size()); + REQUIRE(QueueReturn::QueueDisabled == manager.reservePushIndex(gen, index)); + REQUIRE(2u == manager.size()); // Test that attempting to pop succeeds. - ASSERT_EQ(QueueReturn::Success, manager.reservePopIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePopIndex(gen, index)); manager.commitPopIndex(gen, index); - ASSERT_EQ(1u, manager.size()); + REQUIRE(1u == manager.size()); // Test that attempting to push still fails. - ASSERT_EQ(QueueReturn::QueueDisabled, manager.reservePushIndex(gen, index)); - ASSERT_EQ(1u, manager.size()); + REQUIRE(QueueReturn::QueueDisabled == manager.reservePushIndex(gen, index)); + REQUIRE(1u == manager.size()); // Disable the queue a second time, and verify that has no effect. manager.disable(); - ASSERT_FALSE(manager.enabled()); + REQUIRE_FALSE(manager.enabled()); // Test that attempting to push still fails. - ASSERT_EQ(QueueReturn::QueueDisabled, manager.reservePushIndex(gen, index)); - ASSERT_EQ(1u, manager.size()); + REQUIRE(QueueReturn::QueueDisabled == manager.reservePushIndex(gen, index)); + REQUIRE(1u == manager.size()); // Enable the queue. manager.enable(); - ASSERT_TRUE(manager.enabled()); + REQUIRE(manager.enabled()); // Test that attempting to push succeeds. - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); manager.commitPushIndex(gen, index); - ASSERT_EQ(2u, manager.size()); + REQUIRE(2u == manager.size()); // Test that attempting to pop succeeds. - ASSERT_EQ(QueueReturn::Success, manager.reservePopIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePopIndex(gen, index)); manager.commitPopIndex(gen, index); - ASSERT_EQ(1u, manager.size()); + REQUIRE(1u == manager.size()); // Enable the queue a second time, and verify that has no effect. manager.enable(); - ASSERT_TRUE(manager.enabled()); + REQUIRE(manager.enabled()); // Test that attempting to push succeeds. - ASSERT_EQ(QueueReturn::Success, manager.reservePushIndex(gen, index)); + REQUIRE(QueueReturn::Success == manager.reservePushIndex(gen, index)); manager.commitPushIndex(gen, index); - ASSERT_EQ(2u, manager.size()); + REQUIRE(2u == manager.size()); } diff --git a/win32-setup/lokinet-win32.iss b/win32-setup/lokinet-win32.iss index b9c9f6042..9b0310abd 100644 --- a/win32-setup/lokinet-win32.iss +++ b/win32-setup/lokinet-win32.iss @@ -90,7 +90,6 @@ Source: "{#DevPath}ui-win32\bin\release\lokinetui.exe.config"; DestDir: "{app}"; Source: "{#DevPath}ui-win32\bin\release\lokinetui.pdb"; DestDir: "{app}"; Flags: ignoreversion #endif ; eh, might as well ship the 32-bit port of everything else -;Source: "{#DevPath}build\testAll.exe"; DestDir: "{app}"; Flags: ignoreversion ;Source: "{#DevPath}build\catchAll.exe"; DestDir: "{app}"; Flags: ignoreversion Source: "{#DevPath}build\lokinetctl.exe"; DestDir: "{app}"; Flags: ignoreversion Source: "LICENSE"; DestDir: "{app}"; Flags: ignoreversion