aboutsummaryrefslogtreecommitdiff
path: root/tests/unit_tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests/unit_tests')
-rw-r--r--tests/unit_tests/epee_levin_protocol_handler_async.cpp8
-rw-r--r--tests/unit_tests/epee_utils.cpp69
-rw-r--r--tests/unit_tests/notify.cpp21
-rw-r--r--tests/unit_tests/test_peerlist.cpp6
-rw-r--r--tests/unit_tests/testdb.h4
5 files changed, 94 insertions, 14 deletions
diff --git a/tests/unit_tests/epee_levin_protocol_handler_async.cpp b/tests/unit_tests/epee_levin_protocol_handler_async.cpp
index 10e62c167..9ea71875b 100644
--- a/tests/unit_tests/epee_levin_protocol_handler_async.cpp
+++ b/tests/unit_tests/epee_levin_protocol_handler_async.cpp
@@ -56,22 +56,22 @@ namespace
{
}
- virtual int invoke(int command, const std::string& in_buff, std::string& buff_out, test_levin_connection_context& context)
+ virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, test_levin_connection_context& context)
{
m_invoke_counter.inc();
boost::unique_lock<boost::mutex> lock(m_mutex);
m_last_command = command;
- m_last_in_buf = in_buff;
+ m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size());
buff_out = m_invoke_out_buf;
return m_return_code;
}
- virtual int notify(int command, const std::string& in_buff, test_levin_connection_context& context)
+ virtual int notify(int command, const epee::span<const uint8_t> in_buff, test_levin_connection_context& context)
{
m_notify_counter.inc();
boost::unique_lock<boost::mutex> lock(m_mutex);
m_last_command = command;
- m_last_in_buf = in_buff;
+ m_last_in_buf = std::string((const char*)in_buff.data(), in_buff.size());
return m_return_code;
}
diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp
index c384ce9a5..75cf2fdd4 100644
--- a/tests/unit_tests/epee_utils.cpp
+++ b/tests/unit_tests/epee_utils.cpp
@@ -46,6 +46,7 @@
#include "hex.h"
#include "net/net_utils_base.h"
#include "net/local_ip.h"
+#include "net/buffer.h"
#include "p2p/net_peerlist_boost_serialization.h"
#include "span.h"
#include "string_tools.h"
@@ -764,3 +765,71 @@ TEST(NetUtils, PrivateRanges)
ASSERT_EQ(is_local("0.0.30.172"), false);
ASSERT_EQ(is_local("0.0.30.127"), false);
}
+
+TEST(net_buffer, basic)
+{
+ epee::net_utils::buffer buf;
+
+ ASSERT_EQ(buf.size(), 0);
+ EXPECT_THROW(buf.span(1), std::runtime_error);
+ buf.append("a", 1);
+ epee::span<const uint8_t> span = buf.span(1);
+ ASSERT_EQ(span.size(), 1);
+ ASSERT_EQ(span.data()[0], 'a');
+ EXPECT_THROW(buf.span(2), std::runtime_error);
+ buf.append("bc", 2);
+ buf.erase(1);
+ EXPECT_THROW(buf.span(3), std::runtime_error);
+ span = buf.span(2);
+ ASSERT_EQ(span.size(), 2);
+ ASSERT_EQ(span.data()[0], 'b');
+ ASSERT_EQ(span.data()[1], 'c');
+ buf.erase(1);
+ EXPECT_THROW(buf.span(2), std::runtime_error);
+ span = buf.span(1);
+ ASSERT_EQ(span.size(), 1);
+ ASSERT_EQ(span.data()[0], 'c');
+ EXPECT_THROW(buf.erase(2), std::runtime_error);
+ buf.erase(1);
+ EXPECT_EQ(buf.size(), 0);
+ EXPECT_THROW(buf.span(1), std::runtime_error);
+}
+
+TEST(net_buffer, existing_capacity)
+{
+ epee::net_utils::buffer buf;
+
+ buf.append("123456789", 9);
+ buf.erase(9);
+ buf.append("abc", 3);
+ buf.append("def", 3);
+ ASSERT_EQ(buf.size(), 6);
+ epee::span<const uint8_t> span = buf.span(6);
+ ASSERT_TRUE(!memcmp(span.data(), "abcdef", 6));
+}
+
+TEST(net_buffer, reallocate)
+{
+ epee::net_utils::buffer buf;
+
+ buf.append(std::string(4000, ' ').c_str(), 4000);
+ buf.append(std::string(8000, '0').c_str(), 8000);
+ ASSERT_EQ(buf.size(), 12000);
+ epee::span<const uint8_t> span = buf.span(12000);
+ ASSERT_TRUE(!memcmp(span.data(), std::string(4000, ' ').c_str(), 4000));
+ ASSERT_TRUE(!memcmp(span.data() + 4000, std::string(8000, '0').c_str(), 8000));
+}
+
+TEST(net_buffer, move)
+{
+ epee::net_utils::buffer buf;
+
+ buf.append(std::string(400, ' ').c_str(), 400);
+ buf.erase(399);
+ buf.append(std::string(4000, '0').c_str(), 4000);
+ ASSERT_EQ(buf.size(), 4001);
+ epee::span<const uint8_t> span = buf.span(4001);
+ ASSERT_TRUE(!memcmp(span.data(), std::string(1, ' ').c_str(), 1));
+ ASSERT_TRUE(!memcmp(span.data() + 1, std::string(4000, '0').c_str(), 4000));
+}
+
diff --git a/tests/unit_tests/notify.cpp b/tests/unit_tests/notify.cpp
index 105d21ca8..cd70b7739 100644
--- a/tests/unit_tests/notify.cpp
+++ b/tests/unit_tests/notify.cpp
@@ -69,11 +69,22 @@ TEST(notify, works)
tools::Notify notify(spec.c_str());
notify.notify("1111111111111111111111111111111111111111111111111111111111111111");
- epee::misc_utils::sleep_no_w(100);
-
- std::string s;
- ASSERT_TRUE(epee::file_io_utils::load_file_to_string(name_template, s));
- ASSERT_TRUE(s == "1111111111111111111111111111111111111111111111111111111111111111");
+ bool ok = false;
+ for (int i = 0; i < 10; ++i)
+ {
+ epee::misc_utils::sleep_no_w(100);
+ std::string s;
+ if (epee::file_io_utils::load_file_to_string(name_template, s))
+ {
+ if (s == "1111111111111111111111111111111111111111111111111111111111111111")
+ {
+ ok = true;
+ break;
+ }
+ std::cout << "got: [" << s << "]" << std::endl;
+ }
+ }
boost::filesystem::remove(name_template);
+ ASSERT_TRUE(ok);
}
diff --git a/tests/unit_tests/test_peerlist.cpp b/tests/unit_tests/test_peerlist.cpp
index c6572c6c2..f638c6251 100644
--- a/tests/unit_tests/test_peerlist.cpp
+++ b/tests/unit_tests/test_peerlist.cpp
@@ -42,7 +42,7 @@ TEST(peer_list, peer_list_general)
#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);}
-#define PRINT_HEAD(step) {std::list<nodetool::peerlist_entry> bs_head; bool r = plm.get_peerlist_head(bs_head, 100);std::cout << "step " << step << ": " << bs_head.size() << std::endl;}
+#define PRINT_HEAD(step) {std::vector<nodetool::peerlist_entry> bs_head; bool r = plm.get_peerlist_head(bs_head, 100);std::cout << "step " << step << ": " << bs_head.size() << std::endl;}
ADD_GRAY_NODE(MAKE_IPV4_ADDRESS(123,43,12,1, 8080), 121241, 34345);
ADD_GRAY_NODE(MAKE_IPV4_ADDRESS(123,43,12,2, 8080), 121241, 34345);
@@ -58,7 +58,7 @@ TEST(peer_list, peer_list_general)
size_t gray_list_size = plm.get_gray_peers_count();
ASSERT_EQ(gray_list_size, 1);
- std::list<nodetool::peerlist_entry> bs_head;
+ std::vector<nodetool::peerlist_entry> bs_head;
bool r = plm.get_peerlist_head(bs_head, 100);
std::cout << bs_head.size() << std::endl;
ASSERT_TRUE(r);
@@ -78,7 +78,7 @@ TEST(peer_list, merge_peer_lists)
//ADD_NODE_TO_PL("\2", \3, 0x\1, (1353346618 -(\4*60*60*24+\5*60*60+\6*60+\7 )));\n
nodetool::peerlist_manager plm;
plm.init(false);
- std::list<nodetool::peerlist_entry> outer_bs;
+ std::vector<nodetool::peerlist_entry> outer_bs;
#define ADD_NODE_TO_PL(ip_, port_, id_, timestamp_) { nodetool::peerlist_entry ple; epee::string_tools::get_ip_int32_from_string(ple.adr.ip, ip_); ple.last_seen = timestamp_; ple.adr.port = port_; ple.id = id_;outer_bs.push_back(ple);}
diff --git a/tests/unit_tests/testdb.h b/tests/unit_tests/testdb.h
index 5d9ba5833..709ab40b7 100644
--- a/tests/unit_tests/testdb.h
+++ b/tests/unit_tests/testdb.h
@@ -89,11 +89,11 @@ public:
virtual uint64_t get_tx_block_height(const crypto::hash& h) const { return 0; }
virtual uint64_t get_num_outputs(const uint64_t& amount) const { return 1; }
virtual uint64_t get_indexing_base() const { return 0; }
- virtual cryptonote::output_data_t get_output_key(const uint64_t& amount, const uint64_t& index) { return cryptonote::output_data_t(); }
+ virtual cryptonote::output_data_t get_output_key(const uint64_t& amount, const uint64_t& index) const { return cryptonote::output_data_t(); }
virtual cryptonote::tx_out_index get_output_tx_and_index_from_global(const uint64_t& index) const { return cryptonote::tx_out_index(); }
virtual cryptonote::tx_out_index get_output_tx_and_index(const uint64_t& amount, const uint64_t& index) const { return cryptonote::tx_out_index(); }
virtual void get_output_tx_and_index(const uint64_t& amount, const std::vector<uint64_t> &offsets, std::vector<cryptonote::tx_out_index> &indices) const {}
- virtual void get_output_key(const epee::span<const uint64_t> &amounts, const std::vector<uint64_t> &offsets, std::vector<cryptonote::output_data_t> &outputs, bool allow_partial = false) {}
+ virtual void get_output_key(const epee::span<const uint64_t> &amounts, const std::vector<uint64_t> &offsets, std::vector<cryptonote::output_data_t> &outputs, bool allow_partial = false) const {}
virtual bool can_thread_bulk_indices() const { return false; }
virtual std::vector<uint64_t> get_tx_output_indices(const crypto::hash& h) const { return std::vector<uint64_t>(); }
virtual std::vector<uint64_t> get_tx_amount_output_indices(const uint64_t tx_index) const { return std::vector<uint64_t>(); }