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

Commit a368034a authored by Michael Groover's avatar Michael Groover
Browse files

Setup TelephonyPermissions mocks for all SubscriptionController tests

The WRITE_SMS appop check in TelephonyPermission's phone number access
verification has been moved after the READ_PHONE_STATE and carrier
privilege check. Due to the new phone number access requirements for
target R+ any tests that query for SubscriptionInfo objects must setup
the mocks for the TelephonyPermission checks; this commit does this in
the setUp method as a majority of the SubscriptionController tests will
perform this query.

Bug: 157642567
Test: atest SubscriptionControllerTest
Test: atest TelephonyPermissionsTest
Test: atest PhoneSubInfoControllerTest
Change-Id: I9e5f0bef0c9c9d820492d92baa48e110abf4c517
parent 9e974c13
Loading
Loading
Loading
Loading
+11 −3
Original line number Diff line number Diff line
@@ -647,14 +647,22 @@ public class PhoneSubInfoControllerTest extends TelephonyTest {
        assertEquals("+18052345678",
                mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG, FEATURE_ID));

        /* case 6: only enable READ_SMS */
        /* case 6: only enable READ_SMS; without the appop should throw SecurityException */
        doReturn(AppOpsManager.MODE_ERRORED).when(mAppOsMgr).noteOp(
                eq(AppOpsManager.OPSTR_READ_PHONE_STATE), anyInt(), eq(TAG), eq(FEATURE_ID),
                nullable(String.class));
        mContextFixture.removeCallingOrSelfPermission(READ_PHONE_STATE);
        mContextFixture.addCallingOrSelfPermission(READ_SMS);
        assertNull(mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(0, TAG, FEATURE_ID));
        assertNull(mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG, FEATURE_ID));
        try {
            mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(0, TAG, FEATURE_ID);
            Assert.fail("expected SecurityException thrown");
        } catch (SecurityException expected) {
        }
        try {
            mPhoneSubInfoControllerUT.getLine1NumberForSubscriber(1, TAG, FEATURE_ID);
            Assert.fail("expected SecurityException thrown");
        } catch (SecurityException expected) {
        }

        /* case 7: enable READ_SMS and OP_READ_SMS */
        doReturn(AppOpsManager.MODE_ALLOWED).when(mAppOsMgr).noteOp(
+3 −1
Original line number Diff line number Diff line
@@ -119,6 +119,8 @@ public class SubscriptionControllerTest extends TelephonyTest {

        doReturn(1).when(mProxyController).getMaxRafSupported();
        mContextFixture.putIntArrayResource(com.android.internal.R.array.sim_colors, new int[]{5});

        setupMocksForTelephonyPermissions(Build.VERSION_CODES.R);
    }

    @After
@@ -1386,7 +1388,7 @@ public class SubscriptionControllerTest extends TelephonyTest {
        mSubscriptionControllerUT.setDisplayNumber(DISPLAY_NUMBER, getFirstSubId());
        mContextFixture.removeCallingOrSelfPermission(ContextFixture.PERMISSION_ENABLE_ALL);
        mContextFixture.addCallingOrSelfPermission(Manifest.permission.READ_PHONE_STATE);
        setupMocksForTelephonyPermissions(mCallingPackage, Build.VERSION_CODES.R);
        setupMocksForTelephonyPermissions(Build.VERSION_CODES.R);
        doReturn(AppOpsManager.MODE_DEFAULT).when(mAppOpsManager).noteOp(
                eq(AppOpsManager.OPSTR_WRITE_SMS), anyInt(), anyString(),
                nullable(String.class), nullable(String.class));
+35 −0
Original line number Diff line number Diff line
@@ -272,6 +272,41 @@ public class TelephonyPermissionsTest {
                mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
    public void testCheckReadPhoneNumber_hasReadSmsNoAppop() throws Exception {
        // If an app has been granted the READ_SMS permission, but the OPSTR_READ_SMS appop has been
        // revoked then instead of immediately returning false the phone number access check should
        // check if the caller has the READ_PHONE_NUMBERS permission and appop.
        setupMocksForDeviceIdentifiersErrorPath();
        doNothing().when(mMockContext).enforcePermission(
                android.Manifest.permission.READ_SMS, PID, UID, MSG);
        doNothing().when(mMockContext).enforcePermission(
                android.Manifest.permission.READ_PHONE_NUMBERS, PID, UID, MSG);
        when(mMockAppOps.noteOp(eq(AppOpsManager.OPSTR_READ_PHONE_NUMBERS), eq(UID), eq(PACKAGE),
                eq(FEATURE), nullable(String.class))).thenReturn(AppOpsManager.MODE_ALLOWED);
        assertTrue(TelephonyPermissions.checkReadPhoneNumber(
                mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
    public void testCheckReadPhoneNumber_hasReadSmsAndReadPhoneNumbersNoAppops() throws Exception {
        // If an app has both the READ_SMS and READ_PHONE_NUMBERS permissions granted but does not
        // have the corresponding appops instead of returning false for not having the appop granted
        // a SecurityException should be thrown.
        setupMocksForDeviceIdentifiersErrorPath();
        doNothing().when(mMockContext).enforcePermission(
                android.Manifest.permission.READ_SMS, PID, UID, MSG);
        doNothing().when(mMockContext).enforcePermission(
                android.Manifest.permission.READ_PHONE_NUMBERS, PID, UID, MSG);
        try {
            TelephonyPermissions.checkReadPhoneNumber(
                    mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG);
            fail("Should have thrown SecurityException");
        } catch (SecurityException e) {
            // expected
        }
    }

    @Test
    public void testCheckReadDeviceIdentifiers_noPermissions() throws Exception {
        setupMocksForDeviceIdentifiersErrorPath();
+9 −3
Original line number Diff line number Diff line
@@ -774,17 +774,23 @@ public abstract class TelephonyTest {
    }

    protected void setupMocksForTelephonyPermissions() throws Exception {
        setupMocksForTelephonyPermissions(TAG, Build.VERSION_CODES.Q);
        setupMocksForTelephonyPermissions(Build.VERSION_CODES.Q);
    }

    protected void setupMocksForTelephonyPermissions(String packageName, int targetSdkVersion)
    protected void setupMocksForTelephonyPermissions(int targetSdkVersion)
            throws Exception {
        // If the calling package does not meet the new requirements for device identifier access
        // TelephonyPermissions will query the PackageManager for the ApplicationInfo of the package
        // to determine the target SDK. For apps targeting Q a SecurityException is thrown
        // regardless of if the package satisfies the previous requirements for device ID access.

        // Any tests that query for SubscriptionInfo objects will trigger a phone number access
        // check that will first query the ApplicationInfo as apps targeting R+ can no longer
        // access the phone number with the READ_PHONE_STATE permission and instead must meet one of
        // the other requirements. This ApplicationInfo is generalized to any package name since
        // some tests will simulate invocation from other packages.
        mApplicationInfo.targetSdkVersion = targetSdkVersion;
        doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfoAsUser(eq(packageName),
        doReturn(mApplicationInfo).when(mPackageManager).getApplicationInfoAsUser(anyString(),
                anyInt(), any());

        // TelephonyPermissions uses a SystemAPI to check if the calling package meets any of the