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

Commit a99e0b1b authored by Paul Hu's avatar Paul Hu Committed by Automerger Merge Worker
Browse files

Merge "[RFPM02] Add Dependencies class for injection in tests." am: b95025b2

Original change: https://android-review.googlesource.com/c/platform/frameworks/base/+/1318291

Change-Id: Ibc81b90c9a7428f63afcb6bad27eb593cf2f628e
parents 28d77703 b95025b2
Loading
Loading
Loading
Loading
+23 −7
Original line number Diff line number Diff line
@@ -82,6 +82,7 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
    private final PackageManager mPackageManager;
    private final UserManager mUserManager;
    private final INetd mNetd;
    private final Dependencies mDeps;

    // Values are User IDs.
    @GuardedBy("this")
@@ -102,10 +103,30 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
    @GuardedBy("this")
    private final Set<Integer> mAllApps = new HashSet<>();

    public PermissionMonitor(Context context, INetd netd) {
    /**
     * Dependencies of PermissionMonitor, for injection in tests.
     */
    @VisibleForTesting
    public static class Dependencies {
        /**
         * Get device first sdk version.
         */
        public int getDeviceFirstSdkInt() {
            return Build.VERSION.FIRST_SDK_INT;
        }
    }

    public PermissionMonitor(@NonNull final Context context, @NonNull final INetd netd) {
        this(context, netd, new Dependencies());
    }

    @VisibleForTesting
    PermissionMonitor(@NonNull final Context context, @NonNull final INetd netd,
            @NonNull final Dependencies deps) {
        mPackageManager = context.getPackageManager();
        mUserManager = (UserManager) context.getSystemService(Context.USER_SERVICE);
        mNetd = netd;
        mDeps = deps;
    }

    // Intended to be called only once at startup, after the system is ready. Installs a broadcast
@@ -185,11 +206,6 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
        return appInfo.isVendor() || appInfo.isOem() || appInfo.isProduct();
    }

    @VisibleForTesting
    protected int getDeviceFirstSdkInt() {
        return Build.VERSION.FIRST_SDK_INT;
    }

    @VisibleForTesting
    boolean hasPermission(@NonNull final PackageInfo app, @NonNull final String permission) {
        if (app.requestedPermissions == null || app.requestedPermissionsFlags == null) {
@@ -212,7 +228,7 @@ public class PermissionMonitor implements PackageManagerInternal.PackageListObse
        if (app.applicationInfo != null) {
            // Backward compatibility for b/114245686, on devices that launched before Q daemons
            // and apps running as the system UID are exempted from this check.
            if (app.applicationInfo.uid == SYSTEM_UID && getDeviceFirstSdkInt() < VERSION_Q) {
            if (app.applicationInfo.uid == SYSTEM_UID && mDeps.getDeviceFirstSdkInt() < VERSION_Q) {
                return true;
            }

+4 −3
Original line number Diff line number Diff line
@@ -114,6 +114,7 @@ public class PermissionMonitorTest {
    @Mock private INetd mNetdService;
    @Mock private PackageManagerInternal mMockPmi;
    @Mock private UserManager mUserManager;
    @Mock private PermissionMonitor.Dependencies mDeps;

    private PermissionMonitor mPermissionMonitor;

@@ -128,7 +129,7 @@ public class PermissionMonitorTest {
                        new UserInfo(MOCK_USER2, "", 0),
                }));

        mPermissionMonitor = spy(new PermissionMonitor(mContext, mNetdService));
        mPermissionMonitor = spy(new PermissionMonitor(mContext, mNetdService, mDeps));

        LocalServices.removeServiceForTest(PackageManagerInternal.class);
        LocalServices.addService(PackageManagerInternal.class, mMockPmi);
@@ -283,14 +284,14 @@ public class PermissionMonitorTest {

    @Test
    public void testHasRestrictedNetworkPermissionSystemUid() {
        doReturn(VERSION_P).when(mPermissionMonitor).getDeviceFirstSdkInt();
        doReturn(VERSION_P).when(mDeps).getDeviceFirstSdkInt();
        assertTrue(hasRestrictedNetworkPermission(PARTITION_SYSTEM, VERSION_P, SYSTEM_UID));
        assertTrue(hasRestrictedNetworkPermission(
                PARTITION_SYSTEM, VERSION_P, SYSTEM_UID, CONNECTIVITY_INTERNAL));
        assertTrue(hasRestrictedNetworkPermission(
                PARTITION_SYSTEM, VERSION_P, SYSTEM_UID, CONNECTIVITY_USE_RESTRICTED_NETWORKS));

        doReturn(VERSION_Q).when(mPermissionMonitor).getDeviceFirstSdkInt();
        doReturn(VERSION_Q).when(mDeps).getDeviceFirstSdkInt();
        assertFalse(hasRestrictedNetworkPermission(PARTITION_SYSTEM, VERSION_Q, SYSTEM_UID));
        assertFalse(hasRestrictedNetworkPermission(
                PARTITION_SYSTEM, VERSION_Q, SYSTEM_UID, CONNECTIVITY_INTERNAL));