ResolverTest: add GetAddrInfoV6_MultiAnswers

Bug: 254192523
Test: atest ResolverTest
Change-Id: I2d300d459e974aad14ede8c8dc3f8bd66667a008
diff --git a/tests/resolv_integration_test.cpp b/tests/resolv_integration_test.cpp
index 01b0a8f..a04e696 100644
--- a/tests/resolv_integration_test.cpp
+++ b/tests/resolv_integration_test.cpp
@@ -947,6 +947,60 @@
                                      kHelloExampleComAddrV4_2, kHelloExampleComAddrV4_3));
 }
 
+TEST_F(ResolverTest, GetAddrInfoV6_MultiAnswers) {
+    test::DNSResponder dns(test::DNSResponder::MappingType::BINARY_PACKET);
+    dns.addMappingBinaryPacket(kHelloExampleComQueryV6, kHelloExampleComResponsesV6);
+    StartDns(dns, {});
+    ASSERT_TRUE(mDnsClient.SetResolversForNetwork());
+
+    addrinfo hints = {.ai_family = AF_UNSPEC, .ai_socktype = SOCK_DGRAM};
+    ScopedAddrinfo result = safe_getaddrinfo(kHelloExampleCom, nullptr, &hints);
+    ASSERT_FALSE(result == nullptr);
+
+    // Expect the DNS result order is GUA, teredo tunneling address and IPv4-compatible address
+    // because of the precedence comparison of RFC 6724.
+    //
+    // The reason is here for the sorting result from _rfc6724_compare.
+    // For rule 1: avoid unusable destinations, all addresses are unusable on a fake test network.
+    // For rule 2: prefer matching scope, all addresses don't match because of no source address.
+    //             See rule#1 as well.
+    // (rule 3 is not implemented)
+    // (rule 4 is not implemented)
+    // For rule 5: prefer matching label, the source address is not valid and can't match the dns
+    //             reply addresses. See rule#1 as well.
+    // For rule 6: prefer higher precedence, sorted by the order: gua(40), teredo(5) and
+    //             ipv4-compatible(1).
+    // Ignore from rule 7 to rule 10 because the results has been sorted by rule 6.
+    //
+    // See _get_precedence, _rfc6724_compare in packages/modules/DnsResolver/getaddrinfo.cpp
+    EXPECT_THAT(ToStrings(result),
+                testing::ElementsAre(kHelloExampleComAddrV6_GUA, kHelloExampleComAddrV6_TEREDO,
+                                     kHelloExampleComAddrV6_IPV4COMPAT));
+
+    hints = {.ai_family = AF_UNSPEC};
+    result = safe_getaddrinfo(kHelloExampleCom, nullptr, &hints);
+    ASSERT_FALSE(result == nullptr);
+
+    // The results are sorted in every querying by explore_options and then concatenates all sorted
+    // results. resolv_getaddrinfo() calls explore_fqdn() many times by the different
+    // explore_options. It means that resolv_rfc6724_sort() only sorts the ordering in the results
+    // of each explore_options and concatenates all sorted results into one link list. The address
+    // order of the output addrinfo is:
+    //   2404:6800::5175:15ca (socktype=2, protocol=17) ->
+    //   2001::47c1 (socktype=2, protocol=17) ->
+    //   ::1.2.3.4 (socktype=2, protocol=17) ->
+    //   2404:6800::5175:15ca (socktype=1, protocol=6) ->
+    //   2001::47c1 (socktype=1, protocol=6) ->
+    //   ::1.2.3.4 (socktype=1, protocol=6)
+    //
+    // See resolv_getaddrinfo, explore_fqdn and dns_getaddrinfo.
+    EXPECT_THAT(
+            ToStrings(result),
+            testing::ElementsAre(kHelloExampleComAddrV6_GUA, kHelloExampleComAddrV6_TEREDO,
+                                 kHelloExampleComAddrV6_IPV4COMPAT, kHelloExampleComAddrV6_GUA,
+                                 kHelloExampleComAddrV6_TEREDO, kHelloExampleComAddrV6_IPV4COMPAT));
+}
+
 TEST_F(ResolverTest, GetAddrInfo_cnames) {
     constexpr char host_name[] = "host.example.com.";
     test::DNSResponder dns;
diff --git a/tests/resolv_test_utils.h b/tests/resolv_test_utils.h
index e70dc58..6c2db4b 100644
--- a/tests/resolv_test_utils.h
+++ b/tests/resolv_test_utils.h
@@ -151,6 +151,9 @@
 static constexpr char kHelloExampleComAddrV4_2[] = "8.8.8.8";
 static constexpr char kHelloExampleComAddrV4_3[] = "81.117.21.202";
 static constexpr char kHelloExampleComAddrV6[] = "::1.2.3.4";
+static constexpr char kHelloExampleComAddrV6_IPV4COMPAT[] = "::1.2.3.4";
+static constexpr char kHelloExampleComAddrV6_TEREDO[] = "2001::47c1";
+static constexpr char kHelloExampleComAddrV6_GUA[] = "2404:6800::5175:15ca";
 static constexpr char kExampleComDomain[] = ".example.com";
 
 static const std::string kNat64Prefix = "64:ff9b::/96";
@@ -217,6 +220,7 @@
         0x63, 0x6F, 0x6D, 0x00, /* Name: hello.example.com */
         0x00, 0x01,             /* Type: A */
         0x00, 0x01,             /* Class: IN */
+        /* Answers */
         0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
         0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
         0x00, 0x01,             /* Type: A */
@@ -240,6 +244,72 @@
         0x51, 0x75, 0x15, 0xca  /* Address: 81.117.21.202 */
 };
 
+static const std::vector<uint8_t> kHelloExampleComQueryV6 = {
+        /* Header */
+        0x00, 0x00, /* Transaction ID: 0x0000 */
+        0x01, 0x00, /* Flags: rd */
+        0x00, 0x01, /* Questions: 1 */
+        0x00, 0x00, /* Answer RRs: 0 */
+        0x00, 0x00, /* Authority RRs: 0 */
+        0x00, 0x00, /* Additional RRs: 0 */
+        /* Queries */
+        0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
+        0x00, 0x1c,             /* Type: AAAA */
+        0x00, 0x01              /* Class: IN */
+};
+
+const std::vector<uint8_t> kHelloExampleComResponsesV6 = {
+        // The addresses are IPv4-compatible address, teredo tunneling address and global unicast
+        // address.
+        //
+        // scapy.DNS(
+        // id=0,
+        // qr=1,
+        // ra=1,
+        // qd=scapy.DNSQR(qname="hello.example.com",qtype="AAAA"),
+        // an=scapy.DNSRR(rrname="hello.example.com",type="AAAA",rdata='::1.2.3.4') /
+        //    scapy.DNSRR(rrname="hello.example.com",type="AAAA",rdata='2001::47c1') /
+        //    scapy.DNSRR(rrname="hello.example.com",type="AAAA",rdata='2404:6800::5175:15ca'))
+        /* Header */
+        0x00, 0x00, /* Transaction ID: 0x0000 */
+        0x81, 0x80, /* Flags: qr rd ra */
+        0x00, 0x01, /* Questions: 1 */
+        0x00, 0x03, /* Answer RRs: 3 */
+        0x00, 0x00, /* Authority RRs: 0 */
+        0x00, 0x00, /* Additional RRs: 0 */
+        /* Queries */
+        0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
+        0x00, 0x1c,             /* Type: AAAA */
+        0x00, 0x01,             /* Class: IN */
+        /* Answers */
+        0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
+        0x00, 0x1c,             /* Type: AAAA */
+        0x00, 0x01,             /* Class: IN */
+        0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */
+        0x00, 0x10,             /* Data length: 4 */
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x02, 0x03,
+        0x04, /* Address: ::1.2.3.4 */
+        0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
+        0x00, 0x1c,             /* Type: AAAA */
+        0x00, 0x01,             /* Class: IN */
+        0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */
+        0x00, 0x10,             /* Data length: 4 */
+        0x20, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x47,
+        0xc1, /* Address: 2001::47c1 */
+        0x05, 0x68, 0x65, 0x6c, 0x6c, 0x6f, 0x07, 0x65, 0x78, 0x61, 0x6d, 0x70, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, /* Name: hello.example.com */
+        0x00, 0x1c,             /* Type: AAAA */
+        0x00, 0x01,             /* Class: IN */
+        0x00, 0x00, 0x00, 0x00, /* Time to live: 0 */
+        0x00, 0x10,             /* Data length: 4 */
+        0x24, 0x04, 0x68, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x51, 0x75, 0x15,
+        0xCA /* Address: 2404:6800::5175:15ca */
+};
+
 // Illegal hostnames
 static constexpr char kBadCharAfterPeriodHost[] = "hello.example.^com.";
 static constexpr char kBadCharBeforePeriodHost[] = "hello.example^.com.";