Minor changes for DnsUtils and its test
fix nits for ag/7671031
Bug: 129530368
Test: atest DnsUtilsTest
Change-Id: I562d33870a933a5fe55c89137a2d9574e3c59b90
diff --git a/core/java/android/net/util/DnsUtils.java b/core/java/android/net/util/DnsUtils.java
index e6abd50..7908353 100644
--- a/core/java/android/net/util/DnsUtils.java
+++ b/core/java/android/net/util/DnsUtils.java
@@ -141,14 +141,17 @@
*/
public static @NonNull List<InetAddress> rfc6724Sort(@Nullable Network network,
@NonNull List<InetAddress> answers) {
- List<SortableAddress> sortableAnswerList = new ArrayList<>();
- answers.forEach(addr -> sortableAnswerList.add(
- new SortableAddress(addr, findSrcAddress(network, addr))));
+ final ArrayList<SortableAddress> sortableAnswerList = new ArrayList<>();
+ for (InetAddress addr : answers) {
+ sortableAnswerList.add(new SortableAddress(addr, findSrcAddress(network, addr)));
+ }
Collections.sort(sortableAnswerList, sRfc6724Comparator);
final List<InetAddress> sortedAnswers = new ArrayList<>();
- sortableAnswerList.forEach(ans -> sortedAnswers.add(ans.address));
+ for (SortableAddress ans : sortableAnswerList) {
+ sortedAnswers.add(ans.address);
+ }
return sortedAnswers;
}
diff --git a/tests/net/java/android/net/util/DnsUtilsTest.java b/tests/net/java/android/net/util/DnsUtilsTest.java
index 42e340b..b626db8 100644
--- a/tests/net/java/android/net/util/DnsUtilsTest.java
+++ b/tests/net/java/android/net/util/DnsUtilsTest.java
@@ -57,24 +57,38 @@
@Test
public void testRfc6724Comparator() {
final List<DnsUtils.SortableAddress> test = Arrays.asList(
- makeSortableAddress("216.58.200.36"), // Ipv4
- makeSortableAddress("2404:6800:4008:801::2004"), // global
- makeSortableAddress("::1"), // loop back
- makeSortableAddress("fe80::c46f:1cff:fe04:39b4"), // link local
- makeSortableAddress("::ffff:192.168.95.3"), // IPv4-mapped IPv6
- makeSortableAddress("2001::47c1"), // teredo tunneling
- makeSortableAddress("::216.58.200.36"), // IPv4-compatible
- makeSortableAddress("3ffe::1234:5678")); // 6bone
+ // Ipv4
+ makeSortableAddress("216.58.200.36", "192.168.1.1"),
+ // global with different scope src
+ makeSortableAddress("2404:6800:4008:801::2004", "fe80::1111:2222"),
+ // global without src addr
+ makeSortableAddress("2404:6800:cafe:801::1"),
+ // loop back
+ makeSortableAddress("::1", "::1"),
+ // link local
+ makeSortableAddress("fe80::c46f:1cff:fe04:39b4", "fe80::1"),
+ // teredo tunneling
+ makeSortableAddress("2001::47c1", "2001::2"),
+ // 6bone without src addr
+ makeSortableAddress("3ffe::1234:5678"),
+ // IPv4-compatible
+ makeSortableAddress("::216.58.200.36", "::216.58.200.9"),
+ // 6bone
+ makeSortableAddress("3ffe::1234:5678", "3ffe::1234:1"),
+ // IPv4-mapped IPv6
+ makeSortableAddress("::ffff:192.168.95.7", "::ffff:192.168.95.1"));
final List<InetAddress> expected = Arrays.asList(
stringToAddress("::1"), // loop back
stringToAddress("fe80::c46f:1cff:fe04:39b4"), // link local
- stringToAddress("2404:6800:4008:801::2004"), // global
stringToAddress("216.58.200.36"), // Ipv4
- stringToAddress("::ffff:192.168.95.3"), // IPv4-mapped IPv6
+ stringToAddress("::ffff:192.168.95.7"), // IPv4-mapped IPv6
stringToAddress("2001::47c1"), // teredo tunneling
- stringToAddress("::216.58.200.36"), // IPv4-compatible
- stringToAddress("3ffe::1234:5678")); // 6bone
+ stringToAddress("::216.58.200.36"), // IPv4-compatible
+ stringToAddress("3ffe::1234:5678"), // 6bone
+ stringToAddress("2404:6800:4008:801::2004"), // global with different scope src
+ stringToAddress("2404:6800:cafe:801::1"), // global without src addr
+ stringToAddress("3ffe::1234:5678")); // 6bone without src addr
Collections.sort(test, new DnsUtils.Rfc6724Comparator());