• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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,getSensorLocationOk)115 TEST_F(FakeFingerprintEngineUdfpsTest, getSensorLocationOk) {
116     auto loc = "100:200:30";
117     Fingerprint::cfg().set<std::string>("sensor_location", loc);
118     SensorLocation sc = mEngine.getSensorLocation();
119     ASSERT_TRUE(sc.sensorLocationX == 100);
120     ASSERT_TRUE(sc.sensorLocationY == 200);
121     ASSERT_TRUE(sc.sensorRadius == 30);
122 
123     loc = "100:200:30:screen1";
124     Fingerprint::cfg().set<std::string>("sensor_location", loc);
125     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");
130 }
131 
TEST_F(FakeFingerprintEngineUdfpsTest,getSensorLocationBad)132 TEST_F(FakeFingerprintEngineUdfpsTest, getSensorLocationBad) {
133     const std::vector<std::string> badStr{"", "100", "10:20", "10,20,5", "a:b:c"};
134     SensorLocation sc;
135     for (const auto& s : badStr) {
136         Fingerprint::cfg().set<std::string>("sensor_location", s);
137         sc = mEngine.getSensorLocation();
138         ASSERT_TRUE(isDefaultLocation(sc));
139     }
140 }
141 
TEST_F(FakeFingerprintEngineUdfpsTest,initialization)142 TEST_F(FakeFingerprintEngineUdfpsTest, initialization) {
143     ASSERT_TRUE(mEngine.getWorkMode() == FakeFingerprintEngineUdfps::WorkMode::kIdle);
144 }
145 
TEST_F(FakeFingerprintEngineUdfpsTest,authenticate)146 TEST_F(FakeFingerprintEngineUdfpsTest, authenticate) {
147     std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
148     std::promise<void> cancel;
149     mEngine.notifyFingerdown();
150     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 
TEST_F(FakeFingerprintEngineUdfpsTest,enroll)158 TEST_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}};
162     Fingerprint::cfg().set<std::string>("next_enrollment", "5:0,0:true");
163     mEngine.notifyFingerdown();
164     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 
TEST_F(FakeFingerprintEngineUdfpsTest,detectInteraction)172 TEST_F(FakeFingerprintEngineUdfpsTest, detectInteraction) {
173     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", "");
177     std::shared_ptr<TestSessionCallback> cb = ndk::SharedRefBase::make<TestSessionCallback>();
178     std::promise<void> cancel;
179     mEngine.notifyFingerdown();
180     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 }
187 // More
188 }  // namespace aidl::android::hardware::biometrics::fingerprint
189