blob: ac734dd12db0a92997aef6664f813c0eb571f6f4 [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 Pesavento47ce2ee2023-05-09 01:33:33 -04003 * Copyright (c) 2013-2023 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
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040029namespace ndn::tests {
Junxiao Shi25467942017-06-30 02:53:14 +000030
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040031using namespace ndn::dns;
Junxiao Shi25467942017-06-30 02:53:14 +000032using boost::asio::ip::address_v4;
33using boost::asio::ip::address_v6;
34
35class DnsFixture
36{
37public:
Junxiao Shi25467942017-06-30 02:53:14 +000038 void
39 onSuccess(const IpAddress& resolvedAddress,
40 const IpAddress& expectedAddress,
41 bool isValid,
42 bool shouldCheckAddress = false)
43 {
44 ++m_nSuccesses;
45
46 if (!isValid) {
Davide Pesavento2e481fc2021-07-02 18:20:03 -040047 BOOST_ERROR("Resolved to " + resolvedAddress.to_string() + ", but should have failed");
Junxiao Shi25467942017-06-30 02:53:14 +000048 }
49
50 BOOST_CHECK_EQUAL(resolvedAddress.is_v4(), expectedAddress.is_v4());
51
52 // checking address is not deterministic and should be enabled only
53 // if only one IP address will be returned by resolution
54 if (shouldCheckAddress) {
55 BOOST_CHECK_EQUAL(resolvedAddress, expectedAddress);
56 }
57 }
58
59 void
Davide Pesavento2e481fc2021-07-02 18:20:03 -040060 onFailure(bool shouldFail)
Junxiao Shi25467942017-06-30 02:53:14 +000061 {
62 ++m_nFailures;
Davide Pesavento2e481fc2021-07-02 18:20:03 -040063 BOOST_CHECK_MESSAGE(shouldFail, "Resolution should not have failed");
Junxiao Shi25467942017-06-30 02:53:14 +000064 }
65
66protected:
Davide Pesavento2e481fc2021-07-02 18:20:03 -040067 int m_nFailures = 0;
68 int m_nSuccesses = 0;
Junxiao Shi25467942017-06-30 02:53:14 +000069 boost::asio::io_service m_ioService;
70};
71
72BOOST_AUTO_TEST_SUITE(Net)
73BOOST_FIXTURE_TEST_SUITE(TestDns, DnsFixture)
74
75BOOST_AUTO_TEST_CASE(Asynchronous)
76{
77 SKIP_IF_IP_UNAVAILABLE();
78
79 asyncResolve("nothost.nothost.nothost.arpa",
Davide Pesavento2e481fc2021-07-02 18:20:03 -040080 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), false, false),
81 [this] (auto&&...) { onFailure(true); },
Junxiao Shi25467942017-06-30 02:53:14 +000082 m_ioService); // should fail
83
84 m_ioService.run();
85 BOOST_CHECK_EQUAL(m_nFailures, 1);
86 BOOST_CHECK_EQUAL(m_nSuccesses, 0);
87}
88
89BOOST_AUTO_TEST_CASE(AsynchronousV4)
90{
91 SKIP_IF_IPV4_UNAVAILABLE();
92
93 asyncResolve("192.0.2.1",
Davide Pesavento2e481fc2021-07-02 18:20:03 -040094 std::bind(&DnsFixture::onSuccess, this, _1,
95 IpAddress(address_v4::from_string("192.0.2.1")), true, true),
96 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +000097 m_ioService);
98
99 m_ioService.run();
100 BOOST_CHECK_EQUAL(m_nFailures, 0);
101 BOOST_CHECK_EQUAL(m_nSuccesses, 1);
102}
103
104BOOST_AUTO_TEST_CASE(AsynchronousV6)
105{
106 SKIP_IF_IPV6_UNAVAILABLE();
107
108 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400109 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false),
110 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000111 m_ioService);
112
113 asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400114 std::bind(&DnsFixture::onSuccess, this, _1,
115 IpAddress(address_v6::from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")),
116 true, true),
117 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000118 m_ioService);
119
120 m_ioService.run();
121 BOOST_CHECK_EQUAL(m_nFailures, 0);
122 BOOST_CHECK_EQUAL(m_nSuccesses, 2);
123}
124
125BOOST_AUTO_TEST_CASE(AsynchronousV4AndV6)
126{
127 SKIP_IF_IPV4_UNAVAILABLE();
128 SKIP_IF_IPV6_UNAVAILABLE();
129
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400130 asyncResolve("named-data.net",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400131 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), true, false),
132 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000133 m_ioService, Ipv4Only());
134
135 asyncResolve("a.root-servers.net",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400136 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), true, false),
137 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000138 m_ioService, Ipv4Only()); // request IPv4 address
139
140 asyncResolve("a.root-servers.net",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400141 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false),
142 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000143 m_ioService, Ipv6Only()); // request IPv6 address
144
145 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400146 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false),
147 [this] (auto&&...) { onFailure(false); },
Junxiao Shi25467942017-06-30 02:53:14 +0000148 m_ioService, Ipv6Only());
149
150 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400151 std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), false, false),
152 [this] (auto&&...) { onFailure(true); },
Junxiao Shi25467942017-06-30 02:53:14 +0000153 m_ioService, Ipv4Only()); // should fail
154
155 m_ioService.run();
156 BOOST_CHECK_EQUAL(m_nFailures, 1);
157 BOOST_CHECK_EQUAL(m_nSuccesses, 4);
158}
159
160BOOST_AUTO_TEST_CASE(Synchronous)
161{
162 SKIP_IF_IP_UNAVAILABLE();
163
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400164 IpAddress address = syncResolve("named-data.net", m_ioService);
Junxiao Shi25467942017-06-30 02:53:14 +0000165 BOOST_CHECK(address.is_v4() || address.is_v6());
166}
167
168BOOST_AUTO_TEST_SUITE_END() // TestDns
169BOOST_AUTO_TEST_SUITE_END() // Net
170
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400171} // namespace ndn::tests