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

Commit 25ab1edf authored by Kholoud Mohamed's avatar Kholoud Mohamed
Browse files

Fix failing tests in BugreportManagerServiceImplTest

Bug: 318693675
Test: atest com.android.server.os.BugreportManagerServiceImplTest
Change-Id: Id3cf2cd09cad0e722026f3c4e2e09bdd4b1de0c1
parent dae634e0
Loading
Loading
Loading
Loading
+5 −6
Original line number Diff line number Diff line
@@ -152,14 +152,13 @@ class BugreportManagerServiceImpl extends IDumpstate.Stub {
        @RequiresPermission(value = android.Manifest.permission.INTERACT_ACROSS_USERS,
                conditional = true)
        void ensureCallerPreviouslyGeneratedFile(
                Context context, Pair<Integer, String> callingInfo, int userId,
                String bugreportFile, boolean forceUpdateMapping) {
                Context context, PackageManager packageManager, Pair<Integer, String> callingInfo,
                int userId, String bugreportFile, boolean forceUpdateMapping) {
            synchronized (mLock) {
                if (onboardingBugreportV2Enabled()) {
                    final int uidForUser = Binder.withCleanCallingIdentity(() -> {
                        try {
                            return context.getPackageManager()
                                    .getPackageUidAsUser(callingInfo.second, userId);
                            return packageManager.getPackageUidAsUser(callingInfo.second, userId);
                        } catch (PackageManager.NameNotFoundException exception) {
                            throwInvalidBugreportFileForCallerException(
                                    bugreportFile, callingInfo.second);
@@ -441,8 +440,8 @@ class BugreportManagerServiceImpl extends IDumpstate.Stub {
        Slogf.i(TAG, "Retrieving bugreport for %s / %d", callingPackage, callingUid);
        try {
            mBugreportFileManager.ensureCallerPreviouslyGeneratedFile(
                    mContext, new Pair<>(callingUid, callingPackage), userId, bugreportFile,
                    /* forceUpdateMapping= */ false);
                    mContext, mContext.getPackageManager(), new Pair<>(callingUid, callingPackage),
                    userId, bugreportFile, /* forceUpdateMapping= */ false);
        } catch (IllegalArgumentException e) {
            Slog.e(TAG, e.getMessage());
            reportError(listener, IDumpstateListener.BUGREPORT_ERROR_NO_BUGREPORT_TO_RETRIEVE);
+22 −10
Original line number Diff line number Diff line
@@ -17,16 +17,19 @@
package com.android.server.os;

import android.app.admin.flags.Flags;
import static android.app.admin.flags.Flags.onboardingBugreportV2Enabled;

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

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

import static org.junit.Assert.assertThrows;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.when;

import android.app.role.RoleManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Binder;
import android.os.BugreportManager.BugreportCallback;
import android.os.IBinder;
@@ -48,6 +51,8 @@ import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;

import java.io.FileDescriptor;
import java.util.concurrent.CompletableFuture;
@@ -66,6 +71,9 @@ public class BugreportManagerServiceImplTest {
    private BugreportManagerServiceImpl mService;
    private BugreportManagerServiceImpl.BugreportFileManager mBugreportFileManager;

    @Mock
    private PackageManager mPackageManager;

    private int mCallingUid = 1234;
    private String mCallingPackage  = "test.package";
    private AtomicFile mMappingFile;
@@ -74,7 +82,8 @@ public class BugreportManagerServiceImplTest {
    private String mBugreportFile2 = "bugreport-file2.zip";

    @Before
    public void setUp() {
    public void setUp() throws Exception {
        MockitoAnnotations.initMocks(this);
        mContext = InstrumentationRegistry.getInstrumentation().getContext();
        mMappingFile = new AtomicFile(mContext.getFilesDir(), "bugreport-mapping.xml");
        ArraySet<String> mAllowlistedPackages = new ArraySet<>();
@@ -83,6 +92,7 @@ public class BugreportManagerServiceImplTest {
                new BugreportManagerServiceImpl.Injector(mContext, mAllowlistedPackages,
                        mMappingFile));
        mBugreportFileManager = new BugreportManagerServiceImpl.BugreportFileManager(mMappingFile);
        when(mPackageManager.getPackageUidAsUser(anyString(), anyInt())).thenReturn(mCallingUid);
    }

    @After
@@ -115,12 +125,13 @@ public class BugreportManagerServiceImplTest {

        assertThrows(IllegalArgumentException.class, () ->
                mBugreportFileManager.ensureCallerPreviouslyGeneratedFile(
                        mContext, callingInfo, Process.myUserHandle().getIdentifier(),
                        "unknown-file.zip", /* forceUpdateMapping= */ true));
                        mContext, mPackageManager,  callingInfo,
                        Process.myUserHandle().getIdentifier(), "unknown-file.zip",
                        /* forceUpdateMapping= */ true));

        // No exception should be thrown.
        mBugreportFileManager.ensureCallerPreviouslyGeneratedFile(
                mContext, callingInfo, mContext.getUserId(), mBugreportFile,
                mContext, mPackageManager, callingInfo, mContext.getUserId(), mBugreportFile,
                /* forceUpdateMapping= */ true);
    }

@@ -132,7 +143,7 @@ public class BugreportManagerServiceImplTest {
                callingInfo, mBugreportFile, /* keepOnRetrieval= */ true);

        mBugreportFileManager.ensureCallerPreviouslyGeneratedFile(
                mContext, callingInfo, mContext.getUserId(), mBugreportFile,
                mContext, mPackageManager, callingInfo, mContext.getUserId(), mBugreportFile,
                /* forceUpdateMapping= */ true);

        assertThat(mBugreportFileManager.mBugreportFilesToPersist).containsExactly(mBugreportFile);
@@ -148,10 +159,10 @@ public class BugreportManagerServiceImplTest {

        // No exception should be thrown.
        mBugreportFileManager.ensureCallerPreviouslyGeneratedFile(
                mContext, callingInfo, mContext.getUserId(), mBugreportFile,
                mContext, mPackageManager, callingInfo, mContext.getUserId(), mBugreportFile,
                /* forceUpdateMapping= */ true);
        mBugreportFileManager.ensureCallerPreviouslyGeneratedFile(
                mContext, callingInfo, mContext.getUserId(), mBugreportFile2,
                mContext, mPackageManager, callingInfo, mContext.getUserId(), mBugreportFile2,
                /* forceUpdateMapping= */ true);
    }

@@ -160,8 +171,9 @@ public class BugreportManagerServiceImplTest {
        Pair<Integer, String> callingInfo = new Pair<>(mCallingUid, mCallingPackage);
        assertThrows(IllegalArgumentException.class,
                () -> mBugreportFileManager.ensureCallerPreviouslyGeneratedFile(
                        mContext, callingInfo, Process.myUserHandle().getIdentifier(),
                        "test-file.zip", /* forceUpdateMapping= */ true));
                        mContext, mPackageManager, callingInfo,
                        Process.myUserHandle().getIdentifier(), "test-file.zip",
                        /* forceUpdateMapping= */ true));
    }

    @Test