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

Commit 0c229aa6 authored by Oli Lan's avatar Oli Lan
Browse files

Enforce limit of one main user.

Currently the documentation states that there can only be one main
user, but this is not enforced. This CL adds a check so that a
second user with FLAG_MAIN cannot be created.

Bug: 294977112
Test: atest UserManagerTest
Change-Id: I33c0802b33dd1c049c9838bc9abde6a7a5ce73ad
parent 009d050e
Loading
Loading
Loading
Loading
+5 −0
Original line number Diff line number Diff line
@@ -4907,6 +4907,11 @@ public class UserManagerService extends IUserManager.Stub {
                                USER_OPERATION_ERROR_UNKNOWN);
                    }
                }
                if (isMainUser && getMainUserIdUnchecked() != UserHandle.USER_NULL) {
                    throwCheckedUserOperationException(
                            "Cannot add user with FLAG_MAIN as main user already exists.",
                            UserManager.USER_OPERATION_ERROR_MAX_USERS);
                }
                if (!preCreate && !canAddMoreUsersOfType(userTypeDetails)) {
                    throwCheckedUserOperationException(
                            "Cannot add more users of type " + userType
+32 −2
Original line number Diff line number Diff line
@@ -63,6 +63,8 @@ import org.junit.Rule;
import org.junit.Test;
import org.mockito.Mock;

import java.io.File;

/**
 * Run as {@code atest FrameworksMockingServicesTests:com.android.server.pm.UserManagerServiceTest}
 */
@@ -107,6 +109,8 @@ public final class UserManagerServiceTest {
            .getTargetContext();
    private final SparseArray<UserData> mUsers = new SparseArray<>();

    private File mTestDir;

    private Context mSpiedContext;

    private @Mock PackageManagerService mMockPms;
@@ -144,17 +148,23 @@ public final class UserManagerServiceTest {
        doNothing().when(mSpiedContext).sendBroadcastAsUser(any(), any(), any());

        // Must construct UserManagerService in the UiThread
        mTestDir = new File(mRealContext.getDataDir(), "umstest");
        mTestDir.mkdirs();
        mUms = new UserManagerService(mSpiedContext, mMockPms, mMockUserDataPreparer,
                mPackagesLock, mRealContext.getDataDir(), mUsers);
                mPackagesLock, mTestDir, mUsers);
        mUmi = LocalServices.getService(UserManagerInternal.class);
        assertWithMessage("LocalServices.getService(UserManagerInternal.class)").that(mUmi)
                .isNotNull();
    }

    @After
    public void resetUserManagerInternal() {
    public void tearDown() {
        // LocalServices follows the "Highlander rule" - There can be only one!
        LocalServices.removeServiceForTest(UserManagerInternal.class);

        // Clean up test dir to remove persisted user files.
        assertThat(deleteRecursive(mTestDir)).isTrue();
        mUsers.clear();
    }

    @Test
@@ -496,6 +506,14 @@ public final class UserManagerServiceTest {

    @Test
    public void testMainUser_hasNoCallsOrSMSRestrictionsByDefault() {
        // Remove the main user so we can add another one
        for (int i = 0; i < mUsers.size(); i++) {
            UserData userData = mUsers.valueAt(i);
            if (userData.info.isMain()) {
                mUsers.delete(i);
                break;
            }
        }
        UserInfo mainUser = mUms.createUserWithThrow("main user", USER_TYPE_FULL_SECONDARY,
                UserInfo.FLAG_FULL | UserInfo.FLAG_MAIN);

@@ -621,6 +639,18 @@ public final class UserManagerServiceTest {
        userData.mLastEnteredForegroundTimeMillis = timeMillis;
    }

    public boolean deleteRecursive(File file) {
        if (file.isDirectory()) {
            for (File item : file.listFiles()) {
                boolean success = deleteRecursive(item);
                if (!success) {
                    return false;
                }
            }
        }
        return file.delete();
    }

    private static final class TestUserData extends UserData {

        @SuppressWarnings("deprecation")
+19 −0
Original line number Diff line number Diff line
@@ -1547,6 +1547,25 @@ public final class UserManagerTest {
        assertThat(userInfo.name).isEqualTo(newName);
    }

    @Test
    public void testCannotCreateAdditionalMainUser() {
        UserHandle mainUser = mUserManager.getMainUser();
        assumeTrue("There is no main user", mainUser != null);

        // Users with FLAG_MAIN can't be removed, so no point using the local createUser method.
        UserInfo newMainUser = mUserManager.createUser("test", UserInfo.FLAG_MAIN);
        assertThat(newMainUser).isNull();

        List<UserInfo> users = mUserManager.getUsers();
        int mainUserCount = 0;
        for (UserInfo user : users) {
            if (user.isMain()) {
                mainUserCount++;
            }
        }
        assertThat(mainUserCount).isEqualTo(1);
    }

    private boolean isPackageInstalledForUser(String packageName, int userId) {
        try {
            return mPackageManager.getPackageInfoAsUser(packageName, 0, userId) != null;