blob: 17c5438beb4c4dbe6f16d53cfb777b3b0388c742 [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
Davide Pesavento2f46d652023-11-09 23:40:01 -050027#include <boost/asio/io_context.hpp>
Junxiao Shi25467942017-06-30 02:53:14 +000028
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;
Davide Pesavento2f46d652023-11-09 23:40:01 -050032namespace ip = boost::asio::ip;
Junxiao Shi25467942017-06-30 02:53:14 +000033
34class DnsFixture
35{
36public:
Junxiao Shi25467942017-06-30 02:53:14 +000037 void
Davide Pesavento2f46d652023-11-09 23:40:01 -050038 onSuccess(const boost::asio::ip::address& resolvedAddress,
39 const boost::asio::ip::address& expectedAddress,
Junxiao Shi25467942017-06-30 02:53:14 +000040 bool isValid,
41 bool shouldCheckAddress = false)
42 {
43 ++m_nSuccesses;
44
45 if (!isValid) {
Davide Pesavento2e481fc2021-07-02 18:20:03 -040046 BOOST_ERROR("Resolved to " + resolvedAddress.to_string() + ", but should have failed");
Junxiao Shi25467942017-06-30 02:53:14 +000047 }
48
49 BOOST_CHECK_EQUAL(resolvedAddress.is_v4(), expectedAddress.is_v4());
50
51 // checking address is not deterministic and should be enabled only
52 // if only one IP address will be returned by resolution
53 if (shouldCheckAddress) {
54 BOOST_CHECK_EQUAL(resolvedAddress, expectedAddress);
55 }
56 }
57
58 void
Davide Pesavento2e481fc2021-07-02 18:20:03 -040059 onFailure(bool shouldFail)
Junxiao Shi25467942017-06-30 02:53:14 +000060 {
61 ++m_nFailures;
Davide Pesavento2e481fc2021-07-02 18:20:03 -040062 BOOST_CHECK_MESSAGE(shouldFail, "Resolution should not have failed");
Junxiao Shi25467942017-06-30 02:53:14 +000063 }
64
65protected:
Davide Pesavento2e481fc2021-07-02 18:20:03 -040066 int m_nFailures = 0;
67 int m_nSuccesses = 0;
Davide Pesavento2f46d652023-11-09 23:40:01 -050068 boost::asio::io_context m_ioCtx;
Junxiao Shi25467942017-06-30 02:53:14 +000069};
70
71BOOST_AUTO_TEST_SUITE(Net)
72BOOST_FIXTURE_TEST_SUITE(TestDns, DnsFixture)
73
Davide Pesavento2f46d652023-11-09 23:40:01 -050074BOOST_AUTO_TEST_CASE(Failure)
Junxiao Shi25467942017-06-30 02:53:14 +000075{
76 SKIP_IF_IP_UNAVAILABLE();
77
78 asyncResolve("nothost.nothost.nothost.arpa",
Davide Pesavento2f46d652023-11-09 23:40:01 -050079 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), false, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -040080 [this] (auto&&...) { onFailure(true); },
Davide Pesavento2f46d652023-11-09 23:40:01 -050081 m_ioCtx); // should fail
Junxiao Shi25467942017-06-30 02:53:14 +000082
Davide Pesavento2f46d652023-11-09 23:40:01 -050083 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +000084 BOOST_CHECK_EQUAL(m_nFailures, 1);
85 BOOST_CHECK_EQUAL(m_nSuccesses, 0);
86}
87
Davide Pesavento2f46d652023-11-09 23:40:01 -050088BOOST_AUTO_TEST_CASE(Ipv4)
Junxiao Shi25467942017-06-30 02:53:14 +000089{
90 SKIP_IF_IPV4_UNAVAILABLE();
91
92 asyncResolve("192.0.2.1",
Davide Pesavento2f46d652023-11-09 23:40:01 -050093 std::bind(&DnsFixture::onSuccess, this, _1, ip::make_address_v4("192.0.2.1"), true, true),
Davide Pesavento2e481fc2021-07-02 18:20:03 -040094 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -050095 m_ioCtx);
Junxiao Shi25467942017-06-30 02:53:14 +000096
Davide Pesavento2f46d652023-11-09 23:40:01 -050097 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +000098 BOOST_CHECK_EQUAL(m_nFailures, 0);
99 BOOST_CHECK_EQUAL(m_nSuccesses, 1);
100}
101
Davide Pesavento2f46d652023-11-09 23:40:01 -0500102BOOST_AUTO_TEST_CASE(Ipv6)
Junxiao Shi25467942017-06-30 02:53:14 +0000103{
104 SKIP_IF_IPV6_UNAVAILABLE();
105
106 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2f46d652023-11-09 23:40:01 -0500107 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400108 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500109 m_ioCtx);
Junxiao Shi25467942017-06-30 02:53:14 +0000110
111 asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400112 std::bind(&DnsFixture::onSuccess, this, _1,
Davide Pesavento2f46d652023-11-09 23:40:01 -0500113 ip::make_address_v6("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"), true, true),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400114 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500115 m_ioCtx);
Junxiao Shi25467942017-06-30 02:53:14 +0000116
Davide Pesavento2f46d652023-11-09 23:40:01 -0500117 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +0000118 BOOST_CHECK_EQUAL(m_nFailures, 0);
119 BOOST_CHECK_EQUAL(m_nSuccesses, 2);
120}
121
Davide Pesavento2f46d652023-11-09 23:40:01 -0500122BOOST_AUTO_TEST_CASE(WithAddressSelector)
Junxiao Shi25467942017-06-30 02:53:14 +0000123{
124 SKIP_IF_IPV4_UNAVAILABLE();
125 SKIP_IF_IPV6_UNAVAILABLE();
126
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400127 asyncResolve("named-data.net",
Davide Pesavento2f46d652023-11-09 23:40:01 -0500128 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400129 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500130 m_ioCtx, Ipv4Only());
Junxiao Shi25467942017-06-30 02:53:14 +0000131
132 asyncResolve("a.root-servers.net",
Davide Pesavento2f46d652023-11-09 23:40:01 -0500133 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400134 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500135 m_ioCtx, Ipv4Only()); // request IPv4 address
Junxiao Shi25467942017-06-30 02:53:14 +0000136
137 asyncResolve("a.root-servers.net",
Davide Pesavento2f46d652023-11-09 23:40:01 -0500138 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400139 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500140 m_ioCtx, Ipv6Only()); // request IPv6 address
Junxiao Shi25467942017-06-30 02:53:14 +0000141
142 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2f46d652023-11-09 23:40:01 -0500143 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400144 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500145 m_ioCtx, Ipv6Only());
Junxiao Shi25467942017-06-30 02:53:14 +0000146
147 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2f46d652023-11-09 23:40:01 -0500148 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), false, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400149 [this] (auto&&...) { onFailure(true); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500150 m_ioCtx, Ipv4Only()); // should fail
Junxiao Shi25467942017-06-30 02:53:14 +0000151
Davide Pesavento2f46d652023-11-09 23:40:01 -0500152 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +0000153 BOOST_CHECK_EQUAL(m_nFailures, 1);
154 BOOST_CHECK_EQUAL(m_nSuccesses, 4);
155}
156
Junxiao Shi25467942017-06-30 02:53:14 +0000157BOOST_AUTO_TEST_SUITE_END() // TestDns
158BOOST_AUTO_TEST_SUITE_END() // Net
159
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400160} // namespace ndn::tests