From ccb5f6b6969cc8cdd71efad6ac52d066ee81c369 Mon Sep 17 00:00:00 2001 From: Michael Date: Sat, 19 Jan 2019 18:16:40 +0000 Subject: [PATCH] Add tests for dht::TXOwnere --- CMakeLists.txt | 1 + llarp/dht/context.cpp | 1 + llarp/dht/context.hpp | 37 +-------- llarp/dht/node.hpp | 9 +- llarp/dht/txowner.cpp | 1 + llarp/dht/txowner.hpp | 54 ++++++++++++ llarp/router/router.cpp | 3 +- test/dht/test_llarp_dht_txowner.cpp | 123 ++++++++++++++++++++++++++++ 8 files changed, 188 insertions(+), 41 deletions(-) create mode 100644 llarp/dht/txowner.cpp create mode 100644 llarp/dht/txowner.hpp create mode 100644 test/dht/test_llarp_dht_txowner.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 999ce7098..dd6678eb1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -511,6 +511,7 @@ set(LIB_SRC llarp/dht/messages/gotrouter.cpp llarp/dht/messages/pubintro.cpp llarp/dht/node.cpp + llarp/dht/txowner.cpp llarp/dns.cpp llarp/dnsc.cpp llarp/dnsd.cpp diff --git a/llarp/dht/context.cpp b/llarp/dht/context.cpp index c036696ec..5b488372d 100644 --- a/llarp/dht/context.cpp +++ b/llarp/dht/context.cpp @@ -4,6 +4,7 @@ #include #include #include +#include #include #include #include diff --git a/llarp/dht/context.hpp b/llarp/dht/context.hpp index 4074cd7c3..1f1e84dbb 100644 --- a/llarp/dht/context.hpp +++ b/llarp/dht/context.hpp @@ -7,6 +7,7 @@ #include #include #include +#include #include #include @@ -18,40 +19,6 @@ namespace llarp namespace dht { - struct TXOwner - { - Key_t node; - uint64_t txid = 0; - - TXOwner() = default; - - TXOwner(const Key_t& k, uint64_t id) : node(k), txid(id) - { - } - - bool - operator==(const TXOwner& other) const - { - return txid == other.txid && node == other.node; - } - bool - operator<(const TXOwner& other) const - { - return txid < other.txid || node < other.node; - } - - struct Hash - { - std::size_t - operator()(TXOwner const& o) const noexcept - { - std::size_t sz2; - memcpy(&sz2, &o.node[0], sizeof(std::size_t)); - return o.txid ^ (sz2 << 1); - } - }; - }; - struct Context; template < typename K, typename V > @@ -224,7 +191,7 @@ namespace llarp const Key_t& target, bool recursive, std::vector< std::unique_ptr< IMessage > >& replies); - /// relay a dht messeage from a local path to the main network + /// relay a dht message from a local path to the main network bool RelayRequestForPath(const llarp::PathID_t& localPath, const IMessage* msg); diff --git a/llarp/dht/node.hpp b/llarp/dht/node.hpp index a0fb201bc..5ad58eaea 100644 --- a/llarp/dht/node.hpp +++ b/llarp/dht/node.hpp @@ -11,8 +11,7 @@ namespace llarp { struct RCNode { - llarp::RouterContact rc; - + RouterContact rc; Key_t ID; RCNode() @@ -20,7 +19,7 @@ namespace llarp ID.Zero(); } - RCNode(const llarp::RouterContact& other) : rc(other), ID(other.pubkey) + RCNode(const RouterContact& other) : rc(other), ID(other.pubkey) { } @@ -33,7 +32,7 @@ namespace llarp struct ISNode { - llarp::service::IntroSet introset; + service::IntroSet introset; Key_t ID; @@ -42,7 +41,7 @@ namespace llarp ID.Zero(); } - ISNode(const llarp::service::IntroSet& other) + ISNode(const service::IntroSet& other) { introset = other; introset.A.CalculateAddress(ID.as_array()); diff --git a/llarp/dht/txowner.cpp b/llarp/dht/txowner.cpp new file mode 100644 index 000000000..3707b15f4 --- /dev/null +++ b/llarp/dht/txowner.cpp @@ -0,0 +1 @@ +#include diff --git a/llarp/dht/txowner.hpp b/llarp/dht/txowner.hpp new file mode 100644 index 000000000..1626380f2 --- /dev/null +++ b/llarp/dht/txowner.hpp @@ -0,0 +1,54 @@ +#ifndef LLARP_DHT_TXOWNER_HPP +#define LLARP_DHT_TXOWNER_HPP + +#include + +#include + +namespace llarp +{ + namespace dht + { + struct TXOwner + { + Key_t node; + uint64_t txid = 0; + + TXOwner() = default; + TXOwner(const TXOwner&) = default; + TXOwner(TXOwner&&) = default; + + TXOwner& + operator=(const TXOwner&) = default; + + TXOwner(const Key_t& k, uint64_t id) : node(k), txid(id) + { + } + + bool + operator==(const TXOwner& other) const + { + return std::tie(txid, node) == std::tie(other.txid, other.node); + } + + bool + operator<(const TXOwner& other) const + { + return std::tie(txid, node) < std::tie(other.txid, other.node); + } + + struct Hash + { + std::size_t + operator()(const TXOwner& o) const noexcept + { + std::size_t sz2; + memcpy(&sz2, &o.node[0], sizeof(std::size_t)); + return o.txid ^ (sz2 << 1); + } + }; + }; + } // namespace dht +} // namespace llarp + +#endif diff --git a/llarp/router/router.cpp b/llarp/router/router.cpp index 32a8df731..f0457ee2c 100644 --- a/llarp/router/router.cpp +++ b/llarp/router/router.cpp @@ -3,6 +3,7 @@ #include #include #include +#include #include #include #include @@ -888,7 +889,7 @@ namespace llarp { auto buf = llarp::ConstBuffer(itr->second.front()); if(!chosen->SendTo(remote, buf)) - llarp::LogWarn("failed to send outboud message to ", remote, " via ", + llarp::LogWarn("failed to send outbound message to ", remote, " via ", chosen->Name()); itr->second.pop(); diff --git a/test/dht/test_llarp_dht_txowner.cpp b/test/dht/test_llarp_dht_txowner.cpp new file mode 100644 index 000000000..fbb000a45 --- /dev/null +++ b/test/dht/test_llarp_dht_txowner.cpp @@ -0,0 +1,123 @@ +#include + +#include + +namespace +{ + using llarp::dht::Key_t; + using llarp::dht::TXOwner; + + struct TxOwnerData + { + Key_t node; + uint64_t id; + size_t expectedHash; + + TxOwnerData(const Key_t& k, uint64_t i, size_t h) + : node(k), id(i), expectedHash(h) + { + } + }; + + struct TxOwner : public ::testing::TestWithParam< TxOwnerData > + { + }; + + TEST_F(TxOwner, default_construct) + { + TXOwner dc; + ASSERT_TRUE(dc.node.IsZero()); + ASSERT_EQ(0u, dc.txid); + ASSERT_EQ(0u, TXOwner::Hash()(dc)); + } + + TEST_P(TxOwner, hash) + { + // test single interactions (constructor and hash) + auto d = GetParam(); + TXOwner constructor(d.node, d.id); + + ASSERT_EQ(d.expectedHash, TXOwner::Hash()(constructor)); + } + + std::vector< TxOwnerData > + makeData() + { + std::vector< TxOwnerData > result; + + Key_t zero; + zero.Zero(); + Key_t one; + one.Fill(0x01); + Key_t two; + two.Fill(0x02); + + uint64_t max = std::numeric_limits< uint64_t >::max(); + + result.emplace_back(zero, 0, 0ull); + result.emplace_back(zero, 1, 1ull); + result.emplace_back(one, 0, 144680345676153346ull); + result.emplace_back(one, 1, 144680345676153347ull); + result.emplace_back(two, 0, 289360691352306692ull); + result.emplace_back(two, 2, 289360691352306694ull); + result.emplace_back(zero, max, 18446744073709551615ull); + result.emplace_back(one, max, 18302063728033398269ull); + result.emplace_back(two, max, 18157383382357244923ull); + + return result; + } + + struct TxOwnerCmpData + { + TXOwner lhs; + TXOwner rhs; + bool equal; + bool less; + + TxOwnerCmpData(const TXOwner& l, const TXOwner& r, bool e, bool ls) + : lhs(l), rhs(r), equal(e), less(ls) + { + } + }; + + struct TxOwnerOps : public ::testing::TestWithParam< TxOwnerCmpData > + { + }; + + TEST_P(TxOwnerOps, operators) + { + // test single interactions (constructor and hash) + auto d = GetParam(); + + ASSERT_EQ(d.lhs == d.rhs, d.equal); + ASSERT_EQ(d.lhs < d.rhs, d.less); + } + + std::vector< TxOwnerCmpData > + makeCmpData() + { + std::vector< TxOwnerCmpData > result; + + Key_t zero; + zero.Fill(0x00); + Key_t one; + one.Fill(0x01); + Key_t two; + two.Fill(0x02); + + result.emplace_back(TXOwner(zero, 0), TXOwner(zero, 0), true, false); + result.emplace_back(TXOwner(one, 0), TXOwner(one, 0), true, false); + result.emplace_back(TXOwner(two, 0), TXOwner(two, 0), true, false); + + result.emplace_back(TXOwner(zero, 0), TXOwner(one, 0), false, true); + result.emplace_back(TXOwner(two, 0), TXOwner(one, 0), false, false); + + return result; + } +} // namespace + +INSTANTIATE_TEST_CASE_P(TestDhtTxOwner, TxOwner, + ::testing::ValuesIn(makeData())); + +INSTANTIATE_TEST_CASE_P(TestDhtTxOwner, TxOwnerOps, + ::testing::ValuesIn(makeCmpData()));