blob: f5518992600ec204beff27bd2c7d897f26170103 [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"
26
27using namespace ::android::fingerprint::virt;
28using namespace ::aidl::android::hardware::biometrics::fingerprint;
29using namespace ::aidl::android::hardware::keymaster;
30
31namespace aidl::android::hardware::biometrics::fingerprint {
32
Jeff Pu52653182022-10-12 16:27:23 -040033class TestSessionCallback : public BnSessionCallback {
34 public:
35 ndk::ScopedAStatus onChallengeGenerated(int64_t /*challenge*/) override {
36 return ndk::ScopedAStatus::ok();
37 };
38 ::ndk::ScopedAStatus onChallengeRevoked(int64_t /*challenge*/) override {
39 return ndk::ScopedAStatus::ok();
40 };
41 ::ndk::ScopedAStatus onError(fingerprint::Error /*error*/, int32_t /*vendorCode*/) override {
42 return ndk::ScopedAStatus::ok();
43 };
44 ::ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
45 int32_t /*remaining*/) override {
46 mEnrollmentProgress++;
47 return ndk::ScopedAStatus::ok();
48 };
49
50 ::ndk::ScopedAStatus onAuthenticationSucceeded(int32_t /*enrollmentId*/,
51 const keymaster::HardwareAuthToken&) override {
52 mAuthenticationSuccess++;
53 return ndk::ScopedAStatus::ok();
54 };
55 ::ndk::ScopedAStatus onAuthenticationFailed() override {
56 mAuthenticationFailure++;
57 return ndk::ScopedAStatus::ok();
58 };
59 ::ndk::ScopedAStatus onInteractionDetected() override {
60 mDetectInteraction++;
61 return ndk::ScopedAStatus::ok();
62 };
63 ndk::ScopedAStatus onAcquired(AcquiredInfo /*info*/, int32_t /*vendorCode*/) override {
64 return ndk::ScopedAStatus::ok();
65 }
66 ::ndk::ScopedAStatus onEnrollmentsEnumerated(
67 const std::vector<int32_t>& /*enrollmentIds*/) override {
68 return ndk::ScopedAStatus::ok();
69 };
70 ::ndk::ScopedAStatus onEnrollmentsRemoved(
71 const std::vector<int32_t>& /*enrollmentIds*/) override {
72 return ndk::ScopedAStatus::ok();
73 };
74 ::ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
75 return ndk::ScopedAStatus::ok();
76 };
77 ::ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*authenticatorId*/) override {
78 return ndk::ScopedAStatus::ok();
79 };
80 ::ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); };
81 ndk::ScopedAStatus onLockoutTimed(int64_t /* timeout */) override {
82 return ndk::ScopedAStatus::ok();
83 }
84 ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); }
85 ndk::ScopedAStatus onSessionClosed() override { return ndk::ScopedAStatus::ok(); }
86
87 int32_t getAuthenticationCount() { return mAuthenticationSuccess + mAuthenticationFailure; }
88 int32_t getDetectInteractionCount() { return mDetectInteraction; }
89
90 int32_t mAuthenticationSuccess = 0;
91 int32_t mAuthenticationFailure = 0;
92 int32_t mEnrollmentProgress = 0;
93 int32_t mDetectInteraction = 0;
94};
95
Jeff Pu63f33c72022-07-28 16:06:23 -040096class FakeFingerprintEngineUdfpsTest : public ::testing::Test {
97 protected:
98 void SetUp() override {}
99
100 void TearDown() override {
101 // reset to default
102 FingerprintHalProperties::sensor_location("");
103 }
104
105 FakeFingerprintEngineUdfps mEngine;
106};
107
108bool isDefaultLocation(SensorLocation& sc) {
109 return (sc.sensorLocationX == FakeFingerprintEngineUdfps::defaultSensorLocationX &&
110 sc.sensorLocationY == FakeFingerprintEngineUdfps::defaultSensorLocationY &&
111 sc.sensorRadius == FakeFingerprintEngineUdfps::defaultSensorRadius && sc.display == "");
112}
113
Jeff Pu343ca942022-09-14 15:56:30 -0400114TEST_F(FakeFingerprintEngineUdfpsTest, getSensorLocationOk) {
Jeff Pu63f33c72022-07-28 16:06:23 -0400115 auto loc = "100:200:30";
116 FingerprintHalProperties::sensor_location(loc);
Jeff Pu343ca942022-09-14 15:56:30 -0400117 SensorLocation sc = mEngine.getSensorLocation();
Jeff Pu63f33c72022-07-28 16:06:23 -0400118 ASSERT_TRUE(sc.sensorLocationX == 100);
119 ASSERT_TRUE(sc.sensorLocationY == 200);
120 ASSERT_TRUE(sc.sensorRadius == 30);
121
122 loc = "100:200:30:screen1";
123 FingerprintHalProperties::sensor_location(loc);
124 sc = mEngine.getSensorLocation();
125 ASSERT_TRUE(sc.sensorLocationX == 100);
126 ASSERT_TRUE(sc.sensorLocationY == 200);
127 ASSERT_TRUE(sc.sensorRadius == 30);
128 ASSERT_TRUE(sc.display == "screen1");
Jeff Pu343ca942022-09-14 15:56:30 -0400129}
Jeff Pu63f33c72022-07-28 16:06:23 -0400130
Jeff Pu343ca942022-09-14 15:56:30 -0400131TEST_F(FakeFingerprintEngineUdfpsTest, getSensorLocationBad) {
Jeff Pu52653182022-10-12 16:27:23 -0400132 const std::vector<std::string> badStr{"", "100", "10:20", "10,20,5", "a:b:c"};
133 SensorLocation sc;
134 for (const auto& s : badStr) {
135 FingerprintHalProperties::sensor_location(s);
136 sc = mEngine.getSensorLocation();
137 ASSERT_TRUE(isDefaultLocation(sc));
138 }
Jeff Pu63f33c72022-07-28 16:06:23 -0400139}
140
Jeff Pu52653182022-10-12 16:27:23 -0400141TEST_F(FakeFingerprintEngineUdfpsTest, initialization) {
142 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kIdle);
143}
144
145TEST_F(FakeFingerprintEngineUdfpsTest, authenticate) {
146 std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
147 std::promise<void> cancel;
148 mEngine.authenticateImpl(cb.get(), 1, cancel.get_future());
149 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kAuthenticate);
150 mEngine.onPointerDownImpl(1, 2, 3, 4.0, 5.0);
151 ASSERT_EQ(cb->getAuthenticationCount(), 0);
152 mEngine.onUiReadyImpl();
153 ASSERT_EQ(cb->getAuthenticationCount(), 1);
154}
155
156TEST_F(FakeFingerprintEngineUdfpsTest, enroll) {
157 std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
158 std::promise<void> cancel;
159 keymaster::HardwareAuthToken hat{.mac = {5, 6}};
160 FingerprintHalProperties::next_enrollment("5:0,0:true");
161 mEngine.enrollImpl(cb.get(), hat, cancel.get_future());
162 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kEnroll);
163 mEngine.onPointerDownImpl(1, 2, 3, 4.0, 5.0);
164 ASSERT_EQ(cb->mEnrollmentProgress, 0);
165 mEngine.onUiReadyImpl();
166 ASSERT_TRUE(cb->mEnrollmentProgress > 0);
167}
168
169TEST_F(FakeFingerprintEngineUdfpsTest, detectInteraction) {
170 FingerprintHalProperties::detect_interaction(true);
171 FingerprintHalProperties::enrollments({1, 2});
172 FingerprintHalProperties::enrollment_hit(2);
173 FingerprintHalProperties::operation_detect_interaction_acquired("");
174 std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
175 std::promise<void> cancel;
176 mEngine.detectInteractionImpl(cb.get(), cancel.get_future());
177 ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kDetectInteract);
178 mEngine.onPointerDownImpl(1, 2, 3, 4.0, 5.0);
179 ASSERT_EQ(cb->getDetectInteractionCount(), 0);
180 mEngine.onUiReadyImpl();
181 ASSERT_EQ(cb->getDetectInteractionCount(), 1);
182}
Jeff Pu63f33c72022-07-28 16:06:23 -0400183// More
184} // namespace aidl::android::hardware::biometrics::fingerprint