Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 3 | * Copyright (c) 2013-2016 Regents of the University of California. |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [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 | |
| 22 | #include "util/dns.hpp" |
| 23 | |
| 24 | #include "boost-test.hpp" |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 25 | #include "../network-configuration-detector.hpp" |
Davide Pesavento | 537dc3a | 2016-02-18 19:35:26 +0100 | [diff] [blame] | 26 | |
| 27 | #include <boost/asio.hpp> |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 28 | #include <boost/lexical_cast.hpp> |
| 29 | |
| 30 | namespace ndn { |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 31 | namespace util { |
| 32 | namespace tests { |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 33 | |
| 34 | using boost::asio::ip::address_v4; |
| 35 | using boost::asio::ip::address_v6; |
| 36 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 37 | using ndn::tests::NetworkConfigurationDetector; |
| 38 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 39 | class DnsFixture |
| 40 | { |
| 41 | public: |
| 42 | DnsFixture() |
| 43 | : m_nFailures(0) |
| 44 | , m_nSuccesses(0) |
| 45 | { |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | onSuccess(const dns::IpAddress& resolvedAddress, |
| 50 | const dns::IpAddress& expectedAddress, |
| 51 | bool isValid, |
| 52 | bool shouldCheckAddress = false) |
| 53 | { |
Alexander Afanasyev | 3ccc6f7 | 2014-10-12 12:19:09 -0700 | [diff] [blame] | 54 | BOOST_TEST_MESSAGE("Resolved to: " << resolvedAddress); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 55 | |
| 56 | ++m_nSuccesses; |
| 57 | |
| 58 | if (!isValid) |
| 59 | { |
| 60 | BOOST_FAIL("Resolved to " + boost::lexical_cast<std::string>(resolvedAddress) |
| 61 | + ", but should have failed"); |
| 62 | } |
| 63 | |
| 64 | BOOST_CHECK_EQUAL(resolvedAddress.is_v4(), expectedAddress.is_v4()); |
| 65 | |
| 66 | // checking address is not deterministic and should be enabled only |
| 67 | // if only one IP address will be returned by resolution |
| 68 | if (shouldCheckAddress) |
| 69 | { |
| 70 | BOOST_CHECK_EQUAL(resolvedAddress, expectedAddress); |
| 71 | } |
| 72 | } |
| 73 | |
| 74 | void |
| 75 | onFailure(bool isValid) |
| 76 | { |
| 77 | ++m_nFailures; |
| 78 | |
| 79 | if (!isValid) |
| 80 | { |
| 81 | BOOST_FAIL("Resolution should not have failed"); |
| 82 | } |
| 83 | |
| 84 | BOOST_CHECK_MESSAGE(true, "Resolution failed as expected"); |
| 85 | } |
| 86 | |
| 87 | public: |
| 88 | uint32_t m_nFailures; |
| 89 | uint32_t m_nSuccesses; |
| 90 | |
| 91 | boost::asio::io_service m_ioService; |
| 92 | }; |
| 93 | |
| 94 | BOOST_FIXTURE_TEST_SUITE(UtilDns, DnsFixture) |
| 95 | |
| 96 | BOOST_AUTO_TEST_CASE(Asynchronous) |
| 97 | { |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 98 | if (!NetworkConfigurationDetector::hasIpv4() && !NetworkConfigurationDetector::hasIpv6()) { |
| 99 | BOOST_TEST_MESSAGE("Platform does not support both IPv4 and IPv6, skipping test case"); |
| 100 | return; |
| 101 | } |
| 102 | |
| 103 | dns::asyncResolve("nothost.nothost.nothost.arpa", |
| 104 | bind(&DnsFixture::onSuccess, this, _1, |
| 105 | dns::IpAddress(address_v4()), false, false), |
| 106 | bind(&DnsFixture::onFailure, this, true), |
| 107 | m_ioService); // should fail |
| 108 | m_ioService.run(); |
| 109 | |
| 110 | BOOST_CHECK_EQUAL(m_nFailures, 1); |
| 111 | BOOST_CHECK_EQUAL(m_nSuccesses, 0); |
| 112 | } |
| 113 | |
| 114 | BOOST_AUTO_TEST_CASE(AsynchronousV4) |
| 115 | { |
| 116 | if (!NetworkConfigurationDetector::hasIpv4()) { |
| 117 | BOOST_TEST_MESSAGE("Platform does not support IPv4, skipping the test case"); |
| 118 | return; |
| 119 | } |
| 120 | |
| 121 | dns::asyncResolve("192.0.2.1", |
| 122 | bind(&DnsFixture::onSuccess, this, _1, |
| 123 | dns::IpAddress(address_v4::from_string("192.0.2.1")), |
| 124 | true, true), |
| 125 | bind(&DnsFixture::onFailure, this, false), |
| 126 | m_ioService); |
| 127 | m_ioService.run(); |
| 128 | |
| 129 | BOOST_CHECK_EQUAL(m_nFailures, 0); |
| 130 | BOOST_CHECK_EQUAL(m_nSuccesses, 1); |
| 131 | } |
| 132 | |
| 133 | BOOST_AUTO_TEST_CASE(AsynchronousV6) |
| 134 | { |
| 135 | if (!NetworkConfigurationDetector::hasIpv6()) { |
| 136 | BOOST_TEST_MESSAGE("Platform does not support IPv6, skipping the test case"); |
| 137 | return; |
| 138 | } |
| 139 | |
| 140 | dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available |
| 141 | bind(&DnsFixture::onSuccess, this, _1, |
| 142 | dns::IpAddress(address_v6()), true, false), |
| 143 | bind(&DnsFixture::onFailure, this, false), |
| 144 | m_ioService); |
| 145 | |
| 146 | dns::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", |
| 147 | bind(&DnsFixture::onSuccess, this, _1, |
| 148 | dns::IpAddress(address_v6:: |
| 149 | from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3")), |
| 150 | true, true), |
| 151 | bind(&DnsFixture::onFailure, this, false), |
| 152 | m_ioService); |
| 153 | m_ioService.run(); |
| 154 | |
| 155 | BOOST_CHECK_EQUAL(m_nFailures, 0); |
| 156 | BOOST_CHECK_EQUAL(m_nSuccesses, 2); |
| 157 | } |
| 158 | |
| 159 | BOOST_AUTO_TEST_CASE(AsynchronousV4AndV6) |
| 160 | { |
| 161 | if (!NetworkConfigurationDetector::hasIpv4() || !NetworkConfigurationDetector::hasIpv6()) { |
| 162 | BOOST_TEST_MESSAGE("Platform does not support either IPv4 or IPv6, skipping test case"); |
| 163 | return; |
| 164 | } |
| 165 | |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 166 | dns::asyncResolve("www.named-data.net", |
| 167 | bind(&DnsFixture::onSuccess, this, _1, |
| 168 | dns::IpAddress(address_v4()), true, false), |
| 169 | bind(&DnsFixture::onFailure, this, false), |
| 170 | m_ioService, |
| 171 | dns::Ipv4Only()); |
| 172 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 173 | dns::asyncResolve("a.root-servers.net", |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 174 | bind(&DnsFixture::onSuccess, this, _1, |
| 175 | dns::IpAddress(address_v4()), true, false), |
| 176 | bind(&DnsFixture::onFailure, this, false), |
| 177 | m_ioService, |
| 178 | dns::Ipv4Only()); // request IPv4 address |
| 179 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 180 | dns::asyncResolve("a.root-servers.net", |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 181 | bind(&DnsFixture::onSuccess, this, _1, |
| 182 | dns::IpAddress(address_v6()), true, false), |
| 183 | bind(&DnsFixture::onFailure, this, false), |
| 184 | m_ioService, |
| 185 | dns::Ipv6Only()); // request IPv6 address |
| 186 | |
| 187 | dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available |
| 188 | bind(&DnsFixture::onSuccess, this, _1, |
| 189 | dns::IpAddress(address_v6()), true, false), |
| 190 | bind(&DnsFixture::onFailure, this, false), |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 191 | m_ioService, |
| 192 | dns::Ipv6Only()); |
| 193 | |
| 194 | dns::asyncResolve("ipv6.google.com", // only IPv6 address should be available |
| 195 | bind(&DnsFixture::onSuccess, this, _1, |
| 196 | dns::IpAddress(address_v6()), false, false), |
| 197 | bind(&DnsFixture::onFailure, this, true), // should fail |
| 198 | m_ioService, |
| 199 | dns::Ipv4Only()); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 200 | m_ioService.run(); |
| 201 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 202 | BOOST_CHECK_EQUAL(m_nFailures, 1); |
| 203 | BOOST_CHECK_EQUAL(m_nSuccesses, 4); |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | BOOST_AUTO_TEST_CASE(Synchronous) |
| 207 | { |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 208 | if (!NetworkConfigurationDetector::hasIpv4() && !NetworkConfigurationDetector::hasIpv6()) { |
| 209 | BOOST_TEST_MESSAGE("Platform does not support both IPv4 and IPv6, skipping test case"); |
| 210 | return; |
| 211 | } |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 212 | dns::IpAddress address; |
| 213 | BOOST_CHECK_NO_THROW(address = dns::syncResolve("www.named-data.net", m_ioService)); |
| 214 | |
| 215 | BOOST_CHECK(address.is_v4() || address.is_v6()); |
| 216 | } |
| 217 | |
| 218 | BOOST_AUTO_TEST_SUITE_END() |
| 219 | |
Alexander Afanasyev | 1286e02 | 2015-01-26 10:42:29 -0800 | [diff] [blame] | 220 | } // namespace tests |
| 221 | } // namespace util |
Vince Lehman | 7a6bb35 | 2014-09-22 15:58:19 -0500 | [diff] [blame] | 222 | } // namespace ndn |