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

Commit 4d4c0b75 authored by Shuo Qian's avatar Shuo Qian Committed by Gerrit Code Review
Browse files

Merge "Cleanup ITelephony in TelephonyPermissions"

parents 0e285f62 630e3277
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);
    }

    /**
+10 −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();
@@ -89,6 +91,8 @@ public class SmsPermissionsTest {
    @After
    public void tearDown() throws Exception {
        mHandlerThread.quit();
        mHandlerThread.join();
        super.tearDown();
    }

    @Test
@@ -196,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);
@@ -221,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(AppOpsManager.OPSTR_READ_PHONE_STATE, UID, PACKAGE))
                .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(AppOpsManager.OPSTR_READ_PHONE_STATE, UID, PACKAGE))
                .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(AppOpsManager.OPSTR_WRITE_SMS, UID, PACKAGE))
                .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(AppOpsManager.OPSTR_READ_SMS, UID, PACKAGE))
                .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(AppOpsManager.OPSTR_READ_PHONE_NUMBERS, UID, PACKAGE))
                .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));
@@ -334,7 +352,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,
@@ -348,8 +368,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));
@@ -411,8 +433,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));
@@ -428,7 +452,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 {