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

Commit da537f95 authored by Brad Ebinger's avatar Brad Ebinger Committed by Thomas Stuart
Browse files

Adds code to enforce size limits on simultaneous calling restrictions

Bug: 319904227
Test: atest TelecomUnitTests:PhoneAccountRegistrarTest
Change-Id: I2bb58e0bac6c8f55e7d8a8d793f9bf88d5c67ad3
parent 6403236f
Loading
Loading
Loading
Loading
+41 −0
Original line number Diff line number Diff line
@@ -85,6 +85,7 @@ import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.stream.Collectors;
@@ -951,6 +952,9 @@ public class PhoneAccountRegistrar {
        enforceCharacterLimit(account);
        enforceIconSizeLimit(account);
        enforceMaxPhoneAccountLimit(account);
        if (mTelephonyFeatureFlags.simultaneousCallingIndications()) {
            enforceSimultaneousCallingRestrictionLimit(account);
        }
        addOrReplacePhoneAccount(account);
    }

@@ -1078,6 +1082,43 @@ public class PhoneAccountRegistrar {
        }
    }

    /**
     * Enforce size limits on the simultaneous calling restriction of a PhoneAccount.
     * If a PhoneAccount has a simultaneous calling restriction on it, enforce the following: the
     * number of PhoneAccountHandles in the Set can not exceed the per app restriction on
     * PhoneAccounts registered and each PhoneAccountHandle's fields must not exceed the per field
     * character limit.
     * @param account The PhoneAccount to enforce simultaneous calling restrictions on.
     * @throws IllegalArgumentException if the PhoneAccount exceeds size limits.
     */
    public void enforceSimultaneousCallingRestrictionLimit(@NonNull PhoneAccount account) {
        if (!account.hasSimultaneousCallingRestriction()) return;
        Set<PhoneAccountHandle> restrictions = account.getSimultaneousCallingRestriction();
        if (restrictions.size() > MAX_PHONE_ACCOUNT_REGISTRATIONS) {
            throw new IllegalArgumentException("Can not register a PhoneAccount with a number"
                    + "of simultaneous calling restrictions that is greater than "
                    + MAX_PHONE_ACCOUNT_REGISTRATIONS);
        }
        for (PhoneAccountHandle handle : restrictions) {
            ComponentName component = handle.getComponentName();
            if (component.getPackageName().length() > MAX_PHONE_ACCOUNT_FIELD_CHAR_LIMIT) {
                throw new IllegalArgumentException("A PhoneAccountHandle added as part of "
                        + "a simultaneous calling restriction has a package name that has exceeded "
                        + "the character limit of " + MAX_PHONE_ACCOUNT_FIELD_CHAR_LIMIT);
            }
            if (component.getClassName().length() > MAX_PHONE_ACCOUNT_FIELD_CHAR_LIMIT) {
                throw new IllegalArgumentException("A PhoneAccountHandle added as part of "
                        + "a simultaneous calling restriction has a class name that has exceeded "
                        + "the character limit of " + MAX_PHONE_ACCOUNT_FIELD_CHAR_LIMIT);
            }
            if (handle.getId().length() > MAX_PHONE_ACCOUNT_FIELD_CHAR_LIMIT) {
                throw new IllegalArgumentException("A PhoneAccountHandle added as part of "
                        + "a simultaneous calling restriction has an ID that has exceeded "
                        + "the character limit of " + MAX_PHONE_ACCOUNT_FIELD_CHAR_LIMIT);
            }
        }
    }

    /**
     * Enforce a character limit on all PA and PAH string or char-sequence fields.
     *
+103 −0
Original line number Diff line number Diff line
@@ -27,6 +27,7 @@ import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.clearInvocations;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.times;
@@ -90,6 +91,7 @@ import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
@@ -1624,6 +1626,107 @@ public class PhoneAccountRegistrarTest extends TelecomTestCase {
        }
    }

    /**
     * Ensure an IllegalArgumentException is thrown when adding too many PhoneAccountHandles to
     * a PhoneAccount.
     */
    @Test
    public void testLimitOnSimultaneousCallingRestriction_tooManyElements() throws Exception {
        doReturn(true).when(mTelephonyFeatureFlags).simultaneousCallingIndications();
        mComponentContextFixture.addConnectionService(makeQuickConnectionServiceComponentName(),
                Mockito.mock(IConnectionService.class));
        Set<PhoneAccountHandle> tooManyElements = new HashSet<>(11);
        for (int i = 0; i < 11; i++) {
            tooManyElements.add(makeQuickAccountHandle(TEST_ID + i));
        }
        PhoneAccount tooManyRestrictionsPA = new PhoneAccount.Builder(
                makeQuickAccountHandle(TEST_ID), TEST_LABEL)
                .setSimultaneousCallingRestriction(tooManyElements)
                .build();
        try {
            mRegistrar.registerPhoneAccount(tooManyRestrictionsPA);
            fail("should have hit registrations exception in "
                    + "enforceSimultaneousCallingRestrictionLimit");
        } catch (IllegalArgumentException e) {
            // pass test
        }
    }

    /**
     * Ensure an IllegalArgumentException is thrown when adding a PhoneAccountHandle where the
     * package name field is too large.
     */
    @Test
    public void testLimitOnSimultaneousCallingRestriction_InvalidPackageName() throws Exception {
        doReturn(true).when(mTelephonyFeatureFlags).simultaneousCallingIndications();
        mComponentContextFixture.addConnectionService(makeQuickConnectionServiceComponentName(),
                Mockito.mock(IConnectionService.class));
        Set<PhoneAccountHandle> invalidElement = new HashSet<>(1);
        invalidElement.add(new PhoneAccountHandle(new ComponentName(INVALID_STR, "Class"),
                TEST_ID));
        PhoneAccount invalidRestrictionPA = new PhoneAccount.Builder(
                makeQuickAccountHandle(TEST_ID), TEST_LABEL)
                .setSimultaneousCallingRestriction(invalidElement)
                .build();
        try {
            mRegistrar.registerPhoneAccount(invalidRestrictionPA);
            fail("should have hit package name size limit exception in "
                    + "enforceSimultaneousCallingRestrictionLimit");
        } catch (IllegalArgumentException e) {
            // pass test
        }
    }

    /**
     * Ensure an IllegalArgumentException is thrown when adding a PhoneAccountHandle where the
     * class name field is too large.
     */
    @Test
    public void testLimitOnSimultaneousCallingRestriction_InvalidClassName() throws Exception {
        doReturn(true).when(mTelephonyFeatureFlags).simultaneousCallingIndications();
        mComponentContextFixture.addConnectionService(makeQuickConnectionServiceComponentName(),
                Mockito.mock(IConnectionService.class));
        Set<PhoneAccountHandle> invalidElement = new HashSet<>(1);
        invalidElement.add(new PhoneAccountHandle(new ComponentName("pkg", INVALID_STR),
                TEST_ID));
        PhoneAccount invalidRestrictionPA = new PhoneAccount.Builder(
                makeQuickAccountHandle(TEST_ID), TEST_LABEL)
                .setSimultaneousCallingRestriction(invalidElement)
                .build();
        try {
            mRegistrar.registerPhoneAccount(invalidRestrictionPA);
            fail("should have hit class name size limit exception in "
                    + "enforceSimultaneousCallingRestrictionLimit");
        } catch (IllegalArgumentException e) {
            // pass test
        }
    }

    /**
     * Ensure an IllegalArgumentException is thrown when adding a PhoneAccountHandle where the
     * ID field is too large.
     */
    @Test
    public void testLimitOnSimultaneousCallingRestriction_InvalidIdSize() throws Exception {
        doReturn(true).when(mTelephonyFeatureFlags).simultaneousCallingIndications();
        mComponentContextFixture.addConnectionService(makeQuickConnectionServiceComponentName(),
                Mockito.mock(IConnectionService.class));
        Set<PhoneAccountHandle> invalidIdElement = new HashSet<>(1);
        invalidIdElement.add(new PhoneAccountHandle(makeQuickConnectionServiceComponentName(),
                INVALID_STR));
        PhoneAccount invalidRestrictionPA = new PhoneAccount.Builder(
                makeQuickAccountHandle(TEST_ID), TEST_LABEL)
                .setSimultaneousCallingRestriction(invalidIdElement)
                .build();
        try {
            mRegistrar.registerPhoneAccount(invalidRestrictionPA);
            fail("should have hit ID size limit exception in "
                    + "enforceSimultaneousCallingRestrictionLimit");
        } catch (IllegalArgumentException e) {
            // pass test
        }
    }

    /**
     * Ensure an IllegalArgumentException is thrown when adding an address over the limit
     */