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 #include <aidl/android/hardware/biometrics/fingerprint/BnSessionCallback.h>
18 #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"
26 #include "Fingerprint.h"
27
28 using namespace ::android::fingerprint::virt;
29 using namespace ::aidl::android::hardware::biometrics::fingerprint;
30 using namespace ::aidl::android::hardware::keymaster;
31
32 namespace aidl::android::hardware::biometrics::fingerprint {
33
34 class TestSessionCallback : public BnSessionCallback {
35 public:
onChallengeGenerated(int64_t)36 ndk::ScopedAStatus onChallengeGenerated(int64_t /*challenge*/) override {
37 return ndk::ScopedAStatus::ok();
38 };
onChallengeRevoked(int64_t)39 ::ndk::ScopedAStatus onChallengeRevoked(int64_t /*challenge*/) override {
40 return ndk::ScopedAStatus::ok();
41 };
onError(fingerprint::Error,int32_t)42 ::ndk::ScopedAStatus onError(fingerprint::Error /*error*/, int32_t /*vendorCode*/) override {
43 return ndk::ScopedAStatus::ok();
44 };
onEnrollmentProgress(int32_t,int32_t)45 ::ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
46 int32_t /*remaining*/) override {
47 mEnrollmentProgress++;
48 return ndk::ScopedAStatus::ok();
49 };
50
onAuthenticationSucceeded(int32_t,const keymaster::HardwareAuthToken &)51 ::ndk::ScopedAStatus onAuthenticationSucceeded(int32_t /*enrollmentId*/,
52 const keymaster::HardwareAuthToken&) override {
53 mAuthenticationSuccess++;
54 return ndk::ScopedAStatus::ok();
55 };
onAuthenticationFailed()56 ::ndk::ScopedAStatus onAuthenticationFailed() override {
57 mAuthenticationFailure++;
58 return ndk::ScopedAStatus::ok();
59 };
onInteractionDetected()60 ::ndk::ScopedAStatus onInteractionDetected() override {
61 mDetectInteraction++;
62 return ndk::ScopedAStatus::ok();
63 };
onAcquired(AcquiredInfo,int32_t)64 ndk::ScopedAStatus onAcquired(AcquiredInfo /*info*/, int32_t /*vendorCode*/) override {
65 return ndk::ScopedAStatus::ok();
66 }
onEnrollmentsEnumerated(const std::vector<int32_t> &)67 ::ndk::ScopedAStatus onEnrollmentsEnumerated(
68 const std::vector<int32_t>& /*enrollmentIds*/) override {
69 return ndk::ScopedAStatus::ok();
70 };
onEnrollmentsRemoved(const std::vector<int32_t> &)71 ::ndk::ScopedAStatus onEnrollmentsRemoved(
72 const std::vector<int32_t>& /*enrollmentIds*/) override {
73 return ndk::ScopedAStatus::ok();
74 };
onAuthenticatorIdRetrieved(int64_t)75 ::ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
76 return ndk::ScopedAStatus::ok();
77 };
onAuthenticatorIdInvalidated(int64_t)78 ::ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*authenticatorId*/) override {
79 return ndk::ScopedAStatus::ok();
80 };
onLockoutPermanent()81 ::ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); };
onLockoutTimed(int64_t)82 ndk::ScopedAStatus onLockoutTimed(int64_t /* timeout */) override {
83 return ndk::ScopedAStatus::ok();
84 }
onLockoutCleared()85 ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); }
onSessionClosed()86 ndk::ScopedAStatus onSessionClosed() override { return ndk::ScopedAStatus::ok(); }
87
getAuthenticationCount()88 int32_t getAuthenticationCount() { return mAuthenticationSuccess + mAuthenticationFailure; }
getDetectInteractionCount()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
97 class FakeFingerprintEngineUdfpsTest : public ::testing::Test {
98 protected:
SetUp()99 void SetUp() override {}
100
TearDown()101 void TearDown() override {
102 // reset to default
103 Fingerprint::cfg().set<std::string>("sensor_location", "");
104 }
105
106 FakeFingerprintEngineUdfps mEngine;
107 };
108
isDefaultLocation(SensorLocation & sc)109 bool isDefaultLocation(SensorLocation& sc) {
110 return (sc.sensorLocationX == FakeFingerprintEngineUdfps::defaultSensorLocationX &&
111 sc.sensorLocationY == FakeFingerprintEngineUdfps::defaultSensorLocationY &&
112 sc.sensorRadius == FakeFingerprintEngineUdfps::defaultSensorRadius && sc.display == "");
113 }
114
TEST_F(FakeFingerprintEngineUdfpsTest,getSensorLocationOkSimple)115 TEST_F(FakeFingerprintEngineUdfpsTest, getSensorLocationOkSimple) {
116 auto loc = "100:200:30";
117 Fingerprint::cfg().set<std::string>("sensor_location", loc);
118 std::vector<SensorLocation> sc;
119 mEngine.getSensorLocation(sc);
120 ASSERT_TRUE(sc[0].sensorLocationX == 100);
121 ASSERT_TRUE(sc[0].sensorLocationY == 200);
122 ASSERT_TRUE(sc[0].sensorRadius == 30);
123 }
124
TEST_F(FakeFingerprintEngineUdfpsTest,getSensorLocationOkWithOneDisplay)125 TEST_F(FakeFingerprintEngineUdfpsTest, getSensorLocationOkWithOneDisplay) {
126 auto loc = "100:200:30:screen1";
127 Fingerprint::cfg().set<std::string>("sensor_location", loc);
128 std::vector<SensorLocation> sc;
129 mEngine.getSensorLocation(sc);
130 ASSERT_TRUE(sc[0].sensorLocationX == 100);
131 ASSERT_TRUE(sc[0].sensorLocationY == 200);
132 ASSERT_TRUE(sc[0].sensorRadius == 30);
133 ASSERT_TRUE(sc[0].display == "screen1");
134 }
135
TEST_F(FakeFingerprintEngineUdfpsTest,getSensorLocationOkWithMultipleDisplays)136 TEST_F(FakeFingerprintEngineUdfpsTest, getSensorLocationOkWithMultipleDisplays) {
137 auto loc = "100:200:30:screen1,200:400:60:screen2";
138 Fingerprint::cfg().set<std::string>("sensor_location", loc);
139 std::vector<SensorLocation> sc;
140 mEngine.getSensorLocation(sc);
141 ASSERT_TRUE(sc.size() == 2);
142 ASSERT_TRUE(sc[0].sensorLocationX == 100);
143 ASSERT_TRUE(sc[0].sensorLocationY == 200);
144 ASSERT_TRUE(sc[0].sensorRadius == 30);
145 ASSERT_TRUE(sc[0].display == "screen1");
146 ASSERT_TRUE(sc[1].sensorLocationX == 200);
147 ASSERT_TRUE(sc[1].sensorLocationY == 400);
148 ASSERT_TRUE(sc[1].sensorRadius == 60);
149 ASSERT_TRUE(sc[1].display == "screen2");
150 }
151
TEST_F(FakeFingerprintEngineUdfpsTest,getSensorLocationBad)152 TEST_F(FakeFingerprintEngineUdfpsTest, getSensorLocationBad) {
153 const std::vector<std::string> badStr{
154 "", "100", "10:20", "10,20,5", "a:b:c", "10:20:30:d1,40", "10:20:30:d1,40:50"};
155 std::vector<SensorLocation> sc;
156 for (const auto& s : badStr) {
157 Fingerprint::cfg().set<std::string>("sensor_location", s);
158 sc.clear();
159 mEngine.getSensorLocation(sc);
160 ASSERT_TRUE(isDefaultLocation(sc[0]));
161 }
162 }
163
TEST_F(FakeFingerprintEngineUdfpsTest,initialization)164 TEST_F(FakeFingerprintEngineUdfpsTest, initialization) {
165 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kIdle);
166 }
167
TEST_F(FakeFingerprintEngineUdfpsTest,authenticate)168 TEST_F(FakeFingerprintEngineUdfpsTest, authenticate) {
169 std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
170 std::promise<void> cancel;
171 mEngine.notifyFingerdown();
172 mEngine.authenticateImpl(cb.get(), 1, cancel.get_future());
173 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kAuthenticate);
174 mEngine.onPointerDownImpl(1, 2, 3, 4.0, 5.0);
175 ASSERT_EQ(cb->getAuthenticationCount(), 0);
176 mEngine.onUiReadyImpl();
177 ASSERT_EQ(cb->getAuthenticationCount(), 1);
178 }
179
TEST_F(FakeFingerprintEngineUdfpsTest,enroll)180 TEST_F(FakeFingerprintEngineUdfpsTest, enroll) {
181 std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
182 std::promise<void> cancel;
183 keymaster::HardwareAuthToken hat{.mac = {5, 6}};
184 Fingerprint::cfg().set<std::string>("next_enrollment", "5:0,0:true");
185 mEngine.notifyFingerdown();
186 mEngine.enrollImpl(cb.get(), hat, cancel.get_future());
187 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kEnroll);
188 mEngine.onPointerDownImpl(1, 2, 3, 4.0, 5.0);
189 ASSERT_EQ(cb->mEnrollmentProgress, 0);
190 mEngine.onUiReadyImpl();
191 ASSERT_TRUE(cb->mEnrollmentProgress > 0);
192 }
193
TEST_F(FakeFingerprintEngineUdfpsTest,detectInteraction)194 TEST_F(FakeFingerprintEngineUdfpsTest, detectInteraction) {
195 Fingerprint::cfg().set<bool>("detect_interaction", true);
196 Fingerprint::cfg().setopt<OptIntVec>("enrollments", {1, 2});
197 Fingerprint::cfg().set<std::int32_t>("enrollment_hit", 2);
198 Fingerprint::cfg().set<std::string>("operation_detect_interaction_acquired", "");
199 std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
200 std::promise<void> cancel;
201 mEngine.notifyFingerdown();
202 mEngine.detectInteractionImpl(cb.get(), cancel.get_future());
203 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kDetectInteract);
204 mEngine.onPointerDownImpl(1, 2, 3, 4.0, 5.0);
205 ASSERT_EQ(cb->getDetectInteractionCount(), 0);
206 mEngine.onUiReadyImpl();
207 ASSERT_EQ(cb->getDetectInteractionCount(), 1);
208 }
209 // More
210 } // namespace aidl::android::hardware::biometrics::fingerprint
211