blob: 18d35b727d2ddae40b3dafb0b31ed6afb417d1b3 [file] [log] [blame]
Alexander Afanasyev452e2822014-02-27 14:48:04 -08001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
2/**
Alexander Afanasyev9bcbc7c2014-04-06 19:37:37 -07003 * Copyright (c) 2014 Regents of the University of California,
4 * Arizona Board of Regents,
5 * Colorado State University,
6 * University Pierre & Marie Curie, Sorbonne University,
7 * Washington University in St. Louis,
8 * Beijing Institute of Technology
9 *
10 * This file is part of NFD (Named Data Networking Forwarding Daemon).
11 * See AUTHORS.md for complete list of NFD authors and contributors.
12 *
13 * NFD is free software: you can redistribute it and/or modify it under the terms
14 * of the GNU General Public License as published by the Free Software Foundation,
15 * either version 3 of the License, or (at your option) any later version.
16 *
17 * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
18 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
19 * PURPOSE. See the GNU General Public License for more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>.
23 **/
Alexander Afanasyev452e2822014-02-27 14:48:04 -080024
25#include "core/resolver.hpp"
26#include "core/logger.hpp"
Alexander Afanasyev452e2822014-02-27 14:48:04 -080027
28#include "tests/test-common.hpp"
29
30namespace nfd {
31namespace tests {
32
33NFD_LOG_INIT("tests.CoreResolver");
34
35using boost::asio::ip::address_v4;
36using boost::asio::ip::address_v6;
37using boost::asio::ip::tcp;
38using boost::asio::ip::udp;
39
40BOOST_FIXTURE_TEST_SUITE(CoreResolver, BaseFixture)
41
42template<class Protocol>
43class ResolverFixture : protected BaseFixture
44{
45public:
46 ResolverFixture()
47 : m_nFailures(0)
48 , m_nSuccesses(0)
49 {
50 }
51
52 void
53 onSuccess(const typename Protocol::endpoint& resolvedEndpoint,
54 const typename Protocol::endpoint& expectedEndpoint,
55 bool isValid, bool wantCheckAddress = false)
56 {
57 NFD_LOG_DEBUG("Resolved to: " << resolvedEndpoint);
58 ++m_nSuccesses;
59
60 if (!isValid)
61 {
62 BOOST_FAIL("Resolved to " + boost::lexical_cast<std::string>(resolvedEndpoint)
63 + ", but it should have failed");
64 }
65
66 BOOST_CHECK_EQUAL(resolvedEndpoint.port(), expectedEndpoint.port());
67
68 BOOST_CHECK_EQUAL(resolvedEndpoint.address().is_v4(), expectedEndpoint.address().is_v4());
69
70 // checking address is not deterministic and should be enabled only
71 // if only one IP address will be returned by resolution
72 if (wantCheckAddress)
73 {
74 BOOST_CHECK_EQUAL(resolvedEndpoint.address(), expectedEndpoint.address());
75 }
76 }
77
78 void
79 onFailure(bool isValid)
80 {
81 ++m_nFailures;
82
83 if (!isValid)
84 BOOST_FAIL("Resolution should not have failed");
85
86 BOOST_CHECK_MESSAGE(true, "Resolution failed as expected");
87 }
88
89
90 uint32_t m_nFailures;
91 uint32_t m_nSuccesses;
92};
93
94BOOST_FIXTURE_TEST_CASE(Tcp, ResolverFixture<tcp>)
95{
96 TcpResolver::asyncResolve("www.named-data.net", "6363",
97 bind(&ResolverFixture<tcp>::onSuccess, this, _1,
98 tcp::endpoint(address_v4(), 6363), true, false),
99 bind(&ResolverFixture<tcp>::onFailure, this, false));
100
101 TcpResolver::asyncResolve("www.named-data.net", "notport",
102 bind(&ResolverFixture<tcp>::onSuccess, this, _1,
103 tcp::endpoint(address_v4(), 0), false, false),
104 bind(&ResolverFixture<tcp>::onFailure, this, true)); // should fail
105
106
107 TcpResolver::asyncResolve("nothost.nothost.nothost.arpa", "6363",
108 bind(&ResolverFixture<tcp>::onSuccess, this, _1,
109 tcp::endpoint(address_v4(), 6363), false, false),
110 bind(&ResolverFixture<tcp>::onFailure, this, true)); // should fail
111
112 TcpResolver::asyncResolve("www.google.com", "80",
113 bind(&ResolverFixture<tcp>::onSuccess, this, _1,
114 tcp::endpoint(address_v4(), 80), true, false),
115 bind(&ResolverFixture<tcp>::onFailure, this, false),
116 resolver::Ipv4Address()); // request IPv4 address
117
118 TcpResolver::asyncResolve("www.google.com", "80",
119 bind(&ResolverFixture<tcp>::onSuccess, this, _1,
120 tcp::endpoint(address_v6(), 80), true, false),
121 bind(&ResolverFixture<tcp>::onFailure, this, false),
122 resolver::Ipv6Address()); // request IPv6 address
123
124 TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
125 bind(&ResolverFixture<tcp>::onSuccess, this, _1,
126 tcp::endpoint(address_v6(), 80), true, false),
127 bind(&ResolverFixture<tcp>::onFailure, this, false));
128
129 TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
130 bind(&ResolverFixture<tcp>::onSuccess, this, _1,
131 tcp::endpoint(address_v6(), 80), true, false),
132 bind(&ResolverFixture<tcp>::onFailure, this, false),
133 resolver::Ipv6Address());
134
135 TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
136 bind(&ResolverFixture<tcp>::onSuccess, this, _1,
137 tcp::endpoint(address_v6(), 80), false, false),
138 bind(&ResolverFixture<tcp>::onFailure, this, true), // should fail
139 resolver::Ipv4Address());
140
141 TcpResolver::asyncResolve("192.0.2.1", "80",
142 bind(&ResolverFixture<tcp>::onSuccess, this, _1,
143 tcp::endpoint(address_v4::from_string("192.0.2.1"), 80), true, true),
144 bind(&ResolverFixture<tcp>::onFailure, this, false));
145
146 TcpResolver::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", "80",
147 bind(&ResolverFixture<tcp>::onSuccess, this, _1,
148 tcp::endpoint(address_v6::
149 from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"),
150 80), true, true),
151 bind(&ResolverFixture<tcp>::onFailure, this, false));
152
153 g_io.run();
154
155 BOOST_CHECK_EQUAL(m_nFailures, 3);
156 BOOST_CHECK_EQUAL(m_nSuccesses, 7);
157}
158
159BOOST_AUTO_TEST_CASE(SyncTcp)
160{
161 tcp::endpoint endpoint;
162 BOOST_CHECK_NO_THROW(endpoint = TcpResolver::syncResolve("www.named-data.net", "6363"));
163 NFD_LOG_DEBUG("Resolved to: " << endpoint);
164 BOOST_CHECK_EQUAL(endpoint.address().is_v4(), true);
165 BOOST_CHECK_EQUAL(endpoint.port(), 6363);
166}
167
168BOOST_FIXTURE_TEST_CASE(Udp, ResolverFixture<udp>)
169{
170 UdpResolver::asyncResolve("www.named-data.net", "6363",
171 bind(&ResolverFixture<udp>::onSuccess, this, _1,
172 udp::endpoint(address_v4(), 6363), true, false),
173 bind(&ResolverFixture<udp>::onFailure, this, false));
174
175 UdpResolver::asyncResolve("www.named-data.net", "notport",
176 bind(&ResolverFixture<udp>::onSuccess, this, _1,
177 udp::endpoint(address_v4(), 0), false, false),
178 bind(&ResolverFixture<udp>::onFailure, this, true)); // should fail
179
180
181 UdpResolver::asyncResolve("nothost.nothost.nothost.arpa", "6363",
182 bind(&ResolverFixture<udp>::onSuccess, this, _1,
183 udp::endpoint(address_v4(), 6363), false, false),
184 bind(&ResolverFixture<udp>::onFailure, this, true)); // should fail
185
186 UdpResolver::asyncResolve("www.google.com", "80",
187 bind(&ResolverFixture<udp>::onSuccess, this, _1,
188 udp::endpoint(address_v4(), 80), true, false),
189 bind(&ResolverFixture<udp>::onFailure, this, false),
190 resolver::Ipv4Address()); // request IPv4 address
191
192 UdpResolver::asyncResolve("www.google.com", "80",
193 bind(&ResolverFixture<udp>::onSuccess, this, _1,
194 udp::endpoint(address_v6(), 80), true, false),
195 bind(&ResolverFixture<udp>::onFailure, this, false),
196 resolver::Ipv6Address()); // request IPv6 address
197
198 UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
199 bind(&ResolverFixture<udp>::onSuccess, this, _1,
200 udp::endpoint(address_v6(), 80), true, false),
201 bind(&ResolverFixture<udp>::onFailure, this, false));
202
203 UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
204 bind(&ResolverFixture<udp>::onSuccess, this, _1,
205 udp::endpoint(address_v6(), 80), true, false),
206 bind(&ResolverFixture<udp>::onFailure, this, false),
207 resolver::Ipv6Address());
208
209 UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
210 bind(&ResolverFixture<udp>::onSuccess, this, _1,
211 udp::endpoint(address_v6(), 80), false, false),
212 bind(&ResolverFixture<udp>::onFailure, this, true), // should fail
213 resolver::Ipv4Address());
214
215 UdpResolver::asyncResolve("192.0.2.1", "80",
216 bind(&ResolverFixture<udp>::onSuccess, this, _1,
217 udp::endpoint(address_v4::from_string("192.0.2.1"), 80), true, true),
218 bind(&ResolverFixture<udp>::onFailure, this, false));
219
220 UdpResolver::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", "80",
221 bind(&ResolverFixture<udp>::onSuccess, this, _1,
222 udp::endpoint(address_v6::
223 from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"),
224 80), true, true),
225 bind(&ResolverFixture<udp>::onFailure, this, false));
226
227 g_io.run();
228
229 BOOST_CHECK_EQUAL(m_nFailures, 3);
230 BOOST_CHECK_EQUAL(m_nSuccesses, 7);
231}
232
233BOOST_AUTO_TEST_CASE(SyncUdp)
234{
235 udp::endpoint endpoint;
236 BOOST_CHECK_NO_THROW(endpoint = UdpResolver::syncResolve("www.named-data.net", "6363"));
237 NFD_LOG_DEBUG("Resolved to: " << endpoint);
238 BOOST_CHECK_EQUAL(endpoint.address().is_v4(), true);
239 BOOST_CHECK_EQUAL(endpoint.port(), 6363);
240}
241
242
243BOOST_AUTO_TEST_SUITE_END()
244
245} // namespace tests
246} // namespace nfd