blob: be94ba9cf6b72d22d2c947917be319e1c1a9dbe9 [file] [log] [blame]
Vince Lehman7a6bb352014-09-22 15:58:19 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Davide Pesavento537dc3a2016-02-18 19:35:26 +01003 * Copyright (c) 2013-2016 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"
Davide Pesavento537dc3a2016-02-18 19:35:26 +010026
27#include <boost/asio.hpp>
Vince Lehman7a6bb352014-09-22 15:58:19 -050028#include <boost/lexical_cast.hpp>
29
30namespace ndn {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080031namespace util {
32namespace tests {
Vince Lehman7a6bb352014-09-22 15:58:19 -050033
34using boost::asio::ip::address_v4;
35using boost::asio::ip::address_v6;
36
Alexander Afanasyev1286e022015-01-26 10:42:29 -080037using ndn::tests::NetworkConfigurationDetector;
38
Vince Lehman7a6bb352014-09-22 15:58:19 -050039class DnsFixture
40{
41public:
42 DnsFixture()
43 : m_nFailures(0)
44 , m_nSuccesses(0)
45 {
46 }
47
48 void
49 onSuccess(const dns::IpAddress& resolvedAddress,
50 const dns::IpAddress& expectedAddress,
51 bool isValid,
52 bool shouldCheckAddress = false)
53 {
Alexander Afanasyev3ccc6f72014-10-12 12:19:09 -070054 BOOST_TEST_MESSAGE("Resolved to: " << resolvedAddress);
Vince Lehman7a6bb352014-09-22 15:58:19 -050055
56 ++m_nSuccesses;
57
58 if (!isValid)
59 {
60 BOOST_FAIL("Resolved to " + boost::lexical_cast<std::string>(resolvedAddress)
61 + ", but should have failed");
62 }
63
64 BOOST_CHECK_EQUAL(resolvedAddress.is_v4(), expectedAddress.is_v4());
65
66 // checking address is not deterministic and should be enabled only
67 // if only one IP address will be returned by resolution
68 if (shouldCheckAddress)
69 {
70 BOOST_CHECK_EQUAL(resolvedAddress, expectedAddress);
71 }
72 }
73
74 void
75 onFailure(bool isValid)
76 {
77 ++m_nFailures;
78
79 if (!isValid)
80 {
81 BOOST_FAIL("Resolution should not have failed");
82 }
83
84 BOOST_CHECK_MESSAGE(true, "Resolution failed as expected");
85 }
86
87public:
88 uint32_t m_nFailures;
89 uint32_t m_nSuccesses;
90
91 boost::asio::io_service m_ioService;
92};
93
94BOOST_FIXTURE_TEST_SUITE(UtilDns, DnsFixture)
95
96BOOST_AUTO_TEST_CASE(Asynchronous)
97{
Alexander Afanasyev1286e022015-01-26 10:42:29 -080098 if (!NetworkConfigurationDetector::hasIpv4() && !NetworkConfigurationDetector::hasIpv6()) {
99 BOOST_TEST_MESSAGE("Platform does not support both IPv4 and IPv6, skipping test case");
100 return;
101 }
102
103 dns::asyncResolve("nothost.nothost.nothost.arpa",
104 bind(&DnsFixture::onSuccess, this, _1,
105 dns::IpAddress(address_v4()), false, false),
106 bind(&DnsFixture::onFailure, this, true),
107 m_ioService); // should fail
108 m_ioService.run();
109
110 BOOST_CHECK_EQUAL(m_nFailures, 1);
111 BOOST_CHECK_EQUAL(m_nSuccesses, 0);
112}
113
114BOOST_AUTO_TEST_CASE(AsynchronousV4)
115{
116 if (!NetworkConfigurationDetector::hasIpv4()) {
117 BOOST_TEST_MESSAGE("Platform does not support IPv4, skipping the test case");
118 return;
119 }
120
121 dns::asyncResolve("192.0.2.1",
122 bind(&DnsFixture::onSuccess, this, _1,
123 dns::IpAddress(address_v4::from_string("192.0.2.1")),
124 true, true),
125 bind(&DnsFixture::onFailure, this, false),
126 m_ioService);
127 m_ioService.run();
128
129 BOOST_CHECK_EQUAL(m_nFailures, 0);
130 BOOST_CHECK_EQUAL(m_nSuccesses, 1);
131}
132
133BOOST_AUTO_TEST_CASE(AsynchronousV6)
134{
135 if (!NetworkConfigurationDetector::hasIpv6()) {
136 BOOST_TEST_MESSAGE("Platform does not support IPv6, skipping the test case");
137 return;
138 }
139
140 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
141 bind(&DnsFixture::onSuccess, this, _1,
142 dns::IpAddress(address_v6()), true, false),
143 bind(&DnsFixture::onFailure, this, false),
144 m_ioService);
145
146 dns::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3",
147 bind(&DnsFixture::onSuccess, this, _1,
148 dns::IpAddress(address_v6::
149 from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")),
150 true, true),
151 bind(&DnsFixture::onFailure, this, false),
152 m_ioService);
153 m_ioService.run();
154
155 BOOST_CHECK_EQUAL(m_nFailures, 0);
156 BOOST_CHECK_EQUAL(m_nSuccesses, 2);
157}
158
159BOOST_AUTO_TEST_CASE(AsynchronousV4AndV6)
160{
161 if (!NetworkConfigurationDetector::hasIpv4() || !NetworkConfigurationDetector::hasIpv6()) {
162 BOOST_TEST_MESSAGE("Platform does not support either IPv4 or IPv6, skipping test case");
163 return;
164 }
165
Vince Lehman7a6bb352014-09-22 15:58:19 -0500166 dns::asyncResolve("www.named-data.net",
167 bind(&DnsFixture::onSuccess, this, _1,
168 dns::IpAddress(address_v4()), true, false),
169 bind(&DnsFixture::onFailure, this, false),
170 m_ioService,
171 dns::Ipv4Only());
172
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800173 dns::asyncResolve("a.root-servers.net",
Vince Lehman7a6bb352014-09-22 15:58:19 -0500174 bind(&DnsFixture::onSuccess, this, _1,
175 dns::IpAddress(address_v4()), true, false),
176 bind(&DnsFixture::onFailure, this, false),
177 m_ioService,
178 dns::Ipv4Only()); // request IPv4 address
179
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800180 dns::asyncResolve("a.root-servers.net",
Vince Lehman7a6bb352014-09-22 15:58:19 -0500181 bind(&DnsFixture::onSuccess, this, _1,
182 dns::IpAddress(address_v6()), true, false),
183 bind(&DnsFixture::onFailure, this, false),
184 m_ioService,
185 dns::Ipv6Only()); // request IPv6 address
186
187 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
188 bind(&DnsFixture::onSuccess, this, _1,
189 dns::IpAddress(address_v6()), true, false),
190 bind(&DnsFixture::onFailure, this, false),
Vince Lehman7a6bb352014-09-22 15:58:19 -0500191 m_ioService,
192 dns::Ipv6Only());
193
194 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
195 bind(&DnsFixture::onSuccess, this, _1,
196 dns::IpAddress(address_v6()), false, false),
197 bind(&DnsFixture::onFailure, this, true), // should fail
198 m_ioService,
199 dns::Ipv4Only());
Vince Lehman7a6bb352014-09-22 15:58:19 -0500200 m_ioService.run();
201
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800202 BOOST_CHECK_EQUAL(m_nFailures, 1);
203 BOOST_CHECK_EQUAL(m_nSuccesses, 4);
Vince Lehman7a6bb352014-09-22 15:58:19 -0500204}
205
206BOOST_AUTO_TEST_CASE(Synchronous)
207{
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800208 if (!NetworkConfigurationDetector::hasIpv4() && !NetworkConfigurationDetector::hasIpv6()) {
209 BOOST_TEST_MESSAGE("Platform does not support both IPv4 and IPv6, skipping test case");
210 return;
211 }
Vince Lehman7a6bb352014-09-22 15:58:19 -0500212 dns::IpAddress address;
213 BOOST_CHECK_NO_THROW(address = dns::syncResolve("www.named-data.net", m_ioService));
214
215 BOOST_CHECK(address.is_v4() || address.is_v6());
216}
217
218BOOST_AUTO_TEST_SUITE_END()
219
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800220} // namespace tests
221} // namespace util
Vince Lehman7a6bb352014-09-22 15:58:19 -0500222} // namespace ndn