Merge "Do not crash when duplication found in merged imsi list" into tm-qpr-dev

This commit is contained in:
Junyu Lai
2022-12-15 06:21:58 +00:00
committed by Android (Google) Code Review
2 changed files with 23 additions and 2 deletions

View File

@@ -123,6 +123,7 @@ import static android.telephony.CarrierConfigManager.KEY_DATA_RAPID_NOTIFICATION
import static android.telephony.CarrierConfigManager.KEY_DATA_WARNING_NOTIFICATION_BOOL; import static android.telephony.CarrierConfigManager.KEY_DATA_WARNING_NOTIFICATION_BOOL;
import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID; import static android.telephony.SubscriptionManager.INVALID_SUBSCRIPTION_ID;
import static com.android.internal.annotations.VisibleForTesting.Visibility.PRIVATE;
import static com.android.internal.util.ArrayUtils.appendInt; import static com.android.internal.util.ArrayUtils.appendInt;
import static com.android.internal.util.XmlUtils.readBooleanAttribute; import static com.android.internal.util.XmlUtils.readBooleanAttribute;
import static com.android.internal.util.XmlUtils.readIntAttribute; import static com.android.internal.util.XmlUtils.readIntAttribute;
@@ -3148,7 +3149,8 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
* active merge set [A,B], we'd return a new template that primarily matches * active merge set [A,B], we'd return a new template that primarily matches
* A, but also matches B. * A, but also matches B.
*/ */
private static NetworkTemplate normalizeTemplate(@NonNull NetworkTemplate template, @VisibleForTesting(visibility = PRIVATE)
static NetworkTemplate normalizeTemplate(@NonNull NetworkTemplate template,
@NonNull List<String[]> mergedList) { @NonNull List<String[]> mergedList) {
// Now there are several types of network which uses Subscriber Id to store network // Now there are several types of network which uses Subscriber Id to store network
// information. For instance: // information. For instance:
@@ -3158,6 +3160,12 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
if (template.getSubscriberIds().isEmpty()) return template; if (template.getSubscriberIds().isEmpty()) return template;
for (final String[] merged : mergedList) { for (final String[] merged : mergedList) {
// In some rare cases (e.g. b/243015487), merged subscriberId list might contain
// duplicated items. Deduplication for better error handling.
final ArraySet mergedSet = new ArraySet(merged);
if (mergedSet.size() != merged.length) {
Log.wtf(TAG, "Duplicated merged list detected: " + Arrays.toString(merged));
}
// TODO: Handle incompatible subscriberIds if that happens in practice. // TODO: Handle incompatible subscriberIds if that happens in practice.
for (final String subscriberId : template.getSubscriberIds()) { for (final String subscriberId : template.getSubscriberIds()) {
if (com.android.net.module.util.CollectionUtils.contains(merged, subscriberId)) { if (com.android.net.module.util.CollectionUtils.contains(merged, subscriberId)) {
@@ -3165,7 +3173,7 @@ public class NetworkPolicyManagerService extends INetworkPolicyManager.Stub {
// a template that matches all merged subscribers. // a template that matches all merged subscribers.
return new NetworkTemplate.Builder(template.getMatchRule()) return new NetworkTemplate.Builder(template.getMatchRule())
.setWifiNetworkKeys(template.getWifiNetworkKeys()) .setWifiNetworkKeys(template.getWifiNetworkKeys())
.setSubscriberIds(Set.of(merged)) .setSubscriberIds(mergedSet)
.setMeteredness(template.getMeteredness()) .setMeteredness(template.getMeteredness())
.build(); .build();
} }

View File

@@ -75,6 +75,7 @@ import static com.android.server.net.NetworkPolicyManagerService.TYPE_LIMIT_SNOO
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 com.android.server.net.NetworkPolicyManagerService.UidBlockedState.getEffectiveBlockedReasons;
import static com.android.server.net.NetworkPolicyManagerService.normalizeTemplate;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertFalse;
@@ -2059,6 +2060,18 @@ public class NetworkPolicyManagerServiceTest {
METERED_NO, actualPolicy.template.getMeteredness()); METERED_NO, actualPolicy.template.getMeteredness());
} }
@Test
public void testNormalizeTemplate_duplicatedMergedImsiList() {
final NetworkTemplate template = new NetworkTemplate.Builder(MATCH_CARRIER)
.setSubscriberIds(Set.of(TEST_IMSI)).build();
final String[] mergedImsiGroup = new String[] {TEST_IMSI, TEST_IMSI};
final ArrayList<String[]> mergedList = new ArrayList<>();
mergedList.add(mergedImsiGroup);
// Verify the duplicated items in the merged IMSI list won't crash the system.
final NetworkTemplate result = normalizeTemplate(template, mergedList);
assertEquals(template, result);
}
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(