core: Generic resolver implementation with option to select address filter

Change-Id: I467ed9f49a55392e89417aa9925f30b2d7cbeda9
diff --git a/tests/core/resolver.cpp b/tests/core/resolver.cpp
new file mode 100644
index 0000000..4d7460f
--- /dev/null
+++ b/tests/core/resolver.cpp
@@ -0,0 +1,229 @@
+/* -*- Mode:C++; c-file-style:"gnu"; indent-tabs-mode:nil; -*- */
+/**
+ * Copyright (C) 2014 Named Data Networking Project
+ * See COPYING for copyright and distribution information.
+ */
+
+#include "core/resolver.hpp"
+#include "core/logger.hpp"
+#include <boost/test/unit_test.hpp>
+
+#include "tests/test-common.hpp"
+
+namespace nfd {
+namespace tests {
+
+NFD_LOG_INIT("tests.CoreResolver");
+
+using boost::asio::ip::address_v4;
+using boost::asio::ip::address_v6;
+using boost::asio::ip::tcp;
+using boost::asio::ip::udp;
+
+BOOST_FIXTURE_TEST_SUITE(CoreResolver, BaseFixture)
+
+template<class Protocol>
+class ResolverFixture : protected BaseFixture
+{
+public:
+  ResolverFixture()
+    : m_nFailures(0)
+    , m_nSuccesses(0)
+  {
+  }
+
+  void
+  onSuccess(const typename Protocol::endpoint& resolvedEndpoint,
+            const typename Protocol::endpoint& expectedEndpoint,
+            bool isValid, bool wantCheckAddress = false)
+  {
+    NFD_LOG_DEBUG("Resolved to: " << resolvedEndpoint);
+    ++m_nSuccesses;
+
+    if (!isValid)
+      {
+        BOOST_FAIL("Resolved to " + boost::lexical_cast<std::string>(resolvedEndpoint)
+                   + ", but it should have failed");
+      }
+
+    BOOST_CHECK_EQUAL(resolvedEndpoint.port(), expectedEndpoint.port());
+
+    BOOST_CHECK_EQUAL(resolvedEndpoint.address().is_v4(), expectedEndpoint.address().is_v4());
+
+    // checking address is not deterministic and should be enabled only
+    // if only one IP address will be returned by resolution
+    if (wantCheckAddress)
+      {
+        BOOST_CHECK_EQUAL(resolvedEndpoint.address(), expectedEndpoint.address());
+      }
+  }
+
+  void
+  onFailure(bool isValid)
+  {
+    ++m_nFailures;
+
+    if (!isValid)
+      BOOST_FAIL("Resolution should not have failed");
+
+    BOOST_CHECK_MESSAGE(true, "Resolution failed as expected");
+  }
+
+
+  uint32_t m_nFailures;
+  uint32_t m_nSuccesses;
+};
+
+BOOST_FIXTURE_TEST_CASE(Tcp, ResolverFixture<tcp>)
+{
+  TcpResolver::asyncResolve("www.named-data.net", "6363",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v4(), 6363), true, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false));
+
+  TcpResolver::asyncResolve("www.named-data.net", "notport",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v4(), 0), false, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, true)); // should fail
+
+
+  TcpResolver::asyncResolve("nothost.nothost.nothost.arpa", "6363",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v4(), 6363), false, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, true)); // should fail
+
+  TcpResolver::asyncResolve("www.google.com", "80",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v4(), 80), true, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false),
+                            resolver::Ipv4Address()); // request IPv4 address
+
+  TcpResolver::asyncResolve("www.google.com", "80",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false),
+                            resolver::Ipv6Address()); // request IPv6 address
+
+  TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false));
+
+  TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false),
+                            resolver::Ipv6Address());
+
+  TcpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v6(), 80), false, false),
+                            bind(&ResolverFixture<tcp>::onFailure, this, true), // should fail
+                            resolver::Ipv4Address());
+
+  TcpResolver::asyncResolve("192.0.2.1", "80",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v4::from_string("192.0.2.1"), 80), true, true),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false));
+
+  TcpResolver::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", "80",
+                            bind(&ResolverFixture<tcp>::onSuccess, this, _1,
+                                 tcp::endpoint(address_v6::
+                                               from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"),
+                                               80), true, true),
+                            bind(&ResolverFixture<tcp>::onFailure, this, false));
+
+  g_io.run();
+
+  BOOST_CHECK_EQUAL(m_nFailures, 3);
+  BOOST_CHECK_EQUAL(m_nSuccesses, 7);
+}
+
+BOOST_AUTO_TEST_CASE(SyncTcp)
+{
+  tcp::endpoint endpoint;
+  BOOST_CHECK_NO_THROW(endpoint = TcpResolver::syncResolve("www.named-data.net", "6363"));
+  NFD_LOG_DEBUG("Resolved to: " << endpoint);
+  BOOST_CHECK_EQUAL(endpoint.address().is_v4(), true);
+  BOOST_CHECK_EQUAL(endpoint.port(), 6363);
+}
+
+BOOST_FIXTURE_TEST_CASE(Udp, ResolverFixture<udp>)
+{
+  UdpResolver::asyncResolve("www.named-data.net", "6363",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v4(), 6363), true, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, false));
+
+  UdpResolver::asyncResolve("www.named-data.net", "notport",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v4(), 0), false, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, true)); // should fail
+
+
+  UdpResolver::asyncResolve("nothost.nothost.nothost.arpa", "6363",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v4(), 6363), false, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, true)); // should fail
+
+  UdpResolver::asyncResolve("www.google.com", "80",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v4(), 80), true, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, false),
+                            resolver::Ipv4Address()); // request IPv4 address
+
+  UdpResolver::asyncResolve("www.google.com", "80",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, false),
+                            resolver::Ipv6Address()); // request IPv6 address
+
+  UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, false));
+
+  UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v6(), 80), true, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, false),
+                            resolver::Ipv6Address());
+
+  UdpResolver::asyncResolve("ipv6.google.com", "80", // only IPv6 address should be available
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v6(), 80), false, false),
+                            bind(&ResolverFixture<udp>::onFailure, this, true), // should fail
+                            resolver::Ipv4Address());
+
+  UdpResolver::asyncResolve("192.0.2.1", "80",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v4::from_string("192.0.2.1"), 80), true, true),
+                            bind(&ResolverFixture<udp>::onFailure, this, false));
+
+  UdpResolver::asyncResolve("2001:db8:3f9:0:3025:ccc5:eeeb:86d3", "80",
+                            bind(&ResolverFixture<udp>::onSuccess, this, _1,
+                                 udp::endpoint(address_v6::
+                                               from_string("2001:db8:3f9:0:3025:ccc5:eeeb:86d3"),
+                                               80), true, true),
+                            bind(&ResolverFixture<udp>::onFailure, this, false));
+
+  g_io.run();
+
+  BOOST_CHECK_EQUAL(m_nFailures, 3);
+  BOOST_CHECK_EQUAL(m_nSuccesses, 7);
+}
+
+BOOST_AUTO_TEST_CASE(SyncUdp)
+{
+  udp::endpoint endpoint;
+  BOOST_CHECK_NO_THROW(endpoint = UdpResolver::syncResolve("www.named-data.net", "6363"));
+  NFD_LOG_DEBUG("Resolved to: " << endpoint);
+  BOOST_CHECK_EQUAL(endpoint.address().is_v4(), true);
+  BOOST_CHECK_EQUAL(endpoint.port(), 6363);
+}
+
+
+BOOST_AUTO_TEST_SUITE_END()
+
+} // namespace tests
+} // namespace nfd