1 /* 2 * Copyright (C) 2022 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 #pragma once 18 19 #include <aidl/android/hardware/biometrics/common/SensorStrength.h> 20 #include <aidl/android/hardware/biometrics/face/BnSession.h> 21 #include <aidl/android/hardware/biometrics/face/FaceSensorType.h> 22 #include <aidl/android/hardware/biometrics/face/ISessionCallback.h> 23 24 #include <future> 25 #include <random> 26 #include <vector> 27 28 #include "FakeLockoutTracker.h" 29 30 namespace aidl::android::hardware::biometrics::face { 31 32 namespace face = aidl::android::hardware::biometrics::face; 33 namespace common = aidl::android::hardware::biometrics::common; 34 namespace keymaster = aidl::android::hardware::keymaster; 35 36 using aidl::android::hardware::common::NativeHandle; 37 // A fake engine that is backed by system properties instead of hardware. 38 class FakeFaceEngine { 39 public: FakeFaceEngine()40 FakeFaceEngine() : mRandom(std::mt19937::default_seed) {} ~FakeFaceEngine()41 virtual ~FakeFaceEngine() {} 42 43 static face::FaceSensorType GetSensorType(); 44 static common::SensorStrength GetSensorStrength(); 45 void generateChallengeImpl(ISessionCallback* cb); 46 void revokeChallengeImpl(ISessionCallback* cb, int64_t challenge); 47 void getEnrollmentConfigImpl(ISessionCallback* cb, 48 std::vector<EnrollmentStageConfig>* return_val); 49 void enrollImpl(ISessionCallback* cb, const keymaster::HardwareAuthToken& hat, 50 EnrollmentType enrollmentType, const std::vector<Feature>& features, 51 const std::future<void>& cancel); 52 void authenticateImpl(ISessionCallback* cb, int64_t operationId, 53 const std::future<void>& cancel); 54 void detectInteractionImpl(ISessionCallback* cb, const std::future<void>& cancel); 55 void enumerateEnrollmentsImpl(ISessionCallback* cb); 56 void removeEnrollmentsImpl(ISessionCallback* cb, const std::vector<int32_t>& enrollmentIds); 57 void getFeaturesImpl(ISessionCallback* cb); 58 void setFeatureImpl(ISessionCallback* cb, const keymaster::HardwareAuthToken& hat, 59 Feature feature, bool enabled); 60 void getAuthenticatorIdImpl(ISessionCallback* cb); 61 void invalidateAuthenticatorIdImpl(ISessionCallback* cb); 62 void resetLockoutImpl(ISessionCallback* cb, const keymaster::HardwareAuthToken& /*hat*/); 63 int32_t getLatency(const std::vector<std::optional<std::int32_t>>& latencyVec); 64 toString()65 virtual std::string toString() const { 66 std::ostringstream os; 67 os << "----- FakeFaceEngine:: -----" << std::endl; 68 os << mLockoutTracker.toString(); 69 return os.str(); 70 } 71 72 std::mt19937 mRandom; 73 74 private: 75 int32_t getRandomInRange(int32_t bound1, int32_t bound2); 76 static constexpr int32_t FACE_ACQUIRED_VENDOR_BASE = 1000; 77 static constexpr int32_t FACE_ERROR_VENDOR_BASE = 1000; 78 std::pair<AcquiredInfo, int32_t> convertAcquiredInfo(int32_t code); 79 std::pair<Error, int32_t> convertError(int32_t code); 80 FakeLockoutTracker mLockoutTracker; 81 }; 82 83 } // namespace aidl::android::hardware::biometrics::face 84