blob: 5f1030620dff383041cce2c430777e5b79e38571 [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
24#include <future>
25
26namespace aidl::android::hardware::biometrics::face {
27namespace {
28
29constexpr int kSensorId = 0;
30constexpr int kUserId = 0;
31constexpr auto kCallbackTimeout = std::chrono::seconds(1);
32
33enum class SessionCallbackMethodName {
34 kOnStateChanged,
35};
36
37struct SessionCallbackInvocation {
38 SessionCallbackMethodName method_name;
39 SessionState state;
40};
41
42class SessionCallback : public BnSessionCallback {
43 public:
44 explicit SessionCallback(std::promise<SessionCallbackInvocation> invocation_promise)
45 : invocation_promise_(std::move(invocation_promise)) {}
46 ndk::ScopedAStatus onStateChanged(int32_t /*cookie*/, SessionState state) override {
47 SessionCallbackInvocation invocation = {};
48 invocation.method_name = SessionCallbackMethodName::kOnStateChanged;
49 invocation.state = state;
50 invocation_promise_.set_value(invocation);
51 return ndk::ScopedAStatus::ok();
52 }
53
Kevin Chyn0d13e042020-11-17 15:14:23 -080054 ndk::ScopedAStatus onChallengeGenerated(int64_t /*challenge*/) override {
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070055 return ndk::ScopedAStatus::ok();
56 }
57
Kevin Chyn0d13e042020-11-17 15:14:23 -080058 ndk::ScopedAStatus onChallengeRevoked(int64_t /*challenge*/) override {
Ilya Matyukhin3b542cd2020-10-12 18:23:46 -070059 return ndk::ScopedAStatus::ok();
60 }
61
62 ndk::ScopedAStatus onAcquired(AcquiredInfo /*info*/, int32_t /*vendorCode*/) override {
63 return ndk::ScopedAStatus::ok();
64 }
65
66 ndk::ScopedAStatus onError(Error /*error*/, int32_t /*vendorCode*/) override {
67 return ndk::ScopedAStatus::ok();
68 }
69
70 ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
71 int32_t /*remaining*/) override {
72 return ndk::ScopedAStatus::ok();
73 }
74
75 ndk::ScopedAStatus onAuthenticationSucceeded(
76 int32_t /*enrollmentId*/, const keymaster::HardwareAuthToken& /*hat*/) override {
77 return ndk::ScopedAStatus::ok();
78 }
79
80 ndk::ScopedAStatus onAuthenticationFailed() override { return ndk::ScopedAStatus::ok(); }
81
82 ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override {
83 return ndk::ScopedAStatus::ok();
84 }
85
86 ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); }
87
88 ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); }
89
90 ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); }
91
92 ndk::ScopedAStatus onEnrollmentsEnumerated(
93 const std::vector<int32_t>& /*enrollmentIds*/) override {
94 return ndk::ScopedAStatus::ok();
95 }
96
97 ndk::ScopedAStatus onEnrollmentsRemoved(
98 const std::vector<int32_t>& /*enrollmentIds*/) override {
99 return ndk::ScopedAStatus::ok();
100 }
101
102 ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
103 return ndk::ScopedAStatus::ok();
104 }
105
106 ndk::ScopedAStatus onAuthenticatorIdInvalidated() override { return ndk::ScopedAStatus::ok(); }
107
108 private:
109 std::promise<SessionCallbackInvocation> invocation_promise_;
110};
111
112class Face : public testing::TestWithParam<std::string> {
113 protected:
114 void SetUp() override {
115 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
116 ASSERT_NE(binder, nullptr);
117 hal_ = IFace::fromBinder(ndk::SpAIBinder(binder));
118 }
119
120 std::shared_ptr<IFace> hal_;
121};
122
123TEST_P(Face, AuthenticateTest) {
124 std::promise<SessionCallbackInvocation> invocation_promise;
125 std::future<SessionCallbackInvocation> invocation_future = invocation_promise.get_future();
126 std::shared_ptr<SessionCallback> session_cb =
127 ndk::SharedRefBase::make<SessionCallback>(std::move(invocation_promise));
128
129 std::shared_ptr<ISession> session;
130 ASSERT_TRUE(hal_->createSession(kSensorId, kUserId, session_cb, &session).isOk());
131
132 std::shared_ptr<common::ICancellationSignal> cancel_cb;
133 ASSERT_TRUE(session->authenticate(0, 0, &cancel_cb).isOk());
134 ASSERT_EQ(invocation_future.wait_for(kCallbackTimeout), std::future_status::ready);
135
136 SessionCallbackInvocation invocation = invocation_future.get();
137 EXPECT_EQ(invocation.method_name, SessionCallbackMethodName::kOnStateChanged);
138 EXPECT_EQ(invocation.state, SessionState::AUTHENTICATING);
139}
140
141GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Face);
142INSTANTIATE_TEST_SUITE_P(IFace, Face,
143 testing::ValuesIn(::android::getAidlHalInstanceNames(IFace::descriptor)),
144 ::android::PrintInstanceNameToString);
145
146} // namespace
147
148int main(int argc, char** argv) {
149 ::testing::InitGoogleTest(&argc, argv);
150 ABinderProcess_setThreadPoolMaxThreadCount(1);
151 ABinderProcess_startThreadPool();
152 return RUN_ALL_TESTS();
153}
154
155} // namespace aidl::android::hardware::biometrics::face