Fix NPE in restoring network policies

When backing up network policies, we write null for policies that are
null or are inferred. When restoring, this sends null policies to be set
which results in a NPE.

Bug: 79961560
Test: 1) atest NetworkPolicyManagerServiceTest
2) Manual: adb restore using backup set with null policies does not
crash

Change-Id: I450685e38acae0658ea984b86ca8b17ca27a71a6
This commit is contained in:
Annie Meng
2018-05-18 15:00:49 +01:00
parent 167b451daa
commit 20b4d846ed
2 changed files with 17 additions and 0 deletions

View File

@@ -2706,6 +2706,9 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
private void normalizePoliciesNL(NetworkPolicy[] policies) {
mNetworkPolicy.clear();
for (NetworkPolicy policy : policies) {
if (policy == null) {
continue;
}
// When two normalized templates conflict, prefer the most
// restrictive policy
policy.template = NetworkTemplate.normalize(policy.template, mMergedSubscriberIds);

View File

@@ -1628,6 +1628,20 @@ public class NetworkPolicyManagerServiceTest {
}
}
/**
* Test that policy set of {null, NetworkPolicy, null} does not crash and restores the valid
* NetworkPolicy.
*/
@Test
public void testSetNetworkPolicies_withNullPolicies_doesNotThrow() {
NetworkPolicy[] policies = new NetworkPolicy[3];
policies[1] = buildDefaultFakeMobilePolicy();
setNetworkPolicies(policies);
assertNetworkPolicyEquals(DEFAULT_CYCLE_DAY, mDefaultWarningBytes, mDefaultLimitBytes,
true);
}
private SubscriptionPlan buildMonthlyDataPlan(ZonedDateTime start, long limitBytes) {
return SubscriptionPlan.Builder
.createRecurringMonthly(start)