Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
Davide Pesavento | 74daf74 | 2018-11-23 18:14:13 -0500 | [diff] [blame] | 2 | /* |
Davide Pesavento | 3de7c52 | 2024-01-25 18:50:12 -0500 | [diff] [blame] | 3 | * Copyright (c) 2013-2024 Regents of the University of California. |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 4 | * |
| 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 Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 22 | #include "ndn-cxx/net/dns.hpp" |
Davide Pesavento | 3de7c52 | 2024-01-25 18:50:12 -0500 | [diff] [blame] | 23 | #include "ndn-cxx/detail/common.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 24 | |
Davide Pesavento | 7e78064 | 2018-11-24 15:51:34 -0500 | [diff] [blame] | 25 | #include "tests/boost-test.hpp" |
| 26 | #include "tests/unit/net/network-configuration-detector.hpp" |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 27 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 28 | #include <boost/asio/io_context.hpp> |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 29 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 30 | namespace ndn::tests { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 31 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 32 | using namespace ndn::dns; |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 33 | namespace ip = boost::asio::ip; |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 34 | |
| 35 | class DnsFixture |
| 36 | { |
| 37 | public: |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 38 | void |
Davide Pesavento | 1326051 | 2024-02-01 21:16:04 -0500 | [diff] [blame^] | 39 | onSuccess(const ip::address& resolvedAddress, const ip::address& expectedAddress, |
| 40 | bool isValid, bool shouldCheckAddress = false) |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 41 | { |
| 42 | ++m_nSuccesses; |
| 43 | |
| 44 | if (!isValid) { |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 45 | BOOST_ERROR("Resolved to " + resolvedAddress.to_string() + ", but should have failed"); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 46 | } |
| 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 Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 58 | onFailure(bool shouldFail) |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 59 | { |
| 60 | ++m_nFailures; |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 61 | BOOST_CHECK_MESSAGE(shouldFail, "Resolution should not have failed"); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | protected: |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 65 | int m_nFailures = 0; |
| 66 | int m_nSuccesses = 0; |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 67 | boost::asio::io_context m_ioCtx; |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 68 | }; |
| 69 | |
| 70 | BOOST_AUTO_TEST_SUITE(Net) |
| 71 | BOOST_FIXTURE_TEST_SUITE(TestDns, DnsFixture) |
| 72 | |
Davide Pesavento | 1326051 | 2024-02-01 21:16:04 -0500 | [diff] [blame^] | 73 | BOOST_AUTO_TEST_CASE(Failure, |
| 74 | * ut::precondition(NetworkConfigurationDetector::hasIpv4OrIpv6)) |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 75 | { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 76 | asyncResolve("nothost.nothost.nothost.arpa", |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 77 | std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), false, false), |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 78 | [this] (auto&&...) { onFailure(true); }, |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 79 | m_ioCtx); // should fail |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 80 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 81 | m_ioCtx.run(); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 82 | BOOST_CHECK_EQUAL(m_nFailures, 1); |
| 83 | BOOST_CHECK_EQUAL(m_nSuccesses, 0); |
| 84 | } |
| 85 | |
Davide Pesavento | 1326051 | 2024-02-01 21:16:04 -0500 | [diff] [blame^] | 86 | BOOST_AUTO_TEST_CASE(Ipv4, |
| 87 | * ut::precondition(NetworkConfigurationDetector::hasIpv4)) |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 88 | { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 89 | asyncResolve("192.0.2.1", |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 90 | std::bind(&DnsFixture::onSuccess, this, _1, ip::make_address_v4("192.0.2.1"), true, true), |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 91 | [this] (auto&&...) { onFailure(false); }, |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 92 | m_ioCtx); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 93 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 94 | m_ioCtx.run(); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 95 | BOOST_CHECK_EQUAL(m_nFailures, 0); |
| 96 | BOOST_CHECK_EQUAL(m_nSuccesses, 1); |
| 97 | } |
| 98 | |
Davide Pesavento | 1326051 | 2024-02-01 21:16:04 -0500 | [diff] [blame^] | 99 | BOOST_AUTO_TEST_CASE(Ipv6, |
| 100 | * ut::precondition(NetworkConfigurationDetector::hasIpv6)) |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 101 | { |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 102 | asyncResolve("ipv6.google.com", // only IPv6 address should be available |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 103 | std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), true, false), |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 104 | [this] (auto&&...) { onFailure(false); }, |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 105 | m_ioCtx); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 106 | |
| 107 | asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 108 | std::bind(&DnsFixture::onSuccess, this, _1, |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 109 | ip::make_address_v6("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"), true, true), |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 110 | [this] (auto&&...) { onFailure(false); }, |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 111 | m_ioCtx); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 112 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 113 | m_ioCtx.run(); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 114 | BOOST_CHECK_EQUAL(m_nFailures, 0); |
| 115 | BOOST_CHECK_EQUAL(m_nSuccesses, 2); |
| 116 | } |
| 117 | |
Davide Pesavento | 1326051 | 2024-02-01 21:16:04 -0500 | [diff] [blame^] | 118 | BOOST_AUTO_TEST_CASE(WithAddressSelector, |
| 119 | * ut::precondition(NetworkConfigurationDetector::hasIpv4) |
| 120 | * ut::precondition(NetworkConfigurationDetector::hasIpv6)) |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 121 | { |
Davide Pesavento | 2d7c585 | 2022-07-18 16:02:52 -0400 | [diff] [blame] | 122 | asyncResolve("named-data.net", |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 123 | std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), true, false), |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 124 | [this] (auto&&...) { onFailure(false); }, |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 125 | m_ioCtx, Ipv4Only()); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 126 | |
| 127 | asyncResolve("a.root-servers.net", |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 128 | std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v4(), true, false), |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 129 | [this] (auto&&...) { onFailure(false); }, |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 130 | m_ioCtx, Ipv4Only()); // request IPv4 address |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 131 | |
| 132 | asyncResolve("a.root-servers.net", |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 133 | std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), true, false), |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 134 | [this] (auto&&...) { onFailure(false); }, |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 135 | m_ioCtx, Ipv6Only()); // request IPv6 address |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 136 | |
| 137 | asyncResolve("ipv6.google.com", // only IPv6 address should be available |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 138 | std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), true, false), |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 139 | [this] (auto&&...) { onFailure(false); }, |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 140 | m_ioCtx, Ipv6Only()); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 141 | |
| 142 | asyncResolve("ipv6.google.com", // only IPv6 address should be available |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 143 | std::bind(&DnsFixture::onSuccess, this, _1, ip::address_v6(), false, false), |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 144 | [this] (auto&&...) { onFailure(true); }, |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 145 | m_ioCtx, Ipv4Only()); // should fail |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 146 | |
Davide Pesavento | 2f46d65 | 2023-11-09 23:40:01 -0500 | [diff] [blame] | 147 | m_ioCtx.run(); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 148 | BOOST_CHECK_EQUAL(m_nFailures, 1); |
| 149 | BOOST_CHECK_EQUAL(m_nSuccesses, 4); |
| 150 | } |
| 151 | |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 152 | BOOST_AUTO_TEST_SUITE_END() // TestDns |
| 153 | BOOST_AUTO_TEST_SUITE_END() // Net |
| 154 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame] | 155 | } // namespace ndn::tests |