blob: d8495d1638abfa67a1cb3bfedd2bf4597429d1a3 [file] [log] [blame]
Jeff Pudf81c962024-03-06 10:58:17 -05001/*
2 * Copyright (C) 2024 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 <android/binder_process.h>
18#include <fingerprint.sysprop.h>
19#include <gtest/gtest.h>
20
21#include <android-base/logging.h>
22
23#include "Fingerprint.h"
24#include "VirtualHal.h"
25
26using namespace ::android::fingerprint::virt;
27using namespace ::aidl::android::hardware::biometrics::fingerprint;
28
29namespace aidl::android::hardware::biometrics::fingerprint {
30
31class VirtualHalTest : public ::testing::Test {
32 public:
33 static const int32_t STATUS_FAILED_TO_SET_PARAMETER = 2;
34
35 protected:
36 void SetUp() override {
37 mHal = ndk::SharedRefBase::make<Fingerprint>();
38 mVhal = ndk::SharedRefBase::make<VirtualHal>(mHal.get());
39 ASSERT_TRUE(mVhal != nullptr);
40 mHal->resetConfigToDefault();
41 }
42
43 void TearDown() override { mHal->resetConfigToDefault(); }
44
45 std::shared_ptr<VirtualHal> mVhal;
46
47 ndk::ScopedAStatus validateNonNegativeInputOfInt32(const char* name,
48 ndk::ScopedAStatus (VirtualHal::*f)(int32_t),
49 const std::vector<int32_t>& in_good);
50
51 private:
52 std::shared_ptr<Fingerprint> mHal;
53};
54
55ndk::ScopedAStatus VirtualHalTest::validateNonNegativeInputOfInt32(
56 const char* name, ndk::ScopedAStatus (VirtualHal::*f)(int32_t),
57 const std::vector<int32_t>& in_params_good) {
58 ndk::ScopedAStatus status;
59 for (auto& param : in_params_good) {
60 status = (*mVhal.*f)(param);
61 if (!status.isOk()) return status;
62 if (Fingerprint::cfg().get<int32_t>(name) != param) {
63 return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage(
64 VirtualHalTest::STATUS_FAILED_TO_SET_PARAMETER,
65 "Error: fail to set non-negative parameter"));
66 }
67 }
68
69 int32_t old_param = Fingerprint::cfg().get<int32_t>(name);
70 status = (*mVhal.*f)(-1);
71 if (status.isOk()) {
72 return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage(
73 VirtualHalTest::STATUS_FAILED_TO_SET_PARAMETER, "Error: should return NOK"));
74 }
75 if (status.getServiceSpecificError() != IVirtualHal::STATUS_INVALID_PARAMETER) {
76 return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage(
77 VirtualHalTest::STATUS_FAILED_TO_SET_PARAMETER,
78 "Error: unexpected return error code"));
79 }
80 if (Fingerprint::cfg().get<int32_t>(name) != old_param) {
81 return ndk::ScopedAStatus(AStatus_fromServiceSpecificErrorWithMessage(
82 VirtualHalTest::STATUS_FAILED_TO_SET_PARAMETER,
83 "Error: unexpected parameter change on failed attempt"));
84 }
85 return ndk::ScopedAStatus::ok();
86}
87
88TEST_F(VirtualHalTest, init) {
89 mVhal->setLockout(false);
90 ASSERT_TRUE(Fingerprint::cfg().get<bool>("lockout") == false);
91 ASSERT_TRUE(Fingerprint::cfg().get<std::string>("type") == "rear");
92 ASSERT_TRUE(Fingerprint::cfg().get<std::int32_t>("sensor_strength") == 2);
93 std::int64_t id = Fingerprint::cfg().get<std::int64_t>("authenticator_id");
94 ASSERT_TRUE(Fingerprint::cfg().get<std::int64_t>("authenticator_id") == 0);
95 ASSERT_TRUE(Fingerprint::cfg().getopt<OptIntVec>("enrollments") == OptIntVec());
96}
97
98TEST_F(VirtualHalTest, enrollment_hit_int32) {
99 mVhal->setEnrollmentHit(11);
100 ASSERT_TRUE(Fingerprint::cfg().get<int32_t>("enrollment_hit") == 11);
101}
102
103TEST_F(VirtualHalTest, authenticator_id_int64) {
104 mVhal->setAuthenticatorId(12345678900);
105 ASSERT_TRUE(Fingerprint::cfg().get<int64_t>("authenticator_id") == 12345678900);
106}
107
108TEST_F(VirtualHalTest, opeationAuthenticateFails_bool) {
109 mVhal->setOperationAuthenticateFails(true);
110 ASSERT_TRUE(Fingerprint::cfg().get<bool>("operation_authenticate_fails"));
111}
112
113TEST_F(VirtualHalTest, operationAuthenticateAcquired_int32_vector) {
114 std::vector<int32_t> ac{1, 2, 3, 4, 5, 6, 7};
115 mVhal->setOperationAuthenticateAcquired(ac);
116 OptIntVec ac_get = Fingerprint::cfg().getopt<OptIntVec>("operation_authenticate_acquired");
117 ASSERT_TRUE(ac_get.size() == ac.size());
118 for (int i = 0; i < ac.size(); i++) {
119 ASSERT_TRUE(ac[i] == ac_get[i]);
120 }
121}
122
123TEST_F(VirtualHalTest, type) {
124 struct {
125 FingerprintSensorType type;
126 const char* typeStr;
127 } typeMap[] = {{FingerprintSensorType::REAR, "rear"},
128 {FingerprintSensorType::POWER_BUTTON, "side"},
129 {FingerprintSensorType::UNDER_DISPLAY_OPTICAL, "udfps"},
130 {FingerprintSensorType::UNDER_DISPLAY_ULTRASONIC, "udfps"},
131 {FingerprintSensorType::UNKNOWN, "unknown"}};
132 for (auto const& x : typeMap) {
133 mVhal->setType(x.type);
134 ASSERT_TRUE(Fingerprint::cfg().get<std::string>("type") == x.typeStr);
135 }
136}
137
138TEST_F(VirtualHalTest, sensorStrength) {
139 SensorStrength strengths[] = {SensorStrength::CONVENIENCE, SensorStrength::WEAK,
140 SensorStrength::STRONG};
141
142 for (auto const& strength : strengths) {
143 mVhal->setSensorStrength(strength);
144 ASSERT_TRUE(Fingerprint::cfg().get<int32_t>("sensor_strength") == (int32_t)(strength));
145 }
146}
147
148TEST_F(VirtualHalTest, sensorLocation) {
149 SensorLocation loc = {.sensorLocationX = 1, .sensorLocationY = 2, .sensorRadius = 3};
150 mVhal->setSensorLocation(loc);
151 ASSERT_TRUE(Fingerprint::cfg().get<std::string>("sensor_location") == "1:2:3");
152}
153
154TEST_F(VirtualHalTest, setLatency) {
155 ndk::ScopedAStatus status;
156 std::vector<int32_t> in_lats[] = {{1}, {2, 3}, {5, 4}};
157 for (auto const& in_lat : in_lats) {
158 status = mVhal->setOperationAuthenticateLatency(in_lat);
159 ASSERT_TRUE(status.isOk());
160 OptIntVec out_lat = Fingerprint::cfg().getopt<OptIntVec>("operation_authenticate_latency");
161 ASSERT_TRUE(in_lat.size() == out_lat.size());
162 for (int i = 0; i < in_lat.size(); i++) {
163 ASSERT_TRUE(in_lat[i] == out_lat[i]);
164 }
165 }
166
167 std::vector<int32_t> bad_in_lats[] = {{}, {1, 2, 3}, {1, -3}};
168 for (auto const& in_lat : bad_in_lats) {
169 status = mVhal->setOperationAuthenticateLatency(in_lat);
170 ASSERT_TRUE(!status.isOk());
171 ASSERT_TRUE(status.getServiceSpecificError() == IVirtualHal::STATUS_INVALID_PARAMETER);
172 }
173}
174
175TEST_F(VirtualHalTest, setOperationAuthenticateDuration) {
176 ndk::ScopedAStatus status = validateNonNegativeInputOfInt32(
177 "operation_authenticate_duration", &IVirtualHal::setOperationAuthenticateDuration,
178 {0, 33});
179 ASSERT_TRUE(status.isOk());
180}
181
182TEST_F(VirtualHalTest, setOperationDetectInteractionDuration) {
183 ndk::ScopedAStatus status = validateNonNegativeInputOfInt32(
184 "operation_detect_interaction_duration",
185 &IVirtualHal::setOperationDetectInteractionDuration, {0, 34});
186 ASSERT_TRUE(status.isOk());
187}
188
189TEST_F(VirtualHalTest, setLockoutTimedDuration) {
190 ndk::ScopedAStatus status = validateNonNegativeInputOfInt32(
191 "lockout_timed_duration", &IVirtualHal::setLockoutTimedDuration, {0, 35});
192 ASSERT_TRUE(status.isOk());
193}
194
195TEST_F(VirtualHalTest, setLockoutTimedThreshold) {
196 ndk::ScopedAStatus status = validateNonNegativeInputOfInt32(
197 "lockout_timed_threshold", &IVirtualHal::setLockoutTimedThreshold, {0, 36});
198 ASSERT_TRUE(status.isOk());
199}
200
201TEST_F(VirtualHalTest, setLockoutPermanentThreshold) {
202 ndk::ScopedAStatus status = validateNonNegativeInputOfInt32(
203 "lockout_permanent_threshold", &IVirtualHal::setLockoutPermanentThreshold, {0, 37});
204 ASSERT_TRUE(status.isOk());
205}
206
207TEST_F(VirtualHalTest, setOthers) {
208 // Verify that there is no CHECK() failures
209 mVhal->setEnrollments({7, 6, 5});
210 mVhal->setChallenge(111222333444555666);
211 mVhal->setOperationAuthenticateError(4);
212 mVhal->setOperationEnrollError(5);
213 mVhal->setOperationEnrollLatency({4, 5});
214 mVhal->setOperationDetectInteractionError(6);
215 mVhal->setOperationDetectInteractionAcquired({4, 3, 2});
216 mVhal->setLockout(false);
217 mVhal->setLockoutEnable(false);
218 mVhal->setSensorId(5);
219 mVhal->setMaxEnrollmentPerUser(6);
220 mVhal->setNavigationGuesture(false);
221 mVhal->setDetectInteraction(false);
222 mVhal->setDisplayTouch(false);
223 mVhal->setControlIllumination(false);
224}
225
226} // namespace aidl::android::hardware::biometrics::fingerprint
227
228int main(int argc, char** argv) {
229 testing::InitGoogleTest(&argc, argv);
230 ABinderProcess_startThreadPool();
231 return RUN_ALL_TESTS();
232}