Move tests to use top-level LlarpTest

pull/625/head
Michael 5 years ago
parent 491fee206b
commit a62655d501
No known key found for this signature in database
GPG Key ID: 2D51757B47E2434C

@ -41,7 +41,7 @@ extern "C"
/// setup main context, returns 0 on success
int
llarp_main_setup(struct llarp_main *ptr);
llarp_main_setup(struct llarp_main *ptr, bool debugMode);
/// run main context, returns 0 on success, blocks until program end
int

@ -84,7 +84,7 @@ namespace llarp
GetDatabase(const byte_t *pk);
int
Setup();
Setup(bool debug=false);
int
Run();

@ -51,9 +51,6 @@ namespace llarp
virtual bool
transport_dh_server(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) = 0;
/// blake2b 512 bit
virtual bool
hash(byte_t *, const llarp_buffer_t &) = 0;
/// blake2b 256 bit
virtual bool
shorthash(ShortHash &, const llarp_buffer_t &) = 0;
@ -74,7 +71,7 @@ namespace llarp
randomize(const llarp_buffer_t &) = 0;
/// randomizer memory
virtual void
randbytes(void *, size_t) = 0;
randbytes(byte_t *, size_t) = 0;
/// generate signing keypair
virtual void
identity_keygen(SecretKey &) = 0;

@ -85,7 +85,7 @@ namespace llarp
ntru_init(0);
}
int seed = 0;
this->randbytes(&seed, sizeof(seed));
randombytes(reinterpret_cast< unsigned char * >(&seed), sizeof(seed));
srand(seed);
}
@ -139,14 +139,6 @@ namespace llarp
return dh_server_priv(shared, pk, sk, n);
}
bool
CryptoLibSodium::hash(uint8_t *result, const llarp_buffer_t &buff)
{
return crypto_generichash_blake2b(result, HASHSIZE, buff.base, buff.sz,
nullptr, 0)
!= -1;
}
bool
CryptoLibSodium::shorthash(ShortHash &result, const llarp_buffer_t &buff)
{
@ -249,6 +241,14 @@ namespace llarp
return true;
}
static bool
hash(uint8_t *result, const llarp_buffer_t &buff)
{
return crypto_generichash_blake2b(result, HASHSIZE, buff.base, buff.sz,
nullptr, 0)
!= -1;
}
bool
CryptoLibSodium::sign(Signature &sig, const SecretKey &secret,
const llarp_buffer_t &buf)
@ -327,7 +327,7 @@ namespace llarp
}
void
CryptoLibSodium::randbytes(void *ptr, size_t sz)
CryptoLibSodium::randbytes(byte_t *ptr, size_t sz)
{
randombytes((unsigned char *)ptr, sz);
}

@ -41,9 +41,6 @@ namespace llarp
bool
transport_dh_server(SharedSecret &, const PubKey &, const SecretKey &,
const TunnelNonce &) override;
/// blake2b 512 bit
bool
hash(byte_t *, const llarp_buffer_t &) override;
/// blake2b 256 bit
bool
shorthash(ShortHash &, const llarp_buffer_t &) override;
@ -66,7 +63,7 @@ namespace llarp
randomize(const llarp_buffer_t &) override;
/// randomizer memory
void
randbytes(void *, size_t) override;
randbytes(byte_t *, size_t) override;
/// generate signing keypair
void
identity_keygen(SecretKey &) override;

@ -18,6 +18,7 @@ list(APPEND TEST_SRC
ev/test_ev_loop.cpp
exit/test_llarp_exit_context.cpp
link/test_llarp_link.cpp
llarp_test.cpp
metrics/test_llarp_metrics_metricktank.cpp
metrics/test_llarp_metrics_publisher.cpp
net/test_llarp_net_inaddr.cpp

@ -56,7 +56,7 @@ namespace llarp
MOCK_METHOD1(randomize, void(const llarp_buffer_t &));
MOCK_METHOD2(randbytes, void(void *, size_t));
MOCK_METHOD2(randbytes, void(byte_t *, size_t));
MOCK_METHOD1(identity_keygen, void(SecretKey &));

@ -3,6 +3,7 @@
#include <crypto/mock_crypto.hpp>
#include <dht/mock_context.hpp>
#include <dht/messages/gotintro.hpp>
#include <llarp_test.hpp>
#include <service/intro_set.hpp>
#include <test_util.hpp>
@ -21,10 +22,8 @@ struct MockIntroSetHandler
static constexpr uint64_t EXPIRY = 1548503831ull;
struct TestDhtServiceAddressLookup : public ::testing::Test
struct TestDhtServiceAddressLookup : public test::LlarpTest<>
{
test::MockCrypto crypto;
CryptoManager cm;
MockIntroSetHandler introsetHandler;
dht::Key_t ourKey;
@ -38,8 +37,7 @@ struct TestDhtServiceAddressLookup : public ::testing::Test
std::unique_ptr< dht::ServiceAddressLookup > serviceAddressLookup;
TestDhtServiceAddressLookup()
: cm(&crypto)
, ourKey(makeBuf< dht::Key_t >(0xFF))
: ourKey(makeBuf< dht::Key_t >(0xFF))
, txKey(makeBuf< dht::Key_t >(0x01))
, txId(2)
, txOwner(txKey, txId)
@ -65,7 +63,7 @@ TEST_F(TestDhtServiceAddressLookup, validate)
{
service::IntroSet introset;
EXPECT_CALL(context, Now()).WillOnce(Return(EXPIRY));
EXPECT_CALL(crypto, verify(_, _, _)).WillOnce(Return(false));
EXPECT_CALL(m_crypto, verify(_, _, _)).WillOnce(Return(false));
ASSERT_FALSE(serviceAddressLookup->Validate(introset));
}
@ -80,7 +78,7 @@ TEST_F(TestDhtServiceAddressLookup, validate)
// Set expectations
EXPECT_CALL(context, Now()).WillOnce(Return(EXPIRY));
EXPECT_CALL(crypto, verify(_, _, _)).WillOnce(Return(true));
EXPECT_CALL(m_crypto, verify(_, _, _)).WillOnce(Return(true));
ASSERT_FALSE(serviceAddressLookup->Validate(introset));
}
@ -98,7 +96,7 @@ TEST_F(TestDhtServiceAddressLookup, validate)
// Set expectations
EXPECT_CALL(context, Now()).WillOnce(Return(EXPIRY));
EXPECT_CALL(crypto, verify(_, _, _)).WillOnce(Return(true));
EXPECT_CALL(m_crypto, verify(_, _, _)).WillOnce(Return(true));
ASSERT_TRUE(serviceAddressLookup->Validate(introset));
}

@ -3,6 +3,7 @@
#include <crypto/mock_crypto.hpp>
#include <dht/mock_context.hpp>
#include <dht/messages/gotintro.hpp>
#include <llarp_test.hpp>
#include <service/intro_set.hpp>
#include <test_util.hpp>
@ -15,10 +16,8 @@ using test::makeBuf;
static constexpr uint64_t EXPIRY = 1548503831ull;
struct TestDhtTagLookup : public ::testing::Test
struct TestDhtTagLookup : public test::LlarpTest<>
{
test::MockCrypto crypto;
CryptoManager cm;
dht::Key_t txKey;
uint64_t txId;
dht::TXOwner txOwner;
@ -29,8 +28,7 @@ struct TestDhtTagLookup : public ::testing::Test
dht::TagLookup tagLookup;
TestDhtTagLookup()
: cm(&crypto)
, txKey(makeBuf< dht::Key_t >(0x01))
: txKey(makeBuf< dht::Key_t >(0x01))
, txId(2)
, txOwner(txKey, txId)
, tag(makeBuf< service::Tag >(0x03))
@ -50,7 +48,7 @@ TEST_F(TestDhtTagLookup, validate)
{
service::IntroSet introset;
EXPECT_CALL(context, Now()).WillOnce(Return(EXPIRY));
EXPECT_CALL(crypto, verify(_, _, _)).WillOnce(Return(false));
EXPECT_CALL(m_crypto, verify(_, _, _)).WillOnce(Return(false));
ASSERT_FALSE(tagLookup.Validate(introset));
}
@ -67,7 +65,7 @@ TEST_F(TestDhtTagLookup, validate)
// Set expectations
EXPECT_CALL(context, Now()).WillOnce(Return(EXPIRY));
EXPECT_CALL(crypto, verify(_, _, _)).WillOnce(Return(true));
EXPECT_CALL(m_crypto, verify(_, _, _)).WillOnce(Return(true));
ASSERT_FALSE(tagLookup.Validate(introset));
}
@ -84,7 +82,7 @@ TEST_F(TestDhtTagLookup, validate)
// Set expectations
EXPECT_CALL(context, Now()).WillOnce(Return(EXPIRY));
EXPECT_CALL(crypto, verify(_, _, _)).WillOnce(Return(true));
EXPECT_CALL(m_crypto, verify(_, _, _)).WillOnce(Return(true));
ASSERT_TRUE(tagLookup.Validate(introset));
}

@ -1,14 +1,19 @@
#include <crypto/crypto_libsodium.hpp>
#include <ev/ev.h>
#include <iwp/iwp.hpp>
#include <llarp_test.hpp>
#include <messages/link_intro.hpp>
#include <messages/discard.hpp>
#include <utp/utp.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <test_util.hpp>
#include <gtest/gtest.h>
struct LinkLayerTest : public ::testing::Test
using namespace ::llarp;
using namespace ::testing;
struct LinkLayerTest : public test::LlarpTest< sodium::CryptoLibSodium >
{
static constexpr uint16_t AlicePort = 5000;
static constexpr uint16_t BobPort = 6000;
@ -17,26 +22,26 @@ struct LinkLayerTest : public ::testing::Test
{
Context()
{
llarp::CryptoManager::instance()->identity_keygen(signingKey);
llarp::CryptoManager::instance()->encryption_keygen(encryptionKey);
CryptoManager::instance()->identity_keygen(signingKey);
CryptoManager::instance()->encryption_keygen(encryptionKey);
rc.pubkey = signingKey.toPublic();
rc.enckey = encryptionKey.toPublic();
}
llarp::SecretKey signingKey;
llarp::SecretKey encryptionKey;
SecretKey signingKey;
SecretKey encryptionKey;
llarp::RouterContact rc;
RouterContact rc;
bool gotLIM = false;
const llarp::RouterContact&
const RouterContact&
GetRC() const
{
return rc;
}
llarp::RouterID
RouterID
GetRouterID() const
{
return rc.pubkey;
@ -46,12 +51,12 @@ struct LinkLayerTest : public ::testing::Test
bool
Regen()
{
llarp::CryptoManager::instance()->encryption_keygen(encryptionKey);
rc.enckey = llarp::seckey_topublic(encryptionKey);
CryptoManager::instance()->encryption_keygen(encryptionKey);
rc.enckey = seckey_topublic(encryptionKey);
return rc.Sign(signingKey);
}
std::shared_ptr< llarp::ILinkLayer > link;
std::shared_ptr< ILinkLayer > link;
static std::string
localLoopBack()
@ -65,8 +70,7 @@ struct LinkLayerTest : public ::testing::Test
}
bool
Start(std::shared_ptr< llarp::Logic > logic, llarp_ev_loop_ptr loop,
uint16_t port)
Start(std::shared_ptr< Logic > logic, llarp_ev_loop_ptr loop, uint16_t port)
{
if(!link)
return false;
@ -97,31 +101,28 @@ struct LinkLayerTest : public ::testing::Test
}
};
llarp::sodium::CryptoLibSodium crypto;
llarp::CryptoManager cm;
Context Alice;
Context Bob;
bool success = false;
llarp_ev_loop_ptr netLoop;
std::shared_ptr< llarp::Logic > m_logic;
std::shared_ptr< Logic > m_logic;
llarp_time_t oldRCLifetime;
LinkLayerTest() : cm(&crypto), netLoop(nullptr)
LinkLayerTest() : netLoop(nullptr)
{
}
void
SetUp()
{
oldRCLifetime = llarp::RouterContact::Lifetime;
llarp::RouterContact::IgnoreBogons = true;
llarp::RouterContact::Lifetime = 500;
netLoop = llarp_make_ev_loop();
m_logic.reset(new llarp::Logic());
oldRCLifetime = RouterContact::Lifetime;
RouterContact::IgnoreBogons = true;
RouterContact::Lifetime = 500;
netLoop = llarp_make_ev_loop();
m_logic.reset(new Logic());
}
void
@ -131,8 +132,8 @@ struct LinkLayerTest : public ::testing::Test
Bob.TearDown();
m_logic.reset();
netLoop.reset();
llarp::RouterContact::IgnoreBogons = false;
llarp::RouterContact::Lifetime = oldRCLifetime;
RouterContact::IgnoreBogons = false;
RouterContact::Lifetime = oldRCLifetime;
}
static void
@ -167,10 +168,10 @@ struct LinkLayerTest : public ::testing::Test
TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
{
Alice.link = llarp::utp::NewServer(
Alice.link = utp::NewServer(
Alice.encryptionKey,
[&]() -> const llarp::RouterContact& { return Alice.GetRC(); },
[&](llarp::ILinkSession* s, const llarp_buffer_t& buf) -> bool {
[&]() -> const RouterContact& { return Alice.GetRC(); },
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
if(Alice.gotLIM)
{
Alice.Regen();
@ -178,7 +179,7 @@ TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
}
else
{
llarp::LinkIntroMessage msg;
LinkIntroMessage msg;
ManagedBuffer copy{buf};
if(!msg.BDecode(&copy.underlying))
return false;
@ -188,25 +189,25 @@ TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
return true;
}
},
[&](llarp::ILinkSession* s) -> bool {
[&](ILinkSession* s) -> bool {
const auto rc = s->GetRemoteRC();
return rc.pubkey == Bob.GetRC().pubkey;
},
[&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; },
[&](llarp::Signature& sig, const llarp_buffer_t& buf) -> bool {
return crypto.sign(sig, Alice.signingKey, buf);
[&](RouterContact, RouterContact) -> bool { return true; },
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Alice.signingKey, buf);
},
[&](llarp::ILinkSession* session) {
[&](ILinkSession* session) {
ASSERT_FALSE(session->IsEstablished());
Stop();
},
[&](llarp::RouterID router) { ASSERT_EQ(router, Bob.GetRouterID()); });
[&](RouterID router) { ASSERT_EQ(router, Bob.GetRouterID()); });
auto sendDiscardMessage = [](llarp::ILinkSession* s) -> bool {
auto sendDiscardMessage = [](ILinkSession* s) -> bool {
// send discard message in reply to complete unit test
std::array< byte_t, 32 > tmp;
llarp_buffer_t otherBuf(tmp);
llarp::DiscardMessage discard;
DiscardMessage discard;
if(!discard.BEncode(&otherBuf))
return false;
otherBuf.sz = otherBuf.cur - otherBuf.base;
@ -214,11 +215,10 @@ TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
return s->SendMessageBuffer(otherBuf);
};
Bob.link = llarp::utp::NewServer(
Bob.encryptionKey,
[&]() -> const llarp::RouterContact& { return Bob.GetRC(); },
[&](llarp::ILinkSession* s, const llarp_buffer_t& buf) -> bool {
llarp::LinkIntroMessage msg;
Bob.link = utp::NewServer(
Bob.encryptionKey, [&]() -> const RouterContact& { return Bob.GetRC(); },
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
LinkIntroMessage msg;
ManagedBuffer copy{buf};
if(!msg.BDecode(&copy.underlying))
return false;
@ -227,24 +227,22 @@ TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
Bob.gotLIM = true;
return sendDiscardMessage(s);
},
[&](llarp::ILinkSession* s) -> bool {
[&](ILinkSession* s) -> bool {
if(s->GetRemoteRC().pubkey != Alice.GetRC().pubkey)
return false;
llarp::LogInfo("bob established with alice");
LogInfo("bob established with alice");
return Bob.link->VisitSessionByPubkey(Alice.GetRC().pubkey.as_array(),
sendDiscardMessage);
},
[&](llarp::RouterContact newrc, llarp::RouterContact oldrc) -> bool {
[&](RouterContact newrc, RouterContact oldrc) -> bool {
success = newrc.pubkey == oldrc.pubkey;
return true;
},
[&](llarp::Signature& sig, const llarp_buffer_t& buf) -> bool {
return crypto.sign(sig, Bob.signingKey, buf);
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Bob.signingKey, buf);
},
[&](llarp::ILinkSession* session) {
ASSERT_FALSE(session->IsEstablished());
},
[&](llarp::RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); });
[&](ILinkSession* session) { ASSERT_FALSE(session->IsEstablished()); },
[&](RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); });
ASSERT_TRUE(Alice.Start(m_logic, netLoop, AlicePort));
ASSERT_TRUE(Bob.Start(m_logic, netLoop, BobPort));
@ -258,11 +256,11 @@ TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
{
Alice.link = llarp::utp::NewServer(
Alice.link = utp::NewServer(
Alice.encryptionKey,
[&]() -> const llarp::RouterContact& { return Alice.GetRC(); },
[&](llarp::ILinkSession* s, const llarp_buffer_t& buf) -> bool {
llarp::LinkIntroMessage lim;
[&]() -> const RouterContact& { return Alice.GetRC(); },
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
LinkIntroMessage lim;
llarp_buffer_t copy(buf.base, buf.sz);
if(lim.BDecode(&copy))
{
@ -275,27 +273,26 @@ TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
}
return AliceGotMessage(buf);
},
[&](llarp::ILinkSession* s) -> bool {
[&](ILinkSession* s) -> bool {
if(s->GetRemoteRC().pubkey != Bob.GetRC().pubkey)
return false;
llarp::LogInfo("alice established with bob");
LogInfo("alice established with bob");
return true;
},
[&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; },
[&](llarp::Signature& sig, const llarp_buffer_t& buf) -> bool {
return crypto.sign(sig, Alice.signingKey, buf);
[&](RouterContact, RouterContact) -> bool { return true; },
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Alice.signingKey, buf);
},
[&](llarp::ILinkSession* session) {
[&](ILinkSession* session) {
ASSERT_FALSE(session->IsEstablished());
Stop();
},
[&](llarp::RouterID router) { ASSERT_EQ(router, Bob.GetRouterID()); });
[&](RouterID router) { ASSERT_EQ(router, Bob.GetRouterID()); });
Bob.link = llarp::utp::NewServer(
Bob.encryptionKey,
[&]() -> const llarp::RouterContact& { return Bob.GetRC(); },
[&](llarp::ILinkSession* s, const llarp_buffer_t& buf) -> bool {
llarp::LinkIntroMessage lim;
Bob.link = utp::NewServer(
Bob.encryptionKey, [&]() -> const RouterContact& { return Bob.GetRC(); },
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
LinkIntroMessage lim;
llarp_buffer_t copy(buf.base, buf.sz);
if(lim.BDecode(&copy))
{
@ -308,16 +305,16 @@ TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
}
return true;
},
[&](llarp::ILinkSession* s) -> bool {
[&](ILinkSession* s) -> bool {
if(s->GetRemoteRC().pubkey != Alice.GetRC().pubkey)
return false;
llarp::LogInfo("bob established with alice");
LogInfo("bob established with alice");
m_logic->queue_job({s, [](void* u) {
llarp::ILinkSession* self =
static_cast< llarp::ILinkSession* >(u);
ILinkSession* self =
static_cast< ILinkSession* >(u);
std::array< byte_t, 32 > tmp;
llarp_buffer_t otherBuf(tmp);
llarp::DiscardMessage discard;
DiscardMessage discard;
if(!discard.BEncode(&otherBuf))
return;
otherBuf.sz = otherBuf.cur - otherBuf.base;
@ -326,14 +323,12 @@ TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
}});
return true;
},
[&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; },
[&](llarp::Signature& sig, const llarp_buffer_t& buf) -> bool {
return crypto.sign(sig, Bob.signingKey, buf);
},
[&](llarp::ILinkSession* session) {
ASSERT_FALSE(session->IsEstablished());
[&](RouterContact, RouterContact) -> bool { return true; },
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Bob.signingKey, buf);
},
[&](llarp::RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); });
[&](ILinkSession* session) { ASSERT_FALSE(session->IsEstablished()); },
[&](RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); });
ASSERT_TRUE(Alice.Start(m_logic, netLoop, AlicePort));
ASSERT_TRUE(Bob.Start(m_logic, netLoop, BobPort));
@ -344,93 +339,3 @@ TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
ASSERT_TRUE(Bob.gotLIM);
ASSERT_TRUE(success);
}
/*
TEST_F(LinkLayerTest, TestIWPAliceConnectToBob)
{
Alice.link = llarp::iwp::NewServer(
&crypto, Alice.encryptionKey,
[&]() -> const llarp::RouterContact& { return Alice.GetRC(); },
[&](llarp::ILinkSession* s, const llarp_buffer_t& buf) -> bool {
if(Alice.gotLIM)
{
return AliceGotMessage(buf);
}
else
{
llarp::LinkIntroMessage msg;
ManagedBuffer copy{buf};
if(!msg.BDecode(&copy.underlying))
return false;
if(!s->GotLIM(&msg))
return false;
Alice.gotLIM = true;
return true;
}
},
[&](llarp::RouterContact rc) {
ASSERT_EQ(rc, Bob.GetRC());
llarp::LogInfo("alice established with bob");
},
[&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; },
[&](llarp::Signature& sig, const llarp_buffer_t& buf) -> bool {
return crypto.sign(sig, Alice.signingKey, buf);
},
[&](llarp::ILinkSession* session) {
ASSERT_FALSE(session->IsEstablished());
Stop();
},
[&](llarp::RouterID router) { ASSERT_EQ(router, Bob.GetRouterID()); });
auto sendDiscardMessage = [](llarp::ILinkSession* s) -> bool {
// send discard message in reply to complete unit test
std::array< byte_t, 32 > tmp;
llarp_buffer_t otherBuf(tmp);
llarp::DiscardMessage discard;
if(!discard.BEncode(&otherBuf))
return false;
otherBuf.sz = otherBuf.cur - otherBuf.base;
otherBuf.cur = otherBuf.base;
return s->SendMessageBuffer(otherBuf);
};
Bob.link = llarp::iwp::NewServer(
&crypto, Bob.encryptionKey,
[&]() -> const llarp::RouterContact& { return Bob.GetRC(); },
[&](llarp::ILinkSession* s, const llarp_buffer_t& buf) -> bool {
llarp::LinkIntroMessage msg;
ManagedBuffer copy{buf};
if(!msg.BDecode(&copy.underlying))
return false;
if(!s->GotLIM(&msg))
return false;
Bob.gotLIM = true;
return true;
},
[&](llarp::RouterContact rc) {
ASSERT_EQ(rc, Alice.GetRC());
llarp::LogInfo("bob established with alice");
Bob.link->VisitSessionByPubkey(Alice.GetRC().pubkey.as_array(),
sendDiscardMessage);
},
[&](llarp::RouterContact, llarp::RouterContact) -> bool { return true; },
[&](llarp::Signature& sig, const llarp_buffer_t& buf) -> bool {
return crypto.sign(sig, Bob.signingKey, buf);
},
[&](llarp::ILinkSession* session) {
ASSERT_FALSE(session->IsEstablished());
},
[&](llarp::RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); });
ASSERT_TRUE(Alice.Start(m_logic.get(), netLoop, AlicePort));
ASSERT_TRUE(Bob.Start(m_logic.get(), netLoop, BobPort));
ASSERT_TRUE(Alice.link->TryEstablishTo(Bob.GetRC()));
RunMainloop();
ASSERT_TRUE(Alice.gotLIM);
ASSERT_TRUE(Bob.gotLIM);
ASSERT_TRUE(success);
};
*/

@ -0,0 +1 @@
#include <llarp_test.hpp>

@ -0,0 +1,26 @@
#ifndef LLARP_TEST
#define LLARP_TEST
#include <gtest/gtest.h>
#include <crypto/mock_crypto.hpp>
namespace llarp
{
namespace test
{
template < typename CryptoImpl = MockCrypto >
class LlarpTest : public ::testing::Test
{
protected:
CryptoImpl m_crypto;
CryptoManager cm;
LlarpTest() : cm(&m_crypto)
{
static_assert(std::is_base_of< Crypto, CryptoImpl >::value, "");
}
};
} // namespace test
} // namespace llarp
#endif

@ -2,33 +2,45 @@
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp_test.hpp>
#include <gtest/gtest.h>
#include <gmock/gmock.h>
using ObtainExitMessage = llarp::routing::ObtainExitMessage;
using namespace ::testing;
using namespace ::llarp;
class ObtainExitTest : public ::testing::Test
using ObtainExitMessage = routing::ObtainExitMessage;
class ObtainExitTest : public test::LlarpTest<>
{
public:
llarp::sodium::CryptoLibSodium crypto;
llarp::CryptoManager cm;
llarp::SecretKey alice;
SecretKey alice;
ObtainExitTest() : cm(&crypto)
ObtainExitTest()
{
crypto.identity_keygen(alice);
// m_crypto.identity_keygen(alice);
}
};
void
fill(Signature& s)
{
s.Fill(0xFF);
}
TEST_F(ObtainExitTest, TestSignVerify)
{
EXPECT_CALL(m_crypto, sign(_, alice, _))
.WillOnce(DoAll(WithArg< 0 >(Invoke(&fill)), Return(true)));
EXPECT_CALL(m_crypto, verify(_, _, _)).WillOnce(Return(true));
ObtainExitMessage msg;
msg.Z.Zero();
msg.S = llarp::randint();
msg.T = llarp::randint();
msg.S = randint();
msg.T = randint();
EXPECT_TRUE(msg.Sign(alice));
EXPECT_TRUE(msg.Verify());
EXPECT_TRUE(msg.I == llarp::PubKey(llarp::seckey_topublic(alice)));
EXPECT_TRUE(msg.I == PubKey(seckey_topublic(alice)));
EXPECT_FALSE(msg.version != LLARP_PROTO_VERSION);
EXPECT_FALSE(msg.Z.IsZero());
}

@ -1,5 +1,6 @@
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp_test.hpp>
#include <path/path.hpp>
#include <service/address.hpp>
#include <service/identity.hpp>
@ -15,23 +16,9 @@
using namespace llarp;
using namespace testing;
struct HiddenServiceTest : public ::testing::Test
struct HiddenServiceTest : public test::LlarpTest<>
{
sodium::CryptoLibSodium crypto;
CryptoManager cm;
service::Identity ident;
HiddenServiceTest() : cm(&crypto)
{
ident.RegenerateKeys();
ident.pub.RandomizeVanity();
ident.pub.UpdateAddr();
}
void
SetUp()
{
}
};
TEST_F(HiddenServiceTest, TestGenerateIntroSet)
@ -49,6 +36,10 @@ TEST_F(HiddenServiceTest, TestGenerateIntroSet)
intro.pathID.Randomize();
I.I.emplace_back(std::move(intro));
}
EXPECT_CALL(m_crypto, sign(I.Z, _, _)).WillOnce(Return(true));
EXPECT_CALL(m_crypto, verify(_, _, I.Z)).WillOnce(Return(true));
ASSERT_TRUE(ident.SignIntroSet(I, now));
ASSERT_TRUE(I.Verify(now));
}
@ -61,11 +52,9 @@ TEST_F(HiddenServiceTest, TestAddressToFromString)
ASSERT_TRUE(addr == ident.pub.Addr());
}
struct ServiceIdentityTest : public ::testing::Test
struct ServiceIdentityTest : public test::LlarpTest<>
{
test::MockCrypto crypto;
CryptoManager cm;
ServiceIdentityTest() : cm(&crypto)
ServiceIdentityTest()
{
}
};
@ -84,13 +73,13 @@ TEST_F(ServiceIdentityTest, EnsureKeys)
test::FileGuard guard(p);
EXPECT_CALL(crypto, encryption_keygen(_))
EXPECT_CALL(m_crypto, encryption_keygen(_))
.WillOnce(WithArg< 0 >(FillArg< SecretKey >(0x01)));
EXPECT_CALL(crypto, identity_keygen(_))
EXPECT_CALL(m_crypto, identity_keygen(_))
.WillOnce(WithArg< 0 >(FillArg< SecretKey >(0x02)));
EXPECT_CALL(crypto, pqe_keygen(_))
EXPECT_CALL(m_crypto, pqe_keygen(_))
.WillOnce(WithArg< 0 >(FillArg< PQKeyPair >(0x03)));
service::Identity identity;

@ -2,27 +2,25 @@
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp_test.hpp>
#include <messages/relay_commit.hpp>
#include <test_util.hpp>
#include <gtest/gtest.h>
using EncryptedFrame = llarp::EncryptedFrame;
using SecretKey = llarp::SecretKey;
using PubKey = llarp::PubKey;
using LRCR = llarp::LR_CommitRecord;
using namespace ::llarp;
using namespace ::testing;
using EncryptedFrame = EncryptedFrame;
using SecretKey = SecretKey;
using PubKey = PubKey;
using LRCR = LR_CommitRecord;
class FrameTest : public ::testing::Test
class FrameTest : public test::LlarpTest<>
{
public:
llarp::sodium::CryptoLibSodium crypto;
llarp::CryptoManager cm;
SecretKey alice, bob;
FrameTest() : cm(&crypto)
{
crypto.encryption_keygen(alice);
crypto.encryption_keygen(bob);
}
};
TEST_F(FrameTest, TestFrameCrypto)
@ -36,14 +34,26 @@ TEST_F(FrameTest, TestFrameCrypto)
record.txid.Fill(4);
auto buf = f.Buffer();
buf->cur = buf->base + llarp::EncryptedFrameOverheadSize;
buf->cur = buf->base + EncryptedFrameOverheadSize;
ASSERT_TRUE(record.BEncode(buf));
EXPECT_CALL(m_crypto, randbytes(_, _))
.WillOnce(Invoke(&test::randbytes_impl));
EXPECT_CALL(m_crypto, dh_client(_, _, alice, _)).WillOnce(Return(true));
EXPECT_CALL(m_crypto, xchacha20(_, _, _))
.Times(2)
.WillRepeatedly(Return(true));
EXPECT_CALL(m_crypto, hmac(_, _, _)).Times(2).WillRepeatedly(Return(true));
// rewind buffer
buf->cur = buf->base + llarp::EncryptedFrameOverheadSize;
buf->cur = buf->base + EncryptedFrameOverheadSize;
// encrypt to alice
ASSERT_TRUE(f.EncryptInPlace(alice, bob.toPublic()));
EXPECT_CALL(m_crypto, dh_server(_, _, _, _)).WillOnce(Return(true));
// decrypt from alice
ASSERT_TRUE(f.DecryptInPlace(bob));

@ -2,6 +2,7 @@
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp_test.hpp>
#include <functional>
#include <random>
@ -9,17 +10,14 @@
#include <test_util.hpp>
#include <gtest/gtest.h>
using FindOrCreateFunc =
std::function< bool(const fs::path &, llarp::SecretKey &) >;
using namespace ::llarp;
using namespace ::testing;
struct FindOrCreate : public ::testing::TestWithParam< FindOrCreateFunc >
{
FindOrCreate() : cm(&crypto)
{
}
using FindOrCreateFunc = std::function< bool(const fs::path &, SecretKey &) >;
llarp::sodium::CryptoLibSodium crypto;
llarp::CryptoManager cm;
struct FindOrCreate : public test::LlarpTest<>,
public WithParamInterface< FindOrCreateFunc >
{
};
// Concerns
@ -30,11 +28,19 @@ struct FindOrCreate : public ::testing::TestWithParam< FindOrCreateFunc >
TEST_P(FindOrCreate, find_file_missing)
{
// File missing. Should create a new file
llarp::SecretKey key;
fs::path p = llarp::test::randFilename();
SecretKey key;
fs::path p = test::randFilename();
ASSERT_FALSE(fs::exists(fs::status(p)));
llarp::test::FileGuard guard(p);
test::FileGuard guard(p);
EXPECT_CALL(m_crypto, encryption_keygen(_))
.Times(AtMost(1))
.WillRepeatedly(Invoke(&test::keygen< SecretKey >));
EXPECT_CALL(m_crypto, identity_keygen(_))
.Times(AtMost(1))
.WillRepeatedly(Invoke(&test::keygen< SecretKey >));
ASSERT_TRUE(GetParam()(p, key));
ASSERT_TRUE(fs::exists(fs::status(p)));
@ -44,15 +50,15 @@ TEST_P(FindOrCreate, find_file_missing)
TEST_P(FindOrCreate, find_file_empty)
{
// File empty.
llarp::SecretKey key;
fs::path p = llarp::test::randFilename();
SecretKey key;
fs::path p = test::randFilename();
ASSERT_FALSE(fs::exists(fs::status(p)));
std::fstream f;
f.open(p.string(), std::ios::out);
f.close();
llarp::test::FileGuard guard(p);
test::FileGuard guard(p);
ASSERT_FALSE(GetParam()(p, key));
// Verify we didn't delete an invalid file
@ -62,8 +68,8 @@ TEST_P(FindOrCreate, find_file_empty)
TEST_P(FindOrCreate, happy_path)
{
// happy path.
llarp::SecretKey key;
fs::path p = llarp::test::randFilename();
SecretKey key;
fs::path p = test::randFilename();
ASSERT_FALSE(fs::exists(fs::status(p)));
std::ofstream f;
@ -71,7 +77,7 @@ TEST_P(FindOrCreate, happy_path)
std::fill_n(std::ostream_iterator< byte_t >(f), key.size(), 0x20);
f.close();
llarp::test::FileGuard guard(p);
test::FileGuard guard(p);
ASSERT_TRUE(GetParam()(p, key));
// Verify we didn't delete the file

@ -2,44 +2,48 @@
#include <crypto/crypto.hpp>
#include <crypto/crypto_libsodium.hpp>
#include <llarp_test.hpp>
#include <router_contact.hpp>
using namespace ::llarp;
using namespace ::testing;
static const byte_t DEF_VALUE[] = "unittest";
struct RCTest : public ::testing::Test
struct RCTest : public test::LlarpTest<>
{
using RC_t = llarp::RouterContact;
using SecKey_t = llarp::SecretKey;
using RC_t = RouterContact;
using SecKey_t = SecretKey;
RCTest() : cm(&crypto), oldval(llarp::NetID::DefaultValue())
RCTest() : oldval(NetID::DefaultValue())
{
llarp::NetID::DefaultValue() = llarp::NetID(DEF_VALUE);
NetID::DefaultValue() = NetID(DEF_VALUE);
}
~RCTest()
{
llarp::NetID::DefaultValue() = oldval;
NetID::DefaultValue() = oldval;
}
llarp::sodium::CryptoLibSodium crypto;
llarp::CryptoManager cm;
const llarp::NetID oldval;
const NetID oldval;
};
TEST_F(RCTest, TestSignVerify)
{
llarp::NetID netid(DEF_VALUE);
NetID netid(DEF_VALUE);
RC_t rc;
SecKey_t encr;
SecKey_t sign;
crypto.encryption_keygen(encr);
crypto.identity_keygen(sign);
rc.enckey = encr.toPublic();
rc.pubkey = sign.toPublic();
rc.exits.emplace_back(rc.pubkey, llarp::nuint32_t{50000});
rc.exits.emplace_back(rc.pubkey, nuint32_t{50000});
ASSERT_TRUE(rc.netID == netid);
ASSERT_TRUE(rc.netID == llarp::NetID::DefaultValue());
ASSERT_TRUE(rc.netID == NetID::DefaultValue());
EXPECT_CALL(m_crypto, sign(_, sign, _)).WillOnce(Return(true));
EXPECT_CALL(m_crypto, verify(_, _, _)).WillOnce(Return(true));
ASSERT_TRUE(rc.Sign(sign));
ASSERT_TRUE(rc.Verify(llarp::time_now_ms()));
ASSERT_TRUE(rc.Verify(time_now_ms()));
}

@ -40,6 +40,26 @@ namespace llarp
}
};
inline void
randbytes_impl(byte_t *ptr, size_t sz)
{
std::fill_n(ptr, sz, 0xAA);
}
template < typename T >
inline void
keygen_val(T &val, byte_t x)
{
val.Fill(x);
}
template < typename T >
inline void
keygen(T &val)
{
keygen_val(val, 0xAA);
}
template < typename T >
struct CombinationIterator
{

Loading…
Cancel
Save