aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit_tests')
-rw-r--r--tests/unit_tests/CMakeLists.txt3
-rw-r--r--tests/unit_tests/ban.cpp22
-rw-r--r--tests/unit_tests/blockchain_db.cpp9
-rw-r--r--tests/unit_tests/difficulty.cpp14
-rw-r--r--tests/unit_tests/epee_levin_protocol_handler_async.cpp46
-rw-r--r--tests/unit_tests/logging.cpp17
-rw-r--r--tests/unit_tests/net.cpp148
-rw-r--r--tests/unit_tests/output_distribution.cpp2
-rw-r--r--tests/unit_tests/test_protocol_pack.cpp2
9 files changed, 217 insertions, 46 deletions
diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt
index a5d179040..cac1fa943 100644
--- a/tests/unit_tests/CMakeLists.txt
+++ b/tests/unit_tests/CMakeLists.txt
@@ -117,7 +117,8 @@ target_link_libraries(unit_tests
${Boost_THREAD_LIBRARY}
${GTEST_LIBRARIES}
${CMAKE_THREAD_LIBS_INIT}
- ${EXTRA_LIBRARIES})
+ ${EXTRA_LIBRARIES}
+ ${ZMQ_LIB})
set_property(TARGET unit_tests
PROPERTY
FOLDER "tests")
diff --git a/tests/unit_tests/ban.cpp b/tests/unit_tests/ban.cpp
index 17fba90c6..b710f9226 100644
--- a/tests/unit_tests/ban.cpp
+++ b/tests/unit_tests/ban.cpp
@@ -36,6 +36,7 @@
#include "cryptonote_protocol/cryptonote_protocol_handler.inl"
#define MAKE_IPV4_ADDRESS(a,b,c,d) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),0}
+#define MAKE_IPV4_ADDRESS_PORT(a,b,c,d,e) epee::net_utils::ipv4_network_address{MAKE_IP(a,b,c,d),e}
#define MAKE_IPV4_SUBNET(a,b,c,d,e) epee::net_utils::ipv4_network_subnet{MAKE_IP(a,b,c,d),e}
namespace cryptonote {
@@ -94,10 +95,10 @@ typedef nodetool::node_server<cryptonote::t_cryptonote_protocol_handler<test_cor
static bool is_blocked(Server &server, const epee::net_utils::network_address &address, time_t *t = NULL)
{
- std::map<epee::net_utils::network_address, time_t> hosts = server.get_blocked_hosts();
+ std::map<std::string, time_t> hosts = server.get_blocked_hosts();
for (auto rec: hosts)
{
- if (rec.first == address)
+ if (rec.first == address.host_str())
{
if (t)
*t = rec.second;
@@ -240,5 +241,22 @@ TEST(ban, subnet)
ASSERT_TRUE(server.get_blocked_subnets().size() == 0);
}
+TEST(ban, ignores_port)
+{
+ time_t seconds;
+ test_core pr_core;
+ cryptonote::t_cryptonote_protocol_handler<test_core> cprotocol(pr_core, NULL);
+ Server server(cprotocol);
+ cprotocol.set_p2p_endpoint(&server);
+
+ ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5)));
+ ASSERT_TRUE(server.block_host(MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5), std::numeric_limits<time_t>::max() - 1));
+ ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5)));
+ ASSERT_TRUE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,6)));
+ ASSERT_TRUE(server.unblock_host(MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5)));
+ ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,5)));
+ ASSERT_FALSE(is_blocked(server,MAKE_IPV4_ADDRESS_PORT(1,2,3,4,6)));
+}
+
namespace nodetool { template class node_server<cryptonote::t_cryptonote_protocol_handler<test_core>>; }
namespace cryptonote { template class t_cryptonote_protocol_handler<test_core>; }
diff --git a/tests/unit_tests/blockchain_db.cpp b/tests/unit_tests/blockchain_db.cpp
index d7c60cecb..6b569d113 100644
--- a/tests/unit_tests/blockchain_db.cpp
+++ b/tests/unit_tests/blockchain_db.cpp
@@ -38,9 +38,6 @@
#include "string_tools.h"
#include "blockchain_db/blockchain_db.h"
#include "blockchain_db/lmdb/db_lmdb.h"
-#ifdef BERKELEY_DB
-#include "blockchain_db/berkeleydb/db_bdb.h"
-#endif
#include "cryptonote_basic/cryptonote_format_utils.h"
using namespace cryptonote;
@@ -233,11 +230,7 @@ protected:
using testing::Types;
-typedef Types<BlockchainLMDB
-#ifdef BERKELEY_DB
- , BlockchainBDB
-#endif
-> implementations;
+typedef Types<BlockchainLMDB> implementations;
TYPED_TEST_CASE(BlockchainDBTest, implementations);
diff --git a/tests/unit_tests/difficulty.cpp b/tests/unit_tests/difficulty.cpp
index a732e6969..e9e3272f0 100644
--- a/tests/unit_tests/difficulty.cpp
+++ b/tests/unit_tests/difficulty.cpp
@@ -27,6 +27,7 @@
// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#include "gtest/gtest.h"
+#include "int-util.h"
#include "cryptonote_basic/difficulty.h"
static cryptonote::difficulty_type MKDIFF(uint64_t high, uint64_t low)
@@ -42,13 +43,18 @@ static crypto::hash MKHASH(uint64_t high, uint64_t low)
hash_target = (hash_target << 64) | low;
boost::multiprecision::uint256_t hash_value = std::numeric_limits<boost::multiprecision::uint256_t>::max() / hash_target;
crypto::hash h;
- ((uint64_t*)&h)[0] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
+ uint64_t val;
+ val = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
+ ((uint64_t*)&h)[0] = SWAP64LE(val);
hash_value >>= 64;
- ((uint64_t*)&h)[1] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
+ val = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
+ ((uint64_t*)&h)[1] = SWAP64LE(val);
hash_value >>= 64;
- ((uint64_t*)&h)[2] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
+ val = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
+ ((uint64_t*)&h)[2] = SWAP64LE(val);
hash_value >>= 64;
- ((uint64_t*)&h)[3] = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
+ val = (hash_value & 0xffffffffffffffff).convert_to<uint64_t>();
+ ((uint64_t*)&h)[3] = SWAP64LE(val);
return h;
}
diff --git a/tests/unit_tests/epee_levin_protocol_handler_async.cpp b/tests/unit_tests/epee_levin_protocol_handler_async.cpp
index 7bdb4c43d..b27699a77 100644
--- a/tests/unit_tests/epee_levin_protocol_handler_async.cpp
+++ b/tests/unit_tests/epee_levin_protocol_handler_async.cpp
@@ -241,13 +241,13 @@ namespace
m_in_data.assign(256, 't');
- m_req_head.m_signature = LEVIN_SIGNATURE;
- m_req_head.m_cb = m_in_data.size();
+ m_req_head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
+ m_req_head.m_cb = SWAP64LE(m_in_data.size());
m_req_head.m_have_to_return_data = true;
- m_req_head.m_command = expected_command;
- m_req_head.m_return_code = LEVIN_OK;
- m_req_head.m_flags = LEVIN_PACKET_REQUEST;
- m_req_head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
+ m_req_head.m_command = SWAP32LE(expected_command);
+ m_req_head.m_return_code = SWAP32LE(LEVIN_OK);
+ m_req_head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
+ m_req_head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
m_commands_handler.return_code(expected_return_code);
m_commands_handler.invoke_out_buf(m_expected_invoke_out_buf);
@@ -337,12 +337,12 @@ TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_process
std::string in_data(256, 'q');
epee::levin::bucket_head2 req_head;
- req_head.m_signature = LEVIN_SIGNATURE;
- req_head.m_cb = in_data.size();
+ req_head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
+ req_head.m_cb = SWAP64LE(in_data.size());
req_head.m_have_to_return_data = true;
- req_head.m_command = expected_command;
- req_head.m_flags = LEVIN_PACKET_REQUEST;
- req_head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
+ req_head.m_command = SWAP32LE(expected_command);
+ req_head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
+ req_head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
std::string buf(reinterpret_cast<const char*>(&req_head), sizeof(req_head));
buf += in_data;
@@ -373,13 +373,13 @@ TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_process
// Check sent response
ASSERT_EQ(expected_out_data, out_data);
- ASSERT_EQ(LEVIN_SIGNATURE, resp_head.m_signature);
- ASSERT_EQ(expected_command, resp_head.m_command);
- ASSERT_EQ(expected_return_code, resp_head.m_return_code);
- ASSERT_EQ(expected_out_data.size(), resp_head.m_cb);
+ ASSERT_EQ(LEVIN_SIGNATURE, SWAP64LE(resp_head.m_signature));
+ ASSERT_EQ(expected_command, SWAP32LE(resp_head.m_command));
+ ASSERT_EQ(expected_return_code, SWAP32LE(resp_head.m_return_code));
+ ASSERT_EQ(expected_out_data.size(), SWAP64LE(resp_head.m_cb));
ASSERT_FALSE(resp_head.m_have_to_return_data);
- ASSERT_EQ(LEVIN_PROTOCOL_VER_1, resp_head.m_protocol_version);
- ASSERT_TRUE(0 != (resp_head.m_flags & LEVIN_PACKET_RESPONSE));
+ ASSERT_EQ(SWAP32LE(LEVIN_PROTOCOL_VER_1), resp_head.m_protocol_version);
+ ASSERT_TRUE(0 != (SWAP32LE(resp_head.m_flags) & LEVIN_PACKET_RESPONSE));
}
TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_notify)
@@ -392,12 +392,12 @@ TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_process
std::string in_data(256, 'e');
epee::levin::bucket_head2 req_head;
- req_head.m_signature = LEVIN_SIGNATURE;
- req_head.m_cb = in_data.size();
+ req_head.m_signature = SWAP64LE(LEVIN_SIGNATURE);
+ req_head.m_cb = SWAP64LE(in_data.size());
req_head.m_have_to_return_data = false;
- req_head.m_command = expected_command;
- req_head.m_flags = LEVIN_PACKET_REQUEST;
- req_head.m_protocol_version = LEVIN_PROTOCOL_VER_1;
+ req_head.m_command = SWAP32LE(expected_command);
+ req_head.m_flags = SWAP32LE(LEVIN_PACKET_REQUEST);
+ req_head.m_protocol_version = SWAP32LE(LEVIN_PROTOCOL_VER_1);
std::string buf(reinterpret_cast<const char*>(&req_head), sizeof(req_head));
buf += in_data;
@@ -618,7 +618,7 @@ TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_two_re
TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_unexpected_response)
{
- m_req_head.m_flags = LEVIN_PACKET_RESPONSE;
+ m_req_head.m_flags = SWAP32LE(LEVIN_PACKET_RESPONSE);
prepare_buf();
ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
diff --git a/tests/unit_tests/logging.cpp b/tests/unit_tests/logging.cpp
index 056eae604..c8526abae 100644
--- a/tests/unit_tests/logging.cpp
+++ b/tests/unit_tests/logging.cpp
@@ -178,3 +178,20 @@ TEST(logging, last_precedence)
cleanup();
}
+TEST(logging, multiline)
+{
+ init();
+ mlog_set_categories("global:INFO");
+ MGINFO("first\nsecond\nthird");
+ std::string str;
+ ASSERT_TRUE(load_log_to_string(log_filename, str));
+ ASSERT_TRUE(nlines(str) == 3);
+ ASSERT_TRUE(str.find("global") != std::string::npos);
+ ASSERT_TRUE(str.find("first") != std::string::npos);
+ ASSERT_TRUE(str.find("second") != std::string::npos);
+ ASSERT_TRUE(str.find("third") != std::string::npos);
+ ASSERT_TRUE(str.find("first\nsecond") == std::string::npos);
+ ASSERT_TRUE(str.find("second\nthird") == std::string::npos);
+ cleanup();
+}
+
diff --git a/tests/unit_tests/net.cpp b/tests/unit_tests/net.cpp
index f24ffb45c..253280d4d 100644
--- a/tests/unit_tests/net.cpp
+++ b/tests/unit_tests/net.cpp
@@ -40,6 +40,7 @@
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/combine.hpp>
#include <boost/system/error_code.hpp>
+#include <boost/thread/scoped_thread.hpp>
#include <boost/thread/thread.hpp>
#include <boost/uuid/nil_generator.hpp>
#include <boost/uuid/random_generator.hpp>
@@ -59,6 +60,7 @@
#include "net/socks_connect.h"
#include "net/parse.h"
#include "net/tor_address.h"
+#include "net/zmq.h"
#include "p2p/net_peerlist_boost_serialization.h"
#include "serialization/keyvalue_serialization.h"
#include "storages/portable_storage.h"
@@ -1040,7 +1042,8 @@ TEST(dandelionpp_map, dropped_connection_remapped)
boost::uuids::random_generator random_uuid{};
std::vector<boost::uuids::uuid> connections{3};
- std::generate(connections.begin(), connections.end(), random_uuid);
+ for (auto &e: connections)
+ e = random_uuid();
std::sort(connections.begin(), connections.end());
// select 3 of 3 outgoing connections
@@ -1070,7 +1073,8 @@ TEST(dandelionpp_map, dropped_connection_remapped)
}
std::map<boost::uuids::uuid, boost::uuids::uuid> mapping;
std::vector<boost::uuids::uuid> in_connections{9};
- std::generate(in_connections.begin(), in_connections.end(), random_uuid);
+ for (auto &e: in_connections)
+ e = random_uuid();
{
std::map<boost::uuids::uuid, std::size_t> used;
std::multimap<boost::uuids::uuid, boost::uuids::uuid> inverse_mapping;
@@ -1149,7 +1153,8 @@ TEST(dandelionpp_map, dropped_connection_remapped)
}
// map 8 new incoming connections across 3 outgoing links
in_connections.resize(18);
- std::generate(in_connections.begin() + 10, in_connections.end(), random_uuid);
+ for (size_t i = 10; i < in_connections.size(); ++i)
+ in_connections[i] = random_uuid();
{
std::map<boost::uuids::uuid, std::size_t> used;
for (const boost::uuids::uuid& connection : in_connections)
@@ -1176,7 +1181,8 @@ TEST(dandelionpp_map, dropped_all_connections)
boost::uuids::random_generator random_uuid{};
std::vector<boost::uuids::uuid> connections{8};
- std::generate(connections.begin(), connections.end(), random_uuid);
+ for (auto &e: connections)
+ e = random_uuid();
std::sort(connections.begin(), connections.end());
// select 3 of 8 outgoing connections
@@ -1205,7 +1211,8 @@ TEST(dandelionpp_map, dropped_all_connections)
}
}
std::vector<boost::uuids::uuid> in_connections{9};
- std::generate(in_connections.begin(), in_connections.end(), random_uuid);
+ for (auto &e: in_connections)
+ e = random_uuid();
{
std::map<boost::uuids::uuid, std::size_t> used;
std::map<boost::uuids::uuid, boost::uuids::uuid> mapping;
@@ -1237,7 +1244,8 @@ TEST(dandelionpp_map, dropped_all_connections)
// select 3 of 30 connections, only 7 should be remapped to new indexes (but all to new uuids)
connections.resize(30);
- std::generate(connections.begin(), connections.end(), random_uuid);
+ for (auto &e: connections)
+ e = random_uuid();
EXPECT_TRUE(mapper.update(connections));
{
std::map<boost::uuids::uuid, std::size_t> used;
@@ -1253,3 +1261,131 @@ TEST(dandelionpp_map, dropped_all_connections)
EXPECT_EQ(3u, entry.second);
}
}
+
+TEST(zmq, error_codes)
+{
+ EXPECT_EQ(
+ std::addressof(net::zmq::error_category()),
+ std::addressof(net::zmq::make_error_code(0).category())
+ );
+ EXPECT_EQ(
+ std::make_error_condition(std::errc::not_a_socket),
+ net::zmq::make_error_code(ENOTSOCK)
+ );
+
+ EXPECT_TRUE(
+ []() -> expect<void>
+ {
+ MONERO_ZMQ_CHECK(zmq_msg_send(nullptr, nullptr, 0));
+ return success();
+ }().matches(std::errc::not_a_socket)
+ );
+
+ bool thrown = false;
+ try
+ {
+ MONERO_ZMQ_THROW("stuff");
+ }
+ catch (const std::system_error& e)
+ {
+ thrown = true;
+ EXPECT_EQ(std::make_error_condition(std::errc::not_a_socket), e.code());
+ }
+ EXPECT_TRUE(thrown);
+}
+
+TEST(zmq, read_write)
+{
+ net::zmq::context context{zmq_init(1)};
+ ASSERT_NE(nullptr, context);
+
+ net::zmq::socket send_socket{zmq_socket(context.get(), ZMQ_REQ)};
+ net::zmq::socket recv_socket{zmq_socket(context.get(), ZMQ_REP)};
+ ASSERT_NE(nullptr, send_socket);
+ ASSERT_NE(nullptr, recv_socket);
+
+ ASSERT_EQ(0u, zmq_bind(recv_socket.get(), "inproc://testing"));
+ ASSERT_EQ(0u, zmq_connect(send_socket.get(), "inproc://testing"));
+
+ std::string message;
+ message.resize(1024);
+ crypto::rand(message.size(), reinterpret_cast<std::uint8_t*>(std::addressof(message[0])));
+
+ ASSERT_TRUE(bool(net::zmq::send(epee::strspan<std::uint8_t>(message), send_socket.get())));
+
+ const expect<std::string> received = net::zmq::receive(recv_socket.get());
+ ASSERT_TRUE(bool(received));
+ EXPECT_EQ(message, *received);
+}
+
+TEST(zmq, read_write_multipart)
+{
+ net::zmq::context context{zmq_init(1)};
+ ASSERT_NE(nullptr, context);
+
+ net::zmq::socket send_socket{zmq_socket(context.get(), ZMQ_REQ)};
+ net::zmq::socket recv_socket{zmq_socket(context.get(), ZMQ_REP)};
+ ASSERT_NE(nullptr, send_socket);
+ ASSERT_NE(nullptr, recv_socket);
+
+ ASSERT_EQ(0u, zmq_bind(recv_socket.get(), "inproc://testing"));
+ ASSERT_EQ(0u, zmq_connect(send_socket.get(), "inproc://testing"));
+
+ std::string message;
+ message.resize(999);
+ crypto::rand(message.size(), reinterpret_cast<std::uint8_t*>(std::addressof(message[0])));
+
+ for (unsigned i = 0; i < 3; ++i)
+ {
+ const expect<std::string> received = net::zmq::receive(recv_socket.get(), ZMQ_DONTWAIT);
+ ASSERT_FALSE(bool(received));
+ EXPECT_EQ(net::zmq::make_error_code(EAGAIN), received.error());
+
+ const epee::span<const std::uint8_t> bytes{
+ reinterpret_cast<const std::uint8_t*>(std::addressof(message[0])) + (i * 333), 333
+ };
+ ASSERT_TRUE(bool(net::zmq::send(bytes, send_socket.get(), (i == 2 ? 0 : ZMQ_SNDMORE))));
+ }
+
+ const expect<std::string> received = net::zmq::receive(recv_socket.get(), ZMQ_DONTWAIT);
+ ASSERT_TRUE(bool(received));
+ EXPECT_EQ(message, *received);
+}
+
+TEST(zmq, read_write_termination)
+{
+ net::zmq::context context{zmq_init(1)};
+ ASSERT_NE(nullptr, context);
+
+ // must be declared before sockets and after context
+ boost::scoped_thread<> thread{};
+
+ net::zmq::socket send_socket{zmq_socket(context.get(), ZMQ_REQ)};
+ net::zmq::socket recv_socket{zmq_socket(context.get(), ZMQ_REP)};
+ ASSERT_NE(nullptr, send_socket);
+ ASSERT_NE(nullptr, recv_socket);
+
+ ASSERT_EQ(0u, zmq_bind(recv_socket.get(), "inproc://testing"));
+ ASSERT_EQ(0u, zmq_connect(send_socket.get(), "inproc://testing"));
+
+ std::string message;
+ message.resize(1024);
+ crypto::rand(message.size(), reinterpret_cast<std::uint8_t*>(std::addressof(message[0])));
+
+ ASSERT_TRUE(bool(net::zmq::send(epee::strspan<std::uint8_t>(message), send_socket.get(), ZMQ_SNDMORE)));
+
+ expect<std::string> received = net::zmq::receive(recv_socket.get(), ZMQ_DONTWAIT);
+ ASSERT_FALSE(bool(received));
+ EXPECT_EQ(net::zmq::make_error_code(EAGAIN), received.error());
+
+ thread = boost::scoped_thread<>{
+ boost::thread{
+ [&context] () { context.reset(); }
+ }
+ };
+
+ received = net::zmq::receive(recv_socket.get());
+ ASSERT_FALSE(bool(received));
+ EXPECT_EQ(net::zmq::make_error_code(ETERM), received.error());
+}
+
diff --git a/tests/unit_tests/output_distribution.cpp b/tests/unit_tests/output_distribution.cpp
index 38f442c59..eec694d1e 100644
--- a/tests/unit_tests/output_distribution.cpp
+++ b/tests/unit_tests/output_distribution.cpp
@@ -93,7 +93,7 @@ bool get_output_distribution(uint64_t amount, uint64_t from, uint64_t to, uint64
crypto::hash get_block_hash(uint64_t height)
{
- crypto::hash hash;
+ crypto::hash hash = crypto::null_hash;
*((uint64_t*)&hash) = height;
return hash;
}
diff --git a/tests/unit_tests/test_protocol_pack.cpp b/tests/unit_tests/test_protocol_pack.cpp
index 0ae2e9c68..59e46e332 100644
--- a/tests/unit_tests/test_protocol_pack.cpp
+++ b/tests/unit_tests/test_protocol_pack.cpp
@@ -42,7 +42,7 @@ TEST(protocol_pack, protocol_pack_command)
r.total_height = 3;
for(int i = 1; i < 10000; i += i*10)
{
- r.m_block_ids.resize(i, boost::value_initialized<crypto::hash>());
+ r.m_block_ids.resize(i, crypto::hash{});
bool res = epee::serialization::store_t_to_binary(r, buff);
ASSERT_TRUE(res);