Use PerUidCounter in the connectivity static library

This is a no-op refactoring which uses common PerUidCounter
from the static library and delete the private implementation
inside ConnectivityService. This refactoring includes:
  1. Make a private implementation that inherite PerUidCounter
     to convert and rethrow exception to maintain backward
     compatibility.
  2. Adjust per-uid max limit argument in the counter constructor
     since the private implementation is already buggy.
  3. Use the getter in PerUidCounter for the existing tests.

Test: testNetworkCallbackMaximum
      testProfileNetworkPrefCountsRequestsCorrectlyOnSet
      testRequestCountLimits
Bug: 235771502
Change-Id: I5c49edab18774acf819828201041c9931fabccc4
This commit is contained in:
Junyu Lai
2022-07-05 11:01:52 +08:00
parent 31d38bffdf
commit 00d92df645
3 changed files with 52 additions and 85 deletions

View File

@@ -15761,7 +15761,7 @@ public class ConnectivityServiceTest {
final int otherAppUid = UserHandle.getUid(TEST_WORK_PROFILE_USER_ID,
UserHandle.getAppId(Process.myUid() + 1));
final int remainingCount = ConnectivityService.MAX_NETWORK_REQUESTS_PER_UID
- mService.mNetworkRequestCounter.mUidToNetworkRequestCount.get(otherAppUid)
- mService.mNetworkRequestCounter.get(otherAppUid)
- 1;
final NetworkCallback[] callbacks = new NetworkCallback[remainingCount];
doAsUid(otherAppUid, () -> {
@@ -15815,8 +15815,7 @@ public class ConnectivityServiceTest {
@NonNull final ExceptionalRunnable r) throws Exception {
final ArraySet<TestNetworkCallback> callbacks = new ArraySet<>();
try {
final int requestCount = mService.mSystemNetworkRequestCounter
.mUidToNetworkRequestCount.get(Process.myUid());
final int requestCount = mService.mSystemNetworkRequestCounter.get(Process.myUid());
// The limit is hit when total requests = limit - 1, and exceeded with a crash when
// total requests >= limit.
final int countToFile =
@@ -15829,8 +15828,7 @@ public class ConnectivityServiceTest {
callbacks.add(cb);
}
assertEquals(MAX_NETWORK_REQUESTS_PER_SYSTEM_UID - 1 - countToLeaveAvailable,
mService.mSystemNetworkRequestCounter
.mUidToNetworkRequestCount.get(Process.myUid()));
mService.mSystemNetworkRequestCounter.get(Process.myUid()));
});
// Code to run to check if it triggers a max request count limit error.
r.run();