From 2685c929f40dfa8510b0b25aed6ef1ef57a1bf7a Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Mon, 26 Jul 2021 19:23:26 +0800 Subject: [PATCH] Add a test for metered/non-metered mobile network NetworkTemplate.buildTemplateMobileWithRatType() supports to build a template with metered filter, so a mobile network is metered or non-metered should be tested. This commit also rename buildMobile3gState to buildMobileState because originally this method uses NetworkInfo to simulate the network type. The NetworkInfo related codes were removed. Currently, setMobileRatTypeAndWaitForIdle is used to simulate the network type in test. Ignore-AOSP-First: The parent of this change contains a CL will conflict with internal tree so ignore AOSP first. Bug: 183776809 Test: this Change-Id: I8e54e2acb06db81fb0eaf330c4fd7cb391b01ff3 --- .../server/net/NetworkStatsServiceTest.java | 72 +++++++++++++++---- 1 file changed, 60 insertions(+), 12 deletions(-) diff --git a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java index 5f3d499066..4948e66a93 100644 --- a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java +++ b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java @@ -523,7 +523,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { public void testUidStatsAcrossNetworks() throws Exception { // pretend first mobile network comes online expectDefaultSettings(); - NetworkStateSnapshot[] states = new NetworkStateSnapshot[] {buildMobile3gState(IMSI_1)}; + NetworkStateSnapshot[] states = new NetworkStateSnapshot[] {buildMobileState(IMSI_1)}; expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); @@ -554,7 +554,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { // disappearing, to verify we don't count backwards. incrementCurrentTime(HOUR_IN_MILLIS); expectDefaultSettings(); - states = new NetworkStateSnapshot[] {buildMobile3gState(IMSI_2)}; + states = new NetworkStateSnapshot[] {buildMobileState(IMSI_2)}; expectNetworkStatsSummary(new NetworkStats(getElapsedRealtime(), 1) .insertEntry(TEST_IFACE, 2048L, 16L, 512L, 4L)); expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 3) @@ -666,7 +666,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_NR, METERED_YES); final NetworkStateSnapshot[] states = - new NetworkStateSnapshot[]{buildMobile3gState(IMSI_1)}; + new NetworkStateSnapshot[]{buildMobileState(IMSI_1)}; // 3G network comes online. expectNetworkStatsSummary(buildEmptyStats()); @@ -732,6 +732,45 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { assertUidTotal(template5g, UID_RED, 5L, 13L, 31L, 9L, 2); } + @Test + public void testMobileStatsMeteredness() throws Exception { + // Create metered 5g template. + final NetworkTemplate templateMetered5g = + buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_NR, + METERED_YES); + // Create non-metered 5g template + final NetworkTemplate templateNonMetered5g = + buildTemplateMobileWithRatType(null, TelephonyManager.NETWORK_TYPE_NR, METERED_NO); + + expectDefaultSettings(); + expectNetworkStatsSummary(buildEmptyStats()); + expectNetworkStatsUidDetail(buildEmptyStats()); + + // Pretend that 5g mobile network comes online + final NetworkStateSnapshot[] mobileStates = + new NetworkStateSnapshot[] {buildMobileState(IMSI_1), buildMobileState(TEST_IFACE2, + IMSI_1, true /* isTemporarilyNotMetered */, false /* isRoaming */)}; + setMobileRatTypeAndWaitForIdle(TelephonyManager.NETWORK_TYPE_NR); + mService.notifyNetworkStatus(NETWORKS_MOBILE, mobileStates, + getActiveIface(mobileStates), new UnderlyingNetworkInfo[0]); + + // Create some traffic + // Note that all traffic from NetworkManagementService is tagged as METERED_NO, ROAMING_NO + // and DEFAULT_NETWORK_YES, because these three properties aren't tracked at that layer. + // They are layered on top by inspecting the iface properties. + incrementCurrentTime(HOUR_IN_MILLIS); + expectNetworkStatsUidDetail(new NetworkStats(getElapsedRealtime(), 1) + .insertEntry(TEST_IFACE, UID_RED, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, + DEFAULT_NETWORK_YES, 128L, 2L, 128L, 2L, 0L) + .insertEntry(TEST_IFACE2, UID_RED, SET_DEFAULT, TAG_NONE, METERED_YES, ROAMING_NO, + DEFAULT_NETWORK_YES, 256, 3L, 128L, 5L, 0L)); + forcePollAndWaitForIdle(); + + // Verify service recorded history. + assertUidTotal(templateMetered5g, UID_RED, 128L, 2L, 128L, 2L, 0); + assertUidTotal(templateNonMetered5g, UID_RED, 256, 3L, 128L, 5L, 0); + } + @Test public void testMobileStatsOemManaged() throws Exception { final NetworkTemplate templateOemPaid = new NetworkTemplate(MATCH_MOBILE_WILDCARD, @@ -1115,7 +1154,8 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { // pretend that network comes online expectDefaultSettings(); NetworkStateSnapshot[] states = - new NetworkStateSnapshot[] {buildMobile3gState(IMSI_1, true /* isRoaming */)}; + new NetworkStateSnapshot[] {buildMobileState(TEST_IFACE, IMSI_1, + false /* isTemporarilyNotMetered */, true /* isRoaming */)}; expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); @@ -1154,7 +1194,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { // pretend first mobile network comes online expectDefaultSettings(); final NetworkStateSnapshot[] states = - new NetworkStateSnapshot[]{buildMobile3gState(IMSI_1)}; + new NetworkStateSnapshot[]{buildMobileState(IMSI_1)}; expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); @@ -1489,7 +1529,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { final NetworkTemplate templateAll = buildTemplateMobileWithRatType(null, NETWORK_TYPE_ALL, METERED_YES); final NetworkStateSnapshot[] states = - new NetworkStateSnapshot[]{buildMobile3gState(IMSI_1)}; + new NetworkStateSnapshot[]{buildMobileState(IMSI_1)}; expectNetworkStatsSummary(buildEmptyStats()); expectNetworkStatsUidDetail(buildEmptyStats()); @@ -1566,7 +1606,7 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { // Pretend mobile network comes online, but wifi is the default network. expectDefaultSettings(); NetworkStateSnapshot[] states = new NetworkStateSnapshot[]{ - buildWifiState(true /*isMetered*/, TEST_IFACE2), buildMobile3gState(IMSI_1)}; + buildWifiState(true /*isMetered*/, TEST_IFACE2), buildMobileState(IMSI_1)}; expectNetworkStatsUidDetail(buildEmptyStats()); mService.notifyNetworkStatus(NETWORKS_WIFI, states, getActiveIface(states), new UnderlyingNetworkInfo[0]); @@ -1660,6 +1700,8 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { return states[0].getLinkProperties().getInterfaceName(); } + // TODO: These expect* methods are used to have NetworkStatsService returns the given stats + // instead of expecting anything. Therefore, these methods should be renamed properly. private void expectNetworkStatsSummary(NetworkStats summary) throws Exception { expectNetworkStatsSummaryDev(summary.clone()); expectNetworkStatsSummaryXt(summary.clone()); @@ -1749,15 +1791,21 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { return new NetworkStateSnapshot(WIFI_NETWORK, capabilities, prop, subscriberId, TYPE_WIFI); } - private static NetworkStateSnapshot buildMobile3gState(String subscriberId) { - return buildMobile3gState(subscriberId, false /* isRoaming */); + private static NetworkStateSnapshot buildMobileState(String subscriberId) { + return buildMobileState(TEST_IFACE, subscriberId, false /* isTemporarilyNotMetered */, + false /* isRoaming */); } - private static NetworkStateSnapshot buildMobile3gState(String subscriberId, boolean isRoaming) { + private static NetworkStateSnapshot buildMobileState(String iface, String subscriberId, + boolean isTemporarilyNotMetered, boolean isRoaming) { final LinkProperties prop = new LinkProperties(); - prop.setInterfaceName(TEST_IFACE); + prop.setInterfaceName(iface); final NetworkCapabilities capabilities = new NetworkCapabilities(); - capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_METERED, false); + + if (isTemporarilyNotMetered) { + capabilities.addCapability( + NetworkCapabilities.NET_CAPABILITY_TEMPORARILY_NOT_METERED); + } capabilities.setCapability(NetworkCapabilities.NET_CAPABILITY_NOT_ROAMING, !isRoaming); capabilities.addTransportType(NetworkCapabilities.TRANSPORT_CELLULAR); return new NetworkStateSnapshot(