blob: 08ab5d694b4b406fc9ab4a50f5195fd772e6b023 [file] [log] [blame]
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -07001/*
2 * Copyright (C) 2020 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#include <aidl/Gtest.h>
17#include <aidl/Vintf.h>
18#include <aidl/android/hardware/biometrics/face/BnFace.h>
19#include <aidl/android/hardware/biometrics/face/BnSessionCallback.h>
20
21#include <android/binder_manager.h>
22#include <android/binder_process.h>
23
Ilya Matyukhincc2b6942021-03-23 23:08:41 -070024#include <chrono>
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070025#include <future>
26
27namespace aidl::android::hardware::biometrics::face {
28namespace {
29
Ilya Matyukhincc2b6942021-03-23 23:08:41 -070030using namespace std::literals::chrono_literals;
31
Ilya Matyukhin046020b2021-07-03 00:05:47 +000032using aidl::android::hardware::common::NativeHandle;
33
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070034constexpr int kSensorId = 0;
35constexpr int kUserId = 0;
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070036
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070037class SessionCallback : public BnSessionCallback {
38 public:
Ilya Matyukhin046020b2021-07-03 00:05:47 +000039 ndk::ScopedAStatus onChallengeGenerated(int64_t challenge) override {
40 auto lock = std::lock_guard{mMutex};
41 mOnChallengeGeneratedInvoked = true;
42 mGeneratedChallenge = challenge;
43 mCv.notify_one();
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070044 return ndk::ScopedAStatus::ok();
45 }
46
Ilya Matyukhin046020b2021-07-03 00:05:47 +000047 ndk::ScopedAStatus onChallengeRevoked(int64_t challenge) override {
48 auto lock = std::lock_guard{mMutex};
49 mOnChallengeRevokedInvoked = true;
50 mRevokedChallenge = challenge;
51 mCv.notify_one();
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070052 return ndk::ScopedAStatus::ok();
53 }
54
Ilya Matyukhine13bc812021-01-19 16:04:04 -080055 ndk::ScopedAStatus onAuthenticationFrame(const AuthenticationFrame& /*frame*/) override {
56 return ndk::ScopedAStatus::ok();
57 }
58
59 ndk::ScopedAStatus onEnrollmentFrame(const EnrollmentFrame& /*frame*/) override {
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070060 return ndk::ScopedAStatus::ok();
61 }
62
Ilya Matyukhin61a6d8d2021-07-21 13:22:45 -070063 ndk::ScopedAStatus onError(Error error, int32_t vendorCode) override {
Ilya Matyukhin13d84062021-06-07 16:56:11 -070064 auto lock = std::lock_guard<std::mutex>{mMutex};
65 mError = error;
Ilya Matyukhin61a6d8d2021-07-21 13:22:45 -070066 mVendorCode = vendorCode;
Ilya Matyukhin13d84062021-06-07 16:56:11 -070067 mOnErrorInvoked = true;
68 mCv.notify_one();
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070069 return ndk::ScopedAStatus::ok();
70 }
71
72 ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
73 int32_t /*remaining*/) override {
74 return ndk::ScopedAStatus::ok();
75 }
76
77 ndk::ScopedAStatus onAuthenticationSucceeded(
78 int32_t /*enrollmentId*/, const keymaster::HardwareAuthToken& /*hat*/) override {
79 return ndk::ScopedAStatus::ok();
80 }
81
82 ndk::ScopedAStatus onAuthenticationFailed() override { return ndk::ScopedAStatus::ok(); }
83
84 ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override {
85 return ndk::ScopedAStatus::ok();
86 }
87
88 ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); }
89
90 ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); }
91
92 ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); }
93
94 ndk::ScopedAStatus onEnrollmentsEnumerated(
95 const std::vector<int32_t>& /*enrollmentIds*/) override {
Ilya Matyukhin046020b2021-07-03 00:05:47 +000096 auto lock = std::lock_guard{mMutex};
97 mOnEnrollmentsEnumeratedInvoked = true;
98 mCv.notify_one();
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070099 return ndk::ScopedAStatus::ok();
100 }
101
102 ndk::ScopedAStatus onEnrollmentsRemoved(
103 const std::vector<int32_t>& /*enrollmentIds*/) override {
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000104 auto lock = std::lock_guard{mMutex};
105 mOnEnrollmentsRemovedInvoked = true;
106 mCv.notify_one();
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700107 return ndk::ScopedAStatus::ok();
108 }
109
Ilya Matyukhin9fcf6b12021-04-14 13:43:06 -0700110 ndk::ScopedAStatus onFeaturesRetrieved(const std::vector<Feature>& /*features*/) override {
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000111 auto lock = std::lock_guard{mMutex};
112 mOnFeaturesRetrievedInvoked = true;
113 mCv.notify_one();
Ilya Matyukhinf2d38862021-01-22 11:48:59 -0800114 return ndk::ScopedAStatus::ok();
115 }
116
Ilya Matyukhin9fcf6b12021-04-14 13:43:06 -0700117 ndk::ScopedAStatus onFeatureSet(Feature /*feature*/) override {
Ilya Matyukhinf2d38862021-01-22 11:48:59 -0800118 return ndk::ScopedAStatus::ok();
119 }
120
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700121 ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000122 auto lock = std::lock_guard{mMutex};
123 mOnAuthenticatorIdRetrievedInvoked = true;
124 mCv.notify_one();
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700125 return ndk::ScopedAStatus::ok();
126 }
127
Kevin Chynf7890cc2021-01-11 17:08:36 -0800128 ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*newAuthenticatorId*/) override {
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000129 auto lock = std::lock_guard{mMutex};
130 mOnAuthenticatorIdInvalidatedInvoked = true;
131 mCv.notify_one();
Kevin Chynf7890cc2021-01-11 17:08:36 -0800132 return ndk::ScopedAStatus::ok();
133 }
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700134
Ilya Matyukhincc2b6942021-03-23 23:08:41 -0700135 ndk::ScopedAStatus onSessionClosed() override {
Ilya Matyukhin13d84062021-06-07 16:56:11 -0700136 auto lock = std::lock_guard<std::mutex>{mMutex};
137 mOnSessionClosedInvoked = true;
138 mCv.notify_one();
Ilya Matyukhincc2b6942021-03-23 23:08:41 -0700139 return ndk::ScopedAStatus::ok();
140 }
Ilya Matyukhincbbfa932021-03-22 13:25:15 -0700141
Ilya Matyukhin13d84062021-06-07 16:56:11 -0700142 std::mutex mMutex;
143 std::condition_variable mCv;
144 Error mError = Error::UNKNOWN;
Ilya Matyukhin61a6d8d2021-07-21 13:22:45 -0700145 int32_t mVendorCode = 0;
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000146 int64_t mGeneratedChallenge = 0;
147 int64_t mRevokedChallenge = 0;
148 bool mOnChallengeGeneratedInvoked = false;
149 bool mOnChallengeRevokedInvoked = false;
Ilya Matyukhin13d84062021-06-07 16:56:11 -0700150 bool mOnErrorInvoked = false;
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000151 bool mOnEnrollmentsEnumeratedInvoked = false;
152 bool mOnEnrollmentsRemovedInvoked = false;
153 bool mOnFeaturesRetrievedInvoked = false;
154 bool mOnAuthenticatorIdRetrievedInvoked = false;
155 bool mOnAuthenticatorIdInvalidatedInvoked = false;
Ilya Matyukhin13d84062021-06-07 16:56:11 -0700156 bool mOnSessionClosedInvoked = false;
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700157};
158
159class Face : public testing::TestWithParam<std::string> {
160 protected:
161 void SetUp() override {
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000162 // Prepare the callback.
163 mCb = ndk::SharedRefBase::make<SessionCallback>();
164
165 int retries = 0;
166 bool isOk = false;
167 // If the first attempt to create a session fails, we try to create a session again. The
168 // first attempt might fail if the framework already has an active session. The AIDL
169 // contract doesn't allow to create a new session without closing the old one. However, we
170 // can't close the framework's session from VTS. The expectation here is that the HAL will
171 // crash after the first illegal attempt to create a session, then it will restart, and then
172 // we'll be able to create a session.
173 do {
174 // Get an instance of the HAL.
175 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
176 ASSERT_NE(binder, nullptr);
177 mHal = IFace::fromBinder(ndk::SpAIBinder(binder));
178
179 // Create a session.
180 isOk = mHal->createSession(kSensorId, kUserId, mCb, &mSession).isOk();
181 ++retries;
182 } while (!isOk && retries < 2);
183
184 ASSERT_TRUE(isOk);
185 }
186
187 void TearDown() override {
188 // Close the mSession.
189 ASSERT_TRUE(mSession->close().isOk());
190
191 // Make sure the mSession is closed.
192 auto lock = std::unique_lock<std::mutex>(mCb->mMutex);
193 mCb->mCv.wait(lock, [this] { return mCb->mOnSessionClosedInvoked; });
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700194 }
195
Ilya Matyukhincc2b6942021-03-23 23:08:41 -0700196 std::shared_ptr<IFace> mHal;
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000197 std::shared_ptr<SessionCallback> mCb;
198 std::shared_ptr<ISession> mSession;
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700199};
200
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000201TEST_P(Face, GetSensorPropsWorksTest) {
202 std::vector<SensorProps> sensorProps;
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700203
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000204 // Call the method.
205 ASSERT_TRUE(mHal->getSensorProps(&sensorProps).isOk());
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700206
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000207 // Make sure the sensorProps aren't empty.
208 ASSERT_FALSE(sensorProps.empty());
209 ASSERT_FALSE(sensorProps[0].commonProps.componentInfo.empty());
210}
211
212TEST_P(Face, EnrollWithBadHatResultsInErrorTest) {
213 // Call the method.
214 auto hat = keymaster::HardwareAuthToken{};
Ilya Matyukhincc2b6942021-03-23 23:08:41 -0700215 std::shared_ptr<common::ICancellationSignal> cancellationSignal;
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000216 ASSERT_TRUE(
Ilya Matyukhin88796f42021-07-21 22:54:40 -0700217 mSession->enroll(hat, EnrollmentType::DEFAULT, {}, std::nullopt, &cancellationSignal)
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000218 .isOk());
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700219
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000220 // Make sure an error is returned.
221 auto lock = std::unique_lock{mCb->mMutex};
222 mCb->mCv.wait(lock, [this] { return mCb->mOnErrorInvoked; });
Ilya Matyukhin61a6d8d2021-07-21 13:22:45 -0700223 EXPECT_EQ(mCb->mError, Error::UNABLE_TO_PROCESS);
224 EXPECT_EQ(mCb->mVendorCode, 0);
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000225}
226
227TEST_P(Face, GenerateChallengeProducesUniqueChallengesTest) {
228 static constexpr int kIterations = 100;
229
230 auto challenges = std::set<int>{};
231 for (unsigned int i = 0; i < kIterations; ++i) {
232 // Call the method.
233 ASSERT_TRUE(mSession->generateChallenge().isOk());
234
235 // Check that the generated challenge is unique and not 0.
236 auto lock = std::unique_lock{mCb->mMutex};
237 mCb->mCv.wait(lock, [this] { return mCb->mOnChallengeGeneratedInvoked; });
238 ASSERT_NE(mCb->mGeneratedChallenge, 0);
239 ASSERT_EQ(challenges.find(mCb->mGeneratedChallenge), challenges.end());
240
241 challenges.insert(mCb->mGeneratedChallenge);
242 mCb->mOnChallengeGeneratedInvoked = false;
243 }
244}
245
246TEST_P(Face, RevokeChallengeWorksForNonexistentChallengeTest) {
247 const int64_t nonexistentChallenge = 123;
248
249 // Call the method.
250 ASSERT_TRUE(mSession->revokeChallenge(nonexistentChallenge).isOk());
251
252 // Check that the challenge is revoked and matches the requested challenge.
253 auto lock = std::unique_lock{mCb->mMutex};
254 mCb->mCv.wait(lock, [this] { return mCb->mOnChallengeRevokedInvoked; });
255 ASSERT_EQ(mCb->mRevokedChallenge, nonexistentChallenge);
256}
257
258TEST_P(Face, RevokeChallengeWorksForExistentChallengeTest) {
259 // Generate a challenge.
260 ASSERT_TRUE(mSession->generateChallenge().isOk());
261
262 // Wait for the result.
263 auto lock = std::unique_lock{mCb->mMutex};
264 mCb->mCv.wait(lock, [this] { return mCb->mOnChallengeGeneratedInvoked; });
Ilya Matyukhin13d84062021-06-07 16:56:11 -0700265 lock.unlock();
Ilya Matyukhincc2b6942021-03-23 23:08:41 -0700266
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000267 // Revoke the challenge.
268 ASSERT_TRUE(mSession->revokeChallenge(mCb->mGeneratedChallenge).isOk());
Ilya Matyukhincc2b6942021-03-23 23:08:41 -0700269
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000270 // Check that the challenge is revoked and matches the requested challenge.
Ilya Matyukhin13d84062021-06-07 16:56:11 -0700271 lock.lock();
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000272 mCb->mCv.wait(lock, [this] { return mCb->mOnChallengeRevokedInvoked; });
273 ASSERT_EQ(mCb->mRevokedChallenge, mCb->mGeneratedChallenge);
274}
275
276TEST_P(Face, EnumerateEnrollmentsWorksTest) {
277 // Call the method.
278 ASSERT_TRUE(mSession->enumerateEnrollments().isOk());
279
280 // Wait for the result.
281 auto lock = std::unique_lock{mCb->mMutex};
282 mCb->mCv.wait(lock, [this] { return mCb->mOnEnrollmentsEnumeratedInvoked; });
283}
284
285TEST_P(Face, RemoveEnrollmentsWorksTest) {
286 // Call the method.
287 ASSERT_TRUE(mSession->removeEnrollments({}).isOk());
288
289 // Wait for the result.
290 auto lock = std::unique_lock{mCb->mMutex};
291 mCb->mCv.wait(lock, [this] { return mCb->mOnEnrollmentsRemovedInvoked; });
292}
293
Ilya Matyukhin61a6d8d2021-07-21 13:22:45 -0700294TEST_P(Face, GetFeaturesWithoutEnrollmentsResultsInUnableToProcess) {
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000295 // Call the method.
296 ASSERT_TRUE(mSession->getFeatures().isOk());
297
298 // Wait for the result.
299 auto lock = std::unique_lock{mCb->mMutex};
Ilya Matyukhin61a6d8d2021-07-21 13:22:45 -0700300 mCb->mCv.wait(lock, [this] { return mCb->mOnErrorInvoked; });
301 EXPECT_EQ(mCb->mError, Error::UNABLE_TO_PROCESS);
302 EXPECT_EQ(mCb->mVendorCode, 0);
Ilya Matyukhin046020b2021-07-03 00:05:47 +0000303}
304
305TEST_P(Face, GetAuthenticatorIdWorksTest) {
306 // Call the method.
307 ASSERT_TRUE(mSession->getAuthenticatorId().isOk());
308
309 // Wait for the result.
310 auto lock = std::unique_lock{mCb->mMutex};
311 mCb->mCv.wait(lock, [this] { return mCb->mOnAuthenticatorIdRetrievedInvoked; });
312}
313
314TEST_P(Face, InvalidateAuthenticatorIdWorksTest) {
315 // Call the method.
316 ASSERT_TRUE(mSession->invalidateAuthenticatorId().isOk());
317
318 // Wait for the result.
319 auto lock = std::unique_lock{mCb->mMutex};
320 mCb->mCv.wait(lock, [this] { return mCb->mOnAuthenticatorIdInvalidatedInvoked; });
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700321}
322
323GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Face);
324INSTANTIATE_TEST_SUITE_P(IFace, Face,
325 testing::ValuesIn(::android::getAidlHalInstanceNames(IFace::descriptor)),
326 ::android::PrintInstanceNameToString);
327
328} // namespace
Ilya Matyukhincc2b6942021-03-23 23:08:41 -0700329} // namespace aidl::android::hardware::biometrics::face
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -0700330
331int main(int argc, char** argv) {
332 ::testing::InitGoogleTest(&argc, argv);
333 ABinderProcess_setThreadPoolMaxThreadCount(1);
334 ABinderProcess_startThreadPool();
335 return RUN_ALL_TESTS();
336}