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

Commit 201a8feb authored by Igor Covganet's avatar Igor Covganet
Browse files

Skip main user designation/creation when booting to HSU on provisioned devices.

When the HSUM device is provisioned, it's expected that it will boot in system used. I use the `config_hsumBootStrategy` flag as it specifies
the boot strategy and avoid creating an initial admin user.

Bug: 374926694
Flag: EXEMPT new code protected by new config that is enabled only for desktop
Test: manually build and deploy in DUT and test both cases
Change-Id: Iff76a8b7ab400962676ebe32dcb966e53272fddb
parent 11c49171
Loading
Loading
Loading
Loading
+17 −4
Original line number Diff line number Diff line
@@ -76,6 +76,9 @@ public final class HsumBootUserInitializer {
    /** Whether it should create an initial user, but without setting it as the main user. */
    private final boolean mShouldCreateInitialUser;

    /** The boot strategy. */
    private final int mBootStrategy;

    /** Static factory method for creating a {@link HsumBootUserInitializer} instance. */
    public static @Nullable HsumBootUserInitializer createInstance(UserManagerService ums,
            ActivityManagerService ams, PackageManagerService pms, ContentResolver contentResolver,
@@ -85,19 +88,21 @@ public final class HsumBootUserInitializer {
            return null;
        }
        return new HsumBootUserInitializer(ums, ams, pms, contentResolver,
                designateMainUserOnBoot(context), createInitialUserOnBoot(context));
                designateMainUserOnBoot(context), createInitialUserOnBoot(context),
                getBootStrategy(context));
    }

    @VisibleForTesting
    HsumBootUserInitializer(UserManagerService ums, ActivityManagerService ams,
            PackageManagerService pms, ContentResolver contentResolver,
            boolean shouldDesignateMainUser, boolean shouldCreateInitialUser) {
            boolean shouldDesignateMainUser, boolean shouldCreateInitialUser, int bootStrategy) {
        mUms = ums;
        mAms = ams;
        mPms = pms;
        mContentResolver = contentResolver;
        mShouldDesignateMainUser = shouldDesignateMainUser;
        mShouldCreateInitialUser = shouldCreateInitialUser;
        mBootStrategy = bootStrategy;
        mDeviceProvisionedObserver = (Flags.hsuDeviceProvisioner()
                    ? new HsuDeviceProvisioner(new Handler(Looper.getMainLooper()), contentResolver)
                    : new ContentObserver(new Handler(Looper.getMainLooper())) {
@@ -195,7 +200,10 @@ public final class HsumBootUserInitializer {
        int mainUserId = mUms.getMainUserId();
        t.traceEnd();

        if (mShouldDesignateMainUser) {
        final boolean isHsumBootStrategyOnProvisionedDevice =
                (mBootStrategy == UserManagerService.BOOT_STRATEGY_TO_HSU_FOR_PROVISIONED_DEVICE
                        && isDeviceProvisioned());
        if (mShouldDesignateMainUser && !isHsumBootStrategyOnProvisionedDevice) {
            designateMainUserIfNeeded(t, mainUserId);
            return;
        }
@@ -204,7 +212,7 @@ public final class HsumBootUserInitializer {
        demoteMainUserIfNeeded(t, mainUserId);
        t.traceEnd();

        if (mShouldCreateInitialUser) {
        if (mShouldCreateInitialUser && !isHsumBootStrategyOnProvisionedDevice) {
            createAdminUserIfNeeded(t);
            return;
        }
@@ -471,4 +479,9 @@ public final class HsumBootUserInitializer {
    static boolean createInitialUserOnBoot(Context context) {
        return context.getResources().getBoolean(R.bool.config_createInitialUser);
    }

    @VisibleForTesting
    static int getBootStrategy(Context context) {
        return context.getResources().getInteger(R.integer.config_hsumBootStrategy);
    }
}
+136 −33
Original line number Diff line number Diff line
@@ -89,6 +89,11 @@ public final class HsumBootUserInitializerInitMethodTest {
    @UserIdInt
    private static final int REGULAR_USER_ID = 16;

    private static final int DESKTOP_BOOT_STRATEGY =
            UserManagerService.BOOT_STRATEGY_TO_HSU_FOR_PROVISIONED_DEVICE;
    private static final int MOBILE_BOOT_STRATEGY =
            UserManagerService.BOOT_STRATEGY_DO_NOT_OVERRIDE;

    // Pre-defined users. NOTE: only setting basic flags and not setting UserType.
    private final UserInfo mHeadlessSystemUser =
            createUser(USER_SYSTEM, FLAG_SYSTEM | FLAG_ADMIN);
@@ -126,46 +131,143 @@ public final class HsumBootUserInitializerInitMethodTest {

    private final boolean mShouldAlwaysHaveMainUser;
    private final boolean mShouldCreateInitialUser;
    private final int mBootStrategy;
    private final InitialUsers mInitialUsers;
    private final ExpectedResult mExpectedResult;

    /** Useless javadoc to make checkstyle happy... */
    @Parameters(name = "{index}: hasMain={0},createInitial={1},initial={2},result={3}")
    @Parameters(
            name = "{index}: hasMain={0},createInitial={1},initial={2},bootStrategy={3},result={4}")
    public static Collection<Object[]> junitParametersPassedToConstructor() {
        return Arrays.asList(new Object[][] {
                // shouldAlwaysHaveMainUser false, shouldCreateInitialUser false
                { false, false, SYSTEM_ONLY, NO_USER_CREATED },
                { false, false, SYSTEM_AND_MAIN, MAIN_USER_DEMOTED },
                { false, false, SYSTEM_AND_ADMINS, NO_USER_CREATED },
                { false, false, SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE, NO_USER_CREATED },
                { false, false, SYSTEM_AND_REGULAR, NO_USER_CREATED },
                // shouldAlwaysHaveMainUser false, shouldCreateInitialUser true
                { false, true, SYSTEM_ONLY, ADMIN_USER_CREATED},
                { false, true, SYSTEM_AND_MAIN, MAIN_USER_DEMOTED },
                { false, true, SYSTEM_AND_ADMINS, NO_USER_CREATED },
                { false, true, SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE, NO_USER_CREATED },
                { false, true, SYSTEM_AND_REGULAR, NO_USER_CREATED },
                // shouldAlwaysHaveMainUser true, shouldCreateInitialUser false
                { true, false, SYSTEM_ONLY, MAIN_USER_CREATED },
                { true, false, SYSTEM_AND_MAIN, NO_USER_CREATED },
                { true, false, SYSTEM_AND_ADMINS, FIRST_ADMIN_USER_PROMOTED_TO_MAIN },
                { true, false, SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE,
                    SECOND_ADMIN_USER_PROMOTED_TO_MAIN },
                { true, false, SYSTEM_AND_REGULAR, MAIN_USER_CREATED },
                // shouldAlwaysHaveMainUser true, shouldCreateInitialUser true
                { true, true, SYSTEM_ONLY, MAIN_USER_CREATED },
                { true, true, SYSTEM_AND_MAIN, NO_USER_CREATED },
                { true, true, SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE,
                    SECOND_ADMIN_USER_PROMOTED_TO_MAIN },
                { true, true, SYSTEM_AND_REGULAR, MAIN_USER_CREATED }
        return Arrays.asList(
                new Object[][] {
                    // shouldAlwaysHaveMainUser false, shouldCreateInitialUser false,
                    // bootStrategy MOBILE_BOOT_STRATEGY
                    {false, false, SYSTEM_ONLY, MOBILE_BOOT_STRATEGY, NO_USER_CREATED},
                    {false, false, SYSTEM_AND_MAIN, MOBILE_BOOT_STRATEGY, MAIN_USER_DEMOTED},
                    {false, false, SYSTEM_AND_ADMINS, MOBILE_BOOT_STRATEGY, NO_USER_CREATED},
                    {
                        false,
                        false,
                        SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE,
                        MOBILE_BOOT_STRATEGY,
                        NO_USER_CREATED
                    },
                    {false, false, SYSTEM_AND_REGULAR, MOBILE_BOOT_STRATEGY, NO_USER_CREATED},
                    // shouldAlwaysHaveMainUser false, shouldCreateInitialUser true,
                    // bootStrategy MOBILE_BOOT_STRATEGY
                    // true
                    {false, true, SYSTEM_ONLY, MOBILE_BOOT_STRATEGY, ADMIN_USER_CREATED},
                    {false, true, SYSTEM_AND_MAIN, MOBILE_BOOT_STRATEGY, MAIN_USER_DEMOTED},
                    {false, true, SYSTEM_AND_ADMINS, MOBILE_BOOT_STRATEGY, NO_USER_CREATED},
                    {
                        false,
                        true,
                        SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE,
                        MOBILE_BOOT_STRATEGY,
                        NO_USER_CREATED
                    },
                    {false, true, SYSTEM_AND_REGULAR, MOBILE_BOOT_STRATEGY, NO_USER_CREATED},
                    // shouldAlwaysHaveMainUser true, shouldCreateInitialUser false,
                    // bootStrategy MOBILE_BOOT_STRATEGY
                    {true, false, SYSTEM_ONLY, MOBILE_BOOT_STRATEGY, MAIN_USER_CREATED},
                    {true, false, SYSTEM_AND_MAIN, MOBILE_BOOT_STRATEGY, NO_USER_CREATED},
                    {
                        true,
                        false,
                        SYSTEM_AND_ADMINS,
                        MOBILE_BOOT_STRATEGY,
                        FIRST_ADMIN_USER_PROMOTED_TO_MAIN
                    },
                    {
                        true,
                        false,
                        SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE,
                        MOBILE_BOOT_STRATEGY,
                        SECOND_ADMIN_USER_PROMOTED_TO_MAIN
                    },
                    {true, false, SYSTEM_AND_REGULAR, MOBILE_BOOT_STRATEGY, MAIN_USER_CREATED},
                    // shouldAlwaysHaveMainUser true, shouldCreateInitialUser true,
                    // bootStrategy MOBILE_BOOT_STRATEGY
                    {true, true, SYSTEM_ONLY, MOBILE_BOOT_STRATEGY, MAIN_USER_CREATED},
                    {true, true, SYSTEM_AND_MAIN, MOBILE_BOOT_STRATEGY, NO_USER_CREATED},
                    {
                        true,
                        true,
                        SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE,
                        MOBILE_BOOT_STRATEGY,
                        SECOND_ADMIN_USER_PROMOTED_TO_MAIN
                    },
                    {true, true, SYSTEM_AND_REGULAR, MOBILE_BOOT_STRATEGY, MAIN_USER_CREATED},
                    // bootStrategy DESKTOP_BOOT_STRATEGY
                    {true, true, SYSTEM_AND_MAIN, DESKTOP_BOOT_STRATEGY, NO_USER_CREATED},
                    {true, false, SYSTEM_AND_MAIN, DESKTOP_BOOT_STRATEGY, NO_USER_CREATED},
                    {false, true, SYSTEM_AND_MAIN, DESKTOP_BOOT_STRATEGY, MAIN_USER_DEMOTED},
                    {false, false, SYSTEM_AND_MAIN, DESKTOP_BOOT_STRATEGY, MAIN_USER_DEMOTED},
                    {true, true, SYSTEM_ONLY, DESKTOP_BOOT_STRATEGY, MAIN_USER_CREATED},
                    {
                        true,
                        true,
                        SYSTEM_AND_ADMINS,
                        DESKTOP_BOOT_STRATEGY,
                        FIRST_ADMIN_USER_PROMOTED_TO_MAIN
                    },
{
                        true,
                        true,
                        SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE,
                        DESKTOP_BOOT_STRATEGY,
                        SECOND_ADMIN_USER_PROMOTED_TO_MAIN
                    },
                    {true, true, SYSTEM_AND_REGULAR, DESKTOP_BOOT_STRATEGY, MAIN_USER_CREATED},
                    // hasMain=true, createInitial=false
                    {true, false, SYSTEM_ONLY, DESKTOP_BOOT_STRATEGY, MAIN_USER_CREATED},
                    {
                        true,
                        false,
                        SYSTEM_AND_ADMINS,
                        DESKTOP_BOOT_STRATEGY,
                        FIRST_ADMIN_USER_PROMOTED_TO_MAIN
                    },
                    {
                        true,
                        false,
                        SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE,
                        DESKTOP_BOOT_STRATEGY,
                        SECOND_ADMIN_USER_PROMOTED_TO_MAIN
                    },
                    {true, false, SYSTEM_AND_REGULAR, DESKTOP_BOOT_STRATEGY, MAIN_USER_CREATED},
                    // hasMain=false, createInitial=true
                    {false, true, SYSTEM_ONLY, DESKTOP_BOOT_STRATEGY, ADMIN_USER_CREATED},
                    {false, true, SYSTEM_AND_ADMINS, DESKTOP_BOOT_STRATEGY, NO_USER_CREATED},
                    {
                        false,
                        true,
                        SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE,
                        DESKTOP_BOOT_STRATEGY,
                        NO_USER_CREATED
                    },
                    {false, true, SYSTEM_AND_REGULAR, DESKTOP_BOOT_STRATEGY, NO_USER_CREATED},
                    // hasMain=false, createInitial=false
                    {false, false, SYSTEM_ONLY, DESKTOP_BOOT_STRATEGY, NO_USER_CREATED},
                    {false, false, SYSTEM_AND_ADMINS, DESKTOP_BOOT_STRATEGY, NO_USER_CREATED},
                    {
                        false,
                        false,
                        SYSTEM_AND_ADMINS_FIRST_ADMIN_UNPROMOTABLE,
                        DESKTOP_BOOT_STRATEGY,
                        NO_USER_CREATED
                    },
                    {false, false, SYSTEM_AND_REGULAR, DESKTOP_BOOT_STRATEGY, NO_USER_CREATED}
                });
    }

    public HsumBootUserInitializerInitMethodTest(boolean shouldAlwaysHaveMainUser,
            boolean shouldCreateInitialUser, InitialUsers initialUsers,
            boolean shouldCreateInitialUser, InitialUsers initialUsers, int bootStrategy,
            ExpectedResult expectedResult) {
        mShouldAlwaysHaveMainUser = shouldAlwaysHaveMainUser;
        mShouldCreateInitialUser = shouldCreateInitialUser;
        mBootStrategy = bootStrategy;
        mInitialUsers = initialUsers;
        mExpectedResult = expectedResult;
        Log.i(TAG, "Constructor: shouldAlwaysHaveMainUser=" + shouldAlwaysHaveMainUser
@@ -235,7 +337,7 @@ public final class HsumBootUserInitializerInitMethodTest {
    @EnableFlags(FLAG_CREATE_INITIAL_USER)
    public void testFlagEnabled() {
        var initializer = createHsumBootUserInitializer(mShouldAlwaysHaveMainUser,
                mShouldCreateInitialUser);
                mShouldCreateInitialUser, mBootStrategy);

        initializer.init(mTracer);

@@ -281,7 +383,8 @@ public final class HsumBootUserInitializerInitMethodTest {
    @DisableFlags(FLAG_CREATE_INITIAL_USER)
    public void testFlagDisabled() {
        var initializer =
                createHsumBootUserInitializer(mShouldAlwaysHaveMainUser, mShouldCreateInitialUser);
                createHsumBootUserInitializer(
                        mShouldAlwaysHaveMainUser, mShouldCreateInitialUser, mBootStrategy);

        initializer.init(mTracer);

@@ -300,10 +403,10 @@ public final class HsumBootUserInitializerInitMethodTest {
    }

    private HsumBootUserInitializer createHsumBootUserInitializer(
            boolean shouldAlwaysHaveMainUser, boolean shouldCreateInitialUser) {
            boolean shouldAlwaysHaveMainUser, boolean shouldCreateInitialUser, int bootStrategy) {
        mTracer = new TimingsTraceAndSlog(TAG);
        return new HsumBootUserInitializer(mMockUms, mMockAms, mMockPms, mMockContentResolver,
                shouldAlwaysHaveMainUser, shouldCreateInitialUser);
                shouldAlwaysHaveMainUser, shouldCreateInitialUser, bootStrategy);
    }

    private void expectMainUserCreated() {
+6 −1
Original line number Diff line number Diff line
@@ -54,6 +54,9 @@ public final class HsumBootUserInitializerTest {

    private static final String TAG = HsumBootUserInitializerTest.class.getSimpleName();

    private static final int MOBILE_BOOT_STRATEGY =
            UserManagerService.BOOT_STRATEGY_TO_HSU_FOR_PROVISIONED_DEVICE;

    @Rule
    public final Expect expect = Expect.create();
    @Rule
@@ -83,7 +86,9 @@ public final class HsumBootUserInitializerTest {
    public void setFixtures() {
        mFixture = new HsumBootUserInitializer(mMockUms, mMockAms, mMockPms, mMockContentResolver,
                // value of args below don't matter
                /* shouldDesignateMainUser= */ false, /* shouldCreateInitialUser= */ false);
                /* shouldDesignateMainUser= */ false,
                /* shouldCreateInitialUser= */ false,
                /* bootStrategy= */ MOBILE_BOOT_STRATEGY);
    }

    @Test