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-09-12 18:19:25 +00:00
|
|
|
|
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>
|
2019-01-26 15:40:58 +00:00
|
|
|
|
2019-01-11 00:12:43 +00:00
|
|
|
#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
|
|
|
|
{
|
2019-05-28 19:45:08 +00:00
|
|
|
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
|
|
|
|
2019-11-14 16:48:02 +00:00
|
|
|
bool madeSession = false;
|
2019-01-05 13:45:05 +00:00
|
|
|
bool gotLIM = false;
|
|
|
|
|
2019-11-14 16:48:02 +00:00
|
|
|
bool
|
|
|
|
IsGucci() const
|
|
|
|
{
|
|
|
|
return gotLIM && madeSession;
|
|
|
|
}
|
|
|
|
|
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);
|
2019-05-28 19:45:08 +00:00
|
|
|
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;
|
2019-05-28 19:45:08 +00:00
|
|
|
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)
|
2019-09-12 18:19:25 +00:00
|
|
|
{
|
|
|
|
worker->drain();
|
2019-09-05 17:39:09 +00:00
|
|
|
worker->stop();
|
2019-09-12 18:19:25 +00:00
|
|
|
}
|
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-11-14 21:56:01 +00:00
|
|
|
const bool shouldDebug = false;
|
2019-01-05 13:45:05 +00:00
|
|
|
|
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-11-14 15:06:53 +00:00
|
|
|
llarp::LogLevel oldLevel;
|
2019-01-05 13:45:05 +00:00
|
|
|
|
2019-05-28 19:45:09 +00:00
|
|
|
LinkLayerTest() : netLoop(nullptr)
|
2019-01-05 13:45:05 +00:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
SetUp()
|
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
oldLevel = llarp::LogContext::Instance().minLevel;
|
|
|
|
if(shouldDebug)
|
2019-11-14 21:56:01 +00:00
|
|
|
llarp::SetLogLevel(eLogTrace);
|
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-11-14 15:06:53 +00:00
|
|
|
llarp::SetLogLevel(oldLevel);
|
2019-01-05 13:45:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void
|
|
|
|
RunMainloop()
|
|
|
|
{
|
2019-11-14 15:06:53 +00:00
|
|
|
m_logic->call_later(5000, std::bind(&LinkLayerTest::Stop, this));
|
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-11-14 15:06:53 +00:00
|
|
|
Alice.Stop();
|
|
|
|
Bob.Stop();
|
|
|
|
llarp_ev_loop_stop(netLoop);
|
|
|
|
m_logic->stop();
|
2019-01-05 13:45:05 +00:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
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-11-14 16:48:02 +00:00
|
|
|
auto sendDiscardMessage = [](ILinkSession* s, auto callback) -> bool {
|
|
|
|
// send discard message in reply to complete unit test
|
|
|
|
std::vector< byte_t> tmp(32);
|
|
|
|
llarp_buffer_t otherBuf(tmp);
|
|
|
|
DiscardMessage discard;
|
|
|
|
if(!discard.BEncode(&otherBuf))
|
|
|
|
return false;
|
|
|
|
return s->SendMessageBuffer(std::move(tmp), callback);
|
|
|
|
};
|
2019-08-22 20:53:27 +00:00
|
|
|
Alice.link = iwp::NewInboundLink(
|
2019-05-28 19:45:08 +00:00
|
|
|
Alice.encryptionKey,
|
2019-05-28 19:45:09 +00:00
|
|
|
[&]() -> const RouterContact& { return Alice.GetRC(); },
|
|
|
|
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
|
2019-11-14 16:48:02 +00:00
|
|
|
llarp_buffer_t copy(buf.base, buf.sz);
|
|
|
|
if(not Alice.gotLIM)
|
|
|
|
{
|
|
|
|
LinkIntroMessage msg;
|
|
|
|
if(msg.BDecode(©))
|
|
|
|
{
|
|
|
|
Alice.gotLIM = s->GotLIM(&msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Alice.gotLIM;
|
2019-01-05 13:45:05 +00:00
|
|
|
},
|
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();
|
2019-11-14 16:48:02 +00:00
|
|
|
if(rc.pubkey != Bob.GetRC().pubkey)
|
|
|
|
return false;
|
|
|
|
LogInfo("alice established with bob");
|
|
|
|
Alice.madeSession = true;
|
|
|
|
sendDiscardMessage(s, [&](auto status) {
|
|
|
|
success = status == llarp::ILinkSession::DeliveryStatus::eDeliverySuccess;
|
|
|
|
LogInfo("message sent to bob suceess=", success);
|
|
|
|
Stop();
|
|
|
|
});
|
|
|
|
return true;
|
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();
|
|
|
|
},
|
2019-11-04 18:49:08 +00:00
|
|
|
[&](RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); },
|
|
|
|
[]() {})
|
|
|
|
;
|
2019-08-22 11:18:05 +00:00
|
|
|
|
2019-11-14 16:48:02 +00:00
|
|
|
|
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 {
|
2019-11-14 16:48:02 +00:00
|
|
|
|
|
|
|
llarp_buffer_t copy(buf.base, buf.sz);
|
|
|
|
if(not Bob.gotLIM)
|
|
|
|
{
|
|
|
|
LinkIntroMessage msg;
|
|
|
|
if(msg.BDecode(©))
|
|
|
|
{
|
|
|
|
Bob.gotLIM = s->GotLIM(&msg);
|
|
|
|
}
|
|
|
|
return Bob.gotLIM;
|
|
|
|
}
|
|
|
|
DiscardMessage discard;
|
|
|
|
if(discard.BDecode(©))
|
|
|
|
{
|
|
|
|
LogInfo("bog got discard message from alice");
|
|
|
|
return true;
|
|
|
|
}
|
2019-08-22 11:18:05 +00:00
|
|
|
return false;
|
|
|
|
},
|
|
|
|
|
|
|
|
[&](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");
|
2019-11-14 16:48:02 +00:00
|
|
|
Bob.madeSession = true;
|
|
|
|
|
|
|
|
return true;
|
2019-08-22 11:18:05 +00:00
|
|
|
},
|
|
|
|
[&](RouterContact newrc, RouterContact oldrc) -> bool {
|
2019-11-14 16:48:02 +00:00
|
|
|
return newrc.pubkey == oldrc.pubkey;
|
2019-08-22 11:18:05 +00:00
|
|
|
},
|
|
|
|
[&](ILinkSession* session) { ASSERT_FALSE(session->IsEstablished()); },
|
2019-11-04 18:49:08 +00:00
|
|
|
[&](RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); },
|
|
|
|
[]() {})
|
|
|
|
;
|
2019-08-22 11:18:05 +00:00
|
|
|
|
|
|
|
ASSERT_TRUE(Alice.Start(m_logic, netLoop, AlicePort));
|
|
|
|
ASSERT_TRUE(Bob.Start(m_logic, netLoop, BobPort));
|
|
|
|
|
2019-11-14 21:56:01 +00:00
|
|
|
LogicCall(m_logic, [&]() { ASSERT_TRUE(Alice.link->TryEstablishTo(Bob.GetRC())); });
|
2019-08-22 11:18:05 +00:00
|
|
|
|
|
|
|
RunMainloop();
|
2019-11-14 16:48:02 +00:00
|
|
|
ASSERT_TRUE(Alice.IsGucci());
|
|
|
|
ASSERT_TRUE(Bob.IsGucci());
|
|
|
|
ASSERT_TRUE(success);
|
2019-08-23 20:49:12 +00:00
|
|
|
#endif
|
2019-08-22 11:18:05 +00:00
|
|
|
};
|