From 7514e1b8bb98edc8ab50efbd3b8270d4e636f2fa Mon Sep 17 00:00:00 2001 From: junyulai Date: Tue, 29 Dec 2020 19:17:01 +0800 Subject: [PATCH 1/2] [FUI06] Stop using NetworkInfo in NetworkState from external callers This is achieved by: 1. Use legacy network type inside NetworkState to replace the needs of referencing NetworkInfo.getType(). 2. Let getAllNetworkState only return networks with isConnected() equals true. This allows callers such as NPMS or NSS does not have to reference to NetworkInfo.isConnected(). Test: atest FrameworksNetTests NetworkPolicyManagerServiceTest Bug: 174123988 Change-Id: I1c4eb08d18ca973eb8f41d06258872eabc0006b8 --- core/java/android/net/NetworkIdentity.java | 8 +- core/java/android/net/NetworkState.java | 6 + .../android/server/ConnectivityService.java | 3 +- .../net/NetworkPolicyManagerService.java | 18 +-- .../server/net/NetworkStatsService.java | 143 +++++++++--------- .../net/NetworkPolicyManagerServiceTest.java | 2 + .../java/android/net/NetworkTemplateTest.kt | 1 - 7 files changed, 92 insertions(+), 89 deletions(-) diff --git a/core/java/android/net/NetworkIdentity.java b/core/java/android/net/NetworkIdentity.java index 521ed9b1946ce..b4aed6f96387e 100644 --- a/core/java/android/net/NetworkIdentity.java +++ b/core/java/android/net/NetworkIdentity.java @@ -31,7 +31,7 @@ import java.util.Objects; /** * Network definition that includes strong identity. Analogous to combining - * {@link NetworkInfo} and an IMSI. + * {@link NetworkCapabilities} and an IMSI. * * @hide */ @@ -159,7 +159,7 @@ public class NetworkIdentity implements Comparable { */ public static NetworkIdentity buildNetworkIdentity(Context context, NetworkState state, boolean defaultNetwork, @NetworkType int subType) { - final int type = state.networkInfo.getType(); + final int legacyType = state.legacyNetworkType; String subscriberId = null; String networkId = null; @@ -170,7 +170,7 @@ public class NetworkIdentity implements Comparable { subscriberId = state.subscriberId; - if (type == TYPE_WIFI) { + if (legacyType == TYPE_WIFI) { if (state.networkCapabilities.getSsid() != null) { networkId = state.networkCapabilities.getSsid(); if (networkId == null) { @@ -183,7 +183,7 @@ public class NetworkIdentity implements Comparable { } } - return new NetworkIdentity(type, subType, subscriberId, networkId, roaming, metered, + return new NetworkIdentity(legacyType, subType, subscriberId, networkId, roaming, metered, defaultNetwork); } diff --git a/core/java/android/net/NetworkState.java b/core/java/android/net/NetworkState.java index e1ef8b5ea5c92..2d6ddcbb5dd78 100644 --- a/core/java/android/net/NetworkState.java +++ b/core/java/android/net/NetworkState.java @@ -41,6 +41,7 @@ public class NetworkState implements Parcelable { public final Network network; public final String subscriberId; public final String networkId; + public final int legacyNetworkType; private NetworkState() { networkInfo = null; @@ -49,6 +50,7 @@ public class NetworkState implements Parcelable { network = null; subscriberId = null; networkId = null; + legacyNetworkType = 0; } public NetworkState(@NonNull NetworkInfo networkInfo, @NonNull LinkProperties linkProperties, @@ -60,6 +62,8 @@ public class NetworkState implements Parcelable { this.network = network; this.subscriberId = subscriberId; this.networkId = networkId; + // TODO: Pass legacyNetworkType directly from parameters and remove NetworkInfo. + this.legacyNetworkType = this.networkInfo.getType(); // This object is an atomic view of a network, so the various components // should always agree on roaming state. @@ -80,6 +84,7 @@ public class NetworkState implements Parcelable { network = in.readParcelable(null); subscriberId = in.readString(); networkId = in.readString(); + legacyNetworkType = in.readInt(); } @Override @@ -95,6 +100,7 @@ public class NetworkState implements Parcelable { out.writeParcelable(network, flags); out.writeString(subscriberId); out.writeString(networkId); + out.writeInt(legacyNetworkType); } @UnsupportedAppUsage diff --git a/services/core/java/com/android/server/ConnectivityService.java b/services/core/java/com/android/server/ConnectivityService.java index 96c3e573a8f5e..8ad51f24ff198 100644 --- a/services/core/java/com/android/server/ConnectivityService.java +++ b/services/core/java/com/android/server/ConnectivityService.java @@ -1854,7 +1854,8 @@ public class ConnectivityService extends IConnectivityManager.Stub final ArrayList result = new ArrayList<>(); for (Network network : getAllNetworks()) { final NetworkAgentInfo nai = getNetworkAgentInfoForNetwork(network); - if (nai != null) { + // TODO: Consider include SUSPENDED networks. + if (nai != null && nai.networkInfo.isConnected()) { // TODO (b/73321673) : NetworkState contains a copy of the // NetworkCapabilities, which may contain UIDs of apps to which the // network applies. Should the UIDs be cleared so as not to leak or diff --git a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java index 0fd39a452b04b..c218181562d20 100644 --- a/services/core/java/com/android/server/net/NetworkPolicyManagerService.java +++ b/services/core/java/com/android/server/net/NetworkPolicyManagerService.java @@ -1960,14 +1960,13 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { if (state.network != null) { mNetIdToSubId.put(state.network.netId, parseSubId(state)); } - if (state.networkInfo != null && state.networkInfo.isConnected()) { - // Policies matched by NPMS only match by subscriber ID or by ssid. Thus subtype - // in the object created here is never used and its value doesn't matter, so use - // NETWORK_TYPE_UNKNOWN. - final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state, - true, TelephonyManager.NETWORK_TYPE_UNKNOWN /* subType */); - identified.put(state, ident); - } + + // Policies matched by NPMS only match by subscriber ID or by ssid. Thus subtype + // in the object created here is never used and its value doesn't matter, so use + // NETWORK_TYPE_UNKNOWN. + final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state, + true, TelephonyManager.NETWORK_TYPE_UNKNOWN /* subType */); + identified.put(state, ident); } final ArraySet newMeteredIfaces = new ArraySet<>(); @@ -2042,8 +2041,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub { // One final pass to catch any metered ifaces that don't have explicitly // defined policies; typically Wi-Fi networks. for (NetworkState state : states) { - if (state.networkInfo != null && state.networkInfo.isConnected() - && !state.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)) { + if (!state.networkCapabilities.hasCapability(NET_CAPABILITY_NOT_METERED)) { matchingIfaces.clear(); collectIfaces(matchingIfaces, state); for (int j = matchingIfaces.size() - 1; j >= 0; j--) { diff --git a/services/core/java/com/android/server/net/NetworkStatsService.java b/services/core/java/com/android/server/net/NetworkStatsService.java index 0ab35a911025e..9706bcece924b 100644 --- a/services/core/java/com/android/server/net/NetworkStatsService.java +++ b/services/core/java/com/android/server/net/NetworkStatsService.java @@ -96,7 +96,6 @@ import android.net.LinkProperties; import android.net.Network; import android.net.NetworkCapabilities; import android.net.NetworkIdentity; -import android.net.NetworkInfo; import android.net.NetworkStack; import android.net.NetworkState; import android.net.NetworkStats; @@ -1264,7 +1263,7 @@ public class NetworkStatsService extends INetworkStatsService.Stub { /** * Inspect all current {@link NetworkState} to derive mapping from {@code iface} to {@link - * NetworkStatsHistory}. When multiple {@link NetworkInfo} are active on a single {@code iface}, + * NetworkStatsHistory}. When multiple networks are active on a single {@code iface}, * they are combined under a single {@link NetworkIdentitySet}. */ @GuardedBy("mStatsLock") @@ -1294,84 +1293,82 @@ public class NetworkStatsService extends INetworkStatsService.Stub { final boolean combineSubtypeEnabled = mSettings.getCombineSubtypeEnabled(); final ArraySet mobileIfaces = new ArraySet<>(); for (NetworkState state : states) { - if (state.networkInfo.isConnected()) { - final boolean isMobile = isNetworkTypeMobile(state.networkInfo.getType()); - final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, state.network); - final int subType = combineSubtypeEnabled ? SUBTYPE_COMBINED - : getSubTypeForState(state); - final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state, - isDefault, subType); + final boolean isMobile = isNetworkTypeMobile(state.legacyNetworkType); + final boolean isDefault = ArrayUtils.contains(mDefaultNetworks, state.network); + final int subType = combineSubtypeEnabled ? SUBTYPE_COMBINED + : getSubTypeForState(state); + final NetworkIdentity ident = NetworkIdentity.buildNetworkIdentity(mContext, state, + isDefault, subType); - // Traffic occurring on the base interface is always counted for - // both total usage and UID details. - final String baseIface = state.linkProperties.getInterfaceName(); - if (baseIface != null) { - findOrCreateNetworkIdentitySet(mActiveIfaces, baseIface).add(ident); - findOrCreateNetworkIdentitySet(mActiveUidIfaces, baseIface).add(ident); + // Traffic occurring on the base interface is always counted for + // both total usage and UID details. + final String baseIface = state.linkProperties.getInterfaceName(); + if (baseIface != null) { + findOrCreateNetworkIdentitySet(mActiveIfaces, baseIface).add(ident); + findOrCreateNetworkIdentitySet(mActiveUidIfaces, baseIface).add(ident); - // Build a separate virtual interface for VT (Video Telephony) data usage. - // Only do this when IMS is not metered, but VT is metered. - // If IMS is metered, then the IMS network usage has already included VT usage. - // VT is considered always metered in framework's layer. If VT is not metered - // per carrier's policy, modem will report 0 usage for VT calls. - if (state.networkCapabilities.hasCapability( - NetworkCapabilities.NET_CAPABILITY_IMS) && !ident.getMetered()) { + // Build a separate virtual interface for VT (Video Telephony) data usage. + // Only do this when IMS is not metered, but VT is metered. + // If IMS is metered, then the IMS network usage has already included VT usage. + // VT is considered always metered in framework's layer. If VT is not metered + // per carrier's policy, modem will report 0 usage for VT calls. + if (state.networkCapabilities.hasCapability( + NetworkCapabilities.NET_CAPABILITY_IMS) && !ident.getMetered()) { - // Copy the identify from IMS one but mark it as metered. - NetworkIdentity vtIdent = new NetworkIdentity(ident.getType(), - ident.getSubType(), ident.getSubscriberId(), ident.getNetworkId(), - ident.getRoaming(), true /* metered */, - true /* onDefaultNetwork */); - findOrCreateNetworkIdentitySet(mActiveIfaces, IFACE_VT).add(vtIdent); - findOrCreateNetworkIdentitySet(mActiveUidIfaces, IFACE_VT).add(vtIdent); - } - - if (isMobile) { - mobileIfaces.add(baseIface); - } + // Copy the identify from IMS one but mark it as metered. + NetworkIdentity vtIdent = new NetworkIdentity(ident.getType(), + ident.getSubType(), ident.getSubscriberId(), ident.getNetworkId(), + ident.getRoaming(), true /* metered */, + true /* onDefaultNetwork */); + findOrCreateNetworkIdentitySet(mActiveIfaces, IFACE_VT).add(vtIdent); + findOrCreateNetworkIdentitySet(mActiveUidIfaces, IFACE_VT).add(vtIdent); } - // Traffic occurring on stacked interfaces is usually clatd. - // - // UID stats are always counted on the stacked interface and never on the base - // interface, because the packets on the base interface do not actually match - // application sockets (they're not IPv4) and thus the app uid is not known. - // For receive this is obvious: packets must be translated from IPv6 to IPv4 - // before the application socket can be found. - // For transmit: either they go through the clat daemon which by virtue of going - // through userspace strips the original socket association during the IPv4 to - // IPv6 translation process, or they are offloaded by eBPF, which doesn't: - // However, on an ebpf device the accounting is done in cgroup ebpf hooks, - // which don't trigger again post ebpf translation. - // (as such stats accounted to the clat uid are ignored) - // - // Interface stats are more complicated. - // - // eBPF offloaded 464xlat'ed packets never hit base interface ip6tables, and thus - // *all* statistics are collected by iptables on the stacked v4-* interface. - // - // Additionally for ingress all packets bound for the clat IPv6 address are dropped - // in ip6tables raw prerouting and thus even non-offloaded packets are only - // accounted for on the stacked interface. - // - // For egress, packets subject to eBPF offload never appear on the base interface - // and only appear on the stacked interface. Thus to ensure packets increment - // interface stats, we must collate data from stacked interfaces. For xt_qtaguid - // (or non eBPF offloaded) TX they would appear on both, however egress interface - // accounting is explicitly bypassed for traffic from the clat uid. - // - final List stackedLinks = state.linkProperties.getStackedLinks(); - for (LinkProperties stackedLink : stackedLinks) { - final String stackedIface = stackedLink.getInterfaceName(); - if (stackedIface != null) { - findOrCreateNetworkIdentitySet(mActiveIfaces, stackedIface).add(ident); - findOrCreateNetworkIdentitySet(mActiveUidIfaces, stackedIface).add(ident); - if (isMobile) { - mobileIfaces.add(stackedIface); - } + if (isMobile) { + mobileIfaces.add(baseIface); + } + } - mStatsFactory.noteStackedIface(stackedIface, baseIface); + // Traffic occurring on stacked interfaces is usually clatd. + // + // UID stats are always counted on the stacked interface and never on the base + // interface, because the packets on the base interface do not actually match + // application sockets (they're not IPv4) and thus the app uid is not known. + // For receive this is obvious: packets must be translated from IPv6 to IPv4 + // before the application socket can be found. + // For transmit: either they go through the clat daemon which by virtue of going + // through userspace strips the original socket association during the IPv4 to + // IPv6 translation process, or they are offloaded by eBPF, which doesn't: + // However, on an ebpf device the accounting is done in cgroup ebpf hooks, + // which don't trigger again post ebpf translation. + // (as such stats accounted to the clat uid are ignored) + // + // Interface stats are more complicated. + // + // eBPF offloaded 464xlat'ed packets never hit base interface ip6tables, and thus + // *all* statistics are collected by iptables on the stacked v4-* interface. + // + // Additionally for ingress all packets bound for the clat IPv6 address are dropped + // in ip6tables raw prerouting and thus even non-offloaded packets are only + // accounted for on the stacked interface. + // + // For egress, packets subject to eBPF offload never appear on the base interface + // and only appear on the stacked interface. Thus to ensure packets increment + // interface stats, we must collate data from stacked interfaces. For xt_qtaguid + // (or non eBPF offloaded) TX they would appear on both, however egress interface + // accounting is explicitly bypassed for traffic from the clat uid. + // + final List stackedLinks = state.linkProperties.getStackedLinks(); + for (LinkProperties stackedLink : stackedLinks) { + final String stackedIface = stackedLink.getInterfaceName(); + if (stackedIface != null) { + findOrCreateNetworkIdentitySet(mActiveIfaces, stackedIface).add(ident); + findOrCreateNetworkIdentitySet(mActiveUidIfaces, stackedIface).add(ident); + if (isMobile) { + mobileIfaces.add(stackedIface); } + + mStatsFactory.noteStackedIface(stackedIface, baseIface); } } } diff --git a/services/tests/servicestests/src/com/android/server/net/NetworkPolicyManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/net/NetworkPolicyManagerServiceTest.java index df19aeb137076..46eb37cc3806b 100644 --- a/services/tests/servicestests/src/com/android/server/net/NetworkPolicyManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/net/NetworkPolicyManagerServiceTest.java @@ -24,6 +24,7 @@ import static android.net.INetd.FIREWALL_CHAIN_RESTRICTED; import static android.net.INetd.FIREWALL_RULE_ALLOW; import static android.net.NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING; import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; +import static android.net.NetworkCapabilities.TRANSPORT_WIFI; import static android.net.NetworkPolicy.LIMIT_DISABLED; import static android.net.NetworkPolicy.SNOOZE_NEVER; import static android.net.NetworkPolicy.WARNING_DISABLED; @@ -2051,6 +2052,7 @@ public class NetworkPolicyManagerServiceTest { final LinkProperties prop = new LinkProperties(); prop.setInterfaceName(TEST_IFACE); final NetworkCapabilities networkCapabilities = new NetworkCapabilities(); + networkCapabilities.addTransportType(TRANSPORT_WIFI); networkCapabilities.setSSID(TEST_SSID); return new NetworkState(info, prop, networkCapabilities, null, null, TEST_SSID); } diff --git a/tests/net/java/android/net/NetworkTemplateTest.kt b/tests/net/java/android/net/NetworkTemplateTest.kt index 91fcbc0fd5d78..6f1bcc3bebaf4 100644 --- a/tests/net/java/android/net/NetworkTemplateTest.kt +++ b/tests/net/java/android/net/NetworkTemplateTest.kt @@ -62,7 +62,6 @@ class NetworkTemplateTest { ): NetworkState { val info = mock(NetworkInfo::class.java) doReturn(type).`when`(info).type - doReturn(NetworkInfo.State.CONNECTED).`when`(info).state val lp = LinkProperties() val caps = NetworkCapabilities().apply { setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false) From ed813ed7673a309314dc01447d8ece826b3045e3 Mon Sep 17 00:00:00 2001 From: junyulai Date: Wed, 30 Dec 2020 19:33:45 +0800 Subject: [PATCH 2/2] [FUI07] Stop making NetworkState with NetworkInfo from external callers Follow-up from previous patch. This change stop accessing the constructor that needs NetworkInfo to create NetworkState, which is no longer accessible after ConnectivityService become mainline module. Instead, pass a legacy network type which is dedicated for the need of the type. Test: atest FrameworksNetTests NetworkPolicyManagerServiceTest Bug: 174123988 Change-Id: I24157bc33e5a5819eccd6a3111d2049f531c1d43 --- core/java/android/net/NetworkState.java | 20 +++++++++++++++++-- .../net/NetworkPolicyManagerServiceTest.java | 16 +++------------ .../java/android/net/NetworkTemplateTest.kt | 5 +---- .../server/net/NetworkStatsServiceTest.java | 17 ++++------------ 4 files changed, 26 insertions(+), 32 deletions(-) diff --git a/core/java/android/net/NetworkState.java b/core/java/android/net/NetworkState.java index 2d6ddcbb5dd78..e466d2e626bec 100644 --- a/core/java/android/net/NetworkState.java +++ b/core/java/android/net/NetworkState.java @@ -17,6 +17,7 @@ package android.net; import android.annotation.NonNull; +import android.annotation.Nullable; import android.compat.annotation.UnsupportedAppUsage; import android.os.Build; import android.os.Parcel; @@ -53,17 +54,32 @@ public class NetworkState implements Parcelable { legacyNetworkType = 0; } + public NetworkState(int legacyNetworkType, @NonNull LinkProperties linkProperties, + @NonNull NetworkCapabilities networkCapabilities, @NonNull Network network, + @Nullable String subscriberId, @Nullable String networkId) { + this(legacyNetworkType, new NetworkInfo(legacyNetworkType, 0, null, null), linkProperties, + networkCapabilities, network, subscriberId, networkId); + } + + // Constructor that used internally in ConnectivityService mainline module. public NetworkState(@NonNull NetworkInfo networkInfo, @NonNull LinkProperties linkProperties, @NonNull NetworkCapabilities networkCapabilities, @NonNull Network network, String subscriberId, String networkId) { + this(networkInfo.getType(), networkInfo, linkProperties, + networkCapabilities, network, subscriberId, networkId); + } + + public NetworkState(int legacyNetworkType, @NonNull NetworkInfo networkInfo, + @NonNull LinkProperties linkProperties, + @NonNull NetworkCapabilities networkCapabilities, @NonNull Network network, + String subscriberId, String networkId) { this.networkInfo = networkInfo; this.linkProperties = linkProperties; this.networkCapabilities = networkCapabilities; this.network = network; this.subscriberId = subscriberId; this.networkId = networkId; - // TODO: Pass legacyNetworkType directly from parameters and remove NetworkInfo. - this.legacyNetworkType = this.networkInfo.getType(); + this.legacyNetworkType = legacyNetworkType; // This object is an atomic view of a network, so the various components // should always agree on roaming state. diff --git a/services/tests/servicestests/src/com/android/server/net/NetworkPolicyManagerServiceTest.java b/services/tests/servicestests/src/com/android/server/net/NetworkPolicyManagerServiceTest.java index 46eb37cc3806b..deecd380d649b 100644 --- a/services/tests/servicestests/src/com/android/server/net/NetworkPolicyManagerServiceTest.java +++ b/services/tests/servicestests/src/com/android/server/net/NetworkPolicyManagerServiceTest.java @@ -19,6 +19,7 @@ package com.android.server.net; import static android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS; import static android.Manifest.permission.NETWORK_STACK; import static android.net.ConnectivityManager.CONNECTIVITY_ACTION; +import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.INetd.FIREWALL_CHAIN_RESTRICTED; import static android.net.INetd.FIREWALL_RULE_ALLOW; @@ -113,8 +114,6 @@ import android.net.INetworkPolicyListener; import android.net.LinkProperties; import android.net.Network; import android.net.NetworkCapabilities; -import android.net.NetworkInfo; -import android.net.NetworkInfo.DetailedState; import android.net.NetworkPolicy; import android.net.NetworkPolicyManager; import android.net.NetworkState; @@ -1987,13 +1986,6 @@ public class NetworkPolicyManagerServiceTest { return users; } - private NetworkInfo buildNetworkInfo() { - final NetworkInfo ni = new NetworkInfo(ConnectivityManager.TYPE_MOBILE, - TelephonyManager.NETWORK_TYPE_LTE, null, null); - ni.setDetailedState(NetworkInfo.DetailedState.CONNECTED, null, null); - return ni; - } - private LinkProperties buildLinkProperties(String iface) { final LinkProperties lp = new LinkProperties(); lp.setInterfaceName(iface); @@ -2047,14 +2039,12 @@ public class NetworkPolicyManagerServiceTest { } private static NetworkState buildWifi() { - final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); - info.setDetailedState(DetailedState.CONNECTED, null, null); final LinkProperties prop = new LinkProperties(); prop.setInterfaceName(TEST_IFACE); final NetworkCapabilities networkCapabilities = new NetworkCapabilities(); networkCapabilities.addTransportType(TRANSPORT_WIFI); networkCapabilities.setSSID(TEST_SSID); - return new NetworkState(info, prop, networkCapabilities, null, null, TEST_SSID); + return new NetworkState(TYPE_WIFI, prop, networkCapabilities, null, null, TEST_SSID); } private void expectHasInternetPermission(int uid, boolean hasIt) throws Exception { @@ -2075,7 +2065,7 @@ public class NetworkPolicyManagerServiceTest { when(mCarrierConfigManager.getConfigForSubId(eq(TEST_SUB_ID))) .thenReturn(mCarrierConfig); when(mConnManager.getAllNetworkState()).thenReturn(new NetworkState[] { - new NetworkState(buildNetworkInfo(), + new NetworkState(TYPE_MOBILE, buildLinkProperties(TEST_IFACE), buildNetworkCapabilities(TEST_SUB_ID, roaming), new Network(TEST_NET_ID), TEST_IMSI, null) diff --git a/tests/net/java/android/net/NetworkTemplateTest.kt b/tests/net/java/android/net/NetworkTemplateTest.kt index 6f1bcc3bebaf4..1f8f6f311069f 100644 --- a/tests/net/java/android/net/NetworkTemplateTest.kt +++ b/tests/net/java/android/net/NetworkTemplateTest.kt @@ -35,7 +35,6 @@ import org.junit.Before import org.junit.Test import org.junit.runner.RunWith import org.junit.runners.JUnit4 -import org.mockito.Mockito.doReturn import org.mockito.Mockito.mock import org.mockito.MockitoAnnotations import kotlin.test.assertFalse @@ -60,15 +59,13 @@ class NetworkTemplateTest { subscriberId: String? = null, ssid: String? = null ): NetworkState { - val info = mock(NetworkInfo::class.java) - doReturn(type).`when`(info).type val lp = LinkProperties() val caps = NetworkCapabilities().apply { setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false) setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, true) setSSID(ssid) } - return NetworkState(info, lp, caps, mock(Network::class.java), subscriberId, ssid) + return NetworkState(type, lp, caps, mock(Network::class.java), subscriberId, ssid) } private fun NetworkTemplate.assertMatches(ident: NetworkIdentity) = diff --git a/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java index dde78aa541991..214c82da17dcc 100644 --- a/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java +++ b/tests/net/java/com/android/server/net/NetworkStatsServiceTest.java @@ -80,8 +80,6 @@ import android.net.INetworkStatsSession; import android.net.LinkProperties; import android.net.Network; import android.net.NetworkCapabilities; -import android.net.NetworkInfo; -import android.net.NetworkInfo.DetailedState; import android.net.NetworkState; import android.net.NetworkStats; import android.net.NetworkStatsHistory; @@ -1456,8 +1454,6 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { } private static NetworkState buildWifiState(boolean isMetered, @NonNull String iface) { - final NetworkInfo info = new NetworkInfo(TYPE_WIFI, 0, null, null); - info.setDetailedState(DetailedState.CONNECTED, null, null); final LinkProperties prop = new LinkProperties(); prop.setInterfaceName(iface); final NetworkCapabilities capabilities = new NetworkCapabilities(); @@ -1465,7 +1461,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, true); capabilities.addTransportType(NetworkCapabilities.TRANSPORT_WIFI); capabilities.setSSID(TEST_SSID); - return new NetworkState(info, prop, capabilities, WIFI_NETWORK, null, TEST_SSID); + return new NetworkState(TYPE_WIFI, prop, capabilities, WIFI_NETWORK, null, TEST_SSID); } private static NetworkState buildMobile3gState(String subscriberId) { @@ -1473,17 +1469,14 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { } private static NetworkState buildMobile3gState(String subscriberId, boolean isRoaming) { - final NetworkInfo info = new NetworkInfo( - TYPE_MOBILE, TelephonyManager.NETWORK_TYPE_UMTS, null, null); - info.setDetailedState(DetailedState.CONNECTED, null, null); - info.setRoaming(isRoaming); final LinkProperties prop = new LinkProperties(); prop.setInterfaceName(TEST_IFACE); final NetworkCapabilities capabilities = new NetworkCapabilities(); capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false); capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, !isRoaming); capabilities.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); - return new NetworkState(info, prop, capabilities, MOBILE_NETWORK, subscriberId, null); + return new NetworkState( + TYPE_MOBILE, prop, capabilities, MOBILE_NETWORK, subscriberId, null); } private NetworkStats buildEmptyStats() { @@ -1491,11 +1484,9 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { } private static NetworkState buildVpnState() { - final NetworkInfo info = new NetworkInfo(TYPE_VPN, 0, null, null); - info.setDetailedState(DetailedState.CONNECTED, null, null); final LinkProperties prop = new LinkProperties(); prop.setInterfaceName(TUN_IFACE); - return new NetworkState(info, prop, new NetworkCapabilities(), VPN_NETWORK, null, null); + return new NetworkState(TYPE_VPN, prop, new NetworkCapabilities(), VPN_NETWORK, null, null); } private long getElapsedRealtime() {