blob: 630295b7dfddd6aad68e1f0600dfe180ff64c4c7 [file] [log] [blame]
Vince Lehman7a6bb352014-09-22 15:58:19 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev5946ed12015-01-19 23:41:39 -08003 * Copyright (c) 2013-2015 Regents of the University of California.
Vince Lehman7a6bb352014-09-22 15:58:19 -05004 *
5 * This file is part of ndn-cxx library (NDN C++ library with eXperimental eXtensions).
6 *
7 * ndn-cxx library is free software: you can redistribute it and/or modify it under the
8 * terms of the GNU Lesser General Public License as published by the Free Software
9 * Foundation, either version 3 of the License, or (at your option) any later version.
10 *
11 * ndn-cxx library is distributed in the hope that it will be useful, but WITHOUT ANY
12 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
13 * PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
14 *
15 * You should have received copies of the GNU General Public License and GNU Lesser
16 * General Public License along with ndn-cxx, e.g., in COPYING.md file. If not, see
17 * <http://www.gnu.org/licenses/>.
18 *
19 * See AUTHORS.md for complete list of ndn-cxx authors and contributors.
20 */
21
22#include "util/dns.hpp"
23
24#include "boost-test.hpp"
Alexander Afanasyev1286e022015-01-26 10:42:29 -080025#include "../network-configuration-detector.hpp"
Vince Lehman7a6bb352014-09-22 15:58:19 -050026#include <boost/lexical_cast.hpp>
27
28namespace ndn {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080029namespace util {
30namespace tests {
Vince Lehman7a6bb352014-09-22 15:58:19 -050031
32using boost::asio::ip::address_v4;
33using boost::asio::ip::address_v6;
34
Alexander Afanasyev1286e022015-01-26 10:42:29 -080035using ndn::tests::NetworkConfigurationDetector;
36
Vince Lehman7a6bb352014-09-22 15:58:19 -050037class DnsFixture
38{
39public:
40 DnsFixture()
41 : m_nFailures(0)
42 , m_nSuccesses(0)
43 {
44 }
45
46 void
47 onSuccess(const dns::IpAddress& resolvedAddress,
48 const dns::IpAddress& expectedAddress,
49 bool isValid,
50 bool shouldCheckAddress = false)
51 {
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070052 BOOST_TEST_MESSAGE("Resolved to: " << resolvedAddress);
Vince Lehman7a6bb352014-09-22 15:58:19 -050053
54 ++m_nSuccesses;
55
56 if (!isValid)
57 {
58 BOOST_FAIL("Resolved to " + boost::lexical_cast<std::string>(resolvedAddress)
59 + ", but should have failed");
60 }
61
62 BOOST_CHECK_EQUAL(resolvedAddress.is_v4(), expectedAddress.is_v4());
63
64 // checking address is not deterministic and should be enabled only
65 // if only one IP address will be returned by resolution
66 if (shouldCheckAddress)
67 {
68 BOOST_CHECK_EQUAL(resolvedAddress, expectedAddress);
69 }
70 }
71
72 void
73 onFailure(bool isValid)
74 {
75 ++m_nFailures;
76
77 if (!isValid)
78 {
79 BOOST_FAIL("Resolution should not have failed");
80 }
81
82 BOOST_CHECK_MESSAGE(true, "Resolution failed as expected");
83 }
84
85public:
86 uint32_t m_nFailures;
87 uint32_t m_nSuccesses;
88
89 boost::asio::io_service m_ioService;
90};
91
92BOOST_FIXTURE_TEST_SUITE(UtilDns, DnsFixture)
93
94BOOST_AUTO_TEST_CASE(Asynchronous)
95{
Alexander Afanasyev1286e022015-01-26 10:42:29 -080096 if (!NetworkConfigurationDetector::hasIpv4() && !NetworkConfigurationDetector::hasIpv6()) {
97 BOOST_TEST_MESSAGE("Platform does not support both IPv4 and IPv6, skipping test case");
98 return;
99 }
100
101 dns::asyncResolve("nothost.nothost.nothost.arpa",
102 bind(&DnsFixture::onSuccess, this, _1,
103 dns::IpAddress(address_v4()), false, false),
104 bind(&DnsFixture::onFailure, this, true),
105 m_ioService); // should fail
106 m_ioService.run();
107
108 BOOST_CHECK_EQUAL(m_nFailures, 1);
109 BOOST_CHECK_EQUAL(m_nSuccesses, 0);
110}
111
112BOOST_AUTO_TEST_CASE(AsynchronousV4)
113{
114 if (!NetworkConfigurationDetector::hasIpv4()) {
115 BOOST_TEST_MESSAGE("Platform does not support IPv4, skipping the test case");
116 return;
117 }
118
119 dns::asyncResolve("192.0.2.1",
120 bind(&DnsFixture::onSuccess, this, _1,
121 dns::IpAddress(address_v4::from_string("192.0.2.1")),
122 true, true),
123 bind(&DnsFixture::onFailure, this, false),
124 m_ioService);
125 m_ioService.run();
126
127 BOOST_CHECK_EQUAL(m_nFailures, 0);
128 BOOST_CHECK_EQUAL(m_nSuccesses, 1);
129}
130
131BOOST_AUTO_TEST_CASE(AsynchronousV6)
132{
133 if (!NetworkConfigurationDetector::hasIpv6()) {
134 BOOST_TEST_MESSAGE("Platform does not support IPv6, skipping the test case");
135 return;
136 }
137
138 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
139 bind(&DnsFixture::onSuccess, this, _1,
140 dns::IpAddress(address_v6()), true, false),
141 bind(&DnsFixture::onFailure, this, false),
142 m_ioService);
143
144 dns::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3",
145 bind(&DnsFixture::onSuccess, this, _1,
146 dns::IpAddress(address_v6::
147 from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")),
148 true, true),
149 bind(&DnsFixture::onFailure, this, false),
150 m_ioService);
151 m_ioService.run();
152
153 BOOST_CHECK_EQUAL(m_nFailures, 0);
154 BOOST_CHECK_EQUAL(m_nSuccesses, 2);
155}
156
157BOOST_AUTO_TEST_CASE(AsynchronousV4AndV6)
158{
159 if (!NetworkConfigurationDetector::hasIpv4() || !NetworkConfigurationDetector::hasIpv6()) {
160 BOOST_TEST_MESSAGE("Platform does not support either IPv4 or IPv6, skipping test case");
161 return;
162 }
163
Vince Lehman7a6bb352014-09-22 15:58:19 -0500164 dns::asyncResolve("www.named-data.net",
165 bind(&DnsFixture::onSuccess, this, _1,
166 dns::IpAddress(address_v4()), true, false),
167 bind(&DnsFixture::onFailure, this, false),
168 m_ioService,
169 dns::Ipv4Only());
170
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800171 dns::asyncResolve("a.root-servers.net",
Vince Lehman7a6bb352014-09-22 15:58:19 -0500172 bind(&DnsFixture::onSuccess, this, _1,
173 dns::IpAddress(address_v4()), true, false),
174 bind(&DnsFixture::onFailure, this, false),
175 m_ioService,
176 dns::Ipv4Only()); // request IPv4 address
177
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800178 dns::asyncResolve("a.root-servers.net",
Vince Lehman7a6bb352014-09-22 15:58:19 -0500179 bind(&DnsFixture::onSuccess, this, _1,
180 dns::IpAddress(address_v6()), true, false),
181 bind(&DnsFixture::onFailure, this, false),
182 m_ioService,
183 dns::Ipv6Only()); // request IPv6 address
184
185 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
186 bind(&DnsFixture::onSuccess, this, _1,
187 dns::IpAddress(address_v6()), true, false),
188 bind(&DnsFixture::onFailure, this, false),
Vince Lehman7a6bb352014-09-22 15:58:19 -0500189 m_ioService,
190 dns::Ipv6Only());
191
192 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
193 bind(&DnsFixture::onSuccess, this, _1,
194 dns::IpAddress(address_v6()), false, false),
195 bind(&DnsFixture::onFailure, this, true), // should fail
196 m_ioService,
197 dns::Ipv4Only());
Vince Lehman7a6bb352014-09-22 15:58:19 -0500198 m_ioService.run();
199
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800200 BOOST_CHECK_EQUAL(m_nFailures, 1);
201 BOOST_CHECK_EQUAL(m_nSuccesses, 4);
Vince Lehman7a6bb352014-09-22 15:58:19 -0500202}
203
204BOOST_AUTO_TEST_CASE(Synchronous)
205{
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800206 if (!NetworkConfigurationDetector::hasIpv4() && !NetworkConfigurationDetector::hasIpv6()) {
207 BOOST_TEST_MESSAGE("Platform does not support both IPv4 and IPv6, skipping test case");
208 return;
209 }
Vince Lehman7a6bb352014-09-22 15:58:19 -0500210 dns::IpAddress address;
211 BOOST_CHECK_NO_THROW(address = dns::syncResolve("www.named-data.net", m_ioService));
212
213 BOOST_CHECK(address.is_v4() || address.is_v6());
214}
215
216BOOST_AUTO_TEST_SUITE_END()
217
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800218} // namespace tests
219} // namespace util
Vince Lehman7a6bb352014-09-22 15:58:19 -0500220} // namespace ndn