diff options
Diffstat (limited to 'tests/unit_tests')
-rw-r--r-- | tests/unit_tests/CMakeLists.txt | 5 | ||||
-rw-r--r-- | tests/unit_tests/bootstrap_node_selector.cpp | 172 | ||||
-rw-r--r-- | tests/unit_tests/epee_utils.cpp | 47 | ||||
-rw-r--r-- | tests/unit_tests/hmac_keccak.cpp | 2 | ||||
-rw-r--r-- | tests/unit_tests/keccak.cpp | 4 | ||||
-rw-r--r-- | tests/unit_tests/levin.cpp | 20 | ||||
-rw-r--r-- | tests/unit_tests/mul_div.cpp | 2 | ||||
-rw-r--r-- | tests/unit_tests/net.cpp | 427 | ||||
-rw-r--r-- | tests/unit_tests/node_server.cpp | 3 | ||||
-rw-r--r-- | tests/unit_tests/test_tx_utils.cpp | 2 | ||||
-rw-r--r-- | tests/unit_tests/wipeable_string.cpp | 1 | ||||
-rw-r--r-- | tests/unit_tests/zmq_rpc.cpp | 55 |
12 files changed, 697 insertions, 43 deletions
diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index cda25dfc9..9c24a5ec7 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -34,6 +34,7 @@ set(unit_tests_sources blockchain_db.cpp block_queue.cpp block_reward.cpp + bootstrap_node_selector.cpp bulletproofs.cpp canonical_amounts.cpp chacha.cpp @@ -93,7 +94,8 @@ set(unit_tests_sources wipeable_string.cpp is_hdd.cpp aligned.cpp - rpc_version_str.cpp) + rpc_version_str.cpp + zmq_rpc.cpp) set(unit_tests_headers unit_tests_utils.h) @@ -106,6 +108,7 @@ target_link_libraries(unit_tests ringct cryptonote_protocol cryptonote_core + daemon_messages blockchain_db lmdb_lib rpc diff --git a/tests/unit_tests/bootstrap_node_selector.cpp b/tests/unit_tests/bootstrap_node_selector.cpp new file mode 100644 index 000000000..c609d1223 --- /dev/null +++ b/tests/unit_tests/bootstrap_node_selector.cpp @@ -0,0 +1,172 @@ +// Copyright (c) 2020, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include <gtest/gtest.h> + +#include "rpc/bootstrap_node_selector.h" + +class bootstrap_node_selector : public ::testing::Test +{ +protected: + void SetUp() override + { + nodes.insert(white_nodes.begin(), white_nodes.end()); + nodes.insert(gray_nodes.begin(), gray_nodes.end()); + } + + const std::map<std::string, bool> white_nodes = { + { + "white_node_1:18089", true + }, + { + "white_node_2:18081", true + } + }; + const std::map<std::string, bool> gray_nodes = { + { + "gray_node_1:18081", false + }, + { + "gray_node_2:18089", false + } + }; + + std::map<std::string, bool> nodes; +}; + +TEST_F(bootstrap_node_selector, selector_auto_empty) +{ + cryptonote::bootstrap_node::selector_auto selector([]() { + return std::map<std::string, bool>(); + }); + + EXPECT_FALSE(selector.next_node()); +} + +TEST_F(bootstrap_node_selector, selector_auto_no_credentials) +{ + cryptonote::bootstrap_node::selector_auto selector([this]() { + return nodes; + }); + + for (size_t fails = 0; fails < nodes.size(); ++fails) + { + const auto current = selector.next_node(); + EXPECT_FALSE(current->credentials); + + selector.handle_result(current->address, false); + } +} + +TEST_F(bootstrap_node_selector, selector_auto_success) +{ + cryptonote::bootstrap_node::selector_auto selector([this]() { + return nodes; + }); + + auto current = selector.next_node(); + for (size_t fails = 0; fails < nodes.size(); ++fails) + { + selector.handle_result(current->address, true); + + current = selector.next_node(); + EXPECT_TRUE(white_nodes.count(current->address) > 0); + } +} + +TEST_F(bootstrap_node_selector, selector_auto_failure) +{ + cryptonote::bootstrap_node::selector_auto selector([this]() { + return nodes; + }); + + auto current = selector.next_node(); + for (size_t fails = 0; fails < nodes.size(); ++fails) + { + const auto previous = current; + + selector.handle_result(current->address, false); + + current = selector.next_node(); + EXPECT_NE(current->address, previous->address); + } +} + +TEST_F(bootstrap_node_selector, selector_auto_white_nodes_first) +{ + cryptonote::bootstrap_node::selector_auto selector([this]() { + return nodes; + }); + + for (size_t iterations = 0; iterations < 2; ++iterations) + { + for (size_t fails = 0; fails < white_nodes.size(); ++fails) + { + const auto current = selector.next_node(); + EXPECT_TRUE(white_nodes.count(current->address) > 0); + + selector.handle_result(current->address, false); + } + + for (size_t fails = 0; fails < gray_nodes.size(); ++fails) + { + const auto current = selector.next_node(); + EXPECT_TRUE(gray_nodes.count(current->address) > 0); + + selector.handle_result(current->address, false); + } + } +} + +TEST_F(bootstrap_node_selector, selector_auto_max_nodes) +{ + const size_t max_nodes = nodes.size() / 2; + + bool populated_once = false; + cryptonote::bootstrap_node::selector_auto selector([this, &populated_once]() { + if (!populated_once) + { + populated_once = true; + return nodes; + } + + return std::map<std::string, bool>(); + }, max_nodes); + + std::set<std::string> unique_nodes; + + for (size_t fails = 0; fails < nodes.size(); ++fails) + { + const auto current = selector.next_node(); + unique_nodes.insert(current->address); + + selector.handle_result(current->address, false); + } + + EXPECT_EQ(unique_nodes.size(), max_nodes); +} diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp index ee44ea2d5..42bbb26bb 100644 --- a/tests/unit_tests/epee_utils.cpp +++ b/tests/unit_tests/epee_utils.cpp @@ -45,6 +45,7 @@ #include "boost/archive/portable_binary_iarchive.hpp" #include "boost/archive/portable_binary_oarchive.hpp" #include "byte_slice.h" +#include "crypto/crypto.h" #include "hex.h" #include "net/net_utils_base.h" #include "net/local_ip.h" @@ -823,14 +824,14 @@ TEST(ToHex, String) } -TEST(FromHex, String) +TEST(HexLocale, String) { // the source data to encode and decode std::vector<uint8_t> source{{ 0x00, 0xFF, 0x0F, 0xF0 }}; // encode and decode the data auto hex = epee::to_hex::string({ source.data(), source.size() }); - auto decoded = epee::from_hex::vector(hex); + auto decoded = epee::from_hex_locale::to_vector(hex); // encoded should be twice the size and should decode to the exact same data EXPECT_EQ(source.size() * 2, hex.size()); @@ -839,7 +840,7 @@ TEST(FromHex, String) // we will now create a padded hex string, we want to explicitly allow // decoding it this way also, ignoring spaces and colons between the numbers hex.assign("00:ff 0f:f0"); - EXPECT_EQ(source, epee::from_hex::vector(hex)); + EXPECT_EQ(source, epee::from_hex_locale::to_vector(hex)); } TEST(ToHex, Array) @@ -901,6 +902,46 @@ TEST(ToHex, Formatted) EXPECT_EQ(expected, out.str()); } +TEST(FromHex, ToString) +{ + static constexpr const char hex[] = u8"deadbeeffY"; + static constexpr const char binary[] = { + char(0xde), char(0xad), char(0xbe), char(0xef), 0x00 + }; + + std::string out{}; + EXPECT_FALSE(epee::from_hex::to_string(out, hex)); + + boost::string_ref portion{hex}; + portion.remove_suffix(1); + EXPECT_FALSE(epee::from_hex::to_string(out, portion)); + + portion.remove_suffix(1); + EXPECT_TRUE(epee::from_hex::to_string(out, portion)); + EXPECT_EQ(std::string{binary}, out); +} + +TEST(FromHex, ToBuffer) +{ + static constexpr const char hex[] = u8"deadbeeffY"; + static constexpr const std::uint8_t binary[] = {0xde, 0xad, 0xbe, 0xef}; + + std::vector<std::uint8_t> out{}; + out.resize(sizeof(binary)); + EXPECT_FALSE(epee::from_hex::to_buffer(epee::to_mut_span(out), hex)); + + boost::string_ref portion{hex}; + portion.remove_suffix(1); + EXPECT_FALSE(epee::from_hex::to_buffer(epee::to_mut_span(out), portion)); + + portion.remove_suffix(1); + EXPECT_FALSE(epee::from_hex::to_buffer({out.data(), out.size() - 1}, portion)); + + EXPECT_TRUE(epee::from_hex::to_buffer(epee::to_mut_span(out), portion)); + const std::vector<std::uint8_t> expected{std::begin(binary), std::end(binary)}; + EXPECT_EQ(expected, out); +} + TEST(StringTools, BuffToHex) { const std::vector<unsigned char> all_bytes = get_all_bytes(); diff --git a/tests/unit_tests/hmac_keccak.cpp b/tests/unit_tests/hmac_keccak.cpp index 3898d4a7a..ec7c90dbe 100644 --- a/tests/unit_tests/hmac_keccak.cpp +++ b/tests/unit_tests/hmac_keccak.cpp @@ -123,7 +123,7 @@ static void test_keccak_hmac(const size_t * chunks) } -TEST(keccak_hmac, ) +TEST(keccak_hmac, nullptr) { test_keccak_hmac(nullptr); } diff --git a/tests/unit_tests/keccak.cpp b/tests/unit_tests/keccak.cpp index f4d41a8fa..a869fbaec 100644 --- a/tests/unit_tests/keccak.cpp +++ b/tests/unit_tests/keccak.cpp @@ -54,10 +54,6 @@ extern "C" { keccak_finish(&ctx, md1); \ ASSERT_EQ(memcmp(md0, md1, 32), 0); -TEST(keccak, ) -{ -} - TEST(keccak, 0_and_0) { static const size_t chunks[] = {0}; diff --git a/tests/unit_tests/levin.cpp b/tests/unit_tests/levin.cpp index 38707f075..720103e5a 100644 --- a/tests/unit_tests/levin.cpp +++ b/tests/unit_tests/levin.cpp @@ -462,8 +462,8 @@ TEST_F(levin_notify, fluff_without_padding) } std::vector<cryptonote::blobdata> txs(2); - txs[0].resize(100, 'e'); - txs[1].resize(200, 'f'); + txs[0].resize(100, 'f'); + txs[1].resize(200, 'e'); ASSERT_EQ(10u, contexts_.size()); { @@ -479,6 +479,7 @@ TEST_F(levin_notify, fluff_without_padding) for (++context; context != contexts_.end(); ++context) EXPECT_EQ(1u, context->process_send_queue()); + std::sort(txs.begin(), txs.end()); ASSERT_EQ(9u, receiver_.notified_size()); for (unsigned count = 0; count < 9; ++count) { @@ -510,8 +511,8 @@ TEST_F(levin_notify, fluff_with_padding) } std::vector<cryptonote::blobdata> txs(2); - txs[0].resize(100, 'e'); - txs[1].resize(200, 'f'); + txs[0].resize(100, 'f'); + txs[1].resize(200, 'e'); ASSERT_EQ(10u, contexts_.size()); { @@ -527,6 +528,7 @@ TEST_F(levin_notify, fluff_with_padding) for (++context; context != contexts_.end(); ++context) EXPECT_EQ(1u, context->process_send_queue()); + std::sort(txs.begin(), txs.end()); ASSERT_EQ(9u, receiver_.notified_size()); for (unsigned count = 0; count < 9; ++count) { @@ -558,8 +560,8 @@ TEST_F(levin_notify, private_fluff_without_padding) } std::vector<cryptonote::blobdata> txs(2); - txs[0].resize(100, 'e'); - txs[1].resize(200, 'f'); + txs[0].resize(100, 'f'); + txs[1].resize(200, 'e'); ASSERT_EQ(10u, contexts_.size()); { @@ -572,6 +574,7 @@ TEST_F(levin_notify, private_fluff_without_padding) io_service_.reset(); ASSERT_LT(0u, io_service_.poll()); + std::sort(txs.begin(), txs.end()); EXPECT_EQ(0u, context->process_send_queue()); for (++context; context != contexts_.end(); ++context) { @@ -610,8 +613,8 @@ TEST_F(levin_notify, private_fluff_with_padding) } std::vector<cryptonote::blobdata> txs(2); - txs[0].resize(100, 'e'); - txs[1].resize(200, 'f'); + txs[0].resize(100, 'f'); + txs[1].resize(200, 'e'); ASSERT_EQ(10u, contexts_.size()); { @@ -624,6 +627,7 @@ TEST_F(levin_notify, private_fluff_with_padding) io_service_.reset(); ASSERT_LT(0u, io_service_.poll()); + std::sort(txs.begin(), txs.end()); EXPECT_EQ(0u, context->process_send_queue()); for (++context; context != contexts_.end(); ++context) { diff --git a/tests/unit_tests/mul_div.cpp b/tests/unit_tests/mul_div.cpp index e3f7c34f3..c1a72ce51 100644 --- a/tests/unit_tests/mul_div.cpp +++ b/tests/unit_tests/mul_div.cpp @@ -82,7 +82,7 @@ namespace lo = mul128(0x1111111111111111, 0x1111111111111111, &hi); ASSERT_EQ(lo, 0x0fedcba987654321); - ASSERT_EQ(hi, 0x0123456789abcdf0);; + ASSERT_EQ(hi, 0x0123456789abcdf0); } TEST(mul128_with_carry_1_only, multiplies_correctly) diff --git a/tests/unit_tests/net.cpp b/tests/unit_tests/net.cpp index 221dc631d..36cb28ae0 100644 --- a/tests/unit_tests/net.cpp +++ b/tests/unit_tests/net.cpp @@ -53,8 +53,10 @@ #include <memory> #include <type_traits> +#include "crypto/crypto.h" #include "net/dandelionpp.h" #include "net/error.h" +#include "net/i2p_address.h" #include "net/net_utils_base.h" #include "net/socks.h" #include "net/socks_connect.h" @@ -177,22 +179,33 @@ TEST(tor_address, valid) EXPECT_FALSE(address2.less(*address1)); EXPECT_TRUE(address1->less(address2)); - address2 = MONERO_UNWRAP(net::tor_address::make(std::string{v3_onion} + ":", 65535)); - - EXPECT_EQ(65535, address2.port()); - EXPECT_STREQ(v3_onion, address2.host_str()); - EXPECT_EQ(std::string{v3_onion} + ":65535", address2.str().c_str()); - EXPECT_TRUE(address2.is_blockable()); - EXPECT_FALSE(address2.equal(*address1)); - EXPECT_FALSE(address1->equal(address2)); - EXPECT_FALSE(address2 == *address1); - EXPECT_FALSE(*address1 == address2); - EXPECT_TRUE(address2 != *address1); - EXPECT_TRUE(*address1 != address2); - EXPECT_TRUE(address2.is_same_host(*address1)); - EXPECT_TRUE(address1->is_same_host(address2)); - EXPECT_FALSE(address2.less(*address1)); - EXPECT_TRUE(address1->less(address2)); + net::tor_address address3 = MONERO_UNWRAP(net::tor_address::make(std::string{v3_onion} + ":", 65535)); + + EXPECT_EQ(65535, address3.port()); + EXPECT_STREQ(v3_onion, address3.host_str()); + EXPECT_EQ(std::string{v3_onion} + ":65535", address3.str().c_str()); + EXPECT_TRUE(address3.is_blockable()); + EXPECT_FALSE(address3.equal(*address1)); + EXPECT_FALSE(address1->equal(address3)); + EXPECT_FALSE(address3 == *address1); + EXPECT_FALSE(*address1 == address3); + EXPECT_TRUE(address3 != *address1); + EXPECT_TRUE(*address1 != address3); + EXPECT_TRUE(address3.is_same_host(*address1)); + EXPECT_TRUE(address1->is_same_host(address3)); + EXPECT_FALSE(address3.less(*address1)); + EXPECT_TRUE(address1->less(address3)); + + EXPECT_FALSE(address3.equal(address2)); + EXPECT_FALSE(address2.equal(address3)); + EXPECT_FALSE(address3 == address2); + EXPECT_FALSE(address2 == address3); + EXPECT_TRUE(address3 != address2); + EXPECT_TRUE(address2 != address3); + EXPECT_FALSE(address3.is_same_host(address2)); + EXPECT_FALSE(address2.is_same_host(address3)); + EXPECT_TRUE(address3.less(address2)); + EXPECT_FALSE(address2.less(address3)); } TEST(tor_address, generic_network_address) @@ -220,7 +233,7 @@ TEST(tor_address, generic_network_address) namespace { - struct test_command + struct test_command_tor { net::tor_address tor; @@ -234,7 +247,7 @@ TEST(tor_address, epee_serializev_v2) { std::string buffer{}; { - test_command command{MONERO_UNWRAP(net::tor_address::make(v2_onion, 10))}; + test_command_tor command{MONERO_UNWRAP(net::tor_address::make(v2_onion, 10))}; EXPECT_FALSE(command.tor.is_unknown()); EXPECT_NE(net::tor_address{}, command.tor); EXPECT_STREQ(v2_onion, command.tor.host_str()); @@ -245,7 +258,7 @@ TEST(tor_address, epee_serializev_v2) EXPECT_TRUE(stg.store_to_binary(buffer)); } - test_command command{}; + test_command_tor command{}; { EXPECT_TRUE(command.tor.is_unknown()); EXPECT_EQ(net::tor_address{}, command.tor); @@ -285,7 +298,7 @@ TEST(tor_address, epee_serializev_v3) { std::string buffer{}; { - test_command command{MONERO_UNWRAP(net::tor_address::make(v3_onion, 10))}; + test_command_tor command{MONERO_UNWRAP(net::tor_address::make(v3_onion, 10))}; EXPECT_FALSE(command.tor.is_unknown()); EXPECT_NE(net::tor_address{}, command.tor); EXPECT_STREQ(v3_onion, command.tor.host_str()); @@ -296,7 +309,7 @@ TEST(tor_address, epee_serializev_v3) EXPECT_TRUE(stg.store_to_binary(buffer)); } - test_command command{}; + test_command_tor command{}; { EXPECT_TRUE(command.tor.is_unknown()); EXPECT_EQ(net::tor_address{}, command.tor); @@ -336,7 +349,7 @@ TEST(tor_address, epee_serialize_unknown) { std::string buffer{}; { - test_command command{net::tor_address::unknown()}; + test_command_tor command{net::tor_address::unknown()}; EXPECT_TRUE(command.tor.is_unknown()); EXPECT_EQ(net::tor_address{}, command.tor); EXPECT_STREQ(net::tor_address::unknown_str(), command.tor.host_str()); @@ -347,7 +360,7 @@ TEST(tor_address, epee_serialize_unknown) EXPECT_TRUE(stg.store_to_binary(buffer)); } - test_command command{}; + test_command_tor command{}; { EXPECT_TRUE(command.tor.is_unknown()); EXPECT_EQ(net::tor_address{}, command.tor); @@ -513,6 +526,374 @@ TEST(get_network_address, onion) EXPECT_EQ(net::error::invalid_port, address); } +namespace +{ + static constexpr const char b32_i2p[] = + "vww6ybal4bd7szmgncyruucpgfkqahzddi37ktceo3ah7ngmcopn.b32.i2p"; + static constexpr const char b32_i2p_2[] = + "xmrto2bturnore26xmrto2bturnore26xmrto2bturnore26xmr2.b32.i2p"; +} + +TEST(i2p_address, constants) +{ + static_assert(!net::i2p_address::is_local(), "bad is_local() response"); + static_assert(!net::i2p_address::is_loopback(), "bad is_loopback() response"); + static_assert(net::i2p_address::get_type_id() == epee::net_utils::address_type::i2p, "bad get_type_id() response"); + + EXPECT_FALSE(net::i2p_address::is_local()); + EXPECT_FALSE(net::i2p_address::is_loopback()); + EXPECT_EQ(epee::net_utils::address_type::i2p, net::i2p_address::get_type_id()); + EXPECT_EQ(epee::net_utils::address_type::i2p, net::i2p_address::get_type_id()); +} + +TEST(i2p_address, invalid) +{ + EXPECT_TRUE(net::i2p_address::make("").has_error()); + EXPECT_TRUE(net::i2p_address::make(":").has_error()); + EXPECT_TRUE(net::i2p_address::make(".b32.i2p").has_error()); + EXPECT_TRUE(net::i2p_address::make(".b32.i2p:").has_error()); + EXPECT_TRUE(net::i2p_address::make(b32_i2p + 1).has_error()); + EXPECT_TRUE(net::i2p_address::make(boost::string_ref{b32_i2p, sizeof(b32_i2p) - 2}).has_error()); + EXPECT_TRUE(net::i2p_address::make(std::string{b32_i2p} + ":65536").has_error()); + EXPECT_TRUE(net::i2p_address::make(std::string{b32_i2p} + ":-1").has_error()); + + std::string i2p{b32_i2p}; + i2p.at(10) = 1; + EXPECT_TRUE(net::i2p_address::make(i2p).has_error()); +} + +TEST(i2p_address, unblockable_types) +{ + net::i2p_address i2p{}; + + ASSERT_NE(nullptr, i2p.host_str()); + EXPECT_STREQ("<unknown i2p host>", i2p.host_str()); + EXPECT_STREQ("<unknown i2p host>", i2p.str().c_str()); + EXPECT_EQ(0u, i2p.port()); + EXPECT_TRUE(i2p.is_unknown()); + EXPECT_FALSE(i2p.is_local()); + EXPECT_FALSE(i2p.is_loopback()); + EXPECT_EQ(epee::net_utils::address_type::i2p, i2p.get_type_id()); + EXPECT_EQ(epee::net_utils::zone::i2p, i2p.get_zone()); + + i2p = net::i2p_address::unknown(); + ASSERT_NE(nullptr, i2p.host_str()); + EXPECT_STREQ("<unknown i2p host>", i2p.host_str()); + EXPECT_STREQ("<unknown i2p host>", i2p.str().c_str()); + EXPECT_EQ(0u, i2p.port()); + EXPECT_TRUE(i2p.is_unknown()); + EXPECT_FALSE(i2p.is_local()); + EXPECT_FALSE(i2p.is_loopback()); + EXPECT_EQ(epee::net_utils::address_type::i2p, i2p.get_type_id()); + EXPECT_EQ(epee::net_utils::zone::i2p, i2p.get_zone()); + + EXPECT_EQ(net::i2p_address{}, net::i2p_address::unknown()); +} + +TEST(i2p_address, valid) +{ + const auto address1 = net::i2p_address::make(b32_i2p); + + ASSERT_TRUE(address1.has_value()); + EXPECT_EQ(0u, address1->port()); + EXPECT_STREQ(b32_i2p, address1->host_str()); + EXPECT_STREQ(b32_i2p, address1->str().c_str()); + EXPECT_TRUE(address1->is_blockable()); + + net::i2p_address address2{*address1}; + + EXPECT_EQ(0u, address2.port()); + EXPECT_STREQ(b32_i2p, address2.host_str()); + EXPECT_STREQ(b32_i2p, address2.str().c_str()); + EXPECT_TRUE(address2.is_blockable()); + EXPECT_TRUE(address2.equal(*address1)); + EXPECT_TRUE(address1->equal(address2)); + EXPECT_TRUE(address2 == *address1); + EXPECT_TRUE(*address1 == address2); + EXPECT_FALSE(address2 != *address1); + EXPECT_FALSE(*address1 != address2); + EXPECT_TRUE(address2.is_same_host(*address1)); + EXPECT_TRUE(address1->is_same_host(address2)); + EXPECT_FALSE(address2.less(*address1)); + EXPECT_FALSE(address1->less(address2)); + + address2 = MONERO_UNWRAP(net::i2p_address::make(std::string{b32_i2p_2} + ":6545")); + + EXPECT_EQ(6545, address2.port()); + EXPECT_STREQ(b32_i2p_2, address2.host_str()); + EXPECT_EQ(std::string{b32_i2p_2} + ":6545", address2.str().c_str()); + EXPECT_TRUE(address2.is_blockable()); + EXPECT_FALSE(address2.equal(*address1)); + EXPECT_FALSE(address1->equal(address2)); + EXPECT_FALSE(address2 == *address1); + EXPECT_FALSE(*address1 == address2); + EXPECT_TRUE(address2 != *address1); + EXPECT_TRUE(*address1 != address2); + EXPECT_FALSE(address2.is_same_host(*address1)); + EXPECT_FALSE(address1->is_same_host(address2)); + EXPECT_FALSE(address2.less(*address1)); + EXPECT_TRUE(address1->less(address2)); + + net::i2p_address address3 = MONERO_UNWRAP(net::i2p_address::make(std::string{b32_i2p} + ":", 65535)); + + EXPECT_EQ(65535, address3.port()); + EXPECT_STREQ(b32_i2p, address3.host_str()); + EXPECT_EQ(std::string{b32_i2p} + ":65535", address3.str().c_str()); + EXPECT_TRUE(address3.is_blockable()); + EXPECT_FALSE(address3.equal(*address1)); + EXPECT_FALSE(address1->equal(address3)); + EXPECT_FALSE(address3 == *address1); + EXPECT_FALSE(*address1 == address3); + EXPECT_TRUE(address3 != *address1); + EXPECT_TRUE(*address1 != address3); + EXPECT_TRUE(address3.is_same_host(*address1)); + EXPECT_TRUE(address1->is_same_host(address3)); + EXPECT_FALSE(address3.less(*address1)); + EXPECT_TRUE(address1->less(address3)); + + EXPECT_FALSE(address3.equal(address2)); + EXPECT_FALSE(address2.equal(address3)); + EXPECT_FALSE(address3 == address2); + EXPECT_FALSE(address2 == address3); + EXPECT_TRUE(address3 != address2); + EXPECT_TRUE(address2 != address3); + EXPECT_FALSE(address3.is_same_host(address2)); + EXPECT_FALSE(address2.is_same_host(address3)); + EXPECT_TRUE(address3.less(address2)); + EXPECT_FALSE(address2.less(address3)); +} + +TEST(i2p_address, generic_network_address) +{ + const epee::net_utils::network_address i2p1{MONERO_UNWRAP(net::i2p_address::make(b32_i2p, 8080))}; + const epee::net_utils::network_address i2p2{MONERO_UNWRAP(net::i2p_address::make(b32_i2p, 8080))}; + const epee::net_utils::network_address ip{epee::net_utils::ipv4_network_address{100, 200}}; + + EXPECT_EQ(i2p1, i2p2); + EXPECT_NE(ip, i2p1); + EXPECT_LT(ip, i2p1); + + EXPECT_STREQ(b32_i2p, i2p1.host_str().c_str()); + EXPECT_EQ(std::string{b32_i2p} + ":8080", i2p1.str()); + EXPECT_EQ(epee::net_utils::address_type::i2p, i2p1.get_type_id()); + EXPECT_EQ(epee::net_utils::address_type::i2p, i2p2.get_type_id()); + EXPECT_EQ(epee::net_utils::address_type::ipv4, ip.get_type_id()); + EXPECT_EQ(epee::net_utils::zone::i2p, i2p1.get_zone()); + EXPECT_EQ(epee::net_utils::zone::i2p, i2p2.get_zone()); + EXPECT_EQ(epee::net_utils::zone::public_, ip.get_zone()); + EXPECT_TRUE(i2p1.is_blockable()); + EXPECT_TRUE(i2p2.is_blockable()); + EXPECT_TRUE(ip.is_blockable()); +} + +namespace +{ + struct test_command_i2p + { + net::i2p_address i2p; + + BEGIN_KV_SERIALIZE_MAP() + KV_SERIALIZE(i2p); + END_KV_SERIALIZE_MAP() + }; +} + +TEST(i2p_address, epee_serializev_b32) +{ + std::string buffer{}; + { + test_command_i2p command{MONERO_UNWRAP(net::i2p_address::make(b32_i2p, 10))}; + EXPECT_FALSE(command.i2p.is_unknown()); + EXPECT_NE(net::i2p_address{}, command.i2p); + EXPECT_STREQ(b32_i2p, command.i2p.host_str()); + EXPECT_EQ(10u, command.i2p.port()); + + epee::serialization::portable_storage stg{}; + EXPECT_TRUE(command.store(stg)); + EXPECT_TRUE(stg.store_to_binary(buffer)); + } + + test_command_i2p command{}; + { + EXPECT_TRUE(command.i2p.is_unknown()); + EXPECT_EQ(net::i2p_address{}, command.i2p); + EXPECT_STREQ(net::i2p_address::unknown_str(), command.i2p.host_str()); + EXPECT_EQ(0u, command.i2p.port()); + + epee::serialization::portable_storage stg{}; + EXPECT_TRUE(stg.load_from_binary(buffer)); + EXPECT_TRUE(command.load(stg)); + } + EXPECT_FALSE(command.i2p.is_unknown()); + EXPECT_NE(net::i2p_address{}, command.i2p); + EXPECT_STREQ(b32_i2p, command.i2p.host_str()); + EXPECT_EQ(10u, command.i2p.port()); + + // make sure that exceeding max buffer doesn't destroy i2p_address::_load + { + epee::serialization::portable_storage stg{}; + stg.load_from_binary(buffer); + + std::string host{}; + ASSERT_TRUE(stg.get_value("host", host, stg.open_section("i2p", nullptr, false))); + EXPECT_EQ(std::strlen(b32_i2p), host.size()); + + host.push_back('k'); + EXPECT_TRUE(stg.set_value("host", std::string{host}, stg.open_section("i2p", nullptr, false))); + EXPECT_TRUE(command.load(stg)); // poor error reporting from `KV_SERIALIZE` + } + + EXPECT_TRUE(command.i2p.is_unknown()); + EXPECT_EQ(net::i2p_address{}, command.i2p); + EXPECT_STRNE(b32_i2p, command.i2p.host_str()); + EXPECT_EQ(0u, command.i2p.port()); +} + +TEST(i2p_address, epee_serialize_unknown) +{ + std::string buffer{}; + { + test_command_i2p command{net::i2p_address::unknown()}; + EXPECT_TRUE(command.i2p.is_unknown()); + EXPECT_EQ(net::i2p_address{}, command.i2p); + EXPECT_STREQ(net::i2p_address::unknown_str(), command.i2p.host_str()); + EXPECT_EQ(0u, command.i2p.port()); + + epee::serialization::portable_storage stg{}; + EXPECT_TRUE(command.store(stg)); + EXPECT_TRUE(stg.store_to_binary(buffer)); + } + + test_command_i2p command{}; + { + EXPECT_TRUE(command.i2p.is_unknown()); + EXPECT_EQ(net::i2p_address{}, command.i2p); + EXPECT_STRNE(b32_i2p, command.i2p.host_str()); + EXPECT_EQ(0u, command.i2p.port()); + + epee::serialization::portable_storage stg{}; + EXPECT_TRUE(stg.load_from_binary(buffer)); + EXPECT_TRUE(command.load(stg)); + } + EXPECT_TRUE(command.i2p.is_unknown()); + EXPECT_EQ(net::i2p_address{}, command.i2p); + EXPECT_STREQ(net::i2p_address::unknown_str(), command.i2p.host_str()); + EXPECT_EQ(0u, command.i2p.port()); + + // make sure that exceeding max buffer doesn't destroy i2p_address::_load + { + epee::serialization::portable_storage stg{}; + stg.load_from_binary(buffer); + + std::string host{}; + ASSERT_TRUE(stg.get_value("host", host, stg.open_section("i2p", nullptr, false))); + EXPECT_EQ(std::strlen(net::i2p_address::unknown_str()), host.size()); + + host.push_back('k'); + EXPECT_TRUE(stg.set_value("host", std::string{host}, stg.open_section("i2p", nullptr, false))); + EXPECT_TRUE(command.load(stg)); // poor error reporting from `KV_SERIALIZE` + } + + EXPECT_TRUE(command.i2p.is_unknown()); + EXPECT_EQ(net::i2p_address{}, command.i2p); + EXPECT_STRNE(b32_i2p, command.i2p.host_str()); + EXPECT_EQ(0u, command.i2p.port()); +} + +TEST(i2p_address, boost_serialize_b32) +{ + std::string buffer{}; + { + const net::i2p_address i2p = MONERO_UNWRAP(net::i2p_address::make(b32_i2p, 10)); + EXPECT_FALSE(i2p.is_unknown()); + EXPECT_NE(net::i2p_address{}, i2p); + EXPECT_STREQ(b32_i2p, i2p.host_str()); + EXPECT_EQ(10u, i2p.port()); + + std::ostringstream stream{}; + { + boost::archive::portable_binary_oarchive archive{stream}; + archive << i2p; + } + buffer = stream.str(); + } + + net::i2p_address i2p{}; + { + EXPECT_TRUE(i2p.is_unknown()); + EXPECT_EQ(net::i2p_address{}, i2p); + EXPECT_STREQ(net::i2p_address::unknown_str(), i2p.host_str()); + EXPECT_EQ(0u, i2p.port()); + + std::istringstream stream{buffer}; + boost::archive::portable_binary_iarchive archive{stream}; + archive >> i2p; + } + EXPECT_FALSE(i2p.is_unknown()); + EXPECT_NE(net::i2p_address{}, i2p); + EXPECT_STREQ(b32_i2p, i2p.host_str()); + EXPECT_EQ(10u, i2p.port()); +} + +TEST(i2p_address, boost_serialize_unknown) +{ + std::string buffer{}; + { + const net::i2p_address i2p{}; + EXPECT_TRUE(i2p.is_unknown()); + EXPECT_EQ(net::i2p_address::unknown(), i2p); + EXPECT_STREQ(net::i2p_address::unknown_str(), i2p.host_str()); + EXPECT_EQ(0u, i2p.port()); + + std::ostringstream stream{}; + { + boost::archive::portable_binary_oarchive archive{stream}; + archive << i2p; + } + buffer = stream.str(); + } + + net::i2p_address i2p{}; + { + EXPECT_TRUE(i2p.is_unknown()); + EXPECT_EQ(net::i2p_address{}, i2p); + EXPECT_STREQ(net::i2p_address::unknown_str(), i2p.host_str()); + EXPECT_EQ(0u, i2p.port()); + + std::istringstream stream{buffer}; + boost::archive::portable_binary_iarchive archive{stream}; + archive >> i2p; + } + EXPECT_TRUE(i2p.is_unknown()); + EXPECT_EQ(net::i2p_address::unknown(), i2p); + EXPECT_STREQ(net::i2p_address::unknown_str(), i2p.host_str()); + EXPECT_EQ(0u, i2p.port()); +} + +TEST(get_network_address, i2p) +{ + expect<epee::net_utils::network_address> address = + net::get_network_address("i2p", 0); + EXPECT_EQ(net::error::unsupported_address, address); + + address = net::get_network_address(".b32.i2p", 0); + EXPECT_EQ(net::error::invalid_i2p_address, address); + + address = net::get_network_address(b32_i2p, 1000); + ASSERT_TRUE(bool(address)); + EXPECT_EQ(epee::net_utils::address_type::i2p, address->get_type_id()); + EXPECT_STREQ(b32_i2p, address->host_str().c_str()); + EXPECT_EQ(std::string{b32_i2p} + ":1000", address->str()); + + address = net::get_network_address(std::string{b32_i2p} + ":2000", 1000); + ASSERT_TRUE(bool(address)); + EXPECT_EQ(epee::net_utils::address_type::i2p, address->get_type_id()); + EXPECT_STREQ(b32_i2p, address->host_str().c_str()); + EXPECT_EQ(std::string{b32_i2p} + ":2000", address->str()); + + address = net::get_network_address(std::string{b32_i2p} + ":65536", 1000); + EXPECT_EQ(net::error::invalid_port, address); +} TEST(get_network_address, ipv4) { diff --git a/tests/unit_tests/node_server.cpp b/tests/unit_tests/node_server.cpp index c92f70b97..b656c4858 100644 --- a/tests/unit_tests/node_server.cpp +++ b/tests/unit_tests/node_server.cpp @@ -54,7 +54,6 @@ public: bool init(const boost::program_options::variables_map& vm) {return true ;} bool deinit(){return true;} bool get_short_chain_history(std::list<crypto::hash>& ids) const { return true; } - bool get_stat_info(cryptonote::core_stat_info& st_inf) const {return true;} bool have_block(const crypto::hash& id) const {return true;} void get_blockchain_top(uint64_t& height, crypto::hash& top_id)const{height=0;top_id=crypto::null_hash;} bool handle_incoming_tx(const cryptonote::tx_blob_entry& tx_blob, cryptonote::tx_verification_context& tvc, cryptonote::relay_method tx_relay, bool relayed) { return true; } @@ -91,6 +90,8 @@ public: bool prune_blockchain(uint32_t pruning_seed = 0) { return true; } bool is_within_compiled_block_hash_area(uint64_t height) const { return false; } bool has_block_weights(uint64_t height, uint64_t nblocks) const { return false; } + bool get_txpool_complement(const std::vector<crypto::hash> &hashes, std::vector<cryptonote::blobdata> &txes) { return false; } + bool get_pool_transaction_hashes(std::vector<crypto::hash>& txs, bool include_unrelayed_txes = true) const { return false; } void stop() {} }; diff --git a/tests/unit_tests/test_tx_utils.cpp b/tests/unit_tests/test_tx_utils.cpp index d8d760b07..deb45594d 100644 --- a/tests/unit_tests/test_tx_utils.cpp +++ b/tests/unit_tests/test_tx_utils.cpp @@ -44,7 +44,7 @@ namespace TEST(parse_tx_extra, handles_empty_extra) { - std::vector<uint8_t> extra;; + std::vector<uint8_t> extra; std::vector<cryptonote::tx_extra_field> tx_extra_fields; ASSERT_TRUE(cryptonote::parse_tx_extra(extra, tx_extra_fields)); ASSERT_TRUE(tx_extra_fields.empty()); diff --git a/tests/unit_tests/wipeable_string.cpp b/tests/unit_tests/wipeable_string.cpp index 44e050c5c..9911bd6f4 100644 --- a/tests/unit_tests/wipeable_string.cpp +++ b/tests/unit_tests/wipeable_string.cpp @@ -182,6 +182,7 @@ TEST(wipeable_string, split) ASSERT_TRUE(check_split(" foo bar baz ", {"foo", "bar", "baz"})); ASSERT_TRUE(check_split(" foo bar baz", {"foo", "bar", "baz"})); ASSERT_TRUE(check_split("foo bar baz ", {"foo", "bar", "baz"})); + ASSERT_TRUE(check_split("\tfoo\n bar\r\nbaz", {"foo", "bar", "baz"})); } TEST(wipeable_string, parse_hexstr) diff --git a/tests/unit_tests/zmq_rpc.cpp b/tests/unit_tests/zmq_rpc.cpp new file mode 100644 index 000000000..af1f1608b --- /dev/null +++ b/tests/unit_tests/zmq_rpc.cpp @@ -0,0 +1,55 @@ +// Copyright (c) 2020, The Monero Project +// +// All rights reserved. +// +// Redistribution and use in source and binary forms, with or without modification, are +// permitted provided that the following conditions are met: +// +// 1. Redistributions of source code must retain the above copyright notice, this list of +// conditions and the following disclaimer. +// +// 2. Redistributions in binary form must reproduce the above copyright notice, this list +// of conditions and the following disclaimer in the documentation and/or other +// materials provided with the distribution. +// +// 3. Neither the name of the copyright holder nor the names of its contributors may be +// used to endorse or promote products derived from this software without specific +// prior written permission. +// +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY +// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL +// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, +// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, +// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, +// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF +// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +#include <gtest/gtest.h> + +#include "rpc/message.h" +#include "serialization/json_object.h" + +TEST(ZmqFullMessage, InvalidRequest) +{ + EXPECT_THROW( + (cryptonote::rpc::FullMessage{"{\"jsonrpc\":\"2.0\",\"id\":0,\"params\":[]}", true}), + cryptonote::json::MISSING_KEY + ); + EXPECT_THROW( + (cryptonote::rpc::FullMessage{"{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":3,\"params\":[]}", true}), + cryptonote::json::WRONG_TYPE + ); +} + +TEST(ZmqFullMessage, Request) +{ + static constexpr const char request[] = "{\"jsonrpc\":\"2.0\",\"id\":0,\"method\":\"foo\",\"params\":[]}"; + EXPECT_NO_THROW( + (cryptonote::rpc::FullMessage{request, true}) + ); + + cryptonote::rpc::FullMessage parsed{request, true}; + EXPECT_STREQ("foo", parsed.getRequestType().c_str()); +} |