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

Commit 18d38a48 authored by Shuo Qian's avatar Shuo Qian
Browse files

Cleanup ITelephony in TelephonyPermissions

Change for PhoneSubInfoController
Change for SmsPermissions
Refactor TelephonyPermissionTests
Refactor SmsPermissionsTest

Test: manual
Bug: 145688189
Change-Id: I6262411b2ca499d54970c5d813f957637c1d80fc
parent f0d99e7d
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -270,7 +270,7 @@ public class PhoneSubInfoController extends IPhoneSubInfo.Stub {
            return;
        }
        if (VDBG) log("No read privileged phone permission, check carrier privilege next.");
        TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(subId, message);
        TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mContext, subId, message);
    }

    /**
+2 −1
Original line number Diff line number Diff line
@@ -96,7 +96,8 @@ public class SmsPermissions {
            }
        }

        TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(mPhone.getSubId(), message);
        TelephonyPermissions.enforceCallingOrSelfCarrierPrivilege(
                mContext, mPhone.getSubId(), message);
    }

    /**
+9 −3
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ import org.mockito.MockitoAnnotations;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;

public class SmsPermissionsTest {
public class SmsPermissionsTest extends TelephonyTest {
    private static final String PACKAGE = "com.example.package";
    private static final String MESSAGE = "msg";

@@ -59,6 +59,8 @@ public class SmsPermissionsTest {

    @Before
    public void setUp() throws Exception {
        super.setUp("SmsPermissionsTest");

        MockitoAnnotations.initMocks(this);
        mHandlerThread = new HandlerThread("IccSmsInterfaceManagerTest");
        mHandlerThread.start();
@@ -90,6 +92,7 @@ public class SmsPermissionsTest {
    public void tearDown() throws Exception {
        mHandlerThread.quit();
        mHandlerThread.join();
        super.tearDown();
    }

    @Test
@@ -197,6 +200,8 @@ public class SmsPermissionsTest {

    @Test
    public void testCheckCallingOrSelfCanGetSmscAddressPermissions_noPermissions() {
        Mockito.when(mMockContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(
                mTelephonyManager);
        Mockito.when(mMockContext.checkCallingOrSelfPermission(
                    Manifest.permission.READ_PRIVILEGED_PHONE_STATE))
                .thenReturn(PERMISSION_DENIED);
@@ -222,9 +227,10 @@ public class SmsPermissionsTest {

    @Test
    public void testCheckCallingOrSelfCanSetSmscAddressPermissions_noPermissions() {
        Mockito.when(mMockContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(
                mTelephonyManager);
        Mockito.when(mMockContext.checkCallingOrSelfPermission(
                    Manifest.permission.MODIFY_PHONE_STATE))
                .thenReturn(PERMISSION_DENIED);
                Manifest.permission.MODIFY_PHONE_STATE)).thenReturn(PERMISSION_DENIED);
        assertFalse(mSmsPermissionsTest.checkCallingOrSelfCanSetSmscAddress(PACKAGE, MESSAGE));
    }
}
+53 −27
Original line number Diff line number Diff line
@@ -83,6 +83,12 @@ public class TelephonyPermissionsTest {
    private ApplicationInfo mMockApplicationInfo;
    @Mock
    private DevicePolicyManager mMockDevicePolicyManager;
    @Mock
    private TelephonyManager mTelephonyManagerMock;
    @Mock
    private TelephonyManager mTelephonyManagerMockForSub1;
    @Mock
    private TelephonyManager mTelephonyManagerMockForSub2;

    private MockContentResolver mMockContentResolver;
    private FakeSettingsConfigProvider mFakeSettingsConfigProvider;
@@ -90,6 +96,11 @@ public class TelephonyPermissionsTest {
    @Before
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);

        when(mMockContext.getSystemService(Context.TELEPHONY_SERVICE)).thenReturn(
                mTelephonyManagerMock);
        when(mTelephonyManagerMock.createForSubscriptionId(anyInt())).thenReturn(
                mTelephonyManagerMock);
        when(mMockContext.getSystemService(Context.APP_OPS_SERVICE)).thenReturn(mMockAppOps);
        when(mMockContext.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE)).thenReturn(
                mMockSubscriptionManager);
@@ -122,7 +133,7 @@ public class TelephonyPermissionsTest {
    public void testCheckReadPhoneState_noPermissions() {
        try {
            TelephonyPermissions.checkReadPhoneState(
                    mMockContext, () -> mMockTelephony, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG);
                    mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG);
            fail("Should have thrown SecurityException");
        } catch (SecurityException e) {
            // expected
@@ -134,7 +145,7 @@ public class TelephonyPermissionsTest {
        doNothing().when(mMockContext).enforcePermission(
                android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, PID, UID, MSG);
        assertTrue(TelephonyPermissions.checkReadPhoneState(
                mMockContext, () -> mMockTelephony, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
@@ -144,7 +155,7 @@ public class TelephonyPermissionsTest {
        when(mMockAppOps.noteOp(eq(AppOpsManager.OPSTR_READ_PHONE_STATE), eq(UID), eq(PACKAGE),
                eq(FEATURE), nullable(String.class))).thenReturn(AppOpsManager.MODE_ALLOWED);
        assertTrue(TelephonyPermissions.checkReadPhoneState(
                mMockContext, () -> mMockTelephony, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
@@ -152,21 +163,23 @@ public class TelephonyPermissionsTest {
        doNothing().when(mMockContext).enforcePermission(
                android.Manifest.permission.READ_PHONE_STATE, PID, UID, MSG);
        assertFalse(TelephonyPermissions.checkReadPhoneState(
                mMockContext, () -> mMockTelephony, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
    public void testCheckReadPhoneState_hasCarrierPrivileges() throws Exception {
        when(mMockTelephony.getCarrierPrivilegeStatusForUid(eq(SUB_ID), eq(UID)))
                .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        when(mTelephonyManagerMock.createForSubscriptionId(eq(SUB_ID))).thenReturn(
                mTelephonyManagerMockForSub1);
        when(mTelephonyManagerMockForSub1.getCarrierPrivilegeStatus(anyInt())).thenReturn(
                TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        assertTrue(TelephonyPermissions.checkReadPhoneState(
                mMockContext, () -> mMockTelephony, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
    public void testCheckReadPhoneStateOnAnyActiveSub_noPermissions() {
        assertFalse(TelephonyPermissions.checkReadPhoneStateOnAnyActiveSub(
                mMockContext, () -> mMockTelephony, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
@@ -174,7 +187,7 @@ public class TelephonyPermissionsTest {
        doNothing().when(mMockContext).enforcePermission(
                android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, PID, UID, MSG);
        assertTrue(TelephonyPermissions.checkReadPhoneStateOnAnyActiveSub(
                mMockContext, () -> mMockTelephony, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
@@ -184,7 +197,7 @@ public class TelephonyPermissionsTest {
        when(mMockAppOps.noteOp(eq(AppOpsManager.OPSTR_READ_PHONE_STATE), eq(UID), eq(PACKAGE),
                eq(FEATURE), nullable(String.class))).thenReturn(AppOpsManager.MODE_ALLOWED);
        assertTrue(TelephonyPermissions.checkReadPhoneStateOnAnyActiveSub(
                mMockContext, () -> mMockTelephony, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
@@ -192,22 +205,25 @@ public class TelephonyPermissionsTest {
        doNothing().when(mMockContext).enforcePermission(
                android.Manifest.permission.READ_PHONE_STATE, PID, UID, MSG);
        assertFalse(TelephonyPermissions.checkReadPhoneStateOnAnyActiveSub(
                mMockContext, () -> mMockTelephony, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
    public void testCheckReadPhoneStateOnAnyActiveSub_hasCarrierPrivileges() throws Exception {
        when(mMockTelephony.getCarrierPrivilegeStatusForUid(eq(SUB_ID), eq(UID)))
                .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        when(mTelephonyManagerMock.createForSubscriptionId(eq(SUB_ID))).thenReturn(
                mTelephonyManagerMockForSub1);
        when(mTelephonyManagerMockForSub1.getCarrierPrivilegeStatus(anyInt())).thenReturn(
                TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);

        assertTrue(TelephonyPermissions.checkReadPhoneStateOnAnyActiveSub(
                mMockContext, () -> mMockTelephony, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
    public void testCheckReadPhoneNumber_noPermissions() {
        try {
            TelephonyPermissions.checkReadPhoneNumber(
                    mMockContext, () -> mMockTelephony, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG);
                    mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG);
            fail("Should have thrown SecurityException");
        } catch (SecurityException e) {
            // expected
@@ -219,7 +235,7 @@ public class TelephonyPermissionsTest {
        when(mMockAppOps.noteOp(eq(AppOpsManager.OPSTR_WRITE_SMS), eq(UID), eq(PACKAGE),
                eq(FEATURE), nullable(String.class))).thenReturn(AppOpsManager.MODE_ALLOWED);
        assertTrue(TelephonyPermissions.checkReadPhoneNumber(
                mMockContext, () -> mMockTelephony, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
@@ -227,7 +243,7 @@ public class TelephonyPermissionsTest {
        doNothing().when(mMockContext).enforcePermission(
                android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE, PID, UID, MSG);
        assertTrue(TelephonyPermissions.checkReadPhoneNumber(
                mMockContext, () -> mMockTelephony, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
@@ -237,7 +253,7 @@ public class TelephonyPermissionsTest {
        when(mMockAppOps.noteOp(eq(AppOpsManager.OPSTR_READ_SMS), eq(UID), eq(PACKAGE), eq(FEATURE),
                nullable(String.class))).thenReturn(AppOpsManager.MODE_ALLOWED);
        assertTrue(TelephonyPermissions.checkReadPhoneNumber(
                mMockContext, () -> mMockTelephony, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
@@ -247,7 +263,7 @@ public class TelephonyPermissionsTest {
        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, () -> mMockTelephony, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
                mMockContext, SUB_ID, PID, UID, PACKAGE, FEATURE, MSG));
    }

    @Test
@@ -273,8 +289,10 @@ public class TelephonyPermissionsTest {

    @Test
    public void testCheckReadDeviceIdentifiers_hasCarrierPrivileges() throws Exception {
        when(mMockTelephony.getCarrierPrivilegeStatusForUid(eq(SUB_ID), eq(UID)))
                .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        when(mTelephonyManagerMock.createForSubscriptionId(eq(SUB_ID))).thenReturn(
                mTelephonyManagerMockForSub1);
        when(mTelephonyManagerMockForSub1.getCarrierPrivilegeStatus(anyInt())).thenReturn(
                TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        assertTrue(
                TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mMockContext,
                        SUB_ID, PACKAGE, FEATURE, MSG));
@@ -335,7 +353,9 @@ public class TelephonyPermissionsTest {
            throws Exception {
        when(mMockSubscriptionManager.getActiveSubscriptionIdList(anyBoolean())).thenReturn(
                new int[]{SUB_ID, SUB_ID_2});
        when(mMockTelephony.getCarrierPrivilegeStatusForUid(eq(SUB_ID_2), eq(UID))).thenReturn(
        when(mTelephonyManagerMock.createForSubscriptionId(eq(SUB_ID_2))).thenReturn(
                mTelephonyManagerMockForSub2);
        when(mTelephonyManagerMockForSub2.getCarrierPrivilegeStatus(anyInt())).thenReturn(
                TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        assertTrue(
                TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mMockContext,
@@ -349,8 +369,10 @@ public class TelephonyPermissionsTest {
                new int[]{SUB_ID});
        when(mMockSubscriptionManager.getActiveSubscriptionIdList(false)).thenReturn(
                new int[]{SUB_ID, SUB_ID_2});
        when(mMockTelephony.getCarrierPrivilegeStatusForUid(eq(SUB_ID_2), eq(UID)))
                .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        when(mTelephonyManagerMock.createForSubscriptionId(eq(SUB_ID_2))).thenReturn(
                mTelephonyManagerMockForSub2);
        when(mTelephonyManagerMockForSub2.getCarrierPrivilegeStatus(anyInt())).thenReturn(
                TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        assertTrue(
                TelephonyPermissions.checkCallingOrSelfReadDeviceIdentifiers(mMockContext,
                        SUB_ID, PACKAGE, FEATURE, MSG));
@@ -413,8 +435,10 @@ public class TelephonyPermissionsTest {
        when(mMockContext.checkPermission(
                eq(android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE),
                anyInt(), anyInt())).thenReturn(PackageManager.PERMISSION_DENIED);
        when(mMockTelephony.getCarrierPrivilegeStatusForUid(eq(SUB_ID), anyInt()))
                .thenReturn(TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        when(mTelephonyManagerMock.createForSubscriptionId(eq(SUB_ID))).thenReturn(
                mTelephonyManagerMockForSub1);
        when(mTelephonyManagerMockForSub1.getCarrierPrivilegeStatus(anyInt())).thenReturn(
                TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        assertTrue(
                TelephonyPermissions.checkCallingOrSelfReadSubscriberIdentifiers(mMockContext,
                        SUB_ID, PACKAGE, FEATURE, MSG));
@@ -430,7 +454,9 @@ public class TelephonyPermissionsTest {
                anyInt(), anyInt())).thenReturn(PackageManager.PERMISSION_DENIED);
        when(mMockSubscriptionManager.getActiveSubscriptionIdList(anyBoolean())).thenReturn(
                new int[]{SUB_ID, SUB_ID_2});
        when(mMockTelephony.getCarrierPrivilegeStatusForUid(eq(SUB_ID_2), anyInt())).thenReturn(
        when(mTelephonyManagerMock.createForSubscriptionId(eq(SUB_ID_2))).thenReturn(
                mTelephonyManagerMockForSub2);
        when(mTelephonyManagerMockForSub2.getCarrierPrivilegeStatus(anyInt())).thenReturn(
                TelephonyManager.CARRIER_PRIVILEGE_STATUS_HAS_ACCESS);
        // Carrier privilege on the other active sub shouldn't allow access to this sub.
        try {