lokinet/test/link/test_llarp_link.cpp

351 lines
9.1 KiB
C++
Raw Normal View History

#include <crypto/crypto_noop.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-01-05 13:45:05 +00:00
#include <messages/link_intro.hpp>
#include <messages/discard.hpp>
2019-03-29 14:23:19 +00:00
#include <utp/utp.hpp>
2019-01-05 13:45: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;
struct LinkLayerTest : public test::LlarpTest< NoOpCrypto >
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-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-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__) \
|| (__APPLE__ && __MACH__)
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;
return link->Start(logic);
}
void
Stop()
{
if(link)
link->Stop();
}
void
TearDown()
{
Stop();
link.reset();
}
};
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-05-28 19:45:09 +00:00
oldRCLifetime = RouterContact::Lifetime;
RouterContact::IgnoreBogons = true;
RouterContact::Lifetime = 500;
netLoop = llarp_make_ev_loop();
2019-05-28 19:45:09 +00:00
m_logic.reset(new Logic());
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-05-28 19:45:09 +00:00
RouterContact::IgnoreBogons = false;
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()
{
2019-06-02 21:17:05 +00:00
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;
}
};
TEST_F(LinkLayerTest, TestUTPAliceRenegWithBob)
{
2019-07-18 22:12:14 +00:00
#ifdef WIN32
GTEST_SKIP();
#else
2019-05-28 19:45:09 +00:00
Alice.link = utp::NewServer(
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-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; },
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Alice.signingKey, buf);
2019-01-05 13:45:05 +00:00
},
2019-05-28 19:45:09 +00:00
[&](ILinkSession* session) {
2019-01-05 13:45:05 +00:00
ASSERT_FALSE(session->IsEstablished());
Stop();
},
2019-05-28 19:45:09 +00:00
[&](RouterID router) { ASSERT_EQ(router, Bob.GetRouterID()); });
2019-01-05 13:45:05 +00:00
2019-05-28 19:45:09 +00:00
auto sendDiscardMessage = [](ILinkSession* s) -> bool {
2019-01-05 13:45:05 +00:00
// send discard message in reply to complete unit test
2019-02-02 23:12:42 +00:00
std::array< byte_t, 32 > tmp;
llarp_buffer_t otherBuf(tmp);
2019-05-28 19:45:09 +00:00
DiscardMessage discard;
2019-01-05 13:45:05 +00:00
if(!discard.BEncode(&otherBuf))
return false;
2019-07-18 22:12:14 +00:00
otherBuf.sz = otherBuf.cur - otherBuf.base;
2019-01-05 13:45:05 +00:00
otherBuf.cur = otherBuf.base;
2019-07-26 16:19:31 +00:00
return s->SendMessageBuffer(otherBuf, nullptr);
2019-01-05 13:45:05 +00:00
};
2019-05-28 19:45:09 +00:00
Bob.link = utp::NewServer(
Bob.encryptionKey, [&]() -> const RouterContact& { return Bob.GetRC(); },
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
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;
Bob.gotLIM = true;
return sendDiscardMessage(s);
},
2019-05-28 19:45:09 +00:00
[&](ILinkSession* s) -> bool {
2019-02-27 12:55:26 +00:00
if(s->GetRemoteRC().pubkey != Alice.GetRC().pubkey)
return false;
2019-05-28 19:45:09 +00:00
LogInfo("bob established with alice");
2019-02-27 12:55:26 +00:00
return Bob.link->VisitSessionByPubkey(Alice.GetRC().pubkey.as_array(),
2019-03-04 16:31:05 +00:00
sendDiscardMessage);
2019-01-05 13:45:05 +00:00
},
2019-05-28 19:45:09 +00:00
[&](RouterContact newrc, RouterContact oldrc) -> bool {
2019-01-05 13:45:05 +00:00
success = newrc.pubkey == oldrc.pubkey;
return true;
},
2019-05-28 19:45:09 +00:00
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Bob.signingKey, buf);
2019-01-05 13:45:05 +00:00
},
2019-05-28 19:45:09 +00:00
[&](ILinkSession* session) { ASSERT_FALSE(session->IsEstablished()); },
[&](RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); });
2019-01-05 13:45:05 +00:00
ASSERT_TRUE(Alice.Start(m_logic, netLoop, AlicePort));
ASSERT_TRUE(Bob.Start(m_logic, netLoop, BobPort));
2019-01-05 13:45:05 +00:00
ASSERT_TRUE(Alice.link->TryEstablishTo(Bob.GetRC()));
RunMainloop();
ASSERT_TRUE(Bob.gotLIM);
ASSERT_TRUE(success);
2019-07-18 22:12:14 +00:00
#endif
2019-01-05 13:45:05 +00:00
}
TEST_F(LinkLayerTest, TestUTPAliceConnectToBob)
{
2019-07-18 22:12:14 +00:00
#ifdef WIN32
GTEST_SKIP();
#else
2019-05-28 19:45:09 +00:00
Alice.link = utp::NewServer(
Alice.encryptionKey,
2019-05-28 19:45:09 +00:00
[&]() -> const RouterContact& { return Alice.GetRC(); },
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
LinkIntroMessage lim;
2019-03-04 16:31:05 +00:00
llarp_buffer_t copy(buf.base, buf.sz);
if(lim.BDecode(&copy))
{
if(s->GotLIM(&lim))
{
Alice.gotLIM = true;
return true;
}
return false;
}
2019-02-27 12:55:26 +00:00
return AliceGotMessage(buf);
2019-01-05 13:45:05 +00:00
},
2019-05-28 19:45:09 +00:00
[&](ILinkSession* s) -> bool {
2019-02-27 12:55:26 +00:00
if(s->GetRemoteRC().pubkey != Bob.GetRC().pubkey)
return false;
2019-05-28 19:45:09 +00:00
LogInfo("alice established with bob");
2019-02-27 12:55:26 +00:00
return true;
2019-01-05 13:45:05 +00:00
},
2019-05-28 19:45:09 +00:00
[&](RouterContact, RouterContact) -> bool { return true; },
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Alice.signingKey, buf);
2019-01-05 13:45:05 +00:00
},
2019-05-28 19:45:09 +00:00
[&](ILinkSession* session) {
2019-01-05 13:45:05 +00:00
ASSERT_FALSE(session->IsEstablished());
Stop();
},
2019-05-28 19:45:09 +00:00
[&](RouterID router) { ASSERT_EQ(router, Bob.GetRouterID()); });
2019-01-05 13:45:05 +00:00
2019-05-28 19:45:09 +00:00
Bob.link = utp::NewServer(
Bob.encryptionKey, [&]() -> const RouterContact& { return Bob.GetRC(); },
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
LinkIntroMessage lim;
2019-03-04 16:31:05 +00:00
llarp_buffer_t copy(buf.base, buf.sz);
if(lim.BDecode(&copy))
{
if(s->GotLIM(&lim))
{
Bob.gotLIM = true;
return true;
}
return false;
}
2019-01-05 13:45:05 +00:00
return true;
},
2019-05-28 19:45:09 +00:00
[&](ILinkSession* s) -> bool {
2019-02-27 12:55:26 +00:00
if(s->GetRemoteRC().pubkey != Alice.GetRC().pubkey)
return false;
2019-05-28 19:45:09 +00:00
LogInfo("bob established with alice");
2019-04-25 23:21:19 +00:00
m_logic->queue_job({s, [](void* u) {
2019-05-28 19:45:09 +00:00
ILinkSession* self =
static_cast< ILinkSession* >(u);
2019-04-25 23:21:19 +00:00
std::array< byte_t, 32 > tmp;
llarp_buffer_t otherBuf(tmp);
2019-05-28 19:45:09 +00:00
DiscardMessage discard;
2019-04-25 23:21:19 +00:00
if(!discard.BEncode(&otherBuf))
return;
2019-07-18 22:12:14 +00:00
otherBuf.sz = otherBuf.cur - otherBuf.base;
2019-04-25 23:21:19 +00:00
otherBuf.cur = otherBuf.base;
2019-07-26 16:19:31 +00:00
self->SendMessageBuffer(otherBuf, nullptr);
2019-04-25 23:21:19 +00:00
}});
2019-02-27 12:55:26 +00:00
return true;
2019-01-05 13:45:05 +00:00
},
2019-05-28 19:45:09 +00:00
[&](RouterContact, RouterContact) -> bool { return true; },
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
return m_crypto.sign(sig, Bob.signingKey, buf);
2019-01-05 13:45:05 +00:00
},
2019-05-28 19:45:09 +00:00
[&](ILinkSession* session) { ASSERT_FALSE(session->IsEstablished()); },
[&](RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); });
2019-01-05 13:45:05 +00:00
ASSERT_TRUE(Alice.Start(m_logic, netLoop, AlicePort));
ASSERT_TRUE(Bob.Start(m_logic, netLoop, BobPort));
2019-01-05 13:45:05 +00:00
ASSERT_TRUE(Alice.link->TryEstablishTo(Bob.GetRC()));
RunMainloop();
ASSERT_TRUE(Bob.gotLIM);
ASSERT_TRUE(success);
2019-07-18 22:12:14 +00:00
#endif
2019-01-05 13:45:05 +00:00
}