diff --git a/test/ares-test-misc.cc b/test/ares-test-misc.cc index 0c1d7eda..af9d5ae1 100644 --- a/test/ares-test-misc.cc +++ b/test/ares-test-misc.cc @@ -7,29 +7,6 @@ namespace ares { namespace test { -std::vector GetNameServers(ares_channel channel) { - struct ares_addr_node* servers = nullptr; - EXPECT_EQ(ARES_SUCCESS, ares_get_servers(channel, &servers)); - struct ares_addr_node* server = servers; - std::vector results; - while (server) { - switch (server->family) { - case AF_INET: - results.push_back(AddressToString((char*)&server->addr.addr4, 4)); - break; - case AF_INET6: - results.push_back(AddressToString((char*)&server->addr.addr6, 16)); - break; - default: - results.push_back(""); - break; - } - server = server->next; - } - if (servers) ares_free_data(servers); - return results; -} - TEST_F(DefaultChannelTest, GetServers) { std::vector servers = GetNameServers(channel_); if (verbose) { diff --git a/test/ares-test.cc b/test/ares-test.cc index 9f9de4c9..b6fe1015 100644 --- a/test/ares-test.cc +++ b/test/ares-test.cc @@ -512,6 +512,40 @@ void NameInfoCallback(void *data, int status, int timeouts, if (verbose) std::cerr << "NameInfoCallback(" << *result << ")" << std::endl; } +std::vector GetNameServers(ares_channel channel) { + struct ares_addr_port_node* servers = nullptr; + EXPECT_EQ(ARES_SUCCESS, ares_get_servers_ports(channel, &servers)); + struct ares_addr_port_node* server = servers; + std::vector results; + while (server) { + std::stringstream ss; + switch (server->family) { + case AF_INET: + ss << AddressToString((char*)&server->addr.addr4, 4); + break; + case AF_INET6: + if (server->udp_port != 0) { + ss << '['; + } + ss << AddressToString((char*)&server->addr.addr6, 16); + if (server->udp_port != 0) { + ss << ']'; + } + break; + default: + results.push_back(""); + break; + } + if (server->udp_port != 0) { + ss << ":" << server->udp_port; + } + results.push_back(ss.str()); + server = server->next; + } + if (servers) ares_free_data(servers); + return results; +} + TempFile::TempFile(const std::string& contents) : filename_(tempnam(nullptr, "ares")) { if (!filename_) { diff --git a/test/ares-test.h b/test/ares-test.h index 21c900fe..95009c6c 100644 --- a/test/ares-test.h +++ b/test/ares-test.h @@ -15,7 +15,9 @@ #include #include #include +#include #include +#include namespace ares { @@ -253,6 +255,9 @@ void SearchCallback(void *data, int status, int timeouts, void NameInfoCallback(void *data, int status, int timeouts, char *node, char *service); +// Retrieve the name servers used by a channel. +std::vector GetNameServers(ares_channel channel); + // RAII class for a temporary file with the given contents. class TempFile { public: