lokinet/test/link/test_llarp_link.cpp

281 lines
6.4 KiB
C++
Raw Normal View History

2019-08-22 20:53:27 +00:00
#include <crypto/crypto_libsodium.hpp>
2019-01-11 01:19:36 +00:00
#include <ev/ev.h>
2019-03-29 15:17:49 +00:00
#include <iwp/iwp.hpp>
2019-05-28 19:45:09 +00:00
#include <llarp_test.hpp>
2019-08-22 20:53:27 +00:00
#include <iwp/iwp.hpp>
2019-01-05 13:45:05 +00:00
#include <messages/link_intro.hpp>
#include <messages/discard.hpp>
2019-01-05 13:45:05 +00:00
2019-08-22 11:18:05 +00:00
2019-05-28 19:45:09 +00:00
#include <test_util.hpp>
#include <gtest/gtest.h>
2019-05-28 19:45:09 +00:00
using namespace ::llarp;
using namespace ::testing;
2019-08-22 20:53:27 +00:00
struct LinkLayerTest : public test::LlarpTest< llarp::sodium::CryptoLibSodium >
2019-01-05 13:45:05 +00:00
{
static constexpr uint16_t AlicePort = 5000;
static constexpr uint16_t BobPort = 6000;
struct Context
{
Context()
2019-01-05 13:45:05 +00:00
{
2019-05-28 19:45:09 +00:00
CryptoManager::instance()->identity_keygen(signingKey);
CryptoManager::instance()->encryption_keygen(encryptionKey);
2019-03-28 19:15:20 +00:00
rc.pubkey = signingKey.toPublic();
rc.enckey = encryptionKey.toPublic();
2019-01-05 13:45:05 +00:00
}
2019-09-05 17:39:09 +00:00
std::shared_ptr<thread::ThreadPool> worker;
2019-05-28 19:45:09 +00:00
SecretKey signingKey;
SecretKey encryptionKey;
2019-01-05 13:45:05 +00:00
2019-05-28 19:45:09 +00:00
RouterContact rc;
2019-01-05 13:45:05 +00:00
bool gotLIM = false;
2019-09-05 17:39:09 +00:00
void Setup()
{
worker = std::make_shared<thread::ThreadPool>(1, 128, "test-worker");
worker->start();
}
2019-05-28 19:45:09 +00:00
const RouterContact&
2019-01-05 13:45:05 +00:00
GetRC() const
{
return rc;
}
2019-05-28 19:45:09 +00:00
RouterID
2019-01-05 13:45:05 +00:00
GetRouterID() const
{
return rc.pubkey;
}
/// regenerate rc and rotate onion key
bool
Regen()
{
2019-05-28 19:45:09 +00:00
CryptoManager::instance()->encryption_keygen(encryptionKey);
rc.enckey = seckey_topublic(encryptionKey);
return rc.Sign(signingKey);
2019-01-05 13:45:05 +00:00
}
2019-05-28 19:45:09 +00:00
std::shared_ptr< ILinkLayer > link;
2019-01-05 13:45:05 +00:00
static std::string
localLoopBack()
{
#if defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__NetBSD__) \
2019-08-26 13:44:01 +00:00
|| (__APPLE__ && __MACH__) || (__sun)
2019-01-05 13:45:05 +00:00
return "lo0";
#else
return "lo";
#endif
}
bool
2019-05-28 19:45:09 +00:00
Start(std::shared_ptr< Logic > logic, llarp_ev_loop_ptr loop, uint16_t port)
2019-01-05 13:45:05 +00:00
{
if(!link)
return false;
if(!link->Configure(loop, localLoopBack(), AF_INET, port))
return false;
if(!link->GenEphemeralKeys())
return false;
rc.addrs.emplace_back();
if(!link->GetOurAddressInfo(rc.addrs[0]))
return false;
if(!rc.Sign(signingKey))
2019-01-05 13:45:05 +00:00
return false;
2019-09-05 17:39:09 +00:00
return link->Start(logic, worker);
2019-01-05 13:45:05 +00:00
}
void
Stop()
{
if(link)
link->Stop();
2019-09-05 17:39:09 +00:00
if(worker)
{
worker->drain();
2019-09-05 17:39:09 +00:00
worker->stop();
}
2019-01-05 13:45:05 +00:00
}
void
TearDown()
{
link.reset();
2019-09-05 17:39:09 +00:00
worker.reset();
2019-01-05 13:45:05 +00:00
}
};
Context Alice;
Context Bob;
bool success = false;
2019-04-08 12:01:52 +00:00
llarp_ev_loop_ptr netLoop;
2019-05-28 19:45:09 +00:00
std::shared_ptr< Logic > m_logic;
2019-01-05 13:45:05 +00:00
llarp_time_t oldRCLifetime;
2019-05-28 19:45:09 +00:00
LinkLayerTest() : netLoop(nullptr)
2019-01-05 13:45:05 +00:00
{
}
void
SetUp()
{
2019-08-26 23:29:17 +00:00
oldRCLifetime = RouterContact::Lifetime;
RouterContact::BlockBogons = false;
RouterContact::Lifetime = 500;
netLoop = llarp_make_ev_loop();
2019-05-28 19:45:09 +00:00
m_logic.reset(new Logic());
2019-09-05 17:39:09 +00:00
Alice.Setup();
Bob.Setup();
2019-01-05 13:45:05 +00:00
}
void
TearDown()
{
Alice.TearDown();
Bob.TearDown();
2019-04-25 23:21:19 +00:00
m_logic.reset();
2019-04-08 12:01:52 +00:00
netLoop.reset();
2019-08-26 23:29:17 +00:00
RouterContact::BlockBogons = true;
RouterContact::Lifetime = oldRCLifetime;
2019-01-05 13:45:05 +00:00
}
static void
OnTimeout(void* u, uint64_t, uint64_t left)
{
if(left)
return;
2019-06-02 21:17:05 +00:00
llarp::LogInfo("timed out test");
2019-01-05 13:45:05 +00:00
static_cast< LinkLayerTest* >(u)->Stop();
}
void
RunMainloop()
{
2019-04-25 23:21:19 +00:00
m_logic->call_later({5000, this, &OnTimeout});
2019-07-09 13:47:24 +00:00
llarp_ev_loop_run_single_process(netLoop, m_logic);
2019-01-05 13:45:05 +00:00
}
void
Stop()
{
m_logic->queue_func([&]() {
Alice.Stop();
Bob.Stop();
llarp_ev_loop_stop(netLoop);
});
2019-01-05 13:45:05 +00:00
}
bool
AliceGotMessage(const llarp_buffer_t&)
2019-01-05 13:45:05 +00:00
{
success = true;
Stop();
return true;
}
};
2019-08-22 20:53:27 +00:00
TEST_F(LinkLayerTest, TestIWP)
2019-01-05 13:45:05 +00:00
{
2019-07-18 22:12:14 +00:00
#ifdef WIN32
2019-08-23 20:49:12 +00:00
GTEST_SKIP();
2019-07-18 22:12:14 +00:00
#else
2019-08-22 20:53:27 +00:00
Alice.link = iwp::NewInboundLink(
Alice.encryptionKey,
2019-05-28 19:45:09 +00:00
[&]() -> const RouterContact& { return Alice.GetRC(); },
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
2019-01-05 13:45:05 +00:00
if(Alice.gotLIM)
{
Alice.Regen();
return s->RenegotiateSession();
}
else
{
2019-05-28 19:45:09 +00:00
LinkIntroMessage msg;
2019-02-03 00:48:10 +00:00
ManagedBuffer copy{buf};
2019-02-02 23:12:42 +00:00
if(!msg.BDecode(&copy.underlying))
2019-01-05 13:45:05 +00:00
return false;
if(!s->GotLIM(&msg))
return false;
Alice.gotLIM = true;
return true;
}
},
2019-08-22 11:18:05 +00:00
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Alice.signingKey, buf);
},
2019-05-28 19:45:09 +00:00
[&](ILinkSession* s) -> bool {
2019-02-27 12:55:26 +00:00
const auto rc = s->GetRemoteRC();
return rc.pubkey == Bob.GetRC().pubkey;
2019-01-05 13:45:05 +00:00
},
2019-05-28 19:45:09 +00:00
[&](RouterContact, RouterContact) -> bool { return true; },
2019-08-22 11:18:05 +00:00
[&](ILinkSession* session) {
ASSERT_FALSE(session->IsEstablished());
Stop();
},
[&](RouterID router) { ASSERT_EQ(router, Bob.GetRouterID()); });
auto sendDiscardMessage = [](ILinkSession* s) -> bool {
// send discard message in reply to complete unit test
std::vector< byte_t> tmp(32);
2019-08-22 11:18:05 +00:00
llarp_buffer_t otherBuf(tmp);
DiscardMessage discard;
if(!discard.BEncode(&otherBuf))
return false;
return s->SendMessageBuffer(std::move(tmp), nullptr);
2019-08-22 11:18:05 +00:00
};
2019-08-22 20:53:27 +00:00
Bob.link = iwp::NewInboundLink(
2019-08-22 11:18:05 +00:00
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;
if(!s->GotLIM(&msg))
return false;
Bob.gotLIM = true;
return sendDiscardMessage(s);
},
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Bob.signingKey, buf);
},
[&](ILinkSession* s) -> bool {
if(s->GetRemoteRC().pubkey != Alice.GetRC().pubkey)
return false;
LogInfo("bob established with alice");
return Bob.link->VisitSessionByPubkey(Alice.GetRC().pubkey.as_array(),
sendDiscardMessage);
},
[&](RouterContact newrc, RouterContact oldrc) -> bool {
success = newrc.pubkey == oldrc.pubkey;
return true;
},
[&](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));
m_logic->queue_func([&]() { ASSERT_TRUE(Alice.link->TryEstablishTo(Bob.GetRC())); });
2019-08-22 11:18:05 +00:00
RunMainloop();
ASSERT_TRUE(Bob.gotLIM);
2019-08-23 20:49:12 +00:00
#endif
2019-08-22 11:18:05 +00:00
};