Merge pull request #5793

bdfc63a Add ref-counted buffer byte_slice. Currently used for sending TCP data. (vtnerd)
3b24b1d Added support for 'noise' over I1P/Tor to mask Tx transmission. (vtnerd)
This commit is contained in:
luigi1111 2019-08-29 14:36:41 -05:00
commit 98af2e954b
No known key found for this signature in database
GPG key ID: F4ACA0183641E010
35 changed files with 3576 additions and 278 deletions

View file

@ -149,11 +149,11 @@ namespace
}
// Implement epee::net_utils::i_service_endpoint interface
virtual bool do_send(const void* ptr, size_t cb)
virtual bool do_send(epee::byte_slice message)
{
m_send_counter.inc();
boost::unique_lock<boost::mutex> lock(m_mutex);
m_last_send_data.append(reinterpret_cast<const char*>(ptr), cb);
m_last_send_data.append(reinterpret_cast<const char*>(message.data()), message.size());
return m_send_return;
}

View file

@ -56,6 +56,7 @@ set(unit_tests_sources
hmac_keccak.cpp
http.cpp
keccak.cpp
levin.cpp
logging.cpp
long_term_block_weight.cpp
lmdb.cpp

View file

@ -140,12 +140,12 @@ namespace
}
// Implement epee::net_utils::i_service_endpoint interface
virtual bool do_send(const void* ptr, size_t cb)
virtual bool do_send(epee::byte_slice message)
{
//std::cout << "test_connection::do_send()" << std::endl;
m_send_counter.inc();
boost::unique_lock<boost::mutex> lock(m_mutex);
m_last_send_data.append(reinterpret_cast<const char*>(ptr), cb);
m_last_send_data.append(reinterpret_cast<const char*>(message.data()), message.size());
return m_send_return;
}
@ -367,8 +367,8 @@ TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_process
// Parse send data
std::string send_data = conn->last_send_data();
epee::levin::bucket_head2 resp_head;
resp_head = *reinterpret_cast<const epee::levin::bucket_head2*>(send_data.data());
ASSERT_LT(sizeof(resp_head), send_data.size());
std::memcpy(std::addressof(resp_head), send_data.data(), sizeof(resp_head));
std::string out_data = send_data.substr(sizeof(resp_head));
// Check sent response
@ -425,6 +425,95 @@ TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_process
ASSERT_EQ(3, m_commands_handler.callback_counter());
}
TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_dummy)
{
// Setup
const int expected_command = 4673261;
const std::string in_data(256, 'e');
const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
const epee::byte_slice notify = epee::levin::make_notify(expected_command, epee::strspan<std::uint8_t>(in_data));
test_connection_ptr conn = create_connection();
// Test
ASSERT_TRUE(conn->m_protocol_handler.handle_recv(noise.data(), noise.size()));
// Check connection and levin_commands_handler states
ASSERT_EQ(0u, m_commands_handler.notify_counter());
ASSERT_EQ(0u, m_commands_handler.invoke_counter());
ASSERT_EQ(-1, m_commands_handler.last_command());
ASSERT_TRUE(m_commands_handler.last_in_buf().empty());
ASSERT_EQ(0u, conn->send_counter());
ASSERT_TRUE(conn->last_send_data().empty());
ASSERT_TRUE(conn->m_protocol_handler.handle_recv(notify.data(), notify.size()));
// Check connection and levin_commands_handler states
ASSERT_EQ(1u, m_commands_handler.notify_counter());
ASSERT_EQ(0u, m_commands_handler.invoke_counter());
ASSERT_EQ(expected_command, m_commands_handler.last_command());
ASSERT_EQ(in_data, m_commands_handler.last_in_buf());
ASSERT_EQ(0u, conn->send_counter());
ASSERT_TRUE(conn->last_send_data().empty());
}
TEST_F(positive_test_connection_to_levin_protocol_handler_calls, handler_processes_handle_read_as_fragment)
{
// Setup
const int expected_command = 4673261;
const int expected_fragmented_command = 46732;
const std::string in_data(256, 'e');
std::string in_fragmented_data(1024 * 4, 'c');
const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
const epee::byte_slice notify = epee::levin::make_notify(expected_command, epee::strspan<std::uint8_t>(in_data));
epee::byte_slice fragmented = epee::levin::make_fragmented_notify(noise, expected_fragmented_command, epee::strspan<std::uint8_t>(in_fragmented_data));
EXPECT_EQ(5u, fragmented.size() / 1024);
EXPECT_EQ(0u, fragmented.size() % 1024);
test_connection_ptr conn = create_connection();
while (!fragmented.empty())
{
if ((fragmented.size() / 1024) % 2 == 1)
{
ASSERT_TRUE(conn->m_protocol_handler.handle_recv(notify.data(), notify.size()));
}
ASSERT_EQ(3u - (fragmented.size() / 2048), m_commands_handler.notify_counter());
ASSERT_EQ(0u, m_commands_handler.invoke_counter());
ASSERT_EQ(expected_command, m_commands_handler.last_command());
ASSERT_EQ(in_data, m_commands_handler.last_in_buf());
ASSERT_EQ(0u, conn->send_counter());
ASSERT_TRUE(conn->last_send_data().empty());
epee::byte_slice next = fragmented.take_slice(1024);
ASSERT_TRUE(conn->m_protocol_handler.handle_recv(next.data(), next.size()));
}
in_fragmented_data.resize(((1024 - sizeof(epee::levin::bucket_head2)) * 5) - sizeof(epee::levin::bucket_head2)); // add padding zeroes
ASSERT_EQ(4u, m_commands_handler.notify_counter());
ASSERT_EQ(0u, m_commands_handler.invoke_counter());
ASSERT_EQ(expected_fragmented_command, m_commands_handler.last_command());
ASSERT_EQ(in_fragmented_data, m_commands_handler.last_in_buf());
ASSERT_EQ(0u, conn->send_counter());
ASSERT_TRUE(conn->last_send_data().empty());
ASSERT_TRUE(conn->m_protocol_handler.handle_recv(notify.data(), notify.size()));
ASSERT_EQ(5u, m_commands_handler.notify_counter());
ASSERT_EQ(0u, m_commands_handler.invoke_counter());
ASSERT_EQ(expected_command, m_commands_handler.last_command());
ASSERT_EQ(in_data, m_commands_handler.last_in_buf());
ASSERT_EQ(0u, conn->send_counter());
ASSERT_TRUE(conn->last_send_data().empty());
}
TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_big_packet_1)
{
std::string buf("yyyyyy");
@ -534,3 +623,19 @@ TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_unexpe
ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
}
TEST_F(test_levin_protocol_handler__hanle_recv_with_invalid_data, handles_short_fragment)
{
m_req_head.m_cb = 1;
m_req_head.m_flags = LEVIN_PACKET_BEGIN;
m_req_head.m_command = 0;
m_in_data.resize(1);
prepare_buf();
ASSERT_TRUE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
m_req_head.m_flags = LEVIN_PACKET_END;
prepare_buf();
ASSERT_FALSE(m_conn->m_protocol_handler.handle_recv(m_buf.data(), m_buf.size()));
}

View file

@ -44,6 +44,7 @@
#include "boost/archive/portable_binary_iarchive.hpp"
#include "boost/archive/portable_binary_oarchive.hpp"
#include "byte_slice.h"
#include "hex.h"
#include "net/net_utils_base.h"
#include "net/local_ip.h"
@ -375,6 +376,438 @@ TEST(Span, ToMutSpan)
EXPECT_EQ((std::vector<unsigned>{1, 2, 3, 4}), mut);
}
TEST(ByteSlice, Construction)
{
EXPECT_TRUE(std::is_default_constructible<epee::byte_slice>());
EXPECT_TRUE(std::is_move_constructible<epee::byte_slice>());
EXPECT_FALSE(std::is_copy_constructible<epee::byte_slice>());
EXPECT_TRUE(std::is_move_assignable<epee::byte_slice>());
EXPECT_FALSE(std::is_copy_assignable<epee::byte_slice>());
}
TEST(ByteSlice, NoExcept)
{
EXPECT_TRUE(std::is_nothrow_default_constructible<epee::byte_slice>());
EXPECT_TRUE(std::is_nothrow_move_constructible<epee::byte_slice>());
EXPECT_TRUE(std::is_nothrow_move_assignable<epee::byte_slice>());
epee::byte_slice lvalue{};
const epee::byte_slice clvalue{};
EXPECT_TRUE(noexcept(lvalue.clone()));
EXPECT_TRUE(noexcept(clvalue.clone()));
EXPECT_TRUE(noexcept(lvalue.begin()));
EXPECT_TRUE(noexcept(clvalue.begin()));
EXPECT_TRUE(noexcept(lvalue.end()));
EXPECT_TRUE(noexcept(clvalue.end()));
EXPECT_TRUE(noexcept(lvalue.cbegin()));
EXPECT_TRUE(noexcept(clvalue.cbegin()));
EXPECT_TRUE(noexcept(lvalue.cend()));
EXPECT_TRUE(noexcept(clvalue.cend()));
EXPECT_TRUE(noexcept(lvalue.empty()));
EXPECT_TRUE(noexcept(clvalue.empty()));
EXPECT_TRUE(noexcept(lvalue.data()));
EXPECT_TRUE(noexcept(clvalue.data()));
EXPECT_TRUE(noexcept(lvalue.size()));
EXPECT_TRUE(noexcept(clvalue.size()));
EXPECT_TRUE(noexcept(lvalue.remove_prefix(0)));
EXPECT_TRUE(noexcept(lvalue.take_slice(0)));
}
TEST(ByteSlice, Empty)
{
epee::byte_slice slice{};
EXPECT_EQ(slice.begin(), slice.end());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_TRUE(slice.empty());
EXPECT_EQ(0u, slice.size());
EXPECT_EQ(slice.begin(), slice.data());
EXPECT_EQ(0u, slice.get_slice(0, 0).size());
EXPECT_THROW(slice.get_slice(0, 1), std::out_of_range);
EXPECT_EQ(0u, slice.remove_prefix(1));
EXPECT_EQ(0u, slice.take_slice(1).size());
}
TEST(ByteSlice, CopySpans)
{
const epee::span<const std::uint8_t> part1 = epee::as_byte_span("this is part1");
const epee::span<const std::uint8_t> part2 = epee::as_byte_span("then part2");
const epee::span<const std::uint8_t> part3 = epee::as_byte_span("finally part3");
const epee::byte_slice slice{part1, part2, part3};
EXPECT_NE(nullptr, slice.begin());
EXPECT_NE(nullptr, slice.end());
EXPECT_NE(slice.begin(), slice.end());
EXPECT_NE(slice.cbegin(), slice.cend());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(slice.end(), slice.cend());
ASSERT_EQ(slice.size(), std::size_t(slice.end() - slice.begin()));
EXPECT_FALSE(slice.empty());
EXPECT_EQ(slice.begin(), slice.data());
ASSERT_EQ(part1.size() + part2.size() + part3.size(), slice.size());
EXPECT_TRUE(
boost::range::equal(
part1, boost::make_iterator_range(slice.begin(), slice.begin() + part1.size())
)
);
EXPECT_TRUE(
boost::range::equal(
part2, boost::make_iterator_range(slice.begin() + part1.size(), slice.end() - part3.size())
)
);
EXPECT_TRUE(
boost::range::equal(
part3, boost::make_iterator_range(slice.end() - part3.size(), slice.end())
)
);
}
TEST(ByteSlice, AdaptString)
{
static constexpr const char base_string[] = "this is an example message";
std::string adapted = base_string;
const epee::span<const uint8_t> original = epee::to_byte_span(epee::to_span(adapted));
const epee::byte_slice slice{std::move(adapted)};
EXPECT_EQ(original.begin(), slice.begin());
EXPECT_EQ(original.cbegin(), slice.cbegin());
EXPECT_EQ(original.end(), slice.end());
EXPECT_EQ(original.cend(), slice.cend());
EXPECT_FALSE(slice.empty());
EXPECT_EQ(original.data(), slice.data());
EXPECT_EQ(original.size(), slice.size());
EXPECT_TRUE(boost::range::equal(boost::string_ref{base_string}, slice));
}
TEST(ByteSlice, EmptyAdaptString)
{
epee::byte_slice slice{std::string{}};
EXPECT_EQ(slice.begin(), slice.end());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_TRUE(slice.empty());
EXPECT_EQ(0u, slice.size());
EXPECT_EQ(slice.begin(), slice.data());
EXPECT_EQ(0u, slice.get_slice(0, 0).size());
EXPECT_THROW(slice.get_slice(0, 1), std::out_of_range);
EXPECT_EQ(0u, slice.remove_prefix(1));
EXPECT_EQ(0u, slice.take_slice(1).size());
}
TEST(ByteSlice, AdaptVector)
{
static constexpr const char base_string[] = "this is an example message";
std::vector<std::uint8_t> adapted(sizeof(base_string));
ASSERT_EQ(sizeof(base_string), adapted.size());
std::memcpy(adapted.data(), base_string, sizeof(base_string));
const epee::span<const uint8_t> original = epee::to_span(adapted);
const epee::byte_slice slice{std::move(adapted)};
EXPECT_EQ(sizeof(base_string), original.size());
EXPECT_EQ(original.begin(), slice.begin());
EXPECT_EQ(original.cbegin(), slice.cbegin());
EXPECT_EQ(original.end(), slice.end());
EXPECT_EQ(original.cend(), slice.cend());
EXPECT_FALSE(slice.empty());
EXPECT_EQ(original.data(), slice.data());
EXPECT_EQ(original.size(), slice.size());
EXPECT_TRUE(boost::range::equal(base_string, slice));
}
TEST(ByteSlice, EmptyAdaptVector)
{
epee::byte_slice slice{std::vector<std::uint8_t>{}};
EXPECT_EQ(slice.begin(), slice.end());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_TRUE(slice.empty());
EXPECT_EQ(0u, slice.size());
EXPECT_EQ(slice.begin(), slice.data());
EXPECT_EQ(0u, slice.get_slice(0, 0).size());
EXPECT_THROW(slice.get_slice(0, 1), std::out_of_range);
EXPECT_EQ(0u, slice.remove_prefix(1));
EXPECT_EQ(0u, slice.take_slice(1).size());
}
TEST(ByteSlice, Move)
{
static constexpr const char base_string[] = "another example message";
epee::byte_slice slice{epee::as_byte_span(base_string)};
EXPECT_TRUE(boost::range::equal(base_string, slice));
const epee::span<const std::uint8_t> original = epee::to_span(slice);
epee::byte_slice moved{std::move(slice)};
EXPECT_TRUE(boost::range::equal(base_string, moved));
EXPECT_EQ(slice.begin(), slice.end());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_EQ(original.begin(), moved.begin());
EXPECT_EQ(moved.begin(), moved.cbegin());
EXPECT_EQ(original.end(), moved.end());
EXPECT_EQ(moved.end(), moved.cend());
EXPECT_TRUE(slice.empty());
EXPECT_EQ(slice.begin(), slice.data());
EXPECT_EQ(0u, slice.size());
EXPECT_FALSE(moved.empty());
EXPECT_EQ(moved.begin(), moved.data());
EXPECT_EQ(original.size(), moved.size());
slice = std::move(moved);
EXPECT_TRUE(boost::range::equal(base_string, slice));
EXPECT_EQ(original.begin(), slice.begin());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(original.end(), slice.end());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_FALSE(slice.empty());
EXPECT_EQ(slice.begin(), slice.data());
EXPECT_EQ(original.size(), slice.size());
EXPECT_TRUE(moved.empty());
EXPECT_EQ(moved.begin(), moved.data());
EXPECT_EQ(0u, moved.size());
}
TEST(ByteSlice, Clone)
{
static constexpr const char base_string[] = "another example message";
const epee::byte_slice slice{epee::as_byte_span(base_string)};
EXPECT_TRUE(boost::range::equal(base_string, slice));
const epee::byte_slice clone{slice.clone()};
EXPECT_TRUE(boost::range::equal(base_string, clone));
EXPECT_EQ(slice.begin(), clone.begin());
EXPECT_EQ(slice.cbegin(), clone.cbegin());
EXPECT_EQ(slice.end(), clone.end());
EXPECT_EQ(slice.cend(), clone.cend());
EXPECT_FALSE(slice.empty());
EXPECT_FALSE(clone.empty());
EXPECT_EQ(slice.cbegin(), slice.data());
EXPECT_EQ(slice.data(), clone.data());
EXPECT_EQ(sizeof(base_string), slice.size());
EXPECT_EQ(slice.size(), clone.size());
}
TEST(ByteSlice, RemovePrefix)
{
static constexpr const char base_string[] = "another example message";
static constexpr std::size_t remove_size = sizeof("another");
static constexpr std::size_t remaining = sizeof(base_string) - remove_size;
epee::byte_slice slice{epee::as_byte_span(base_string)};
EXPECT_TRUE(boost::range::equal(base_string, slice));
const epee::span<const std::uint8_t> original = epee::to_span(slice);
EXPECT_EQ(remove_size, slice.remove_prefix(remove_size));
EXPECT_EQ(original.begin() + remove_size, slice.begin());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(original.end(), slice.end());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_FALSE(slice.empty());
EXPECT_EQ(slice.cbegin(), slice.data());
EXPECT_EQ(remaining, slice.size());
// touch original pointers to check "free" status
EXPECT_TRUE(boost::range::equal(base_string, original));
EXPECT_EQ(remaining, slice.remove_prefix(remaining + 1));
EXPECT_EQ(slice.begin(), slice.end());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_TRUE(slice.empty());
EXPECT_EQ(slice.cbegin(), slice.data());
EXPECT_EQ(0, slice.size());
}
TEST(ByteSlice, TakeSlice)
{
static constexpr const char base_string[] = "another example message";
static constexpr std::size_t remove_size = sizeof("another");
static constexpr std::size_t remaining = sizeof(base_string) - remove_size;
epee::byte_slice slice{epee::as_byte_span(base_string)};
EXPECT_TRUE(boost::range::equal(base_string, slice));
const epee::span<const std::uint8_t> original = epee::to_span(slice);
const epee::byte_slice slice2 = slice.take_slice(remove_size);
EXPECT_EQ(original.begin() + remove_size, slice.begin());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(original.end(), slice.end());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_EQ(original.begin(), slice2.begin());
EXPECT_EQ(slice2.begin(), slice2.cbegin());
EXPECT_EQ(original.begin() + remove_size, slice2.end());
EXPECT_EQ(slice2.end(), slice2.cend());
EXPECT_FALSE(slice.empty());
EXPECT_EQ(slice.cbegin(), slice.data());
EXPECT_EQ(remaining, slice.size());
EXPECT_FALSE(slice2.empty());
EXPECT_EQ(slice2.cbegin(), slice2.data());
EXPECT_EQ(remove_size, slice2.size());
// touch original pointers to check "free" status
EXPECT_TRUE(boost::range::equal(base_string, original));
const epee::byte_slice slice3 = slice.take_slice(remaining + 1);
EXPECT_EQ(slice.begin(), slice.end());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_EQ(original.begin(), slice2.begin());
EXPECT_EQ(slice2.begin(), slice2.cbegin());
EXPECT_EQ(original.begin() + remove_size, slice2.end());
EXPECT_EQ(slice2.end(), slice2.cend());
EXPECT_EQ(slice2.end(), slice3.begin());
EXPECT_EQ(slice3.begin(), slice3.cbegin());
EXPECT_EQ(original.end(), slice3.end());
EXPECT_EQ(slice3.end(), slice3.cend());
EXPECT_TRUE(slice.empty());
EXPECT_EQ(slice.cbegin(), slice.data());
EXPECT_EQ(0, slice.size());
EXPECT_FALSE(slice2.empty());
EXPECT_EQ(slice2.cbegin(), slice2.data());
EXPECT_EQ(remove_size, slice2.size());
EXPECT_FALSE(slice3.empty());
EXPECT_EQ(slice3.cbegin(), slice3.data());
EXPECT_EQ(remaining, slice3.size());
// touch original pointers to check "free" status
slice = nullptr;
EXPECT_TRUE(boost::range::equal(base_string, original));
}
TEST(ByteSlice, GetSlice)
{
static constexpr const char base_string[] = "another example message";
static constexpr std::size_t get_size = sizeof("another");
static constexpr std::size_t get2_size = sizeof(base_string) - get_size;
epee::span<const std::uint8_t> original{};
epee::byte_slice slice2{};
epee::byte_slice slice3{};
// make sure get_slice increments ref count
{
const epee::byte_slice slice{epee::as_byte_span(base_string)};
EXPECT_TRUE(boost::range::equal(base_string, slice));
original = epee::to_span(slice);
slice2 = slice.get_slice(0, get_size);
EXPECT_EQ(original.begin(), slice.begin());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(original.end(), slice.end());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_EQ(original.begin(), slice2.begin());
EXPECT_EQ(slice2.begin(), slice2.cbegin());
EXPECT_EQ(original.begin() + get_size, slice2.end());
EXPECT_EQ(slice2.end(), slice2.cend());
EXPECT_FALSE(slice.empty());
EXPECT_EQ(slice.cbegin(), slice.data());
EXPECT_EQ(original.size(), slice.size());
EXPECT_FALSE(slice2.empty());
EXPECT_EQ(slice2.cbegin(), slice2.data());
EXPECT_EQ(get_size, slice2.size());
// touch original pointers to check "free" status
EXPECT_TRUE(boost::range::equal(base_string, original));
slice3 = slice.get_slice(get_size, sizeof(base_string));
EXPECT_EQ(original.begin(), slice.begin());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(original.end(), slice.end());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_EQ(original.begin(), slice2.begin());
EXPECT_EQ(slice2.begin(), slice2.cbegin());
EXPECT_EQ(original.begin() + get_size, slice2.end());
EXPECT_EQ(slice2.end(), slice2.cend());
EXPECT_EQ(slice2.end(), slice3.begin());
EXPECT_EQ(slice3.begin(), slice3.cbegin());
EXPECT_EQ(original.end(), slice3.end());
EXPECT_EQ(slice3.end(), slice3.cend());
EXPECT_FALSE(slice.empty());
EXPECT_EQ(slice.cbegin(), slice.data());
EXPECT_EQ(original.size(), slice.size());
EXPECT_FALSE(slice2.empty());
EXPECT_EQ(slice2.cbegin(), slice2.data());
EXPECT_EQ(get_size, slice2.size());
EXPECT_FALSE(slice3.empty());
EXPECT_EQ(slice3.cbegin(), slice3.data());
EXPECT_EQ(get2_size, slice3.size());
EXPECT_THROW(slice.get_slice(1, 0), std::out_of_range);
EXPECT_THROW(slice.get_slice(0, sizeof(base_string) + 1), std::out_of_range);
EXPECT_THROW(slice.get_slice(sizeof(base_string) + 1, sizeof(base_string) + 1), std::out_of_range);
EXPECT_TRUE(slice.get_slice(sizeof(base_string), sizeof(base_string)).empty());
EXPECT_EQ(original.begin(), slice.begin());
EXPECT_EQ(slice.begin(), slice.cbegin());
EXPECT_EQ(original.end(), slice.end());
EXPECT_EQ(slice.end(), slice.cend());
EXPECT_FALSE(slice.empty());
EXPECT_EQ(slice.cbegin(), slice.data());
EXPECT_EQ(original.size(), slice.size());
}
// touch original pointers to check "free" status
EXPECT_TRUE(boost::range::equal(base_string, original));
}
TEST(ToHex, String)
{
EXPECT_TRUE(epee::to_hex::string(nullptr).empty());

586
tests/unit_tests/levin.cpp Normal file
View file

@ -0,0 +1,586 @@
// Copyright (c) 2019, 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 <algorithm>
#include <boost/uuid/nil_generator.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid.hpp>
#include <cstring>
#include <gtest/gtest.h>
#include <limits>
#include <set>
#include "byte_slice.h"
#include "crypto/crypto.h"
#include "cryptonote_basic/connection_context.h"
#include "cryptonote_protocol/cryptonote_protocol_defs.h"
#include "cryptonote_protocol/levin_notify.h"
#include "int-util.h"
#include "p2p/net_node.h"
#include "net/dandelionpp.h"
#include "net/levin_base.h"
#include "span.h"
namespace
{
class test_endpoint final : public epee::net_utils::i_service_endpoint
{
boost::asio::io_service& io_service_;
std::size_t ref_count_;
virtual bool do_send(epee::byte_slice message) override final
{
send_queue_.push_back(std::move(message));
return true;
}
virtual bool close() override final
{
return true;
}
virtual bool send_done() override final
{
throw std::logic_error{"send_done not implemented"};
}
virtual bool call_run_once_service_io() override final
{
return io_service_.run_one();
}
virtual bool request_callback() override final
{
throw std::logic_error{"request_callback not implemented"};
}
virtual boost::asio::io_service& get_io_service() override final
{
return io_service_;
}
virtual bool add_ref() override final
{
++ref_count_;
return true;
}
virtual bool release() override final
{
--ref_count_;
return true;
}
public:
test_endpoint(boost::asio::io_service& io_service)
: epee::net_utils::i_service_endpoint(),
io_service_(io_service),
ref_count_(0),
send_queue_()
{}
virtual ~test_endpoint() noexcept(false) override final
{
EXPECT_EQ(0u, ref_count_);
}
std::deque<epee::byte_slice> send_queue_;
};
class test_connection
{
test_endpoint endpoint_;
cryptonote::levin::detail::p2p_context context_;
epee::levin::async_protocol_handler<cryptonote::levin::detail::p2p_context> handler_;
public:
test_connection(boost::asio::io_service& io_service, cryptonote::levin::connections& connections, boost::uuids::random_generator& random_generator)
: context_(),
endpoint_(io_service),
handler_(std::addressof(endpoint_), connections, context_)
{
const_cast<boost::uuids::uuid&>(context_.m_connection_id) = random_generator();
handler_.after_init_connection();
}
//\return Number of messages processed
std::size_t process_send_queue()
{
std::size_t count = 0;
for ( ; !endpoint_.send_queue_.empty(); ++count, endpoint_.send_queue_.pop_front())
{
// invalid messages shoudn't be possible in this test;
EXPECT_TRUE(handler_.handle_recv(endpoint_.send_queue_.front().data(), endpoint_.send_queue_.front().size()));
}
return count;
}
const boost::uuids::uuid& get_id() const noexcept
{
return context_.m_connection_id;
}
};
struct received_message
{
boost::uuids::uuid connection;
int command;
std::string payload;
};
class test_receiver : public epee::levin::levin_commands_handler<cryptonote::levin::detail::p2p_context>
{
std::deque<received_message> invoked_;
std::deque<received_message> notified_;
template<typename T>
static std::pair<boost::uuids::uuid, typename T::request> get_message(std::deque<received_message>& queue)
{
if (queue.empty())
throw std::logic_error{"Queue has no received messges"};
if (queue.front().command != T::ID)
throw std::logic_error{"Unexpected ID at front of message queue"};
epee::serialization::portable_storage storage{};
if(!storage.load_from_binary(epee::strspan<std::uint8_t>(queue.front().payload)))
throw std::logic_error{"Unable to parse epee binary format"};
typename T::request request{};
if (!request.load(storage))
throw std::logic_error{"Unable to load into expected request"};
boost::uuids::uuid connection = queue.front().connection;
queue.pop_front();
return {connection, std::move(request)};
}
virtual int invoke(int command, const epee::span<const uint8_t> in_buff, std::string& buff_out, cryptonote::levin::detail::p2p_context& context) override final
{
buff_out.clear();
invoked_.push_back(
{context.m_connection_id, command, std::string{reinterpret_cast<const char*>(in_buff.data()), in_buff.size()}}
);
return 1;
}
virtual int notify(int command, const epee::span<const uint8_t> in_buff, cryptonote::levin::detail::p2p_context& context) override final
{
notified_.push_back(
{context.m_connection_id, command, std::string{reinterpret_cast<const char*>(in_buff.data()), in_buff.size()}}
);
return 1;
}
virtual void callback(cryptonote::levin::detail::p2p_context& context) override final
{}
virtual void on_connection_new(cryptonote::levin::detail::p2p_context&) override final
{}
virtual void on_connection_close(cryptonote::levin::detail::p2p_context&) override final
{}
public:
test_receiver()
: epee::levin::levin_commands_handler<cryptonote::levin::detail::p2p_context>(),
invoked_(),
notified_()
{}
virtual ~test_receiver() noexcept override final{}
std::size_t invoked_size() const noexcept
{
return invoked_.size();
}
std::size_t notified_size() const noexcept
{
return notified_.size();
}
template<typename T>
std::pair<boost::uuids::uuid, typename T::request> get_invoked()
{
return get_message<T>(invoked_);
}
template<typename T>
std::pair<boost::uuids::uuid, typename T::request> get_notification()
{
return get_message<T>(notified_);
}
};
class levin_notify : public ::testing::Test
{
const std::shared_ptr<cryptonote::levin::connections> connections_;
std::set<boost::uuids::uuid> connection_ids_;
public:
levin_notify()
: ::testing::Test(),
connections_(std::make_shared<cryptonote::levin::connections>()),
connection_ids_(),
random_generator_(),
io_service_(),
receiver_(),
contexts_()
{
connections_->set_handler(std::addressof(receiver_), nullptr);
}
virtual void TearDown() override final
{
EXPECT_EQ(0u, receiver_.invoked_size());
EXPECT_EQ(0u, receiver_.notified_size());
}
void add_connection()
{
contexts_.emplace_back(io_service_, *connections_, random_generator_);
EXPECT_TRUE(connection_ids_.emplace(contexts_.back().get_id()).second);
EXPECT_EQ(connection_ids_.size(), connections_->get_connections_count());
}
cryptonote::levin::notify make_notifier(const std::size_t noise_size)
{
epee::byte_slice noise = nullptr;
if (noise_size)
noise = epee::levin::make_noise_notify(noise_size);
return cryptonote::levin::notify{io_service_, connections_, std::move(noise)};
}
boost::uuids::random_generator random_generator_;
boost::asio::io_service io_service_;
test_receiver receiver_;
std::deque<test_connection> contexts_;
};
}
TEST(make_header, no_expect_return)
{
static constexpr const std::size_t max_length = std::numeric_limits<std::size_t>::max();
const epee::levin::bucket_head2 header1 = epee::levin::make_header(1024, max_length, 5601, false);
EXPECT_EQ(SWAP64LE(LEVIN_SIGNATURE), header1.m_signature);
EXPECT_FALSE(header1.m_have_to_return_data);
EXPECT_EQ(SWAP64LE(max_length), header1.m_cb);
EXPECT_EQ(SWAP32LE(1024), header1.m_command);
EXPECT_EQ(SWAP32LE(LEVIN_PROTOCOL_VER_1), header1.m_protocol_version);
EXPECT_EQ(SWAP32LE(5601), header1.m_flags);
}
TEST(make_header, expect_return)
{
const epee::levin::bucket_head2 header1 = epee::levin::make_header(65535, 0, 0, true);
EXPECT_EQ(SWAP64LE(LEVIN_SIGNATURE), header1.m_signature);
EXPECT_TRUE(header1.m_have_to_return_data);
EXPECT_EQ(0u, header1.m_cb);
EXPECT_EQ(SWAP32LE(65535), header1.m_command);
EXPECT_EQ(SWAP32LE(LEVIN_PROTOCOL_VER_1), header1.m_protocol_version);
EXPECT_EQ(0u, header1.m_flags);
}
TEST(make_notify, empty_payload)
{
const epee::byte_slice message = epee::levin::make_notify(443, nullptr);
const epee::levin::bucket_head2 header =
epee::levin::make_header(443, 0, LEVIN_PACKET_REQUEST, false);
ASSERT_EQ(sizeof(header), message.size());
EXPECT_TRUE(std::memcmp(std::addressof(header), message.data(), sizeof(header)) == 0);
}
TEST(make_notify, with_payload)
{
std::string bytes(100, 'a');
std::generate(bytes.begin(), bytes.end(), crypto::random_device{});
const epee::byte_slice message = epee::levin::make_notify(443, epee::strspan<std::uint8_t>(bytes));
const epee::levin::bucket_head2 header =
epee::levin::make_header(443, bytes.size(), LEVIN_PACKET_REQUEST, false);
ASSERT_EQ(sizeof(header) + bytes.size(), message.size());
EXPECT_TRUE(std::memcmp(std::addressof(header), message.data(), sizeof(header)) == 0);
EXPECT_TRUE(std::memcmp(bytes.data(), message.data() + sizeof(header), bytes.size()) == 0);
}
TEST(make_noise, invalid)
{
EXPECT_TRUE(epee::levin::make_noise_notify(sizeof(epee::levin::bucket_head2) - 1).empty());
}
TEST(make_noise, valid)
{
static constexpr const std::uint32_t flags =
LEVIN_PACKET_BEGIN | LEVIN_PACKET_END;
const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
const epee::levin::bucket_head2 header =
epee::levin::make_header(0, 1024 - sizeof(epee::levin::bucket_head2), flags, false);
ASSERT_EQ(1024, noise.size());
EXPECT_TRUE(std::memcmp(std::addressof(header), noise.data(), sizeof(header)) == 0);
EXPECT_EQ(1024 - sizeof(header), std::count(noise.cbegin() + sizeof(header), noise.cend(), 0));
}
TEST(make_fragment, invalid)
{
EXPECT_TRUE(epee::levin::make_fragmented_notify(nullptr, 0, nullptr).empty());
}
TEST(make_fragment, single)
{
const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
const epee::byte_slice fragment = epee::levin::make_fragmented_notify(noise, 11, nullptr);
const epee::levin::bucket_head2 header =
epee::levin::make_header(11, 1024 - sizeof(epee::levin::bucket_head2), LEVIN_PACKET_REQUEST, false);
EXPECT_EQ(1024, noise.size());
ASSERT_EQ(1024, fragment.size());
EXPECT_TRUE(std::memcmp(std::addressof(header), fragment.data(), sizeof(header)) == 0);
EXPECT_EQ(1024 - sizeof(header), std::count(noise.cbegin() + sizeof(header), noise.cend(), 0));
}
TEST(make_fragment, multiple)
{
std::string bytes(1024 * 3 - 150, 'a');
std::generate(bytes.begin(), bytes.end(), crypto::random_device{});
const epee::byte_slice noise = epee::levin::make_noise_notify(1024);
epee::byte_slice fragment = epee::levin::make_fragmented_notify(noise, 114, epee::strspan<std::uint8_t>(bytes));
epee::levin::bucket_head2 header =
epee::levin::make_header(0, 1024 - sizeof(epee::levin::bucket_head2), LEVIN_PACKET_BEGIN, false);
ASSERT_LE(sizeof(header), fragment.size());
EXPECT_TRUE(std::memcmp(std::addressof(header), fragment.data(), sizeof(header)) == 0);
fragment.take_slice(sizeof(header));
header.m_flags = LEVIN_PACKET_REQUEST;
header.m_cb = bytes.size();
header.m_command = 114;
ASSERT_LE(sizeof(header), fragment.size());
EXPECT_TRUE(std::memcmp(std::addressof(header), fragment.data(), sizeof(header)) == 0);
fragment.take_slice(sizeof(header));
ASSERT_LE(bytes.size(), fragment.size());
EXPECT_TRUE(std::memcmp(bytes.data(), fragment.data(), 1024 - sizeof(header) * 2) == 0);
bytes.erase(0, 1024 - sizeof(header) * 2);
fragment.take_slice(1024 - sizeof(header) * 2);
header.m_flags = 0;
header.m_cb = 1024 - sizeof(header);
header.m_command = 0;
ASSERT_LE(sizeof(header), fragment.size());
EXPECT_TRUE(std::memcmp(std::addressof(header), fragment.data(), sizeof(header)) == 0);
fragment.take_slice(sizeof(header));
ASSERT_LE(bytes.size(), fragment.size());
EXPECT_TRUE(std::memcmp(bytes.data(), fragment.data(), 1024 - sizeof(header)) == 0);
bytes.erase(0, 1024 - sizeof(header));
fragment.take_slice(1024 - sizeof(header));
header.m_flags = LEVIN_PACKET_END;
ASSERT_LE(sizeof(header), fragment.size());
EXPECT_TRUE(std::memcmp(std::addressof(header), fragment.data(), sizeof(header)) == 0);
fragment.take_slice(sizeof(header));
EXPECT_TRUE(std::memcmp(bytes.data(), fragment.data(), bytes.size()) == 0);
fragment.take_slice(bytes.size());
EXPECT_EQ(18, std::count(fragment.cbegin(), fragment.cend(), 0));
}
TEST_F(levin_notify, defaulted)
{
cryptonote::levin::notify notifier{};
{
const auto status = notifier.get_status();
EXPECT_FALSE(status.has_noise);
EXPECT_FALSE(status.connections_filled);
}
EXPECT_FALSE(notifier.send_txs({}, random_generator_(), false));
}
TEST_F(levin_notify, flood)
{
cryptonote::levin::notify notifier = make_notifier(0);
for (unsigned count = 0; count < 10; ++count)
add_connection();
{
const auto status = notifier.get_status();
EXPECT_FALSE(status.has_noise);
EXPECT_FALSE(status.connections_filled);
}
notifier.new_out_connection();
io_service_.poll();
{
const auto status = notifier.get_status();
EXPECT_FALSE(status.has_noise);
EXPECT_FALSE(status.connections_filled); // not tracked
}
std::vector<cryptonote::blobdata> txs(2);
txs[0].resize(100, 'e');
txs[1].resize(200, 'f');
ASSERT_EQ(10u, contexts_.size());
{
auto context = contexts_.begin();
EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), false));
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
EXPECT_EQ(0u, context->process_send_queue());
for (++context; context != contexts_.end(); ++context)
EXPECT_EQ(1u, context->process_send_queue());
ASSERT_EQ(9u, receiver_.notified_size());
for (unsigned count = 0; count < 9; ++count)
{
auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
EXPECT_EQ(txs, notification.txs);
EXPECT_TRUE(notification._.empty());
}
}
ASSERT_EQ(10u, contexts_.size());
{
auto context = contexts_.begin();
EXPECT_TRUE(notifier.send_txs(txs, context->get_id(), true));
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
EXPECT_EQ(0u, context->process_send_queue());
for (++context; context != contexts_.end(); ++context)
EXPECT_EQ(1u, context->process_send_queue());
ASSERT_EQ(9u, receiver_.notified_size());
for (unsigned count = 0; count < 9; ++count)
{
auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
EXPECT_EQ(txs, notification.txs);
EXPECT_FALSE(notification._.empty());
}
}
}
TEST_F(levin_notify, noise)
{
for (unsigned count = 0; count < 10; ++count)
add_connection();
std::vector<cryptonote::blobdata> txs(1);
txs[0].resize(1900, 'h');
const boost::uuids::uuid incoming_id = random_generator_();
cryptonote::levin::notify notifier = make_notifier(2048);
{
const auto status = notifier.get_status();
EXPECT_TRUE(status.has_noise);
EXPECT_FALSE(status.connections_filled);
}
ASSERT_LT(0u, io_service_.poll());
{
const auto status = notifier.get_status();
EXPECT_TRUE(status.has_noise);
EXPECT_TRUE(status.connections_filled);
}
notifier.run_stems();
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
{
std::size_t sent = 0;
for (auto& context : contexts_)
sent += context.process_send_queue();
EXPECT_EQ(2u, sent);
EXPECT_EQ(0u, receiver_.notified_size());
}
EXPECT_TRUE(notifier.send_txs(txs, incoming_id, false));
notifier.run_stems();
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
{
std::size_t sent = 0;
for (auto& context : contexts_)
sent += context.process_send_queue();
ASSERT_EQ(2u, sent);
while (sent--)
{
auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
EXPECT_EQ(txs, notification.txs);
EXPECT_TRUE(notification._.empty());
}
}
txs[0].resize(3000, 'r');
EXPECT_TRUE(notifier.send_txs(txs, incoming_id, true));
notifier.run_stems();
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
{
std::size_t sent = 0;
for (auto& context : contexts_)
sent += context.process_send_queue();
EXPECT_EQ(2u, sent);
EXPECT_EQ(0u, receiver_.notified_size());
}
notifier.run_stems();
io_service_.reset();
ASSERT_LT(0u, io_service_.poll());
{
std::size_t sent = 0;
for (auto& context : contexts_)
sent += context.process_send_queue();
ASSERT_EQ(2u, sent);
while (sent--)
{
auto notification = receiver_.get_notification<cryptonote::NOTIFY_NEW_TRANSACTIONS>().second;
EXPECT_EQ(txs, notification.txs);
EXPECT_TRUE(notification._.empty());
}
}
}

View file

@ -26,6 +26,7 @@
// 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 <algorithm>
#include <atomic>
#include <boost/archive/portable_binary_oarchive.hpp>
#include <boost/archive/portable_binary_iarchive.hpp>
@ -36,13 +37,22 @@
#include <boost/asio/steady_timer.hpp>
#include <boost/asio/write.hpp>
#include <boost/endian/conversion.hpp>
#include <boost/range/adaptor/sliced.hpp>
#include <boost/range/combine.hpp>
#include <boost/system/error_code.hpp>
#include <boost/thread/thread.hpp>
#include <boost/uuid/nil_generator.hpp>
#include <boost/uuid/random_generator.hpp>
#include <boost/uuid/uuid.hpp>
#include <cstdint>
#include <cstring>
#include <functional>
#include <gtest/gtest.h>
#include <map>
#include <memory>
#include <type_traits>
#include "net/dandelionpp.h"
#include "net/error.h"
#include "net/net_utils_base.h"
#include "net/socks.h"
@ -857,3 +867,389 @@ TEST(socks_connector, timeout)
EXPECT_THROW(sock.get().is_open(), boost::system::system_error);
}
TEST(dandelionpp_map, traits)
{
EXPECT_TRUE(std::is_default_constructible<net::dandelionpp::connection_map>());
EXPECT_TRUE(std::is_move_constructible<net::dandelionpp::connection_map>());
EXPECT_TRUE(std::is_move_assignable<net::dandelionpp::connection_map>());
EXPECT_FALSE(std::is_copy_constructible<net::dandelionpp::connection_map>());
EXPECT_FALSE(std::is_copy_assignable<net::dandelionpp::connection_map>());
}
TEST(dandelionpp_map, empty)
{
const net::dandelionpp::connection_map mapper{};
EXPECT_EQ(mapper.begin(), mapper.end());
EXPECT_EQ(0u, mapper.size());
const net::dandelionpp::connection_map cloned = mapper.clone();
EXPECT_EQ(cloned.begin(), cloned.end());
EXPECT_EQ(0u, cloned.size());
}
TEST(dandelionpp_map, zero_stems)
{
std::vector<boost::uuids::uuid> connections{6};
std::generate(connections.begin(), connections.end(), boost::uuids::random_generator{});
net::dandelionpp::connection_map mapper{connections, 0};
EXPECT_EQ(mapper.begin(), mapper.end());
EXPECT_EQ(0u, mapper.size());
for (const boost::uuids::uuid& connection : connections)
EXPECT_TRUE(mapper.get_stem(connection).is_nil());
EXPECT_FALSE(mapper.update(connections));
EXPECT_EQ(mapper.begin(), mapper.end());
EXPECT_EQ(0u, mapper.size());
for (const boost::uuids::uuid& connection : connections)
EXPECT_TRUE(mapper.get_stem(connection).is_nil());
const net::dandelionpp::connection_map cloned = mapper.clone();
EXPECT_EQ(cloned.end(), cloned.begin());
EXPECT_EQ(0u, cloned.size());
}
TEST(dandelionpp_map, dropped_connection)
{
std::vector<boost::uuids::uuid> connections{6};
std::generate(connections.begin(), connections.end(), boost::uuids::random_generator{});
std::sort(connections.begin(), connections.end());
// select 3 of 6 outgoing connections
net::dandelionpp::connection_map mapper{connections, 3};
EXPECT_EQ(3u, mapper.size());
EXPECT_EQ(3, mapper.end() - mapper.begin());
{
std::set<boost::uuids::uuid> used;
for (const boost::uuids::uuid& connection : mapper)
{
EXPECT_TRUE(used.insert(connection).second);
EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection));
}
}
{
const net::dandelionpp::connection_map cloned = mapper.clone();
EXPECT_EQ(3u, cloned.size());
ASSERT_EQ(mapper.end() - mapper.begin(), cloned.end() - cloned.begin());
for (auto elem : boost::combine(mapper, cloned))
EXPECT_EQ(boost::get<0>(elem), boost::get<1>(elem));
}
EXPECT_FALSE(mapper.update(connections));
EXPECT_EQ(3u, mapper.size());
ASSERT_EQ(3, mapper.end() - mapper.begin());
{
std::set<boost::uuids::uuid> used;
for (const boost::uuids::uuid& connection : mapper)
{
EXPECT_FALSE(connection.is_nil());
EXPECT_TRUE(used.insert(connection).second);
EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection));
}
}
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(), boost::uuids::random_generator{});
{
std::map<boost::uuids::uuid, std::size_t> used;
std::multimap<boost::uuids::uuid, boost::uuids::uuid> inverse_mapping;
for (const boost::uuids::uuid& connection : in_connections)
{
const boost::uuids::uuid out = mapper.get_stem(connection);
EXPECT_FALSE(out.is_nil());
EXPECT_TRUE(mapping.emplace(connection, out).second);
inverse_mapping.emplace(out, connection);
used[out]++;
}
EXPECT_EQ(3u, used.size());
for (const std::pair<boost::uuids::uuid, std::size_t>& entry : used)
EXPECT_EQ(3u, entry.second);
for (const boost::uuids::uuid& connection : in_connections)
EXPECT_EQ(mapping[connection], mapper.get_stem(connection));
// drop 1 connection, and select replacement from 1 of unused 3.
const boost::uuids::uuid lost_connection = *(++mapper.begin());
const auto elem = std::lower_bound(connections.begin(), connections.end(), lost_connection);
ASSERT_NE(connections.end(), elem);
ASSERT_EQ(lost_connection, *elem);
connections.erase(elem);
EXPECT_TRUE(mapper.update(connections));
EXPECT_EQ(3u, mapper.size());
ASSERT_EQ(3, mapper.end() - mapper.begin());
for (const boost::uuids::uuid& connection : mapper)
{
EXPECT_FALSE(connection.is_nil());
EXPECT_NE(lost_connection, connection);
}
const boost::uuids::uuid newly_mapped = *(++mapper.begin());
EXPECT_FALSE(newly_mapped.is_nil());
EXPECT_NE(lost_connection, newly_mapped);
for (auto elems = inverse_mapping.equal_range(lost_connection); elems.first != elems.second; ++elems.first)
mapping[elems.first->second] = newly_mapped;
}
{
const net::dandelionpp::connection_map cloned = mapper.clone();
EXPECT_EQ(3u, cloned.size());
ASSERT_EQ(mapper.end() - mapper.begin(), cloned.end() - cloned.begin());
for (auto elem : boost::combine(mapper, cloned))
EXPECT_EQ(boost::get<0>(elem), boost::get<1>(elem));
}
// mappings should remain evenly distributed amongst 2, with 3 sitting in waiting
{
std::set<boost::uuids::uuid> used;
for (const boost::uuids::uuid& connection : mapper)
{
EXPECT_FALSE(connection.is_nil());
EXPECT_TRUE(used.insert(connection).second);
EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection));
}
}
{
std::map<boost::uuids::uuid, std::size_t> used;
for (const boost::uuids::uuid& connection : in_connections)
{
const boost::uuids::uuid& out = mapper.get_stem(connection);
EXPECT_FALSE(out.is_nil());
EXPECT_EQ(mapping[connection], out);
used[out]++;
}
EXPECT_EQ(3u, used.size());
for (const std::pair<boost::uuids::uuid, std::size_t>& entry : used)
EXPECT_EQ(3u, entry.second);
}
{
const net::dandelionpp::connection_map cloned = mapper.clone();
EXPECT_EQ(3u, cloned.size());
ASSERT_EQ(mapper.end() - mapper.begin(), cloned.end() - cloned.begin());
for (auto elem : boost::combine(mapper, cloned))
EXPECT_EQ(boost::get<0>(elem), boost::get<1>(elem));
}
}
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);
std::sort(connections.begin(), connections.end());
// select 3 of 3 outgoing connections
net::dandelionpp::connection_map mapper{connections, 3};
EXPECT_EQ(3u, mapper.size());
EXPECT_EQ(3, mapper.end() - mapper.begin());
{
std::set<boost::uuids::uuid> used;
for (const boost::uuids::uuid& connection : mapper)
{
EXPECT_FALSE(connection.is_nil());
EXPECT_TRUE(used.insert(connection).second);
EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection));
}
}
EXPECT_FALSE(mapper.update(connections));
EXPECT_EQ(3u, mapper.size());
ASSERT_EQ(3, mapper.end() - mapper.begin());
{
std::set<boost::uuids::uuid> used;
for (const boost::uuids::uuid& connection : mapper)
{
EXPECT_FALSE(connection.is_nil());
EXPECT_TRUE(used.insert(connection).second);
EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection));
}
}
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);
{
std::map<boost::uuids::uuid, std::size_t> used;
std::multimap<boost::uuids::uuid, boost::uuids::uuid> inverse_mapping;
for (const boost::uuids::uuid& connection : in_connections)
{
const boost::uuids::uuid out = mapper.get_stem(connection);
EXPECT_FALSE(out.is_nil());
EXPECT_TRUE(mapping.emplace(connection, out).second);
inverse_mapping.emplace(out, connection);
used[out]++;
}
EXPECT_EQ(3u, used.size());
for (const std::pair<boost::uuids::uuid, std::size_t>& entry : used)
EXPECT_EQ(3u, entry.second);
for (const boost::uuids::uuid& connection : in_connections)
EXPECT_EQ(mapping[connection], mapper.get_stem(connection));
// drop 1 connection leaving "hole"
const boost::uuids::uuid lost_connection = *(++mapper.begin());
const auto elem = std::lower_bound(connections.begin(), connections.end(), lost_connection);
ASSERT_NE(connections.end(), elem);
ASSERT_EQ(lost_connection, *elem);
connections.erase(elem);
EXPECT_TRUE(mapper.update(connections));
EXPECT_EQ(2u, mapper.size());
EXPECT_EQ(3, mapper.end() - mapper.begin());
for (auto elems = inverse_mapping.equal_range(lost_connection); elems.first != elems.second; ++elems.first)
mapping[elems.first->second] = boost::uuids::nil_uuid();
}
// remap 3 connections and map 1 new connection to 2 remaining out connections
in_connections.resize(10);
in_connections[9] = random_uuid();
{
std::map<boost::uuids::uuid, std::size_t> used;
for (const boost::uuids::uuid& connection : in_connections)
{
const boost::uuids::uuid& out = mapper.get_stem(connection);
EXPECT_FALSE(out.is_nil());
used[out]++;
boost::uuids::uuid& expected = mapping[connection];
if (!expected.is_nil())
EXPECT_EQ(expected, out);
else
expected = out;
}
EXPECT_EQ(2u, used.size());
for (const std::pair<boost::uuids::uuid, std::size_t>& entry : used)
EXPECT_EQ(5u, entry.second);
}
// select 3 of 3 connections but do not remap existing links
connections.resize(3);
connections[2] = random_uuid();
EXPECT_TRUE(mapper.update(connections));
EXPECT_EQ(3u, mapper.size());
EXPECT_EQ(3, mapper.end() - mapper.begin());
{
std::map<boost::uuids::uuid, std::size_t> used;
for (const boost::uuids::uuid& connection : in_connections)
{
const boost::uuids::uuid& out = mapper.get_stem(connection);
EXPECT_FALSE(out.is_nil());
used[out]++;
EXPECT_EQ(mapping[connection], out);
}
EXPECT_EQ(2u, used.size());
for (const std::pair<boost::uuids::uuid, std::size_t>& entry : used)
EXPECT_EQ(5u, entry.second);
}
// map 8 new incoming connections across 3 outgoing links
in_connections.resize(18);
std::generate(in_connections.begin() + 10, in_connections.end(), random_uuid);
{
std::map<boost::uuids::uuid, std::size_t> used;
for (const boost::uuids::uuid& connection : in_connections)
{
const boost::uuids::uuid& out = mapper.get_stem(connection);
EXPECT_FALSE(out.is_nil());
used[out]++;
boost::uuids::uuid& expected = mapping[connection];
if (!expected.is_nil())
EXPECT_EQ(expected, out);
else
expected = out;
}
EXPECT_EQ(3u, used.size());
for (const std::pair<boost::uuids::uuid, std::size_t>& entry : used)
EXPECT_EQ(6u, entry.second);
}
}
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);
std::sort(connections.begin(), connections.end());
// select 3 of 8 outgoing connections
net::dandelionpp::connection_map mapper{connections, 3};
EXPECT_EQ(3u, mapper.size());
EXPECT_EQ(3, mapper.end() - mapper.begin());
{
std::set<boost::uuids::uuid> used;
for (const boost::uuids::uuid& connection : mapper)
{
EXPECT_FALSE(connection.is_nil());
EXPECT_TRUE(used.insert(connection).second);
EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection));
}
}
EXPECT_FALSE(mapper.update(connections));
EXPECT_EQ(3u, mapper.size());
ASSERT_EQ(3, mapper.end() - mapper.begin());
{
std::set<boost::uuids::uuid> used;
for (const boost::uuids::uuid& connection : mapper)
{
EXPECT_FALSE(connection.is_nil());
EXPECT_TRUE(used.insert(connection).second);
EXPECT_TRUE(std::binary_search(connections.begin(), connections.end(), connection));
}
}
std::vector<boost::uuids::uuid> in_connections{9};
std::generate(in_connections.begin(), in_connections.end(), random_uuid);
{
std::map<boost::uuids::uuid, std::size_t> used;
std::map<boost::uuids::uuid, boost::uuids::uuid> mapping;
for (const boost::uuids::uuid& connection : in_connections)
{
const boost::uuids::uuid out = mapper.get_stem(connection);
EXPECT_FALSE(out.is_nil());
EXPECT_TRUE(mapping.emplace(connection, out).second);
used[out]++;
}
EXPECT_EQ(3u, used.size());
for (const std::pair<boost::uuids::uuid, std::size_t>& entry : used)
EXPECT_EQ(3u, entry.second);
for (const boost::uuids::uuid& connection : in_connections)
EXPECT_EQ(mapping[connection], mapper.get_stem(connection));
// drop all connections
connections.clear();
EXPECT_TRUE(mapper.update(connections));
EXPECT_EQ(0u, mapper.size());
EXPECT_EQ(3, mapper.end() - mapper.begin());
}
// remap 7 connections to nothing
for (const boost::uuids::uuid& connection : boost::adaptors::slice(in_connections, 0, 7))
EXPECT_TRUE(mapper.get_stem(connection).is_nil());
// 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);
EXPECT_TRUE(mapper.update(connections));
{
std::map<boost::uuids::uuid, std::size_t> used;
for (const boost::uuids::uuid& connection : in_connections)
{
const boost::uuids::uuid& out = mapper.get_stem(connection);
EXPECT_FALSE(out.is_nil());
used[out]++;
}
EXPECT_EQ(3u, used.size());
for (const std::pair<boost::uuids::uuid, std::size_t>& entry : used)
EXPECT_EQ(3u, entry.second);
}
}