mirror of https://github.com/oxen-io/lokinet
make unit tests pass for iwp
This commit is contained in:
parent
1a864832c8
commit
b34caa3e26
|
@ -596,6 +596,7 @@ namespace llarp
|
|||
}
|
||||
}
|
||||
SendMACK();
|
||||
Pump();
|
||||
m_Parent->PumpDone();
|
||||
}
|
||||
|
||||
|
|
|
@ -38,9 +38,17 @@ namespace llarp
|
|||
}
|
||||
|
||||
bool
|
||||
DecodeKey(ABSL_ATTRIBUTE_UNUSED const llarp_buffer_t& key,
|
||||
ABSL_ATTRIBUTE_UNUSED llarp_buffer_t* buf) override
|
||||
DecodeKey(const llarp_buffer_t& key, llarp_buffer_t* buf) override
|
||||
{
|
||||
if(key == "a")
|
||||
{
|
||||
llarp_buffer_t strbuf;
|
||||
if(!bencode_read_string(buf, &strbuf))
|
||||
return false;
|
||||
if(strbuf.sz != 1)
|
||||
return false;
|
||||
return *strbuf.cur == 'x';
|
||||
}
|
||||
return false;
|
||||
}
|
||||
|
||||
|
|
|
@ -37,8 +37,15 @@ struct LinkLayerTest : public test::LlarpTest< llarp::sodium::CryptoLibSodium >
|
|||
|
||||
RouterContact rc;
|
||||
|
||||
bool madeSession = false;
|
||||
bool gotLIM = false;
|
||||
|
||||
bool
|
||||
IsGucci() const
|
||||
{
|
||||
return gotLIM && madeSession;
|
||||
}
|
||||
|
||||
void Setup()
|
||||
{
|
||||
worker = std::make_shared<thread::ThreadPool>(1, 128, "test-worker");
|
||||
|
@ -120,7 +127,7 @@ struct LinkLayerTest : public test::LlarpTest< llarp::sodium::CryptoLibSodium >
|
|||
Context Bob;
|
||||
|
||||
bool success = false;
|
||||
const bool shouldDebug = false;
|
||||
const bool shouldDebug = true;
|
||||
|
||||
llarp_ev_loop_ptr netLoop;
|
||||
std::shared_ptr< Logic > m_logic;
|
||||
|
@ -174,14 +181,6 @@ struct LinkLayerTest : public test::LlarpTest< llarp::sodium::CryptoLibSodium >
|
|||
llarp_ev_loop_stop(netLoop);
|
||||
m_logic->stop();
|
||||
}
|
||||
|
||||
bool
|
||||
AliceGotMessage(const llarp_buffer_t&)
|
||||
{
|
||||
success = true;
|
||||
Stop();
|
||||
return true;
|
||||
}
|
||||
};
|
||||
|
||||
TEST_F(LinkLayerTest, TestIWP)
|
||||
|
@ -189,33 +188,45 @@ TEST_F(LinkLayerTest, TestIWP)
|
|||
#ifdef WIN32
|
||||
GTEST_SKIP();
|
||||
#else
|
||||
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);
|
||||
};
|
||||
Alice.link = iwp::NewInboundLink(
|
||||
Alice.encryptionKey,
|
||||
[&]() -> const RouterContact& { return Alice.GetRC(); },
|
||||
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
|
||||
if(Alice.gotLIM)
|
||||
{
|
||||
Alice.Regen();
|
||||
return s->RenegotiateSession();
|
||||
}
|
||||
else
|
||||
{
|
||||
LinkIntroMessage msg;
|
||||
ManagedBuffer copy{buf};
|
||||
if(!msg.BDecode(©.underlying))
|
||||
return false;
|
||||
if(!s->GotLIM(&msg))
|
||||
return false;
|
||||
Alice.gotLIM = true;
|
||||
return true;
|
||||
}
|
||||
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;
|
||||
},
|
||||
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
|
||||
return m_crypto.sign(sig, Alice.signingKey, buf);
|
||||
},
|
||||
[&](ILinkSession* s) -> bool {
|
||||
const auto rc = s->GetRemoteRC();
|
||||
return rc.pubkey == Bob.GetRC().pubkey;
|
||||
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;
|
||||
},
|
||||
[&](RouterContact, RouterContact) -> bool { return true; },
|
||||
|
||||
|
@ -227,27 +238,29 @@ TEST_F(LinkLayerTest, TestIWP)
|
|||
[]() {})
|
||||
;
|
||||
|
||||
auto sendDiscardMessage = [](ILinkSession* s) -> 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), nullptr);
|
||||
};
|
||||
|
||||
|
||||
Bob.link = iwp::NewInboundLink(
|
||||
Bob.encryptionKey, [&]() -> const RouterContact& { return Bob.GetRC(); },
|
||||
[&](ILinkSession* s, const llarp_buffer_t& buf) -> bool {
|
||||
LinkIntroMessage msg;
|
||||
ManagedBuffer copy{buf};
|
||||
if(!msg.BDecode(©.underlying))
|
||||
|
||||
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;
|
||||
}
|
||||
return false;
|
||||
if(!s->GotLIM(&msg))
|
||||
return false;
|
||||
Bob.gotLIM = true;
|
||||
return sendDiscardMessage(s);
|
||||
},
|
||||
|
||||
[&](Signature& sig, const llarp_buffer_t& buf) -> bool {
|
||||
|
@ -257,12 +270,12 @@ TEST_F(LinkLayerTest, TestIWP)
|
|||
if(s->GetRemoteRC().pubkey != Alice.GetRC().pubkey)
|
||||
return false;
|
||||
LogInfo("bob established with alice");
|
||||
return Bob.link->VisitSessionByPubkey(Alice.GetRC().pubkey.as_array(),
|
||||
sendDiscardMessage);
|
||||
Bob.madeSession = true;
|
||||
|
||||
return true;
|
||||
},
|
||||
[&](RouterContact newrc, RouterContact oldrc) -> bool {
|
||||
success = newrc.pubkey == oldrc.pubkey;
|
||||
return true;
|
||||
return newrc.pubkey == oldrc.pubkey;
|
||||
},
|
||||
[&](ILinkSession* session) { ASSERT_FALSE(session->IsEstablished()); },
|
||||
[&](RouterID router) { ASSERT_EQ(router, Alice.GetRouterID()); },
|
||||
|
@ -275,6 +288,8 @@ TEST_F(LinkLayerTest, TestIWP)
|
|||
m_logic->queue_func([&]() { ASSERT_TRUE(Alice.link->TryEstablishTo(Bob.GetRC())); });
|
||||
|
||||
RunMainloop();
|
||||
ASSERT_TRUE(Bob.gotLIM);
|
||||
ASSERT_TRUE(Alice.IsGucci());
|
||||
ASSERT_TRUE(Bob.IsGucci());
|
||||
ASSERT_TRUE(success);
|
||||
#endif
|
||||
};
|
||||
|
|
Loading…
Reference in New Issue