2020-09-17 19:18:08 +00:00
|
|
|
#include <catch2/catch.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
#include <dns/dns.hpp>
|
2019-01-11 00:12:43 +00:00
|
|
|
#include <dns/message.hpp>
|
|
|
|
#include <dns/name.hpp>
|
|
|
|
#include <dns/rr.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <net/net.hpp>
|
2019-06-12 13:48:14 +00:00
|
|
|
#include <net/ip.hpp>
|
2019-01-11 01:42:02 +00:00
|
|
|
#include <util/buffer.hpp>
|
2018-12-12 02:52:51 +00:00
|
|
|
|
2018-12-03 22:22:59 +00:00
|
|
|
#include <algorithm>
|
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
constexpr auto tld = ".loki";
|
2018-12-03 22:22:59 +00:00
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
TEST_CASE("Test Has TLD", "[dns]")
|
2019-05-01 13:40:10 +00:00
|
|
|
{
|
|
|
|
llarp::dns::Question question;
|
|
|
|
question.qname = "a.loki.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.HasTLD(tld));
|
2019-05-01 13:40:10 +00:00
|
|
|
question.qname = "a.loki..";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(not question.HasTLD(tld));
|
2019-05-01 13:40:10 +00:00
|
|
|
question.qname = "bepis.loki.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.HasTLD(tld));
|
2019-05-01 13:40:10 +00:00
|
|
|
question.qname = "bepis.logi.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(not question.HasTLD(tld));
|
2019-05-01 13:40:10 +00:00
|
|
|
question.qname = "a.net.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(not question.HasTLD(tld));
|
2019-05-01 13:40:10 +00:00
|
|
|
question.qname = "a.boki.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(not question.HasTLD(tld));
|
2019-05-03 18:56:31 +00:00
|
|
|
question.qname = "t.co.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(not question.HasTLD(tld));
|
2019-05-01 13:40:10 +00:00
|
|
|
};
|
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
TEST_CASE("Test Is Localhost.loki", "[dns]")
|
2020-08-31 23:25:58 +00:00
|
|
|
{
|
|
|
|
llarp::dns::Question question;
|
|
|
|
|
|
|
|
question.qname = "localhost.loki.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.IsLocalhost());
|
2020-08-31 23:25:58 +00:00
|
|
|
question.qname = "foo.localhost.loki.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.IsLocalhost());
|
2020-08-31 23:25:58 +00:00
|
|
|
question.qname = "foo.bar.localhost.loki.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.IsLocalhost());
|
2020-08-31 23:25:58 +00:00
|
|
|
|
|
|
|
question.qname = "something.loki.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(not question.IsLocalhost());
|
2020-08-31 23:25:58 +00:00
|
|
|
question.qname = "localhost.something.loki.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(not question.IsLocalhost());
|
2020-08-31 23:25:58 +00:00
|
|
|
question.qname = "notlocalhost.loki.";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(not question.IsLocalhost());
|
2020-08-31 23:25:58 +00:00
|
|
|
};
|
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
TEST_CASE("Test Get Subdomains" , "[dns]")
|
2020-08-31 23:25:58 +00:00
|
|
|
{
|
|
|
|
llarp::dns::Question question;
|
|
|
|
std::string expected;
|
|
|
|
|
|
|
|
question.qname = "localhost.loki.";
|
|
|
|
expected = "";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.Subdomains() == expected);
|
2020-08-31 23:25:58 +00:00
|
|
|
|
|
|
|
question.qname = "foo.localhost.loki.";
|
|
|
|
expected = "foo";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.Subdomains() == expected);
|
2020-08-31 23:25:58 +00:00
|
|
|
|
|
|
|
question.qname = "foo.bar.localhost.loki.";
|
|
|
|
expected = "foo.bar";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.Subdomains() == expected);
|
2020-08-31 23:25:58 +00:00
|
|
|
|
|
|
|
// not legal, but test it anyway
|
|
|
|
question.qname = ".localhost.loki.";
|
|
|
|
expected = "";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.Subdomains() == expected);
|
2020-08-31 23:25:58 +00:00
|
|
|
|
|
|
|
question.qname = ".loki.";
|
|
|
|
expected = "";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.Subdomains() == expected);
|
2020-08-31 23:25:58 +00:00
|
|
|
|
|
|
|
question.qname = "loki.";
|
|
|
|
expected = "";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.Subdomains() == expected);
|
2020-08-31 23:25:58 +00:00
|
|
|
|
|
|
|
question.qname = ".";
|
|
|
|
expected = "";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.Subdomains() == expected);
|
2020-08-31 23:25:58 +00:00
|
|
|
|
|
|
|
question.qname = "";
|
|
|
|
expected = "";
|
2020-09-17 19:18:08 +00:00
|
|
|
CHECK(question.Subdomains() == expected);
|
2020-08-31 23:25:58 +00:00
|
|
|
};
|
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
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 =
|
2020-05-20 19:14:05 +00:00
|
|
|
llarp::net::ExpandV4(llarp::ipaddr_ipv4_bits(10, 10, 10, 1));
|
2020-09-17 19:18:08 +00:00
|
|
|
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
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
TEST_CASE("Test Serialize Header", "[dns]")
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2020-09-17 19:18:08 +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;
|
2020-09-17 19:18:08 +00:00
|
|
|
|
|
|
|
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
|
|
|
|
2020-09-17 19:18:08 +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;
|
2020-09-17 19:18:08 +00:00
|
|
|
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
|
|
|
|
2020-09-17 19:18:08 +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;
|
2020-09-17 19:18:08 +00:00
|
|
|
|
|
|
|
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;
|
2020-09-17 19:18:08 +00:00
|
|
|
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
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
TEST_CASE("Test Encode/Decode RData" , "[dns]")
|
2018-12-03 22:22:59 +00:00
|
|
|
{
|
2020-09-17 19:18:08 +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;
|
|
|
|
|
2020-09-17 19:18:08 +00:00
|
|
|
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
|
|
|
}
|