From 4dfc6da55f224d75896aa72ec8f146adfacbd42f Mon Sep 17 00:00:00 2001 From: Xiao Ma Date: Fri, 2 Feb 2024 12:56:05 +0900 Subject: [PATCH 1/3] Make NduseroptMessage constructor visible for testing. Refactor IpClientTest by parsing the netlink message directly instead of netd callbacks, so we have to initialize an NduseroptMessage instance to trigger IPv6 RDNSS option update in the unit test. Bug: 318764217 Test: atest NetworkStackTests Change-Id: Ic2e8ca0d5e71b73c0dbdc3311e72486af371c899 --- .../net/module/util/netlink/NduseroptMessage.java | 15 +++++++++++++++ 1 file changed, 15 insertions(+) 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 bdf574db89..2e9a99bb06 100644 --- a/staticlibs/device/com/android/net/module/util/netlink/NduseroptMessage.java +++ b/staticlibs/device/com/android/net/module/util/netlink/NduseroptMessage.java @@ -20,6 +20,7 @@ import static android.system.OsConstants.AF_INET6; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; import java.net.Inet6Address; import java.net.InetAddress; @@ -63,6 +64,20 @@ public class NduseroptMessage extends NetlinkMessage { /** The IP address that sent the packet containing the option. */ public final InetAddress srcaddr; + @VisibleForTesting + public NduseroptMessage(@NonNull final StructNlMsgHdr header, byte family, int optslen, + int ifindex, byte icmptype, byte icmpcode, @NonNull final NdOption option, + final InetAddress srcaddr) { + super(header); + this.family = family; + this.opts_len = optslen; + this.ifindex = ifindex; + this.icmp_type = icmptype; + this.icmp_code = icmpcode; + this.option = option; + this.srcaddr = srcaddr; + } + NduseroptMessage(@NonNull StructNlMsgHdr header, @NonNull ByteBuffer buf) throws UnknownHostException { super(header); From 2040d8bcc84ddb748ed13fdf1706304c8807c1f6 Mon Sep 17 00:00:00 2001 From: Xiao Ma Date: Fri, 2 Feb 2024 13:12:38 +0900 Subject: [PATCH 2/3] Make StructRtMsg constructor visible for testing. Refactor IpClientTest by parsing the netlink message directly instead of netd callbacks, so we have to initialize a StructRtMsg instance to fill in the RtNetlinkRouteMessage constructor. Bug: 318764217 Test: atest NetworkStackTests Change-Id: I1481cb8fab29ae94d48fa342ecf2b303a9701a31 --- .../com/android/net/module/util/netlink/StructRtMsg.java | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/staticlibs/device/com/android/net/module/util/netlink/StructRtMsg.java b/staticlibs/device/com/android/net/module/util/netlink/StructRtMsg.java index 3cd729222b..6d9318c81e 100644 --- a/staticlibs/device/com/android/net/module/util/netlink/StructRtMsg.java +++ b/staticlibs/device/com/android/net/module/util/netlink/StructRtMsg.java @@ -18,6 +18,7 @@ package com.android.net.module.util.netlink; import androidx.annotation.NonNull; import androidx.annotation.Nullable; +import androidx.annotation.VisibleForTesting; import com.android.net.module.util.Struct; import com.android.net.module.util.Struct.Field; @@ -57,8 +58,9 @@ public class StructRtMsg extends Struct { @Field(order = 8, type = Type.U32) public final long flags; - StructRtMsg(short family, short dstLen, short srcLen, short tos, short table, short protocol, - short scope, short type, long flags) { + @VisibleForTesting + public StructRtMsg(short family, short dstLen, short srcLen, short tos, short table, + short protocol, short scope, short type, long flags) { this.family = family; this.dstLen = dstLen; this.srcLen = srcLen; From f505ef047969e7831c4dec9e85cea85e4e76addb Mon Sep 17 00:00:00 2001 From: Xiao Ma Date: Fri, 2 Feb 2024 13:16:53 +0900 Subject: [PATCH 3/3] Make RtNetlinkRouteMessage constructor visible for testing. Refactor IpClientTest by parsing the netlink message directly instead of netd callbacks, so we have to initialize a RtNetlinkRouteMessage instance to trigger IPv4/v6 route updates in the unit test. Bug: 318764217 Test: atest NetworkStackTests Change-Id: Icfcc7ade636c89a63d897f4e9246410f6387b3c9 --- .../util/netlink/RtNetlinkRouteMessage.java | 28 +++++++++++-------- 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/staticlibs/device/com/android/net/module/util/netlink/RtNetlinkRouteMessage.java b/staticlibs/device/com/android/net/module/util/netlink/RtNetlinkRouteMessage.java index b2b1e9302a..545afeaf6c 100644 --- a/staticlibs/device/com/android/net/module/util/netlink/RtNetlinkRouteMessage.java +++ b/staticlibs/device/com/android/net/module/util/netlink/RtNetlinkRouteMessage.java @@ -19,10 +19,8 @@ package com.android.net.module.util.netlink; import static android.system.OsConstants.AF_INET; import static android.system.OsConstants.AF_INET6; -import static android.system.OsConstants.NETLINK_ROUTE; import static com.android.net.module.util.NetworkStackConstants.IPV4_ADDR_ANY; import static com.android.net.module.util.NetworkStackConstants.IPV6_ADDR_ANY; -import static com.android.net.module.util.netlink.NetlinkConstants.hexify; import static com.android.net.module.util.netlink.NetlinkConstants.RTNL_FAMILY_IP6MR; import android.annotation.SuppressLint; @@ -38,9 +36,6 @@ import java.net.Inet4Address; import java.net.Inet6Address; import java.net.InetAddress; import java.nio.ByteBuffer; -import java.nio.ByteOrder; -import java.nio.IntBuffer; -import java.util.Arrays; /** * A NetlinkMessage subclass for rtnetlink route messages. @@ -86,18 +81,27 @@ public class RtNetlinkRouteMessage extends NetlinkMessage { private long mSinceLastUseMillis; // Milliseconds since the route was used, // for resolved multicast routes - public RtNetlinkRouteMessage(StructNlMsgHdr header, StructRtMsg rtMsg) { + + @VisibleForTesting + public RtNetlinkRouteMessage(final StructNlMsgHdr header, final StructRtMsg rtMsg, + final IpPrefix source, final IpPrefix destination, final InetAddress gateway, + int iif, int oif, final StructRtaCacheInfo cacheInfo) { super(header); mRtmsg = rtMsg; - mSource = null; - mDestination = null; - mGateway = null; - mIifIndex = 0; - mOifIndex = 0; - mRtaCacheInfo = null; + mSource = source; + mDestination = destination; + mGateway = gateway; + mIifIndex = iif; + mOifIndex = oif; + mRtaCacheInfo = cacheInfo; mSinceLastUseMillis = -1; } + public RtNetlinkRouteMessage(StructNlMsgHdr header, StructRtMsg rtMsg) { + this(header, rtMsg, null /* source */, null /* destination */, null /* gateway */, + 0 /* iif */, 0 /* oif */, null /* cacheInfo */); + } + /** * Returns the rtnetlink family. */