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

Commit eb3e2b3b authored by Jonathan Scott's avatar Jonathan Scott
Browse files

Throw IllegalArgumentException when calling

LockPatternUtils#getCredentialTypeForUser with invalid ID.

Currently, using e.g. USER_CURRENT results in returning that the user
requires no credentials - which can lead to un-noticed bugs such as b/176801033

Test: atest com.android.internal.widget.LockPatternUtilsTest
Change-Id: I09be7f8f32e67ad039d9cd1d1710ef34ec198773
parent 6ac99dea
Loading
Loading
Loading
Loading
+4 −0
Original line number Diff line number Diff line
@@ -1103,6 +1103,10 @@ public class LockPatternUtils {
     * {@link #CREDENTIAL_TYPE_PASSWORD}
     */
    public @CredentialType int getCredentialTypeForUser(int userHandle) {
        if (userHandle < 0) {
            throw new IllegalArgumentException("Invalid userHandle: " + userHandle);
        }

        try {
            return getLockSettings().getCredentialType(userHandle);
        } catch (RemoteException re) {
+12 −0
Original line number Diff line number Diff line
@@ -18,10 +18,13 @@ package com.android.internal.widget;

import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertTrue;
import static org.testng.Assert.assertThrows;

import android.content.Context;
import android.os.UserHandle;

import androidx.test.filters.SmallTest;
import androidx.test.platform.app.InstrumentationRegistry;
import androidx.test.runner.AndroidJUnit4;

import org.junit.Test;
@@ -31,6 +34,9 @@ import org.junit.runner.RunWith;
@SmallTest
public class LockPatternUtilsTest {

    private static final int INVALID_USER_ID = -1;
    private final Context mContext = InstrumentationRegistry.getInstrumentation().getContext();

    @Test
    public void testUserFrp_isNotRegularUser() throws Exception {
        assertTrue(LockPatternUtils.USER_FRP < 0);
@@ -43,4 +49,10 @@ public class LockPatternUtilsTest {
        assertNotEquals(UserHandle.USER_CURRENT, LockPatternUtils.USER_FRP);
        assertNotEquals(UserHandle.USER_CURRENT_OR_SELF, LockPatternUtils.USER_FRP);
    }

    @Test
    public void getCredentialTypeForUser_invalidUserId_throwsIllegalArgumentException() {
        assertThrows(IllegalArgumentException.class,
                () -> new LockPatternUtils(mContext).getCredentialTypeForUser(INVALID_USER_ID));
    }
}