diff options
author | Hungming Chen <nuccachen@google.com> | 2022-04-18 12:59:35 +0000 |
---|---|---|
committer | Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com> | 2022-04-18 12:59:35 +0000 |
commit | c5457d4cfa769c95ab27f26de68b92e6abcba22d (patch) | |
tree | 84941eac244afe12d50988d6d7d49b33b2c86520 | |
parent | 14d0605601e18cf3725beefc83cbe580745b67e5 (diff) | |
parent | 32535f78471ff5faa93be2c8d1bd0a37b8c251aa (diff) | |
download | DnsResolver-c5457d4cfa769c95ab27f26de68b92e6abcba22d.tar.gz |
[DS#1] ResolverTest: assign specified socket type for GetAddrInfo_Dns64* am: b55756648a am: 32535f7847
Original change: https://android-review.googlesource.com/c/platform/packages/modules/DnsResolver/+/2063888
Change-Id: Ib383c0085bade530546472299526744c6908b484
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
-rw-r--r-- | tests/resolv_integration_test.cpp | 132 |
1 files changed, 61 insertions, 71 deletions
diff --git a/tests/resolv_integration_test.cpp b/tests/resolv_integration_test.cpp index 76427f37..eb9a3f1e 100644 --- a/tests/resolv_integration_test.cpp +++ b/tests/resolv_integration_test.cpp @@ -3074,32 +3074,29 @@ TEST_F(ResolverTest, GetAddrInfo_Dns64Synthesize) { EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); - // hints are necessary in order to let netd know which type of addresses the caller is - // interested in. - const addrinfo hints = {.ai_family = AF_UNSPEC}; + // If the socket type is not specified, every address will appear twice, once for + // SOCK_STREAM and one for SOCK_DGRAM. Just pick one because the addresses for + // the second query of different socket type are responded by the cache. + // See android_getaddrinfofornetcontext in packages/modules/DnsResolver/getaddrinfo.cpp + const addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM}; ScopedAddrinfo result = safe_getaddrinfo("v4only", nullptr, &hints); EXPECT_TRUE(result != nullptr); - // TODO: BUG: there should only be two queries, one AAAA (which returns no records) and one A - // (which returns 1.2.3.4). But there is an extra AAAA. - EXPECT_EQ(3U, GetNumQueries(dns, host_name)); - - std::string result_str = ToString(result); - EXPECT_EQ(result_str, "64:ff9b::102:304"); + // Expect that there are two queries, one AAAA (which returns no records) and one A + // (which returns 1.2.3.4). + EXPECT_EQ(2U, GetNumQueries(dns, host_name)); + EXPECT_EQ(ToString(result), "64:ff9b::102:304"); // Stopping NAT64 prefix discovery disables synthesis. EXPECT_TRUE(mDnsClient.resolvService()->stopPrefix64Discovery(TEST_NETID).isOk()); EXPECT_TRUE(WaitForNat64Prefix(EXPECT_NOT_FOUND)); - dns.clearQueries(); result = safe_getaddrinfo("v4only", nullptr, &hints); EXPECT_TRUE(result != nullptr); - // TODO: BUG: there should only be one query, an AAAA (which returns no records), because the - // A is already cached. But there is an extra AAAA. - EXPECT_EQ(2U, GetNumQueries(dns, host_name)); - - result_str = ToString(result); - EXPECT_EQ(result_str, "1.2.3.4"); + // Expect that there is one query, an AAAA (which returns no records), because the + // A is already cached. + EXPECT_EQ(1U, GetNumQueries(dns, host_name)); + EXPECT_EQ(ToString(result), "1.2.3.4"); } TEST_F(ResolverTest, GetAddrInfo_Dns64QuerySpecified) { @@ -3120,22 +3117,22 @@ TEST_F(ResolverTest, GetAddrInfo_Dns64QuerySpecified) { EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); - // Ensure to synthesize AAAA if AF_INET6 is specified, and not to synthesize AAAA - // in AF_INET case. - addrinfo hints; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET6; + // Synthesize AAAA if AF_INET6 is specified and there is A record only. Make sure that A record + // is not returned as well. + addrinfo hints = {.ai_family = AF_INET6, .ai_socktype = SOCK_DGRAM}; ScopedAddrinfo result = safe_getaddrinfo("v4only", nullptr, &hints); EXPECT_TRUE(result != nullptr); - std::string result_str = ToString(result); - EXPECT_EQ(result_str, "64:ff9b::102:304"); + // One for AAAA query without an answer and one for A query which is used for DNS64 synthesis. + EXPECT_EQ(2U, GetNumQueries(dns, host_name)); + EXPECT_EQ(ToString(result), "64:ff9b::102:304"); + dns.clearQueries(); - hints.ai_family = AF_INET; + // Don't synthesize AAAA if AF_INET is specified and there is A record only. + hints = {.ai_family = AF_INET, .ai_socktype = SOCK_DGRAM}; result = safe_getaddrinfo("v4only", nullptr, &hints); EXPECT_TRUE(result != nullptr); - EXPECT_LE(2U, GetNumQueries(dns, host_name)); - result_str = ToString(result); - EXPECT_EQ(result_str, "1.2.3.4"); + EXPECT_EQ(0U /*cached in previous queries*/, GetNumQueries(dns, host_name)); + EXPECT_EQ(ToString(result), "1.2.3.4"); } TEST_F(ResolverTest, GetAddrInfo_Dns64QueryUnspecifiedV6) { @@ -3157,17 +3154,13 @@ TEST_F(ResolverTest, GetAddrInfo_Dns64QueryUnspecifiedV6) { EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); - const addrinfo hints = {.ai_family = AF_UNSPEC}; + const addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM}; ScopedAddrinfo result = safe_getaddrinfo("v4v6", nullptr, &hints); EXPECT_TRUE(result != nullptr); - EXPECT_LE(2U, GetNumQueries(dns, host_name)); + EXPECT_EQ(2U, GetNumQueries(dns, host_name)); - // In AF_UNSPEC case, do not synthesize AAAA if there's at least one AAAA answer. - const std::vector<std::string> result_strs = ToStrings(result); - for (const auto& str : result_strs) { - EXPECT_TRUE(str == "1.2.3.4" || str == "2001:db8::102:304") - << ", result_str='" << str << "'"; - } + // Do not synthesize AAAA if there's at least one AAAA answer. + EXPECT_THAT(ToStrings(result), testing::ElementsAre("2001:db8::102:304", "1.2.3.4")); } TEST_F(ResolverTest, GetAddrInfo_Dns64QueryUnspecifiedNoV6) { @@ -3188,14 +3181,13 @@ TEST_F(ResolverTest, GetAddrInfo_Dns64QueryUnspecifiedNoV6) { EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); - const addrinfo hints = {.ai_family = AF_UNSPEC}; + const addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM}; ScopedAddrinfo result = safe_getaddrinfo("v4v6", nullptr, &hints); EXPECT_TRUE(result != nullptr); - EXPECT_LE(2U, GetNumQueries(dns, host_name)); + EXPECT_EQ(2U, GetNumQueries(dns, host_name)); - // In AF_UNSPEC case, synthesize AAAA if there's no AAAA answer. - std::string result_str = ToString(result); - EXPECT_EQ(result_str, "64:ff9b::102:304"); + // Synthesize AAAA if there's no AAAA answer and AF_UNSPEC is specified. + EXPECT_EQ(ToString(result), "64:ff9b::102:304"); } TEST_F(ResolverTest, GetAddrInfo_Dns64QuerySpecialUseIPv4Addresses) { @@ -3245,24 +3237,22 @@ TEST_F(ResolverTest, GetAddrInfo_Dns64QuerySpecialUseIPv4Addresses) { const char* host_name = testHostName.c_str(); dns.addMapping(host_name, ns_type::ns_t_a, config.addr.c_str()); - addrinfo hints; - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_INET6; + // Expect no result because AF_INET6 is specified and don't synthesize special use IPv4 + // address. + addrinfo hints = {.ai_family = AF_INET6, .ai_socktype = SOCK_DGRAM}; ScopedAddrinfo result = safe_getaddrinfo(config.name.c_str(), nullptr, &hints); - // In AF_INET6 case, don't return IPv4 answers EXPECT_TRUE(result == nullptr); - EXPECT_LE(2U, GetNumQueries(dns, host_name)); + EXPECT_EQ(2U, GetNumQueries(dns, host_name)); dns.clearQueries(); - memset(&hints, 0, sizeof(hints)); - hints.ai_family = AF_UNSPEC; + // Expect special use IPv4 address only because AF_UNSPEC is specified and don't synthesize + // special use IPv4 address. + hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM}; result = safe_getaddrinfo(config.name.c_str(), nullptr, &hints); EXPECT_TRUE(result != nullptr); // Expect IPv6 query only. IPv4 answer has been cached in previous query. - EXPECT_LE(1U, GetNumQueries(dns, host_name)); - // In AF_UNSPEC case, don't synthesize special use IPv4 address. - std::string result_str = ToString(result); - EXPECT_EQ(result_str, config.addr.c_str()); + EXPECT_EQ(1U, GetNumQueries(dns, host_name)); + EXPECT_EQ(ToString(result), config.addr); dns.clearQueries(); } } @@ -3288,24 +3278,25 @@ TEST_F(ResolverTest, GetAddrInfo_Dns64QueryWithNullArgumentHints) { EXPECT_TRUE(mDnsClient.resolvService()->startPrefix64Discovery(TEST_NETID).isOk()); EXPECT_TRUE(WaitForNat64Prefix(EXPECT_FOUND)); - // Assign argument hints of getaddrinfo() as null is equivalent to set ai_family AF_UNSPEC. - // In AF_UNSPEC case, synthesize AAAA if there has A answer only. + // Synthesize AAAA if there is A answer only and AF_UNSPEC (hints NULL) is specified. + // Assign argument hints of getaddrinfo() as null is equivalent to set ai_family AF_UNSPEC, + // ai_socktype 0 (any), and ai_protocol 0 (any). Note the setting ai_socktype 0 (any) causes + // that every address will appear twice, once for SOCK_STREAM and one for SOCK_DGRAM. + // See resolv_getaddrinfo in packages/modules/DnsResolver/getaddrinfo.cpp. ScopedAddrinfo result = safe_getaddrinfo("v4only", nullptr, nullptr); EXPECT_TRUE(result != nullptr); EXPECT_LE(2U, GetNumQueries(dns, host_name)); - std::string result_str = ToString(result); - EXPECT_EQ(result_str, "64:ff9b::102:304"); + EXPECT_EQ(ToString(result), "64:ff9b::102:304"); dns.clearQueries(); - // In AF_UNSPEC case, do not synthesize AAAA if there's at least one AAAA answer. + // Do not synthesize AAAA if there's at least one AAAA answer. + // The reason which the addresses appear twice is as mentioned above. result = safe_getaddrinfo("v4v6", nullptr, nullptr); EXPECT_TRUE(result != nullptr); EXPECT_LE(2U, GetNumQueries(dns, host_name2)); - std::vector<std::string> result_strs = ToStrings(result); - for (const auto& str : result_strs) { - EXPECT_TRUE(str == "1.2.3.4" || str == "2001:db8::102:304") - << ", result_str='" << str << "'"; - } + EXPECT_THAT(ToStrings(result), + testing::UnorderedElementsAre("2001:db8::102:304", "2001:db8::102:304", "1.2.3.4", + "1.2.3.4")); } TEST_F(ResolverTest, GetAddrInfo_Dns64QueryNullArgumentNode) { @@ -3363,11 +3354,11 @@ TEST_F(ResolverTest, GetAddrInfo_Dns64QueryNullArgumentNode) { ASSERT_TRUE(result != nullptr); // Can't be synthesized because it should not get into Netd. - std::vector<std::string> result_strs = ToStrings(result); - for (const auto& str : result_strs) { - EXPECT_TRUE(str == config.addr_v4 || str == config.addr_v6) - << ", result_str='" << str << "'"; - } + // Every address appears twice, once for SOCK_STREAM and one for SOCK_DGRAM because the + // socket type is not specified. + EXPECT_THAT(ToStrings(result), + testing::UnorderedElementsAre(config.addr_v4, config.addr_v4, config.addr_v6, + config.addr_v6)); // Assign hostname as null and service as numeric port number. hints.ai_flags = config.flag | AI_NUMERICSERV; @@ -3375,11 +3366,10 @@ TEST_F(ResolverTest, GetAddrInfo_Dns64QueryNullArgumentNode) { ASSERT_TRUE(result != nullptr); // Can't be synthesized because it should not get into Netd. - result_strs = ToStrings(result); - for (const auto& str : result_strs) { - EXPECT_TRUE(str == config.addr_v4 || str == config.addr_v6) - << ", result_str='" << str << "'"; - } + // The reason which the addresses appear twice is as mentioned above. + EXPECT_THAT(ToStrings(result), + testing::UnorderedElementsAre(config.addr_v4, config.addr_v4, config.addr_v6, + config.addr_v6)); } } |