blob: 0e1aadafe50ac31b5c3297393e39ddd21d27008f [file] [log] [blame]
Junxiao Shi25467942017-06-30 02:53:14 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2017 Regents of the University of California.
4 *
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 "net/dns.hpp"
23
24#include "boost-test.hpp"
25#include "network-configuration-detector.hpp"
26
27#include <boost/asio/io_service.hpp>
28
29namespace ndn {
30namespace dns {
31namespace tests {
32
33using boost::asio::ip::address_v4;
34using boost::asio::ip::address_v6;
35
36class DnsFixture
37{
38public:
39 DnsFixture()
40 : m_nFailures(0)
41 , m_nSuccesses(0)
42 {
43 }
44
45 void
46 onSuccess(const IpAddress& resolvedAddress,
47 const IpAddress& expectedAddress,
48 bool isValid,
49 bool shouldCheckAddress = false)
50 {
51 ++m_nSuccesses;
52
53 if (!isValid) {
54 BOOST_FAIL("Resolved to " + resolvedAddress.to_string() + ", but should have failed");
55 }
56
57 BOOST_CHECK_EQUAL(resolvedAddress.is_v4(), expectedAddress.is_v4());
58
59 // checking address is not deterministic and should be enabled only
60 // if only one IP address will be returned by resolution
61 if (shouldCheckAddress) {
62 BOOST_CHECK_EQUAL(resolvedAddress, expectedAddress);
63 }
64 }
65
66 void
67 onFailure(bool isValid)
68 {
69 ++m_nFailures;
70
71 if (!isValid) {
72 BOOST_FAIL("Resolution should not have failed");
73 }
74
75 BOOST_CHECK_MESSAGE(true, "Resolution failed as expected");
76 }
77
78protected:
79 uint32_t m_nFailures;
80 uint32_t m_nSuccesses;
81 boost::asio::io_service m_ioService;
82};
83
84BOOST_AUTO_TEST_SUITE(Net)
85BOOST_FIXTURE_TEST_SUITE(TestDns, DnsFixture)
86
87BOOST_AUTO_TEST_CASE(Asynchronous)
88{
89 SKIP_IF_IP_UNAVAILABLE();
90
91 asyncResolve("nothost.nothost.nothost.arpa",
92 bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), false, false),
93 bind(&DnsFixture::onFailure, this, true),
94 m_ioService); // should fail
95
96 m_ioService.run();
97 BOOST_CHECK_EQUAL(m_nFailures, 1);
98 BOOST_CHECK_EQUAL(m_nSuccesses, 0);
99}
100
101BOOST_AUTO_TEST_CASE(AsynchronousV4)
102{
103 SKIP_IF_IPV4_UNAVAILABLE();
104
105 asyncResolve("192.0.2.1",
106 bind(&DnsFixture::onSuccess, this, _1,
107 IpAddress(address_v4::from_string("192.0.2.1")), true, true),
108 bind(&DnsFixture::onFailure, this, false),
109 m_ioService);
110
111 m_ioService.run();
112 BOOST_CHECK_EQUAL(m_nFailures, 0);
113 BOOST_CHECK_EQUAL(m_nSuccesses, 1);
114}
115
116BOOST_AUTO_TEST_CASE(AsynchronousV6)
117{
118 SKIP_IF_IPV6_UNAVAILABLE();
119
120 asyncResolve("ipv6.google.com", // only IPv6 address should be available
121 bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false),
122 bind(&DnsFixture::onFailure, this, false),
123 m_ioService);
124
125 asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3",
126 bind(&DnsFixture::onSuccess, this, _1,
127 IpAddress(address_v6::from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")),
128 true, true),
129 bind(&DnsFixture::onFailure, this, false),
130 m_ioService);
131
132 m_ioService.run();
133 BOOST_CHECK_EQUAL(m_nFailures, 0);
134 BOOST_CHECK_EQUAL(m_nSuccesses, 2);
135}
136
137BOOST_AUTO_TEST_CASE(AsynchronousV4AndV6)
138{
139 SKIP_IF_IPV4_UNAVAILABLE();
140 SKIP_IF_IPV6_UNAVAILABLE();
141
142 asyncResolve("www.named-data.net",
143 bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), true, false),
144 bind(&DnsFixture::onFailure, this, false),
145 m_ioService, Ipv4Only());
146
147 asyncResolve("a.root-servers.net",
148 bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), true, false),
149 bind(&DnsFixture::onFailure, this, false),
150 m_ioService, Ipv4Only()); // request IPv4 address
151
152 asyncResolve("a.root-servers.net",
153 bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false),
154 bind(&DnsFixture::onFailure, this, false),
155 m_ioService, Ipv6Only()); // request IPv6 address
156
157 asyncResolve("ipv6.google.com", // only IPv6 address should be available
158 bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false),
159 bind(&DnsFixture::onFailure, this, false),
160 m_ioService, Ipv6Only());
161
162 asyncResolve("ipv6.google.com", // only IPv6 address should be available
163 bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), false, false),
164 bind(&DnsFixture::onFailure, this, true),
165 m_ioService, Ipv4Only()); // should fail
166
167 m_ioService.run();
168 BOOST_CHECK_EQUAL(m_nFailures, 1);
169 BOOST_CHECK_EQUAL(m_nSuccesses, 4);
170}
171
172BOOST_AUTO_TEST_CASE(Synchronous)
173{
174 SKIP_IF_IP_UNAVAILABLE();
175
176 IpAddress address = syncResolve("www.named-data.net", m_ioService);
177 BOOST_CHECK(address.is_v4() || address.is_v6());
178}
179
180BOOST_AUTO_TEST_SUITE_END() // TestDns
181BOOST_AUTO_TEST_SUITE_END() // Net
182
183} // namespace tests
184} // namespace dns
185} // namespace ndn