From dc7bfba12e0981b9d958c9f36487c91ff0a2f71b Mon Sep 17 00:00:00 2001 From: Junyu Lai Date: Sat, 24 May 2025 10:46:43 +0800 Subject: [PATCH] Add NetworkTemplate get/set transport type methods This is needed for counter-part modules to query data usage with specified transport type. This is kept hidden and will be exposed in follow-up CLs. Flag: EXEMPT controlled in the counter-part CL Test: atest ConnectivityCoverageTests --test-filter \ 'NetworkTemplate*' --update-device Bug: 419123367 Bug: 420531377 Change-Id: I503b4d7307d008a64a14aa713d9714428e399809 --- .../src/android/net/NetworkTemplate.java | 148 ++++++++++-------- .../SatelliteCoarseUsageMetricsCollector.java | 5 +- .../net/netstats/NetworkTemplateTest.kt | 132 +++++----------- .../java/android/net/NetworkTemplateTest.kt | 48 ++---- .../server/net/NetworkStatsServiceTest.java | 32 ++-- 5 files changed, 162 insertions(+), 203 deletions(-) diff --git a/framework-t/src/android/net/NetworkTemplate.java b/framework-t/src/android/net/NetworkTemplate.java index 8f62b4d151..af44cf7da5 100644 --- a/framework-t/src/android/net/NetworkTemplate.java +++ b/framework-t/src/android/net/NetworkTemplate.java @@ -24,12 +24,6 @@ import static android.net.ConnectivityManager.TYPE_PROXY; import static android.net.ConnectivityManager.TYPE_WIFI; import static android.net.ConnectivityManager.TYPE_WIFI_P2P; import static android.net.ConnectivityManager.TYPE_WIMAX; -import static android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH; -import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; -import static android.net.NetworkCapabilities.TRANSPORT_ETHERNET; -import static android.net.NetworkCapabilities.TRANSPORT_SATELLITE; -import static android.net.NetworkCapabilities.TRANSPORT_TEST; -import static android.net.NetworkCapabilities.TRANSPORT_WIFI; import static android.net.NetworkIdentity.OEM_NONE; import static android.net.NetworkIdentity.OEM_PAID; import static android.net.NetworkIdentity.OEM_PRIVATE; @@ -43,9 +37,10 @@ import static android.net.NetworkStats.ROAMING_ALL; import static android.net.NetworkStats.ROAMING_NO; import static android.net.NetworkStats.ROAMING_YES; -import static com.android.net.module.util.CollectionUtils.toIntArray; import static com.android.net.module.util.NetworkCapabilitiesUtils.TYPE_TEST; +import static java.util.Objects.requireNonNull; + import android.annotation.IntDef; import android.annotation.NonNull; import android.annotation.Nullable; @@ -97,6 +92,8 @@ public final class NetworkTemplate implements Parcelable { MATCH_BLUETOOTH, MATCH_PROXY, MATCH_CARRIER, + MATCH_TEST, + MATCH_ALL, }) public @interface TemplateMatchRule{} @@ -126,6 +123,13 @@ public final class NetworkTemplate implements Parcelable { */ @VisibleForTesting public static final int MATCH_TEST = 11; + /** + * Match rule to match networks with other filters inside the template. + * + * Note that exactly one transport type is needed to build the template with this match rule. + * @hide + */ + public static final int MATCH_ALL = 12; // TODO: Remove this and replace all callers with WIFI_NETWORK_KEY_ALL. /** @hide */ @@ -191,6 +195,7 @@ public final class NetworkTemplate implements Parcelable { case MATCH_PROXY: case MATCH_CARRIER: case MATCH_TEST: + case MATCH_ALL: return true; default: @@ -535,8 +540,8 @@ public final class NetworkTemplate implements Parcelable { public NetworkTemplate(int matchRule, String[] matchSubscriberIds, String[] matchWifiNetworkKeys, int metered, int roaming, int defaultNetwork, int ratType, int oemManaged, long transportTypesBits) { - Objects.requireNonNull(matchWifiNetworkKeys); - Objects.requireNonNull(matchSubscriberIds); + requireNonNull(matchWifiNetworkKeys); + requireNonNull(matchSubscriberIds); mMatchRule = matchRule; mMatchSubscriberIds = matchSubscriberIds; mMatchWifiNetworkKeys = matchWifiNetworkKeys; @@ -657,8 +662,15 @@ public final class NetworkTemplate implements Parcelable { /** * Get match rule of the template. See {@code MATCH_*}. + * + * @throws IllegalStateException if the template is not built from constructors with + * match rule, which can occur if the template was created with the + * {@link Builder#Builder()} constructor. */ public int getMatchRule() { + if (MATCH_ALL == mMatchRule) { + throw new IllegalStateException("Match rule is not set"); + } return mMatchRule; } @@ -731,6 +743,20 @@ public final class NetworkTemplate implements Parcelable { return mRatType; } + /** + * Get the transport type filter of the template. + * + * This is only valid when the template is constructed by using + * {@link NetworkTemplate.Builder}. + * @hide + */ + public int getTransportType() { + if (mTransportTypesBits == TRANSPORT_TYPES_ALL) { + throw new IllegalStateException("Transport was not set"); + } + return getTransportTypes()[0]; + } + /** * Get the transport types filter of the template. * @@ -757,12 +783,11 @@ public final class NetworkTemplate implements Parcelable { */ @SystemApi(client = MODULE_LIBRARIES) public boolean matches(@NonNull NetworkIdentity ident) { - Objects.requireNonNull(ident); + requireNonNull(ident); if (!matchesMetered(ident)) return false; if (!matchesRoaming(ident)) return false; if (!matchesDefaultNetwork(ident)) return false; if (!matchesOemNetwork(ident)) return false; - if (!matchesTransportTypes(ident)) return false; // TODO: Convert match rule handling into transport type matches and // eliminate the need of legacy network types. @@ -781,6 +806,8 @@ public final class NetworkTemplate implements Parcelable { return matchesCarrier(ident); case MATCH_TEST: return matchesTest(ident); + case MATCH_ALL: + return matchesTransportTypes(ident); default: // We have no idea what kind of network template we are, so we // just claim not to match anything. @@ -952,6 +979,8 @@ public final class NetworkTemplate implements Parcelable { return "CARRIER"; case MATCH_TEST: return "TEST"; + case MATCH_ALL: + return "ALL"; default: return "UNKNOWN(" + matchRule + ")"; } @@ -1106,6 +1135,17 @@ public final class NetworkTemplate implements Parcelable { mTransportTypesBits = TRANSPORT_TYPES_ALL; } + /** + * Creates a new Builder to construct NetworkTemplate objects. + * + * Note that {@link #setTransportType} MUST be called to specify + * the transport type filter. + * @hide + */ + public Builder() { + this(MATCH_ALL); + } + /** * Set the Subscriber Ids. Calling this function with an empty set represents * the intention of matching any Subscriber Ids. @@ -1115,7 +1155,7 @@ public final class NetworkTemplate implements Parcelable { */ @NonNull public Builder setSubscriberIds(@NonNull Set subscriberIds) { - Objects.requireNonNull(subscriberIds); + requireNonNull(subscriberIds); mMatchSubscriberIds.clear(); mMatchSubscriberIds.addAll(subscriberIds); return this; @@ -1136,7 +1176,7 @@ public final class NetworkTemplate implements Parcelable { */ @NonNull public Builder setWifiNetworkKeys(@NonNull Set wifiNetworkKeys) { - Objects.requireNonNull(wifiNetworkKeys); + requireNonNull(wifiNetworkKeys); for (String key : wifiNetworkKeys) { if (key == null) { throw new IllegalArgumentException("Null is not a valid key"); @@ -1198,6 +1238,25 @@ public final class NetworkTemplate implements Parcelable { return this; } + /** + * Sets the transport type filter for this template. + * + * This is only valid when the template is constructed by using + * {@link NetworkTemplate.Builder}. + * + * @param transportType {@code NetworkCapabilities#TRANSPORT_*} constant. + * @return This {@code Builder} for chaining. + @hide + */ + @NonNull + public Builder setTransportType(int transportType) { + if (mMatchRule != MATCH_ALL) { + throw new IllegalArgumentException("setTransportType is only supported for builder" + + " built by default constructor."); + } + return setTransportTypes(new int[] {transportType}); + } + /** * Sets the transport types filter for this template. * @@ -1212,7 +1271,7 @@ public final class NetworkTemplate implements Parcelable { * @hide */ @NonNull - public Builder setTransportTypes(int[] transportTypes) { + private Builder setTransportTypes(int[] transportTypes) { mTransportTypesBits = BitUtils.packBits(transportTypes); return this; } @@ -1257,56 +1316,19 @@ public final class NetworkTemplate implements Parcelable { } } - // Validates if transport types are specified (i.e., mTransportTypesBits is not 0), - // they must be consistent with the template's mMatchRule. This means - // the specified transports must include at least one type that is - // deducible or expected for that matchRule. - // For example, if mMatchRule is MATCH_WIFI and transports are specified, - // TRANSPORT_WIFI must be among them. + /** + * Validates that: + * 1. For MATCH_ALL, exactly one transport type is specified. + * 2. For all other match rules, no transport type should be specified. + */ private void validateTransportTypes() { - assertDeducedTransportTypes(mMatchRule, mTransportTypesBits); - } - - /** - * Determines possible transport types for a match rule. - * @return Set of TRANSPORT_* constants. - * @throws IllegalArgumentException for unknown matchRule. - */ - private static Set getPossibleTransportsForMatchRule(int matchRule) { - return switch (matchRule) { - case MATCH_MOBILE -> Set.of(TRANSPORT_CELLULAR, TRANSPORT_SATELLITE); - case MATCH_WIFI -> Set.of(TRANSPORT_WIFI); - case MATCH_ETHERNET -> Set.of(TRANSPORT_ETHERNET); - // See NetworkCapabilitiesUtils#deduceTransportTypeForLegacyIdentity. - case MATCH_PROXY, MATCH_BLUETOOTH -> Set.of(TRANSPORT_BLUETOOTH); - // Carrier networks are identified by subscriberId and can be either cellular - // or carrier-provided Wi-Fi. - case MATCH_CARRIER -> Set.of(TRANSPORT_CELLULAR, TRANSPORT_WIFI); - case MATCH_TEST -> Set.of(TRANSPORT_TEST); - default -> throw new IllegalArgumentException( - "Unexpected match rule: " + matchRule); - }; - } - - /** - * Asserts template's transport types are consistent with its match rule. - * Called during NetworkTemplate construction. - * - * @param matchRule The NetworkTemplate match rule. - * @param transportTypesBits Bitmask of transport types. No-op if 0. - * @throws IllegalArgumentException if transports are inconsistent with the rule. - */ - private static void assertDeducedTransportTypes(int matchRule, long transportTypesBits) { - // No transports specified, nothing to check. - if (transportTypesBits == 0L) return; - - Set possibleTransportsSet = getPossibleTransportsForMatchRule(matchRule); - final long possibleTransportBitsForMatchRule = - BitUtils.packBits(toIntArray(possibleTransportsSet)); - - // Template transports must overlap with possible transports for the rule. - if ((transportTypesBits & possibleTransportBitsForMatchRule) == 0L) { - throw new IllegalArgumentException("No deduced transport type: " + matchRule); + final int transportCount = Long.bitCount(mTransportTypesBits); + if (mMatchRule != MATCH_ALL && transportCount != 0) { + throw new IllegalArgumentException( + "Transports type can't be set with a match rule."); + } else if (mMatchRule == MATCH_ALL && transportCount != 1) { + throw new IllegalArgumentException( + "Transport type not set (use Builder#setTransportType)."); } } diff --git a/service/src/com/android/metrics/SatelliteCoarseUsageMetricsCollector.java b/service/src/com/android/metrics/SatelliteCoarseUsageMetricsCollector.java index 79f2f1dc74..7e8e463389 100644 --- a/service/src/com/android/metrics/SatelliteCoarseUsageMetricsCollector.java +++ b/service/src/com/android/metrics/SatelliteCoarseUsageMetricsCollector.java @@ -18,7 +18,6 @@ package com.android.metrics; import static android.net.NetworkCapabilities.NET_CAPABILITY_INTERNET; import static android.net.NetworkCapabilities.TRANSPORT_SATELLITE; -import static android.net.NetworkTemplate.MATCH_MOBILE; import static com.android.net.module.util.FrameworkConnectivityStatsLog.CORE_NETWORKING_CRITICAL_BYTES_EVENT_OCCURRED; import static com.android.net.module.util.FrameworkConnectivityStatsLog.CORE_NETWORKING_CRITICAL_BYTES_EVENT_OCCURRED__EVENT_TYPE__CRITICAL_BYTES_EVENT_TYPE_SATELLITE_COARSE_RX_USAGE; @@ -63,8 +62,8 @@ public class SatelliteCoarseUsageMetricsCollector { // To prevent NetworkTemplate#Builder not found error when // initializing CSTest on S-based image. if (SdkLevel.isAtLeastU()) { - SATELLITE_TEMPLATE = new NetworkTemplate.Builder(MATCH_MOBILE) - .setTransportTypes(new int[]{TRANSPORT_SATELLITE}).build(); + SATELLITE_TEMPLATE = new NetworkTemplate.Builder() + .setTransportType(TRANSPORT_SATELLITE).build(); } else { SATELLITE_TEMPLATE = null; } diff --git a/tests/common/java/android/net/netstats/NetworkTemplateTest.kt b/tests/common/java/android/net/netstats/NetworkTemplateTest.kt index 90109d1430..0eb28afbf6 100644 --- a/tests/common/java/android/net/netstats/NetworkTemplateTest.kt +++ b/tests/common/java/android/net/netstats/NetworkTemplateTest.kt @@ -16,12 +16,7 @@ package android.net.netstats -import android.net.NetworkCapabilities.TRANSPORT_BLUETOOTH import android.net.NetworkCapabilities.TRANSPORT_CELLULAR -import android.net.NetworkCapabilities.TRANSPORT_ETHERNET -import android.net.NetworkCapabilities.TRANSPORT_SATELLITE -import android.net.NetworkCapabilities.TRANSPORT_TEST -import android.net.NetworkCapabilities.TRANSPORT_VPN import android.net.NetworkCapabilities.TRANSPORT_WIFI import android.net.NetworkStats.DEFAULT_NETWORK_ALL import android.net.NetworkStats.METERED_ALL @@ -34,7 +29,6 @@ import android.net.NetworkTemplate.MATCH_CARRIER import android.net.NetworkTemplate.MATCH_ETHERNET import android.net.NetworkTemplate.MATCH_MOBILE import android.net.NetworkTemplate.MATCH_PROXY -import android.net.NetworkTemplate.MATCH_TEST import android.net.NetworkTemplate.MATCH_WIFI import android.net.NetworkTemplate.NETWORK_TYPE_ALL import android.net.NetworkTemplate.OEM_MANAGED_ALL @@ -299,93 +293,53 @@ class NetworkTemplateTest { } } - // Verify the builder does not throw, and transports read from the getter is expected. + /** + * Tests the successful setting and retrieval of a transport type. + */ @Test - fun testBuilderTransportTypes() { - assertGetSetTransportTypes(MATCH_MOBILE, TRANSPORT_CELLULAR) - assertGetSetTransportTypes(MATCH_MOBILE, TRANSPORT_SATELLITE) - assertGetSetTransportTypes(MATCH_WIFI, TRANSPORT_WIFI) - assertGetSetTransportTypes(MATCH_ETHERNET, TRANSPORT_ETHERNET) - assertGetSetTransportTypes(MATCH_BLUETOOTH, TRANSPORT_BLUETOOTH) - assertGetSetTransportTypes(MATCH_PROXY, TRANSPORT_BLUETOOTH) - assertGetSetTransportTypes(MATCH_CARRIER, TRANSPORT_CELLULAR) - assertGetSetTransportTypes(MATCH_CARRIER, TRANSPORT_WIFI) - assertGetSetTransportTypes(MATCH_TEST, TRANSPORT_TEST) - } - - private fun assertGetSetTransportTypes(matchRule: Int, transport: Int) { - val builder = NetworkTemplate.Builder(matchRule) - .setTransportTypes(intArrayOf(transport)) - if (matchRule == MATCH_CARRIER) { - builder.setSubscriberIds(setOf(TEST_IMSI1)) - } - val template = builder.build() - assertEquals(1, template.transportTypes.size) - assertEquals(transport, template.transportTypes[0]) - } - - @Test - fun testValidateTransportTypesNoTransportsSetSuccess() { - // Should succeed for any match rule if no transports are explicitly set. - listOf( - MATCH_WIFI, - MATCH_MOBILE, - MATCH_ETHERNET, - MATCH_BLUETOOTH, - MATCH_PROXY, - MATCH_TEST - ).forEach { matchRule -> - NetworkTemplate.Builder(matchRule) - .setTransportTypes(intArrayOf()) // Explicitly empty, or just don't call it - .build() // Should not throw - } - NetworkTemplate.Builder(MATCH_CARRIER) - .setSubscriberIds(setOf(TEST_IMSI1)) - .setTransportTypes(intArrayOf()) - .build() // Should not throw - } - - @Test - fun testValidateTransportTypesDeducedTransportPlusCompatibleSuccess() { - NetworkTemplate.Builder(MATCH_WIFI) - .setTransportTypes(intArrayOf(TRANSPORT_WIFI, TRANSPORT_VPN)) - .build() - NetworkTemplate.Builder(MATCH_MOBILE) - .setTransportTypes(intArrayOf(TRANSPORT_CELLULAR, TRANSPORT_TEST)) + fun testSetAndGetTransportType() { + val template = NetworkTemplate.Builder() + .setTransportType(TRANSPORT_CELLULAR) .build() + + assertFailsWith { template.matchRule } + assertEquals(TRANSPORT_CELLULAR, template.transportType) } + /** + * Verifies that if `setTransportType` is called multiple times, the last value supplied is used. + */ @Test - fun testValidateTransportTypesDeducedTransportMissingThrows() { - assertFailsWith { - NetworkTemplate.Builder(MATCH_WIFI) - .setTransportTypes(intArrayOf(TRANSPORT_CELLULAR)) - .build() - } - assertFailsWith { - NetworkTemplate.Builder(MATCH_MOBILE) - .setTransportTypes(intArrayOf(TRANSPORT_WIFI)) - .build() - } - assertFailsWith { - NetworkTemplate.Builder(MATCH_ETHERNET) - .setTransportTypes(intArrayOf(TRANSPORT_WIFI)) - .build() - } - assertFailsWith { - NetworkTemplate.Builder(MATCH_CARRIER) - .setTransportTypes(intArrayOf(TRANSPORT_SATELLITE)) - .build() - } - assertFailsWith { - NetworkTemplate.Builder(MATCH_TEST) - .setTransportTypes(intArrayOf(TRANSPORT_SATELLITE)) - .build() - } - assertFailsWith { - NetworkTemplate.Builder(MATCH_TEST) - .setTransportTypes(intArrayOf(INVALID_TRANSPORT)) - .build() - } + fun testMultipleSetTransportTypeOverrides() { + val template = NetworkTemplate.Builder() + .setTransportType(TRANSPORT_CELLULAR).setTransportType(TRANSPORT_WIFI).build() + assertEquals(TRANSPORT_WIFI, template.transportType) + } + + /** + * Verifies that building a template with a transport type set for a + * non-transport match rule throws an IllegalArgumentException. + */ + @Test(expected = IllegalArgumentException::class) + fun testSetTransportType_withNonTransportRuleThrows() { + NetworkTemplate.Builder(MATCH_WIFI).setTransportType(TRANSPORT_CELLULAR) + } + + /** + * Verifies that building a template with a non-transport match rule throws an + * IllegalStateException when getTransportType() calls. + */ + @Test(expected = IllegalStateException::class) + fun testNonTransportRule_getTransportThrows() { + val template = NetworkTemplate.Builder(MATCH_WIFI).build() + template.transportType + } + + /** + * Verifies template built with default constructor but doesn't specify transport throws. + */ + @Test(expected = IllegalArgumentException::class) + fun testGetTransportType_whenNotSetThrows() { + NetworkTemplate.Builder().build() } } diff --git a/tests/unit/java/android/net/NetworkTemplateTest.kt b/tests/unit/java/android/net/NetworkTemplateTest.kt index 10392b47f6..1435cd01e7 100644 --- a/tests/unit/java/android/net/NetworkTemplateTest.kt +++ b/tests/unit/java/android/net/NetworkTemplateTest.kt @@ -51,7 +51,6 @@ import android.net.NetworkTemplate.normalize import android.net.wifi.WifiInfo import android.os.Build import android.telephony.TelephonyManager -import com.android.net.module.util.BitUtils import com.android.testutils.DevSdkIgnoreRule import com.android.testutils.DevSdkIgnoreRunner import com.android.testutils.NonNullTestUtils @@ -684,7 +683,6 @@ class NetworkTemplateTest { fun testEquals() { val templateImsi1 = NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES) .setSubscriberIds(setOf(TEST_IMSI1)).setRatType(TelephonyManager.NETWORK_TYPE_UMTS) - .setTransportTypes(intArrayOf(TRANSPORT_SATELLITE, TRANSPORT_VPN)) .build() val dupTemplateImsi1 = NetworkTemplate( MATCH_MOBILE, @@ -695,7 +693,7 @@ class NetworkTemplateTest { DEFAULT_NETWORK_ALL, TelephonyManager.NETWORK_TYPE_UMTS, OEM_MANAGED_ALL, - BitUtils.packBits(intArrayOf(TRANSPORT_SATELLITE, TRANSPORT_VPN)) + TRANSPORT_TYPES_ALL ) val templateImsi2 = NetworkTemplate.Builder(MATCH_MOBILE).setMeteredness(METERED_YES) .setSubscriberIds(setOf(TEST_IMSI2)).setRatType(TelephonyManager.NETWORK_TYPE_UMTS) @@ -707,7 +705,6 @@ class NetworkTemplateTest { val templateWifiKey1 = NetworkTemplate.Builder(MATCH_WIFI) .setWifiNetworkKeys(setOf(TEST_WIFI_KEY1)) - .setTransportTypes(intArrayOf(TRANSPORT_WIFI)) .build() val dupTemplateWifiKey1 = NetworkTemplate( MATCH_WIFI, @@ -718,7 +715,7 @@ class NetworkTemplateTest { DEFAULT_NETWORK_ALL, NETWORK_TYPE_ALL, OEM_MANAGED_ALL, - BitUtils.packBits(intArrayOf(TRANSPORT_WIFI)) + TRANSPORT_TYPES_ALL ) val templateWifiKey2 = NetworkTemplate.Builder(MATCH_WIFI) .setWifiNetworkKeys(setOf(TEST_WIFI_KEY2)).build() @@ -911,46 +908,31 @@ class NetworkTemplateTest { .setType(TYPE_MOBILE) .setTransportTypes(setOf(TRANSPORT_SATELLITE)) .build() - val identMobileVpn = NetworkIdentity.Builder() + val identCellularVpn = NetworkIdentity.Builder() .setType(TYPE_MOBILE) .setTransportTypes(setOf(TRANSPORT_CELLULAR, TRANSPORT_VPN)) .build() - val identOnlyCellular = NetworkIdentity.Builder() + val identCellular = NetworkIdentity.Builder() .setType(TYPE_MOBILE) .setTransportTypes(setOf(TRANSPORT_CELLULAR)) .build() - val templateMobileNoTransport = NetworkTemplate.Builder(MATCH_MOBILE) - .build() // Wildcard for transports - val templateMobileWithCellularTransport = NetworkTemplate.Builder(MATCH_MOBILE) - .setTransportTypes(intArrayOf(TRANSPORT_CELLULAR)) + val templateCellular = NetworkTemplate.Builder() + .setTransportType(TRANSPORT_CELLULAR) .build() - val templateSatellite = NetworkTemplate.Builder(MATCH_MOBILE) - .setTransportTypes(intArrayOf(TRANSPORT_SATELLITE)) - .build() - val templateVpnAndCellular = NetworkTemplate.Builder(MATCH_MOBILE) - .setTransportTypes(intArrayOf(TRANSPORT_VPN, TRANSPORT_CELLULAR)) + val templateSatellite = NetworkTemplate.Builder() + .setTransportType(TRANSPORT_SATELLITE) .build() - // 1. Mobile wildcard template - templateMobileNoTransport.assertMatches(identSatellite) - templateMobileNoTransport.assertMatches(identMobileVpn) - templateMobileNoTransport.assertMatches(identOnlyCellular) + // 1. Template requiring TRANSPORT_CELLULAR + templateCellular.assertDoesNotMatch(identSatellite) + templateCellular.assertMatches(identCellularVpn) + templateCellular.assertMatches(identCellular) - // 2. Template requiring TRANSPORT_CELLULAR - templateMobileWithCellularTransport.assertDoesNotMatch(identSatellite) - templateMobileWithCellularTransport.assertMatches(identMobileVpn) - templateMobileWithCellularTransport.assertMatches(identOnlyCellular) - - // 3. Template requiring TRANSPORT_SATELLITE + // 2. Template requiring TRANSPORT_SATELLITE templateSatellite.assertMatches(identSatellite) - templateSatellite.assertDoesNotMatch(identMobileVpn) - templateSatellite.assertDoesNotMatch(identOnlyCellular) - - // 4. Template requiring TRANSPORT_VPN AND TRANSPORT_CELLULAR - templateVpnAndCellular.assertDoesNotMatch(identSatellite) - templateVpnAndCellular.assertMatches(identMobileVpn) - templateVpnAndCellular.assertDoesNotMatch(identOnlyCellular) + templateSatellite.assertDoesNotMatch(identCellularVpn) + templateSatellite.assertDoesNotMatch(identCellular) } @Test diff --git a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java index 90c9fc5c0a..955701e8ec 100644 --- a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java +++ b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java @@ -27,6 +27,8 @@ import static android.content.pm.PackageManager.PERMISSION_GRANTED; import static android.net.ConnectivityManager.TYPE_MOBILE; import static android.net.ConnectivityManager.TYPE_TEST; import static android.net.ConnectivityManager.TYPE_WIFI; +import static android.net.NetworkCapabilities.TRANSPORT_CELLULAR; +import static android.net.NetworkCapabilities.TRANSPORT_SATELLITE; import static android.net.NetworkIdentity.OEM_PAID; import static android.net.NetworkIdentity.OEM_PRIVATE; import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK; @@ -1138,7 +1140,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { // Pretend that 5g mobile network comes online final NetworkStateSnapshot[] mobileStates = new NetworkStateSnapshot[] {buildMobileState(IMSI_1), buildStateOfTransport( - NetworkCapabilities.TRANSPORT_CELLULAR, TYPE_MOBILE, + TRANSPORT_CELLULAR, TYPE_MOBILE, TEST_IFACE2, IMSI_1, null /* wifiNetworkKey */, true /* isTemporarilyNotMetered */, false /* isRoaming */)}; setMobileRatTypeAndWaitForIdle(TelephonyManager.NETWORK_TYPE_NR); @@ -1413,7 +1415,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { mockNetworkStatsUidDetail(buildEmptyStats()); final NetworkStateSnapshot mobileState = buildStateOfTransport( - NetworkCapabilities.TRANSPORT_CELLULAR, TYPE_MOBILE, + TRANSPORT_CELLULAR, TYPE_MOBILE, TEST_IFACE2, IMSI_1, null /* wifiNetworkKey */, false /* isTemporarilyNotMetered */, false /* isRoaming */); @@ -1472,7 +1474,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { final String[] mobileIfaces = {TEST_IFACE2}; mockNetworkStatsUidDetail(buildEmptyStats(), emptyTetherStats, mobileIfaces); final NetworkStats mobileStats = mService.getUidStatsForTransport( - NetworkCapabilities.TRANSPORT_CELLULAR); + TRANSPORT_CELLULAR); assertEquals(2, mobileStats.size()); // Verify the operation count stats that caused by incrementOperationCount only appears @@ -1491,7 +1493,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { mockNetworkStatsUidDetail(buildEmptyStats()); final NetworkStateSnapshot mobileState = buildStateOfTransport( - NetworkCapabilities.TRANSPORT_CELLULAR, TYPE_MOBILE, + TRANSPORT_CELLULAR, TYPE_MOBILE, TEST_IFACE2, IMSI_1, null /* wifiNetworkKey */, false /* isTemporarilyNotMetered */, false /* isRoaming */); @@ -1522,7 +1524,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { // with getUidStatsForTransport(TRANSPORT_CELLULAR) return stats of both cellular // and satellite final NetworkStats mobileStats = mService.getUidStatsForTransport( - NetworkCapabilities.TRANSPORT_CELLULAR); + TRANSPORT_CELLULAR); // The iface field of the returned stats should be null because getUidStatsForTransport // clears the interface field before it returns the result. @@ -1531,7 +1533,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { // getUidStatsForTransport(TRANSPORT_SATELLITE) is not supported assertThrows(IllegalArgumentException.class, - () -> mService.getUidStatsForTransport(NetworkCapabilities.TRANSPORT_SATELLITE)); + () -> mService.getUidStatsForTransport(TRANSPORT_SATELLITE)); } @@ -1543,17 +1545,17 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { public void testGetSatelliteStats() throws Exception { mockDefaultSettings(); final NetworkTemplate templateSatellite = - new NetworkTemplate.Builder(MATCH_MOBILE) + new NetworkTemplate.Builder() .setMeteredness(METERED_YES) .setSubscriberIds(Set.of(IMSI_1)) - .setTransportTypes(new int[]{NetworkCapabilities.TRANSPORT_SATELLITE}) + .setTransportType(TRANSPORT_SATELLITE) .build(); final NetworkTemplate templateCellularOnly = - new NetworkTemplate.Builder(MATCH_MOBILE) + new NetworkTemplate.Builder() .setMeteredness(METERED_YES) .setSubscriberIds(Set.of(IMSI_1)) - .setTransportTypes(new int[]{NetworkCapabilities.TRANSPORT_CELLULAR}) + .setTransportType(TRANSPORT_CELLULAR) .build(); // Initial state: Assert all relevant templates are empty @@ -1712,7 +1714,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { mockDefaultSettings(); NetworkStateSnapshot[] states = new NetworkStateSnapshot[] {buildStateOfTransport( - NetworkCapabilities.TRANSPORT_CELLULAR, TYPE_MOBILE, + TRANSPORT_CELLULAR, TYPE_MOBILE, TEST_IFACE, IMSI_1, null /* wifiNetworkKey */, false /* isTemporarilyNotMetered */, true /* isRoaming */)}; mockNetworkStatsSummary(buildEmptyStats()); @@ -2892,13 +2894,13 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { } private static NetworkStateSnapshot buildMobileState(String subscriberId) { - return buildStateOfTransport(NetworkCapabilities.TRANSPORT_CELLULAR, TYPE_MOBILE, + return buildStateOfTransport(TRANSPORT_CELLULAR, TYPE_MOBILE, TEST_IFACE, subscriberId, null /* wifiNetworkKey */, false /* isTemporarilyNotMetered */, false /* isRoaming */); } private static NetworkStateSnapshot buildSatelliteMobileState(String subscriberId) { - return buildStateOfTransport(NetworkCapabilities.TRANSPORT_SATELLITE, TYPE_MOBILE, + return buildStateOfTransport(TRANSPORT_SATELLITE, TYPE_MOBILE, TEST_IFACE, subscriberId, null /* wifiNetworkKey */, false /* isTemporarilyNotMetered */, false /* isRoaming */); } @@ -2942,7 +2944,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { for (int nc : oemNetCapabilities) { capabilities.setCapability(nc, true); } - capabilities.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); + capabilities.addTransportType(TRANSPORT_CELLULAR); return new NetworkStateSnapshot(MOBILE_NETWORK, capabilities, prop, subscriberId, TYPE_MOBILE); } @@ -2955,7 +2957,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, true); capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, true); capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_IMS, true); - capabilities.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); + capabilities.addTransportType(TRANSPORT_CELLULAR); capabilities.setNetworkSpecifier(new TelephonyNetworkSpecifier(subId)); return new NetworkStateSnapshot( MOBILE_NETWORK, capabilities, prop, subscriberId, TYPE_MOBILE);