mirror of
https://github.com/oxen-io/lokinet.git
synced 2024-10-31 09:20:21 +00:00
15229ea7ff
* initial work for RC SRVs. Needs tests for new RC format. Needs SRVs added to new RC, and associated tests. * convert rc sign/verify test to catch2, add test for new rc Also fixes a mistake in new rc serialization * bump loki-mq submodule need support for viewing bt deserialize consumer buffer so we know how much it has consumed. * fix some behavior errors need to advance llarp_buffer_t after consuming data from it only rewind and modify size of llarp_buffer_t in owning context. * Add test for router contact (de-)serialization Adds a test that makes a list of RouterContact with mixed versions and ensures it serializes and deserializes correctly.
126 lines
3.0 KiB
C++
126 lines
3.0 KiB
C++
#include <catch2/catch.hpp>
|
|
|
|
#include <crypto/crypto.hpp>
|
|
#include <crypto/crypto_libsodium.hpp>
|
|
#include <router_contact.hpp>
|
|
#include <net/net_int.hpp>
|
|
|
|
namespace
|
|
{
|
|
llarp::sodium::CryptoLibSodium crypto;
|
|
llarp::CryptoManager cmanager(&crypto);
|
|
}
|
|
|
|
namespace llarp
|
|
{
|
|
|
|
TEST_CASE("RouterContact Sign and Verify", "[RC][RouterContact][signature][sign][verify]")
|
|
{
|
|
RouterContact rc;
|
|
|
|
SecretKey sign;
|
|
cmanager.instance()->identity_keygen(sign);
|
|
|
|
SecretKey encr;
|
|
cmanager.instance()->encryption_keygen(encr);
|
|
|
|
rc.enckey = encr.toPublic();
|
|
rc.pubkey = sign.toPublic();
|
|
|
|
REQUIRE(rc.Sign(sign));
|
|
REQUIRE(rc.Verify(time_now_ms()));
|
|
}
|
|
|
|
TEST_CASE("RouterContact Decode Version 1", "[RC][RouterContact][V1]")
|
|
{
|
|
RouterContact rc;
|
|
|
|
SecretKey sign;
|
|
cmanager.instance()->identity_keygen(sign);
|
|
|
|
SecretKey encr;
|
|
cmanager.instance()->encryption_keygen(encr);
|
|
|
|
rc.version = 1;
|
|
|
|
rc.enckey = encr.toPublic();
|
|
rc.pubkey = sign.toPublic();
|
|
|
|
REQUIRE(rc.Sign(sign));
|
|
|
|
std::array<byte_t, 5000> encoded_buffer;
|
|
llarp_buffer_t encoded_llarp(encoded_buffer);
|
|
|
|
rc.BEncode(&encoded_llarp);
|
|
|
|
encoded_llarp.sz = encoded_llarp.cur - encoded_llarp.base;
|
|
encoded_llarp.cur = encoded_llarp.base;
|
|
|
|
RouterContact decoded_rc;
|
|
|
|
REQUIRE(decoded_rc.BDecode(&encoded_llarp));
|
|
|
|
REQUIRE(decoded_rc.Verify(time_now_ms()));
|
|
|
|
REQUIRE(decoded_rc == rc);
|
|
}
|
|
|
|
TEST_CASE("RouterContact Decode Mixed Versions", "[RC][RouterContact]")
|
|
{
|
|
RouterContact rc1, rc2, rc3, rc4;
|
|
|
|
rc1.version = 0;
|
|
rc2.version = 1;
|
|
rc3.version = 0;
|
|
rc4.version = 1;
|
|
|
|
SecretKey sign1, sign2, sign3, sign4;
|
|
cmanager.instance()->identity_keygen(sign1);
|
|
cmanager.instance()->identity_keygen(sign2);
|
|
cmanager.instance()->identity_keygen(sign3);
|
|
cmanager.instance()->identity_keygen(sign4);
|
|
|
|
SecretKey encr1, encr2, encr3, encr4;
|
|
cmanager.instance()->encryption_keygen(encr1);
|
|
cmanager.instance()->encryption_keygen(encr2);
|
|
cmanager.instance()->encryption_keygen(encr3);
|
|
cmanager.instance()->encryption_keygen(encr4);
|
|
|
|
rc1.enckey = encr1.toPublic();
|
|
rc2.enckey = encr2.toPublic();
|
|
rc3.enckey = encr3.toPublic();
|
|
rc4.enckey = encr4.toPublic();
|
|
rc1.pubkey = sign1.toPublic();
|
|
rc2.pubkey = sign2.toPublic();
|
|
rc3.pubkey = sign3.toPublic();
|
|
rc4.pubkey = sign4.toPublic();
|
|
|
|
REQUIRE(rc1.Sign(sign1));
|
|
REQUIRE(rc2.Sign(sign2));
|
|
REQUIRE(rc3.Sign(sign3));
|
|
REQUIRE(rc4.Sign(sign4));
|
|
|
|
std::vector<RouterContact> rc_vec;
|
|
rc_vec.push_back(rc1);
|
|
rc_vec.push_back(rc2);
|
|
rc_vec.push_back(rc3);
|
|
rc_vec.push_back(rc4);
|
|
|
|
std::array<byte_t, 20000> encoded_buffer;
|
|
llarp_buffer_t encoded_llarp(encoded_buffer);
|
|
|
|
BEncodeWriteList(rc_vec.begin(), rc_vec.end(), &encoded_llarp);
|
|
encoded_llarp.sz = encoded_llarp.cur - encoded_llarp.base;
|
|
encoded_llarp.cur = encoded_llarp.base;
|
|
|
|
std::vector<RouterContact> rc_vec_out;
|
|
|
|
BEncodeReadList(rc_vec_out, &encoded_llarp);
|
|
|
|
REQUIRE(rc_vec.size() == rc_vec_out.size());
|
|
for (size_t i=0; i<4; i++)
|
|
REQUIRE(rc_vec[i] == rc_vec_out[i]);
|
|
}
|
|
|
|
} // namespace llarp
|