diff --git a/test/ares-test-mock-ai.cc b/test/ares-test-mock-ai.cc index a36918cd64..4643548dff 100644 --- a/test/ares-test-mock-ai.cc +++ b/test/ares-test-mock-ai.cc @@ -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> { + 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); diff --git a/test/ares-test-mock.cc b/test/ares-test-mock.cc index d53d04d076..8c74c05f33 100644 --- a/test/ares-test-mock.cc +++ b/test/ares-test-mock.cc @@ -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 @@ -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