2018-06-14 14:04:42 +00:00
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <llarp/dht.hpp>
|
|
|
|
|
2018-06-29 14:25:09 +00:00
|
|
|
using Key_t = llarp::dht::Key_t;
|
2018-06-14 14:04:42 +00:00
|
|
|
|
|
|
|
class KademliaDHTTest : public ::testing::Test
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
KademliaDHTTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
~KademliaDHTTest()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetUp()
|
|
|
|
{
|
|
|
|
us.Fill(16);
|
2018-06-29 14:25:09 +00:00
|
|
|
nodes = new llarp::dht::Bucket< llarp::dht::RCNode >(us);
|
2018-06-14 14:04:42 +00:00
|
|
|
size_t numNodes = 10;
|
|
|
|
byte_t fill = 1;
|
|
|
|
while(numNodes)
|
|
|
|
{
|
2018-06-29 14:25:09 +00:00
|
|
|
llarp::dht::RCNode n;
|
2018-06-14 14:04:42 +00:00
|
|
|
n.ID.Fill(fill);
|
|
|
|
nodes->PutNode(n);
|
|
|
|
--numNodes;
|
|
|
|
++fill;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
TearDown()
|
|
|
|
{
|
|
|
|
delete nodes;
|
|
|
|
}
|
|
|
|
|
2018-06-29 14:25:09 +00:00
|
|
|
llarp::dht::Bucket< llarp::dht::RCNode >* nodes = nullptr;
|
|
|
|
llarp::dht::Key_t us;
|
2018-06-14 14:04:42 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(KademliaDHTTest, TestBucketFindClosest)
|
|
|
|
{
|
2018-06-29 14:25:09 +00:00
|
|
|
llarp::dht::Key_t result;
|
|
|
|
llarp::dht::Key_t target;
|
|
|
|
llarp::dht::Key_t oldResult;
|
2018-06-14 14:04:42 +00:00
|
|
|
target.Fill(5);
|
|
|
|
ASSERT_TRUE(nodes->FindClosest(target, result));
|
|
|
|
ASSERT_TRUE(target == result);
|
|
|
|
oldResult = result;
|
|
|
|
target.Fill(0xf5);
|
|
|
|
ASSERT_TRUE(nodes->FindClosest(target, result));
|
|
|
|
ASSERT_TRUE(oldResult == result);
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(KademliaDHTTest, TestBucketRandomzied)
|
|
|
|
{
|
|
|
|
size_t moreNodes = 100;
|
|
|
|
while(moreNodes--)
|
|
|
|
{
|
2018-06-29 14:25:09 +00:00
|
|
|
llarp::dht::RCNode n;
|
2018-06-14 14:04:42 +00:00
|
|
|
n.ID.Randomize();
|
|
|
|
nodes->PutNode(n);
|
|
|
|
}
|
2018-06-29 14:25:09 +00:00
|
|
|
llarp::dht::Key_t result;
|
|
|
|
llarp::dht::Key_t target;
|
|
|
|
llarp::dht::Key_t oldResult;
|
2018-06-14 14:04:42 +00:00
|
|
|
target.Randomize();
|
|
|
|
ASSERT_TRUE(nodes->FindClosest(target, result));
|
|
|
|
};
|