mirror of https://github.com/oxen-io/lokinet
Merge pull request #1561 from majestrate/catch2_migrate-2021-03-03
totally remove gtest/gmock from unit tests.pull/1567/head
commit
db515e48ec
@ -1 +0,0 @@
|
||||
Subproject commit 703bd9caab50b139428cea1aaff9974ebee5742e
|
@ -1,2 +1,38 @@
|
||||
#define CATCH_CONFIG_MAIN
|
||||
#define CATCH_CONFIG_RUNNER
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
#include <util/logging/logger.hpp>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
int
|
||||
startWinsock()
|
||||
{
|
||||
WSADATA wsockd;
|
||||
int err;
|
||||
err = ::WSAStartup(MAKEWORD(2, 2), &wsockd);
|
||||
if (err)
|
||||
{
|
||||
perror("Failed to start Windows Sockets");
|
||||
return err;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
main(int argc, char* argv[])
|
||||
{
|
||||
llarp::LogSilencer shutup{};
|
||||
|
||||
#ifdef _WIN32
|
||||
if (startWinsock())
|
||||
return -1;
|
||||
#endif
|
||||
|
||||
int result = Catch::Session().run(argc, argv);
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
return result;
|
||||
}
|
||||
|
@ -1,60 +1,56 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <config/ini.hpp>
|
||||
|
||||
struct TestINIParser : public ::testing::Test
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
TEST_CASE("ConfigParser", "[config]")
|
||||
{
|
||||
llarp::ConfigParser parser;
|
||||
|
||||
void
|
||||
TearDown()
|
||||
SECTION("Parse empty")
|
||||
{
|
||||
parser.Clear();
|
||||
REQUIRE(parser.LoadFromStr(""));
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(TestINIParser, TestParseEmpty)
|
||||
{
|
||||
ASSERT_TRUE(parser.LoadFromStr(""));
|
||||
}
|
||||
SECTION("Parse one section")
|
||||
{
|
||||
llarp::ConfigParser::SectionValues_t sect;
|
||||
// this is an anti pattern don't write this kind of code with configpaser
|
||||
auto assertVisit = [§](const auto& section) -> bool {
|
||||
sect = section;
|
||||
return true;
|
||||
};
|
||||
REQUIRE(parser.LoadFromStr("[test]\nkey=val \n"));
|
||||
REQUIRE(parser.VisitSection("test", assertVisit));
|
||||
auto itr = sect.find("notfound");
|
||||
REQUIRE(itr == sect.end());
|
||||
itr = sect.find("key");
|
||||
REQUIRE(itr != sect.end());
|
||||
REQUIRE(itr->second == "val");
|
||||
}
|
||||
|
||||
TEST_F(TestINIParser, TestParseOneSection)
|
||||
{
|
||||
llarp::ConfigParser::SectionValues_t sect;
|
||||
// this is an anti pattern don't write this kind of code with configpaser
|
||||
auto assertVisit = [§](const auto& section) -> bool {
|
||||
sect = section;
|
||||
return true;
|
||||
};
|
||||
ASSERT_TRUE(parser.LoadFromStr("[test]\nkey=val \n"));
|
||||
ASSERT_TRUE(parser.VisitSection("test", assertVisit));
|
||||
auto itr = sect.find("notfound");
|
||||
ASSERT_EQ(itr, sect.end());
|
||||
itr = sect.find("key");
|
||||
ASSERT_NE(itr, sect.end());
|
||||
ASSERT_EQ(itr->second, "val");
|
||||
}
|
||||
SECTION("Parse section duplicate keys")
|
||||
{
|
||||
REQUIRE(parser.LoadFromStr("[test]\nkey1=val1\nkey1=val2"));
|
||||
size_t num = 0;
|
||||
auto visit = [&num](const auto& section) -> bool {
|
||||
num = section.count("key1");
|
||||
return true;
|
||||
};
|
||||
REQUIRE(parser.VisitSection("test", visit));
|
||||
REQUIRE(num == size_t(2));
|
||||
}
|
||||
|
||||
TEST_F(TestINIParser, TestParseSectionDuplicateKeys)
|
||||
{
|
||||
ASSERT_TRUE(parser.LoadFromStr("[test]\nkey1=val1\nkey1=val2"));
|
||||
size_t num = 0;
|
||||
auto visit = [&num](const auto& section) -> bool {
|
||||
num = section.count("key1");
|
||||
return true;
|
||||
};
|
||||
ASSERT_TRUE(parser.VisitSection("test", visit));
|
||||
ASSERT_EQ(num, size_t(2));
|
||||
}
|
||||
SECTION("No key")
|
||||
{
|
||||
REQUIRE_FALSE(parser.LoadFromStr("[test]\n=1090\n"));
|
||||
}
|
||||
|
||||
TEST_F(TestINIParser, TestNoKey)
|
||||
{
|
||||
ASSERT_FALSE(parser.LoadFromStr("[test]\n=1090\n"));
|
||||
}
|
||||
SECTION("Parse invalid")
|
||||
{
|
||||
REQUIRE_FALSE(
|
||||
parser.LoadFromStr("srged5ghe5\nf34wtge5\nw34tgfs4ygsd5yg=4;\n#"
|
||||
"g4syhgd5\n"));
|
||||
}
|
||||
|
||||
TEST_F(TestINIParser, TestParseInvalid)
|
||||
{
|
||||
ASSERT_FALSE(
|
||||
parser.LoadFromStr("srged5ghe5\nf34wtge5\nw34tgfs4ygsd5yg=4;\n#"
|
||||
"g4syhgd5\n"));
|
||||
parser.Clear();
|
||||
}
|
||||
|
@ -1 +0,0 @@
|
||||
#include <crypto/mock_crypto.hpp>
|
@ -1,86 +0,0 @@
|
||||
#ifndef TEST_LLARP_CRYPTO_MOCK_CRYPTO
|
||||
#define TEST_LLARP_CRYPTO_MOCK_CRYPTO
|
||||
|
||||
#include <crypto/crypto.hpp>
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace test
|
||||
{
|
||||
struct MockCrypto final : public Crypto
|
||||
{
|
||||
MOCK_METHOD3(maybe_decrypt_name,std::optional<AlignedBuffer<32>>(std::string_view, llarp::SymmNonce, std::string_view));
|
||||
|
||||
|
||||
MOCK_METHOD3(xchacha20,
|
||||
bool(const llarp_buffer_t &, const SharedSecret &,
|
||||
const TunnelNonce &));
|
||||
|
||||
MOCK_METHOD4(xchacha20_alt,
|
||||
bool(const llarp_buffer_t &, const llarp_buffer_t &,
|
||||
const SharedSecret &, const byte_t *));
|
||||
|
||||
MOCK_METHOD4(dh_client,
|
||||
bool(SharedSecret &, const PubKey &, const SecretKey &,
|
||||
const TunnelNonce &));
|
||||
|
||||
MOCK_METHOD4(dh_server,
|
||||
bool(SharedSecret &, const PubKey &, const SecretKey &,
|
||||
const TunnelNonce &));
|
||||
|
||||
MOCK_METHOD4(transport_dh_client,
|
||||
bool(SharedSecret &, const PubKey &, const SecretKey &,
|
||||
const TunnelNonce &));
|
||||
|
||||
MOCK_METHOD4(transport_dh_server,
|
||||
bool(SharedSecret &, const PubKey &, const SecretKey &,
|
||||
const TunnelNonce &));
|
||||
|
||||
MOCK_METHOD2(hash, bool(byte_t *, const llarp_buffer_t &));
|
||||
|
||||
MOCK_METHOD2(shorthash, bool(ShortHash &, const llarp_buffer_t &));
|
||||
|
||||
MOCK_METHOD3(hmac,
|
||||
bool(byte_t *, const llarp_buffer_t &,
|
||||
const SharedSecret &));
|
||||
|
||||
MOCK_METHOD4(derive_subkey, bool(PubKey &, const PubKey &, uint64_t, const AlignedBuffer<32> *));
|
||||
|
||||
MOCK_METHOD4(derive_subkey_private,
|
||||
bool(PrivateKey &, const SecretKey &, uint64_t, const AlignedBuffer<32> *));
|
||||
|
||||
MOCK_METHOD(bool, sign, (Signature &, const SecretKey &, const llarp_buffer_t &));
|
||||
|
||||
MOCK_METHOD(bool, sign, (Signature &, const PrivateKey &, const llarp_buffer_t &));
|
||||
|
||||
MOCK_METHOD3(verify,
|
||||
bool(const PubKey &, const llarp_buffer_t &,
|
||||
const Signature &));
|
||||
|
||||
MOCK_METHOD2(seed_to_secretkey,
|
||||
bool(llarp::SecretKey &, const llarp::IdentitySecret &));
|
||||
|
||||
MOCK_METHOD1(randomize, void(const llarp_buffer_t &));
|
||||
|
||||
MOCK_METHOD2(randbytes, void(byte_t *, size_t));
|
||||
|
||||
MOCK_METHOD1(identity_keygen, void(SecretKey &));
|
||||
|
||||
MOCK_METHOD1(encryption_keygen, void(SecretKey &));
|
||||
|
||||
MOCK_METHOD1(pqe_keygen, void(PQKeyPair &));
|
||||
|
||||
MOCK_METHOD3(pqe_decrypt,
|
||||
bool(const PQCipherBlock &, SharedSecret &, const byte_t *));
|
||||
|
||||
MOCK_METHOD3(pqe_encrypt,
|
||||
bool(PQCipherBlock &, SharedSecret &, const PQPubKey &));
|
||||
|
||||
MOCK_METHOD1(check_identity_privkey, bool(const SecretKey &));
|
||||
};
|
||||
} // namespace test
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -1 +0,0 @@
|
||||
#include <dht/mock_context.hpp>
|
@ -1,115 +0,0 @@
|
||||
#ifndef TEST_LLARP_DHT_MOCK_CONTEXT
|
||||
#define TEST_LLARP_DHT_MOCK_CONTEXT
|
||||
|
||||
#include <dht/context.hpp>
|
||||
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
namespace llarp
|
||||
{
|
||||
namespace test
|
||||
{
|
||||
struct MockContext final : public dht::AbstractContext
|
||||
{
|
||||
MOCK_CONST_METHOD1(StoreRC, void(const RouterContact));
|
||||
|
||||
MOCK_METHOD2(LookupRouter, bool(const RouterID&, RouterLookupHandler));
|
||||
|
||||
MOCK_METHOD5(LookupRouterRecursive,
|
||||
void(const RouterID&, const dht::Key_t&, uint64_t,
|
||||
const dht::Key_t&, RouterLookupHandler));
|
||||
|
||||
MOCK_METHOD6(LookupIntroSetRelayed,
|
||||
void(const dht::Key_t&, const dht::Key_t&, uint64_t,
|
||||
const dht::Key_t&, uint64_t,
|
||||
service::EncryptedIntroSetLookupHandler));
|
||||
|
||||
MOCK_METHOD5(LookupIntroSetDirect,
|
||||
void(const dht::Key_t&, const dht::Key_t&, uint64_t,
|
||||
const dht::Key_t&,
|
||||
service::EncryptedIntroSetLookupHandler));
|
||||
|
||||
MOCK_CONST_METHOD1(HasRouterLookup, bool(const RouterID& target));
|
||||
|
||||
MOCK_METHOD4(LookupRouterForPath,
|
||||
void(const RouterID& target, uint64_t txid,
|
||||
const PathID_t& path, const dht::Key_t& askpeer));
|
||||
|
||||
MOCK_METHOD5(LookupIntroSetForPath,
|
||||
void(const dht::Key_t&, uint64_t, const PathID_t&,
|
||||
const dht::Key_t&, uint64_t));
|
||||
|
||||
MOCK_METHOD3(DHTSendTo, void(const RouterID&, dht::IMessage*, bool));
|
||||
|
||||
MOCK_METHOD4(
|
||||
HandleExploritoryRouterLookup,
|
||||
bool(const dht::Key_t& requester, uint64_t txid,
|
||||
const RouterID& target,
|
||||
std::vector< std::unique_ptr< dht::IMessage > >& reply));
|
||||
|
||||
MOCK_METHOD5(
|
||||
LookupRouterRelayed,
|
||||
void(const dht::Key_t& requester, uint64_t txid,
|
||||
const dht::Key_t& target, bool recursive,
|
||||
std::vector< std::unique_ptr< dht::IMessage > >& replies));
|
||||
|
||||
MOCK_METHOD2(RelayRequestForPath,
|
||||
bool(const PathID_t& localPath, const dht::IMessage& msg));
|
||||
|
||||
MOCK_CONST_METHOD2(GetRCFromNodeDB,
|
||||
bool(const dht::Key_t& k, RouterContact& rc));
|
||||
|
||||
MOCK_METHOD5(PropagateIntroSetTo,
|
||||
void(const dht::Key_t& source, uint64_t sourceTX,
|
||||
const service::EncryptedIntroSet& introset,
|
||||
const dht::Key_t& peer, uint64_t relayOrder));
|
||||
MOCK_METHOD5(PropagateLocalIntroSet,
|
||||
void(const PathID_t& source, uint64_t sourceTX,
|
||||
const service::EncryptedIntroSet& introset,
|
||||
const dht::Key_t& peer, uint64_t relayOrder));
|
||||
|
||||
MOCK_METHOD2(Init,
|
||||
void(const dht::Key_t&, AbstractRouter*));
|
||||
|
||||
MOCK_CONST_METHOD1(GetIntroSetByLocation,
|
||||
std::optional< llarp::service::EncryptedIntroSet >(
|
||||
const llarp::dht::Key_t&));
|
||||
|
||||
MOCK_CONST_METHOD0(ExtractStatus, util::StatusObject());
|
||||
|
||||
MOCK_CONST_METHOD0(Now, llarp_time_t());
|
||||
|
||||
MOCK_METHOD1(ExploreNetworkVia, void(const dht::Key_t& peer));
|
||||
|
||||
MOCK_CONST_METHOD0(GetRouter, llarp::AbstractRouter*());
|
||||
|
||||
MOCK_CONST_METHOD0(OurKey, const dht::Key_t&());
|
||||
|
||||
MOCK_CONST_METHOD0(pendingIntrosetLookups,
|
||||
const PendingIntrosetLookups&());
|
||||
MOCK_METHOD0(pendingIntrosetLookups, PendingIntrosetLookups&());
|
||||
|
||||
MOCK_METHOD0(pendingRouterLookups, PendingRouterLookups&());
|
||||
|
||||
MOCK_CONST_METHOD0(pendingRouterLookups, const PendingRouterLookups&());
|
||||
|
||||
MOCK_METHOD0(pendingExploreLookups, PendingExploreLookups&());
|
||||
|
||||
MOCK_CONST_METHOD0(pendingExploreLookups, const PendingExploreLookups&());
|
||||
|
||||
MOCK_METHOD0(services, dht::Bucket< dht::ISNode >*());
|
||||
|
||||
MOCK_CONST_METHOD0(AllowTransit, const bool&());
|
||||
MOCK_METHOD0(AllowTransit, bool&());
|
||||
|
||||
MOCK_CONST_METHOD0(Nodes, dht::Bucket< dht::RCNode >*());
|
||||
MOCK_METHOD1(PutRCNodeAsync, void(const dht::RCNode& val));
|
||||
MOCK_METHOD1(DelRCNodeAsync, void(const dht::Key_t& val));
|
||||
|
||||
MOCK_METHOD2(FloodRCLater, void(const dht::Key_t, const RouterContact));
|
||||
};
|
||||
|
||||
} // namespace test
|
||||
} // namespace llarp
|
||||
|
||||
#endif
|
@ -1,370 +0,0 @@
|
||||
#include <dht/bucket.hpp>
|
||||
#include <dht/key.hpp>
|
||||
#include <dht/node.hpp>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using Key_t = llarp::dht::Key_t;
|
||||
using Value_t = llarp::dht::RCNode;
|
||||
using Bucket_t = llarp::dht::Bucket< Value_t >;
|
||||
|
||||
class TestDhtBucket : public ::testing::Test
|
||||
{
|
||||
public:
|
||||
TestDhtBucket() : randInt(0)
|
||||
{
|
||||
us.Fill(16);
|
||||
nodes = std::make_unique< Bucket_t >(us, [&]() { return randInt++; });
|
||||
size_t numNodes = 10;
|
||||
byte_t fill = 1;
|
||||
while(numNodes)
|
||||
{
|
||||
Value_t n;
|
||||
n.ID.Fill(fill);
|
||||
nodes->PutNode(n);
|
||||
--numNodes;
|
||||
++fill;
|
||||
}
|
||||
}
|
||||
|
||||
uint64_t randInt;
|
||||
|
||||
llarp::dht::Key_t us;
|
||||
std::unique_ptr< Bucket_t > nodes;
|
||||
};
|
||||
|
||||
TEST_F(TestDhtBucket, simple_cycle)
|
||||
{
|
||||
// Empty the current bucket.
|
||||
nodes->Clear();
|
||||
|
||||
// Create a simple value, and add it to the bucket.
|
||||
Value_t val;
|
||||
val.ID.Fill(1);
|
||||
|
||||
nodes->PutNode(val);
|
||||
|
||||
// Verify the value is in the bucket
|
||||
ASSERT_TRUE(nodes->HasNode(val.ID));
|
||||
ASSERT_EQ(1u, nodes->size());
|
||||
|
||||
// Verify after deletion, the value is no longer in the bucket
|
||||
nodes->DelNode(val.ID);
|
||||
ASSERT_FALSE(nodes->HasNode(val.ID));
|
||||
|
||||
// Verify deleting again succeeds;
|
||||
nodes->DelNode(val.ID);
|
||||
ASSERT_FALSE(nodes->HasNode(val.ID));
|
||||
}
|
||||
|
||||
TEST_F(TestDhtBucket, get_random_node_excluding)
|
||||
{
|
||||
// Empty the current bucket.
|
||||
nodes->Clear();
|
||||
|
||||
// We expect not to find anything
|
||||
Key_t result;
|
||||
std::set< Key_t > excludeSet;
|
||||
ASSERT_FALSE(nodes->GetRandomNodeExcluding(result, excludeSet));
|
||||
|
||||
// Create a simple value.
|
||||
Value_t val;
|
||||
val.ID.Fill(1);
|
||||
|
||||
// Add the simple value to the exclude set
|
||||
excludeSet.insert(val.ID);
|
||||
ASSERT_FALSE(nodes->GetRandomNodeExcluding(result, excludeSet));
|
||||
|
||||
// Add the simple value to the bucket
|
||||
nodes->PutNode(val);
|
||||
ASSERT_FALSE(nodes->GetRandomNodeExcluding(result, excludeSet));
|
||||
|
||||
excludeSet.clear();
|
||||
|
||||
ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet));
|
||||
ASSERT_EQ(val.ID, result);
|
||||
|
||||
// Add an element to the exclude set which isn't the bucket.
|
||||
Key_t other;
|
||||
other.Fill(0xff);
|
||||
excludeSet.insert(other);
|
||||
|
||||
ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet));
|
||||
ASSERT_EQ(val.ID, result);
|
||||
|
||||
// Add a node which is in both bucket and excludeSet
|
||||
Value_t nextVal;
|
||||
nextVal.ID.Fill(0xAA);
|
||||
excludeSet.insert(nextVal.ID);
|
||||
nodes->PutNode(nextVal);
|
||||
|
||||
ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet));
|
||||
ASSERT_EQ(val.ID, result);
|
||||
|
||||
// Clear the excludeSet - we should still have 2 nodes in the bucket
|
||||
excludeSet.clear();
|
||||
|
||||
randInt = 0;
|
||||
ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet));
|
||||
ASSERT_EQ(val.ID, result);
|
||||
|
||||
// Set the random value to be 1, we should get the other node.
|
||||
randInt = 1;
|
||||
ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet));
|
||||
ASSERT_EQ(nextVal.ID, result);
|
||||
|
||||
// Set the random value to be 100, we should get the first node.
|
||||
randInt = 100;
|
||||
ASSERT_TRUE(nodes->GetRandomNodeExcluding(result, excludeSet));
|
||||
ASSERT_EQ(val.ID, result);
|
||||
}
|
||||
|
||||
TEST_F(TestDhtBucket, find_closest)
|
||||
{
|
||||
// Empty the current bucket.
|
||||
nodes->Clear();
|
||||
|
||||
// We expect not to find anything
|
||||
Key_t target;
|
||||
target.Fill(0xF0);
|
||||
|
||||
Key_t result;
|
||||
ASSERT_FALSE(nodes->FindClosest(target, result));
|
||||
|
||||
// Add a node to the bucket
|
||||
Value_t first;
|
||||
first.ID.Zero();
|
||||
nodes->PutNode(first);
|
||||
|
||||
ASSERT_TRUE(nodes->FindClosest(target, result));
|
||||
ASSERT_EQ(result, first.ID);
|
||||
|
||||
// Add another node to the bucket, closer to the target
|
||||
Value_t second;
|
||||
second.ID.Fill(0x10);
|
||||
nodes->PutNode(second);
|
||||
ASSERT_TRUE(nodes->FindClosest(target, result));
|
||||
ASSERT_EQ(result, second.ID);
|
||||
|
||||
// Add a third node to the bucket, closer to the target
|
||||
Value_t third;
|
||||
third.ID.Fill(0x20);
|
||||
nodes->PutNode(third);
|
||||
ASSERT_TRUE(nodes->FindClosest(target, result));
|
||||
ASSERT_EQ(result, third.ID);
|
||||
|
||||
// Add a fourth node to the bucket, greater than the target
|
||||
Value_t fourth;
|
||||
fourth.ID.Fill(0xF1);
|
||||
nodes->PutNode(fourth);
|
||||
ASSERT_TRUE(nodes->FindClosest(target, result));
|
||||
ASSERT_EQ(result, fourth.ID);
|
||||
|
||||
// Add a fifth node to the bucket, equal to the target
|
||||
Value_t fifth;
|
||||
fifth.ID.Fill(0xF0);
|
||||
nodes->PutNode(fifth);
|
||||
ASSERT_TRUE(nodes->FindClosest(target, result));
|
||||
ASSERT_EQ(result, fifth.ID);
|
||||
}
|
||||
|
||||
TEST_F(TestDhtBucket, get_many_random)
|
||||
{
|
||||
// Empty the current bucket.
|
||||
nodes->Clear();
|
||||
|
||||
// Verify behaviour with empty node set
|
||||
std::set< Key_t > result;
|
||||
ASSERT_FALSE(nodes->GetManyRandom(result, 0));
|
||||
ASSERT_FALSE(nodes->GetManyRandom(result, 1));
|
||||
|
||||
// Add 5 nodes to the bucket
|
||||
std::set< Value_t > curValues;
|
||||
std::set< Key_t > curKeys;
|
||||
for(byte_t i = 0x00; i < 0x05; ++i)
|
||||
{
|
||||
Value_t v;
|
||||
v.ID.Fill(i);
|
||||
ASSERT_TRUE(curKeys.insert(v.ID).second);
|
||||
nodes->PutNode(v);
|
||||
}
|
||||
|
||||
// Fetching more than the current size fails
|
||||
ASSERT_EQ(5u, nodes->size());
|
||||
ASSERT_FALSE(nodes->GetManyRandom(result, nodes->size() + 1));
|
||||
|
||||
// Fetching the current size succeeds
|
||||
ASSERT_TRUE(nodes->GetManyRandom(result, nodes->size()));
|
||||
ASSERT_EQ(curKeys, result);
|
||||
|
||||
// Fetching a subset succeeds.
|
||||
// Note we hack this by "fixing" the random number generator
|
||||
result.clear();
|
||||
|
||||
ASSERT_TRUE(nodes->GetManyRandom(result, 1u));
|
||||
ASSERT_EQ(1u, result.size());
|
||||
ASSERT_EQ(*curKeys.begin(), *result.begin());
|
||||
|
||||
randInt = 0;
|
||||
result.clear();
|
||||
|
||||
ASSERT_TRUE(nodes->GetManyRandom(result, nodes->size() - 1));
|
||||
ASSERT_EQ(nodes->size() - 1, result.size());
|
||||
ASSERT_EQ(std::set< Key_t >(++curKeys.rbegin(), curKeys.rend()), result);
|
||||
}
|
||||
|
||||
TEST_F(TestDhtBucket, find_close_excluding)
|
||||
{
|
||||
// Empty the current bucket.
|
||||
nodes->Clear();
|
||||
|
||||
Key_t target;
|
||||
target.Zero();
|
||||
std::set< Key_t > exclude;
|
||||
Key_t result;
|
||||
|
||||
// Empty node + exclude set fails
|
||||
ASSERT_FALSE(nodes->FindCloseExcluding(target, result, exclude));
|
||||
|
||||
Value_t first;
|
||||
first.ID.Fill(0xF0);
|
||||
exclude.insert(first.ID);
|
||||
|
||||
// Empty nodes fails
|
||||
ASSERT_FALSE(nodes->FindCloseExcluding(target, result, exclude));
|
||||
|
||||
// Nodes and exclude set match
|
||||
nodes->PutNode(first);
|
||||
ASSERT_FALSE(nodes->FindCloseExcluding(target, result, exclude));
|
||||
|
||||
// Exclude set empty
|
||||
exclude.clear();
|
||||
ASSERT_TRUE(nodes->FindCloseExcluding(target, result, exclude));
|
||||
result = first.ID;
|
||||
|
||||
Value_t second;
|
||||
second.ID.Fill(0x01);
|
||||
nodes->PutNode(second);
|
||||
|
||||
ASSERT_TRUE(nodes->FindCloseExcluding(target, result, exclude));
|
||||
result = second.ID;
|
||||
|
||||
exclude.insert(second.ID);
|
||||
ASSERT_TRUE(nodes->FindCloseExcluding(target, result, exclude));
|
||||
result = first.ID;
|
||||
}
|
||||
|
||||
TEST_F(TestDhtBucket, find_many_near_excluding)
|
||||
{
|
||||
// Empty the current bucket.
|
||||
nodes->Clear();
|
||||
|
||||
Key_t target;
|
||||
target.Zero();
|
||||
std::set< Key_t > exclude;
|
||||
std::set< Key_t > result;
|
||||
|
||||
// Empty node + exclude set, with size 0 succeeds
|
||||
ASSERT_TRUE(nodes->GetManyNearExcluding(target, result, 0, exclude));
|
||||
ASSERT_EQ(0u, result.size());
|
||||
// Empty node + exclude set fails
|
||||
ASSERT_FALSE(nodes->GetManyNearExcluding(target, result, 1, exclude));
|
||||
|
||||
Value_t first;
|
||||
first.ID.Fill(0xF0);
|
||||
exclude.insert(first.ID);
|
||||
|
||||
// Empty nodes fails
|
||||
ASSERT_FALSE(nodes->GetManyNearExcluding(target, result, 1, exclude));
|
||||
|
||||
// Nodes and exclude set match
|
||||
nodes->PutNode(first);
|
||||
ASSERT_FALSE(nodes->GetManyNearExcluding(target, result, 1, exclude));
|
||||
|
||||
// Single node succeeds
|
||||
exclude.clear();
|
||||
ASSERT_TRUE(nodes->GetManyNearExcluding(target, result, 1, exclude));
|
||||
ASSERT_EQ(result, std::set< Key_t >({first.ID}));
|
||||
|
||||
// Trying to grab 2 nodes from a 1 node set fails
|
||||
result.clear();
|
||||
ASSERT_FALSE(nodes->GetManyNearExcluding(target, result, 2, exclude));
|
||||
|
||||
// two nodes finds closest
|
||||
Value_t second;
|
||||
second.ID.Fill(0x01);
|
||||
nodes->PutNode(second);
|
||||
result.clear();
|
||||
ASSERT_TRUE(nodes->GetManyNearExcluding(target, result, 1, exclude));
|
||||
ASSERT_EQ(result, std::set< Key_t >({second.ID}));
|
||||
|
||||
// 3 nodes finds 2 closest
|
||||
Value_t third;
|
||||
third.ID.Fill(0x02);
|
||||
nodes->PutNode(third);
|
||||
result.clear();
|
||||
ASSERT_TRUE(nodes->GetManyNearExcluding(target, result, 2, exclude));
|
||||
ASSERT_EQ(result, std::set< Key_t >({second.ID, third.ID}));
|
||||
|
||||
// 4 nodes, one in exclude set finds 2 closest
|
||||
Value_t fourth;
|
||||
fourth.ID.Fill(0x03);
|
||||
nodes->PutNode(fourth);
|
||||
exclude.insert(third.ID);
|
||||
result.clear();
|
||||
ASSERT_TRUE(nodes->GetManyNearExcluding(target, result, 2, exclude));
|
||||
ASSERT_EQ(result, std::set< Key_t >({second.ID, fourth.ID}));
|
||||
}
|
||||
|
||||
TEST_F(TestDhtBucket, TestBucketFindClosest)
|
||||
{
|
||||
llarp::dht::Key_t result;
|
||||
llarp::dht::Key_t target;
|
||||
target.Fill(5);
|
||||
ASSERT_TRUE(nodes->FindClosest(target, result));
|
||||
ASSERT_EQ(target, result);
|
||||
const llarp::dht::Key_t oldResult = result;
|
||||
target.Fill(0xf5);
|
||||
ASSERT_TRUE(nodes->FindClosest(target, result));
|
||||
ASSERT_EQ(oldResult, result);
|
||||
}
|
||||
|
||||
TEST_F(TestDhtBucket, TestBucketRandomized_1000)
|
||||
{
|
||||
size_t moreNodes = 100;
|
||||
while(moreNodes--)
|
||||
{
|
||||
llarp::dht::RCNode n;
|
||||
n.ID.Fill(randInt);
|
||||
randInt++;
|
||||
nodes->PutNode(n);
|
||||
}
|
||||
const size_t count = 1000;
|
||||
size_t left = count;
|
||||
while(left--)
|
||||
{
|
||||
llarp::dht::Key_t result;
|
||||
llarp::dht::Key_t target;
|
||||
target.Randomize();
|
||||
const llarp::dht::Key_t expect = target;
|
||||
ASSERT_TRUE(nodes->FindClosest(target, result));
|
||||
if(target == result)
|
||||
{
|
||||
ASSERT_GE(result ^ target, expect ^ target);
|
||||
ASSERT_EQ(result ^ target, expect ^ target);
|
||||
ASSERT_EQ(result ^ target, expect ^ target);
|
||||
}
|
||||
else
|
||||
{
|
||||
Key_t dist = result ^ target;
|
||||
Key_t oldDist = expect ^ target;
|
||||
ASSERT_NE(result ^ target, expect ^ target);
|
||||
|
||||
ASSERT_GE(result ^ target, expect ^ target)
|
||||
<< "result=" << result << "expect=" << expect << std::endl
|
||||
<< dist << ">=" << oldDist << "iteration=" << (count - left);
|
||||
|
||||
ASSERT_NE(result ^ target, expect ^ target);
|
||||
}
|
||||
}
|
||||
}
|
@ -1,92 +0,0 @@
|
||||
#include <dht/explorenetworkjob.hpp>
|
||||
|
||||
#include <dht/messages/findrouter.hpp>
|
||||
#include <dht/mock_context.hpp>
|
||||
#include <test_util.hpp>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using namespace llarp;
|
||||
using namespace ::testing;
|
||||
|
||||
using test::makeBuf;
|
||||
|
||||
struct TestDhtExploreNetworkJob : public ::testing::Test
|
||||
{
|
||||
RouterID peer;
|
||||
test::MockContext context;
|
||||
dht::ExploreNetworkJob exploreNetworkJob;
|
||||
|
||||
TestDhtExploreNetworkJob()
|
||||
: peer(makeBuf< RouterID >(0x01)), exploreNetworkJob(peer, &context)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(TestDhtExploreNetworkJob, validate)
|
||||
{
|
||||
const RouterID other = makeBuf< RouterID >(0x02);
|
||||
ASSERT_TRUE(exploreNetworkJob.Validate(other));
|
||||
}
|
||||
|
||||
TEST_F(TestDhtExploreNetworkJob, start)
|
||||
{
|
||||
// Verify input arguments are passed correctly.
|
||||
// The actual logic is inside the `dht::AbstractContext` implementation.
|
||||
|
||||
const auto txKey = makeBuf< dht::Key_t >(0x02);
|
||||
uint64_t txId = 4;
|
||||
|
||||
dht::TXOwner txOwner(txKey, txId);
|
||||
|
||||
// clang-format off
|
||||
EXPECT_CALL(context, DHTSendTo(
|
||||
Eq(txKey.as_array()),
|
||||
WhenDynamicCastTo< dht::FindRouterMessage* >(NotNull()),
|
||||
true)
|
||||
).Times(1);
|
||||
// clang-format off
|
||||
|
||||
ASSERT_NO_THROW(exploreNetworkJob.Start(txOwner));
|
||||
}
|
||||
|
||||
TEST_F(TestDhtExploreNetworkJob, send_reply)
|
||||
{
|
||||
// Concerns:
|
||||
// - Empty collection
|
||||
// - Lookup router fails (returns false)
|
||||
// - Number of calls matches collection size
|
||||
|
||||
{
|
||||
exploreNetworkJob.valuesFound.clear();
|
||||
EXPECT_CALL(context, LookupRouter(_, _)).Times(0);
|
||||
EXPECT_CALL(context, GetRouter()).WillOnce(Return(nullptr));
|
||||
|
||||
ASSERT_NO_THROW(exploreNetworkJob.SendReply());
|
||||
}
|
||||
|
||||
{
|
||||
exploreNetworkJob.valuesFound.clear();
|
||||
exploreNetworkJob.valuesFound.push_back(makeBuf<RouterID>(0x00));
|
||||
exploreNetworkJob.valuesFound.push_back(makeBuf<RouterID>(0x01));
|
||||
exploreNetworkJob.valuesFound.push_back(makeBuf<RouterID>(0x02));
|
||||
|
||||
EXPECT_CALL(context, GetRouter()).WillOnce(Return(nullptr));
|
||||
EXPECT_CALL(context, LookupRouter(Ne(makeBuf<RouterID>(0x01)), _)).Times(2).WillRepeatedly(Return(true));
|
||||
EXPECT_CALL(context, LookupRouter(Eq(makeBuf<RouterID>(0x01)), _)).WillOnce(Return(false));
|
||||
|
||||
ASSERT_NO_THROW(exploreNetworkJob.SendReply());
|
||||
}
|
||||
|
||||
{
|
||||
exploreNetworkJob.valuesFound.clear();
|
||||
exploreNetworkJob.valuesFound.push_back(makeBuf<RouterID>(0x00));
|
||||
exploreNetworkJob.valuesFound.push_back(makeBuf<RouterID>(0x01));
|
||||
exploreNetworkJob.valuesFound.push_back(makeBuf<RouterID>(0x02));
|
||||
|
||||
EXPECT_CALL(context, GetRouter()).WillOnce(Return(nullptr));
|
||||
EXPECT_CALL(context, LookupRouter(_, _)).Times(3).WillRepeatedly(Return(true));
|
||||
|
||||
ASSERT_NO_THROW(exploreNetworkJob.SendReply());
|
||||
}
|
||||
}
|
@ -1,88 +0,0 @@
|
||||
#include <dht/kademlia.hpp>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
using llarp::dht::Key_t;
|
||||
|
||||
using Array = std::array< byte_t, Key_t::SIZE >;
|
||||
|
||||
struct XorMetricData
|
||||
{
|
||||
Array us;
|
||||
Array left;
|
||||
Array right;
|
||||
bool result;
|
||||
|
||||
XorMetricData(const Array& u, const Array& l, const Array& r, bool res)
|
||||
: us(u), left(l), right(r), result(res)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
std::ostream&
|
||||
operator<<(std::ostream& stream, const XorMetricData& x)
|
||||
{
|
||||
stream << int(x.us[0]) << " " << int(x.left[0]) << " " << int(x.right[0])
|
||||
<< " " << std::boolalpha << x.result;
|
||||
|
||||
return stream;
|
||||
}
|
||||
|
||||
struct XorMetric : public ::testing::TestWithParam< XorMetricData >
|
||||
{
|
||||
};
|
||||
|
||||
TEST_P(XorMetric, test)
|
||||
{
|
||||
auto d = GetParam();
|
||||
ASSERT_EQ(llarp::dht::XorMetric{Key_t{d.us}}(Key_t{d.left}, Key_t{d.right}),
|
||||
d.result);
|
||||
}
|
||||
|
||||
std::vector< XorMetricData >
|
||||
makeData()
|
||||
{
|
||||
std::vector< XorMetricData > result;
|
||||
|
||||
Array zero;
|
||||
zero.fill(0);
|
||||
Array one;
|
||||
one.fill(1);
|
||||
Array two;
|
||||
two.fill(2);
|
||||
Array three;
|
||||
three.fill(3);
|
||||
|
||||
result.emplace_back(zero, zero, zero, false);
|
||||
result.emplace_back(zero, zero, one, true);
|
||||
result.emplace_back(zero, zero, two, true);
|
||||
result.emplace_back(zero, one, zero, false);
|
||||
result.emplace_back(zero, one, one, false);
|
||||
result.emplace_back(zero, one, two, true);
|
||||
result.emplace_back(zero, two, zero, false);
|
||||
result.emplace_back(zero, two, one, false);
|
||||
result.emplace_back(zero, two, two, false);
|
||||
result.emplace_back(one, zero, zero, false);
|
||||
result.emplace_back(one, zero, one, false);
|
||||
result.emplace_back(one, zero, two, true);
|
||||
result.emplace_back(one, one, zero, true);
|
||||
result.emplace_back(one, one, one, false);
|
||||
result.emplace_back(one, one, two, true);
|
||||
result.emplace_back(one, two, zero, false);
|
||||
result.emplace_back(one, two, one, false);
|
||||
result.emplace_back(one, two, two, false);
|
||||
result.emplace_back(two, zero, zero, false);
|
||||
result.emplace_back(two, zero, one, true);
|
||||
result.emplace_back(two, zero, two, false);
|
||||
result.emplace_back(two, one, zero, false);
|
||||
result.emplace_back(two, one, one, false);
|
||||
result.emplace_back(two, one, two, false);
|
||||
result.emplace_back(two, two, zero, true);
|
||||
result.emplace_back(two, two, one, true);
|
||||
result.emplace_back(two, two, two, false);
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(TestDhtXorMetric, XorMetric,
|
||||
::testing::ValuesIn(makeData()));
|
@ -1,123 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <dht/key.hpp>
|
||||
|
||||
using namespace llarp;
|
||||
|
||||
using Array = std::array< byte_t, dht::Key_t::SIZE >;
|
||||
|
||||
struct DHT : public ::testing::TestWithParam< Array >
|
||||
{
|
||||
};
|
||||
|
||||
TEST_P(DHT, constructor)
|
||||
{
|
||||
auto d = GetParam();
|
||||
|
||||
dht::Key_t a(d);
|
||||
dht::Key_t b(d.data());
|
||||
dht::Key_t c;
|
||||
|
||||
ASSERT_EQ(a, b);
|
||||
|
||||
if(a.IsZero())
|
||||
{
|
||||
ASSERT_EQ(a, c);
|
||||
}
|
||||
else
|
||||
{
|
||||
ASSERT_NE(a, c);
|
||||
}
|
||||
}
|
||||
|
||||
static constexpr Array emptyArray{
|
||||
{0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
|
||||
0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00}};
|
||||
|
||||
static constexpr Array fullArray{
|
||||
{0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
|
||||
0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff}};
|
||||
|
||||
static constexpr Array seqArray{
|
||||
{0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A,
|
||||
0x0B, 0x0C, 0x0D, 0x0E, 0x0F, 0x10, 0x11, 0x12, 0x13, 0x14, 0x15,
|
||||
0x16, 0x17, 0x18, 0x19, 0x1A, 0x1B, 0x1C, 0x1D, 0x1E, 0x1F}};
|
||||
|
||||
static const Array data[] = {emptyArray, fullArray, seqArray};
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(TestDhtKey, DHT, ::testing::ValuesIn(data));
|
||||
|
||||
TEST(TestDhtKey, eq)
|
||||
{
|
||||
ASSERT_EQ(dht::Key_t(emptyArray), dht::Key_t(emptyArray));
|
||||
ASSERT_EQ(dht::Key_t(fullArray), dht::Key_t(fullArray));
|
||||
ASSERT_EQ(dht::Key_t(seqArray), dht::Key_t(seqArray));
|
||||
}
|
||||
TEST(TestDhtKey, ne)
|
||||
{
|
||||
ASSERT_NE(dht::Key_t(emptyArray), dht::Key_t(fullArray));
|
||||
ASSERT_NE(dht::Key_t(emptyArray), dht::Key_t(seqArray));
|
||||
ASSERT_NE(dht::Key_t(fullArray), dht::Key_t(seqArray));
|
||||
}
|
||||
|
||||
TEST(TestDhtKey, lt)
|
||||
{
|
||||
ASSERT_LT(dht::Key_t(emptyArray), dht::Key_t(fullArray));
|
||||
ASSERT_LT(dht::Key_t(emptyArray), dht::Key_t(seqArray));
|
||||
ASSERT_LT(dht::Key_t(seqArray), dht::Key_t(fullArray));
|
||||
}
|
||||
|
||||
TEST(TestDhtKey, gt)
|
||||
{
|
||||
ASSERT_GT(dht::Key_t(fullArray), dht::Key_t(emptyArray));
|
||||
ASSERT_GT(dht::Key_t(seqArray), dht::Key_t(emptyArray));
|
||||
ASSERT_GT(dht::Key_t(fullArray), dht::Key_t(seqArray));
|
||||
}
|
||||
|
||||
TEST(TestDhtKey, XOR)
|
||||
{
|
||||
ASSERT_EQ(dht::Key_t(emptyArray),
|
||||
dht::Key_t(emptyArray) ^ dht::Key_t(emptyArray));
|
||||
|
||||
ASSERT_EQ(dht::Key_t(seqArray),
|
||||
dht::Key_t(emptyArray) ^ dht::Key_t(seqArray));
|
||||
|
||||
ASSERT_EQ(dht::Key_t(fullArray),
|
||||
dht::Key_t(emptyArray) ^ dht::Key_t(fullArray));
|
||||
|
||||
ASSERT_EQ(dht::Key_t(emptyArray),
|
||||
dht::Key_t(fullArray) ^ dht::Key_t(fullArray));
|
||||
|
||||
ASSERT_EQ(dht::Key_t(emptyArray),
|
||||
dht::Key_t(seqArray) ^ dht::Key_t(seqArray));
|
||||
|
||||
Array xorResult;
|
||||
std::iota(xorResult.rbegin(), xorResult.rend(), 0xE0);
|
||||
ASSERT_EQ(dht::Key_t(xorResult),
|
||||
dht::Key_t(seqArray) ^ dht::Key_t(fullArray));
|
||||
}
|
||||
|
||||
TEST(TestDhtKey, TestBucketOperators)
|
||||
{
|
||||
dht::Key_t zero;
|
||||
dht::Key_t one;
|
||||
dht::Key_t three;
|
||||
|
||||
zero.Zero();
|
||||
one.Fill(1);
|
||||
three.Fill(3);
|
||||
ASSERT_LT(zero, one);
|
||||
ASSERT_LT(zero, three);
|
||||
ASSERT_FALSE(zero > one);
|
||||
ASSERT_FALSE(zero > three);
|
||||
ASSERT_NE(zero, three);
|
||||
ASSERT_FALSE(zero == three);
|
||||
ASSERT_EQ(zero ^ one, one);
|
||||
ASSERT_LT(one, three);
|
||||
ASSERT_GT(three, one);
|
||||
ASSERT_NE(one, three);
|
||||
ASSERT_FALSE(one == three);
|
||||
ASSERT_EQ(one ^ three, three ^ one);
|
||||
}
|
@ -1,116 +0,0 @@
|
||||
#include <dht/node.hpp>
|
||||
|
||||
#include <test_util.hpp>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
using namespace llarp;
|
||||
using namespace ::testing;
|
||||
|
||||
using test::makeBuf;
|
||||
|
||||
struct TestDhtRCNode : public ::testing::Test
|
||||
{
|
||||
};
|
||||
|
||||
TEST_F(TestDhtRCNode, construct)
|
||||
{
|
||||
dht::RCNode node;
|
||||
ASSERT_THAT(node.ID, Property(&dht::Key_t::IsZero, true));
|
||||
|
||||
node.ID.Fill(0xCA);
|
||||
node.rc.last_updated = 101s;
|
||||
|
||||
dht::RCNode other{node};
|
||||
ASSERT_EQ(node.ID, other.ID);
|
||||
ASSERT_EQ(node.rc, other.rc);
|
||||
|
||||
RouterContact contact;
|
||||
contact.pubkey.Randomize();
|
||||
|
||||
dht::RCNode fromContact{contact};
|
||||
ASSERT_EQ(fromContact.ID.as_array(), contact.pubkey.as_array());
|
||||
}
|
||||
|
||||
TEST_F(TestDhtRCNode, lt)
|
||||
{
|
||||
dht::RCNode one;
|
||||
dht::RCNode two;
|
||||
dht::RCNode three;
|
||||
dht::RCNode eqThree;
|
||||
|
||||
one.rc.last_updated = 1s;
|
||||
two.rc.last_updated = 2s;
|
||||
three.rc.last_updated = 3s;
|
||||
eqThree.rc.last_updated = 3s;
|
||||
|
||||
// LT cases
|
||||
ASSERT_THAT(one, Lt(two));
|
||||
ASSERT_THAT(one, Lt(three));
|
||||
ASSERT_THAT(one, Lt(eqThree));
|
||||
ASSERT_THAT(two, Lt(three));
|
||||
ASSERT_THAT(two, Lt(eqThree));
|
||||
|
||||
// !LT cases
|
||||
ASSERT_THAT(one, Not(Lt(one)));
|
||||
ASSERT_THAT(two, Not(Lt(one)));
|
||||
ASSERT_THAT(two, Not(Lt(two)));
|
||||
ASSERT_THAT(three, Not(Lt(one)));
|
||||
ASSERT_THAT(three, Not(Lt(two)));
|
||||
ASSERT_THAT(three, Not(Lt(three)));
|
||||
ASSERT_THAT(three, Not(Lt(eqThree)));
|
||||
}
|
||||
|
||||
struct TestDhtISNode : public ::testing::Test
|
||||
{
|
||||
};
|
||||
|
||||
TEST_F(TestDhtISNode, construct)
|
||||
{
|
||||
dht::ISNode node;
|
||||
ASSERT_THAT(node.ID, Property(&dht::Key_t::IsZero, true));
|
||||
|
||||
node.ID.Fill(0xCA);
|
||||
node.introset.derivedSigningKey.Fill(0xDB);
|
||||
|
||||
dht::ISNode other{node};
|
||||
ASSERT_EQ(node.ID, other.ID);
|
||||
ASSERT_EQ(node.introset, other.introset);
|
||||
|
||||
service::EncryptedIntroSet introSet;
|
||||
introSet.derivedSigningKey.Randomize();
|
||||
|
||||
dht::ISNode fromIntro{introSet};
|
||||
|
||||
ASSERT_EQ(fromIntro.ID.as_array(), introSet.derivedSigningKey);
|
||||
}
|
||||
|
||||
TEST_F(TestDhtISNode, lt)
|
||||
{
|
||||
dht::ISNode one;
|
||||
dht::ISNode two;
|
||||
dht::ISNode three;
|
||||
dht::ISNode eqThree;
|
||||
|
||||
one.introset.signedAt = 1s;
|
||||
two.introset.signedAt = 2s;
|
||||
three.introset.signedAt = 3s;
|
||||
eqThree.introset.signedAt = 3s;
|
||||
|
||||
// LT cases
|
||||
ASSERT_THAT(one, Lt(two));
|
||||
ASSERT_THAT(one, Lt(three));
|
||||
ASSERT_THAT(one, Lt(eqThree));
|
||||
ASSERT_THAT(two, Lt(three));
|
||||
ASSERT_THAT(two, Lt(eqThree));
|
||||
|
||||
// !LT cases
|
||||
ASSERT_THAT(one, Not(Lt(one)));
|
||||
ASSERT_THAT(two, Not(Lt(one)));
|
||||
ASSERT_THAT(two, Not(Lt(two)));
|
||||
ASSERT_THAT(three, Not(Lt(one)));
|
||||
ASSERT_THAT(three, Not(Lt(two)));
|
||||
ASSERT_THAT(three, Not(Lt(three)));
|
||||
ASSERT_THAT(three, Not(Lt(eqThree)));
|
||||
}
|
@ -1,105 +0,0 @@
|
||||
#include <dht/tx.hpp>
|
||||
#include <service/tag.hpp>
|
||||
#include <test_util.hpp>
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <gmock/gmock.h>
|
||||
|
||||
using namespace llarp;
|
||||
using namespace ::testing;
|
||||
|
||||
using llarp::test::makeBuf;
|
||||
|
||||
using Val_t = llarp::service::Tag;
|
||||
|
||||
// Mock implementation of TX.
|
||||
struct TestTx final : public dht::TX< dht::Key_t, Val_t >
|
||||
{
|
||||
TestTx(const dht::TXOwner& asker, const dht::Key_t& k,
|
||||
dht::AbstractContext* p)
|
||||
: dht::TX< dht::Key_t, Val_t >(asker, k, p)
|
||||
{
|
||||
}
|
||||
|
||||
MOCK_CONST_METHOD1(Validate, bool(const Val_t&));
|
||||
|
||||
MOCK_METHOD1(Start, void(const dht::TXOwner&));
|
||||
|
||||
MOCK_METHOD0(SendReply, void());
|
||||
};
|
||||
|
||||
struct TestDhtTx : public Test
|
||||
{
|
||||
dht::TXOwner asker;
|
||||
dht::Key_t m_key;
|
||||
TestTx tx;
|
||||
|
||||
TestDhtTx() : tx(asker, m_key, nullptr)
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(TestDhtTx, on_found)
|
||||
{
|
||||
// Concerns
|
||||
// - Validate returns true
|
||||
// - Repeated call on success
|
||||
// - Validate returns false
|
||||
// - Repeated call on failure
|
||||
// - Repeated call on success after failure
|
||||
|
||||
const auto key = makeBuf< dht::Key_t >(0x00);
|
||||
Val_t val("good value");
|
||||
|
||||
// Validate returns true
|
||||
{
|
||||
EXPECT_CALL(tx, Validate(val)).WillOnce(Return(true));
|
||||
|
||||
tx.OnFound(key, val);
|
||||
|
||||
ASSERT_THAT(tx.peersAsked, Contains(key));
|
||||
ASSERT_THAT(tx.valuesFound, Contains(val));
|
||||
}
|
||||
|
||||
// Repeated call on success
|
||||
{
|
||||
EXPECT_CALL(tx, Validate(val)).WillOnce(Return(true));
|
||||
tx.OnFound(key, val);
|
||||
ASSERT_THAT(tx.peersAsked, Contains(key));
|
||||
ASSERT_THAT(tx.valuesFound, Contains(val));
|
||||
}
|
||||
|
||||
const auto key1 = makeBuf< dht::Key_t >(0x01);
|
||||
Val_t badVal("bad value");
|
||||
|
||||
// Validate returns false
|
||||
{
|
||||
EXPECT_CALL(tx, Validate(badVal)).WillOnce(Return(false));
|
||||
|
||||
tx.OnFound(key1, badVal);
|
||||
|
||||
ASSERT_THAT(tx.peersAsked, Contains(key1));
|
||||
ASSERT_THAT(tx.valuesFound, Not(Contains(badVal)));
|
||||
}
|
||||
|
||||
// Repeated call on failure
|
||||
{
|
||||
EXPECT_CALL(tx, Validate(badVal)).WillOnce(Return(false));
|
||||
|
||||
tx.OnFound(key1, badVal);
|
||||
|
||||
ASSERT_THAT(tx.peersAsked, Contains(key1));
|
||||
ASSERT_THAT(tx.valuesFound, Not(Contains(badVal)));
|
||||
}
|
||||
|
||||
// Repeated call on success after failure
|
||||
{
|
||||
EXPECT_CALL(tx, Validate(badVal)).WillOnce(Return(true));
|
||||
|
||||
tx.OnFound(key1, badVal);
|
||||
|
||||
ASSERT_THAT(tx.peersAsked, Contains(key1));
|
||||
ASSERT_THAT(tx.valuesFound, Contains(badVal));
|
||||
}
|
||||
}
|
||||
|
@ -1,123 +0,0 @@
|
||||
#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_SUITE_P(TestDhtTxOwner, TxOwner,
|
||||
::testing::ValuesIn(makeData()));
|
||||
|
||||
INSTANTIATE_TEST_SUITE_P(TestDhtTxOwner, TxOwnerOps,
|
||||
::testing::ValuesIn(makeCmpData()));
|
@ -1 +0,0 @@
|
||||
#include <llarp_test.hpp>
|
@ -1,26 +1,30 @@
|
||||
#ifndef LLARP_TEST
|
||||
#define LLARP_TEST
|
||||
#pragma once
|
||||
|
||||
#include <gtest/gtest.h>
|
||||
#include <crypto/mock_crypto.hpp>
|
||||
#include <crypto/crypto_libsodium.hpp>
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
namespace llarp
|
||||
namespace llarp::test
|
||||
{
|
||||
namespace test
|
||||
|
||||
template <typename CryptoImpl = llarp::sodium::CryptoLibSodium>
|
||||
class LlarpTest
|
||||
{
|
||||
template < typename CryptoImpl = MockCrypto >
|
||||
class LlarpTest : public ::testing::Test
|
||||
protected:
|
||||
CryptoImpl m_crypto;
|
||||
CryptoManager cm;
|
||||
|
||||
LlarpTest() : cm(&m_crypto)
|
||||
{
|
||||
protected:
|
||||
CryptoImpl m_crypto;
|
||||
CryptoManager cm;
|
||||
static_assert(std::is_base_of<Crypto, CryptoImpl>::value, "");
|
||||
}
|
||||
|
||||
LlarpTest() : cm(&m_crypto)
|
||||
{
|
||||
static_assert(std::is_base_of< Crypto, CryptoImpl >::value, "");
|
||||
}
|
||||
};
|
||||
} // namespace test
|
||||
} // namespace llarp
|
||||
~LlarpTest()
|
||||
{}
|
||||
};
|
||||
|
||||
#endif
|
||||
template <>
|
||||
inline LlarpTest<llarp::sodium::CryptoLibSodium>::~LlarpTest()
|
||||
{
|
||||
|
||||
} // namespace test
|
||||
}
|
||||
|
@ -1,37 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <util/logging/logger.hpp>
|
||||
|
||||
#ifdef _WIN32
|
||||
#include <winsock2.h>
|
||||
int
|
||||
startWinsock()
|
||||
{
|
||||
WSADATA wsockd;
|
||||
int err;
|
||||
err = ::WSAStartup(MAKEWORD(2, 2), &wsockd);
|
||||
if (err)
|
||||
{
|
||||
perror("Failed to start Windows Sockets");
|
||||
return err;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
#endif
|
||||
|
||||
int
|
||||
main(int argc, char** argv)
|
||||
{
|
||||
llarp::LogSilencer shutup;
|
||||
#ifdef _WIN32
|
||||
if (startWinsock())
|
||||
return -1;
|
||||
#endif
|
||||
|
||||
::testing::InitGoogleTest(&argc, argv);
|
||||
int r = RUN_ALL_TESTS();
|
||||
#ifdef _WIN32
|
||||
WSACleanup();
|
||||
#endif
|
||||
return r;
|
||||
}
|
@ -1,99 +1,104 @@
|
||||
#include <gtest/gtest.h>
|
||||
|
||||
#include <net/net_int.hpp>
|
||||
#include <net/ip.hpp>
|
||||
#include <net/ip_range.hpp>
|
||||
#include <net/net.hpp>
|
||||
|
||||
struct TestNet : public ::testing::Test
|
||||
{
|
||||
};
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
TEST_F(TestNet, TestIn6AddrFromString)
|
||||
TEST_CASE("In6Addr")
|
||||
{
|
||||
llarp::huint128_t ip;
|
||||
ASSERT_TRUE(ip.FromString("fc00::1"));
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestIn6AddrFromStringFail)
|
||||
{
|
||||
llarp::huint128_t ip;
|
||||
ASSERT_FALSE(ip.FromString("10.1.1.1"));
|
||||
SECTION("From string")
|
||||
{
|
||||
REQUIRE(ip.FromString("fc00::1"));
|
||||
}
|
||||
|
||||
SECTION("From string fail")
|
||||
{
|
||||
REQUIRE_FALSE(ip.FromString("10.1.1.1"));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestIn6AddrToHUIntLoopback)
|
||||
TEST_CASE("In6AddrToHUIntLoopback")
|
||||
{
|
||||
llarp::huint128_t loopback = {0};
|
||||
ASSERT_TRUE(loopback.FromString("::1"));
|
||||
REQUIRE(loopback.FromString("::1"));
|
||||
in6_addr addr = IN6ADDR_LOOPBACK_INIT;
|
||||
auto huint = llarp::net::In6ToHUInt(addr);
|
||||
ASSERT_EQ(huint, loopback);
|
||||
REQUIRE(huint == loopback);
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestIn6AddrToHUInt)
|
||||
TEST_CASE("In6AddrToHUInt")
|
||||
{
|
||||
llarp::huint128_t huint_parsed = {0};
|
||||
ASSERT_TRUE(huint_parsed.FromString("fd00::1"));
|
||||
REQUIRE(huint_parsed.FromString("fd00::1"));
|
||||
in6_addr addr = {{{0xfd, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0x01}}};
|
||||
auto huint = llarp::net::In6ToHUInt(addr);
|
||||
ASSERT_EQ(huint, huint_parsed);
|
||||
REQUIRE(huint == huint_parsed);
|
||||
huint_parsed.h++;
|
||||
ASSERT_NE(huint, huint_parsed);
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestRangeContains8)
|
||||
{
|
||||
ASSERT_TRUE(
|
||||
llarp::IPRange::FromIPv4(10, 0, 0, 1, 8).Contains(llarp::ipaddr_ipv4_bits(10, 40, 11, 6)));
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestRangeContains24)
|
||||
{
|
||||
ASSERT_TRUE(llarp::IPRange::FromIPv4(10, 200, 0, 1, 24)
|
||||
.Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253)));
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestRangeContainsFail)
|
||||
{
|
||||
ASSERT_TRUE(!llarp::IPRange::FromIPv4(192, 168, 0, 1, 24)
|
||||
.Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253)));
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestIPv4Netmask)
|
||||
{
|
||||
ASSERT_TRUE(llarp::netmask_ipv4_bits(8) == llarp::huint32_t{0xFF000000});
|
||||
ASSERT_TRUE(llarp::netmask_ipv4_bits(24) == llarp::huint32_t{0xFFFFFF00});
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestBogon_10_8)
|
||||
{
|
||||
ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(10, 40, 11, 6)));
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestBogon_192_168_16)
|
||||
{
|
||||
ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(192, 168, 1, 111)));
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestBogon_DoD_8)
|
||||
{
|
||||
ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(21, 3, 37, 70)));
|
||||
REQUIRE(huint != huint_parsed);
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestBogon_127_8)
|
||||
TEST_CASE("Range")
|
||||
{
|
||||
ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(127, 0, 0, 1)));
|
||||
SECTION("Contains 8")
|
||||
{
|
||||
REQUIRE(
|
||||
llarp::IPRange::FromIPv4(10, 0, 0, 1, 8).Contains(llarp::ipaddr_ipv4_bits(10, 40, 11, 6)));
|
||||
}
|
||||
|
||||
SECTION("Contains 24")
|
||||
{
|
||||
REQUIRE(llarp::IPRange::FromIPv4(10, 200, 0, 1, 24)
|
||||
.Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253)));
|
||||
}
|
||||
|
||||
SECTION("Contains fail")
|
||||
{
|
||||
REQUIRE(!llarp::IPRange::FromIPv4(192, 168, 0, 1, 24)
|
||||
.Contains(llarp::ipaddr_ipv4_bits(10, 200, 0, 253)));
|
||||
}
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestBogon_0_8)
|
||||
TEST_CASE("IPv4 netmask")
|
||||
{
|
||||
ASSERT_TRUE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(0, 0, 0, 0)));
|
||||
REQUIRE(llarp::netmask_ipv4_bits(8) == llarp::huint32_t{0xFF000000});
|
||||
REQUIRE(llarp::netmask_ipv4_bits(24) == llarp::huint32_t{0xFFFFFF00});
|
||||
}
|
||||
|
||||
TEST_F(TestNet, TestBogon_NonBogon)
|
||||
TEST_CASE("Bogon")
|
||||
{
|
||||
ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(1, 1, 1, 1)));
|
||||
ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(8, 8, 6, 6)));
|
||||
ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(141, 55, 12, 99)));
|
||||
ASSERT_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(79, 12, 3, 4)));
|
||||
SECTION("Bogon_10_8")
|
||||
{
|
||||
REQUIRE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(10, 40, 11, 6)));
|
||||
}
|
||||
|
||||
SECTION("Bogon_192_168_16")
|
||||
{
|
||||
REQUIRE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(192, 168, 1, 111)));
|
||||
}
|
||||
|
||||
SECTION("Bogon_DoD_8")
|
||||
{
|
||||
REQUIRE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(21, 3, 37, 70)));
|
||||
}
|
||||
|
||||
SECTION("Bogon_127_8")
|
||||
{
|
||||
REQUIRE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(127, 0, 0, 1)));
|
||||
}
|
||||
|
||||
SECTION("Bogon_0_8")
|
||||
{
|
||||
REQUIRE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(0, 0, 0, 0)));
|
||||
}
|
||||
|
||||
SECTION("Non-bogon")
|
||||
{
|
||||
REQUIRE_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(1, 1, 1, 1)));
|
||||
REQUIRE_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(8, 8, 6, 6)));
|
||||
REQUIRE_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(141, 55, 12, 99)));
|
||||
REQUIRE_FALSE(llarp::IsIPv4Bogon(llarp::ipaddr_ipv4_bits(79, 12, 3, 4)));
|
||||
}
|
||||
}
|
||||
|
@ -1,91 +1,87 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <router_version.hpp>
|
||||
#include "router/router.hpp"
|
||||
|
||||
class TestRouterVersion : public ::testing::Test
|
||||
{
|
||||
};
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
using Catch::Matchers::Equals;
|
||||
|
||||
TEST_F(TestRouterVersion, TestCompatibilityWhenProtocolEqual)
|
||||
TEST_CASE("Compatibility when protocol equal", "[RouterVersion]")
|
||||
{
|
||||
llarp::RouterVersion v1( {0, 1, 2}, 1);
|
||||
llarp::RouterVersion v2( {0, 1, 2}, 1);
|
||||
llarp::RouterVersion v1({0, 1, 2}, 1);
|
||||
llarp::RouterVersion v2({0, 1, 2}, 1);
|
||||
|
||||
EXPECT_TRUE(v1.IsCompatableWith(v2));
|
||||
CHECK(v1.IsCompatableWith(v2));
|
||||
}
|
||||
|
||||
TEST_F(TestRouterVersion, TestCompatibilityWhenProtocolUnequal)
|
||||
TEST_CASE("Compatibility when protocol unequal", "[RouterVersion]")
|
||||
{
|
||||
llarp::RouterVersion older( {0, 1, 2}, 1);
|
||||
llarp::RouterVersion newer( {0, 1, 2}, 2);
|
||||
llarp::RouterVersion older({0, 1, 2}, 1);
|
||||
llarp::RouterVersion newer({0, 1, 2}, 2);
|
||||
|
||||
EXPECT_FALSE(older.IsCompatableWith(newer));
|
||||
EXPECT_FALSE(newer.IsCompatableWith(older));
|
||||
CHECK_FALSE(older.IsCompatableWith(newer));
|
||||
CHECK_FALSE(newer.IsCompatableWith(older));
|
||||
}
|
||||
|
||||
TEST_F(TestRouterVersion, TestEmptyCompatibility)
|
||||
TEST_CASE("Empty compatibility", "[RouterVersion]")
|
||||
{
|
||||
llarp::RouterVersion v1( {0, 0, 1}, LLARP_PROTO_VERSION);
|
||||
llarp::RouterVersion v1({0, 0, 1}, LLARP_PROTO_VERSION);
|
||||
|
||||
EXPECT_FALSE(v1.IsCompatableWith(llarp::emptyRouterVersion));
|
||||
CHECK_FALSE(v1.IsCompatableWith(llarp::emptyRouterVersion));
|
||||
}
|
||||
|
||||
TEST_F(TestRouterVersion, TestIsEmpty)
|
||||
TEST_CASE("IsEmpty", "[RouterVersion]")
|
||||
{
|
||||
llarp::RouterVersion notEmpty( {0, 0, 1}, LLARP_PROTO_VERSION);
|
||||
EXPECT_FALSE(notEmpty.IsEmpty());
|
||||
llarp::RouterVersion notEmpty({0, 0, 1}, LLARP_PROTO_VERSION);
|
||||
CHECK_FALSE(notEmpty.IsEmpty());
|
||||
|
||||
EXPECT_TRUE(llarp::emptyRouterVersion.IsEmpty());
|
||||
CHECK(llarp::emptyRouterVersion.IsEmpty());
|
||||
}
|
||||
|
||||
TEST_F(TestRouterVersion, TestClear)
|
||||
TEST_CASE("Clear", "[RouterVersion]")
|
||||
{
|
||||
llarp::RouterVersion version( {0, 0, 1}, LLARP_PROTO_VERSION);
|
||||
EXPECT_FALSE(version.IsEmpty());
|
||||
llarp::RouterVersion version({0, 0, 1}, LLARP_PROTO_VERSION);
|
||||
CHECK_FALSE(version.IsEmpty());
|
||||
|
||||
version.Clear();
|
||||
|
||||
EXPECT_TRUE(version.IsEmpty());
|
||||
CHECK(version.IsEmpty());
|
||||
}
|
||||
|
||||
TEST_F(TestRouterVersion, TestBEncode)
|
||||
TEST_CASE("BEncode", "[RouterVersion]")
|
||||
{
|
||||
llarp::RouterVersion v1235( {1, 2, 3}, 5);
|
||||
llarp::RouterVersion v1235({1, 2, 3}, 5);
|
||||
|
||||
std::array< byte_t, 128 > tmp{};
|
||||
std::array<byte_t, 128> tmp{};
|
||||
llarp_buffer_t buf(tmp);
|
||||
|
||||
EXPECT_TRUE(v1235.BEncode(&buf));
|
||||
CHECK(v1235.BEncode(&buf));
|
||||
|
||||
std::string s((const char*)buf.begin(), (buf.end() - buf.begin()));
|
||||
LogInfo("bencoded: ", buf.begin());
|
||||
|
||||
EXPECT_STREQ((const char*)buf.begin(), "li5ei1ei2ei3ee");
|
||||
|
||||
CHECK_THAT((const char*)buf.begin(), Equals("li5ei1ei2ei3ee"));
|
||||
}
|
||||
|
||||
TEST_F(TestRouterVersion, TestBDecode)
|
||||
TEST_CASE("BDecode", "[RouterVersion]")
|
||||
{
|
||||
llarp::RouterVersion version;
|
||||
version.Clear();
|
||||
|
||||
const std::string bString("li9ei3ei2ei1ee");
|
||||
llarp_buffer_t buf(bString.data(), bString.size());
|
||||
EXPECT_TRUE(version.BDecode(&buf));
|
||||
CHECK(version.BDecode(&buf));
|
||||
|
||||
llarp::RouterVersion expected( {3, 2, 1}, 9);
|
||||
|
||||
EXPECT_EQ(expected, version);
|
||||
llarp::RouterVersion expected({3, 2, 1}, 9);
|
||||
|
||||
CHECK(expected == version);
|
||||
}
|
||||
|
||||
TEST_F(TestRouterVersion, TestDecodeLongVersionArray)
|
||||
TEST_CASE("Decode long version array", "[RouterVersion]")
|
||||
{
|
||||
llarp::RouterVersion version;
|
||||
version.Clear();
|
||||
|
||||
const std::string bString("li9ei3ei2ei1ei2ei3ei4ei5ei6ei7ei8ei9ee");
|
||||
llarp_buffer_t buf(bString.data(), bString.size());
|
||||
EXPECT_FALSE(version.BDecode(&buf));
|
||||
|
||||
CHECK_FALSE(version.BDecode(&buf));
|
||||
}
|
||||
|
@ -1,24 +0,0 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <routing/transfer_traffic_message.hpp>
|
||||
|
||||
using TransferTrafficMessage = llarp::routing::TransferTrafficMessage;
|
||||
|
||||
class TransferTrafficTest : public ::testing::Test
|
||||
{
|
||||
};
|
||||
|
||||
TEST_F(TransferTrafficTest, TestPutBufferOverflow)
|
||||
{
|
||||
TransferTrafficMessage msg;
|
||||
std::array< byte_t, llarp::routing::MaxExitMTU* 2 > tmp = {{0}};
|
||||
llarp_buffer_t buf(tmp);
|
||||
ASSERT_FALSE(msg.PutBuffer(buf, 1));
|
||||
}
|
||||
|
||||
TEST_F(TransferTrafficTest, TestPutBuffer)
|
||||
{
|
||||
TransferTrafficMessage msg;
|
||||
std::array< byte_t, llarp::routing::MaxExitMTU > tmp = {{0}};
|
||||
llarp_buffer_t buf(tmp);
|
||||
ASSERT_TRUE(msg.PutBuffer(buf, 1));
|
||||
}
|
@ -0,0 +1,24 @@
|
||||
#include <routing/transfer_traffic_message.hpp>
|
||||
|
||||
#include <catch2/catch.hpp>
|
||||
|
||||
using TransferTrafficMessage = llarp::routing::TransferTrafficMessage;
|
||||
|
||||
TEST_CASE("TransferTrafficMessage", "[TransferTrafficMessage]")
|
||||
{
|
||||
TransferTrafficMessage msg;
|
||||
|
||||
SECTION("Put buffer overflow")
|
||||
{
|
||||
std::array<byte_t, llarp::routing::MaxExitMTU* 2> tmp = {{0}};
|
||||
llarp_buffer_t buf(tmp);
|
||||
REQUIRE_FALSE(msg.PutBuffer(buf, 1));
|
||||
}
|
||||
|
||||
SECTION("Put buffer")
|
||||
{
|
||||
std::array<byte_t, llarp::routing::MaxExitMTU> tmp = {{0}};
|
||||
llarp_buffer_t buf(tmp);
|
||||
REQUIRE(msg.PutBuffer(buf, 1));
|
||||
}
|
||||
}
|
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue