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 b494025
Show file tree
Hide file tree
Showing 2 changed files with 179 additions and 2 deletions.
176 changes: 176 additions & 0 deletions test/ares-test-mock-ai.cc
Original file line number Diff line number Diff line change
Expand Up @@ -979,6 +979,182 @@ 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) {}
};

static 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_);
EXPECT_EQ(result.status_, ARES_SUCCESS);
EXPECT_THAT(result.ai_, IncludesNumAddresses(1));
EXPECT_THAT(result.ai_, IncludesV4Address("127.0.0.1"));
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_);
EXPECT_EQ(result.status_, ARES_SUCCESS);
EXPECT_THAT(result.ai_, IncludesNumAddresses(1));
EXPECT_THAT(result.ai_, IncludesV6Address("0000:0000:0000:0000:0000:0000:0000:0001"));
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_);
EXPECT_EQ(result.status_, ARES_SUCCESS);
EXPECT_THAT(result.ai_, IncludesNumAddresses(2));
EXPECT_THAT(result.ai_, IncludesV4Address("127.0.0.1"));
EXPECT_THAT(result.ai_, IncludesV6Address("0000:0000:0000:0000:0000:0000:0000:0001"));
return HasFailure();
}


/* Issue #946 says if a v4 localhost entry exists, but not a v6 entry, v6
* isn't output correctly. */
static 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_);
EXPECT_EQ(result.status_, ARES_SUCCESS);
EXPECT_THAT(result.ai_, IncludesNumAddresses(1));
EXPECT_THAT(result.ai_, IncludesV4Address("127.0.0.1"));
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_);
EXPECT_EQ(result.status_, ARES_SUCCESS);
EXPECT_THAT(result.ai_, IncludesNumAddresses(1));
EXPECT_THAT(result.ai_, IncludesV6Address("0000:0000:0000:0000:0000:0000:0000:0001"));
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_);
EXPECT_EQ(result.status_, ARES_SUCCESS);
EXPECT_THAT(result.ai_, IncludesNumAddresses(2));
EXPECT_THAT(result.ai_, IncludesV4Address("127.0.0.1"));
EXPECT_THAT(result.ai_, IncludesV6Address("0000:0000:0000:0000:0000:0000:0000:0001"));
return HasFailure();
}


static NameContentList files_localhost_v6localhostonly = {
{"/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", "::1 localhost\n"},
{"/etc/nsswitch.conf", "hosts: files dns\n"}};
CONTAINED_TEST_P(ContainedMockChannelAISysConfig, v6OnlyLocalHostv4,
"myhostname", "mydomainname.org", files_localhost_v6localhostonly) {
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_);
EXPECT_EQ(result.status_, ARES_SUCCESS);
EXPECT_THAT(result.ai_, IncludesNumAddresses(1));
EXPECT_THAT(result.ai_, IncludesV4Address("127.0.0.1"));
return HasFailure();
}

CONTAINED_TEST_P(ContainedMockChannelAISysConfig, v6OnlyLocalHostv6,
"myhostname", "mydomainname.org", files_localhost_v6localhostonly) {
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_);
EXPECT_EQ(result.status_, ARES_SUCCESS);
EXPECT_THAT(result.ai_, IncludesNumAddresses(1));
EXPECT_THAT(result.ai_, IncludesV6Address("0000:0000:0000:0000:0000:0000:0000:0001"));
return HasFailure();
}

CONTAINED_TEST_P(ContainedMockChannelAISysConfig, v6OnlyLocalHostUnspec,
"myhostname", "mydomainname.org", files_localhost_v6localhostonly) {
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_);
EXPECT_EQ(result.status_, ARES_SUCCESS);
EXPECT_THAT(result.ai_, IncludesNumAddresses(2));
EXPECT_THAT(result.ai_, IncludesV4Address("127.0.0.1"));
EXPECT_THAT(result.ai_, IncludesV6Address("0000:0000:0000:0000:0000:0000:0000:0001"));
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
5 changes: 3 additions & 2 deletions test/ares-test-mock.cc
Original file line number Diff line number Diff line change
Expand Up @@ -850,7 +850,7 @@ class ContainedMockChannelSysConfig
: MockChannelOptsTest(1, GetParam().first, GetParam().second, true, nullptr, 0) {}
};

NameContentList files_no_ndots = {
static 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
Expand All @@ -875,7 +875,8 @@ CONTAINED_TEST_P(ContainedMockChannelSysConfig, SysConfigNdotsDefault,
return HasFailure();
}

NameContentList files_ndots0 = {

static NameContentList files_ndots0 = {
{"/etc/resolv.conf", "nameserver 1.2.3.4\n" // Will be replaced
"search example.com example.org\n"
"options edns0 trust-ad ndots:0\n"}, // ndots:1 is default
Expand Down

0 comments on commit b494025

Please sign in to comment.