lokinet/test/service/test_llarp_service_name.cpp
Jeff 21930cf667
LNS (#1342)
* initial relay side lns

* fix typo

* add reserved names and refactor test for dns

* lns name decryption

* all wired up (allegedly)

* refact to use service::EncryptedName for LNS responses to include nonce with ciphertext

* fully rwemove tag_lookup_job

* replace lns cache with DecayingHashTable

* check for lns name validity against the following rules:

* not localhost.loki, loki.loki, or snode.loki

* if it contains no dash then max 32 characters long, not including the .loki tld (and also assuming a leading subdomain has been stripped)

* These are from general DNS requirements, and also enforced in
registrations:

* Must be all [A-Za-z0-9-]. (A-Z will be lower-cased by the RPC call).

* cannot start or end with a -

* max 63 characters long if it does contain a dash

* cannot contain -- in the third and fourth characters unless it starts with xn--

* handle timeout in name lookup job by calling the right handler with std::nullopt
2020-09-17 15:18:08 -04:00

44 lines
2.0 KiB
C++

#include "catch2/catch.hpp"
#include <crypto/crypto_libsodium.hpp>
#include <service/name.hpp>
#include <lokimq/hex.h>
using namespace std::literals;
TEST_CASE("Test LNS name decrypt", "[lns]")
{
llarp::sodium::CryptoLibSodium crypto;
constexpr auto recordhex = "0ba76cbfdb6dc8f950da57ae781912f31c8ad0c55dbf86b88cb0391f563261a9656571a817be4092969f8a78ee0fcee260424acb4a1f4bbdd27348b71de006b6152dd04ed11bf3c4"sv;
const auto recordbin = lokimq::from_hex(recordhex);
CHECK(not recordbin.empty());
llarp::SymmNonce n{};
std::vector<byte_t> ciphertext{};
const auto len = recordbin.size() - n.size();
std::copy_n(recordbin.cbegin() + len, n.size(), n.data());
std::copy_n(recordbin.cbegin(), len, std::back_inserter(ciphertext));
const auto maybe = crypto.maybe_decrypt_name(std::string_view{reinterpret_cast<const char *>(ciphertext.data()), ciphertext.size()}, n, "jason.loki");
CHECK(maybe.has_value());
const llarp::service::Address addr{*maybe};
CHECK(addr.ToString() == "azfoj73snr9f3neh5c6sf7rtbaeabyxhr1m4un5aydsmsrxo964o.loki");
}
TEST_CASE("Test LNS validity", "[lns]")
{
CHECK(not llarp::service::NameIsValid("loki.loki"));
CHECK(not llarp::service::NameIsValid("snode.loki"));
CHECK(not llarp::service::NameIsValid("localhost.loki"));
CHECK(not llarp::service::NameIsValid("gayballs22.loki.loki"));
CHECK(not llarp::service::NameIsValid("-loki.loki"));
CHECK(not llarp::service::NameIsValid("super-mario-gayballs-.loki"));
CHECK(not llarp::service::NameIsValid("bn--lolexdeeeeee.loki"));
CHECK(not llarp::service::NameIsValid("2222222222a-.loki"));
CHECK(not llarp::service::NameIsValid("aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa.loki"));
CHECK(llarp::service::NameIsValid("xn--animewasindeedamistake.loki"));
CHECK(llarp::service::NameIsValid("memerionos.loki"));
CHECK(llarp::service::NameIsValid("whyis.xn--animehorrible.loki"));
CHECK(llarp::service::NameIsValid("the.goog.loki"));
CHECK(llarp::service::NameIsValid("420.loki"));
}