lokinet/test/dns/test_llarp_dns_dns.cpp

194 lines
4.7 KiB
C++
Raw Normal View History

#include <catch2/catch.hpp>
#include <dns/dns.hpp>
#include <dns/message.hpp>
#include <dns/name.hpp>
#include <dns/rr.hpp>
#include <net/net.hpp>
2019-06-12 13:48:14 +00:00
#include <net/ip.hpp>
#include <util/buffer.hpp>
2018-12-03 22:22:59 +00:00
#include <algorithm>
constexpr auto tld = ".loki";
2018-12-03 22:22:59 +00:00
TEST_CASE("Test Has TLD", "[dns]")
{
llarp::dns::Question question;
question.qname = "a.loki.";
CHECK(question.HasTLD(tld));
question.qname = "a.loki..";
CHECK(not question.HasTLD(tld));
question.qname = "bepis.loki.";
CHECK(question.HasTLD(tld));
question.qname = "bepis.logi.";
CHECK(not question.HasTLD(tld));
question.qname = "a.net.";
CHECK(not question.HasTLD(tld));
question.qname = "a.boki.";
CHECK(not question.HasTLD(tld));
2019-05-03 18:56:31 +00:00
question.qname = "t.co.";
CHECK(not question.HasTLD(tld));
};
TEST_CASE("Test Is Localhost.loki", "[dns]")
{
llarp::dns::Question question;
question.qname = "localhost.loki.";
CHECK(question.IsLocalhost());
question.qname = "foo.localhost.loki.";
CHECK(question.IsLocalhost());
question.qname = "foo.bar.localhost.loki.";
CHECK(question.IsLocalhost());
question.qname = "something.loki.";
CHECK(not question.IsLocalhost());
question.qname = "localhost.something.loki.";
CHECK(not question.IsLocalhost());
question.qname = "notlocalhost.loki.";
CHECK(not question.IsLocalhost());
};
TEST_CASE("Test Get Subdomains" , "[dns]")
{
llarp::dns::Question question;
std::string expected;
question.qname = "localhost.loki.";
expected = "";
CHECK(question.Subdomains() == expected);
question.qname = "foo.localhost.loki.";
expected = "foo";
CHECK(question.Subdomains() == expected);
question.qname = "foo.bar.localhost.loki.";
expected = "foo.bar";
CHECK(question.Subdomains() == expected);
// not legal, but test it anyway
question.qname = ".localhost.loki.";
expected = "";
CHECK(question.Subdomains() == expected);
question.qname = ".loki.";
expected = "";
CHECK(question.Subdomains() == expected);
question.qname = "loki.";
expected = "";
CHECK(question.Subdomains() == expected);
question.qname = ".";
expected = "";
CHECK(question.Subdomains() == expected);
question.qname = "";
expected = "";
CHECK(question.Subdomains() == expected);
};
TEST_CASE("Test PTR records", "[dns]")
2018-12-04 16:52:48 +00:00
{
2020-02-12 21:18:25 +00:00
llarp::huint128_t ip = {0};
llarp::huint128_t expected =
llarp::net::ExpandV4(llarp::ipaddr_ipv4_bits(10, 10, 10, 1));
CHECK(llarp::dns::DecodePTR("1.10.10.10.in-addr.arpa.", ip));
CHECK(ip == expected);
2019-04-25 23:21:19 +00:00
}
2018-12-04 16:52:48 +00:00
TEST_CASE("Test Serialize Header", "[dns]")
2018-12-03 22:22:59 +00:00
{
std::array<byte_t, 1500> data{};
llarp_buffer_t buf(data);
2018-12-04 16:16:43 +00:00
llarp::dns::MessageHeader hdr, other;
hdr.id = 0x1234;
hdr.fields = (1 << 15);
hdr.qd_count = 1;
hdr.an_count = 1;
hdr.ns_count = 0;
hdr.ar_count = 0;
CHECK(hdr.Encode(&buf));
CHECK((buf.cur - buf.base) == llarp::dns::MessageHeader::Size);
// rewind
buf.cur = buf.base;
CHECK(other.Decode(&buf));
CHECK(hdr == other);
CHECK(other.id == 0x1234);
CHECK(other.fields == (1 << 15));
2019-04-25 23:21:19 +00:00
}
2018-12-03 22:22:59 +00:00
TEST_CASE("Test Serialize Name" , "[dns]")
2018-12-03 22:22:59 +00:00
{
2018-12-04 16:16:43 +00:00
const llarp::dns::Name_t name = "whatever.tld";
const llarp::dns::Name_t expected = "whatever.tld.";
llarp::dns::Name_t other;
std::array<byte_t, 1500> data{};
llarp_buffer_t buf(data);
CHECK(llarp::dns::EncodeName(&buf, name));
buf.cur = buf.base;
CHECK(buf.base[0] == 8);
CHECK(buf.base[1] == 'w');
CHECK(buf.base[2] == 'h');
CHECK(buf.base[3] == 'a');
CHECK(buf.base[4] == 't');
CHECK(buf.base[5] == 'e');
CHECK(buf.base[6] == 'v');
CHECK(buf.base[7] == 'e');
CHECK(buf.base[8] == 'r');
CHECK(buf.base[9] == 3);
CHECK(buf.base[10] == 't');
CHECK(buf.base[11] == 'l');
CHECK(buf.base[12] == 'd');
CHECK(buf.base[13] == 0);
CHECK(llarp::dns::DecodeName(&buf, other));
CHECK(expected == other);
2019-04-25 23:21:19 +00:00
}
2018-12-03 22:22:59 +00:00
TEST_CASE("Test serialize question", "[dns]")
2018-12-03 22:22:59 +00:00
{
2018-12-04 16:16:43 +00:00
const std::string name = "whatever.tld";
const std::string expected_name = name + ".";
llarp::dns::Question q, other;
std::array<byte_t, 1500> data{};
llarp_buffer_t buf(data);
2018-12-04 16:16:43 +00:00
q.qname = name;
q.qclass = 1;
q.qtype = 1;
CHECK(q.Encode(&buf));
buf.cur = buf.base;
CHECK(other.Decode(&buf));
CHECK(other.qname == expected_name);
CHECK(q.qclass == other.qclass);
CHECK(q.qtype == other.qtype);
2019-04-25 23:21:19 +00:00
}
2018-12-03 22:22:59 +00:00
TEST_CASE("Test Encode/Decode RData" , "[dns]")
2018-12-03 22:22:59 +00:00
{
std::array<byte_t, 1500> data{};
llarp_buffer_t buf(data);
2018-12-03 22:22:59 +00:00
2018-12-04 16:16:43 +00:00
static constexpr size_t rdatasize = 32;
llarp::dns::RR_RData_t rdata(rdatasize);
std::fill(rdata.begin(), rdata.end(), 'a');
llarp::dns::RR_RData_t other_rdata;
CHECK(llarp::dns::EncodeRData(&buf, rdata));
CHECK(buf.cur - buf.base == rdatasize + sizeof(uint16_t));
buf.cur = buf.base;
CHECK(llarp::dns::DecodeRData(&buf, other_rdata));
CHECK(rdata == other_rdata);
2019-04-25 23:21:19 +00:00
}