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

Commit 591152ec authored by Michael Groover's avatar Michael Groover Committed by Automerger Merge Worker
Browse files

Merge "Setup TelephonyPermissions mocks for all SubscriptionController tests"...

Merge "Setup TelephonyPermissions mocks for all SubscriptionController tests" into rvc-dev am: a44841ea am: e58ab231

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/opt/telephony/+/11933064

Change-Id: I23fe3447e9579a826f640b170cff01f3c3b1b1b1
parents d4de5c30 e58ab231
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