blob: 94cf027b130e105172f09b6eba5c6597194ff16d [file] [log] [blame]
Vince Lehman7a6bb352014-09-22 15:58:19 -05001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
3 * Copyright (c) 2013-2014 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 "util/dns.hpp"
23
24#include "boost-test.hpp"
25#include <boost/lexical_cast.hpp>
26
27namespace ndn {
28
29using boost::asio::ip::address_v4;
30using boost::asio::ip::address_v6;
31
32class DnsFixture
33{
34public:
35 DnsFixture()
36 : m_nFailures(0)
37 , m_nSuccesses(0)
38 {
39 }
40
41 void
42 onSuccess(const dns::IpAddress& resolvedAddress,
43 const dns::IpAddress& expectedAddress,
44 bool isValid,
45 bool shouldCheckAddress = false)
46 {
47 std::cout << "Resolved to: " << resolvedAddress << std::endl;
48
49 ++m_nSuccesses;
50
51 if (!isValid)
52 {
53 BOOST_FAIL("Resolved to " + boost::lexical_cast<std::string>(resolvedAddress)
54 + ", 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 {
63 BOOST_CHECK_EQUAL(resolvedAddress, expectedAddress);
64 }
65 }
66
67 void
68 onFailure(bool isValid)
69 {
70 ++m_nFailures;
71
72 if (!isValid)
73 {
74 BOOST_FAIL("Resolution should not have failed");
75 }
76
77 BOOST_CHECK_MESSAGE(true, "Resolution failed as expected");
78 }
79
80public:
81 uint32_t m_nFailures;
82 uint32_t m_nSuccesses;
83
84 boost::asio::io_service m_ioService;
85};
86
87BOOST_FIXTURE_TEST_SUITE(UtilDns, DnsFixture)
88
89BOOST_AUTO_TEST_CASE(Asynchronous)
90{
91 dns::asyncResolve("www.named-data.net",
92 bind(&DnsFixture::onSuccess, this, _1,
93 dns::IpAddress(address_v4()), true, false),
94 bind(&DnsFixture::onFailure, this, false),
95 m_ioService,
96 dns::Ipv4Only());
97
98 dns::asyncResolve("nothost.nothost.nothost.arpa",
99 bind(&DnsFixture::onSuccess, this, _1,
100 dns::IpAddress(address_v4()), false, false),
101 bind(&DnsFixture::onFailure, this, true),
102 m_ioService); // should fail
103
104 dns::asyncResolve("www.google.com",
105 bind(&DnsFixture::onSuccess, this, _1,
106 dns::IpAddress(address_v4()), true, false),
107 bind(&DnsFixture::onFailure, this, false),
108 m_ioService,
109 dns::Ipv4Only()); // request IPv4 address
110
111 dns::asyncResolve("www.google.com",
112 bind(&DnsFixture::onSuccess, this, _1,
113 dns::IpAddress(address_v6()), true, false),
114 bind(&DnsFixture::onFailure, this, false),
115 m_ioService,
116 dns::Ipv6Only()); // request IPv6 address
117
118 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
119 bind(&DnsFixture::onSuccess, this, _1,
120 dns::IpAddress(address_v6()), true, false),
121 bind(&DnsFixture::onFailure, this, false),
122 m_ioService);
123
124 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
125 bind(&DnsFixture::onSuccess, this, _1,
126 dns::IpAddress(address_v6()), true, false),
127 bind(&DnsFixture::onFailure, this, false),
128 m_ioService,
129 dns::Ipv6Only());
130
131 dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available
132 bind(&DnsFixture::onSuccess, this, _1,
133 dns::IpAddress(address_v6()), false, false),
134 bind(&DnsFixture::onFailure, this, true), // should fail
135 m_ioService,
136 dns::Ipv4Only());
137
138 dns::asyncResolve("192.0.2.1",
139 bind(&DnsFixture::onSuccess, this, _1,
140 dns::IpAddress(address_v4::from_string("192.0.2.1")),
141 true, true),
142 bind(&DnsFixture::onFailure, this, false),
143 m_ioService);
144
145 dns::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3",
146 bind(&DnsFixture::onSuccess, this, _1,
147 dns::IpAddress(address_v6::
148 from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")),
149 true, true),
150 bind(&DnsFixture::onFailure, this, false),
151 m_ioService);
152
153 m_ioService.run();
154
155 BOOST_CHECK_EQUAL(m_nFailures, 2);
156 BOOST_CHECK_EQUAL(m_nSuccesses, 7);
157}
158
159BOOST_AUTO_TEST_CASE(Synchronous)
160{
161 dns::IpAddress address;
162 BOOST_CHECK_NO_THROW(address = dns::syncResolve("www.named-data.net", m_ioService));
163
164 BOOST_CHECK(address.is_v4() || address.is_v6());
165}
166
167BOOST_AUTO_TEST_SUITE_END()
168
169} // namespace ndn