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