Alexander Afanasyev | 452e282 | 2014-02-27 14:48:04 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
Alexander Afanasyev | 9bcbc7c | 2014-04-06 19:37:37 -0700 | [diff] [blame] | 3 | * Copyright (c) 2014 Regents of the University of California, |
| 4 | * Arizona Board of Regents, |
| 5 | * Colorado State University, |
| 6 | * University Pierre & Marie Curie, Sorbonne University, |
| 7 | * Washington University in St. Louis, |
| 8 | * Beijing Institute of Technology |
| 9 | * |
| 10 | * This file is part of NFD (Named Data Networking Forwarding Daemon). |
| 11 | * See AUTHORS.md for complete list of NFD authors and contributors. |
| 12 | * |
| 13 | * NFD is free software: you can redistribute it and/or modify it under the terms |
| 14 | * of the GNU General Public License as published by the Free Software Foundation, |
| 15 | * either version 3 of the License, or (at your option) any later version. |
| 16 | * |
| 17 | * NFD is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; |
| 18 | * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR |
| 19 | * PURPOSE. See the GNU General Public License for more details. |
| 20 | * |
| 21 | * You should have received a copy of the GNU General Public License along with |
| 22 | * NFD, e.g., in COPYING.md file. If not, see <http://www.gnu.org/licenses/>. |
| 23 | **/ |
Alexander Afanasyev | 452e282 | 2014-02-27 14:48:04 -0800 | [diff] [blame] | 24 | |
| 25 | #include "core/resolver.hpp" |
| 26 | #include "core/logger.hpp" |
| 27 | #include <boost/test/unit_test.hpp> |
| 28 | |
| 29 | #include "tests/test-common.hpp" |
| 30 | |
| 31 | namespace nfd { |
| 32 | namespace tests { |
| 33 | |
| 34 | NFD_LOG_INIT("tests.CoreResolver"); |
| 35 | |
| 36 | using boost::asio::ip::address_v4; |
| 37 | using boost::asio::ip::address_v6; |
| 38 | using boost::asio::ip::tcp; |
| 39 | using boost::asio::ip::udp; |
| 40 | |
| 41 | BOOST_FIXTURE_TEST_SUITE(CoreResolver, BaseFixture) |
| 42 | |
| 43 | template<class Protocol> |
| 44 | class ResolverFixture : protected BaseFixture |
| 45 | { |
| 46 | public: |
| 47 | ResolverFixture() |
| 48 | : m_nFailures(0) |
| 49 | , m_nSuccesses(0) |
| 50 | { |
| 51 | } |
| 52 | |
| 53 | void |
| 54 | onSuccess(const typename Protocol::endpoint& resolvedEndpoint, |
| 55 | const typename Protocol::endpoint& expectedEndpoint, |
| 56 | bool isValid, bool wantCheckAddress = false) |
| 57 | { |
| 58 | NFD_LOG_DEBUG("Resolved to: " << resolvedEndpoint); |
| 59 | ++m_nSuccesses; |
| 60 | |
| 61 | if (!isValid) |
| 62 | { |
| 63 | BOOST_FAIL("Resolved to " + boost::lexical_cast<std::string>(resolvedEndpoint) |
| 64 | + ", but it should have failed"); |
| 65 | } |
| 66 | |
| 67 | BOOST_CHECK_EQUAL(resolvedEndpoint.port(), expectedEndpoint.port()); |
| 68 | |
| 69 | BOOST_CHECK_EQUAL(resolvedEndpoint.address().is_v4(), expectedEndpoint.address().is_v4()); |
| 70 | |
| 71 | // checking address is not deterministic and should be enabled only |
| 72 | // if only one IP address will be returned by resolution |
| 73 | if (wantCheckAddress) |
| 74 | { |
| 75 | BOOST_CHECK_EQUAL(resolvedEndpoint.address(), expectedEndpoint.address()); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | void |
| 80 | onFailure(bool isValid) |
| 81 | { |
| 82 | ++m_nFailures; |
| 83 | |
| 84 | if (!isValid) |
| 85 | BOOST_FAIL("Resolution should not have failed"); |
| 86 | |
| 87 | BOOST_CHECK_MESSAGE(true, "Resolution failed as expected"); |
| 88 | } |
| 89 | |
| 90 | |
| 91 | uint32_t m_nFailures; |
| 92 | uint32_t m_nSuccesses; |
| 93 | }; |
| 94 | |
| 95 | BOOST_FIXTURE_TEST_CASE(Tcp, ResolverFixture<tcp>) |
| 96 | { |
| 97 | TcpResolver::asyncResolve("www.named-data.net", "6363", |
| 98 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 99 | tcp::endpoint(address_v4(), 6363), true, false), |
| 100 | bind(&ResolverFixture<tcp>::onFailure, this, false)); |
| 101 | |
| 102 | TcpResolver::asyncResolve("www.named-data.net", "notport", |
| 103 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 104 | tcp::endpoint(address_v4(), 0), false, false), |
| 105 | bind(&ResolverFixture<tcp>::onFailure, this, true)); // should fail |
| 106 | |
| 107 | |
| 108 | TcpResolver::asyncResolve("nothost.nothost.nothost.arpa", "6363", |
| 109 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 110 | tcp::endpoint(address_v4(), 6363), false, false), |
| 111 | bind(&ResolverFixture<tcp>::onFailure, this, true)); // should fail |
| 112 | |
| 113 | TcpResolver::asyncResolve("www.google.com", "80", |
| 114 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 115 | tcp::endpoint(address_v4(), 80), true, false), |
| 116 | bind(&ResolverFixture<tcp>::onFailure, this, false), |
| 117 | resolver::Ipv4Address()); // request IPv4 address |
| 118 | |
| 119 | TcpResolver::asyncResolve("www.google.com", "80", |
| 120 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 121 | tcp::endpoint(address_v6(), 80), true, false), |
| 122 | bind(&ResolverFixture<tcp>::onFailure, this, false), |
| 123 | resolver::Ipv6Address()); // request IPv6 address |
| 124 | |
| 125 | TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 126 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 127 | tcp::endpoint(address_v6(), 80), true, false), |
| 128 | bind(&ResolverFixture<tcp>::onFailure, this, false)); |
| 129 | |
| 130 | TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 131 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 132 | tcp::endpoint(address_v6(), 80), true, false), |
| 133 | bind(&ResolverFixture<tcp>::onFailure, this, false), |
| 134 | resolver::Ipv6Address()); |
| 135 | |
| 136 | TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 137 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 138 | tcp::endpoint(address_v6(), 80), false, false), |
| 139 | bind(&ResolverFixture<tcp>::onFailure, this, true), // should fail |
| 140 | resolver::Ipv4Address()); |
| 141 | |
| 142 | TcpResolver::asyncResolve("192.0.2.1", "80", |
| 143 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 144 | tcp::endpoint(address_v4::from_string("192.0.2.1"), 80), true, true), |
| 145 | bind(&ResolverFixture<tcp>::onFailure, this, false)); |
| 146 | |
| 147 | TcpResolver::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", "80", |
| 148 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 149 | tcp::endpoint(address_v6:: |
| 150 | from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"), |
| 151 | 80), true, true), |
| 152 | bind(&ResolverFixture<tcp>::onFailure, this, false)); |
| 153 | |
| 154 | g_io.run(); |
| 155 | |
| 156 | BOOST_CHECK_EQUAL(m_nFailures, 3); |
| 157 | BOOST_CHECK_EQUAL(m_nSuccesses, 7); |
| 158 | } |
| 159 | |
| 160 | BOOST_AUTO_TEST_CASE(SyncTcp) |
| 161 | { |
| 162 | tcp::endpoint endpoint; |
| 163 | BOOST_CHECK_NO_THROW(endpoint = TcpResolver::syncResolve("www.named-data.net", "6363")); |
| 164 | NFD_LOG_DEBUG("Resolved to: " << endpoint); |
| 165 | BOOST_CHECK_EQUAL(endpoint.address().is_v4(), true); |
| 166 | BOOST_CHECK_EQUAL(endpoint.port(), 6363); |
| 167 | } |
| 168 | |
| 169 | BOOST_FIXTURE_TEST_CASE(Udp, ResolverFixture<udp>) |
| 170 | { |
| 171 | UdpResolver::asyncResolve("www.named-data.net", "6363", |
| 172 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 173 | udp::endpoint(address_v4(), 6363), true, false), |
| 174 | bind(&ResolverFixture<udp>::onFailure, this, false)); |
| 175 | |
| 176 | UdpResolver::asyncResolve("www.named-data.net", "notport", |
| 177 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 178 | udp::endpoint(address_v4(), 0), false, false), |
| 179 | bind(&ResolverFixture<udp>::onFailure, this, true)); // should fail |
| 180 | |
| 181 | |
| 182 | UdpResolver::asyncResolve("nothost.nothost.nothost.arpa", "6363", |
| 183 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 184 | udp::endpoint(address_v4(), 6363), false, false), |
| 185 | bind(&ResolverFixture<udp>::onFailure, this, true)); // should fail |
| 186 | |
| 187 | UdpResolver::asyncResolve("www.google.com", "80", |
| 188 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 189 | udp::endpoint(address_v4(), 80), true, false), |
| 190 | bind(&ResolverFixture<udp>::onFailure, this, false), |
| 191 | resolver::Ipv4Address()); // request IPv4 address |
| 192 | |
| 193 | UdpResolver::asyncResolve("www.google.com", "80", |
| 194 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 195 | udp::endpoint(address_v6(), 80), true, false), |
| 196 | bind(&ResolverFixture<udp>::onFailure, this, false), |
| 197 | resolver::Ipv6Address()); // request IPv6 address |
| 198 | |
| 199 | UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 200 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 201 | udp::endpoint(address_v6(), 80), true, false), |
| 202 | bind(&ResolverFixture<udp>::onFailure, this, false)); |
| 203 | |
| 204 | UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 205 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 206 | udp::endpoint(address_v6(), 80), true, false), |
| 207 | bind(&ResolverFixture<udp>::onFailure, this, false), |
| 208 | resolver::Ipv6Address()); |
| 209 | |
| 210 | UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 211 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 212 | udp::endpoint(address_v6(), 80), false, false), |
| 213 | bind(&ResolverFixture<udp>::onFailure, this, true), // should fail |
| 214 | resolver::Ipv4Address()); |
| 215 | |
| 216 | UdpResolver::asyncResolve("192.0.2.1", "80", |
| 217 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 218 | udp::endpoint(address_v4::from_string("192.0.2.1"), 80), true, true), |
| 219 | bind(&ResolverFixture<udp>::onFailure, this, false)); |
| 220 | |
| 221 | UdpResolver::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", "80", |
| 222 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 223 | udp::endpoint(address_v6:: |
| 224 | from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"), |
| 225 | 80), true, true), |
| 226 | bind(&ResolverFixture<udp>::onFailure, this, false)); |
| 227 | |
| 228 | g_io.run(); |
| 229 | |
| 230 | BOOST_CHECK_EQUAL(m_nFailures, 3); |
| 231 | BOOST_CHECK_EQUAL(m_nSuccesses, 7); |
| 232 | } |
| 233 | |
| 234 | BOOST_AUTO_TEST_CASE(SyncUdp) |
| 235 | { |
| 236 | udp::endpoint endpoint; |
| 237 | BOOST_CHECK_NO_THROW(endpoint = UdpResolver::syncResolve("www.named-data.net", "6363")); |
| 238 | NFD_LOG_DEBUG("Resolved to: " << endpoint); |
| 239 | BOOST_CHECK_EQUAL(endpoint.address().is_v4(), true); |
| 240 | BOOST_CHECK_EQUAL(endpoint.port(), 6363); |
| 241 | } |
| 242 | |
| 243 | |
| 244 | BOOST_AUTO_TEST_SUITE_END() |
| 245 | |
| 246 | } // namespace tests |
| 247 | } // namespace nfd |