diff --git a/service-t/src/com/android/server/net/NetworkStatsService.java b/service-t/src/com/android/server/net/NetworkStatsService.java index f977a2724c..e7ef510160 100644 --- a/service-t/src/com/android/server/net/NetworkStatsService.java +++ b/service-t/src/com/android/server/net/NetworkStatsService.java @@ -46,6 +46,7 @@ import static android.net.NetworkStats.TAG_NONE; import static android.net.NetworkStats.UID_ALL; import static android.net.NetworkStatsHistory.FIELD_ALL; import static android.net.NetworkTemplate.MATCH_MOBILE; +import static android.net.NetworkTemplate.MATCH_TEST; import static android.net.NetworkTemplate.MATCH_WIFI; import static android.net.TrafficStats.KB_IN_BYTES; import static android.net.TrafficStats.MB_IN_BYTES; @@ -1582,7 +1583,9 @@ public class NetworkStatsService extends INetworkStatsService.Stub { // For a template with wifi network keys, it is possible for a malicious // client to track the user locations via querying data usage. Thus, enforce // fine location permission check. - if (!template.getWifiNetworkKeys().isEmpty()) { + // For a template with MATCH_TEST, since the wifi network key is just a placeholder + // to identify a specific test network, it is not related to track user location. + if (!template.getWifiNetworkKeys().isEmpty() && template.getMatchRule() != MATCH_TEST) { final boolean canAccessFineLocation = mLocationPermissionChecker .checkCallersLocationPermission(callingPackage, null /* featureId */, diff --git a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java index 99f6d638ca..b8b0289b06 100644 --- a/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java +++ b/tests/unit/java/com/android/server/net/NetworkStatsServiceTest.java @@ -1926,12 +1926,17 @@ public class NetworkStatsServiceTest extends NetworkStatsBaseTest { // Templates w/o wifi network keys can query stats as usual. assertNetworkTotal(sTemplateCarrierWifi1, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateImsi1, 0L, 0L, 0L, 0L, 0); + // Templates for test network does not need to enforce location permission. + final NetworkTemplate templateTestIface1 = new NetworkTemplate.Builder(MATCH_TEST) + .setWifiNetworkKeys(Set.of(TEST_IFACE)).build(); + assertNetworkTotal(templateTestIface1, 0L, 0L, 0L, 0L, 0); doReturn(true).when(mLocationPermissionChecker) .checkCallersLocationPermission(any(), any(), anyInt(), anyBoolean(), any()); assertNetworkTotal(sTemplateCarrierWifi1, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateWifi, 0L, 0L, 0L, 0L, 0); assertNetworkTotal(sTemplateImsi1, 0L, 0L, 0L, 0L, 0); + assertNetworkTotal(templateTestIface1, 0L, 0L, 0L, 0L, 0); } /**