Electroneum
Loading...
Searching...
No Matches
dns_resolver.cpp File Reference
#include <iostream>
#include <vector>
#include "gtest/gtest.h"
#include "common/dns_utils.h"
Include dependency graph for dns_resolver.cpp:

Go to the source code of this file.

Functions

 TEST (DNSResolver, IPv4Success)
 TEST (DNSResolver, IPv4Failure)
 TEST (DNSResolver, DNSSECSuccess)
 TEST (DNSResolver, DNSSECFailure)
 TEST (DNSResolver, IPv6Failure)
 TEST (DNSResolver, GetTXTRecord)
bool is_equal (const char *s, const std::vector< std::string > &v)
 TEST (DNS_PUBLIC, empty)
 TEST (DNS_PUBLIC, default)
 TEST (DNS_PUBLIC, invalid_scheme)
 TEST (DNS_PUBLIC, invalid_ip_alpha)
 TEST (DNS_PUBLIC, invalid_ip_num1)
 TEST (DNS_PUBLIC, invalid_ip_num3)
 TEST (DNS_PUBLIC, invalid_ip_num4_extra)
 TEST (DNS_PUBLIC, invalid_ip_num4_range)
 TEST (DNS_PUBLIC, invalid_ip_dot)
 TEST (DNS_PUBLIC, invalid_ip_num5)
 TEST (DNS_PUBLIC, invalid_ip_4_missing)
 TEST (DNS_PUBLIC, valid_ip_lo)
 TEST (DNS_PUBLIC, valid_ip)

Function Documentation

◆ is_equal()

bool is_equal ( const char * s,
const std::vector< std::string > & v )

Definition at line 161 of file dns_resolver.cpp.

161{ return v.size() == 1 && v[0] == s; }
Here is the caller graph for this function:

◆ TEST() [1/19]

TEST ( DNS_PUBLIC ,
default  )

Definition at line 164 of file dns_resolver.cpp.

#define EXPECT_TRUE(condition)
Definition gtest.h:1859
std::vector< std::string > parse_dns_public(const char *s)
Here is the call graph for this function:

◆ TEST() [2/19]

TEST ( DNS_PUBLIC ,
empty  )

Definition at line 163 of file dns_resolver.cpp.

Here is the call graph for this function:

◆ TEST() [3/19]

TEST ( DNS_PUBLIC ,
invalid_ip_4_missing  )

Definition at line 173 of file dns_resolver.cpp.

Here is the call graph for this function:

◆ TEST() [4/19]

TEST ( DNS_PUBLIC ,
invalid_ip_alpha  )

Definition at line 166 of file dns_resolver.cpp.

Here is the call graph for this function:

◆ TEST() [5/19]

TEST ( DNS_PUBLIC ,
invalid_ip_dot  )

Definition at line 171 of file dns_resolver.cpp.

Here is the call graph for this function:

◆ TEST() [6/19]

TEST ( DNS_PUBLIC ,
invalid_ip_num1  )

Definition at line 167 of file dns_resolver.cpp.

Here is the call graph for this function:

◆ TEST() [7/19]

TEST ( DNS_PUBLIC ,
invalid_ip_num3  )

Definition at line 168 of file dns_resolver.cpp.

Here is the call graph for this function:

◆ TEST() [8/19]

TEST ( DNS_PUBLIC ,
invalid_ip_num4_extra  )

Definition at line 169 of file dns_resolver.cpp.

Here is the call graph for this function:

◆ TEST() [9/19]

TEST ( DNS_PUBLIC ,
invalid_ip_num4_range  )

Definition at line 170 of file dns_resolver.cpp.

Here is the call graph for this function:

◆ TEST() [10/19]

TEST ( DNS_PUBLIC ,
invalid_ip_num5  )

Definition at line 172 of file dns_resolver.cpp.

Here is the call graph for this function:

◆ TEST() [11/19]

TEST ( DNS_PUBLIC ,
invalid_scheme  )

Definition at line 165 of file dns_resolver.cpp.

Here is the call graph for this function:

◆ TEST() [12/19]

TEST ( DNS_PUBLIC ,
valid_ip  )

Definition at line 175 of file dns_resolver.cpp.

175{ EXPECT_TRUE(is_equal("3.4.5.6", tools::dns_utils::parse_dns_public("tcp://3.4.5.6"))); }
bool is_equal(const char *s, const std::vector< std::string > &v)
Here is the call graph for this function:

◆ TEST() [13/19]

TEST ( DNS_PUBLIC ,
valid_ip_lo  )

Definition at line 174 of file dns_resolver.cpp.

174{ EXPECT_TRUE(is_equal("127.0.0.1", tools::dns_utils::parse_dns_public("tcp://127.0.0.1"))); }
Here is the call graph for this function:

◆ TEST() [14/19]

TEST ( DNSResolver ,
DNSSECFailure  )

Definition at line 87 of file dns_resolver.cpp.

88{
90
91 bool avail, valid;
92
93 auto ips = resolver.get_ipv4("dnssec-failed.org", avail, valid);
94
95 ASSERT_EQ(1, ips.size());
96
97 //ASSERT_STREQ("93.184.216.119", ips[0].c_str());
98
99 ASSERT_TRUE(avail);
100 ASSERT_FALSE(valid);
101}
Provides high-level access to DNS resolution.
Definition dns_utils.h:56
std::vector< std::string > get_ipv4(const std::string &url, bool &dnssec_available, bool &dnssec_valid)
gets ipv4 addresses from DNS query of a URL
static DNSResolver create()
Gets a new instance of DNSResolver.
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1956
#define ASSERT_FALSE(condition)
Definition gtest.h:1868
#define ASSERT_TRUE(condition)
Definition gtest.h:1865
Here is the call graph for this function:

◆ TEST() [15/19]

TEST ( DNSResolver ,
DNSSECSuccess  )

Definition at line 71 of file dns_resolver.cpp.

72{
74
75 bool avail, valid;
76
77 auto ips = resolver.get_ipv4("example.com", avail, valid);
78
79 ASSERT_EQ(1, ips.size());
80
81 //ASSERT_STREQ("93.184.216.119", ips[0].c_str());
82
83 ASSERT_TRUE(avail);
84 ASSERT_TRUE(valid);
85}
Here is the call graph for this function:

◆ TEST() [16/19]

TEST ( DNSResolver ,
GetTXTRecord  )

Definition at line 139 of file dns_resolver.cpp.

140{
141 bool avail, valid;
142
143 std::vector<std::string> records = tools::DNSResolver::instance().get_txt_record("donate.electroneumpulse.com", avail, valid);
144
145 EXPECT_NE(0, records.size());
146
147 for (auto& rec : records)
148 {
149 std::cout << "TXT record for donate.electroneumpulse.com: " << rec << std::endl;
150 }
151
152 // replace first @ with .
153 std::string addr = tools::DNSResolver::instance().get_dns_format_from_oa_address("donate@electroneumpulse.com");
154 EXPECT_STREQ("donate.electroneumpulse.com", addr.c_str());
155
156 // no change
157 addr = tools::DNSResolver::instance().get_dns_format_from_oa_address("donate.electroneumpulse.com");
158 EXPECT_STREQ("donate.electroneumpulse.com", addr.c_str());
159}
static DNSResolver & instance()
Gets the singleton instance of DNSResolver.
std::vector< std::string > get_txt_record(const std::string &url, bool &dnssec_available, bool &dnssec_valid)
gets all TXT records from a DNS query for the supplied URL; if no TXT record present returns an empty...
std::string get_dns_format_from_oa_address(const std::string &oa_addr)
Gets a DNS address from OpenAlias format.
#define EXPECT_NE(val1, val2)
Definition gtest.h:1926
#define EXPECT_STREQ(s1, s2)
Definition gtest.h:1995
Here is the call graph for this function:

◆ TEST() [17/19]

TEST ( DNSResolver ,
IPv4Failure  )

Definition at line 55 of file dns_resolver.cpp.

56{
57 // guaranteed by IANA/ICANN/RFC to be invalid
59
60 bool avail, valid;
61
62 auto ips = resolver.get_ipv4("example.invalid", avail, valid);
63
64 ASSERT_EQ(0, ips.size());
65
66 ips = tools::DNSResolver::instance().get_ipv4("example.invalid", avail, valid);
67
68 ASSERT_EQ(0, ips.size());
69}
Here is the call graph for this function:

◆ TEST() [18/19]

TEST ( DNSResolver ,
IPv4Success  )

Definition at line 36 of file dns_resolver.cpp.

37{
39
40 bool avail, valid;
41
42 auto ips = resolver.get_ipv4("example.com", avail, valid);
43
44 ASSERT_EQ(1, ips.size());
45
46 //ASSERT_STREQ("93.184.216.119", ips[0].c_str());
47
48 ips = tools::DNSResolver::instance().get_ipv4("example.com", avail, valid);
49
50 ASSERT_EQ(1, ips.size());
51
52 //ASSERT_STREQ("93.184.216.119", ips[0].c_str());
53}
Here is the call graph for this function:

◆ TEST() [19/19]

TEST ( DNSResolver ,
IPv6Failure  )

Definition at line 123 of file dns_resolver.cpp.

124{
125 // guaranteed by IANA/ICANN/RFC to be invalid
127
128 bool avail, valid;
129
130 auto ips = resolver.get_ipv6("example.invalid", avail, valid);
131
132 ASSERT_EQ(0, ips.size());
133
134 ips = tools::DNSResolver::instance().get_ipv6("example.invalid", avail, valid);
135
136 ASSERT_EQ(0, ips.size());
137}
std::vector< std::string > get_ipv6(const std::string &url, bool &dnssec_available, bool &dnssec_valid)
gets ipv6 addresses from DNS query
Here is the call graph for this function: