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 | |
| 27 | #include <boost/asio/io_service.hpp> |
| 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; |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 32 | using boost::asio::ip::address_v4; |
| 33 | using boost::asio::ip::address_v6; |
| 34 | |
| 35 | class DnsFixture |
| 36 | { |
| 37 | public: |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 38 | void |
| 39 | onSuccess(const IpAddress& resolvedAddress, |
| 40 | const IpAddress& expectedAddress, |
| 41 | bool isValid, |
| 42 | bool shouldCheckAddress = false) |
| 43 | { |
| 44 | ++m_nSuccesses; |
| 45 | |
| 46 | if (!isValid) { |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 47 | BOOST_ERROR("Resolved to " + resolvedAddress.to_string() + ", but should have failed"); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 48 | } |
| 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 Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 60 | onFailure(bool shouldFail) |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 61 | { |
| 62 | ++m_nFailures; |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 63 | BOOST_CHECK_MESSAGE(shouldFail, "Resolution should not have failed"); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 64 | } |
| 65 | |
| 66 | protected: |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 67 | int m_nFailures = 0; |
| 68 | int m_nSuccesses = 0; |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 69 | boost::asio::io_service m_ioService; |
| 70 | }; |
| 71 | |
| 72 | BOOST_AUTO_TEST_SUITE(Net) |
| 73 | BOOST_FIXTURE_TEST_SUITE(TestDns, DnsFixture) |
| 74 | |
| 75 | BOOST_AUTO_TEST_CASE(Asynchronous) |
| 76 | { |
| 77 | SKIP_IF_IP_UNAVAILABLE(); |
| 78 | |
| 79 | asyncResolve("nothost.nothost.nothost.arpa", |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 80 | std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), false, false), |
| 81 | [this] (auto&&...) { onFailure(true); }, |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 82 | m_ioService); // should fail |
| 83 | |
| 84 | m_ioService.run(); |
| 85 | BOOST_CHECK_EQUAL(m_nFailures, 1); |
| 86 | BOOST_CHECK_EQUAL(m_nSuccesses, 0); |
| 87 | } |
| 88 | |
| 89 | BOOST_AUTO_TEST_CASE(AsynchronousV4) |
| 90 | { |
| 91 | SKIP_IF_IPV4_UNAVAILABLE(); |
| 92 | |
| 93 | asyncResolve("192.0.2.1", |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 94 | std::bind(&DnsFixture::onSuccess, this, _1, |
| 95 | IpAddress(address_v4::from_string("192.0.2.1")), true, true), |
| 96 | [this] (auto&&...) { onFailure(false); }, |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 97 | m_ioService); |
| 98 | |
| 99 | m_ioService.run(); |
| 100 | BOOST_CHECK_EQUAL(m_nFailures, 0); |
| 101 | BOOST_CHECK_EQUAL(m_nSuccesses, 1); |
| 102 | } |
| 103 | |
| 104 | BOOST_AUTO_TEST_CASE(AsynchronousV6) |
| 105 | { |
| 106 | SKIP_IF_IPV6_UNAVAILABLE(); |
| 107 | |
| 108 | asyncResolve("ipv6.google.com", // only IPv6 address should be available |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 109 | std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false), |
| 110 | [this] (auto&&...) { onFailure(false); }, |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 111 | m_ioService); |
| 112 | |
| 113 | asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 114 | std::bind(&DnsFixture::onSuccess, this, _1, |
| 115 | IpAddress(address_v6::from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")), |
| 116 | true, true), |
| 117 | [this] (auto&&...) { onFailure(false); }, |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 118 | m_ioService); |
| 119 | |
| 120 | m_ioService.run(); |
| 121 | BOOST_CHECK_EQUAL(m_nFailures, 0); |
| 122 | BOOST_CHECK_EQUAL(m_nSuccesses, 2); |
| 123 | } |
| 124 | |
| 125 | BOOST_AUTO_TEST_CASE(AsynchronousV4AndV6) |
| 126 | { |
| 127 | SKIP_IF_IPV4_UNAVAILABLE(); |
| 128 | SKIP_IF_IPV6_UNAVAILABLE(); |
| 129 | |
Davide Pesavento | 2d7c585 | 2022-07-18 16:02:52 -0400 | [diff] [blame] | 130 | asyncResolve("named-data.net", |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 131 | std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), true, false), |
| 132 | [this] (auto&&...) { onFailure(false); }, |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 133 | m_ioService, Ipv4Only()); |
| 134 | |
| 135 | asyncResolve("a.root-servers.net", |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 136 | std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v4()), true, false), |
| 137 | [this] (auto&&...) { onFailure(false); }, |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 138 | m_ioService, Ipv4Only()); // request IPv4 address |
| 139 | |
| 140 | asyncResolve("a.root-servers.net", |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 141 | std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false), |
| 142 | [this] (auto&&...) { onFailure(false); }, |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 143 | m_ioService, Ipv6Only()); // request IPv6 address |
| 144 | |
| 145 | asyncResolve("ipv6.google.com", // only IPv6 address should be available |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 146 | std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), true, false), |
| 147 | [this] (auto&&...) { onFailure(false); }, |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 148 | m_ioService, Ipv6Only()); |
| 149 | |
| 150 | asyncResolve("ipv6.google.com", // only IPv6 address should be available |
Davide Pesavento | 2e481fc | 2021-07-02 18:20:03 -0400 | [diff] [blame] | 151 | std::bind(&DnsFixture::onSuccess, this, _1, IpAddress(address_v6()), false, false), |
| 152 | [this] (auto&&...) { onFailure(true); }, |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 153 | m_ioService, Ipv4Only()); // should fail |
| 154 | |
| 155 | m_ioService.run(); |
| 156 | BOOST_CHECK_EQUAL(m_nFailures, 1); |
| 157 | BOOST_CHECK_EQUAL(m_nSuccesses, 4); |
| 158 | } |
| 159 | |
| 160 | BOOST_AUTO_TEST_CASE(Synchronous) |
| 161 | { |
| 162 | SKIP_IF_IP_UNAVAILABLE(); |
| 163 | |
Davide Pesavento | 2d7c585 | 2022-07-18 16:02:52 -0400 | [diff] [blame] | 164 | IpAddress address = syncResolve("named-data.net", m_ioService); |
Junxiao Shi | 2546794 | 2017-06-30 02:53:14 +0000 | [diff] [blame] | 165 | BOOST_CHECK(address.is_v4() || address.is_v6()); |
| 166 | } |
| 167 | |
| 168 | BOOST_AUTO_TEST_SUITE_END() // TestDns |
| 169 | BOOST_AUTO_TEST_SUITE_END() // Net |
| 170 | |
Davide Pesavento | 47ce2ee | 2023-05-09 01:33:33 -0400 | [diff] [blame^] | 171 | } // namespace ndn::tests |