try fixing unit tests

pull/236/head
Jeff Becker 5 years ago
parent 41e8691702
commit 6064ff5a68
No known key found for this signature in database
GPG Key ID: F357B3B42F6F9B05

@ -22,7 +22,8 @@ namespace llarp
dialect = other.dialect; dialect = other.dialect;
pubkey = other.pubkey; pubkey = other.pubkey;
memcpy(ip.s6_addr, other.ip.s6_addr, 16); memcpy(ip.s6_addr, other.ip.s6_addr, 16);
port = other.port; port = other.port;
version = other.version;
return *this; return *this;
} }

@ -32,13 +32,12 @@ namespace llarp
} }
AddressInfo(const AddressInfo& other) AddressInfo(const AddressInfo& other)
: IBEncodeMessage() : IBEncodeMessage(other.version)
, rank(other.rank) , rank(other.rank)
, dialect(other.dialect) , dialect(other.dialect)
, pubkey(other.pubkey) , pubkey(other.pubkey)
, port(other.port)
{ {
port = other.port;
version = other.version;
memcpy(ip.s6_addr, other.ip.s6_addr, 16); memcpy(ip.s6_addr, other.ip.s6_addr, 16);
} }

@ -51,12 +51,34 @@ namespace llarp
return bencode_end(buf); return bencode_end(buf);
} }
static bool
bdecode_ip_string(llarp_buffer_t* buf, in6_addr& ip)
{
char tmp[128] = {0};
llarp_buffer_t strbuf;
if(!bencode_read_string(buf, &strbuf))
return false;
if(strbuf.sz >= sizeof(tmp))
return false;
memcpy(tmp, strbuf.base, strbuf.sz);
tmp[strbuf.sz] = 0;
return inet_pton(AF_INET6, tmp, &ip.s6_addr[0]) == 1;
}
bool bool
ExitInfo::DecodeKey(__attribute__((unused)) llarp_buffer_t k, ExitInfo::DecodeKey(llarp_buffer_t k, llarp_buffer_t* buf)
__attribute__((unused)) llarp_buffer_t* buf)
{ {
bool read = false; bool read = false;
// TODO: implement me if(!BEncodeMaybeReadDictEntry("k", pubkey, read, k, buf))
return false;
if(!BEncodeMaybeReadDictInt("v", version, read, k, buf))
return false;
if(llarp_buffer_eq(k, "a"))
return bdecode_ip_string(buf, address);
if(llarp_buffer_eq(k, "b"))
return bdecode_ip_string(buf, netmask);
return read; return read;
} }

@ -258,10 +258,10 @@ namespace llarp
bool bool
RouterContact::Verify(llarp::Crypto *crypto, llarp_time_t now) const RouterContact::Verify(llarp::Crypto *crypto, llarp_time_t now) const
{ {
static const NetID networkNetID; if(netID != NetID::DefaultValue())
if(netID != networkNetID)
{ {
llarp::LogError("netid missmatch: '", netID, "' != '", networkNetID, "'"); llarp::LogError("netid missmatch: '", netID, "' != '",
NetID::DefaultValue(), "'");
return false; return false;
} }
if(IsExpired(now)) if(IsExpired(now))

@ -162,6 +162,22 @@ namespace llarp
return last_updated < other.last_updated; return last_updated < other.last_updated;
} }
friend std::ostream &
operator<<(std::ostream &out, const RouterContact &rc)
{
out << "[RouterContact k=" << rc.pubkey;
out << " updated=" << rc.last_updated;
out << " netid=" << rc.netID;
out << " v=" << rc.version;
out << " ai=[ ";
for(const auto &addr : rc.addrs)
out << addr << " ";
out << " ] xi=[ ";
for(const auto &xi : rc.exits)
out << xi << " ";
return out << " ] e=" << rc.enckey << " z=" << rc.signature << " ]";
}
bool bool
Read(const char *fname); Read(const char *fname);

@ -14,7 +14,6 @@ struct RCTest : public ::testing::Test
: crypto(llarp::Crypto::sodium{}), oldval(llarp::NetID::DefaultValue()) : crypto(llarp::Crypto::sodium{}), oldval(llarp::NetID::DefaultValue())
{ {
llarp::NetID::DefaultValue() = llarp::NetID(DEF_VALUE); llarp::NetID::DefaultValue() = llarp::NetID(DEF_VALUE);
rc.Clear();
} }
~RCTest() ~RCTest()
@ -22,18 +21,22 @@ struct RCTest : public ::testing::Test
llarp::NetID::DefaultValue() = oldval; llarp::NetID::DefaultValue() = oldval;
} }
RC_t rc;
llarp::Crypto crypto; llarp::Crypto crypto;
const llarp::NetID oldval; const llarp::NetID oldval;
}; };
TEST_F(RCTest, TestSignVerify) TEST_F(RCTest, TestSignVerify)
{ {
llarp::NetID netid(DEF_VALUE);
RC_t rc;
SecKey_t encr; SecKey_t encr;
SecKey_t sign; SecKey_t sign;
crypto.encryption_keygen(encr); crypto.encryption_keygen(encr);
crypto.identity_keygen(sign); crypto.identity_keygen(sign);
rc.enckey = encr.toPublic(); rc.enckey = encr.toPublic();
rc.pubkey = sign.toPublic();
ASSERT_TRUE(rc.netID == netid);
ASSERT_TRUE(rc.netID == llarp::NetID::DefaultValue());
ASSERT_TRUE(rc.Sign(&crypto, sign)); ASSERT_TRUE(rc.Sign(&crypto, sign));
ASSERT_TRUE(rc.Verify(&crypto, llarp::time_now_ms())); ASSERT_TRUE(rc.Verify(&crypto, llarp::time_now_ms()));
} }

Loading…
Cancel
Save