Merge "Parse ND options in netlink message as an ByteBuffer slice."
diff --git a/common/framework/com/android/net/module/util/CollectionUtils.java b/common/framework/com/android/net/module/util/CollectionUtils.java
index 0696cca..6e1af55 100644
--- a/common/framework/com/android/net/module/util/CollectionUtils.java
+++ b/common/framework/com/android/net/module/util/CollectionUtils.java
@@ -123,6 +123,19 @@
     /**
      * @return true if the array contains the specified value.
      */
+    public static boolean contains(@Nullable short[] array, short value) {
+        if (array == null) return false;
+        for (int element : array) {
+            if (element == value) {
+                return true;
+            }
+        }
+        return false;
+    }
+
+    /**
+     * @return true if the array contains the specified value.
+     */
     public static boolean contains(@Nullable int[] array, int value) {
         if (array == null) return false;
         for (int element : array) {
diff --git a/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt b/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt
index 0886426..96648a5 100644
--- a/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt
+++ b/common/tests/unit/src/com/android/net/module/util/CollectionUtilsTest.kt
@@ -61,4 +61,20 @@
         assertTrue(CollectionUtils.all(listOf(1)) { true })
         assertFalse(CollectionUtils.all(listOf(1)) { false })
     }
+
+    @Test
+    fun testContains() {
+        assertTrue(CollectionUtils.contains(shortArrayOf(10, 20, 30), 10))
+        assertTrue(CollectionUtils.contains(shortArrayOf(10, 20, 30), 30))
+        assertFalse(CollectionUtils.contains(shortArrayOf(10, 20, 30), 40))
+        assertFalse(CollectionUtils.contains(null, 10.toShort()))
+        assertTrue(CollectionUtils.contains(intArrayOf(10, 20, 30), 10))
+        assertTrue(CollectionUtils.contains(intArrayOf(10, 20, 30), 30))
+        assertFalse(CollectionUtils.contains(intArrayOf(10, 20, 30), 40))
+        assertFalse(CollectionUtils.contains(null, 10.toInt()))
+        assertTrue(CollectionUtils.contains(arrayOf("A", "B", "C"), "A"))
+        assertTrue(CollectionUtils.contains(arrayOf("A", "B", "C"), "C"))
+        assertFalse(CollectionUtils.contains(arrayOf("A", "B", "C"), "D"))
+        assertFalse(CollectionUtils.contains(null, "A"))
+    }
 }
diff --git a/common/testutils/devicetests/com/android/testutils/TestNetworkTracker.kt b/common/testutils/devicetests/com/android/testutils/TestNetworkTracker.kt
index 36c09ce..40731ea 100644
--- a/common/testutils/devicetests/com/android/testutils/TestNetworkTracker.kt
+++ b/common/testutils/devicetests/com/android/testutils/TestNetworkTracker.kt
@@ -31,17 +31,35 @@
 import java.util.concurrent.TimeUnit
 
 /**
- * Create a test network based on a TUN interface.
+ * Create a test network based on a TUN interface with a LinkAddress.
  *
  * This method will block until the test network is available. Requires
  * [android.Manifest.permission.CHANGE_NETWORK_STATE] and
  * [android.Manifest.permission.MANAGE_TEST_NETWORKS].
  */
-fun initTestNetwork(context: Context, interfaceAddr: LinkAddress, setupTimeoutMs: Long = 10_000L):
-        TestNetworkTracker {
+fun initTestNetwork(
+    context: Context,
+    interfaceAddr: LinkAddress,
+    setupTimeoutMs: Long = 10_000L
+): TestNetworkTracker {
+    return initTestNetwork(context, listOf(interfaceAddr), setupTimeoutMs)
+}
+
+/**
+ * Create a test network based on a TUN interface with giving LinkAddress list.
+ *
+ * This method will block until the test network is available. Requires
+ * [android.Manifest.permission.CHANGE_NETWORK_STATE] and
+ * [android.Manifest.permission.MANAGE_TEST_NETWORKS].
+ */
+fun initTestNetwork(
+    context: Context,
+    linkAddrs: List<LinkAddress>,
+    setupTimeoutMs: Long = 10_000L
+): TestNetworkTracker {
     val tnm = context.getSystemService(TestNetworkManager::class.java)
-    val iface = if (isAtLeastS()) tnm.createTunInterface(listOf(interfaceAddr))
-            else tnm.createTunInterface(arrayOf(interfaceAddr))
+    val iface = if (isAtLeastS()) tnm.createTunInterface(linkAddrs)
+            else tnm.createTunInterface(linkAddrs.toTypedArray())
     return TestNetworkTracker(context, iface, tnm, setupTimeoutMs)
 }
 
@@ -61,6 +79,7 @@
 
     private val networkCallback: NetworkCallback
     val network: Network
+    val testIface: TestNetworkInterface
 
     init {
         val networkFuture = CompletableFuture<Network>()
@@ -85,6 +104,8 @@
             teardown()
             throw e
         }
+
+        testIface = iface
     }
 
     fun teardown() {