2019-01-22 23:50:26 +00:00
|
|
|
#include <dht/tx.hpp>
|
2019-02-11 17:14:43 +00:00
|
|
|
#include <service/tag.hpp>
|
2019-01-22 23:50:26 +00:00
|
|
|
#include <test_util.hpp>
|
|
|
|
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include <gmock/gmock.h>
|
|
|
|
|
|
|
|
using namespace llarp;
|
|
|
|
using namespace ::testing;
|
|
|
|
|
|
|
|
using llarp::test::makeBuf;
|
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
using Val_t = llarp::service::Tag;
|
|
|
|
|
2019-01-22 23:50:26 +00:00
|
|
|
// Mock implementation of TX.
|
2019-02-11 17:14:43 +00:00
|
|
|
struct TestTx final : public dht::TX< dht::Key_t, Val_t >
|
2019-01-22 23:50:26 +00:00
|
|
|
{
|
|
|
|
TestTx(const dht::TXOwner& asker, const dht::Key_t& k,
|
|
|
|
dht::AbstractContext* p)
|
2019-02-11 17:14:43 +00:00
|
|
|
: dht::TX< dht::Key_t, Val_t >(asker, k, p)
|
2019-01-22 23:50:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2019-02-11 17:14:43 +00:00
|
|
|
MOCK_CONST_METHOD1(Validate, bool(const Val_t&));
|
2019-01-22 23:50:26 +00:00
|
|
|
|
|
|
|
MOCK_METHOD1(Start, void(const dht::TXOwner&));
|
|
|
|
|
|
|
|
MOCK_METHOD0(SendReply, void());
|
|
|
|
};
|
|
|
|
|
|
|
|
struct TestDhtTx : public Test
|
|
|
|
{
|
|
|
|
dht::TXOwner asker;
|
2019-08-02 09:27:27 +00:00
|
|
|
dht::Key_t m_key;
|
2019-01-22 23:50:26 +00:00
|
|
|
TestTx tx;
|
|
|
|
|
2019-08-02 09:27:27 +00:00
|
|
|
TestDhtTx() : tx(asker, m_key, nullptr)
|
2019-01-22 23:50:26 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
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);
|
2019-02-11 17:14:43 +00:00
|
|
|
Val_t val("good value");
|
2019-01-22 23:50:26 +00:00
|
|
|
|
|
|
|
// 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);
|
2019-02-11 17:14:43 +00:00
|
|
|
Val_t badVal("bad value");
|
2019-01-22 23:50:26 +00:00
|
|
|
|
|
|
|
// 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));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|