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