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

Commit 8c37d2b6 authored by Achim Thesmann's avatar Achim Thesmann
Browse files

Move tests to align with tested classes

A lot of tests remained in ActivityStarterTests that tested logic that
was moved out of ActivityStarter at some point (e.g into
BackgroundActivityStartController). This moved the BAL related tests
to BackgroundActivityStartControllerExemptionTests (other parts have
been moved to BackgroundActivityStartController*Tests before) and the
tests only test the actual BAL logic. Instead of testing
BackgroundLaunchProcessController related code in
BackgroundActivityStartController*Tests a new test class was created.

Flag: TEST_ONLY
Test: atest BackgroundActivityStartController*Tests ActivityStarterTests BackgroundLaunchProcessControllerTests
Bug: 345358113
Change-Id: I78cbca097278c172b854a80f899460537eb04e21
parent 3e4f626e
Loading
Loading
Loading
Loading
+24 −467

File changed.

Preview size limit exceeded, changes collapsed.

+133 −0
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import static com.google.common.truth.Truth.assertWithMessage;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.app.ActivityOptions;
@@ -564,4 +565,136 @@ public class BackgroundActivityStartControllerExemptionTests {
        assertWithMessage(balState.toString()).that(callerVerdict.getCode()).isEqualTo(
                BAL_ALLOW_SAW_PERMISSION);
    }

    @Test
    public void testCaller_isRecents() {
        int callingUid = REGULAR_UID_1;
        int callingPid = REGULAR_PID_1;
        final String callingPackage = REGULAR_PACKAGE_1;
        int realCallingUid = REGULAR_UID_2;
        int realCallingPid = REGULAR_PID_2;

        // setup state
        //if (mSupervisor.mRecentTasks.isCallerRecents(state.mCallingUid))
        RecentTasks recentTasks = mock(RecentTasks.class);
        when(recentTasks.isCallerRecents(eq(callingUid))).thenReturn(true);
        mSupervisor.mRecentTasks = recentTasks;

        // prepare call
        PendingIntentRecord originatingPendingIntent = mPendingIntentRecord;
        BackgroundStartPrivileges forcedBalByPiSender = BackgroundStartPrivileges.NONE;
        Intent intent = TEST_INTENT;
        ActivityOptions checkedOptions = mCheckedOptions;
        BackgroundActivityStartController.BalState balState = mController.new BalState(callingUid,
                callingPid, callingPackage, realCallingUid, realCallingPid, null,
                originatingPendingIntent, forcedBalByPiSender, mResultRecord, intent,
                checkedOptions);

        // call
        BalVerdict callerVerdict = mController.checkBackgroundActivityStartAllowedByCaller(
                balState);
        balState.setResultForCaller(callerVerdict);

        // assertions
        assertWithMessage(balState.toString()).that(callerVerdict.getCode()).isEqualTo(
                BAL_ALLOW_ALLOWLISTED_COMPONENT);
    }

    @Test
    public void testCaller_isDeviceOwner() {
        int callingUid = REGULAR_UID_1;
        int callingPid = REGULAR_PID_1;
        final String callingPackage = REGULAR_PACKAGE_1;
        int realCallingUid = REGULAR_UID_2;
        int realCallingPid = REGULAR_PID_2;

        // setup state
        when(mService.isDeviceOwner(eq(callingUid))).thenReturn(true);

        // prepare call
        PendingIntentRecord originatingPendingIntent = mPendingIntentRecord;
        BackgroundStartPrivileges forcedBalByPiSender = BackgroundStartPrivileges.NONE;
        Intent intent = TEST_INTENT;
        ActivityOptions checkedOptions = mCheckedOptions;
        BackgroundActivityStartController.BalState balState = mController.new BalState(callingUid,
                callingPid, callingPackage, realCallingUid, realCallingPid, null,
                originatingPendingIntent, forcedBalByPiSender, mResultRecord, intent,
                checkedOptions);

        // call
        BalVerdict callerVerdict = mController.checkBackgroundActivityStartAllowedByCaller(
                balState);
        balState.setResultForCaller(callerVerdict);

        // assertions
        assertWithMessage(balState.toString()).that(callerVerdict.getCode()).isEqualTo(
                BAL_ALLOW_ALLOWLISTED_COMPONENT);
    }

    @Test
    public void testCaller_isAffiliatedProfileOwner() {
        int callingUid = REGULAR_UID_1;
        int callingPid = REGULAR_PID_1;
        final String callingPackage = REGULAR_PACKAGE_1;
        int realCallingUid = REGULAR_UID_2;
        int realCallingPid = REGULAR_PID_2;

        // setup state
        when(mService.isAffiliatedProfileOwner(eq(callingUid))).thenReturn(true);

        // prepare call
        PendingIntentRecord originatingPendingIntent = mPendingIntentRecord;
        BackgroundStartPrivileges forcedBalByPiSender = BackgroundStartPrivileges.NONE;
        Intent intent = TEST_INTENT;
        ActivityOptions checkedOptions = mCheckedOptions;
        BackgroundActivityStartController.BalState balState = mController.new BalState(callingUid,
                callingPid, callingPackage, realCallingUid, realCallingPid, null,
                originatingPendingIntent, forcedBalByPiSender, mResultRecord, intent,
                checkedOptions);

        // call
        BalVerdict callerVerdict = mController.checkBackgroundActivityStartAllowedByCaller(
                balState);
        balState.setResultForCaller(callerVerdict);

        // assertions
        assertWithMessage(balState.toString()).that(callerVerdict.getCode()).isEqualTo(
                BAL_ALLOW_ALLOWLISTED_COMPONENT);
    }

    @Test
    public void testCaller_isExemptFromBgStartRestriction() {
        int callingUid = REGULAR_UID_1;
        int callingPid = REGULAR_PID_1;
        final String callingPackage = REGULAR_PACKAGE_1;
        int realCallingUid = REGULAR_UID_2;
        int realCallingPid = REGULAR_PID_2;

        mDeviceConfig.set("system_exempt_from_activity_bg_start_restriction_enabled", "true");
        AppOpsManager appOpsManager = mock(AppOpsManager.class);
        when(mService.getAppOpsManager()).thenReturn(appOpsManager);
        when(appOpsManager.checkOpNoThrow(eq(
                        AppOpsManager.OP_SYSTEM_EXEMPT_FROM_ACTIVITY_BG_START_RESTRICTION),
                eq(callingUid), eq(callingPackage))).thenReturn(AppOpsManager.MODE_ALLOWED);


        // prepare call
        PendingIntentRecord originatingPendingIntent = mPendingIntentRecord;
        BackgroundStartPrivileges forcedBalByPiSender = BackgroundStartPrivileges.NONE;
        Intent intent = TEST_INTENT;
        ActivityOptions checkedOptions = mCheckedOptions;
        BackgroundActivityStartController.BalState balState = mController.new BalState(callingUid,
                callingPid, callingPackage, realCallingUid, realCallingPid, null,
                originatingPendingIntent, forcedBalByPiSender, mResultRecord, intent,
                checkedOptions);

        // call
        BalVerdict callerVerdict = mController.checkBackgroundActivityStartAllowedByCaller(
                balState);
        balState.setResultForCaller(callerVerdict);

        // assertions
        assertWithMessage(balState.toString()).that(callerVerdict.getCode()).isEqualTo(
                BAL_ALLOW_PERMISSION);
    }
}
+189 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.server.wm;

import static com.android.server.wm.ActivityTaskManagerService.APP_SWITCH_ALLOW;
import static com.android.server.wm.ActivityTaskManagerService.APP_SWITCH_DISALLOW;
import static com.android.server.wm.BackgroundActivityStartController.BAL_ALLOW_FOREGROUND;
import static com.android.server.wm.BackgroundActivityStartController.BAL_ALLOW_GRACE_PERIOD;
import static com.android.server.wm.BackgroundActivityStartController.BAL_ALLOW_PERMISSION;
import static com.android.server.wm.BackgroundActivityStartController.BAL_ALLOW_VISIBLE_WINDOW;
import static com.android.server.wm.BackgroundActivityStartController.BAL_BLOCK;

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

import android.app.BackgroundStartPrivileges;
import android.content.Context;
import android.os.Binder;
import android.os.IBinder;
import android.platform.test.annotations.Presubmit;

import androidx.test.filters.SmallTest;

import com.android.server.wm.BackgroundActivityStartController.BalVerdict;

import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Tests for the {@link BackgroundLaunchProcessController} class.
 *
 * Build/Install/Run:
 * atest WmTests:BackgroundLaunchProcessControllerTests
 */
@SmallTest
@Presubmit
@RunWith(JUnit4.class)
public class BackgroundLaunchProcessControllerTests {

    Set<IBinder> mActivityStartAllowed = new HashSet<>();
    Set<Integer> mHasActiveVisibleWindow = new HashSet<>();

    BackgroundActivityStartCallback mCallback = new BackgroundActivityStartCallback() {
        @Override
        public boolean isActivityStartAllowed(Collection<IBinder> tokens, int uid,
                String packageName) {
            for (IBinder token : tokens) {
                if (token == null || mActivityStartAllowed.contains(token)) {
                    return true;
                }
            }
            return false;
        }

        @Override
        public boolean canCloseSystemDialogs(Collection<IBinder> tokens, int uid) {
            return false;
        }
    };
    BackgroundLaunchProcessController mController = new BackgroundLaunchProcessController(
            mHasActiveVisibleWindow::contains, mCallback);

    int mPid = 123;
    int mUid = 234;
    String mPackageName = "package.name";
    int mAppSwitchState = APP_SWITCH_DISALLOW;
    boolean mIsCheckingForFgsStart = false;
    boolean mHasActivityInVisibleTask = false;
    boolean mHasBackgroundActivityStartPrivileges = false;
    long mLastStopAppSwitchesTime = 0L;
    long mLastActivityLaunchTime = 0L;
    long mLastActivityFinishTime = 0L;

    @Test
    public void testNothingAllows() {
        BalVerdict balVerdict = mController.areBackgroundActivityStartsAllowed(
                mPid, mUid, mPackageName,
                mAppSwitchState, mIsCheckingForFgsStart,
                mHasActivityInVisibleTask, mHasBackgroundActivityStartPrivileges,
                mLastStopAppSwitchesTime, mLastActivityLaunchTime,
                mLastActivityFinishTime);
        assertThat(balVerdict.getCode()).isEqualTo(BAL_BLOCK);
    }

    @Test
    public void testInstrumenting() {
        mHasBackgroundActivityStartPrivileges = true;
        BalVerdict balVerdict = mController.areBackgroundActivityStartsAllowed(
                mPid, mUid, mPackageName,
                mAppSwitchState, mIsCheckingForFgsStart,
                mHasActivityInVisibleTask, mHasBackgroundActivityStartPrivileges,
                mLastStopAppSwitchesTime, mLastActivityLaunchTime,
                mLastActivityFinishTime);
        assertThat(balVerdict.getCode()).isEqualTo(BAL_ALLOW_PERMISSION);
    }

    @Test
    public void testAllowedByTokenNoCallback() {
        mController = new BackgroundLaunchProcessController(mHasActiveVisibleWindow::contains,
                null);
        Binder token = new Binder();
        mActivityStartAllowed.add(token);
        mController.addOrUpdateAllowBackgroundStartPrivileges(token,
                BackgroundStartPrivileges.ALLOW_BAL);
        BalVerdict balVerdict = mController.areBackgroundActivityStartsAllowed(
                mPid, mUid, mPackageName,
                mAppSwitchState, mIsCheckingForFgsStart,
                mHasActivityInVisibleTask, mHasBackgroundActivityStartPrivileges,
                mLastStopAppSwitchesTime, mLastActivityLaunchTime,
                mLastActivityFinishTime);
        assertThat(balVerdict.getCode()).isEqualTo(BAL_ALLOW_PERMISSION);
    }

    @Test
    public void testAllowedByToken() {
        Binder token = new Binder();
        mActivityStartAllowed.add(token);
        mController.addOrUpdateAllowBackgroundStartPrivileges(token,
                BackgroundStartPrivileges.ALLOW_BAL);
        BalVerdict balVerdict = mController.areBackgroundActivityStartsAllowed(
                mPid, mUid, mPackageName,
                mAppSwitchState, mIsCheckingForFgsStart,
                mHasActivityInVisibleTask, mHasBackgroundActivityStartPrivileges,
                mLastStopAppSwitchesTime, mLastActivityLaunchTime,
                mLastActivityFinishTime);
        assertThat(balVerdict.getCode()).isEqualTo(BAL_ALLOW_PERMISSION);
    }

    @Test
    public void testBoundByForeground() {
        mAppSwitchState = APP_SWITCH_ALLOW;
        mController.addBoundClientUid(999, "visible.package", Context.BIND_ALLOW_ACTIVITY_STARTS);
        mHasActiveVisibleWindow.add(999);
        BalVerdict balVerdict = mController.areBackgroundActivityStartsAllowed(
                mPid, mUid, mPackageName,
                mAppSwitchState, mIsCheckingForFgsStart,
                mHasActivityInVisibleTask, mHasBackgroundActivityStartPrivileges,
                mLastStopAppSwitchesTime, mLastActivityLaunchTime,
                mLastActivityFinishTime);
        assertThat(balVerdict.getCode()).isEqualTo(BAL_ALLOW_VISIBLE_WINDOW);
    }

    @Test
    public void testForegroundTask() {
        mAppSwitchState = APP_SWITCH_ALLOW;
        mHasActivityInVisibleTask = true;
        BalVerdict balVerdict = mController.areBackgroundActivityStartsAllowed(
                mPid, mUid, mPackageName,
                mAppSwitchState, mIsCheckingForFgsStart,
                mHasActivityInVisibleTask, mHasBackgroundActivityStartPrivileges,
                mLastStopAppSwitchesTime, mLastActivityLaunchTime,
                mLastActivityFinishTime);
        assertThat(balVerdict.getCode()).isEqualTo(BAL_ALLOW_FOREGROUND);
    }

    @Test
    public void testGracePeriod() {
        mAppSwitchState = APP_SWITCH_ALLOW;
        long now = System.currentTimeMillis();
        mLastStopAppSwitchesTime = now - 10000;
        mLastActivityLaunchTime = now - 9000;
        mLastActivityFinishTime = now - 100;
        BalVerdict balVerdict = mController.areBackgroundActivityStartsAllowed(
                mPid, mUid, mPackageName,
                mAppSwitchState, mIsCheckingForFgsStart,
                mHasActivityInVisibleTask, mHasBackgroundActivityStartPrivileges,
                mLastStopAppSwitchesTime, mLastActivityLaunchTime,
                mLastActivityFinishTime);
        assertThat(balVerdict.getCode()).isEqualTo(BAL_ALLOW_GRACE_PERIOD);
    }
}