Debian Codegraph @

tests/unit_tests/dns_resolver.cpp

raw symbols SCIP
1 // Copyright (c) 2014-2022, The Monero Project
2 //
3 // All rights reserved.
4 //
5 // Redistribution and use in source and binary forms, with or without modification, are
6 // permitted provided that the following conditions are met:
7 //
8 // 1. Redistributions of source code must retain the above copyright notice, this list of
9 // conditions and the following disclaimer.
10 //
11 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
12 // of conditions and the following disclaimer in the documentation and/or other
13 // materials provided with the distribution.
14 //
15 // 3. Neither the name of the copyright holder nor the names of its contributors may be
16 // used to endorse or promote products derived from this software without specific
17 // prior written permission.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29 #include <iostream>
30 #include <vector>
31
32 #include "gtest/gtest.h"
33
34 #include "common/dns_utils.h"
35
36 TEST(DNSResolver, IPv4Success)
37 {
38 tools::DNSResolver resolver = tools::DNSResolver::create();
39
40 bool avail, valid;
41
42 auto ips = resolver.get_ipv4("example.com", avail, valid);
43
44 ASSERT_LE(1, ips.size());
45
46 ips = tools::DNSResolver::instance().get_ipv4("example.com", avail, valid);
47
48 ASSERT_LE(1, ips.size());
49 }
50
51 TEST(DNSResolver, IPv4Failure)
52 {
53 // guaranteed by IANA/ICANN/RFC to be invalid
54 tools::DNSResolver resolver = tools::DNSResolver::create();
55
56 bool avail, valid;
57
58 auto ips = resolver.get_ipv4("example.invalid", avail, valid);
59
60 ASSERT_EQ(0, ips.size());
61
62 ips = tools::DNSResolver::instance().get_ipv4("example.invalid", avail, valid);
63
64 ASSERT_EQ(0, ips.size());
65 }
66
67 TEST(DNSResolver, DNSSECSuccess)
68 {
69 tools::DNSResolver resolver = tools::DNSResolver::create();
70
71 bool avail, valid;
72
73 auto ips = resolver.get_ipv4("example.com", avail, valid);
74
75 ASSERT_LE(1, ips.size());
76
77 ASSERT_TRUE(avail);
78 ASSERT_TRUE(valid);
79 }
80
81 TEST(DNSResolver, DNSSECFailure)
82 {
83 tools::DNSResolver resolver = tools::DNSResolver::create();
84
85 bool avail, valid;
86
87 auto ips = resolver.get_ipv4("dnssec-failed.org", avail, valid);
88
89 ASSERT_EQ(1, ips.size());
90
91 //ASSERT_STREQ("93.184.216.119", ips[0].c_str());
92
93 ASSERT_TRUE(avail);
94 ASSERT_FALSE(valid);
95 }
96
97 // It would be great to include an IPv6 test and assume it'll pass, but not every ISP / resolver plays nicely with IPv6;)
98 /*TEST(DNSResolver, IPv6Success)
99 {
100 tools::DNSResolver resolver = tools::DNSResolver::create();
101
102 bool avail, valid;
103
104 auto ips = resolver.get_ipv6("example.com", avail, valid);
105
106 ASSERT_EQ(1, ips.size());
107
108 ASSERT_STREQ("2606:2800:220:6d:26bf:1447:1097:aa7", ips[0].c_str());
109
110 ips = tools::DNSResolver::instance().get_ipv6("example.com", avail, valid);
111
112 ASSERT_EQ(1, ips.size());
113
114 ASSERT_STREQ("2606:2800:220:6d:26bf:1447:1097:aa7", ips[0].c_str());
115 }*/
116
117 TEST(DNSResolver, IPv6Failure)
118 {
119 // guaranteed by IANA/ICANN/RFC to be invalid
120 tools::DNSResolver resolver = tools::DNSResolver::create();
121
122 bool avail, valid;
123
124 auto ips = resolver.get_ipv6("example.invalid", avail, valid);
125
126 ASSERT_EQ(0, ips.size());
127
128 ips = tools::DNSResolver::instance().get_ipv6("example.invalid", avail, valid);
129
130 ASSERT_EQ(0, ips.size());
131 }
132
133 TEST(DNSResolver, GetTXTRecord)
134 {
135 bool avail, valid;
136
137 std::vector<std::string> records = tools::DNSResolver::instance().get_txt_record("donate.getmonero.org", avail, valid);
138
139 EXPECT_NE(0, records.size());
140
141 for (auto& rec : records)
142 {
143 std::cout << "TXT record for donate.getmonero.org: " << rec << std::endl;
144 }
145
146 // replace first @ with .
147 std::string addr = tools::DNSResolver::instance().get_dns_format_from_oa_address("[email protected]");
148 EXPECT_STREQ("donate.getmonero.org", addr.c_str());
149
150 // no change
151 addr = tools::DNSResolver::instance().get_dns_format_from_oa_address("donate.getmonero.org");
152 EXPECT_STREQ("donate.getmonero.org", addr.c_str());
153 }
154
155 TEST(DNSResolver, Localhost)
156 {
157 tools::DNSResolver resolver = tools::DNSResolver::create();
158
159 bool avail, valid;
160 std::vector<std::string> ips = resolver.get_ipv4("localhost", avail, valid);
161
162 ASSERT_EQ(1, ips.size());
163 ASSERT_EQ("127.0.0.1", ips[0]);
164 }
165
166 bool is_equal(const char *s, const std::vector<std::string> &v) { return v.size() == 1 && v[0] == s; }
167
168 TEST(DNS_PUBLIC, empty) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("").empty()); }
169 TEST(DNS_PUBLIC, default) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("tcp").size() > 0); }
170 TEST(DNS_PUBLIC, invalid_scheme) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("invalid").empty()); }
171 TEST(DNS_PUBLIC, invalid_ip_alpha) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("tcp://invalid").empty()); }
172 TEST(DNS_PUBLIC, invalid_ip_num1) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("tcp://3").empty()); }
173 TEST(DNS_PUBLIC, invalid_ip_num3) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("tcp://3.4.5").empty()); }
174 TEST(DNS_PUBLIC, invalid_ip_num4_extra) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("tcp://3.4.5.6x").empty()); }
175 TEST(DNS_PUBLIC, invalid_ip_num4_range) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("tcp://3.4.542.6").empty()); }
176 TEST(DNS_PUBLIC, invalid_ip_dot) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("tcp://3.4.5.6.").empty()); }
177 TEST(DNS_PUBLIC, invalid_ip_num5) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("tcp://3.4.5.6.7").empty()); }
178 TEST(DNS_PUBLIC, invalid_ip_4_missing) { EXPECT_TRUE(tools::dns_utils::parse_dns_public("tcp://3.4..7").empty()); }
179 TEST(DNS_PUBLIC, valid_ip_lo) { EXPECT_TRUE(is_equal("127.0.0.1", tools::dns_utils::parse_dns_public("tcp://127.0.0.1"))); }
180 TEST(DNS_PUBLIC, valid_ip) { EXPECT_TRUE(is_equal("3.4.5.6", tools::dns_utils::parse_dns_public("tcp://3.4.5.6"))); }