Merge "Fix an issue in NPMS where ALLOWED_REASON_SYSTEM will be ignored." am: fe7a49b5d0 am: c888b0b3bb
Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1664154 Change-Id: I6ee3a10087df100a12c005fa1e8ff9fa3717d939
This commit is contained in:
@@ -772,6 +772,12 @@ public class NetworkPolicyManager {
|
|||||||
return DebugUtils.flagsToString(NetworkPolicyManager.class, "BLOCKED_", blockedReasons);
|
return DebugUtils.flagsToString(NetworkPolicyManager.class, "BLOCKED_", blockedReasons);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @hide */
|
||||||
|
@NonNull
|
||||||
|
public static String allowedReasonsToString(int allowedReasons) {
|
||||||
|
return DebugUtils.flagsToString(NetworkPolicyManager.class, "ALLOWED_", allowedReasons);
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Register a {@link NetworkPolicyCallback} to listen for changes to network blocked status
|
* Register a {@link NetworkPolicyCallback} to listen for changes to network blocked status
|
||||||
* of apps.
|
* of apps.
|
||||||
|
|||||||
@@ -5853,7 +5853,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
|
|||||||
return (bundle != null) ? bundle.getBoolean(key, defaultValue) : defaultValue;
|
return (bundle != null) ? bundle.getBoolean(key, defaultValue) : defaultValue;
|
||||||
}
|
}
|
||||||
|
|
||||||
private class UidBlockedState {
|
@VisibleForTesting
|
||||||
|
static final class UidBlockedState {
|
||||||
public int blockedReasons;
|
public int blockedReasons;
|
||||||
public int allowedReasons;
|
public int allowedReasons;
|
||||||
public int effectiveBlockedReasons;
|
public int effectiveBlockedReasons;
|
||||||
@@ -5865,16 +5866,21 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
|
|||||||
}
|
}
|
||||||
|
|
||||||
void updateEffectiveBlockedReasons() {
|
void updateEffectiveBlockedReasons() {
|
||||||
effectiveBlockedReasons = blockedReasons;
|
effectiveBlockedReasons = getEffectiveBlockedReasons(blockedReasons, allowedReasons);
|
||||||
|
}
|
||||||
|
|
||||||
|
@VisibleForTesting
|
||||||
|
static int getEffectiveBlockedReasons(int blockedReasons, int allowedReasons) {
|
||||||
|
int effectiveBlockedReasons = blockedReasons;
|
||||||
// If the uid is not subject to any blocked reasons, then return early
|
// If the uid is not subject to any blocked reasons, then return early
|
||||||
if (blockedReasons == BLOCKED_REASON_NONE) {
|
if (blockedReasons == BLOCKED_REASON_NONE) {
|
||||||
return;
|
return effectiveBlockedReasons;
|
||||||
}
|
}
|
||||||
if ((allowedReasons & ALLOWED_REASON_SYSTEM) != 0) {
|
if ((allowedReasons & ALLOWED_REASON_SYSTEM) != 0) {
|
||||||
effectiveBlockedReasons = (blockedReasons & ALLOWED_METERED_REASON_MASK);
|
effectiveBlockedReasons &= ALLOWED_METERED_REASON_MASK;
|
||||||
}
|
}
|
||||||
if ((allowedReasons & ALLOWED_METERED_REASON_SYSTEM) != 0) {
|
if ((allowedReasons & ALLOWED_METERED_REASON_SYSTEM) != 0) {
|
||||||
effectiveBlockedReasons = (blockedReasons & ~ALLOWED_METERED_REASON_MASK);
|
effectiveBlockedReasons &= ~ALLOWED_METERED_REASON_MASK;
|
||||||
}
|
}
|
||||||
if ((allowedReasons & ALLOWED_REASON_FOREGROUND) != 0) {
|
if ((allowedReasons & ALLOWED_REASON_FOREGROUND) != 0) {
|
||||||
effectiveBlockedReasons &= ~BLOCKED_REASON_BATTERY_SAVER;
|
effectiveBlockedReasons &= ~BLOCKED_REASON_BATTERY_SAVER;
|
||||||
@@ -5900,6 +5906,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
|
|||||||
if ((allowedReasons & ALLOWED_METERED_REASON_USER_EXEMPTED) != 0) {
|
if ((allowedReasons & ALLOWED_METERED_REASON_USER_EXEMPTED) != 0) {
|
||||||
effectiveBlockedReasons &= ~BLOCKED_METERED_REASON_DATA_SAVER;
|
effectiveBlockedReasons &= ~BLOCKED_METERED_REASON_DATA_SAVER;
|
||||||
}
|
}
|
||||||
|
return effectiveBlockedReasons;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -18,6 +18,12 @@ package com.android.server.net;
|
|||||||
|
|
||||||
import static android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS;
|
import static android.Manifest.permission.CONNECTIVITY_USE_RESTRICTED_NETWORKS;
|
||||||
import static android.Manifest.permission.NETWORK_STACK;
|
import static android.Manifest.permission.NETWORK_STACK;
|
||||||
|
import static android.net.ConnectivityManager.BLOCKED_METERED_REASON_DATA_SAVER;
|
||||||
|
import static android.net.ConnectivityManager.BLOCKED_METERED_REASON_USER_RESTRICTED;
|
||||||
|
import static android.net.ConnectivityManager.BLOCKED_REASON_APP_STANDBY;
|
||||||
|
import static android.net.ConnectivityManager.BLOCKED_REASON_BATTERY_SAVER;
|
||||||
|
import static android.net.ConnectivityManager.BLOCKED_REASON_DOZE;
|
||||||
|
import static android.net.ConnectivityManager.BLOCKED_REASON_NONE;
|
||||||
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
|
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
|
||||||
import static android.net.ConnectivityManager.TYPE_MOBILE;
|
import static android.net.ConnectivityManager.TYPE_MOBILE;
|
||||||
import static android.net.ConnectivityManager.TYPE_WIFI;
|
import static android.net.ConnectivityManager.TYPE_WIFI;
|
||||||
@@ -29,10 +35,17 @@ import static android.net.NetworkCapabilities.TRANSPORT_WIFI;
|
|||||||
import static android.net.NetworkPolicy.LIMIT_DISABLED;
|
import static android.net.NetworkPolicy.LIMIT_DISABLED;
|
||||||
import static android.net.NetworkPolicy.SNOOZE_NEVER;
|
import static android.net.NetworkPolicy.SNOOZE_NEVER;
|
||||||
import static android.net.NetworkPolicy.WARNING_DISABLED;
|
import static android.net.NetworkPolicy.WARNING_DISABLED;
|
||||||
|
import static android.net.NetworkPolicyManager.ALLOWED_METERED_REASON_FOREGROUND;
|
||||||
|
import static android.net.NetworkPolicyManager.ALLOWED_METERED_REASON_SYSTEM;
|
||||||
|
import static android.net.NetworkPolicyManager.ALLOWED_REASON_FOREGROUND;
|
||||||
|
import static android.net.NetworkPolicyManager.ALLOWED_REASON_NONE;
|
||||||
|
import static android.net.NetworkPolicyManager.ALLOWED_REASON_SYSTEM;
|
||||||
import static android.net.NetworkPolicyManager.FIREWALL_RULE_DEFAULT;
|
import static android.net.NetworkPolicyManager.FIREWALL_RULE_DEFAULT;
|
||||||
import static android.net.NetworkPolicyManager.POLICY_ALLOW_METERED_BACKGROUND;
|
import static android.net.NetworkPolicyManager.POLICY_ALLOW_METERED_BACKGROUND;
|
||||||
import static android.net.NetworkPolicyManager.POLICY_NONE;
|
import static android.net.NetworkPolicyManager.POLICY_NONE;
|
||||||
import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
|
import static android.net.NetworkPolicyManager.POLICY_REJECT_METERED_BACKGROUND;
|
||||||
|
import static android.net.NetworkPolicyManager.allowedReasonsToString;
|
||||||
|
import static android.net.NetworkPolicyManager.blockedReasonsToString;
|
||||||
import static android.net.NetworkPolicyManager.uidPoliciesToString;
|
import static android.net.NetworkPolicyManager.uidPoliciesToString;
|
||||||
import static android.net.NetworkPolicyManager.uidRulesToString;
|
import static android.net.NetworkPolicyManager.uidRulesToString;
|
||||||
import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK;
|
import static android.net.NetworkStack.PERMISSION_MAINLINE_NETWORK_STACK;
|
||||||
@@ -59,6 +72,7 @@ import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT;
|
|||||||
import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT_SNOOZED;
|
import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT_SNOOZED;
|
||||||
import static com.android.server.net.NetworkPolicyManagerService.TYPE_RAPID;
|
import static com.android.server.net.NetworkPolicyManagerService.TYPE_RAPID;
|
||||||
import static com.android.server.net.NetworkPolicyManagerService.TYPE_WARNING;
|
import static com.android.server.net.NetworkPolicyManagerService.TYPE_WARNING;
|
||||||
|
import static com.android.server.net.NetworkPolicyManagerService.UidBlockedState.getEffectiveBlockedReasons;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
import static org.junit.Assert.assertEquals;
|
||||||
import static org.junit.Assert.assertFalse;
|
import static org.junit.Assert.assertFalse;
|
||||||
@@ -134,8 +148,10 @@ import android.text.TextUtils;
|
|||||||
import android.util.ArrayMap;
|
import android.util.ArrayMap;
|
||||||
import android.util.DataUnit;
|
import android.util.DataUnit;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
import android.util.Pair;
|
||||||
import android.util.Range;
|
import android.util.Range;
|
||||||
import android.util.RecurrenceRule;
|
import android.util.RecurrenceRule;
|
||||||
|
import android.util.SparseArray;
|
||||||
|
|
||||||
import androidx.test.InstrumentationRegistry;
|
import androidx.test.InstrumentationRegistry;
|
||||||
import androidx.test.filters.FlakyTest;
|
import androidx.test.filters.FlakyTest;
|
||||||
@@ -1896,6 +1912,65 @@ public class NetworkPolicyManagerServiceTest {
|
|||||||
assertFalse(mService.isUidNetworkingBlocked(UID_E, false));
|
assertFalse(mService.isUidNetworkingBlocked(UID_E, false));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void testUpdateEffectiveBlockedReasons() {
|
||||||
|
final SparseArray<Pair<Integer, Integer>> effectiveBlockedReasons = new SparseArray<>();
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_NONE,
|
||||||
|
Pair.create(BLOCKED_REASON_NONE, ALLOWED_REASON_NONE));
|
||||||
|
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_NONE,
|
||||||
|
Pair.create(BLOCKED_REASON_BATTERY_SAVER, ALLOWED_REASON_SYSTEM));
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_NONE,
|
||||||
|
Pair.create(BLOCKED_REASON_BATTERY_SAVER | BLOCKED_REASON_DOZE,
|
||||||
|
ALLOWED_REASON_SYSTEM));
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_NONE,
|
||||||
|
Pair.create(BLOCKED_METERED_REASON_DATA_SAVER,
|
||||||
|
ALLOWED_METERED_REASON_SYSTEM));
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_NONE,
|
||||||
|
Pair.create(BLOCKED_METERED_REASON_DATA_SAVER
|
||||||
|
| BLOCKED_METERED_REASON_USER_RESTRICTED,
|
||||||
|
ALLOWED_METERED_REASON_SYSTEM));
|
||||||
|
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_METERED_REASON_DATA_SAVER,
|
||||||
|
Pair.create(BLOCKED_REASON_BATTERY_SAVER | BLOCKED_METERED_REASON_DATA_SAVER,
|
||||||
|
ALLOWED_REASON_SYSTEM));
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_APP_STANDBY,
|
||||||
|
Pair.create(BLOCKED_REASON_APP_STANDBY | BLOCKED_METERED_REASON_USER_RESTRICTED,
|
||||||
|
ALLOWED_METERED_REASON_SYSTEM));
|
||||||
|
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_NONE,
|
||||||
|
Pair.create(BLOCKED_REASON_BATTERY_SAVER, ALLOWED_REASON_FOREGROUND));
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_NONE,
|
||||||
|
Pair.create(BLOCKED_REASON_BATTERY_SAVER | BLOCKED_REASON_DOZE,
|
||||||
|
ALLOWED_REASON_FOREGROUND));
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_NONE,
|
||||||
|
Pair.create(BLOCKED_METERED_REASON_DATA_SAVER, ALLOWED_METERED_REASON_FOREGROUND));
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_NONE,
|
||||||
|
Pair.create(BLOCKED_METERED_REASON_DATA_SAVER
|
||||||
|
| BLOCKED_METERED_REASON_USER_RESTRICTED,
|
||||||
|
ALLOWED_METERED_REASON_FOREGROUND));
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_METERED_REASON_DATA_SAVER,
|
||||||
|
Pair.create(BLOCKED_REASON_BATTERY_SAVER | BLOCKED_METERED_REASON_DATA_SAVER,
|
||||||
|
ALLOWED_REASON_FOREGROUND));
|
||||||
|
effectiveBlockedReasons.put(BLOCKED_REASON_BATTERY_SAVER,
|
||||||
|
Pair.create(BLOCKED_REASON_BATTERY_SAVER
|
||||||
|
| BLOCKED_METERED_REASON_USER_RESTRICTED,
|
||||||
|
ALLOWED_METERED_REASON_FOREGROUND));
|
||||||
|
// TODO: test more combinations of blocked reasons.
|
||||||
|
|
||||||
|
for (int i = 0; i < effectiveBlockedReasons.size(); ++i) {
|
||||||
|
final int expectedEffectiveBlockedReasons = effectiveBlockedReasons.keyAt(i);
|
||||||
|
final int blockedReasons = effectiveBlockedReasons.valueAt(i).first;
|
||||||
|
final int allowedReasons = effectiveBlockedReasons.valueAt(i).second;
|
||||||
|
final String errorMsg = "Expected="
|
||||||
|
+ blockedReasonsToString(expectedEffectiveBlockedReasons)
|
||||||
|
+ "; blockedReasons=" + blockedReasonsToString(blockedReasons)
|
||||||
|
+ ", allowedReasons=" + allowedReasonsToString(allowedReasons);
|
||||||
|
assertEquals(errorMsg, expectedEffectiveBlockedReasons,
|
||||||
|
getEffectiveBlockedReasons(blockedReasons, allowedReasons));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
private String formatBlockedStateError(int uid, int rule, boolean metered,
|
private String formatBlockedStateError(int uid, int rule, boolean metered,
|
||||||
boolean backgroundRestricted) {
|
boolean backgroundRestricted) {
|
||||||
return String.format(
|
return String.format(
|
||||||
|
|||||||
Reference in New Issue
Block a user