blob: ddbc0fee5a28fb9b0af5ce4e7f89e0ec6af2ba49 [file] [log] [blame]
Ilya Matyukhin14412df2020-08-27 14:26:06 -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/fingerprint/BnFingerprint.h>
19#include <aidl/android/hardware/biometrics/fingerprint/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::fingerprint {
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
47 ndk::ScopedAStatus onStateChanged(int32_t /*cookie*/, SessionState state) override {
48 SessionCallbackInvocation invocation = {};
49 invocation.method_name = SessionCallbackMethodName::kOnStateChanged;
50 invocation.state = state;
51 invocation_promise_.set_value(invocation);
52 return ndk::ScopedAStatus::ok();
53 }
54
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070055 ndk::ScopedAStatus onChallengeGenerated(int64_t /*challenge*/) override {
56 return ndk::ScopedAStatus::ok();
57 }
58
59 ndk::ScopedAStatus onChallengeRevoked(int64_t /*challenge*/) override {
60 return ndk::ScopedAStatus::ok();
61 }
62
Ilya Matyukhin14412df2020-08-27 14:26:06 -070063 ndk::ScopedAStatus onAcquired(AcquiredInfo /*info*/, int32_t /*vendorCode*/) override {
64 return ndk::ScopedAStatus::ok();
65 }
66
67 ndk::ScopedAStatus onError(Error /*error*/, int32_t /*vendorCode*/) override {
68 return ndk::ScopedAStatus::ok();
69 }
70
Kevin Chyn1288c102020-09-18 16:42:44 -070071 ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
72 int32_t /*remaining*/) override {
Ilya Matyukhin14412df2020-08-27 14:26:06 -070073 return ndk::ScopedAStatus::ok();
74 }
75
Kevin Chyn64c13a02020-09-21 12:37:56 -070076 ndk::ScopedAStatus onAuthenticationSucceeded(int32_t /*enrollmentId*/,
Ilya Matyukhin14412df2020-08-27 14:26:06 -070077 const keymaster::HardwareAuthToken& /*hat*/) override {
78 return ndk::ScopedAStatus::ok();
79 }
80
Kevin Chyn64c13a02020-09-21 12:37:56 -070081 ndk::ScopedAStatus onAuthenticationFailed() override {
82 return ndk::ScopedAStatus::ok();
83 }
84
Kevin Chynef79d662020-09-22 12:14:44 -070085 ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override {
86 return ndk::ScopedAStatus::ok();
87 }
88
89 ndk::ScopedAStatus onLockoutPermanent() override {
90 return ndk::ScopedAStatus::ok();
91 }
92
93 ndk::ScopedAStatus onLockoutCleared() override {
94 return ndk::ScopedAStatus::ok();
95 }
96
Ilya Matyukhin14412df2020-08-27 14:26:06 -070097 ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); }
98
99 ndk::ScopedAStatus onEnrollmentsEnumerated(
100 const std::vector<int32_t>& /*enrollmentIds*/) override {
101 return ndk::ScopedAStatus::ok();
102 }
103
104 ndk::ScopedAStatus onEnrollmentsRemoved(
105 const std::vector<int32_t>& /*enrollmentIds*/) override {
106 return ndk::ScopedAStatus::ok();
107 }
108
Kevin Chyn6e862c32020-09-16 18:27:37 -0700109 ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
110 return ndk::ScopedAStatus::ok();
111 }
112
Kevin Chynf7890cc2021-01-11 17:08:36 -0800113 ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*newAuthenticatorId*/) override {
Kevin Chyn6e862c32020-09-16 18:27:37 -0700114 return ndk::ScopedAStatus::ok();
115 }
116
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700117 private:
118 std::promise<SessionCallbackInvocation> invocation_promise_;
119};
120
121class Fingerprint : public testing::TestWithParam<std::string> {
122 protected:
123 void SetUp() override {
124 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
125 ASSERT_NE(binder, nullptr);
126 hal_ = IFingerprint::fromBinder(ndk::SpAIBinder(binder));
127 }
128
129 std::shared_ptr<IFingerprint> hal_;
130};
131
132TEST_P(Fingerprint, AuthenticateTest) {
133 std::promise<SessionCallbackInvocation> invocation_promise;
134 std::future<SessionCallbackInvocation> invocation_future = invocation_promise.get_future();
135 std::shared_ptr<SessionCallback> session_cb =
136 ndk::SharedRefBase::make<SessionCallback>(std::move(invocation_promise));
137
138 std::shared_ptr<ISession> session;
139 ASSERT_TRUE(hal_->createSession(kSensorId, kUserId, session_cb, &session).isOk());
140
Kevin Chyne33abd62020-09-18 10:31:50 -0700141 std::shared_ptr<common::ICancellationSignal> cancel_cb;
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700142 ASSERT_TRUE(session->authenticate(0, 0, &cancel_cb).isOk());
143 ASSERT_EQ(invocation_future.wait_for(kCallbackTimeout), std::future_status::ready);
144
145 SessionCallbackInvocation invocation = invocation_future.get();
146 EXPECT_EQ(invocation.method_name, SessionCallbackMethodName::kOnStateChanged);
147 EXPECT_EQ(invocation.state, SessionState::AUTHENTICATING);
148}
149
150GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Fingerprint);
151INSTANTIATE_TEST_SUITE_P(
152 IFingerprint, Fingerprint,
153 testing::ValuesIn(::android::getAidlHalInstanceNames(IFingerprint::descriptor)),
154 ::android::PrintInstanceNameToString);
155
156} // namespace
157
158int main(int argc, char** argv) {
159 ::testing::InitGoogleTest(&argc, argv);
160 ABinderProcess_setThreadPoolMaxThreadCount(1);
161 ABinderProcess_startThreadPool();
162 return RUN_ALL_TESTS();
163}
164
165} // namespace aidl::android::hardware::biometrics::fingerprint