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

Commit 6cfe05a6 authored by Dennis Kiilerich's avatar Dennis Kiilerich Committed by Android (Google) Code Review
Browse files

Merge "Create a RoleManagerWrapper in BugreportManagerServiceImpl.Injector to...

Merge "Create a RoleManagerWrapper in BugreportManagerServiceImpl.Injector to allow for overriding the behaviour in tests." into main
parents b6d10b8c e30e705c
Loading
Loading
Loading
Loading
+15 −1
Original line number Diff line number Diff line
@@ -19,6 +19,7 @@ package com.android.server.os;
import static android.app.admin.flags.Flags.onboardingBugreportV2Enabled;

import android.Manifest;
import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.RequiresPermission;
import android.app.AppOpsManager;
@@ -68,6 +69,7 @@ import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalInt;
@@ -335,14 +337,22 @@ class BugreportManagerServiceImpl extends IDumpstate.Stub {
    }

    static class Injector {
        class RoleManagerWrapper {
            List<String> getRoleHolders(@NonNull String roleName) {
                return mContext.getSystemService(RoleManager.class).getRoleHolders(roleName);
            }
        }

        Context mContext;
        ArraySet<String> mAllowlistedPackages;
        AtomicFile mMappingFile;
        RoleManagerWrapper mRoleManagerWrapper;

        Injector(Context context, ArraySet<String> allowlistedPackages, AtomicFile mappingFile) {
            mContext = context;
            mAllowlistedPackages = allowlistedPackages;
            mMappingFile = mappingFile;
            mRoleManagerWrapper = new RoleManagerWrapper();
        }

        Context getContext() {
@@ -368,6 +378,10 @@ class BugreportManagerServiceImpl extends IDumpstate.Stub {
        void setSystemProperty(String key, String value) {
            SystemProperties.set(key, value);
        }

        RoleManagerWrapper getRoleManagerWrapper() {
            return mRoleManagerWrapper;
        }
    }

    BugreportManagerServiceImpl(Context context) {
@@ -546,7 +560,7 @@ class BugreportManagerServiceImpl extends IDumpstate.Stub {
        if (!allowlisted) {
            final long token = Binder.clearCallingIdentity();
            try {
                allowlisted = mContext.getSystemService(RoleManager.class).getRoleHolders(
                allowlisted = mInjector.getRoleManagerWrapper().getRoleHolders(
                        ROLE_SYSTEM_AUTOMOTIVE_PROJECTION).contains(callingPackage);
            } finally {
                Binder.restoreCallingIdentity(token);
+27 −44
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@

package com.android.server.os;

import static com.android.compatibility.common.util.SystemUtil.runWithShellPermissionIdentity;

import static com.google.common.truth.Truth.assertThat;

import static org.junit.Assert.assertThrows;
@@ -25,9 +23,9 @@ import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import android.annotation.NonNull;
import android.app.admin.DevicePolicyManager;
import android.app.admin.flags.Flags;
import android.app.role.RoleManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.content.pm.UserInfo;
@@ -61,6 +59,8 @@ import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.FileDescriptor;
import java.util.Collections;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -104,7 +104,7 @@ public class BugreportManagerServiceImplTest {
        ArraySet<String> mAllowlistedPackages = new ArraySet<>();
        mAllowlistedPackages.add(mContext.getPackageName());
        mInjector = new TestInjector(mContext, mAllowlistedPackages, mMappingFile,
                mMockUserManager, mMockDevicePolicyManager);
                mMockUserManager, mMockDevicePolicyManager, null);
        mService = new BugreportManagerServiceImpl(mInjector);
        mBugreportFileManager = new BugreportManagerServiceImpl.BugreportFileManager(mMappingFile);
        when(mPackageManager.getPackageUidAsUser(anyString(), anyInt())).thenReturn(mCallingUid);
@@ -114,24 +114,8 @@ public class BugreportManagerServiceImplTest {

    @After
    public void tearDown() throws Exception {
        // Changes to RoleManager persist between tests, so we need to clear out any funny
        // business we did in previous tests.
        // Clean up the mapping file between tests since it would otherwise persist.
        mMappingFile.delete();
        RoleManager roleManager = mContext.getSystemService(RoleManager.class);
        CallbackFuture future = new CallbackFuture();
        runWithShellPermissionIdentity(
                () -> {
                    roleManager.setBypassingRoleQualification(false);
                    roleManager.removeRoleHolderAsUser(
                            "android.app.role.SYSTEM_AUTOMOTIVE_PROJECTION",
                            mContext.getPackageName(),
                            /* flags= */ 0,
                            Process.myUserHandle(),
                            mContext.getMainExecutor(),
                            future);
                });

        assertThat(future.get()).isEqualTo(true);
    }

    @Test
@@ -267,7 +251,10 @@ public class BugreportManagerServiceImplTest {

    @Test
    public void testCancelBugreportWithoutRole() {
        clearAllowlist();
        // Create a new service to clear the allowlist
        mService = new BugreportManagerServiceImpl(
                new TestInjector(mContext, new ArraySet<>(), mMappingFile,
                        mMockUserManager, mMockDevicePolicyManager, null));

        assertThrows(SecurityException.class, () -> mService.cancelBugreport(
                Binder.getCallingUid(), mContext.getPackageName()));
@@ -275,29 +262,13 @@ public class BugreportManagerServiceImplTest {

    @Test
    public void testCancelBugreportWithRole() throws Exception {
        clearAllowlist();
        RoleManager roleManager = mContext.getSystemService(RoleManager.class);
        CallbackFuture future = new CallbackFuture();
        runWithShellPermissionIdentity(
                () -> {
                    roleManager.setBypassingRoleQualification(true);
                    roleManager.addRoleHolderAsUser(
                            "android.app.role.SYSTEM_AUTOMOTIVE_PROJECTION",
                            mContext.getPackageName(),
                            /* flags= */ 0,
                            Process.myUserHandle(),
                            mContext.getMainExecutor(),
                            future);
                });

        assertThat(future.get()).isEqualTo(true);
        mService.cancelBugreport(Binder.getCallingUid(), mContext.getPackageName());
    }

    private void clearAllowlist() {
        // Create a new service to clear the allowlist, but override the role manager
        mService = new BugreportManagerServiceImpl(
                new TestInjector(mContext, new ArraySet<>(), mMappingFile,
                        mMockUserManager, mMockDevicePolicyManager));
                        mMockUserManager, mMockDevicePolicyManager,
                        "android.app.role.SYSTEM_AUTOMOTIVE_PROJECTION"));

        mService.cancelBugreport(Binder.getCallingUid(), mContext.getPackageName());
    }

    private static class Listener implements IDumpstateListener {
@@ -359,10 +330,22 @@ public class BugreportManagerServiceImplTest {
        private boolean mBugreportStarted = false;

        TestInjector(Context context, ArraySet<String> allowlistedPackages, AtomicFile mappingFile,
                UserManager um, DevicePolicyManager dpm) {
                UserManager um, DevicePolicyManager dpm, String grantedRole) {
            super(context, allowlistedPackages, mappingFile);
            mUserManager = um;
            mDevicePolicyManager = dpm;

            if (grantedRole != null) {
                mRoleManagerWrapper =
                        new BugreportManagerServiceImpl.Injector.RoleManagerWrapper() {
                            @Override
                            List<String> getRoleHolders(@NonNull String roleName) {
                                return roleName.equals(grantedRole)
                                        ? Collections.singletonList(mContext.getPackageName())
                                        : Collections.emptyList();
                            }
                        };
            }
        }

        @Override