blob: e76d5938c614bd43cd5c9d96d4f671781202a4be [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
Davide Pesaventoeee3e822016-11-26 19:19:34 +010027#include <boost/asio/io_service.hpp>
Vince Lehman7a6bb352014-09-22 15:58:19 -050028
29namespace ndn {
Alexander Afanasyev1286e022015-01-26 10:42:29 -080030namespace util {
31namespace tests {
Vince Lehman7a6bb352014-09-22 15:58:19 -050032
33using boost::asio::ip::address_v4;
34using boost::asio::ip::address_v6;
35
Alexander Afanasyev1286e022015-01-26 10:42:29 -080036using ndn::tests::NetworkConfigurationDetector;
37
Vince Lehman7a6bb352014-09-22 15:58:19 -050038class DnsFixture
39{
40public:
41 DnsFixture()
42 : m_nFailures(0)
43 , m_nSuccesses(0)
44 {
45 }
46
47 void
48 onSuccess(const dns::IpAddress& resolvedAddress,
49 const dns::IpAddress& expectedAddress,
50 bool isValid,
51 bool shouldCheckAddress = false)
52 {
Vince Lehman7a6bb352014-09-22 15:58:19 -050053 ++m_nSuccesses;
54
Davide Pesaventoeee3e822016-11-26 19:19:34 +010055 if (!isValid) {
56 BOOST_FAIL("Resolved to " + resolvedAddress.to_string() + ", but should have failed");
57 }
Vince Lehman7a6bb352014-09-22 15:58:19 -050058
59 BOOST_CHECK_EQUAL(resolvedAddress.is_v4(), expectedAddress.is_v4());
60
61 // checking address is not deterministic and should be enabled only
62 // if only one IP address will be returned by resolution
Davide Pesaventoeee3e822016-11-26 19:19:34 +010063 if (shouldCheckAddress) {
64 BOOST_CHECK_EQUAL(resolvedAddress, expectedAddress);
65 }
Vince Lehman7a6bb352014-09-22 15:58:19 -050066 }
67
68 void
69 onFailure(bool isValid)
70 {
71 ++m_nFailures;
72
Davide Pesaventoeee3e822016-11-26 19:19:34 +010073 if (!isValid) {
74 BOOST_FAIL("Resolution should not have failed");
75 }
Vince Lehman7a6bb352014-09-22 15:58:19 -050076
77 BOOST_CHECK_MESSAGE(true, "Resolution failed as expected");
78 }
79
Davide Pesaventoeee3e822016-11-26 19:19:34 +010080protected:
Vince Lehman7a6bb352014-09-22 15:58:19 -050081 uint32_t m_nFailures;
82 uint32_t m_nSuccesses;
Vince Lehman7a6bb352014-09-22 15:58:19 -050083 boost::asio::io_service m_ioService;
84};
85
Davide Pesaventoeee3e822016-11-26 19:19:34 +010086BOOST_AUTO_TEST_SUITE(Util)
87BOOST_FIXTURE_TEST_SUITE(TestDns, DnsFixture)
Vince Lehman7a6bb352014-09-22 15:58:19 -050088
89BOOST_AUTO_TEST_CASE(Asynchronous)
90{
Alexander Afanasyev1286e022015-01-26 10:42:29 -080091 if (!NetworkConfigurationDetector::hasIpv4() && !NetworkConfigurationDetector::hasIpv6()) {
Davide Pesaventoeee3e822016-11-26 19:19:34 +010092 BOOST_WARN_MESSAGE(false, "skipping assertions that require either IPv4 or IPv6 support");
Alexander Afanasyev1286e022015-01-26 10:42:29 -080093 return;
94 }
95
96 dns::asyncResolve("nothost.nothost.nothost.arpa",
97 bind(&DnsFixture::onSuccess, this, _1,
98 dns::IpAddress(address_v4()), false, false),
99 bind(&DnsFixture::onFailure, this, true),
100 m_ioService); // should fail
101 m_ioService.run();
102
103 BOOST_CHECK_EQUAL(m_nFailures, 1);
104 BOOST_CHECK_EQUAL(m_nSuccesses, 0);
105}
106
107BOOST_AUTO_TEST_CASE(AsynchronousV4)
108{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100109 SKIP_IF_IPV4_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800110
111 dns::asyncResolve("192.0.2.1",
112 bind(&DnsFixture::onSuccess, this, _1,
113 dns::IpAddress(address_v4::from_string("192.0.2.1")),
114 true, true),
115 bind(&DnsFixture::onFailure, this, false),
116 m_ioService);
117 m_ioService.run();
118
119 BOOST_CHECK_EQUAL(m_nFailures, 0);
120 BOOST_CHECK_EQUAL(m_nSuccesses, 1);
121}
122
123BOOST_AUTO_TEST_CASE(AsynchronousV6)
124{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100125 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800126
127 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
128 bind(&DnsFixture::onSuccess, this, _1,
129 dns::IpAddress(address_v6()), true, false),
130 bind(&DnsFixture::onFailure, this, false),
131 m_ioService);
132
133 dns::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3",
134 bind(&DnsFixture::onSuccess, this, _1,
135 dns::IpAddress(address_v6::
136 from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")),
137 true, true),
138 bind(&DnsFixture::onFailure, this, false),
139 m_ioService);
140 m_ioService.run();
141
142 BOOST_CHECK_EQUAL(m_nFailures, 0);
143 BOOST_CHECK_EQUAL(m_nSuccesses, 2);
144}
145
146BOOST_AUTO_TEST_CASE(AsynchronousV4AndV6)
147{
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100148 SKIP_IF_IPV4_UNAVAILABLE();
149 SKIP_IF_IPV6_UNAVAILABLE();
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800150
Vince Lehman7a6bb352014-09-22 15:58:19 -0500151 dns::asyncResolve("www.named-data.net",
152 bind(&DnsFixture::onSuccess, this, _1,
153 dns::IpAddress(address_v4()), true, false),
154 bind(&DnsFixture::onFailure, this, false),
155 m_ioService,
156 dns::Ipv4Only());
157
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800158 dns::asyncResolve("a.root-servers.net",
Vince Lehman7a6bb352014-09-22 15:58:19 -0500159 bind(&DnsFixture::onSuccess, this, _1,
160 dns::IpAddress(address_v4()), true, false),
161 bind(&DnsFixture::onFailure, this, false),
162 m_ioService,
163 dns::Ipv4Only()); // request IPv4 address
164
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800165 dns::asyncResolve("a.root-servers.net",
Vince Lehman7a6bb352014-09-22 15:58:19 -0500166 bind(&DnsFixture::onSuccess, this, _1,
167 dns::IpAddress(address_v6()), true, false),
168 bind(&DnsFixture::onFailure, this, false),
169 m_ioService,
170 dns::Ipv6Only()); // request IPv6 address
171
172 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
173 bind(&DnsFixture::onSuccess, this, _1,
174 dns::IpAddress(address_v6()), true, false),
175 bind(&DnsFixture::onFailure, this, false),
Vince Lehman7a6bb352014-09-22 15:58:19 -0500176 m_ioService,
177 dns::Ipv6Only());
178
179 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
180 bind(&DnsFixture::onSuccess, this, _1,
181 dns::IpAddress(address_v6()), false, false),
182 bind(&DnsFixture::onFailure, this, true), // should fail
183 m_ioService,
184 dns::Ipv4Only());
Vince Lehman7a6bb352014-09-22 15:58:19 -0500185 m_ioService.run();
186
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800187 BOOST_CHECK_EQUAL(m_nFailures, 1);
188 BOOST_CHECK_EQUAL(m_nSuccesses, 4);
Vince Lehman7a6bb352014-09-22 15:58:19 -0500189}
190
191BOOST_AUTO_TEST_CASE(Synchronous)
192{
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800193 if (!NetworkConfigurationDetector::hasIpv4() && !NetworkConfigurationDetector::hasIpv6()) {
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100194 BOOST_WARN_MESSAGE(false, "skipping assertions that require either IPv4 or IPv6 support");
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800195 return;
196 }
Vince Lehman7a6bb352014-09-22 15:58:19 -0500197 dns::IpAddress address;
198 BOOST_CHECK_NO_THROW(address = dns::syncResolve("www.named-data.net", m_ioService));
199
200 BOOST_CHECK(address.is_v4() || address.is_v6());
201}
202
Davide Pesaventoeee3e822016-11-26 19:19:34 +0100203BOOST_AUTO_TEST_SUITE_END() // TestDns
204BOOST_AUTO_TEST_SUITE_END() // Util
Vince Lehman7a6bb352014-09-22 15:58:19 -0500205
Alexander Afanasyev1286e022015-01-26 10:42:29 -0800206} // namespace tests
207} // namespace util
Vince Lehman7a6bb352014-09-22 15:58:19 -0500208} // namespace ndn