• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
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 
24 #include <chrono>
25 #include <future>
26 
27 namespace aidl::android::hardware::biometrics::face {
28 namespace {
29 
30 using namespace std::literals::chrono_literals;
31 
32 using aidl::android::hardware::common::NativeHandle;
33 
34 constexpr int kSensorId = 0;
35 constexpr int kUserId = 0;
36 
37 class SessionCallback : public BnSessionCallback {
38   public:
onChallengeGenerated(int64_t challenge)39     ndk::ScopedAStatus onChallengeGenerated(int64_t challenge) override {
40         auto lock = std::lock_guard{mMutex};
41         mOnChallengeGeneratedInvoked = true;
42         mGeneratedChallenge = challenge;
43         mCv.notify_one();
44         return ndk::ScopedAStatus::ok();
45     }
46 
onChallengeRevoked(int64_t challenge)47     ndk::ScopedAStatus onChallengeRevoked(int64_t challenge) override {
48         auto lock = std::lock_guard{mMutex};
49         mOnChallengeRevokedInvoked = true;
50         mRevokedChallenge = challenge;
51         mCv.notify_one();
52         return ndk::ScopedAStatus::ok();
53     }
54 
onAuthenticationFrame(const AuthenticationFrame &)55     ndk::ScopedAStatus onAuthenticationFrame(const AuthenticationFrame& /*frame*/) override {
56         return ndk::ScopedAStatus::ok();
57     }
58 
onEnrollmentFrame(const EnrollmentFrame &)59     ndk::ScopedAStatus onEnrollmentFrame(const EnrollmentFrame& /*frame*/) override {
60         return ndk::ScopedAStatus::ok();
61     }
62 
onError(Error error,int32_t vendorCode)63     ndk::ScopedAStatus onError(Error error, int32_t vendorCode) override {
64         auto lock = std::lock_guard<std::mutex>{mMutex};
65         mError = error;
66         mVendorCode = vendorCode;
67         mOnErrorInvoked = true;
68         mCv.notify_one();
69         return ndk::ScopedAStatus::ok();
70     }
71 
onEnrollmentProgress(int32_t,int32_t)72     ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
73                                             int32_t /*remaining*/) override {
74         return ndk::ScopedAStatus::ok();
75     }
76 
onAuthenticationSucceeded(int32_t,const keymaster::HardwareAuthToken &)77     ndk::ScopedAStatus onAuthenticationSucceeded(
78             int32_t /*enrollmentId*/, const keymaster::HardwareAuthToken& /*hat*/) override {
79         return ndk::ScopedAStatus::ok();
80     }
81 
onAuthenticationFailed()82     ndk::ScopedAStatus onAuthenticationFailed() override { return ndk::ScopedAStatus::ok(); }
83 
onLockoutTimed(int64_t)84     ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override {
85         return ndk::ScopedAStatus::ok();
86     }
87 
onLockoutPermanent()88     ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); }
89 
onLockoutCleared()90     ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); }
91 
onInteractionDetected()92     ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); }
93 
onEnrollmentsEnumerated(const std::vector<int32_t> &)94     ndk::ScopedAStatus onEnrollmentsEnumerated(
95             const std::vector<int32_t>& /*enrollmentIds*/) override {
96         auto lock = std::lock_guard{mMutex};
97         mOnEnrollmentsEnumeratedInvoked = true;
98         mCv.notify_one();
99         return ndk::ScopedAStatus::ok();
100     }
101 
onEnrollmentsRemoved(const std::vector<int32_t> &)102     ndk::ScopedAStatus onEnrollmentsRemoved(
103             const std::vector<int32_t>& /*enrollmentIds*/) override {
104         auto lock = std::lock_guard{mMutex};
105         mOnEnrollmentsRemovedInvoked = true;
106         mCv.notify_one();
107         return ndk::ScopedAStatus::ok();
108     }
109 
onFeaturesRetrieved(const std::vector<Feature> &)110     ndk::ScopedAStatus onFeaturesRetrieved(const std::vector<Feature>& /*features*/) override {
111         auto lock = std::lock_guard{mMutex};
112         mOnFeaturesRetrievedInvoked = true;
113         mCv.notify_one();
114         return ndk::ScopedAStatus::ok();
115     }
116 
onFeatureSet(Feature)117     ndk::ScopedAStatus onFeatureSet(Feature /*feature*/) override {
118         return ndk::ScopedAStatus::ok();
119     }
120 
onAuthenticatorIdRetrieved(int64_t)121     ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
122         auto lock = std::lock_guard{mMutex};
123         mOnAuthenticatorIdRetrievedInvoked = true;
124         mCv.notify_one();
125         return ndk::ScopedAStatus::ok();
126     }
127 
onAuthenticatorIdInvalidated(int64_t)128     ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*newAuthenticatorId*/) override {
129         auto lock = std::lock_guard{mMutex};
130         mOnAuthenticatorIdInvalidatedInvoked = true;
131         mCv.notify_one();
132         return ndk::ScopedAStatus::ok();
133     }
134 
onSessionClosed()135     ndk::ScopedAStatus onSessionClosed() override {
136         auto lock = std::lock_guard<std::mutex>{mMutex};
137         mOnSessionClosedInvoked = true;
138         mCv.notify_one();
139         return ndk::ScopedAStatus::ok();
140     }
141 
142     std::mutex mMutex;
143     std::condition_variable mCv;
144     Error mError = Error::UNKNOWN;
145     int32_t mVendorCode = 0;
146     int64_t mGeneratedChallenge = 0;
147     int64_t mRevokedChallenge = 0;
148     bool mOnChallengeGeneratedInvoked = false;
149     bool mOnChallengeRevokedInvoked = false;
150     bool mOnErrorInvoked = false;
151     bool mOnEnrollmentsEnumeratedInvoked = false;
152     bool mOnEnrollmentsRemovedInvoked = false;
153     bool mOnFeaturesRetrievedInvoked = false;
154     bool mOnAuthenticatorIdRetrievedInvoked = false;
155     bool mOnAuthenticatorIdInvalidatedInvoked = false;
156     bool mOnSessionClosedInvoked = false;
157 };
158 
159 class Face : public testing::TestWithParam<std::string> {
160   protected:
SetUp()161     void SetUp() override {
162         // 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 
TearDown()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; });
194     }
195 
196     std::shared_ptr<IFace> mHal;
197     std::shared_ptr<SessionCallback> mCb;
198     std::shared_ptr<ISession> mSession;
199 };
200 
TEST_P(Face,GetSensorPropsWorksTest)201 TEST_P(Face, GetSensorPropsWorksTest) {
202     std::vector<SensorProps> sensorProps;
203 
204     // Call the method.
205     ASSERT_TRUE(mHal->getSensorProps(&sensorProps).isOk());
206 
207     // Make sure the sensorProps aren't empty.
208     ASSERT_FALSE(sensorProps.empty());
209     ASSERT_FALSE(sensorProps[0].commonProps.componentInfo.empty());
210 }
211 
TEST_P(Face,EnrollWithBadHatResultsInErrorTest)212 TEST_P(Face, EnrollWithBadHatResultsInErrorTest) {
213     // Call the method.
214     auto hat = keymaster::HardwareAuthToken{};
215     std::shared_ptr<common::ICancellationSignal> cancellationSignal;
216     ASSERT_TRUE(
217             mSession->enroll(hat, EnrollmentType::DEFAULT, {}, std::nullopt, &cancellationSignal)
218                     .isOk());
219 
220     // Make sure an error is returned.
221     auto lock = std::unique_lock{mCb->mMutex};
222     mCb->mCv.wait(lock, [this] { return mCb->mOnErrorInvoked; });
223     EXPECT_EQ(mCb->mError, Error::UNABLE_TO_PROCESS);
224     EXPECT_EQ(mCb->mVendorCode, 0);
225 }
226 
TEST_P(Face,GenerateChallengeProducesUniqueChallengesTest)227 TEST_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 
TEST_P(Face,RevokeChallengeWorksForNonexistentChallengeTest)246 TEST_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 
TEST_P(Face,RevokeChallengeWorksForExistentChallengeTest)258 TEST_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; });
265     lock.unlock();
266 
267     // Revoke the challenge.
268     ASSERT_TRUE(mSession->revokeChallenge(mCb->mGeneratedChallenge).isOk());
269 
270     // Check that the challenge is revoked and matches the requested challenge.
271     lock.lock();
272     mCb->mCv.wait(lock, [this] { return mCb->mOnChallengeRevokedInvoked; });
273     ASSERT_EQ(mCb->mRevokedChallenge, mCb->mGeneratedChallenge);
274 }
275 
TEST_P(Face,EnumerateEnrollmentsWorksTest)276 TEST_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 
TEST_P(Face,RemoveEnrollmentsWorksTest)285 TEST_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 
TEST_P(Face,GetFeaturesWithoutEnrollmentsResultsInUnableToProcess)294 TEST_P(Face, GetFeaturesWithoutEnrollmentsResultsInUnableToProcess) {
295     // Call the method.
296     ASSERT_TRUE(mSession->getFeatures().isOk());
297 
298     // Wait for the result.
299     auto lock = std::unique_lock{mCb->mMutex};
300     mCb->mCv.wait(lock, [this] { return mCb->mOnErrorInvoked; });
301     EXPECT_EQ(mCb->mError, Error::UNABLE_TO_PROCESS);
302     EXPECT_EQ(mCb->mVendorCode, 0);
303 }
304 
TEST_P(Face,GetAuthenticatorIdWorksTest)305 TEST_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 
TEST_P(Face,InvalidateAuthenticatorIdWorksTest)314 TEST_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; });
321 }
322 
323 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Face);
324 INSTANTIATE_TEST_SUITE_P(IFace, Face,
325                          testing::ValuesIn(::android::getAidlHalInstanceNames(IFace::descriptor)),
326                          ::android::PrintInstanceNameToString);
327 
328 }  // namespace
329 }  // namespace aidl::android::hardware::biometrics::face
330 
main(int argc,char ** argv)331 int main(int argc, char** argv) {
332     ::testing::InitGoogleTest(&argc, argv);
333     ABinderProcess_setThreadPoolMaxThreadCount(1);
334     ABinderProcess_startThreadPool();
335     return RUN_ALL_TESTS();
336 }
337