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