Tests for dht::TagLookup

pull/259/head
Michael 5 years ago
parent 24066ea9e0
commit b1213c7a7c
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -622,6 +622,7 @@ set(DNS_SRC
set(TEST_SRC
# helpers
test/main.cpp
test/crypto/mock_crypto.cpp
test/dht/mock_context.cpp
test/test_util.cpp
# actual test cases
@ -631,6 +632,7 @@ set(TEST_SRC
test/dht/test_llarp_dht_explorenetworkjob.cpp
test/dht/test_llarp_dht_kademlia.cpp
test/dht/test_llarp_dht_key.cpp
test/dht/test_llarp_dht_taglookup.cpp
test/dht/test_llarp_dht_tx.cpp
test/dht/test_llarp_dht_txowner.cpp
test/dns/test_llarp_dns_dns.cpp

@ -33,25 +33,17 @@ namespace llarp
void
TagLookup::SendReply()
{
std::set< service::IntroSet > found;
for(const auto &remoteTag : valuesFound)
{
found.insert(remoteTag);
}
std::set< service::IntroSet > found(valuesFound.begin(),
valuesFound.end());
// collect our local values if we haven't hit a limit
if(found.size() < 2)
{
for(const auto &localTag :
parent->FindRandomIntroSetsWithTagExcluding(target, 1, found))
{
found.insert(localTag);
}
}
std::vector< service::IntroSet > values;
for(const auto &introset : found)
{
values.push_back(introset);
auto tags =
parent->FindRandomIntroSetsWithTagExcluding(target, 1, found);
std::copy(tags.begin(), tags.end(), std::inserter(found, found.end()));
}
std::vector< service::IntroSet > values(found.begin(), found.end());
parent->DHTSendTo(whoasked.node.as_array(),
new GotIntroMessage(values, whoasked.txid));
}

@ -115,16 +115,22 @@ namespace llarp
copy = *this;
copy.Z.Zero();
if(!copy.BEncode(&buf))
{
return false;
}
// rewind and resize buffer
buf.sz = buf.cur - buf.base;
buf.cur = buf.base;
if(!A.Verify(crypto, buf, Z))
{
return false;
}
// validate PoW
using namespace std::placeholders;
if(W && !W->IsValid(std::bind(&Crypto::shorthash, crypto, _1, _2), now))
{
return false;
}
// valid timestamps
// add max clock skew
now += MAX_INTROSET_TIME_DELTA;

@ -0,0 +1 @@
#include <crypto/mock_crypto.hpp>

@ -0,0 +1,71 @@
#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(xchacha20,
bool(llarp_buffer_t, const SharedSecret &,
const TunnelNonce &));
MOCK_METHOD4(xchacha20_alt,
bool(llarp_buffer_t, 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 *, llarp_buffer_t));
MOCK_METHOD2(shorthash, bool(ShortHash &, llarp_buffer_t));
MOCK_METHOD3(hmac, bool(byte_t *, llarp_buffer_t, const SharedSecret &));
MOCK_METHOD3(sign, bool(Signature &, const SecretKey &, llarp_buffer_t));
MOCK_METHOD3(verify,
bool(const PubKey &, llarp_buffer_t, const Signature &));
MOCK_METHOD2(seed_to_secretkey,
bool(llarp::SecretKey &, const llarp::IdentitySecret &));
MOCK_METHOD1(randomize, void(llarp_buffer_t));
MOCK_METHOD2(randbytes, void(void *, 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 &));
};
} // namespace test
} // namespace llarp
#endif

@ -1,5 +1,5 @@
#ifndef TEST_LLARP_MOCK_CONTEXT
#define TEST_LLARP_MOCK_CONTEXT
#ifndef TEST_LLARP_DHT_MOCK_CONTEXT
#define TEST_LLARP_DHT_MOCK_CONTEXT
#include <dht/context.hpp>

@ -0,0 +1,229 @@
#include <dht/taglookup.hpp>
#include <crypto/mock_crypto.hpp>
#include <dht/mock_context.hpp>
#include <dht/messages/gotintro.hpp>
#include <service/IntroSet.hpp>
#include <test_util.hpp>
#include <gtest/gtest.h>
using namespace llarp;
using namespace ::testing;
using test::makeBuf;
static constexpr uint64_t EXPIRY = 1548503831ull;
struct TestDhtTagLookup : public ::testing::Test
{
test::MockCrypto crypto;
dht::Key_t txKey;
uint64_t txId;
dht::TXOwner txOwner;
service::Tag tag;
test::MockContext context;
uint64_t r;
dht::TagLookup tagLookup;
TestDhtTagLookup()
: txKey(makeBuf< dht::Key_t >(0x01))
, txId(2)
, txOwner(txKey, txId)
, tag(makeBuf< service::Tag >(0x03))
, r(4)
, tagLookup(txOwner, tag, &context, r)
{
}
};
TEST_F(TestDhtTagLookup, validate)
{
// Concerns
// - introset fails to verify
// - introset topic is not the target
// - happy path
{
service::IntroSet introset;
EXPECT_CALL(context, Crypto()).WillOnce(Return(&crypto));
EXPECT_CALL(context, Now()).WillOnce(Return(EXPIRY));
EXPECT_CALL(crypto, verify(_, _, _)).WillOnce(Return(false));
ASSERT_FALSE(tagLookup.Validate(introset));
}
{
service::IntroSet introset;
// Set topic to be different to the current tag
introset.topic = makeBuf< service::Tag >(0x02);
// Fiddle with the introset so we pass the Verify call
introset.I.emplace_back();
introset.I.front().expiresAt =
EXPIRY + service::MAX_INTROSET_TIME_DELTA + 1;
// Set expectations
EXPECT_CALL(context, Crypto()).WillOnce(Return(&crypto));
EXPECT_CALL(context, Now()).WillOnce(Return(EXPIRY));
EXPECT_CALL(crypto, verify(_, _, _)).WillOnce(Return(true));
ASSERT_FALSE(tagLookup.Validate(introset));
}
{
service::IntroSet introset;
// Set topic to be equal to the current tag
introset.topic = tag;
// Fiddle with the introset so we pass the Verify call
introset.I.emplace_back();
introset.I.front().expiresAt =
EXPIRY + service::MAX_INTROSET_TIME_DELTA + 1;
// Set expectations
EXPECT_CALL(context, Crypto()).WillOnce(Return(&crypto));
EXPECT_CALL(context, Now()).WillOnce(Return(EXPIRY));
EXPECT_CALL(crypto, verify(_, _, _)).WillOnce(Return(true));
ASSERT_TRUE(tagLookup.Validate(introset));
}
}
TEST_F(TestDhtTagLookup, start)
{
// Verify input arguments are passed correctly.
// The actual logic is inside the `dht::AbstractContext` implementation.
// clang-format off
EXPECT_CALL(context, DHTSendTo(
Eq(txKey.as_array()),
WhenDynamicCastTo< dht::FindIntroMessage* >(NotNull()),
true)
).Times(1);
// clang-format off
ASSERT_NO_THROW(tagLookup.Start(txOwner));
}
TEST_F(TestDhtTagLookup, get_next_peer)
{
dht::Key_t key = makeBuf< dht::Key_t >(0x02);
std::set< dht::Key_t > exclude;
ASSERT_FALSE(tagLookup.GetNextPeer(key, exclude));
}
TEST_F(TestDhtTagLookup, do_next)
{
const dht::Key_t key = makeBuf< dht::Key_t >(0x02);
ASSERT_NO_THROW(tagLookup.DoNextRequest(key));
}
TEST_F(TestDhtTagLookup, send_reply)
{
// Concerns
// - empty values found
// - when found.size < 2
// - FindRandomIntroSetsWithTagExcluding returns empty
// - FindRandomIntroSetsWithTagExcluding result are added to call
// - DHTSendTo called with correct params
{
tagLookup.valuesFound.clear();
// clang-format off
EXPECT_CALL(context, FindRandomIntroSetsWithTagExcluding(tag, _, IsEmpty()))
.WillOnce(Return(std::set< service::IntroSet >()));
EXPECT_CALL(
context,
DHTSendTo(
Eq(txKey.as_array()),
WhenDynamicCastTo<dht::GotIntroMessage *>(
AllOf(
NotNull(),
Field(&dht::GotIntroMessage::I, IsEmpty())
)
),
true
)
);
// clang-format on
ASSERT_NO_THROW(tagLookup.SendReply());
}
{
tagLookup.valuesFound.clear();
std::set< service::IntroSet > results;
results.emplace();
// clang-format off
EXPECT_CALL(context, FindRandomIntroSetsWithTagExcluding(tag, _, IsEmpty()))
.WillOnce(Return(results));
EXPECT_CALL(
context,
DHTSendTo(
Eq(txKey.as_array()),
WhenDynamicCastTo<dht::GotIntroMessage *>(
AllOf(
NotNull(),
Field(&dht::GotIntroMessage::I, SizeIs(1))
)
),
true
)
);
// clang-format on
ASSERT_NO_THROW(tagLookup.SendReply());
}
{
// clang-format off
tagLookup.valuesFound.clear();
tagLookup.valuesFound.emplace_back();
EXPECT_CALL(context, FindRandomIntroSetsWithTagExcluding(tag, _, SizeIs(1)))
.WillOnce(Return(std::set< service::IntroSet >()));
EXPECT_CALL(
context,
DHTSendTo(
Eq(txKey.as_array()),
WhenDynamicCastTo<dht::GotIntroMessage *>(
AllOf(
NotNull(),
Field(&dht::GotIntroMessage::I, SizeIs(1))
)
),
true
)
);
// clang-format on
ASSERT_NO_THROW(tagLookup.SendReply());
}
{
tagLookup.valuesFound.clear();
tagLookup.valuesFound.emplace_back();
tagLookup.valuesFound.back().T = 1;
tagLookup.valuesFound.emplace_back();
tagLookup.valuesFound.back().T = 2;
// clang-format off
EXPECT_CALL(context, FindRandomIntroSetsWithTagExcluding(_, _, _)).Times(0);
EXPECT_CALL(
context,
DHTSendTo(
Eq(txKey.as_array()),
WhenDynamicCastTo<dht::GotIntroMessage *>(
AllOf(
NotNull(),
Field(&dht::GotIntroMessage::I, SizeIs(2))
)
),
true
)
);
// clang-format on
ASSERT_NO_THROW(tagLookup.SendReply());
}
}
Loading…
Cancel
Save