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