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

Commit 8320baba authored by Diya Bera's avatar Diya Bera
Browse files

Send error if enroll is invoked when face is already enrolled

Bug: 240091312
Test: atest FaceManagerTest
Change-Id: I8a111580989b2f930e08d2c5186aa6b217184810
parent 5cb5953a
Loading
Loading
Loading
Loading
+8 −0
Original line number Diff line number Diff line
@@ -342,6 +342,14 @@ public class FaceManager implements BiometricAuthenticator, BiometricFaceConstan
            return;
        }

        if (getEnrolledFaces(userId).size()
                >= mContext.getResources().getInteger(R.integer.config_faceMaxTemplatesPerUser)) {
            callback.onEnrollmentError(FACE_ERROR_HW_UNAVAILABLE,
                    getErrorString(mContext, FACE_ERROR_HW_UNAVAILABLE,
                            0 /* vendorCode */));
            return;
        }

        if (mService != null) {
            try {
                mEnrollmentCallback = callback;
+38 −3
Original line number Diff line number Diff line
@@ -21,8 +21,11 @@ import static android.hardware.biometrics.BiometricFaceConstants.FACE_ERROR_HW_U
import static com.google.common.truth.Truth.assertThat;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyLong;
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.atMost;
import static org.mockito.Mockito.eq;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify;
@@ -38,6 +41,8 @@ import android.os.RemoteException;
import android.os.test.TestLooper;
import android.platform.test.annotations.Presubmit;

import com.android.internal.R;

import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -50,6 +55,7 @@ import org.mockito.junit.MockitoJUnitRunner;
import org.mockito.junit.MockitoRule;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Presubmit
@@ -70,6 +76,8 @@ public class FaceManagerTest {
    private IFaceService mService;
    @Mock
    private FaceManager.AuthenticationCallback mAuthCallback;
    @Mock
    private FaceManager.EnrollmentCallback mEnrollmentCallback;

    @Captor
    private ArgumentCaptor<IFaceAuthenticatorsRegisteredCallback> mCaptor;
@@ -107,9 +115,7 @@ public class FaceManagerTest {

    @Test
    public void getSensorPropertiesInternal_noBinderCalls() throws RemoteException {
        verify(mService).addAuthenticatorsRegisteredCallback(mCaptor.capture());

        mCaptor.getValue().onAllAuthenticatorsRegistered(mProps);
        initializeProperties();
        List<FaceSensorPropertiesInternal> actual = mFaceManager.getSensorPropertiesInternal();

        assertThat(actual).containsExactlyElementsIn(mProps);
@@ -148,4 +154,33 @@ public class FaceManagerTest {

        verify(mAuthCallback).onAuthenticationError(eq(FACE_ERROR_HW_UNAVAILABLE), any());
    }

    @Test
    public void enrollment_errorWhenFaceEnrollmentExists() throws RemoteException {
        when(mResources.getInteger(R.integer.config_faceMaxTemplatesPerUser)).thenReturn(1);
        when(mService.getEnrolledFaces(anyInt(), anyInt(), anyString()))
                .thenReturn(Collections.emptyList())
                .thenReturn(Collections.singletonList(new Face("Face" /* name */, 0 /* faceId */,
                        0 /* deviceId */)));

        initializeProperties();
        mFaceManager.enroll(USER_ID, new byte[]{},
                new CancellationSignal(), mEnrollmentCallback, null /* disabledFeatures */);

        verify(mService).enroll(eq(USER_ID), any(), any(), any(), anyString(), any(), any(),
                anyBoolean());

        mFaceManager.enroll(USER_ID, new byte[]{},
                new CancellationSignal(), mEnrollmentCallback, null /* disabledFeatures */);

        verify(mService, atMost(1 /* maxNumberOfInvocations */)).enroll(eq(USER_ID), any(), any(),
                any(), anyString(), any(), any(), anyBoolean());
        verify(mEnrollmentCallback).onEnrollmentError(eq(FACE_ERROR_HW_UNAVAILABLE), anyString());
    }

    private void initializeProperties() throws RemoteException {
        verify(mService).addAuthenticatorsRegisteredCallback(mCaptor.capture());

        mCaptor.getValue().onAllAuthenticatorsRegistered(mProps);
    }
}