blob: c1e867949309c31fb55a0f07a8e623570c74b642 [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 Pesavento3de7c522024-01-25 18:50:12 -05003 * Copyright (c) 2013-2024 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"
Davide Pesavento3de7c522024-01-25 18:50:12 -050023#include "ndn-cxx/detail/common.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000024
Davide Pesavento7e780642018-11-24 15:51:34 -050025#include "tests/boost-test.hpp"
26#include "tests/unit/net/network-configuration-detector.hpp"
Junxiao Shi25467942017-06-30 02:53:14 +000027
Davide Pesavento2f46d652023-11-09 23:40:01 -050028#include <boost/asio/io_context.hpp>
Junxiao Shi25467942017-06-30 02:53:14 +000029
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040030namespace ndn::tests {
Junxiao Shi25467942017-06-30 02:53:14 +000031
Davide Pesavento47ce2ee2023-05-09 01:33:33 -040032using namespace ndn::dns;
Davide Pesavento2f46d652023-11-09 23:40:01 -050033namespace ip = boost::asio::ip;
Junxiao Shi25467942017-06-30 02:53:14 +000034
35class DnsFixture
36{
37public:
Junxiao Shi25467942017-06-30 02:53:14 +000038 void
Davide Pesavento2f46d652023-11-09 23:40:01 -050039 onSuccess(const boost::asio::ip::address& resolvedAddress,
40 const boost::asio::ip::address& expectedAddress,
Junxiao Shi25467942017-06-30 02:53:14 +000041 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;
Davide Pesavento2f46d652023-11-09 23:40:01 -050069 boost::asio::io_context m_ioCtx;
Junxiao Shi25467942017-06-30 02:53:14 +000070};
71
72BOOST_AUTO_TEST_SUITE(Net)
73BOOST_FIXTURE_TEST_SUITE(TestDns, DnsFixture)
74
Davide Pesavento2f46d652023-11-09 23:40:01 -050075BOOST_AUTO_TEST_CASE(Failure)
Junxiao Shi25467942017-06-30 02:53:14 +000076{
77 SKIP_IF_IP_UNAVAILABLE();
78
79 asyncResolve("nothost.nothost.nothost.arpa",
Davide Pesavento2f46d652023-11-09 23:40:01 -050080 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), false, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -040081 [this] (auto&&...) { onFailure(true); },
Davide Pesavento2f46d652023-11-09 23:40:01 -050082 m_ioCtx); // should fail
Junxiao Shi25467942017-06-30 02:53:14 +000083
Davide Pesavento2f46d652023-11-09 23:40:01 -050084 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +000085 BOOST_CHECK_EQUAL(m_nFailures, 1);
86 BOOST_CHECK_EQUAL(m_nSuccesses, 0);
87}
88
Davide Pesavento2f46d652023-11-09 23:40:01 -050089BOOST_AUTO_TEST_CASE(Ipv4)
Junxiao Shi25467942017-06-30 02:53:14 +000090{
91 SKIP_IF_IPV4_UNAVAILABLE();
92
93 asyncResolve("192.0.2.1",
Davide Pesavento2f46d652023-11-09 23:40:01 -050094 std::bind(&DnsFixture::onSuccess, this, _1, ip::make_address_v4("192.0.2.1"), true, true),
Davide Pesavento2e481fc2021-07-02 18:20:03 -040095 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -050096 m_ioCtx);
Junxiao Shi25467942017-06-30 02:53:14 +000097
Davide Pesavento2f46d652023-11-09 23:40:01 -050098 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +000099 BOOST_CHECK_EQUAL(m_nFailures, 0);
100 BOOST_CHECK_EQUAL(m_nSuccesses, 1);
101}
102
Davide Pesavento2f46d652023-11-09 23:40:01 -0500103BOOST_AUTO_TEST_CASE(Ipv6)
Junxiao Shi25467942017-06-30 02:53:14 +0000104{
105 SKIP_IF_IPV6_UNAVAILABLE();
106
107 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2f46d652023-11-09 23:40:01 -0500108 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400109 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500110 m_ioCtx);
Junxiao Shi25467942017-06-30 02:53:14 +0000111
112 asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400113 std::bind(&DnsFixture::onSuccess, this, _1,
Davide Pesavento2f46d652023-11-09 23:40:01 -0500114 ip::make_address_v6("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"), true, true),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400115 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500116 m_ioCtx);
Junxiao Shi25467942017-06-30 02:53:14 +0000117
Davide Pesavento2f46d652023-11-09 23:40:01 -0500118 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +0000119 BOOST_CHECK_EQUAL(m_nFailures, 0);
120 BOOST_CHECK_EQUAL(m_nSuccesses, 2);
121}
122
Davide Pesavento2f46d652023-11-09 23:40:01 -0500123BOOST_AUTO_TEST_CASE(WithAddressSelector)
Junxiao Shi25467942017-06-30 02:53:14 +0000124{
125 SKIP_IF_IPV4_UNAVAILABLE();
126 SKIP_IF_IPV6_UNAVAILABLE();
127
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400128 asyncResolve("named-data.net",
Davide Pesavento2f46d652023-11-09 23:40:01 -0500129 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400130 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500131 m_ioCtx, Ipv4Only());
Junxiao Shi25467942017-06-30 02:53:14 +0000132
133 asyncResolve("a.root-servers.net",
Davide Pesavento2f46d652023-11-09 23:40:01 -0500134 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400135 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500136 m_ioCtx, Ipv4Only()); // request IPv4 address
Junxiao Shi25467942017-06-30 02:53:14 +0000137
138 asyncResolve("a.root-servers.net",
Davide Pesavento2f46d652023-11-09 23:40:01 -0500139 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400140 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500141 m_ioCtx, Ipv6Only()); // request IPv6 address
Junxiao Shi25467942017-06-30 02:53:14 +0000142
143 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2f46d652023-11-09 23:40:01 -0500144 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400145 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500146 m_ioCtx, Ipv6Only());
Junxiao Shi25467942017-06-30 02:53:14 +0000147
148 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2f46d652023-11-09 23:40:01 -0500149 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), false, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400150 [this] (auto&&...) { onFailure(true); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500151 m_ioCtx, Ipv4Only()); // should fail
Junxiao Shi25467942017-06-30 02:53:14 +0000152
Davide Pesavento2f46d652023-11-09 23:40:01 -0500153 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +0000154 BOOST_CHECK_EQUAL(m_nFailures, 1);
155 BOOST_CHECK_EQUAL(m_nSuccesses, 4);
156}
157
Junxiao Shi25467942017-06-30 02:53:14 +0000158BOOST_AUTO_TEST_SUITE_END() // TestDns
159BOOST_AUTO_TEST_SUITE_END() // Net
160
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400161} // namespace ndn::tests