From 3c1889fbd0d7b96aae16f3479ac8aae70a7e15f7 Mon Sep 17 00:00:00 2001 From: Florent Le Coz Date: Wed, 28 Oct 2015 19:13:53 +0100 Subject: Use Catch for our test suite `make check` is also added to compile and run the tests Catch is fetched with cmake automatically into the build directory when needed --- tests/dns.cpp | 83 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 tests/dns.cpp (limited to 'tests/dns.cpp') diff --git a/tests/dns.cpp b/tests/dns.cpp new file mode 100644 index 0000000..e8cbc7f --- /dev/null +++ b/tests/dns.cpp @@ -0,0 +1,83 @@ +#include "catch.hpp" + +#include +#include +#include + +#include + +TEST_CASE("DNS resolver") +{ + Resolver resolver; + + /** + * If we are using cares, we need to run a poller loop until each + * resolution is finished. Without cares we get the result before + * resolve() returns because it’s blocking. + */ +#ifdef CARES_FOUND + auto p = std::make_shared(); + + const auto loop = [&p]() + { + do + { + DNSHandler::instance.watch_dns_sockets(p); + } + while (p->poll(utils::no_timeout) != -1); + }; +#else + // We don’t need to do anything if we are not using cares. + const auto loop = [](){}; +#endif + + std::string hostname; + std::string port = "6667"; + + bool success = true; + + auto error_cb = [&success, &hostname, &port](const char* msg) + { + INFO("Failed to resolve " << hostname << ":" << port << ": " << msg); + success = false; + }; + auto success_cb = [&success, &hostname, &port](const struct addrinfo* addr) + { + INFO("Successfully resolved " << hostname << ":" << port << ": " << addr_to_string(addr)); + success = true; + }; + + hostname = "example.com"; + resolver.resolve(hostname, port, + success_cb, error_cb); + loop(); + CHECK(success); + + hostname = "this.should.fail.because.it.is..misformatted"; + resolver.resolve(hostname, port, + success_cb, error_cb); + loop(); + CHECK(!success); + + hostname = "this.should.fail.because.it.is.does.not.exist.invalid"; + resolver.resolve(hostname, port, + success_cb, error_cb); + loop(); + CHECK(!success); + + hostname = "localhost6"; + resolver.resolve(hostname, port, + success_cb, error_cb); + loop(); + CHECK(success); + + hostname = "localhost"; + resolver.resolve(hostname, port, + success_cb, error_cb); + loop(); + CHECK(success); + +#ifdef CARES_FOUND + DNSHandler::instance.destroy(); +#endif +} -- cgit v1.2.3