Skip to content

Commit

Permalink
use getaddrinfo
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh352 committed Dec 27, 2024
1 parent aa6bc5a commit 171c1f1
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 83 deletions.
122 changes: 122 additions & 0 deletions test/ares-test-mock-ai.cc
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,128 @@ TEST_P(MockChannelTestAI, FamilyV4ServiceName) {
EXPECT_EQ("{addr=[1.1.1.1:80], addr=[2.2.2.2:80]}", ss.str());
}

#ifdef HAVE_CONTAINER

class ContainedMockChannelAISysConfig
: public MockChannelOptsTest,
public ::testing::WithParamInterface<std::pair<int, bool>> {
public:
ContainedMockChannelAISysConfig()
: MockChannelOptsTest(1, GetParam().first, GetParam().second, true, nullptr, 0) {}
};

NameContentList files_no_ndots = {
{"/etc/resolv.conf", "nameserver 1.2.3.4\n" // Will be replaced
"search example.com example.org\n"
"options edns0 trust-ad\n"}, // ndots:1 is default
{"/etc/hosts", "3.4.5.6 ahostname.com\n"},
{"/etc/nsswitch.conf", "hosts: files dns\n"}};

/* These tests should still work even with /etc/hosts not having any localhost
* entries */
CONTAINED_TEST_P(ContainedMockChannelAISysConfig, NoHostsLocalHostv4,
"myhostname", "mydomainname.org", files_no_ndots) {
AddrInfoResult result = {};
struct ares_addrinfo_hints hints = {0, 0, 0, 0};
hints.ai_family = AF_INET;
hints.ai_flags = ARES_AI_NOSORT;
ares_getaddrinfo(channel_, "localhost", NULL, &hints, AddrInfoCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.ai_;
EXPECT_EQ("{addr=[127.0.0.1]}", ss.str());
return HasFailure();
}

CONTAINED_TEST_P(ContainedMockChannelAISysConfig, NoHostsLocalHostv6,
"myhostname", "mydomainname.org", files_no_ndots) {
AddrInfoResult result = {};
struct ares_addrinfo_hints hints = {0, 0, 0, 0};
hints.ai_family = AF_INET6;
hints.ai_flags = ARES_AI_NOSORT;
ares_getaddrinfo(channel_, "localhost", NULL, &hints, AddrInfoCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.ai_;
EXPECT_EQ("{addr=[0000:0000:0000:0000:0000:0000:0000:0001]}", ss.str());
return HasFailure();
}

CONTAINED_TEST_P(ContainedMockChannelAISysConfig, NoHostsLocalHostUnspec,
"myhostname", "mydomainname.org", files_no_ndots) {
AddrInfoResult result = {};
struct ares_addrinfo_hints hints = {0, 0, 0, 0};
hints.ai_family = AF_UNSPEC;
hints.ai_flags = ARES_AI_NOSORT;
ares_getaddrinfo(channel_, "localhost", NULL, &hints, AddrInfoCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.ai_;
EXPECT_EQ("{addr=[127.0.0.1], addr=[0000:0000:0000:0000:0000:0000:0000:0001]}", ss.str());
return HasFailure();
}


/* Issue #946 says if a v4 localhost entry exists, but not a v6 entry, v6
* isn't output correctly. */
NameContentList files_localhost_v4localhostonly = {
{"/etc/resolv.conf", "nameserver 1.2.3.4\n" // Will be replaced
"search example.com example.org\n"
"options edns0 trust-ad\n"}, // ndots:1 is default
{"/etc/hosts", "127.0.0.1 localhost\n"},
{"/etc/nsswitch.conf", "hosts: files dns\n"}};
CONTAINED_TEST_P(ContainedMockChannelAISysConfig, v4OnlyLocalHostv4,
"myhostname", "mydomainname.org", files_localhost_v4localhostonly) {
AddrInfoResult result = {};
struct ares_addrinfo_hints hints = {0, 0, 0, 0};
hints.ai_family = AF_INET;
hints.ai_flags = ARES_AI_NOSORT;
ares_getaddrinfo(channel_, "localhost", NULL, &hints, AddrInfoCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.ai_;
EXPECT_EQ("{addr=[127.0.0.1]}", ss.str());
return HasFailure();
}

CONTAINED_TEST_P(ContainedMockChannelAISysConfig, v4OnlyLocalHostv6,
"myhostname", "mydomainname.org", files_localhost_v4localhostonly) {
AddrInfoResult result = {};
struct ares_addrinfo_hints hints = {0, 0, 0, 0};
hints.ai_family = AF_INET6;
hints.ai_flags = ARES_AI_NOSORT;
ares_getaddrinfo(channel_, "localhost", NULL, &hints, AddrInfoCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.ai_;
EXPECT_EQ("{addr=[0000:0000:0000:0000:0000:0000:0000:0001]}", ss.str());
return HasFailure();
}

CONTAINED_TEST_P(ContainedMockChannelAISysConfig, v4OnlyLocalHostUnspec,
"myhostname", "mydomainname.org", files_localhost_v4localhostonly) {
AddrInfoResult result = {};
struct ares_addrinfo_hints hints = {0, 0, 0, 0};
hints.ai_family = AF_UNSPEC;
hints.ai_flags = ARES_AI_NOSORT;
ares_getaddrinfo(channel_, "localhost", NULL, &hints, AddrInfoCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.ai_;
EXPECT_EQ("{addr=[127.0.0.1], addr=[0000:0000:0000:0000:0000:0000:0000:0001]}", ss.str());
return HasFailure();
}


INSTANTIATE_TEST_SUITE_P(AddressFamiliesAI, ContainedMockChannelAISysConfig, ::testing::ValuesIn(ares::test::families_modes), PrintFamilyMode);
#endif

INSTANTIATE_TEST_SUITE_P(AddressFamiliesAI, MockChannelTestAI,
::testing::ValuesIn(ares::test::families_modes), PrintFamilyMode);

Expand Down
83 changes: 0 additions & 83 deletions test/ares-test-mock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -875,89 +875,6 @@ CONTAINED_TEST_P(ContainedMockChannelSysConfig, SysConfigNdotsDefault,
return HasFailure();
}

/* These tests should still work even with /etc/hosts not having any localhost
* entries */
CONTAINED_TEST_P(ContainedMockChannelSysConfig, NoHostsLocalHostv4,
"myhostname", "mydomainname.org", files_no_ndots) {
HostResult result;
ares_gethostbyname(channel_, "localhost", AF_INET, HostCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.host_;
EXPECT_EQ("{'localhost' aliases=[] addrs=[127.0.0.1]}", ss.str());
return HasFailure();
}

CONTAINED_TEST_P(ContainedMockChannelSysConfig, NoHostsLocalHostv6,
"myhostname", "mydomainname.org", files_no_ndots) {
HostResult result;
ares_gethostbyname(channel_, "localhost", AF_INET6, HostCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.host_;
EXPECT_EQ("{'localhost' aliases=[] addrs=[0000:0000:0000:0000:0000:0000:0000:0001]}", ss.str());
return HasFailure();
}

CONTAINED_TEST_P(ContainedMockChannelSysConfig, NoHostsLocalHostUnspec,
"myhostname", "mydomainname.org", files_no_ndots) {
HostResult result;
ares_gethostbyname(channel_, "localhost", AF_UNSPEC, HostCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.host_;
EXPECT_EQ("{'localhost' aliases=[] addrs=[127.0.0.1,0000:0000:0000:0000:0000:0000:0000:0001]}", ss.str());
return HasFailure();
}


/* Issue #946 says if a v4 localhost entry exists, but not a v6 entry, v6
* isn't output correctly. */
NameContentList files_localhost_v4localhostonly = {
{"/etc/resolv.conf", "nameserver 1.2.3.4\n" // Will be replaced
"search example.com example.org\n"
"options edns0 trust-ad\n"}, // ndots:1 is default
{"/etc/hosts", "127.0.0.1 localhost\n"},
{"/etc/nsswitch.conf", "hosts: files dns\n"}};
CONTAINED_TEST_P(ContainedMockChannelSysConfig, v4OnlyLocalHostv4,
"myhostname", "mydomainname.org", files_localhost_v4localhostonly) {
HostResult result;
ares_gethostbyname(channel_, "localhost", AF_INET, HostCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.host_;
EXPECT_EQ("{'localhost' aliases=[] addrs=[127.0.0.1]}", ss.str());
return HasFailure();
}

CONTAINED_TEST_P(ContainedMockChannelSysConfig, v4OnlyLocalHostv6,
"myhostname", "mydomainname.org", files_localhost_v4localhostonly) {
HostResult result;
ares_gethostbyname(channel_, "localhost", AF_INET6, HostCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.host_;
EXPECT_EQ("{'localhost' aliases=[] addrs=[0000:0000:0000:0000:0000:0000:0000:0001]}", ss.str());
return HasFailure();
}

CONTAINED_TEST_P(ContainedMockChannelSysConfig, v4OnlyLocalHostUnspec,
"myhostname", "mydomainname.org", files_localhost_v4localhostonly) {
HostResult result;
ares_gethostbyname(channel_, "localhost", AF_UNSPEC, HostCallback, &result);
Process();
EXPECT_TRUE(result.done_);
std::stringstream ss;
ss << result.host_;
EXPECT_EQ("{'localhost' aliases=[] addrs=[127.0.0.1,0000:0000:0000:0000:0000:0000:0000:0001]}", ss.str());
return HasFailure();
}


NameContentList files_ndots0 = {
{"/etc/resolv.conf", "nameserver 1.2.3.4\n" // Will be replaced
Expand Down

0 comments on commit 171c1f1

Please sign in to comment.