diff options
Diffstat (limited to 'tests')
-rw-r--r-- | tests/CMakeLists.txt | 1 | ||||
-rw-r--r-- | tests/performance_tests/construct_tx.h | 2 | ||||
-rw-r--r-- | tests/performance_tests/derive_secret_key.h | 2 | ||||
-rw-r--r-- | tests/unit_tests/CMakeLists.txt | 2 | ||||
-rw-r--r-- | tests/unit_tests/ban.cpp | 2 | ||||
-rw-r--r-- | tests/unit_tests/epee_utils.cpp | 231 | ||||
-rw-r--r-- | tests/unit_tests/main.cpp | 20 | ||||
-rw-r--r-- | tests/unit_tests/serialization.cpp | 17 | ||||
-rw-r--r-- | tests/unit_tests/test_peerlist.cpp | 2 | ||||
-rw-r--r-- | tests/unit_tests/unit_tests_utils.h | 3 |
10 files changed, 268 insertions, 14 deletions
diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 85763f8b5..a5f5335db 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -30,6 +30,7 @@ # The docs say this only affects grouping in IDEs set(folder "tests") +set(TEST_DATA_DIR "${CMAKE_CURRENT_LIST_DIR}/data") if (WIN32 AND STATIC) add_definitions(-DSTATICLIB) diff --git a/tests/performance_tests/construct_tx.h b/tests/performance_tests/construct_tx.h index 0610c86ed..90e4cf76e 100644 --- a/tests/performance_tests/construct_tx.h +++ b/tests/performance_tests/construct_tx.h @@ -43,7 +43,7 @@ class test_construct_tx : private multi_tx_test_base<a_in_count> static_assert(0 < a_out_count, "out_count must be greater than 0"); public: - static const size_t loop_count = (a_in_count + a_out_count < 100) ? 100 : 10; + static const size_t loop_count = (a_in_count + a_out_count < 10) ? (a_rct ? 10 : 200) : (a_in_count + a_out_count) < 100 ? (a_rct ? 5 : 25) : 5; static const size_t in_count = a_in_count; static const size_t out_count = a_out_count; static const bool rct = a_rct; diff --git a/tests/performance_tests/derive_secret_key.h b/tests/performance_tests/derive_secret_key.h index 1a2945a77..f97deb974 100644 --- a/tests/performance_tests/derive_secret_key.h +++ b/tests/performance_tests/derive_secret_key.h @@ -38,7 +38,7 @@ class test_derive_secret_key : public single_tx_test_base { public: - static const size_t loop_count = 1000000; + static const size_t loop_count = 10000; bool init() { diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 53d93fcce..1dac92bed 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -100,4 +100,4 @@ endif () add_test( NAME unit_tests - COMMAND unit_tests) + COMMAND unit_tests "${TEST_DATA_DIR}") diff --git a/tests/unit_tests/ban.cpp b/tests/unit_tests/ban.cpp index b8d57452e..d15716a42 100644 --- a/tests/unit_tests/ban.cpp +++ b/tests/unit_tests/ban.cpp @@ -33,7 +33,7 @@ #include "p2p/net_node.h" #include "cryptonote_protocol/cryptonote_protocol_handler.h" -#define MAKE_IPV4_ADDRESS(a,b,c,d) new epee::net_utils::ipv4_network_address(MAKE_IP(a,b,c,d),0) +#define MAKE_IPV4_ADDRESS(a,b,c,d) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),0} namespace cryptonote { class blockchain_storage; diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp index e8ddbe3f5..f6cb0c163 100644 --- a/tests/unit_tests/epee_utils.cpp +++ b/tests/unit_tests/epee_utils.cpp @@ -27,6 +27,7 @@ // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. #include <array> +#include <boost/endian/conversion.hpp> #include <boost/range/algorithm/equal.hpp> #include <boost/range/algorithm_ext/iota.hpp> #include <cstdint> @@ -42,7 +43,11 @@ # include <arpa/inet.h> #endif +#include "boost/archive/portable_binary_iarchive.hpp" +#include "boost/archive/portable_binary_oarchive.hpp" #include "hex.h" +#include "net/net_utils_base.h" +#include "p2p/net_peerlist_boost_serialization.h" #include "span.h" #include "string_tools.h" @@ -100,6 +105,40 @@ namespace boost::range::iota(out, 0); return out; } + + #define CHECK_EQUAL(lhs, rhs) \ + EXPECT_TRUE( lhs == rhs ); \ + EXPECT_TRUE( rhs == lhs ); \ + EXPECT_FALSE( lhs != rhs ); \ + EXPECT_FALSE( rhs != lhs ); \ + EXPECT_FALSE( lhs < rhs ); \ + EXPECT_FALSE( rhs < lhs ); \ + EXPECT_TRUE( lhs <= rhs ); \ + EXPECT_TRUE( rhs <= lhs ); \ + EXPECT_FALSE( lhs > rhs ); \ + EXPECT_FALSE( rhs > lhs ); \ + EXPECT_TRUE( lhs >= rhs ); \ + EXPECT_TRUE( rhs >= lhs ) + + #define CHECK_LESS(lhs, rhs) \ + EXPECT_FALSE( lhs == rhs ); \ + EXPECT_FALSE( rhs == lhs ); \ + EXPECT_TRUE( lhs != rhs ); \ + EXPECT_TRUE( rhs != lhs ); \ + EXPECT_TRUE( lhs < rhs ); \ + EXPECT_FALSE( rhs < lhs ); \ + EXPECT_TRUE( lhs <= rhs ); \ + EXPECT_FALSE( rhs <= lhs ); \ + EXPECT_FALSE( lhs > rhs ); \ + EXPECT_TRUE( rhs > lhs ); \ + EXPECT_FALSE( lhs >= rhs ); \ + EXPECT_TRUE( rhs >= lhs ) + + #ifdef BOOST_LITTLE_ENDIAN + #define CHECK_LESS_ENDIAN(lhs, rhs) CHECK_LESS( rhs , lhs ) + #else + #define CHECK_LESS_ENDIAN(lhs, rhs) CHECK_LESS( lhs , rhs ) + #endif } TEST(Span, Traits) @@ -419,3 +458,195 @@ TEST(StringTools, GetIpInt32) EXPECT_EQ(htonl(0xff0aff00), ip); } +TEST(NetUtils, IPv4NetworkAddress) +{ + const auto ip1 = boost::endian::native_to_big(0x330012FFu); + const auto ip_loopback = boost::endian::native_to_big(0x7F000001u); + const auto ip_local = boost::endian::native_to_big(0x0A000000u); + + epee::net_utils::ipv4_network_address address1{ip1, 65535}; + CHECK_EQUAL(address1, address1); + EXPECT_STREQ("51.0.18.255:65535", address1.str().c_str()); + EXPECT_STREQ("51.0.18.255", address1.host_str().c_str()); + EXPECT_FALSE(address1.is_loopback()); + EXPECT_FALSE(address1.is_local()); + EXPECT_EQ(epee::net_utils::ipv4_network_address::ID, address1.get_type_id()); + EXPECT_EQ(ip1, address1.ip()); + EXPECT_EQ(65535, address1.port()); + EXPECT_TRUE(epee::net_utils::ipv4_network_address{std::move(address1)} == address1); + EXPECT_TRUE(epee::net_utils::ipv4_network_address{address1} == address1); + + const epee::net_utils::ipv4_network_address loopback{ip_loopback, 0}; + CHECK_EQUAL(loopback, loopback); + CHECK_LESS_ENDIAN(address1, loopback); + EXPECT_STREQ("127.0.0.1:0", loopback.str().c_str()); + EXPECT_STREQ("127.0.0.1", loopback.host_str().c_str()); + EXPECT_TRUE(loopback.is_loopback()); + EXPECT_FALSE(loopback.is_local()); + EXPECT_EQ(epee::net_utils::ipv4_network_address::ID, address1.get_type_id()); + EXPECT_EQ(ip_loopback, loopback.ip()); + EXPECT_EQ(0, loopback.port()); + + const epee::net_utils::ipv4_network_address local{ip_local, 8080}; + CHECK_EQUAL(local, local); + CHECK_LESS(local, address1); + CHECK_LESS(local, loopback); + EXPECT_FALSE(local.is_loopback()); + EXPECT_TRUE(local.is_local()); + + epee::net_utils::ipv4_network_address address2{ip1, 55}; + CHECK_EQUAL(address2, address2); + CHECK_LESS_ENDIAN(address2, loopback); + CHECK_LESS(local, address2); + EXPECT_STREQ("51.0.18.255:55", address2.str().c_str()); + EXPECT_STREQ("51.0.18.255", address2.host_str().c_str()); + + + address2 = std::move(address1); + CHECK_EQUAL(address2, address1); + + address2 = local; + CHECK_EQUAL(address2, local); + CHECK_LESS(address2, address1); + + { + std::stringstream stream; + { + boost::archive::portable_binary_oarchive ostream{stream}; + ostream << address1; + } + { + boost::archive::portable_binary_iarchive istream{stream}; + istream >> address2; + } + } + CHECK_EQUAL(address1, address2); + EXPECT_EQ(ip1, address2.ip()); + EXPECT_EQ(65535, address2.port()); +} + +TEST(NetUtils, NetworkAddress) +{ + const auto ip1 = boost::endian::native_to_big(0x330012FFu); + const auto ip_loopback = boost::endian::native_to_big(0x7F000001u); + const auto ip_local = boost::endian::native_to_big(0x0A000000u); + + struct custom_address { + constexpr static bool equal(const custom_address&) noexcept { return false; } + constexpr static bool less(const custom_address&) noexcept { return false; } + constexpr static bool is_same_host(const custom_address&) noexcept { return false; } + constexpr static bool is_loopback() noexcept { return false; } + constexpr static bool is_local() noexcept { return false; } + static std::string str() { return {}; } + static std::string host_str() { return {}; } + constexpr static uint8_t get_type_id() noexcept { return uint8_t(-1); } + }; + + const epee::net_utils::network_address empty; + CHECK_EQUAL(empty, empty); + EXPECT_TRUE(empty.is_same_host(empty)); + EXPECT_STREQ("<none>", empty.str().c_str()); + EXPECT_STREQ("<none>", empty.host_str().c_str()); + EXPECT_FALSE(empty.is_loopback()); + EXPECT_FALSE(empty.is_local()); + EXPECT_EQ(0, empty.get_type_id()); + EXPECT_THROW(empty.as<custom_address>(), std::bad_cast); + + epee::net_utils::network_address address1{ + epee::net_utils::ipv4_network_address{ip1, 65535} + }; + CHECK_EQUAL(address1, address1); + CHECK_EQUAL(epee::net_utils::network_address{address1}, address1); + CHECK_LESS(empty, address1); + EXPECT_TRUE(address1.is_same_host(address1)); + EXPECT_FALSE(empty.is_same_host(address1)); + EXPECT_FALSE(address1.is_same_host(empty)); + EXPECT_STREQ("51.0.18.255:65535", address1.str().c_str()); + EXPECT_STREQ("51.0.18.255", address1.host_str().c_str()); + EXPECT_FALSE(address1.is_loopback()); + EXPECT_FALSE(address1.is_local()); + EXPECT_EQ(epee::net_utils::ipv4_network_address::ID, address1.get_type_id()); + EXPECT_NO_THROW(address1.as<epee::net_utils::ipv4_network_address>()); + EXPECT_THROW(address1.as<custom_address>(), std::bad_cast); + + const epee::net_utils::network_address loopback{ + epee::net_utils::ipv4_network_address{ip_loopback, 0} + }; + CHECK_EQUAL(loopback, loopback); + CHECK_LESS(empty, loopback); + CHECK_LESS_ENDIAN(address1, loopback); + EXPECT_TRUE(loopback.is_same_host(loopback)); + EXPECT_FALSE(loopback.is_same_host(address1)); + EXPECT_FALSE(address1.is_same_host(loopback)); + EXPECT_STREQ("127.0.0.1:0", loopback.str().c_str()); + EXPECT_STREQ("127.0.0.1", loopback.host_str().c_str()); + EXPECT_TRUE(loopback.is_loopback()); + EXPECT_FALSE(loopback.is_local()); + EXPECT_EQ(epee::net_utils::ipv4_network_address::ID, address1.get_type_id()); + + const epee::net_utils::network_address local{ + epee::net_utils::ipv4_network_address{ip_local, 8080} + }; + CHECK_EQUAL(local, local); + CHECK_LESS(local, loopback); + CHECK_LESS(local, address1); + EXPECT_FALSE(local.is_loopback()); + EXPECT_TRUE(local.is_local()); + + epee::net_utils::network_address address2{ + epee::net_utils::ipv4_network_address{ip1, 55} + }; + CHECK_EQUAL(address2, address2); + CHECK_LESS(address2, address1); + CHECK_LESS(local, address2); + CHECK_LESS_ENDIAN(address2, loopback); + EXPECT_TRUE(address1.is_same_host(address2)); + EXPECT_TRUE(address2.is_same_host(address1)); + EXPECT_STREQ("51.0.18.255:55", address2.str().c_str()); + EXPECT_STREQ("51.0.18.255", address2.host_str().c_str()); + + address2 = std::move(address1); + CHECK_EQUAL(address1, address1); + CHECK_EQUAL(empty, address1); + CHECK_LESS(address1, address2); + EXPECT_FALSE(address1.is_same_host(address2)); + EXPECT_FALSE(address2.is_same_host(address1)); + EXPECT_STREQ("51.0.18.255:65535", address2.str().c_str()); + EXPECT_STREQ("51.0.18.255", address2.host_str().c_str()); + EXPECT_FALSE(address1.is_loopback()); + EXPECT_FALSE(address1.is_local()); + EXPECT_THROW(address1.as<epee::net_utils::ipv4_network_address>(), std::bad_cast); + EXPECT_NO_THROW(address2.as<epee::net_utils::ipv4_network_address>()); + + address2 = local; + CHECK_EQUAL(address2, local); + CHECK_LESS(address1, address2); + EXPECT_TRUE(address2.is_same_host(local)); + EXPECT_TRUE(local.is_same_host(address2)); + EXPECT_FALSE(address2.is_same_host(address1)); + EXPECT_FALSE(address1.is_same_host(address2)); + + { + std::stringstream stream; + { + boost::archive::portable_binary_oarchive ostream{stream}; + ostream << address2; + } + { + boost::archive::portable_binary_iarchive istream{stream}; + istream >> address1; + } + } + CHECK_EQUAL(address1, address2); + EXPECT_TRUE(address1.is_same_host(address2)); + EXPECT_TRUE(address2.is_same_host(address1)); + EXPECT_NO_THROW(address1.as<epee::net_utils::ipv4_network_address>()); + + address1 = custom_address{}; + CHECK_EQUAL(address1, address1); + CHECK_LESS(address2, address1); + EXPECT_FALSE(address1.is_same_host(loopback)); + EXPECT_FALSE(loopback.is_same_host(address1)); + EXPECT_THROW(address1.as<epee::net_utils::ipv4_network_address>(), std::bad_cast); + EXPECT_NO_THROW(address1.as<custom_address>()); +} diff --git a/tests/unit_tests/main.cpp b/tests/unit_tests/main.cpp index b470249a3..b2abad942 100644 --- a/tests/unit_tests/main.cpp +++ b/tests/unit_tests/main.cpp @@ -30,14 +30,32 @@ #include "gtest/gtest.h" +#include <boost/filesystem.hpp> + #include "include_base_utils.h" +#include "unit_tests_utils.h" + +boost::filesystem::path unit_test::data_dir; int main(int argc, char** argv) { - epee::string_tools::set_module_name_and_folder(argv[0]); mlog_configure(mlog_get_default_log_path("unit_tests.log"), true); epee::debug::get_set_enable_assert(true, false); ::testing::InitGoogleTest(&argc, argv); + + // Process remaining arguments + if (argc == 2 && argv[1] != NULL) { // one arg: path to dir with test data + unit_test::data_dir = argv[1]; + } else if (argc == 1) { // legacy: assume test binaries in 'build/release' + epee::string_tools::set_module_name_and_folder(argv[0]); + unit_test::data_dir = boost::filesystem::path(epee::string_tools::get_current_module_folder()) + .parent_path().parent_path().parent_path().parent_path() + .append("tests").append("data"); + } else { + std::cerr << "Usage: " << argv[0] << " [<path-to-test-data-dir>]" << std::endl; + return 1; + } + return RUN_ALL_TESTS(); } diff --git a/tests/unit_tests/serialization.cpp b/tests/unit_tests/serialization.cpp index 6f9fe7d11..e701a53ce 100644 --- a/tests/unit_tests/serialization.cpp +++ b/tests/unit_tests/serialization.cpp @@ -47,6 +47,7 @@ #include "serialization/binary_utils.h" #include "wallet/wallet2.h" #include "gtest/gtest.h" +#include "unit_tests_utils.h" using namespace std; struct Struct @@ -671,12 +672,12 @@ TEST(Serialization, portability_wallet) const bool testnet = true; const bool restricted = false; tools::wallet2 w(testnet, restricted); - string wallet_file = epee::string_tools::get_current_module_folder() + "/../../../../tests/data/wallet_9svHk1"; + const boost::filesystem::path wallet_file = unit_test::data_dir / "wallet_9svHk1"; string password = "test"; bool r = false; try { - w.load(wallet_file, password); + w.load(wallet_file.native(), password); r = true; } catch (const exception& e) @@ -791,9 +792,9 @@ TEST(Serialization, portability_wallet) TEST(Serialization, portability_outputs) { // read file - const std::string filename = epee::string_tools::get_current_module_folder() + "/../../../../tests/data/outputs"; + const boost::filesystem::path filename = unit_test::data_dir / "outputs"; std::string data; - bool r = epee::file_io_utils::load_file_to_string(filename, data); + bool r = epee::file_io_utils::load_file_to_string(filename.native(), data); ASSERT_TRUE(r); const size_t magiclen = strlen(OUTPUT_EXPORT_FILE_MAGIC); ASSERT_FALSE(data.size() < magiclen || memcmp(data.data(), OUTPUT_EXPORT_FILE_MAGIC, magiclen)); @@ -906,10 +907,10 @@ TEST(Serialization, portability_outputs) #define UNSIGNED_TX_PREFIX "Monero unsigned tx set\003" TEST(Serialization, portability_unsigned_tx) { - const string filename = epee::string_tools::get_current_module_folder() + "/../../../../tests/data/unsigned_monero_tx"; + const boost::filesystem::path filename = unit_test::data_dir / "unsigned_monero_tx"; std::string s; const bool testnet = true; - bool r = epee::file_io_utils::load_file_to_string(filename, s); + bool r = epee::file_io_utils::load_file_to_string(filename.native(), s); ASSERT_TRUE(r); const size_t magiclen = strlen(UNSIGNED_TX_PREFIX); ASSERT_FALSE(strncmp(s.c_str(), UNSIGNED_TX_PREFIX, magiclen)); @@ -1054,10 +1055,10 @@ TEST(Serialization, portability_unsigned_tx) #define SIGNED_TX_PREFIX "Monero signed tx set\003" TEST(Serialization, portability_signed_tx) { - const string filename = epee::string_tools::get_current_module_folder() + "/../../../../tests/data/signed_monero_tx"; + const boost::filesystem::path filename = unit_test::data_dir / "signed_monero_tx"; const bool testnet = true; std::string s; - bool r = epee::file_io_utils::load_file_to_string(filename, s); + bool r = epee::file_io_utils::load_file_to_string(filename.native(), s); ASSERT_TRUE(r); const size_t magiclen = strlen(SIGNED_TX_PREFIX); ASSERT_FALSE(strncmp(s.c_str(), SIGNED_TX_PREFIX, magiclen)); diff --git a/tests/unit_tests/test_peerlist.cpp b/tests/unit_tests/test_peerlist.cpp index 4a546b50d..849020d25 100644 --- a/tests/unit_tests/test_peerlist.cpp +++ b/tests/unit_tests/test_peerlist.cpp @@ -38,7 +38,7 @@ TEST(peer_list, peer_list_general) { nodetool::peerlist_manager plm; plm.init(false); -#define MAKE_IPV4_ADDRESS(a,b,c,d,e) new epee::net_utils::ipv4_network_address(MAKE_IP(a,b,c,d),e) +#define MAKE_IPV4_ADDRESS(a,b,c,d,e) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),e} #define ADD_GRAY_NODE(addr_, id_, last_seen_) { nodetool::peerlist_entry ple; ple.last_seen=last_seen_;ple.adr = addr_; ple.id = id_;plm.append_with_peer_gray(ple);} #define ADD_WHITE_NODE(addr_, id_, last_seen_) { nodetool::peerlist_entry ple;ple.last_seen=last_seen_; ple.adr = addr_; ple.id = id_;plm.append_with_peer_white(ple);} diff --git a/tests/unit_tests/unit_tests_utils.h b/tests/unit_tests/unit_tests_utils.h index 11230c0db..a07eaf02b 100644 --- a/tests/unit_tests/unit_tests_utils.h +++ b/tests/unit_tests/unit_tests_utils.h @@ -31,9 +31,12 @@ #pragma once #include <atomic> +#include <boost/filesystem.hpp> namespace unit_test { + extern boost::filesystem::path data_dir; + class call_counter { public: |