aboutsummaryrefslogtreecommitdiff
path: root/tests
diff options
context:
space:
mode:
Diffstat (limited to 'tests')
-rw-r--r--tests/unit_tests/crypto.cpp213
-rw-r--r--tests/unit_tests/epee_boosted_tcp_server.cpp75
-rw-r--r--tests/unit_tests/epee_utils.cpp4
3 files changed, 289 insertions, 3 deletions
diff --git a/tests/unit_tests/crypto.cpp b/tests/unit_tests/crypto.cpp
index 7ac87ca7c..b550b35b0 100644
--- a/tests/unit_tests/crypto.cpp
+++ b/tests/unit_tests/crypto.cpp
@@ -33,6 +33,7 @@
#include <string>
#include "cryptonote_basic/cryptonote_basic_impl.h"
+#include "cryptonote_basic/merge_mining.h"
namespace
{
@@ -99,3 +100,215 @@ TEST(Crypto, verify_32)
}
}
}
+
+TEST(Crypto, tree_branch)
+{
+ crypto::hash inputs[6];
+ crypto::hash branch[8];
+ crypto::hash root, root2;
+ size_t depth;
+ uint32_t path, path2;
+
+ auto hasher = [](const crypto::hash &h0, const crypto::hash &h1) -> crypto::hash
+ {
+ char buffer[64];
+ memcpy(buffer, &h0, 32);
+ memcpy(buffer + 32, &h1, 32);
+ crypto::hash res;
+ cn_fast_hash(buffer, 64, res);
+ return res;
+ };
+
+ for (int n = 0; n < 6; ++n)
+ {
+ memset(&inputs[n], 0, 32);
+ inputs[n].data[0] = n + 1;
+ }
+
+ // empty
+ ASSERT_FALSE(crypto::tree_branch((const char(*)[32])inputs, 0, crypto::null_hash.data, (char(*)[32])branch, &depth, &path));
+
+ // one, matching
+ ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 1, inputs[0].data, (char(*)[32])branch, &depth, &path));
+ ASSERT_EQ(depth, 0);
+ ASSERT_EQ(path, 0);
+ ASSERT_TRUE(crypto::tree_path(1, 0, &path2));
+ ASSERT_EQ(path, path2);
+ crypto::tree_hash((const char(*)[32])inputs, 1, root.data);
+ ASSERT_EQ(root, inputs[0]);
+ ASSERT_TRUE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
+
+ // one, not found
+ ASSERT_FALSE(crypto::tree_branch((const char(*)[32])inputs, 1, inputs[1].data, (char(*)[32])branch, &depth, &path));
+
+ // two, index 0
+ ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 2, inputs[0].data, (char(*)[32])branch, &depth, &path));
+ ASSERT_EQ(depth, 1);
+ ASSERT_EQ(path, 0);
+ ASSERT_TRUE(crypto::tree_path(2, 0, &path2));
+ ASSERT_EQ(path, path2);
+ ASSERT_EQ(branch[0], inputs[1]);
+ crypto::tree_hash((const char(*)[32])inputs, 2, root.data);
+ ASSERT_EQ(root, hasher(inputs[0], inputs[1]));
+ ASSERT_TRUE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
+
+ // two, index 1
+ ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 2, inputs[1].data, (char(*)[32])branch, &depth, &path));
+ ASSERT_EQ(depth, 1);
+ ASSERT_EQ(path, 1);
+ ASSERT_TRUE(crypto::tree_path(2, 1, &path2));
+ ASSERT_EQ(path, path2);
+ ASSERT_EQ(branch[0], inputs[0]);
+ crypto::tree_hash((const char(*)[32])inputs, 2, root.data);
+ ASSERT_EQ(root, hasher(inputs[0], inputs[1]));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_TRUE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
+
+ // two, not found
+ ASSERT_FALSE(crypto::tree_branch((const char(*)[32])inputs, 2, inputs[2].data, (char(*)[32])branch, &depth, &path));
+
+ // a b c 0
+ // x y
+ // z
+
+ // three, index 0
+ ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 3, inputs[0].data, (char(*)[32])branch, &depth, &path));
+ ASSERT_GE(depth, 1);
+ ASSERT_LE(depth, 2);
+ ASSERT_TRUE(crypto::tree_path(3, 0, &path2));
+ ASSERT_EQ(path, path2);
+ crypto::tree_hash((const char(*)[32])inputs, 3, root.data);
+ ASSERT_EQ(root, hasher(inputs[0], hasher(inputs[1], inputs[2])));
+ ASSERT_TRUE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
+
+ // three, index 1
+ ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 3, inputs[1].data, (char(*)[32])branch, &depth, &path));
+ ASSERT_GE(depth, 1);
+ ASSERT_LE(depth, 2);
+ ASSERT_TRUE(crypto::tree_path(3, 1, &path2));
+ ASSERT_EQ(path, path2);
+ crypto::tree_hash((const char(*)[32])inputs, 3, root.data);
+ ASSERT_EQ(root, hasher(inputs[0], hasher(inputs[1], inputs[2])));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_TRUE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
+
+ // three, index 2
+ ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 3, inputs[2].data, (char(*)[32])branch, &depth, &path));
+ ASSERT_GE(depth, 1);
+ ASSERT_LE(depth, 2);
+ ASSERT_TRUE(crypto::tree_path(3, 2, &path2));
+ ASSERT_EQ(path, path2);
+ crypto::tree_hash((const char(*)[32])inputs, 3, root.data);
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_TRUE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_TRUE(crypto::tree_branch_hash(inputs[2].data, (const char(*)[32])branch, depth, path, root2.data));
+ ASSERT_EQ(root, root2);
+
+ // three, not found
+ ASSERT_FALSE(crypto::tree_branch((const char(*)[32])inputs, 3, inputs[3].data, (char(*)[32])branch, &depth, &path));
+
+ // a b c d e 0 0 0
+ // x y
+ // z
+ // w
+
+ // five, index 0
+ ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 5, inputs[0].data, (char(*)[32])branch, &depth, &path));
+ ASSERT_GE(depth, 2);
+ ASSERT_LE(depth, 3);
+ ASSERT_TRUE(crypto::tree_path(5, 0, &path2));
+ ASSERT_EQ(path, path2);
+ crypto::tree_hash((const char(*)[32])inputs, 5, root.data);
+ ASSERT_TRUE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[5].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
+
+ // five, index 1
+ ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 5, inputs[1].data, (char(*)[32])branch, &depth, &path));
+ ASSERT_GE(depth, 2);
+ ASSERT_LE(depth, 3);
+ ASSERT_TRUE(crypto::tree_path(5, 1, &path2));
+ ASSERT_EQ(path, path2);
+ crypto::tree_hash((const char(*)[32])inputs, 5, root.data);
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_TRUE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[5].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
+
+ // five, index 2
+ ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 5, inputs[2].data, (char(*)[32])branch, &depth, &path));
+ ASSERT_GE(depth, 2);
+ ASSERT_LE(depth, 3);
+ ASSERT_TRUE(crypto::tree_path(5, 2, &path2));
+ ASSERT_EQ(path, path2);
+ crypto::tree_hash((const char(*)[32])inputs, 5, root.data);
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_TRUE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[5].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
+
+ // five, index 4
+ ASSERT_TRUE(crypto::tree_branch((const char(*)[32])inputs, 5, inputs[4].data, (char(*)[32])branch, &depth, &path));
+ ASSERT_GE(depth, 2);
+ ASSERT_LE(depth, 3);
+ ASSERT_TRUE(crypto::tree_path(5, 4, &path2));
+ ASSERT_EQ(path, path2);
+ crypto::tree_hash((const char(*)[32])inputs, 5, root.data);
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[0].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[1].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[2].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[3].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_TRUE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[5].data, root.data, (const char(*)[32])branch, depth, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(crypto::null_hash.data, root.data, (const char(*)[32])branch, depth, path));
+
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth - 1, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth + 1, path));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path ^ 1));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path ^ 2));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])branch, depth, path ^ 3));
+ ASSERT_FALSE(crypto::is_branch_in_tree(inputs[4].data, root.data, (const char(*)[32])(branch + 1), depth, path));
+
+ // five, not found
+ ASSERT_FALSE(crypto::tree_branch((const char(*)[32])inputs, 5, crypto::null_hash.data, (char(*)[32])branch, &depth, &path));
+
+ // depth encoding roundtrip
+ for (uint32_t n_chains = 1; n_chains <= 65; ++n_chains)
+ {
+ for (uint32_t nonce = 0; nonce < 1024; ++nonce)
+ {
+ const uint32_t depth = cryptonote::encode_mm_depth(n_chains, nonce);
+ uint32_t n_chains_2, nonce_2;
+ ASSERT_TRUE(cryptonote::decode_mm_depth(depth, n_chains_2, nonce_2));
+ ASSERT_EQ(n_chains, n_chains_2);
+ ASSERT_EQ(nonce, nonce_2);
+ }
+ }
+}
diff --git a/tests/unit_tests/epee_boosted_tcp_server.cpp b/tests/unit_tests/epee_boosted_tcp_server.cpp
index e99666eb1..06e076a3a 100644
--- a/tests/unit_tests/epee_boosted_tcp_server.cpp
+++ b/tests/unit_tests/epee_boosted_tcp_server.cpp
@@ -143,14 +143,24 @@ TEST(test_epee_connection, test_lifetime)
static constexpr bool handshake_complete() noexcept { return true; }
};
+ using functional_obj_t = std::function<void ()>;
struct command_handler_t: epee::levin::levin_commands_handler<context_t> {
size_t delay;
- command_handler_t(size_t delay = 0): delay(delay) {}
+ functional_obj_t on_connection_close_f;
+ command_handler_t(size_t delay = 0,
+ functional_obj_t on_connection_close_f = nullptr
+ ):
+ delay(delay),
+ on_connection_close_f(on_connection_close_f)
+ {}
virtual int invoke(int, const epee::span<const uint8_t>, epee::byte_slice&, context_t&) override { epee::misc_utils::sleep_no_w(delay); return {}; }
virtual int notify(int, const epee::span<const uint8_t>, context_t&) override { return {}; }
virtual void callback(context_t&) override {}
virtual void on_connection_new(context_t&) override {}
- virtual void on_connection_close(context_t&) override {}
+ virtual void on_connection_close(context_t&) override {
+ if (on_connection_close_f)
+ on_connection_close_f();
+ }
virtual ~command_handler_t() override {}
static void destroy(epee::levin::levin_commands_handler<context_t>* ptr) { delete ptr; }
};
@@ -168,6 +178,14 @@ TEST(test_epee_connection, test_lifetime)
using work_ptr = std::shared_ptr<work_t>;
using workers_t = std::vector<std::thread>;
using server_t = epee::net_utils::boosted_tcp_server<handler_t>;
+ using lock_t = std::mutex;
+ using lock_guard_t = std::lock_guard<lock_t>;
+ using connection_weak_ptr = boost::weak_ptr<connection_t>;
+ struct shared_conn_t {
+ lock_t lock;
+ connection_weak_ptr conn;
+ };
+ using shared_conn_ptr = std::shared_ptr<shared_conn_t>;
io_context_t io_context;
work_ptr work(std::make_shared<work_t>(io_context));
@@ -255,6 +273,7 @@ TEST(test_epee_connection, test_lifetime)
ASSERT_TRUE(index == N * N);
ASSERT_TRUE(shared_state->get_connections_count() == 0);
+ while (shared_state->sock_count);
ASSERT_TRUE(shared_state->get_connections_count() == 0);
constexpr auto DELAY = 30;
constexpr auto TIMEOUT = 1;
@@ -273,6 +292,58 @@ TEST(test_epee_connection, test_lifetime)
shared_state->close(tag);
ASSERT_TRUE(shared_state->get_connections_count() == 0);
}
+
+ while (shared_state->sock_count);
+ constexpr auto ZERO_DELAY = 0;
+ size_t counter = 0;
+ shared_state->set_handler(new command_handler_t(ZERO_DELAY,
+ [&counter]{
+ ASSERT_TRUE(counter++ == 0);
+ }
+ ),
+ &command_handler_t::destroy
+ );
+ connection_ptr conn(new connection_t(io_context, shared_state, {}, {}));
+ conn->socket().connect(endpoint);
+ conn->start({}, {});
+ ASSERT_TRUE(shared_state->get_connections_count() == 1);
+ shared_state->del_out_connections(1);
+ ASSERT_TRUE(shared_state->get_connections_count() == 0);
+ conn.reset();
+
+ while (shared_state->sock_count);
+ shared_conn_ptr shared_conn(std::make_shared<shared_conn_t>());
+ shared_state->set_handler(new command_handler_t(ZERO_DELAY,
+ [shared_state, shared_conn]{
+ {
+ connection_ptr conn;
+ {
+ lock_guard_t guard(shared_conn->lock);
+ conn = std::move(shared_conn->conn.lock());
+ }
+ if (conn)
+ conn->cancel();
+ }
+ const auto success = shared_state->foreach_connection([](context_t&){
+ return true;
+ });
+ ASSERT_TRUE(success);
+ }
+ ),
+ &command_handler_t::destroy
+ );
+ for (auto i = 0; i < N; ++i) {
+ {
+ connection_ptr conn(new connection_t(io_context, shared_state, {}, {}));
+ conn->socket().connect(endpoint);
+ conn->start({}, {});
+ lock_guard_t guard(shared_conn->lock);
+ shared_conn->conn = conn;
+ }
+ ASSERT_TRUE(shared_state->get_connections_count() == 1);
+ shared_state->del_out_connections(1);
+ ASSERT_TRUE(shared_state->get_connections_count() == 0);
+ }
});
for (auto& w: workers) {
diff --git a/tests/unit_tests/epee_utils.cpp b/tests/unit_tests/epee_utils.cpp
index 256f8c3c2..cbe3c61b1 100644
--- a/tests/unit_tests/epee_utils.cpp
+++ b/tests/unit_tests/epee_utils.cpp
@@ -1115,11 +1115,13 @@ TEST(ByteStream, ToByteSlice)
epee::byte_stream stream;
+ stream.reserve(128*1024);
stream.write(source);
EXPECT_EQ(sizeof(source), stream.size());
+ EXPECT_EQ(128*1024, stream.capacity());
EXPECT_TRUE(equal(source, byte_span{stream.data(), stream.size()}));
- const epee::byte_slice slice{std::move(stream)};
+ const epee::byte_slice slice{std::move(stream), true};
EXPECT_EQ(0u, stream.size());
EXPECT_EQ(0u, stream.available());
EXPECT_EQ(0u, stream.capacity());