Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 5f369de3 authored by Michele Berionne's avatar Michele Berionne Committed by android-build-merger
Browse files

Merge "Add API to check if a SIM card matches carrier restrictions." am: b4d55f76

am: 43fd63d9

Change-Id: I443df3b13db376890ab55a0544ad7bd7b4d266e6
parents d9ca2b38 43fd63d9
Loading
Loading
Loading
Loading
+169 −1
Original line number Diff line number Diff line
@@ -23,6 +23,8 @@ import android.test.AndroidTestCase;
import android.test.suitebuilder.annotation.SmallTest;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

/** Unit tests for {@link CarrierRestrictionRules}. */

@@ -32,11 +34,22 @@ public class CarrierRestrictionRulesTest extends AndroidTestCase {
    private static final String MNC1 = "210";
    private static final String MCC2 = "120";
    private static final String MNC2 = "220";
    private static final String MCC3 = "130";
    private static final String MNC3 = "230";
    private static final String MCC4 = "140";
    private static final String MNC4 = "240";
    private static final String MCC5 = "150";
    private static final String MNC5 = "250";
    private static final String MCC6 = "360";
    private static final String MNC6 = "380";
    private static final String MCC1_WILDCHAR = "3??";
    private static final String MNC1_WILDCHAR = "???";
    private static final String MCC2_WILDCHAR = "31?";
    private static final String MNC2_WILDCHAR = "?0";
    private static final String SPN = "spn";
    private static final String IMSI = "123456";
    private static final String GID1 = "80";
    private static final String GID2 = "506090";

    @SmallTest
    public void testBuilderAllowedAndExcludedCarriers() {
@@ -101,7 +114,7 @@ public class CarrierRestrictionRulesTest extends AndroidTestCase {
        allowedCarriers.add(new CarrierIdentifier(MCC2, MNC2, null, null, null, null));

        ArrayList<CarrierIdentifier> excludedCarriers = new ArrayList<>();
        allowedCarriers.add(new CarrierIdentifier(MCC2, MNC2, null, null, GID1, null));
        excludedCarriers.add(new CarrierIdentifier(MCC2, MNC2, null, null, GID1, null));

        CarrierRestrictionRules rules = CarrierRestrictionRules.newBuilder()
                .setAllowedCarriers(allowedCarriers)
@@ -172,4 +185,159 @@ public class CarrierRestrictionRulesTest extends AndroidTestCase {

        assertEquals(CarrierRestrictionRules.MULTISIM_POLICY_NONE, rules.getMultiSimPolicy());
    }

    @SmallTest
    public void testTestCarrierId_allCarriersAllowed() {
        // Carrier lock configuration
        CarrierRestrictionRules rules = CarrierRestrictionRules.newBuilder()
                .setAllCarriersAllowed()
                .build();

        // Carrier list for test
        ArrayList<CarrierIdentifier> list = new ArrayList<>();
        list.add(new CarrierIdentifier(MCC1, MNC1, null, null, null, null));
        list.add(new CarrierIdentifier(MCC1, MNC1, SPN, null, GID1, GID2));
        list.add(new CarrierIdentifier(MCC2, MNC2, SPN, null, null, null));
        list.add(new CarrierIdentifier(MCC3, MNC3, null, IMSI, null, null));
        list.add(new CarrierIdentifier(MCC4, MNC4, null, null, GID1, null));
        list.add(new CarrierIdentifier(MCC6, MNC1, null, null, null, null));
        list.add(new CarrierIdentifier(MCC1, MNC6, null, null, null, null));
        list.add(new CarrierIdentifier(MCC2, MNC2, null, null, null, null));
        list.add(new CarrierIdentifier(MCC3, MNC3, null, null, null, null));
        list.add(new CarrierIdentifier(MCC4, MNC4, null, null, null, null));
        list.add(new CarrierIdentifier(MCC5, MNC5, null, null, null, null));

        List<Boolean> result = rules.isCarrierIdentifiersAllowed(list);

        List<Boolean> expected =
                Arrays.asList(true, true, true, true, true, true, true, true, true, true, true);
        assertTrue(result.equals(expected));
    }

    @SmallTest
    public void testTestCarrierId_defaultNotAllowed() {
        // Carrier lock configuration
        ArrayList<CarrierIdentifier> allowedCarriers = new ArrayList<>();
        allowedCarriers.add(new CarrierIdentifier(MCC1, MNC1, null, null, null, null));
        allowedCarriers.add(new CarrierIdentifier(MCC2, MNC2, SPN, null, null, null));
        allowedCarriers.add(new CarrierIdentifier(MCC3, MNC3, null, IMSI, null, null));
        allowedCarriers.add(new CarrierIdentifier(MCC4, MNC4, null, null, GID1, null));
        allowedCarriers.add(new CarrierIdentifier(MCC1_WILDCHAR, MNC1, null, null, null, null));
        allowedCarriers.add(new CarrierIdentifier(MCC1, MNC1_WILDCHAR, null, null, null, null));

        CarrierRestrictionRules rules = CarrierRestrictionRules.newBuilder()
                .setAllowedCarriers(allowedCarriers)
                .setDefaultCarrierRestriction(
                    CarrierRestrictionRules.CARRIER_RESTRICTION_DEFAULT_NOT_ALLOWED)
                .build();

        // Carrier list for test
        ArrayList<CarrierIdentifier> list = new ArrayList<>();
        // Allowed carriers
        list.add(new CarrierIdentifier(MCC1, MNC1, null, null, null, null));
        list.add(new CarrierIdentifier(MCC1, MNC1, SPN, null, GID1, GID2));
        list.add(new CarrierIdentifier(MCC2, MNC2, SPN, null, null, null));
        list.add(new CarrierIdentifier(MCC3, MNC3, null, IMSI, null, null));
        list.add(new CarrierIdentifier(MCC4, MNC4, null, null, GID1, null));
        list.add(new CarrierIdentifier(MCC6, MNC1, null, null, null, null));
        list.add(new CarrierIdentifier(MCC1, MNC6, null, null, null, null));
        // Excluded carriers
        list.add(new CarrierIdentifier(MCC2, MNC2, null, null, null, null));
        list.add(new CarrierIdentifier(MCC3, MNC3, null, null, null, null));
        list.add(new CarrierIdentifier(MCC4, MNC4, null, null, null, null));
        list.add(new CarrierIdentifier(MCC5, MNC5, null, null, null, null));

        List<Boolean> result = rules.isCarrierIdentifiersAllowed(list);

        List<Boolean> expected =
                Arrays.asList(true, true, true, true, true, true, true,
                    false, false, false, false);
        assertTrue(result.equals(expected));
    }

    @SmallTest
    public void testTestCarrierId_defaultAllowed() {
        // Carrier lock configuration
        ArrayList<CarrierIdentifier> allowedCarriers = new ArrayList<>();
        ArrayList<CarrierIdentifier> excludedCarriers = new ArrayList<>();
        excludedCarriers.add(new CarrierIdentifier(MCC1, MNC1_WILDCHAR, null, null, null, null));
        allowedCarriers.add(new CarrierIdentifier(MCC1, MNC1, null, null, null, null));
        allowedCarriers.add(new CarrierIdentifier(MCC1, MNC2, null, null, null, null));

        CarrierRestrictionRules rules = CarrierRestrictionRules.newBuilder()
                .setAllowedCarriers(allowedCarriers)
                .setExcludedCarriers(excludedCarriers)
                .setDefaultCarrierRestriction(
                    CarrierRestrictionRules.CARRIER_RESTRICTION_DEFAULT_ALLOWED)
                .build();

        // Carrier list for test
        ArrayList<CarrierIdentifier> list = new ArrayList<>();
        // Allowed carriers
        list.add(new CarrierIdentifier(MCC1, MNC1, null, null, null, null));
        list.add(new CarrierIdentifier(MCC1, MNC2, null, null, null, null));
        list.add(new CarrierIdentifier(MCC2, MNC2, null, null, null, null));
        list.add(new CarrierIdentifier(MCC3, MNC3, null, null, null, null));
        // Excluded carriers
        list.add(new CarrierIdentifier(MCC1, MNC3, null, null, null, null));
        list.add(new CarrierIdentifier(MCC1, MNC4, null, null, null, null));

        List<Boolean> result = rules.isCarrierIdentifiersAllowed(list);

        List<Boolean> expected = Arrays.asList(true, true, true, true, false, false);
        assertTrue(result.equals(expected));
    }

    @SmallTest
    public void testTestCarrierId_OneValidCardMustBePresentPolicy_CardPresent() {
        // Carrier lock configuration
        ArrayList<CarrierIdentifier> allowedCarriers = new ArrayList<>();
        allowedCarriers.add(new CarrierIdentifier(MCC1, MNC1, null, null, null, null));
        allowedCarriers.add(new CarrierIdentifier(MCC2, MNC2, null, null, null, null));

        CarrierRestrictionRules rules = CarrierRestrictionRules.newBuilder()
                .setAllowedCarriers(allowedCarriers)
                .setMultiSimPolicy(
                    CarrierRestrictionRules.MULTISIM_POLICY_ONE_VALID_SIM_MUST_BE_PRESENT)
                .build();

        // Carrier list for test: this contains at least one valid carrier and one non valid
        // carrier to verify that multisim policy is applied correctly.
        ArrayList<CarrierIdentifier> list = new ArrayList<>();
        list.add(new CarrierIdentifier(MCC1, MNC1, SPN, null, GID1, GID2));  // allowed carrier
        list.add(new CarrierIdentifier(MCC3, MNC3, null, null, null, null));
        list.add(new CarrierIdentifier(MCC4, MNC4, null, null, null, null));
        list.add(new CarrierIdentifier(MCC5, MNC5, null, null, null, null));

        List<Boolean> result = rules.isCarrierIdentifiersAllowed(list);

        List<Boolean> expected = Arrays.asList(true, true, true, true);
        assertTrue(result.equals(expected));
    }

    @SmallTest
    public void testTestCarrierId_OneValidCardMustBePresentPolicy_CardNotPresent() {
        // Carrier lock configuration
        ArrayList<CarrierIdentifier> allowedCarriers = new ArrayList<>();
        allowedCarriers.add(new CarrierIdentifier(MCC1, MNC1, null, null, null, null));
        allowedCarriers.add(new CarrierIdentifier(MCC2, MNC2, null, null, null, null));

        CarrierRestrictionRules rules = CarrierRestrictionRules.newBuilder()
                .setAllowedCarriers(allowedCarriers)
                .setMultiSimPolicy(
                    CarrierRestrictionRules.MULTISIM_POLICY_ONE_VALID_SIM_MUST_BE_PRESENT)
                .build();

        // Carrier list for test. This does not contain any valid carrier to verify that multisim
        // policy is applied correctly.
        ArrayList<CarrierIdentifier> list = new ArrayList<>();
        list.add(new CarrierIdentifier(MCC3, MNC3, null, null, null, null));
        list.add(new CarrierIdentifier(MCC4, MNC4, null, null, null, null));
        list.add(new CarrierIdentifier(MCC5, MNC5, null, null, null, null));

        List<Boolean> result = rules.isCarrierIdentifiersAllowed(list);

        List<Boolean> expected = Arrays.asList(false, false, false);
        assertTrue(result.equals(expected));
    }
}