blob: b6a24b5e77e1d84be525c449852c00cdd12630e9 [file] [log] [blame]
Junxiao Shi25467942017-06-30 02:53:14 +00001/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
Davide Pesavento74daf742018-11-23 18:14:13 -05002/*
Davide Pesavento2d7c5852022-07-18 16:02:52 -04003 * Copyright (c) 2013-2022 Regents of the University of California.
Junxiao Shi25467942017-06-30 02:53:14 +00004 *
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
Davide Pesavento7e780642018-11-24 15:51:34 -050022#include "ndn-cxx/net/dns.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000023
Davide Pesavento7e780642018-11-24 15:51:34 -050024#include "tests/boost-test.hpp"
25#include "tests/unit/net/network-configuration-detector.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000026
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:
Junxiao Shi25467942017-06-30 02:53:14 +000039 void
40 onSuccess(const IpAddress& resolvedAddress,
41 const IpAddress& expectedAddress,
42 bool isValid,
43 bool shouldCheckAddress = false)
44 {
45 ++m_nSuccesses;
46
47 if (!isValid) {
Davide Pesavento2e481fc2021-07-02 18:20:03 -040048 BOOST_ERROR("Resolved to " + resolvedAddress.to_string() + ", but should have failed");
Junxiao Shi25467942017-06-30 02:53:14 +000049 }
50
51 BOOST_CHECK_EQUAL(resolvedAddress.is_v4(), expectedAddress.is_v4());
52
53 // checking address is not deterministic and should be enabled only
54 // if only one IP address will be returned by resolution
55 if (shouldCheckAddress) {
56 BOOST_CHECK_EQUAL(resolvedAddress, expectedAddress);
57 }
58 }
59
60 void
Davide Pesavento2e481fc2021-07-02 18:20:03 -040061 onFailure(bool shouldFail)
Junxiao Shi25467942017-06-30 02:53:14 +000062 {
63 ++m_nFailures;
Davide Pesavento2e481fc2021-07-02 18:20:03 -040064 BOOST_CHECK_MESSAGE(shouldFail, "Resolution should not have failed");
Junxiao Shi25467942017-06-30 02:53:14 +000065 }
66
67protected:
Davide Pesavento2e481fc2021-07-02 18:20:03 -040068 int m_nFailures = 0;
69 int m_nSuccesses = 0;
Junxiao Shi25467942017-06-30 02:53:14 +000070 boost::asio::io_service m_ioService;
71};
72
73BOOST_AUTO_TEST_SUITE(Net)
74BOOST_FIXTURE_TEST_SUITE(TestDns, DnsFixture)
75
76BOOST_AUTO_TEST_CASE(Asynchronous)
77{
78 SKIP_IF_IP_UNAVAILABLE();
79
80 asyncResolve("nothost.nothost.nothost.arpa",
Davide Pesavento2e481fc2021-07-02 18:20:03 -040081 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), false, false),
82 [this] (auto&&...) { onFailure(true); },
Junxiao Shi25467942017-06-30 02:53:14 +000083 m_ioService); // should fail
84
85 m_ioService.run();
86 BOOST_CHECK_EQUAL(m_nFailures, 1);
87 BOOST_CHECK_EQUAL(m_nSuccesses, 0);
88}
89
90BOOST_AUTO_TEST_CASE(AsynchronousV4)
91{
92 SKIP_IF_IPV4_UNAVAILABLE();
93
94 asyncResolve("192.0.2.1",
Davide Pesavento2e481fc2021-07-02 18:20:03 -040095 std::bind(&DnsFixture::onSuccess, this, _1,
96 IpAddress(address_v4::from_string("192.0.2.1")), true, true),
97 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +000098 m_ioService);
99
100 m_ioService.run();
101 BOOST_CHECK_EQUAL(m_nFailures, 0);
102 BOOST_CHECK_EQUAL(m_nSuccesses, 1);
103}
104
105BOOST_AUTO_TEST_CASE(AsynchronousV6)
106{
107 SKIP_IF_IPV6_UNAVAILABLE();
108
109 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400110 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false),
111 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000112 m_ioService);
113
114 asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400115 std::bind(&DnsFixture::onSuccess, this, _1,
116 IpAddress(address_v6::from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")),
117 true, true),
118 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000119 m_ioService);
120
121 m_ioService.run();
122 BOOST_CHECK_EQUAL(m_nFailures, 0);
123 BOOST_CHECK_EQUAL(m_nSuccesses, 2);
124}
125
126BOOST_AUTO_TEST_CASE(AsynchronousV4AndV6)
127{
128 SKIP_IF_IPV4_UNAVAILABLE();
129 SKIP_IF_IPV6_UNAVAILABLE();
130
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400131 asyncResolve("named-data.net",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400132 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), true, false),
133 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000134 m_ioService, Ipv4Only());
135
136 asyncResolve("a.root-servers.net",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400137 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), true, false),
138 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000139 m_ioService, Ipv4Only()); // request IPv4 address
140
141 asyncResolve("a.root-servers.net",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400142 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false),
143 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000144 m_ioService, Ipv6Only()); // request IPv6 address
145
146 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400147 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false),
148 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000149 m_ioService, Ipv6Only());
150
151 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400152 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), false, false),
153 [this] (auto&&...) { onFailure(true); },
Junxiao Shi25467942017-06-30 02:53:14 +0000154 m_ioService, Ipv4Only()); // should fail
155
156 m_ioService.run();
157 BOOST_CHECK_EQUAL(m_nFailures, 1);
158 BOOST_CHECK_EQUAL(m_nSuccesses, 4);
159}
160
161BOOST_AUTO_TEST_CASE(Synchronous)
162{
163 SKIP_IF_IP_UNAVAILABLE();
164
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400165 IpAddress address = syncResolve("named-data.net", m_ioService);
Junxiao Shi25467942017-06-30 02:53:14 +0000166 BOOST_CHECK(address.is_v4() || address.is_v6());
167}
168
169BOOST_AUTO_TEST_SUITE_END() // TestDns
170BOOST_AUTO_TEST_SUITE_END() // Net
171
172} // namespace tests
173} // namespace dns
174} // namespace ndn