sometimes this returns nullptr
[ghsmtp.git] / DNS-test.cpp
blob98f279cc2cb477dc8435ef920dbaa8e4713f8710
1 #include "DNS-fcrdns.hpp"
2 #include "DNS-iostream.hpp"
3 #include "DNS-ldns.hpp"
4 #include "DNS.hpp"
5 #include "Domain.hpp"
6 #include "osutil.hpp"
8 #include <algorithm>
10 #include <glog/logging.h>
12 template <typename... Ts>
13 std::ostream& operator<<(std::ostream& os, const std::variant<Ts...>& v)
15 std::visit([&os](auto&& arg) { os << arg; }, v);
16 return os;
19 int main(int argc, char const* argv[])
21 auto const config_path = osutil::get_config_dir();
23 DNS::Resolver res(config_path);
24 DNS_ldns::Resolver res_ldns;
26 struct lkp {
27 DNS::RR_type typ;
28 char const* name;
31 lkp lookups[] {
32 {DNS::RR_type::A, "amazon.com"},
33 {DNS::RR_type::A, "dee.test.digilicious.com"},
34 {DNS::RR_type::A, "does-not-exist.test.digilicious.com"},
35 {DNS::RR_type::A, "google-public-dns-a.google.com"},
36 {DNS::RR_type::A, "google-public-dns-b.google.com"},
37 {DNS::RR_type::AAAA, "google-public-dns-a.google.com"},
38 {DNS::RR_type::AAAA, "google-public-dns-b.google.com"},
39 {DNS::RR_type::CNAME, "cname4.digilicious.com"},
40 {DNS::RR_type::CNAME, "com.digilicious.in-addr.arpa"},
41 {DNS::RR_type::MX, "anyold.host"},
42 {DNS::RR_type::MX, "cname.test.digilicious.com"},
43 {DNS::RR_type::PTR, "com.digilicious.in-addr.arpa"},
44 {DNS::RR_type::PTR, "com.google.in-addr.arpa"},
45 {DNS::RR_type::TLSA, "_25._tcp.digilicious.com"},
46 {DNS::RR_type::TLSA, "_443._tcp.digilicious.com"},
47 {DNS::RR_type::TXT, "digilicious.com"},
50 for (auto const& lookup : lookups) {
51 DNS::Query q(res, lookup.typ, lookup.name);
52 DNS_ldns::Query q_ldns(res_ldns, lookup.typ, lookup.name);
54 CHECK_EQ(q.nx_domain(), q_ldns.nx_domain()) <<
55 lookup.name;
57 CHECK_EQ(q.bogus_or_indeterminate(), q_ldns.bogus_or_indeterminate());
59 CHECK_EQ(q.authentic_data(), q_ldns.authentic_data()) <<
60 lookup.name;
62 auto rrs{q.get_records()};
63 auto rrs_ldns{q_ldns.get_records()};
65 CHECK_EQ(size(rrs), size(rrs_ldns));
67 std::sort(begin(rrs), end(rrs));
68 std::sort(begin(rrs_ldns), end(rrs_ldns));
70 auto [rr, rr_ldns]
71 = std::mismatch(begin(rrs), end(rrs), begin(rrs_ldns), end(rrs_ldns));
72 if (rr != end(rrs)) {
73 LOG(FATAL) << *rr << " != " << *rr_ldns;
77 // These IP addresses might be stable for a while.
79 auto const goog_a{"google-public-dns-a.google.com"};
80 auto const goog_b{"google-public-dns-b.google.com"};
82 auto const aaaaddrs_a{res.get_strings(DNS::RR_type::AAAA, goog_a)};
83 CHECK_EQ(aaaaddrs_a.size(), 1U);
84 CHECK_EQ(aaaaddrs_a[0], "2001:4860:4860::8888");
86 auto const aaaaddrs_b{res.get_strings(DNS::RR_type::AAAA, goog_b)};
87 CHECK_EQ(aaaaddrs_b.size(), 1U);
88 CHECK_EQ(aaaaddrs_b[0], "2001:4860:4860::8844");
90 auto const addrs_a{res.get_strings(DNS::RR_type::A, goog_a)};
91 CHECK_EQ(addrs_a.size(), 1U);
92 CHECK_EQ(addrs_a[0], "8.8.8.8");
94 auto const addrs_b{res.get_strings(DNS::RR_type::A, goog_b)};
95 CHECK_EQ(addrs_b.size(), 1U);
96 CHECK_EQ(addrs_b[0], "8.8.4.4");
98 auto const fcrdnses4{fcrdns4(res, "1.1.1.1")};
99 CHECK_EQ(fcrdnses4.size(), 1);
100 CHECK(Domain::match(fcrdnses4.front(), "one.one.one.one"))
101 << "no match for " << fcrdnses4.front();
103 auto const fcrdnses6{fcrdns6(res, "2606:4700:4700::1111")};
104 CHECK_EQ(fcrdnses6.size(), 1);
105 CHECK(Domain::match(fcrdnses6.front(), "one.one.one.one"))
106 << "no match for " << fcrdnses6.front();
108 auto const quad9{fcrdns4(res, "9.9.9.9")};
109 CHECK(Domain::match(quad9.front(), "dns9.quad9.net"))
110 << "no match for " << quad9.front();