test: Make GetNameServers() utility function port-aware

Also make it generally available.
pull/34/head
David Drysdale 9 years ago
parent e26a678f26
commit 3231480a85
  1. 23
      test/ares-test-misc.cc
  2. 34
      test/ares-test.cc
  3. 5
      test/ares-test.h

@ -7,29 +7,6 @@
namespace ares {
namespace test {
std::vector<std::string> 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<std::string> 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("<unknown family>");
break;
}
server = server->next;
}
if (servers) ares_free_data(servers);
return results;
}
TEST_F(DefaultChannelTest, GetServers) {
std::vector<std::string> servers = GetNameServers(channel_);
if (verbose) {

@ -512,6 +512,40 @@ void NameInfoCallback(void *data, int status, int timeouts,
if (verbose) std::cerr << "NameInfoCallback(" << *result << ")" << std::endl;
}
std::vector<std::string> 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<std::string> 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("<unknown family>");
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_) {

@ -15,7 +15,9 @@
#include <functional>
#include <map>
#include <set>
#include <string>
#include <utility>
#include <vector>
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<std::string> GetNameServers(ares_channel channel);
// RAII class for a temporary file with the given contents.
class TempFile {
public:

Loading…
Cancel
Save