Merge "Move netd AIDL to frameworks/lib/net"
diff --git a/staticlibs/device/com/android/net/module/util/netlink/ConntrackMessage.java b/staticlibs/device/com/android/net/module/util/netlink/ConntrackMessage.java
index 1763c04..dfed3ef 100644
--- a/staticlibs/device/com/android/net/module/util/netlink/ConntrackMessage.java
+++ b/staticlibs/device/com/android/net/module/util/netlink/ConntrackMessage.java
@@ -228,7 +228,9 @@
      * @return the parsed netfilter conntrack message, or {@code null} if the netfilter conntrack
      *         message could not be parsed successfully (for example, if it was truncated).
      */
-    public static ConntrackMessage parse(StructNlMsgHdr header, ByteBuffer byteBuffer) {
+    @Nullable
+    public static ConntrackMessage parse(@NonNull StructNlMsgHdr header,
+            @NonNull ByteBuffer byteBuffer) {
         // Just build the netlink header and netfilter header for now and pretend the whole message
         // was consumed.
         // TODO: Parse the conntrack attributes.
diff --git a/staticlibs/device/com/android/net/module/util/netlink/InetDiagMessage.java b/staticlibs/device/com/android/net/module/util/netlink/InetDiagMessage.java
index 7b200e7..a8aef7b 100644
--- a/staticlibs/device/com/android/net/module/util/netlink/InetDiagMessage.java
+++ b/staticlibs/device/com/android/net/module/util/netlink/InetDiagMessage.java
@@ -31,6 +31,7 @@
 import android.system.ErrnoException;
 import android.util.Log;
 
+import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
 
 import java.io.FileDescriptor;
@@ -107,7 +108,7 @@
 
     public StructInetDiagMsg mStructInetDiagMsg;
 
-    private InetDiagMessage(StructNlMsgHdr header) {
+    private InetDiagMessage(@NonNull StructNlMsgHdr header) {
         super(header);
         mStructInetDiagMsg = new StructInetDiagMsg();
     }
@@ -115,7 +116,9 @@
     /**
      * Parse an inet_diag_req_v2 message from buffer.
      */
-    public static InetDiagMessage parse(StructNlMsgHdr header, ByteBuffer byteBuffer) {
+    @NonNull
+    public static InetDiagMessage parse(@NonNull StructNlMsgHdr header,
+            @NonNull ByteBuffer byteBuffer) {
         final InetDiagMessage msg = new InetDiagMessage(header);
         msg.mStructInetDiagMsg = StructInetDiagMsg.parse(byteBuffer);
         return msg;
diff --git a/staticlibs/device/com/android/net/module/util/netlink/NduseroptMessage.java b/staticlibs/device/com/android/net/module/util/netlink/NduseroptMessage.java
index 9d2402d..bdf574d 100644
--- a/staticlibs/device/com/android/net/module/util/netlink/NduseroptMessage.java
+++ b/staticlibs/device/com/android/net/module/util/netlink/NduseroptMessage.java
@@ -123,6 +123,7 @@
      * @return the parsed option, or {@code null} if the option could not be parsed successfully
      *         (for example, if it was truncated, or if the prefix length code was wrong).
      */
+    @Nullable
     public static NduseroptMessage parse(@NonNull StructNlMsgHdr header, @NonNull ByteBuffer buf) {
         if (buf == null || buf.remaining() < STRUCT_SIZE) return null;
         ByteOrder oldOrder = buf.order();
diff --git a/staticlibs/device/com/android/net/module/util/netlink/NetlinkErrorMessage.java b/staticlibs/device/com/android/net/module/util/netlink/NetlinkErrorMessage.java
index d9fb09e..4831432 100644
--- a/staticlibs/device/com/android/net/module/util/netlink/NetlinkErrorMessage.java
+++ b/staticlibs/device/com/android/net/module/util/netlink/NetlinkErrorMessage.java
@@ -16,6 +16,9 @@
 
 package com.android.net.module.util.netlink;
 
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
 import java.nio.ByteBuffer;
 
 /**
@@ -32,7 +35,9 @@
      * @return the parsed netlink error message, or {@code null} if the netlink error message
      *         could not be parsed successfully (for example, if it was truncated).
      */
-    public static NetlinkErrorMessage parse(StructNlMsgHdr header, ByteBuffer byteBuffer) {
+    @Nullable
+    public static NetlinkErrorMessage parse(@NonNull StructNlMsgHdr header,
+            @NonNull ByteBuffer byteBuffer) {
         final NetlinkErrorMessage errorMsg = new NetlinkErrorMessage(header);
 
         errorMsg.mNlMsgErr = StructNlMsgErr.parse(byteBuffer);
@@ -45,7 +50,7 @@
 
     private StructNlMsgErr mNlMsgErr;
 
-    NetlinkErrorMessage(StructNlMsgHdr header) {
+    NetlinkErrorMessage(@NonNull StructNlMsgHdr header) {
         super(header);
         mNlMsgErr = null;
     }
diff --git a/staticlibs/device/com/android/net/module/util/netlink/NetlinkMessage.java b/staticlibs/device/com/android/net/module/util/netlink/NetlinkMessage.java
index f425384..723d682 100644
--- a/staticlibs/device/com/android/net/module/util/netlink/NetlinkMessage.java
+++ b/staticlibs/device/com/android/net/module/util/netlink/NetlinkMessage.java
@@ -79,12 +79,14 @@
         return null;
     }
 
-    protected StructNlMsgHdr mHeader;
+    @NonNull
+    protected final StructNlMsgHdr mHeader;
 
-    public NetlinkMessage(StructNlMsgHdr nlmsghdr) {
+    public NetlinkMessage(@NonNull StructNlMsgHdr nlmsghdr) {
         mHeader = nlmsghdr;
     }
 
+    @NonNull
     public StructNlMsgHdr getHeader() {
         return mHeader;
     }
@@ -96,7 +98,7 @@
         // a string by StructNlMsgHdr#toString and just keep as an integer. The specific message
         // which inherits NetlinkMessage could override NetlinkMessage#toString and provide the
         // specific netlink family to StructNlMsgHdr#toString.
-        return "NetlinkMessage{" + (mHeader == null ? "" : mHeader.toString()) + "}";
+        return "NetlinkMessage{" + mHeader.toString() + "}";
     }
 
     @NonNull
diff --git a/staticlibs/device/com/android/net/module/util/netlink/RtNetlinkNeighborMessage.java b/staticlibs/device/com/android/net/module/util/netlink/RtNetlinkNeighborMessage.java
index a75ef8d..4a09015 100644
--- a/staticlibs/device/com/android/net/module/util/netlink/RtNetlinkNeighborMessage.java
+++ b/staticlibs/device/com/android/net/module/util/netlink/RtNetlinkNeighborMessage.java
@@ -23,6 +23,9 @@
 
 import android.system.OsConstants;
 
+import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
+
 import java.net.Inet6Address;
 import java.net.InetAddress;
 import java.nio.ByteBuffer;
@@ -53,7 +56,9 @@
      * @param header netlink message header.
      * @param byteBuffer the ByteBuffer instance that wraps the raw netlink message bytes.
      */
-    public static RtNetlinkNeighborMessage parse(StructNlMsgHdr header, ByteBuffer byteBuffer) {
+    @Nullable
+    public static RtNetlinkNeighborMessage parse(@NonNull StructNlMsgHdr header,
+            @NonNull ByteBuffer byteBuffer) {
         final RtNetlinkNeighborMessage neighMsg = new RtNetlinkNeighborMessage(header);
 
         neighMsg.mNdmsg = StructNdMsg.parse(byteBuffer);
@@ -154,7 +159,7 @@
     private int mNumProbes;
     private StructNdaCacheInfo mCacheInfo;
 
-    private RtNetlinkNeighborMessage(StructNlMsgHdr header) {
+    private RtNetlinkNeighborMessage(@NonNull StructNlMsgHdr header) {
         super(header);
         mNdmsg = null;
         mDestination = null;
diff --git a/staticlibs/device/com/android/net/module/util/netlink/StructNlMsgHdr.java b/staticlibs/device/com/android/net/module/util/netlink/StructNlMsgHdr.java
index ddf1562..9567cce 100644
--- a/staticlibs/device/com/android/net/module/util/netlink/StructNlMsgHdr.java
+++ b/staticlibs/device/com/android/net/module/util/netlink/StructNlMsgHdr.java
@@ -93,7 +93,8 @@
     /**
      * Parse netlink message header from buffer.
      */
-    public static StructNlMsgHdr parse(ByteBuffer byteBuffer) {
+    @Nullable
+    public static StructNlMsgHdr parse(@NonNull ByteBuffer byteBuffer) {
         if (!hasAvailableSpace(byteBuffer)) return null;
 
         // The ByteOrder must have already been set by the caller.  In most
diff --git a/staticlibs/tests/unit/src/android/net/util/NetUtilsTest.java b/staticlibs/tests/unit/src/android/net/util/NetUtilsTest.java
index 338d1c8..d523e14 100644
--- a/staticlibs/tests/unit/src/android/net/util/NetUtilsTest.java
+++ b/staticlibs/tests/unit/src/android/net/util/NetUtilsTest.java
@@ -71,18 +71,18 @@
         route = NetUtils.selectBestRoute(routes, v4_dest);
         assertEquals(null, route);
 
-        final RouteInfo v4_expected = new RouteInfo(new IpPrefix("75.208.8.10/24"),
+        final RouteInfo v4_expected = new RouteInfo(new IpPrefix("75.208.8.0/24"),
                 V4_GATEWAY, "wlan0");
         routes.add(v4_expected);
-        // "75.208.8.10/16" is not an expected result since it is not the longest prefix.
-        routes.add(new RouteInfo(new IpPrefix("75.208.8.10/16"), V4_GATEWAY, "wlan0"));
-        routes.add(new RouteInfo(new IpPrefix("75.208.7.32/24"), V4_GATEWAY, "wlan0"));
+        // "75.208.0.0/16" is not an expected result since it is not the longest prefix.
+        routes.add(new RouteInfo(new IpPrefix("75.208.0.0/16"), V4_GATEWAY, "wlan0"));
+        routes.add(new RouteInfo(new IpPrefix("75.208.7.0/24"), V4_GATEWAY, "wlan0"));
 
         final RouteInfo v6_expected = new RouteInfo(new IpPrefix("2001:db8:cafe::/64"),
                 V6_GATEWAY, "wlan0");
         routes.add(v6_expected);
-        // "2001:db8:cafe::123/32" is not an expected result since it is not the longest prefix.
-        routes.add(new RouteInfo(new IpPrefix("2001:db8:cafe::123/32"), V6_GATEWAY, "wlan0"));
+        // "2001:db8::/32" is not an expected result since it is not the longest prefix.
+        routes.add(new RouteInfo(new IpPrefix("2001:db8::/32"), V6_GATEWAY, "wlan0"));
         routes.add(new RouteInfo(new IpPrefix("2001:db8:beef::/64"), V6_GATEWAY, "wlan0"));
 
         // Verify expected v4 route is selected
diff --git a/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkStatsProvider.kt b/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkStatsProvider.kt
index d034a7d..be5c9b2 100644
--- a/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkStatsProvider.kt
+++ b/staticlibs/testutils/devicetests/com/android/testutils/TestableNetworkStatsProvider.kt
@@ -30,7 +30,16 @@
 ) : NetworkStatsProvider() {
     sealed class CallbackType {
         data class OnRequestStatsUpdate(val token: Int) : CallbackType()
-        data class OnSetLimit(val iface: String?, val quotaBytes: Long) : CallbackType()
+        data class OnSetWarningAndLimit(
+            val iface: String,
+            val warningBytes: Long,
+            val limitBytes: Long
+        ) : CallbackType()
+        data class OnSetLimit(val iface: String, val limitBytes: Long) : CallbackType() {
+            // Add getter for backward compatibility since old tests do not recognize limitBytes.
+            val quotaBytes: Long
+                get() = limitBytes
+        }
         data class OnSetAlert(val quotaBytes: Long) : CallbackType()
     }
 
@@ -42,6 +51,10 @@
         history.add(CallbackType.OnRequestStatsUpdate(token))
     }
 
+    override fun onSetWarningAndLimit(iface: String, warningBytes: Long, limitBytes: Long) {
+        history.add(CallbackType.OnSetWarningAndLimit(iface, warningBytes, limitBytes))
+    }
+
     override fun onSetLimit(iface: String, quotaBytes: Long) {
         history.add(CallbackType.OnSetLimit(iface, quotaBytes))
     }
@@ -56,10 +69,10 @@
         if (token != TOKEN_ANY) {
             assertEquals(token, event.token)
         }
-        return token
+        return event.token
     }
 
-    fun expectOnSetLimit(iface: String?, quotaBytes: Long, timeout: Long = defaultTimeoutMs) {
+    fun expectOnSetLimit(iface: String, quotaBytes: Long, timeout: Long = defaultTimeoutMs) {
         assertEquals(CallbackType.OnSetLimit(iface, quotaBytes), history.poll(timeout))
     }