Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | package com.android.systemui.biometrics; |
| 18 | |
| 19 | import static junit.framework.Assert.assertEquals; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 20 | import static junit.framework.Assert.assertNull; |
| 21 | import static junit.framework.TestCase.assertNotNull; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 22 | |
| 23 | import static org.mockito.ArgumentMatchers.any; |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 24 | import static org.mockito.ArgumentMatchers.anyInt; |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 25 | import static org.mockito.ArgumentMatchers.anyString; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 26 | import static org.mockito.ArgumentMatchers.eq; |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 27 | import static org.mockito.Mockito.doAnswer; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 28 | import static org.mockito.Mockito.mock; |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 29 | import static org.mockito.Mockito.never; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 30 | import static org.mockito.Mockito.spy; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 31 | import static org.mockito.Mockito.verify; |
| 32 | import static org.mockito.Mockito.when; |
| 33 | |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 34 | import android.app.ActivityManager; |
| 35 | import android.app.IActivityTaskManager; |
| 36 | import android.content.ComponentName; |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 37 | import android.content.Context; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 38 | import android.content.pm.PackageManager; |
| 39 | import android.content.res.Configuration; |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 40 | import android.hardware.biometrics.Authenticator; |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 41 | import android.hardware.biometrics.BiometricAuthenticator; |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 42 | import android.hardware.biometrics.BiometricConstants; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 43 | import android.hardware.biometrics.BiometricPrompt; |
| 44 | import android.hardware.biometrics.IBiometricServiceReceiverInternal; |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 45 | import android.hardware.face.FaceManager; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 46 | import android.os.Bundle; |
| 47 | import android.test.suitebuilder.annotation.SmallTest; |
| 48 | import android.testing.AndroidTestingRunner; |
| 49 | import android.testing.TestableContext; |
| 50 | import android.testing.TestableLooper.RunWithLooper; |
| 51 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 52 | import com.android.internal.R; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 53 | import com.android.systemui.SysuiTestCase; |
| 54 | import com.android.systemui.statusbar.CommandQueue; |
| 55 | import com.android.systemui.statusbar.phone.StatusBar; |
| 56 | |
| 57 | import org.junit.Before; |
| 58 | import org.junit.Test; |
| 59 | import org.junit.runner.RunWith; |
| 60 | import org.mockito.ArgumentCaptor; |
| 61 | import org.mockito.Mock; |
| 62 | import org.mockito.MockitoAnnotations; |
| 63 | |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 64 | import java.util.ArrayList; |
| 65 | import java.util.List; |
| 66 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 67 | @RunWith(AndroidTestingRunner.class) |
| 68 | @RunWithLooper |
| 69 | @SmallTest |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 70 | public class AuthControllerTest extends SysuiTestCase { |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 71 | |
| 72 | @Mock |
| 73 | private PackageManager mPackageManager; |
| 74 | @Mock |
| 75 | private IBiometricServiceReceiverInternal mReceiver; |
| 76 | @Mock |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 77 | private AuthDialog mDialog1; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 78 | @Mock |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 79 | private AuthDialog mDialog2; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 80 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 81 | private TestableAuthController mAuthController; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 82 | |
| 83 | |
| 84 | @Before |
| 85 | public void setup() { |
| 86 | MockitoAnnotations.initMocks(this); |
| 87 | |
| 88 | TestableContext context = spy(mContext); |
| 89 | |
| 90 | mContext.putComponent(StatusBar.class, mock(StatusBar.class)); |
| 91 | mContext.putComponent(CommandQueue.class, mock(CommandQueue.class)); |
| 92 | |
| 93 | when(context.getPackageManager()).thenReturn(mPackageManager); |
| 94 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_FACE)) |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 95 | .thenReturn(true); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 96 | when(mPackageManager.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 97 | .thenReturn(true); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 98 | |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 99 | when(mDialog1.getOpPackageName()).thenReturn("Dialog1"); |
| 100 | when(mDialog2.getOpPackageName()).thenReturn("Dialog2"); |
| 101 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 102 | when(mDialog1.isAllowDeviceCredentials()).thenReturn(false); |
| 103 | when(mDialog2.isAllowDeviceCredentials()).thenReturn(false); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 104 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 105 | mAuthController = new TestableAuthController(context, new MockInjector()); |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 106 | mAuthController.mComponents = mContext.getComponents(); |
| 107 | |
| 108 | mAuthController.start(); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 109 | } |
| 110 | |
| 111 | // Callback tests |
| 112 | |
| 113 | @Test |
| 114 | public void testSendsReasonUserCanceled_whenDismissedByUserCancel() throws Exception { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 115 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 116 | mAuthController.onDismissed(AuthDialogCallback.DISMISSED_USER_CANCELED); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 117 | verify(mReceiver).onDialogDismissed(BiometricPrompt.DISMISSED_REASON_USER_CANCEL); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Test |
| 121 | public void testSendsReasonNegative_whenDismissedByButtonNegative() throws Exception { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 122 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 123 | mAuthController.onDismissed(AuthDialogCallback.DISMISSED_BUTTON_NEGATIVE); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 124 | verify(mReceiver).onDialogDismissed(BiometricPrompt.DISMISSED_REASON_NEGATIVE); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 125 | } |
| 126 | |
| 127 | @Test |
| 128 | public void testSendsReasonConfirmed_whenDismissedByButtonPositive() throws Exception { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 129 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 130 | mAuthController.onDismissed(AuthDialogCallback.DISMISSED_BUTTON_POSITIVE); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 131 | verify(mReceiver).onDialogDismissed(BiometricPrompt.DISMISSED_REASON_BIOMETRIC_CONFIRMED); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | @Test |
| 135 | public void testSendsReasonConfirmNotRequired_whenDismissedByAuthenticated() throws Exception { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 136 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 137 | mAuthController.onDismissed(AuthDialogCallback.DISMISSED_BIOMETRIC_AUTHENTICATED); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 138 | verify(mReceiver).onDialogDismissed( |
| 139 | BiometricPrompt.DISMISSED_REASON_BIOMETRIC_CONFIRM_NOT_REQUIRED); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 140 | } |
| 141 | |
| 142 | @Test |
| 143 | public void testSendsReasonError_whenDismissedByError() throws Exception { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 144 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 145 | mAuthController.onDismissed(AuthDialogCallback.DISMISSED_ERROR); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 146 | verify(mReceiver).onDialogDismissed(BiometricPrompt.DISMISSED_REASON_ERROR); |
| 147 | } |
| 148 | |
| 149 | @Test |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 150 | public void testSendsReasonServerRequested_whenDismissedByServer() throws Exception { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 151 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 152 | mAuthController.onDismissed(AuthDialogCallback.DISMISSED_BY_SYSTEM_SERVER); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 153 | verify(mReceiver).onDialogDismissed(BiometricPrompt.DISMISSED_REASON_SERVER_REQUESTED); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 156 | @Test |
| 157 | public void testSendsReasonCredentialConfirmed_whenDeviceCredentialAuthenticated() |
| 158 | throws Exception { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 159 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 160 | mAuthController.onDismissed(AuthDialogCallback.DISMISSED_CREDENTIAL_AUTHENTICATED); |
Kevin Chyn | ff168dc | 2019-09-16 16:04:38 -0700 | [diff] [blame] | 161 | verify(mReceiver).onDialogDismissed(BiometricPrompt.DISMISSED_REASON_CREDENTIAL_CONFIRMED); |
| 162 | } |
| 163 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 164 | // Statusbar tests |
| 165 | |
| 166 | @Test |
| 167 | public void testShowInvoked_whenSystemRequested() |
| 168 | throws Exception { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 169 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | 9cf8991 | 2019-08-30 13:33:58 -0700 | [diff] [blame] | 170 | verify(mDialog1).show(any(), any()); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 174 | public void testOnAuthenticationSucceededInvoked_whenSystemRequested() { |
| 175 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 176 | mAuthController.onBiometricAuthenticated(); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 177 | verify(mDialog1).onAuthenticationSucceeded(); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | @Test |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 181 | public void testOnAuthenticationFailedInvoked_whenBiometricRejected() { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 182 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 183 | mAuthController.onBiometricError(BiometricAuthenticator.TYPE_NONE, |
| 184 | BiometricConstants.BIOMETRIC_PAUSED_REJECTED, |
| 185 | 0 /* vendorCode */); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 186 | |
| 187 | ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 188 | verify(mDialog1).onAuthenticationFailed(captor.capture()); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 189 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 190 | assertEquals(captor.getValue(), mContext.getString(R.string.biometric_not_recognized)); |
| 191 | } |
| 192 | |
| 193 | @Test |
| 194 | public void testOnAuthenticationFailedInvoked_whenBiometricTimedOut() { |
| 195 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 196 | final int error = BiometricConstants.BIOMETRIC_ERROR_TIMEOUT; |
| 197 | final int vendorCode = 0; |
| 198 | mAuthController.onBiometricError(BiometricAuthenticator.TYPE_FACE, error, vendorCode); |
| 199 | |
| 200 | ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
| 201 | verify(mDialog1).onAuthenticationFailed(captor.capture()); |
| 202 | |
| 203 | assertEquals(captor.getValue(), FaceManager.getErrorString(mContext, error, vendorCode)); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 207 | public void testOnHelpInvoked_whenSystemRequested() { |
| 208 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 209 | final String helpMessage = "help"; |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 210 | mAuthController.onBiometricHelp(helpMessage); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 211 | |
| 212 | ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 213 | verify(mDialog1).onHelp(captor.capture()); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 214 | |
| 215 | assertEquals(captor.getValue(), helpMessage); |
| 216 | } |
| 217 | |
| 218 | @Test |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 219 | public void testOnErrorInvoked_whenSystemRequested() throws Exception { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 220 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 221 | final int error = 1; |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 222 | final int vendorCode = 0; |
| 223 | mAuthController.onBiometricError(BiometricAuthenticator.TYPE_FACE, error, vendorCode); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 224 | |
| 225 | ArgumentCaptor<String> captor = ArgumentCaptor.forClass(String.class); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 226 | verify(mDialog1).onError(captor.capture()); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 227 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 228 | assertEquals(captor.getValue(), FaceManager.getErrorString(mContext, error, vendorCode)); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 232 | public void testErrorLockout_whenCredentialAllowed_AnimatesToCredentialUI() { |
| 233 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 234 | final int error = BiometricConstants.BIOMETRIC_ERROR_LOCKOUT; |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 235 | final int vendorCode = 0; |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 236 | |
| 237 | when(mDialog1.isAllowDeviceCredentials()).thenReturn(true); |
| 238 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 239 | mAuthController.onBiometricError(BiometricAuthenticator.TYPE_FACE, error, vendorCode); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 240 | verify(mDialog1, never()).onError(anyString()); |
| 241 | verify(mDialog1).animateToCredentialUI(); |
| 242 | } |
| 243 | |
| 244 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 245 | public void testErrorLockoutPermanent_whenCredentialAllowed_AnimatesToCredentialUI() { |
| 246 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 247 | final int error = BiometricConstants.BIOMETRIC_ERROR_LOCKOUT_PERMANENT; |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 248 | final int vendorCode = 0; |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 249 | |
| 250 | when(mDialog1.isAllowDeviceCredentials()).thenReturn(true); |
| 251 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 252 | mAuthController.onBiometricError(BiometricAuthenticator.TYPE_FACE, error, vendorCode); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 253 | verify(mDialog1, never()).onError(anyString()); |
| 254 | verify(mDialog1).animateToCredentialUI(); |
| 255 | } |
| 256 | |
| 257 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 258 | public void testErrorLockout_whenCredentialNotAllowed_sendsOnError() { |
| 259 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 260 | final int error = BiometricConstants.BIOMETRIC_ERROR_LOCKOUT; |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 261 | final int vendorCode = 0; |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 262 | |
| 263 | when(mDialog1.isAllowDeviceCredentials()).thenReturn(false); |
| 264 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 265 | mAuthController.onBiometricError(BiometricAuthenticator.TYPE_FACE, error, vendorCode); |
| 266 | verify(mDialog1).onError(eq(FaceManager.getErrorString(mContext, error, vendorCode))); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 267 | verify(mDialog1, never()).animateToCredentialUI(); |
| 268 | } |
| 269 | |
| 270 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 271 | public void testErrorLockoutPermanent_whenCredentialNotAllowed_sendsOnError() { |
| 272 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 273 | final int error = BiometricConstants.BIOMETRIC_ERROR_LOCKOUT_PERMANENT; |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 274 | final int vendorCode = 0; |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 275 | |
| 276 | when(mDialog1.isAllowDeviceCredentials()).thenReturn(false); |
| 277 | |
Ilya Matyukhin | 0f9da35 | 2019-10-03 14:10:01 -0700 | [diff] [blame] | 278 | mAuthController.onBiometricError(BiometricAuthenticator.TYPE_FACE, error, vendorCode); |
| 279 | verify(mDialog1).onError(eq(FaceManager.getErrorString(mContext, error, vendorCode))); |
Kevin Chyn | 8429da2 | 2019-09-24 12:42:35 -0700 | [diff] [blame] | 280 | verify(mDialog1, never()).animateToCredentialUI(); |
| 281 | } |
| 282 | |
| 283 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 284 | public void testDismissWithoutCallbackInvoked_whenSystemRequested() { |
| 285 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 286 | mAuthController.hideAuthenticationDialog(); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 287 | verify(mDialog1).dismissFromSystemServer(); |
| 288 | } |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 289 | |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 290 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 291 | public void testClientNotified_whenDismissedBySystemServer() { |
| 292 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 293 | mAuthController.hideAuthenticationDialog(); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 294 | verify(mDialog1).dismissFromSystemServer(); |
| 295 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 296 | assertNotNull(mAuthController.mCurrentDialog); |
| 297 | assertNotNull(mAuthController.mReceiver); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 298 | } |
| 299 | |
| 300 | // Corner case tests |
| 301 | |
| 302 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 303 | public void testShowNewDialog_beforeOldDialogDismissed_SkipsAnimations() { |
| 304 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | 9cf8991 | 2019-08-30 13:33:58 -0700 | [diff] [blame] | 305 | verify(mDialog1).show(any(), any()); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 306 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 307 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 308 | |
| 309 | // First dialog should be dismissed without animation |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 310 | verify(mDialog1).dismissWithoutCallback(eq(false) /* animate */); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 311 | |
| 312 | // Second dialog should be shown without animation |
Kevin Chyn | 9cf8991 | 2019-08-30 13:33:58 -0700 | [diff] [blame] | 313 | verify(mDialog2).show(any(), any()); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 314 | } |
| 315 | |
| 316 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 317 | public void testConfigurationPersists_whenOnConfigurationChanged() { |
| 318 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | 9cf8991 | 2019-08-30 13:33:58 -0700 | [diff] [blame] | 319 | verify(mDialog1).show(any(), any()); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 320 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 321 | // Return that the UI is in "showing" state |
| 322 | doAnswer(invocation -> { |
| 323 | Object[] args = invocation.getArguments(); |
| 324 | Bundle savedState = (Bundle) args[0]; |
| 325 | savedState.putInt( |
| 326 | AuthDialog.KEY_CONTAINER_STATE, AuthContainerView.STATE_SHOWING); |
| 327 | return null; // onSaveState returns void |
| 328 | }).when(mDialog1).onSaveState(any()); |
| 329 | |
| 330 | mAuthController.onConfigurationChanged(new Configuration()); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 331 | |
| 332 | ArgumentCaptor<Bundle> captor = ArgumentCaptor.forClass(Bundle.class); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 333 | verify(mDialog1).onSaveState(captor.capture()); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 334 | |
| 335 | // Old dialog doesn't animate |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 336 | verify(mDialog1).dismissWithoutCallback(eq(false /* animate */)); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 337 | |
| 338 | // Saved state is restored into new dialog |
| 339 | ArgumentCaptor<Bundle> captor2 = ArgumentCaptor.forClass(Bundle.class); |
Kevin Chyn | 9cf8991 | 2019-08-30 13:33:58 -0700 | [diff] [blame] | 340 | verify(mDialog2).show(any(), captor2.capture()); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 341 | |
| 342 | // TODO: This should check all values we want to save/restore |
| 343 | assertEquals(captor.getValue(), captor2.getValue()); |
| 344 | } |
| 345 | |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 346 | @Test |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 347 | public void testConfigurationPersists_whenBiometricFallbackToCredential() { |
| 348 | showDialog(Authenticator.TYPE_CREDENTIAL | Authenticator.TYPE_BIOMETRIC, |
| 349 | BiometricPrompt.TYPE_FACE); |
| 350 | verify(mDialog1).show(any(), any()); |
| 351 | |
| 352 | // Pretend that the UI is now showing device credential UI. |
| 353 | doAnswer(invocation -> { |
| 354 | Object[] args = invocation.getArguments(); |
| 355 | Bundle savedState = (Bundle) args[0]; |
| 356 | savedState.putInt( |
| 357 | AuthDialog.KEY_CONTAINER_STATE, AuthContainerView.STATE_SHOWING); |
| 358 | savedState.putBoolean(AuthDialog.KEY_CREDENTIAL_SHOWING, true); |
| 359 | return null; // onSaveState returns void |
| 360 | }).when(mDialog1).onSaveState(any()); |
| 361 | |
| 362 | mAuthController.onConfigurationChanged(new Configuration()); |
| 363 | |
| 364 | // Check that the new dialog was initialized to the credential UI. |
| 365 | ArgumentCaptor<Bundle> captor = ArgumentCaptor.forClass(Bundle.class); |
| 366 | verify(mDialog2).show(any(), captor.capture()); |
| 367 | assertEquals(Authenticator.TYPE_CREDENTIAL, |
| 368 | mAuthController.mLastBiometricPromptBundle |
| 369 | .getInt(BiometricPrompt.KEY_AUTHENTICATORS_ALLOWED)); |
| 370 | } |
| 371 | |
| 372 | @Test |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 373 | public void testClientNotified_whenTaskStackChangesDuringAuthentication() throws Exception { |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 374 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 375 | |
| 376 | List<ActivityManager.RunningTaskInfo> tasks = new ArrayList<>(); |
| 377 | ActivityManager.RunningTaskInfo taskInfo = mock(ActivityManager.RunningTaskInfo.class); |
| 378 | taskInfo.topActivity = mock(ComponentName.class); |
| 379 | when(taskInfo.topActivity.getPackageName()).thenReturn("other_package"); |
| 380 | tasks.add(taskInfo); |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 381 | when(mAuthController.mActivityTaskManager.getTasks(anyInt())).thenReturn(tasks); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 382 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 383 | mAuthController.mTaskStackListener.onTaskStackChanged(); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 384 | waitForIdleSync(); |
| 385 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 386 | assertNull(mAuthController.mCurrentDialog); |
| 387 | assertNull(mAuthController.mReceiver); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 388 | verify(mDialog1).dismissWithoutCallback(true /* animate */); |
| 389 | verify(mReceiver).onDialogDismissed(eq(BiometricPrompt.DISMISSED_REASON_USER_CANCEL)); |
| 390 | } |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 391 | |
Curtis Belmonte | ffa9d87 | 2019-10-24 12:55:01 -0700 | [diff] [blame] | 392 | @Test |
| 393 | public void testDoesNotCrash_whenTryAgainPressedAfterDismissal() { |
| 394 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 395 | mAuthController.onDismissed(AuthDialogCallback.DISMISSED_USER_CANCELED); |
| 396 | mAuthController.onTryAgainPressed(); |
| 397 | } |
| 398 | |
| 399 | @Test |
| 400 | public void testDoesNotCrash_whenDeviceCredentialPressedAfterDismissal() { |
| 401 | showDialog(Authenticator.TYPE_BIOMETRIC, BiometricPrompt.TYPE_FACE); |
| 402 | mAuthController.onDismissed(AuthDialogCallback.DISMISSED_USER_CANCELED); |
| 403 | mAuthController.onDeviceCredentialPressed(); |
| 404 | } |
| 405 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 406 | // Helpers |
| 407 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 408 | private void showDialog(int authenticators, int biometricModality) { |
| 409 | mAuthController.showAuthenticationDialog(createTestDialogBundle(authenticators), |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 410 | mReceiver /* receiver */, |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 411 | biometricModality, |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 412 | true /* requireConfirmation */, |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 413 | 0 /* userId */, |
| 414 | "testPackage"); |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 417 | private Bundle createTestDialogBundle(int authenticators) { |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 418 | Bundle bundle = new Bundle(); |
| 419 | |
| 420 | bundle.putCharSequence(BiometricPrompt.KEY_TITLE, "Title"); |
| 421 | bundle.putCharSequence(BiometricPrompt.KEY_SUBTITLE, "Subtitle"); |
| 422 | bundle.putCharSequence(BiometricPrompt.KEY_DESCRIPTION, "Description"); |
| 423 | bundle.putCharSequence(BiometricPrompt.KEY_NEGATIVE_TEXT, "Negative Button"); |
| 424 | |
| 425 | // RequireConfirmation is a hint to BiometricService. This can be forced to be required |
| 426 | // by user settings, and should be tested in BiometricService. |
| 427 | bundle.putBoolean(BiometricPrompt.KEY_REQUIRE_CONFIRMATION, true); |
| 428 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 429 | bundle.putInt(BiometricPrompt.KEY_AUTHENTICATORS_ALLOWED, authenticators); |
| 430 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 431 | return bundle; |
| 432 | } |
| 433 | |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 434 | private final class TestableAuthController extends AuthController { |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 435 | private int mBuildCount = 0; |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 436 | private Bundle mLastBiometricPromptBundle; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 437 | |
Dave Mankoff | a5d8a39 | 2019-10-10 12:21:09 -0400 | [diff] [blame] | 438 | TestableAuthController(Context context, Injector injector) { |
| 439 | super(context, injector); |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 442 | @Override |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 443 | protected AuthDialog buildDialog(Bundle biometricPromptBundle, |
Kevin Chyn | fc46826 | 2019-08-20 17:17:11 -0700 | [diff] [blame] | 444 | boolean requireConfirmation, int userId, int type, String opPackageName, |
Kevin Chyn | 86f1b8e | 2019-09-24 19:00:49 -0700 | [diff] [blame] | 445 | boolean skipIntro) { |
| 446 | |
| 447 | mLastBiometricPromptBundle = biometricPromptBundle; |
| 448 | |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 449 | AuthDialog dialog; |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 450 | if (mBuildCount == 0) { |
| 451 | dialog = mDialog1; |
| 452 | } else if (mBuildCount == 1) { |
| 453 | dialog = mDialog2; |
| 454 | } else { |
| 455 | dialog = null; |
| 456 | } |
| 457 | mBuildCount++; |
| 458 | return dialog; |
| 459 | } |
| 460 | } |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 461 | |
Kevin Chyn | f8688a0 | 2019-08-27 17:04:05 -0700 | [diff] [blame] | 462 | private final class MockInjector extends AuthController.Injector { |
Kevin Chyn | 050315f | 2019-08-08 14:22:54 -0700 | [diff] [blame] | 463 | @Override |
| 464 | IActivityTaskManager getActivityTaskManager() { |
| 465 | return mock(IActivityTaskManager.class); |
| 466 | } |
| 467 | } |
Kevin Chyn | c53d981 | 2019-07-30 18:10:30 -0700 | [diff] [blame] | 468 | } |
| 469 | |