blob: eb45f987b0314f365c5c13973064c2e351064c1a [file] [log] [blame]
Jeff Pu63f33c72022-07-28 16:06:23 -04001/*
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
Jeff Pu52653182022-10-12 16:27:23 -040017#include <aidl/android/hardware/biometrics/fingerprint/BnSessionCallback.h>
Jeff Pu63f33c72022-07-28 16:06:23 -040018#include <android/binder_process.h>
19#include <fingerprint.sysprop.h>
20#include <gtest/gtest.h>
21
22#include <android-base/logging.h>
23
24#include "FakeFingerprintEngine.h"
25#include "FakeFingerprintEngineUdfps.h"
Jeff Pu6ccd9562024-02-21 10:46:35 -050026#include "Fingerprint.h"
Jeff Pu63f33c72022-07-28 16:06:23 -040027
28using namespace ::android::fingerprint::virt;
29using namespace ::aidl::android::hardware::biometrics::fingerprint;
30using namespace ::aidl::android::hardware::keymaster;
31
32namespace aidl::android::hardware::biometrics::fingerprint {
33
Jeff Pu52653182022-10-12 16:27:23 -040034class TestSessionCallback : public BnSessionCallback {
35 public:
36 ndk::ScopedAStatus onChallengeGenerated(int64_t /*challenge*/) override {
37 return ndk::ScopedAStatus::ok();
38 };
39 ::ndk::ScopedAStatus onChallengeRevoked(int64_t /*challenge*/) override {
40 return ndk::ScopedAStatus::ok();
41 };
42 ::ndk::ScopedAStatus onError(fingerprint::Error /*error*/, int32_t /*vendorCode*/) override {
43 return ndk::ScopedAStatus::ok();
44 };
45 ::ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
46 int32_t /*remaining*/) override {
47 mEnrollmentProgress++;
48 return ndk::ScopedAStatus::ok();
49 };
50
51 ::ndk::ScopedAStatus onAuthenticationSucceeded(int32_t /*enrollmentId*/,
52 const keymaster::HardwareAuthToken&) override {
53 mAuthenticationSuccess++;
54 return ndk::ScopedAStatus::ok();
55 };
56 ::ndk::ScopedAStatus onAuthenticationFailed() override {
57 mAuthenticationFailure++;
58 return ndk::ScopedAStatus::ok();
59 };
60 ::ndk::ScopedAStatus onInteractionDetected() override {
61 mDetectInteraction++;
62 return ndk::ScopedAStatus::ok();
63 };
64 ndk::ScopedAStatus onAcquired(AcquiredInfo /*info*/, int32_t /*vendorCode*/) override {
65 return ndk::ScopedAStatus::ok();
66 }
67 ::ndk::ScopedAStatus onEnrollmentsEnumerated(
68 const std::vector<int32_t>& /*enrollmentIds*/) override {
69 return ndk::ScopedAStatus::ok();
70 };
71 ::ndk::ScopedAStatus onEnrollmentsRemoved(
72 const std::vector<int32_t>& /*enrollmentIds*/) override {
73 return ndk::ScopedAStatus::ok();
74 };
75 ::ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
76 return ndk::ScopedAStatus::ok();
77 };
78 ::ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*authenticatorId*/) override {
79 return ndk::ScopedAStatus::ok();
80 };
81 ::ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); };
82 ndk::ScopedAStatus onLockoutTimed(int64_t /* timeout */) override {
83 return ndk::ScopedAStatus::ok();
84 }
85 ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); }
86 ndk::ScopedAStatus onSessionClosed() override { return ndk::ScopedAStatus::ok(); }
87
88 int32_t getAuthenticationCount() { return mAuthenticationSuccess + mAuthenticationFailure; }
89 int32_t getDetectInteractionCount() { return mDetectInteraction; }
90
91 int32_t mAuthenticationSuccess = 0;
92 int32_t mAuthenticationFailure = 0;
93 int32_t mEnrollmentProgress = 0;
94 int32_t mDetectInteraction = 0;
95};
96
Jeff Pu63f33c72022-07-28 16:06:23 -040097class FakeFingerprintEngineUdfpsTest : public ::testing::Test {
98 protected:
99 void SetUp() override {}
100
101 void TearDown() override {
102 // reset to default
Jeff Pu6ccd9562024-02-21 10:46:35 -0500103 Fingerprint::cfg().set<std::string>("sensor_location", "");
Jeff Pu63f33c72022-07-28 16:06:23 -0400104 }
105
106 FakeFingerprintEngineUdfps mEngine;
107};
108
109bool isDefaultLocation(SensorLocation& sc) {
110 return (sc.sensorLocationX == FakeFingerprintEngineUdfps::defaultSensorLocationX &&
111 sc.sensorLocationY == FakeFingerprintEngineUdfps::defaultSensorLocationY &&
112 sc.sensorRadius == FakeFingerprintEngineUdfps::defaultSensorRadius && sc.display == "");
113}
114
Jeff Pu343ca942022-09-14 15:56:30 -0400115TEST_F(FakeFingerprintEngineUdfpsTest, getSensorLocationOk) {
Jeff Pu63f33c72022-07-28 16:06:23 -0400116 auto loc = "100:200:30";
Jeff Pu6ccd9562024-02-21 10:46:35 -0500117 Fingerprint::cfg().set<std::string>("sensor_location", loc);
Jeff Pu343ca942022-09-14 15:56:30 -0400118 SensorLocation sc = mEngine.getSensorLocation();
Jeff Pu63f33c72022-07-28 16:06:23 -0400119 ASSERT_TRUE(sc.sensorLocationX == 100);
120 ASSERT_TRUE(sc.sensorLocationY == 200);
121 ASSERT_TRUE(sc.sensorRadius == 30);
122
123 loc = "100:200:30:screen1";
Jeff Pu6ccd9562024-02-21 10:46:35 -0500124 Fingerprint::cfg().set<std::string>("sensor_location", loc);
Jeff Pu63f33c72022-07-28 16:06:23 -0400125 sc = mEngine.getSensorLocation();
126 ASSERT_TRUE(sc.sensorLocationX == 100);
127 ASSERT_TRUE(sc.sensorLocationY == 200);
128 ASSERT_TRUE(sc.sensorRadius == 30);
129 ASSERT_TRUE(sc.display == "screen1");
Jeff Pu343ca942022-09-14 15:56:30 -0400130}
Jeff Pu63f33c72022-07-28 16:06:23 -0400131
Jeff Pu343ca942022-09-14 15:56:30 -0400132TEST_F(FakeFingerprintEngineUdfpsTest, getSensorLocationBad) {
Jeff Pu52653182022-10-12 16:27:23 -0400133 const std::vector<std::string> badStr{"", "100", "10:20", "10,20,5", "a:b:c"};
134 SensorLocation sc;
135 for (const auto& s : badStr) {
Jeff Pu6ccd9562024-02-21 10:46:35 -0500136 Fingerprint::cfg().set<std::string>("sensor_location", s);
Jeff Pu52653182022-10-12 16:27:23 -0400137 sc = mEngine.getSensorLocation();
138 ASSERT_TRUE(isDefaultLocation(sc));
139 }
Jeff Pu63f33c72022-07-28 16:06:23 -0400140}
141
Jeff Pu52653182022-10-12 16:27:23 -0400142TEST_F(FakeFingerprintEngineUdfpsTest, initialization) {
143 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kIdle);
144}
145
146TEST_F(FakeFingerprintEngineUdfpsTest, authenticate) {
147 std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
148 std::promise<void> cancel;
Jeff Pu74e25d22024-01-08 22:21:20 +0000149 mEngine.notifyFingerdown();
Jeff Pu52653182022-10-12 16:27:23 -0400150 mEngine.authenticateImpl(cb.get(), 1, cancel.get_future());
151 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kAuthenticate);
152 mEngine.onPointerDownImpl(1, 2, 3, 4.0, 5.0);
153 ASSERT_EQ(cb->getAuthenticationCount(), 0);
154 mEngine.onUiReadyImpl();
155 ASSERT_EQ(cb->getAuthenticationCount(), 1);
156}
157
158TEST_F(FakeFingerprintEngineUdfpsTest, enroll) {
159 std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
160 std::promise<void> cancel;
161 keymaster::HardwareAuthToken hat{.mac = {5, 6}};
Jeff Pu6ccd9562024-02-21 10:46:35 -0500162 Fingerprint::cfg().set<std::string>("next_enrollment", "5:0,0:true");
Jeff Pu74e25d22024-01-08 22:21:20 +0000163 mEngine.notifyFingerdown();
Jeff Pu52653182022-10-12 16:27:23 -0400164 mEngine.enrollImpl(cb.get(), hat, cancel.get_future());
165 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kEnroll);
166 mEngine.onPointerDownImpl(1, 2, 3, 4.0, 5.0);
167 ASSERT_EQ(cb->mEnrollmentProgress, 0);
168 mEngine.onUiReadyImpl();
169 ASSERT_TRUE(cb->mEnrollmentProgress > 0);
170}
171
172TEST_F(FakeFingerprintEngineUdfpsTest, detectInteraction) {
Jeff Pu6ccd9562024-02-21 10:46:35 -0500173 Fingerprint::cfg().set<bool>("detect_interaction", true);
174 Fingerprint::cfg().setopt<OptIntVec>("enrollments", {1, 2});
175 Fingerprint::cfg().set<std::int32_t>("enrollment_hit", 2);
176 Fingerprint::cfg().set<std::string>("operation_detect_interaction_acquired", "");
Jeff Pu52653182022-10-12 16:27:23 -0400177 std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
178 std::promise<void> cancel;
Jeff Pu74e25d22024-01-08 22:21:20 +0000179 mEngine.notifyFingerdown();
Jeff Pu52653182022-10-12 16:27:23 -0400180 mEngine.detectInteractionImpl(cb.get(), cancel.get_future());
181 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kDetectInteract);
182 mEngine.onPointerDownImpl(1, 2, 3, 4.0, 5.0);
183 ASSERT_EQ(cb->getDetectInteractionCount(), 0);
184 mEngine.onUiReadyImpl();
185 ASSERT_EQ(cb->getDetectInteractionCount(), 1);
186}
Jeff Pu63f33c72022-07-28 16:06:23 -0400187// More
188} // namespace aidl::android::hardware::biometrics::fingerprint