blob: b28d6edfd9072dbfcf20f1cdb0f2229858f58806 [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 Pesavento13260512024-02-01 21:16:04 -050039 onSuccess(const ip::address& resolvedAddress, const ip::address& expectedAddress,
40 bool isValid, bool shouldCheckAddress = false)
Junxiao Shi25467942017-06-30 02:53:14 +000041 {
42 ++m_nSuccesses;
43
44 if (!isValid) {
Davide Pesavento2e481fc2021-07-02 18:20:03 -040045 BOOST_ERROR("Resolved to " + resolvedAddress.to_string() + ", but should have failed");
Junxiao Shi25467942017-06-30 02:53:14 +000046 }
47
48 BOOST_CHECK_EQUAL(resolvedAddress.is_v4(), expectedAddress.is_v4());
49
50 // checking address is not deterministic and should be enabled only
51 // if only one IP address will be returned by resolution
52 if (shouldCheckAddress) {
53 BOOST_CHECK_EQUAL(resolvedAddress, expectedAddress);
54 }
55 }
56
57 void
Davide Pesavento2e481fc2021-07-02 18:20:03 -040058 onFailure(bool shouldFail)
Junxiao Shi25467942017-06-30 02:53:14 +000059 {
60 ++m_nFailures;
Davide Pesavento2e481fc2021-07-02 18:20:03 -040061 BOOST_CHECK_MESSAGE(shouldFail, "Resolution should not have failed");
Junxiao Shi25467942017-06-30 02:53:14 +000062 }
63
64protected:
Davide Pesavento2e481fc2021-07-02 18:20:03 -040065 int m_nFailures = 0;
66 int m_nSuccesses = 0;
Davide Pesavento2f46d652023-11-09 23:40:01 -050067 boost::asio::io_context m_ioCtx;
Junxiao Shi25467942017-06-30 02:53:14 +000068};
69
70BOOST_AUTO_TEST_SUITE(Net)
71BOOST_FIXTURE_TEST_SUITE(TestDns, DnsFixture)
72
Davide Pesavento13260512024-02-01 21:16:04 -050073BOOST_AUTO_TEST_CASE(Failure,
74 * ut::precondition(NetworkConfigurationDetector::hasIpv4OrIpv6))
Junxiao Shi25467942017-06-30 02:53:14 +000075{
Junxiao Shi25467942017-06-30 02:53:14 +000076 asyncResolve("nothost.nothost.nothost.arpa",
Davide Pesavento2f46d652023-11-09 23:40:01 -050077 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), false, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -040078 [this] (auto&&...) { onFailure(true); },
Davide Pesavento2f46d652023-11-09 23:40:01 -050079 m_ioCtx); // should fail
Junxiao Shi25467942017-06-30 02:53:14 +000080
Davide Pesavento2f46d652023-11-09 23:40:01 -050081 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +000082 BOOST_CHECK_EQUAL(m_nFailures, 1);
83 BOOST_CHECK_EQUAL(m_nSuccesses, 0);
84}
85
Davide Pesavento13260512024-02-01 21:16:04 -050086BOOST_AUTO_TEST_CASE(Ipv4,
87 * ut::precondition(NetworkConfigurationDetector::hasIpv4))
Junxiao Shi25467942017-06-30 02:53:14 +000088{
Junxiao Shi25467942017-06-30 02:53:14 +000089 asyncResolve("192.0.2.1",
Davide Pesavento2f46d652023-11-09 23:40:01 -050090 std::bind(&DnsFixture::onSuccess, this, _1, ip::make_address_v4("192.0.2.1"), true, true),
Davide Pesavento2e481fc2021-07-02 18:20:03 -040091 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -050092 m_ioCtx);
Junxiao Shi25467942017-06-30 02:53:14 +000093
Davide Pesavento2f46d652023-11-09 23:40:01 -050094 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +000095 BOOST_CHECK_EQUAL(m_nFailures, 0);
96 BOOST_CHECK_EQUAL(m_nSuccesses, 1);
97}
98
Davide Pesavento13260512024-02-01 21:16:04 -050099BOOST_AUTO_TEST_CASE(Ipv6,
100 * ut::precondition(NetworkConfigurationDetector::hasIpv6))
Junxiao Shi25467942017-06-30 02:53:14 +0000101{
Junxiao Shi25467942017-06-30 02:53:14 +0000102 asyncResolve("ipv6.google.com", // only IPv6 address should be available
Davide Pesavento2f46d652023-11-09 23:40:01 -0500103 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400104 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500105 m_ioCtx);
Junxiao Shi25467942017-06-30 02:53:14 +0000106
107 asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3",
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400108 std::bind(&DnsFixture::onSuccess, this, _1,
Davide Pesavento2f46d652023-11-09 23:40:01 -0500109 ip::make_address_v6("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"), true, true),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400110 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500111 m_ioCtx);
Junxiao Shi25467942017-06-30 02:53:14 +0000112
Davide Pesavento2f46d652023-11-09 23:40:01 -0500113 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +0000114 BOOST_CHECK_EQUAL(m_nFailures, 0);
115 BOOST_CHECK_EQUAL(m_nSuccesses, 2);
116}
117
Davide Pesavento13260512024-02-01 21:16:04 -0500118BOOST_AUTO_TEST_CASE(WithAddressSelector,
119 * ut::precondition(NetworkConfigurationDetector::hasIpv4)
120 * ut::precondition(NetworkConfigurationDetector::hasIpv6))
Junxiao Shi25467942017-06-30 02:53:14 +0000121{
Davide Pesavento2d7c5852022-07-18 16:02:52 -0400122 asyncResolve("named-data.net",
Davide Pesavento2f46d652023-11-09 23:40:01 -0500123 std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400124 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500125 m_ioCtx, Ipv4Only());
Junxiao Shi25467942017-06-30 02:53:14 +0000126
127 asyncResolve("a.root-servers.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()); // request IPv4 address
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_v6(), true, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400134 [this] (auto&&...) { onFailure(false); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500135 m_ioCtx, Ipv6Only()); // request IPv6 address
Junxiao Shi25467942017-06-30 02:53:14 +0000136
137 asyncResolve("ipv6.google.com", // only IPv6 address should be available
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());
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(), false, false),
Davide Pesavento2e481fc2021-07-02 18:20:03 -0400144 [this] (auto&&...) { onFailure(true); },
Davide Pesavento2f46d652023-11-09 23:40:01 -0500145 m_ioCtx, Ipv4Only()); // should fail
Junxiao Shi25467942017-06-30 02:53:14 +0000146
Davide Pesavento2f46d652023-11-09 23:40:01 -0500147 m_ioCtx.run();
Junxiao Shi25467942017-06-30 02:53:14 +0000148 BOOST_CHECK_EQUAL(m_nFailures, 1);
149 BOOST_CHECK_EQUAL(m_nSuccesses, 4);
150}
151
Junxiao Shi25467942017-06-30 02:53:14 +0000152BOOST_AUTO_TEST_SUITE_END() // TestDns
153BOOST_AUTO_TEST_SUITE_END() // Net
154
Davide Pesavento47ce2ee2023-05-09 01:33:33 -0400155} // namespace ndn::tests