1
1
Fork 0
mirror of https://github.com/oxen-io/lokinet synced 2023-12-14 06:53:00 +01:00

Add tests for dht::TXOwnere

This commit is contained in:
Michael 2019-01-19 18:16:40 +00:00
parent 2d395cc3f9
commit ccb5f6b696
No known key found for this signature in database
GPG key ID: 2D51757B47E2434C
8 changed files with 188 additions and 41 deletions

View file

@ -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

View file

@ -4,6 +4,7 @@
#include <dht/messages/gotintro.hpp>
#include <dht/messages/gotrouter.hpp>
#include <dht/messages/pubintro.hpp>
#include <dht/node.hpp>
#include <messages/dht.hpp>
#include <messages/dht_immediate.hpp>
#include <router/router.hpp>

View file

@ -7,6 +7,7 @@
#include <dht/message.hpp>
#include <dht/messages/findintro.hpp>
#include <dht/node.hpp>
#include <dht/txowner.hpp>
#include <service/IntroSet.hpp>
#include <util/time.hpp>
@ -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);

View file

@ -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());

1
llarp/dht/txowner.cpp Normal file
View file

@ -0,0 +1 @@
#include <dht/txowner.hpp>

54
llarp/dht/txowner.hpp Normal file
View file

@ -0,0 +1,54 @@
#ifndef LLARP_DHT_TXOWNER_HPP
#define LLARP_DHT_TXOWNER_HPP
#include <dht/key.hpp>
#include <cstdint>
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

View file

@ -3,6 +3,7 @@
#include <constants/proto.hpp>
#include <crypto/crypto.hpp>
#include <dht/context.hpp>
#include <dht/node.hpp>
#include <link/iwp.hpp>
#include <link/server.hpp>
#include <link/utp.hpp>
@ -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();

View file

@ -0,0 +1,123 @@
#include <dht/txowner.hpp>
#include <gtest/gtest.h>
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()));