From ea0732e6153e921f367897f9f997e8763b3891a2 Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Fri, 10 Sep 2021 16:18:21 +0800 Subject: [PATCH 1/2] Remove ForceAllNetworkTypes from test Before sForceAllNetworkTypes is removed, the network type and meteredness will be ignored when matchesMobile or matchesMobileWildcard is called. After sForceAllNetworkTypes is removed, the matches method should check the network type and the meteredness. Thus, if the test data contains different type or it's not metered should not be counted. Bug: 183776809 Test: FrameworksNetTests Change-Id: Ie7194495d26c0f5ef7a247733f43c64688626c67 Merged-In: Ie7194495d26c0f5ef7a247733f43c64688626c67 --- .../server/net/NetworkStatsCollectionTest.java | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/tests/unit/java/com/android/server/net/NetworkStatsCollectionTest.java b/tests/unit/java/com/android/server/net/NetworkStatsCollectionTest.java index e771558a20..6b4ead5b9b 100644 --- a/tests/unit/java/com/android/server/net/NetworkStatsCollectionTest.java +++ b/tests/unit/java/com/android/server/net/NetworkStatsCollectionTest.java @@ -98,14 +98,11 @@ public class NetworkStatsCollectionTest { @Before public void setUp() throws Exception { sOriginalClock = RecurrenceRule.sClock; - // ignore any device overlay while testing - NetworkTemplate.forceAllNetworkTypes(); } @After public void tearDown() throws Exception { RecurrenceRule.sClock = sOriginalClock; - NetworkTemplate.resetForceAllNetworkTypes(); } private void setClock(Instant instant) { @@ -123,7 +120,7 @@ public class NetworkStatsCollectionTest { // verify that history read correctly assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), - 636016770L, 709306L, 88038768L, 518836L, NetworkStatsAccess.Level.DEVICE); + 636014522L, 709291L, 88037144L, 518820L, NetworkStatsAccess.Level.DEVICE); // now export into a unified format final ByteArrayOutputStream bos = new ByteArrayOutputStream(); @@ -137,7 +134,7 @@ public class NetworkStatsCollectionTest { // and read back into structure, verifying that totals are same collection.read(new ByteArrayInputStream(bos.toByteArray())); assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), - 636016770L, 709306L, 88038768L, 518836L, NetworkStatsAccess.Level.DEVICE); + 636014522L, 709291L, 88037144L, 518820L, NetworkStatsAccess.Level.DEVICE); } @Test @@ -151,7 +148,7 @@ public class NetworkStatsCollectionTest { // verify that history read correctly assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), - 637076152L, 711413L, 88343717L, 521022L, NetworkStatsAccess.Level.DEVICE); + 637073904L, 711398L, 88342093L, 521006L, NetworkStatsAccess.Level.DEVICE); // now export into a unified format final ByteArrayOutputStream bos = new ByteArrayOutputStream(); @@ -165,7 +162,7 @@ public class NetworkStatsCollectionTest { // and read back into structure, verifying that totals are same collection.read(new ByteArrayInputStream(bos.toByteArray())); assertSummaryTotal(collection, buildTemplateMobileAll(TEST_IMSI), - 637076152L, 711413L, 88343717L, 521022L, NetworkStatsAccess.Level.DEVICE); + 637073904L, 711398L, 88342093L, 521006L, NetworkStatsAccess.Level.DEVICE); } @Test From 35bbf12886ac6b7eb8578319abce2024d61fec2c Mon Sep 17 00:00:00 2001 From: Aaron Huang Date: Tue, 3 Aug 2021 09:53:49 +0800 Subject: [PATCH 2/2] Test a template matches mobile and mobile wildcard Add two test cases for matchesMobile and matchesMobileWildcard Bug: 183776809 Test: FrameworksNetTests:NetworkTemplateTest Change-Id: I14d31071655204fe6ad36c71f7f521640e01c96e Merged-In: I14d31071655204fe6ad36c71f7f521640e01c96e --- .../java/android/net/NetworkTemplateTest.kt | 53 +++++++++++++++++++ 1 file changed, 53 insertions(+) diff --git a/tests/unit/java/android/net/NetworkTemplateTest.kt b/tests/unit/java/android/net/NetworkTemplateTest.kt index 49c7271c56..9ff594ab1d 100644 --- a/tests/unit/java/android/net/NetworkTemplateTest.kt +++ b/tests/unit/java/android/net/NetworkTemplateTest.kt @@ -39,6 +39,8 @@ import android.net.NetworkTemplate.OEM_MANAGED_YES import android.net.NetworkTemplate.SUBSCRIBER_ID_MATCH_RULE_EXACT import android.net.NetworkTemplate.WIFI_NETWORKID_ALL import android.net.NetworkTemplate.buildTemplateCarrierMetered +import android.net.NetworkTemplate.buildTemplateMobileAll +import android.net.NetworkTemplate.buildTemplateMobileWildcard import android.net.NetworkTemplate.buildTemplateMobileWithRatType import android.net.NetworkTemplate.buildTemplateWifi import android.net.NetworkTemplate.buildTemplateWifiWildcard @@ -170,6 +172,57 @@ class NetworkTemplateTest { templateWifiSsidAllImsi1.assertMatches(identWifiImsi1Ssid2) } + @Test + fun testMobileMatches() { + val templateMobileImsi1 = buildTemplateMobileAll(TEST_IMSI1) + val templateMobileImsi2WithRatType = buildTemplateMobileWithRatType(TEST_IMSI2, + TelephonyManager.NETWORK_TYPE_UMTS) + + val mobileImsi1 = buildNetworkState(TYPE_MOBILE, TEST_IMSI1, null /* ssid */, + OEM_NONE, true /* metered */) + val identMobile1 = buildNetworkIdentity(mockContext, mobileImsi1, + false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS) + val mobileImsi2 = buildMobileNetworkState(TEST_IMSI2) + val identMobile2Umts = buildNetworkIdentity(mockContext, mobileImsi2, + false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS) + + val identWifiImsi1Ssid1 = buildNetworkIdentity( + mockContext, buildWifiNetworkState(TEST_IMSI1, TEST_SSID1), true, 0) + + // Verify that the template matches type and the subscriberId. + templateMobileImsi1.assertMatches(identMobile1) + templateMobileImsi2WithRatType.assertMatches(identMobile2Umts) + + // Verify that the template does not match the different subscriberId. + templateMobileImsi1.assertDoesNotMatch(identMobile2Umts) + templateMobileImsi2WithRatType.assertDoesNotMatch(identMobile1) + + // Verify that the different type does not match. + templateMobileImsi1.assertDoesNotMatch(identWifiImsi1Ssid1) + } + + @Test + fun testMobileWildcardMatches() { + val templateMobileWildcard = buildTemplateMobileWildcard() + val templateMobileNullImsiWithRatType = buildTemplateMobileWithRatType(null, + TelephonyManager.NETWORK_TYPE_UMTS) + + val mobileImsi1 = buildMobileNetworkState(TEST_IMSI1) + val identMobile1 = buildNetworkIdentity(mockContext, mobileImsi1, + false /* defaultNetwork */, TelephonyManager.NETWORK_TYPE_UMTS) + + // Verify that the template matches any subscriberId. + templateMobileWildcard.assertMatches(identMobile1) + templateMobileNullImsiWithRatType.assertMatches(identMobile1) + + val identWifiImsi1Ssid1 = buildNetworkIdentity( + mockContext, buildWifiNetworkState(TEST_IMSI1, TEST_SSID1), true, 0) + + // Verify that the different type does not match. + templateMobileWildcard.assertDoesNotMatch(identWifiImsi1Ssid1) + templateMobileNullImsiWithRatType.assertDoesNotMatch(identWifiImsi1Ssid1) + } + @Test fun testCarrierMeteredMatches() { val templateCarrierImsi1Metered = buildTemplateCarrierMetered(TEST_IMSI1)