Drop the redundant type and caller uid checks for TrafficStats

This commit removes the redundant type and caller uid
checks in TrafficStats to address the review feedback at
aosp/3313055.

Test: TH
Bug: 343260158
Change-Id: I0ed6e850835136c9a324efd7271dd80a34160aa6
This commit is contained in:
Junyu Lai 2024-11-04 10:26:31 +08:00
commit 739d200d35
2 changed files with 1 additions and 127 deletions

View file

@ -17,8 +17,6 @@
package android.net;
import static android.annotation.SystemApi.Client.MODULE_LIBRARIES;
import static android.net.NetworkStats.UID_ALL;
import static android.os.Process.SYSTEM_UID;
import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
@ -188,8 +186,6 @@ public class TrafficStats {
private static INetworkStatsService sStatsService;
@GuardedBy("TrafficStats.class")
private static INetworkStatsService sStatsServiceForTest = null;
@GuardedBy("TrafficStats.class")
private static int sMyUidForTest = UID_ALL;
@UnsupportedAppUsage(maxTargetSdk = Build.VERSION_CODES.P, trackingBug = 130143562)
private synchronized static INetworkStatsService getStatsService() {
@ -202,12 +198,7 @@ public class TrafficStats {
}
/** @hide */
protected static int getMyUid() {
synchronized (TrafficStats.class) {
if (sMyUidForTest != UID_ALL) {
return sMyUidForTest;
}
}
private static int getMyUid() {
return android.os.Process.myUid();
}
@ -223,18 +214,6 @@ public class TrafficStats {
}
}
/**
* Set myUid for test, or UID_ALL to reset.
*
* @hide
*/
@VisibleForTesting(visibility = PRIVATE)
public static void setMyUidForTest(int myUid) {
synchronized (TrafficStats.class) {
sMyUidForTest = myUid;
}
}
/**
* Snapshot of {@link NetworkStats} when the currently active profiling
* session started, or {@code null} if no session active.
@ -1006,13 +985,6 @@ public class TrafficStats {
/** @hide */
public static long getUidStats(int uid, int type) {
// Perform a quick check on the UID to avoid unnecessary work.
// This mirrors a similar check on the service side, but is primarily for
// efficiency rather than security, as user-space checks can be bypassed.
final int myUid = getMyUid();
if (!isEntryValueTypeValid(type) || (myUid != SYSTEM_UID && myUid != uid)) {
return UNSUPPORTED;
}
final StatsResult stats;
try {
stats = getStatsService().getUidStats(uid);
@ -1024,9 +996,6 @@ public class TrafficStats {
/** @hide */
public static long getTotalStats(int type) {
if (!isEntryValueTypeValid(type)) {
return UNSUPPORTED;
}
final StatsResult stats;
try {
stats = getStatsService().getTotalStats();
@ -1038,9 +1007,6 @@ public class TrafficStats {
/** @hide */
public static long getIfaceStats(String iface, int type) {
if (!isEntryValueTypeValid(type)) {
return UNSUPPORTED;
}
final StatsResult stats;
try {
stats = getStatsService().getIfaceStats(iface);

View file

@ -1,92 +0,0 @@
/*
* Copyright (C) 2024 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.net
import android.net.NetworkStats.UID_ALL
import android.net.TrafficStats.UNSUPPORTED
import android.net.netstats.StatsResult
import android.os.Build
import android.os.Process.SYSTEM_UID
import com.android.testutils.DevSdkIgnoreRule
import com.android.testutils.DevSdkIgnoreRunner
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mockito.doReturn
import org.mockito.Mockito.mock
import org.mockito.Mockito.never
import org.mockito.Mockito.verify
@RunWith(DevSdkIgnoreRunner::class)
@DevSdkIgnoreRule.IgnoreUpTo(Build.VERSION_CODES.S_V2)
class TrafficStatsTest {
private val binder = mock(INetworkStatsService::class.java)
private val myUid = android.os.Process.myUid()
private val notMyUid = myUid + 1
private val mockSystemUidStatsResult = StatsResult(1L, 2L, 3L, 4L)
private val mockMyUidStatsResult = StatsResult(5L, 6L, 7L, 8L)
private val mockNotMyUidStatsResult = StatsResult(9L, 10L, 11L, 12L)
private val unsupportedStatsResult =
StatsResult(UNSUPPORTED.toLong(), UNSUPPORTED.toLong(),
UNSUPPORTED.toLong(), UNSUPPORTED.toLong())
@Before
fun setUp() {
TrafficStats.setServiceForTest(binder)
doReturn(mockSystemUidStatsResult).`when`(binder).getUidStats(SYSTEM_UID)
doReturn(mockMyUidStatsResult).`when`(binder).getUidStats(myUid)
doReturn(mockNotMyUidStatsResult).`when`(binder).getUidStats(notMyUid)
}
@After
fun tearDown() {
TrafficStats.setServiceForTest(null)
TrafficStats.setMyUidForTest(UID_ALL)
}
private fun assertUidStats(uid: Int, stats: StatsResult) {
assertEquals(stats.rxBytes, TrafficStats.getUidRxBytes(uid))
assertEquals(stats.rxPackets, TrafficStats.getUidRxPackets(uid))
assertEquals(stats.txBytes, TrafficStats.getUidTxBytes(uid))
assertEquals(stats.txPackets, TrafficStats.getUidTxPackets(uid))
}
// Verify a normal caller could get a quick UNSUPPORTED result in the TrafficStats
// without accessing the service if query stats other than itself.
@Test
fun testGetUidStats_appCaller() {
assertUidStats(SYSTEM_UID, unsupportedStatsResult)
assertUidStats(notMyUid, unsupportedStatsResult)
verify(binder, never()).getUidStats(anyInt())
assertUidStats(myUid, mockMyUidStatsResult)
}
// Verify that callers with SYSTEM_UID can access network
// stats for other UIDs. While this behavior is not officially documented
// in the API, it exists for compatibility with existing callers that may
// rely on it.
@Test
fun testGetUidStats_systemCaller() {
TrafficStats.setMyUidForTest(SYSTEM_UID)
assertUidStats(SYSTEM_UID, mockSystemUidStatsResult)
assertUidStats(myUid, mockMyUidStatsResult)
assertUidStats(notMyUid, mockNotMyUidStatsResult)
}
}