Home | History | Annotate | Download | only in security
      1 /*
      2  * Copyright (C) 2018 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.cts.verifier.security;
     18 
     19 import android.content.DialogInterface;
     20 import android.hardware.biometrics.BiometricPrompt;
     21 import android.os.CancellationSignal;
     22 import android.os.Handler;
     23 import android.os.Looper;
     24 
     25 import java.util.concurrent.Executor;
     26 
     27 public class BiometricPromptBoundKeysTest extends FingerprintBoundKeysTest {
     28 
     29     private DialogCallback mDialogCallback;
     30     private BiometricPrompt mBiometricPrompt;
     31     private CancellationSignal mCancellationSignal;
     32 
     33     private final Handler mHandler = new Handler(Looper.getMainLooper());
     34 
     35     private final Executor mExecutor = (runnable) -> {
     36         mHandler.post(runnable);
     37     };
     38 
     39     private final Runnable mNegativeButtonRunnable = () -> {
     40         showToast("Authentication canceled by user");
     41     };
     42 
     43     private class DialogCallback extends
     44             BiometricPrompt.AuthenticationCallback {
     45         @Override
     46         public void onAuthenticationError(int errMsgId, CharSequence errString) {
     47             showToast(errString.toString());
     48         }
     49 
     50         @Override
     51         public void onAuthenticationSucceeded(BiometricPrompt.AuthenticationResult result) {
     52             if (tryEncrypt()) {
     53                 showToast("Test passed.");
     54                 getPassButton().setEnabled(true);
     55             } else {
     56                 showToast("Test failed. Key not accessible after auth");
     57             }
     58         }
     59     }
     60 
     61     @Override
     62     protected void showAuthenticationScreen() {
     63         mCancellationSignal = new CancellationSignal();
     64         mDialogCallback = new DialogCallback();
     65         mBiometricPrompt = new BiometricPrompt.Builder(getApplicationContext())
     66                 .setTitle("Authenticate with fingerprint")
     67                 .setNegativeButton("Cancel", mExecutor,
     68                         (DialogInterface dialogInterface, int which) -> {
     69                             if (which == DialogInterface.BUTTON_NEGATIVE) {
     70                                 mHandler.post(mNegativeButtonRunnable);
     71                             }
     72                         })
     73                 .build();
     74         mBiometricPrompt.authenticate(
     75                 new BiometricPrompt
     76                 .CryptoObject(getCipher()),
     77                 mCancellationSignal, mExecutor, mDialogCallback);
     78     }
     79 }
     80