Skip to content

Commit

Permalink
add additional localhost tests
Browse files Browse the repository at this point in the history
  • Loading branch information
bradh352 committed Dec 27, 2024
1 parent 613c263 commit aa6bc5a
Showing 1 changed file with 84 additions and 0 deletions.
84 changes: 84 additions & 0 deletions test/ares-test-mock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,90 @@ 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
"search example.com example.org\n"
Expand Down

0 comments on commit aa6bc5a

Please sign in to comment.