Alexander Afanasyev | 452e282 | 2014-02-27 14:48:04 -0800 | [diff] [blame] | 1 | /* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */ |
| 2 | /** |
| 3 | * Copyright (C) 2014 Named Data Networking Project |
| 4 | * See COPYING for copyright and distribution information. |
| 5 | */ |
| 6 | |
| 7 | #include "core/resolver.hpp" |
| 8 | #include "core/logger.hpp" |
| 9 | #include <boost/test/unit_test.hpp> |
| 10 | |
| 11 | #include "tests/test-common.hpp" |
| 12 | |
| 13 | namespace nfd { |
| 14 | namespace tests { |
| 15 | |
| 16 | NFD_LOG_INIT("tests.CoreResolver"); |
| 17 | |
| 18 | using boost::asio::ip::address_v4; |
| 19 | using boost::asio::ip::address_v6; |
| 20 | using boost::asio::ip::tcp; |
| 21 | using boost::asio::ip::udp; |
| 22 | |
| 23 | BOOST_FIXTURE_TEST_SUITE(CoreResolver, BaseFixture) |
| 24 | |
| 25 | template<class Protocol> |
| 26 | class ResolverFixture : protected BaseFixture |
| 27 | { |
| 28 | public: |
| 29 | ResolverFixture() |
| 30 | : m_nFailures(0) |
| 31 | , m_nSuccesses(0) |
| 32 | { |
| 33 | } |
| 34 | |
| 35 | void |
| 36 | onSuccess(const typename Protocol::endpoint& resolvedEndpoint, |
| 37 | const typename Protocol::endpoint& expectedEndpoint, |
| 38 | bool isValid, bool wantCheckAddress = false) |
| 39 | { |
| 40 | NFD_LOG_DEBUG("Resolved to: " << resolvedEndpoint); |
| 41 | ++m_nSuccesses; |
| 42 | |
| 43 | if (!isValid) |
| 44 | { |
| 45 | BOOST_FAIL("Resolved to " + boost::lexical_cast<std::string>(resolvedEndpoint) |
| 46 | + ", but it should have failed"); |
| 47 | } |
| 48 | |
| 49 | BOOST_CHECK_EQUAL(resolvedEndpoint.port(), expectedEndpoint.port()); |
| 50 | |
| 51 | BOOST_CHECK_EQUAL(resolvedEndpoint.address().is_v4(), expectedEndpoint.address().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 (wantCheckAddress) |
| 56 | { |
| 57 | BOOST_CHECK_EQUAL(resolvedEndpoint.address(), expectedEndpoint.address()); |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | void |
| 62 | onFailure(bool isValid) |
| 63 | { |
| 64 | ++m_nFailures; |
| 65 | |
| 66 | if (!isValid) |
| 67 | BOOST_FAIL("Resolution should not have failed"); |
| 68 | |
| 69 | BOOST_CHECK_MESSAGE(true, "Resolution failed as expected"); |
| 70 | } |
| 71 | |
| 72 | |
| 73 | uint32_t m_nFailures; |
| 74 | uint32_t m_nSuccesses; |
| 75 | }; |
| 76 | |
| 77 | BOOST_FIXTURE_TEST_CASE(Tcp, ResolverFixture<tcp>) |
| 78 | { |
| 79 | TcpResolver::asyncResolve("www.named-data.net", "6363", |
| 80 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 81 | tcp::endpoint(address_v4(), 6363), true, false), |
| 82 | bind(&ResolverFixture<tcp>::onFailure, this, false)); |
| 83 | |
| 84 | TcpResolver::asyncResolve("www.named-data.net", "notport", |
| 85 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 86 | tcp::endpoint(address_v4(), 0), false, false), |
| 87 | bind(&ResolverFixture<tcp>::onFailure, this, true)); // should fail |
| 88 | |
| 89 | |
| 90 | TcpResolver::asyncResolve("nothost.nothost.nothost.arpa", "6363", |
| 91 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 92 | tcp::endpoint(address_v4(), 6363), false, false), |
| 93 | bind(&ResolverFixture<tcp>::onFailure, this, true)); // should fail |
| 94 | |
| 95 | TcpResolver::asyncResolve("www.google.com", "80", |
| 96 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 97 | tcp::endpoint(address_v4(), 80), true, false), |
| 98 | bind(&ResolverFixture<tcp>::onFailure, this, false), |
| 99 | resolver::Ipv4Address()); // request IPv4 address |
| 100 | |
| 101 | TcpResolver::asyncResolve("www.google.com", "80", |
| 102 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 103 | tcp::endpoint(address_v6(), 80), true, false), |
| 104 | bind(&ResolverFixture<tcp>::onFailure, this, false), |
| 105 | resolver::Ipv6Address()); // request IPv6 address |
| 106 | |
| 107 | TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 108 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 109 | tcp::endpoint(address_v6(), 80), true, false), |
| 110 | bind(&ResolverFixture<tcp>::onFailure, this, false)); |
| 111 | |
| 112 | TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 113 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 114 | tcp::endpoint(address_v6(), 80), true, false), |
| 115 | bind(&ResolverFixture<tcp>::onFailure, this, false), |
| 116 | resolver::Ipv6Address()); |
| 117 | |
| 118 | TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 119 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 120 | tcp::endpoint(address_v6(), 80), false, false), |
| 121 | bind(&ResolverFixture<tcp>::onFailure, this, true), // should fail |
| 122 | resolver::Ipv4Address()); |
| 123 | |
| 124 | TcpResolver::asyncResolve("192.0.2.1", "80", |
| 125 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 126 | tcp::endpoint(address_v4::from_string("192.0.2.1"), 80), true, true), |
| 127 | bind(&ResolverFixture<tcp>::onFailure, this, false)); |
| 128 | |
| 129 | TcpResolver::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", "80", |
| 130 | bind(&ResolverFixture<tcp>::onSuccess, this, _1, |
| 131 | tcp::endpoint(address_v6:: |
| 132 | from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"), |
| 133 | 80), true, true), |
| 134 | bind(&ResolverFixture<tcp>::onFailure, this, false)); |
| 135 | |
| 136 | g_io.run(); |
| 137 | |
| 138 | BOOST_CHECK_EQUAL(m_nFailures, 3); |
| 139 | BOOST_CHECK_EQUAL(m_nSuccesses, 7); |
| 140 | } |
| 141 | |
| 142 | BOOST_AUTO_TEST_CASE(SyncTcp) |
| 143 | { |
| 144 | tcp::endpoint endpoint; |
| 145 | BOOST_CHECK_NO_THROW(endpoint = TcpResolver::syncResolve("www.named-data.net", "6363")); |
| 146 | NFD_LOG_DEBUG("Resolved to: " << endpoint); |
| 147 | BOOST_CHECK_EQUAL(endpoint.address().is_v4(), true); |
| 148 | BOOST_CHECK_EQUAL(endpoint.port(), 6363); |
| 149 | } |
| 150 | |
| 151 | BOOST_FIXTURE_TEST_CASE(Udp, ResolverFixture<udp>) |
| 152 | { |
| 153 | UdpResolver::asyncResolve("www.named-data.net", "6363", |
| 154 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 155 | udp::endpoint(address_v4(), 6363), true, false), |
| 156 | bind(&ResolverFixture<udp>::onFailure, this, false)); |
| 157 | |
| 158 | UdpResolver::asyncResolve("www.named-data.net", "notport", |
| 159 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 160 | udp::endpoint(address_v4(), 0), false, false), |
| 161 | bind(&ResolverFixture<udp>::onFailure, this, true)); // should fail |
| 162 | |
| 163 | |
| 164 | UdpResolver::asyncResolve("nothost.nothost.nothost.arpa", "6363", |
| 165 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 166 | udp::endpoint(address_v4(), 6363), false, false), |
| 167 | bind(&ResolverFixture<udp>::onFailure, this, true)); // should fail |
| 168 | |
| 169 | UdpResolver::asyncResolve("www.google.com", "80", |
| 170 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 171 | udp::endpoint(address_v4(), 80), true, false), |
| 172 | bind(&ResolverFixture<udp>::onFailure, this, false), |
| 173 | resolver::Ipv4Address()); // request IPv4 address |
| 174 | |
| 175 | UdpResolver::asyncResolve("www.google.com", "80", |
| 176 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 177 | udp::endpoint(address_v6(), 80), true, false), |
| 178 | bind(&ResolverFixture<udp>::onFailure, this, false), |
| 179 | resolver::Ipv6Address()); // request IPv6 address |
| 180 | |
| 181 | UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 182 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 183 | udp::endpoint(address_v6(), 80), true, false), |
| 184 | bind(&ResolverFixture<udp>::onFailure, this, false)); |
| 185 | |
| 186 | UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 187 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 188 | udp::endpoint(address_v6(), 80), true, false), |
| 189 | bind(&ResolverFixture<udp>::onFailure, this, false), |
| 190 | resolver::Ipv6Address()); |
| 191 | |
| 192 | UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available |
| 193 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 194 | udp::endpoint(address_v6(), 80), false, false), |
| 195 | bind(&ResolverFixture<udp>::onFailure, this, true), // should fail |
| 196 | resolver::Ipv4Address()); |
| 197 | |
| 198 | UdpResolver::asyncResolve("192.0.2.1", "80", |
| 199 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 200 | udp::endpoint(address_v4::from_string("192.0.2.1"), 80), true, true), |
| 201 | bind(&ResolverFixture<udp>::onFailure, this, false)); |
| 202 | |
| 203 | UdpResolver::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", "80", |
| 204 | bind(&ResolverFixture<udp>::onSuccess, this, _1, |
| 205 | udp::endpoint(address_v6:: |
| 206 | from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"), |
| 207 | 80), true, true), |
| 208 | bind(&ResolverFixture<udp>::onFailure, this, false)); |
| 209 | |
| 210 | g_io.run(); |
| 211 | |
| 212 | BOOST_CHECK_EQUAL(m_nFailures, 3); |
| 213 | BOOST_CHECK_EQUAL(m_nSuccesses, 7); |
| 214 | } |
| 215 | |
| 216 | BOOST_AUTO_TEST_CASE(SyncUdp) |
| 217 | { |
| 218 | udp::endpoint endpoint; |
| 219 | BOOST_CHECK_NO_THROW(endpoint = UdpResolver::syncResolve("www.named-data.net", "6363")); |
| 220 | NFD_LOG_DEBUG("Resolved to: " << endpoint); |
| 221 | BOOST_CHECK_EQUAL(endpoint.address().is_v4(), true); |
| 222 | BOOST_CHECK_EQUAL(endpoint.port(), 6363); |
| 223 | } |
| 224 | |
| 225 | |
| 226 | BOOST_AUTO_TEST_SUITE_END() |
| 227 | |
| 228 | } // namespace tests |
| 229 | } // namespace nfd |