blob: 496badc741a17079cfbe386977ed45063dfa916e [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
55 ndk::ScopedAStatus onAcquired(AcquiredInfo /*info*/, int32_t /*vendorCode*/) override {
56 return ndk::ScopedAStatus::ok();
57 }
58
59 ndk::ScopedAStatus onError(Error /*error*/, int32_t /*vendorCode*/) override {
60 return ndk::ScopedAStatus::ok();
61 }
62
Kevin Chyn1288c102020-09-18 16:42:44 -070063 ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
64 int32_t /*remaining*/) override {
Ilya Matyukhin14412df2020-08-27 14:26:06 -070065 return ndk::ScopedAStatus::ok();
66 }
67
Kevin Chyn64c13a02020-09-21 12:37:56 -070068 ndk::ScopedAStatus onAuthenticationSucceeded(int32_t /*enrollmentId*/,
Ilya Matyukhin14412df2020-08-27 14:26:06 -070069 const keymaster::HardwareAuthToken& /*hat*/) override {
70 return ndk::ScopedAStatus::ok();
71 }
72
Kevin Chyn64c13a02020-09-21 12:37:56 -070073 ndk::ScopedAStatus onAuthenticationFailed() override {
74 return ndk::ScopedAStatus::ok();
75 }
76
Kevin Chynef79d662020-09-22 12:14:44 -070077 ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override {
78 return ndk::ScopedAStatus::ok();
79 }
80
81 ndk::ScopedAStatus onLockoutPermanent() override {
82 return ndk::ScopedAStatus::ok();
83 }
84
85 ndk::ScopedAStatus onLockoutCleared() override {
86 return ndk::ScopedAStatus::ok();
87 }
88
Ilya Matyukhin14412df2020-08-27 14:26:06 -070089 ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); }
90
91 ndk::ScopedAStatus onEnrollmentsEnumerated(
92 const std::vector<int32_t>& /*enrollmentIds*/) override {
93 return ndk::ScopedAStatus::ok();
94 }
95
96 ndk::ScopedAStatus onEnrollmentsRemoved(
97 const std::vector<int32_t>& /*enrollmentIds*/) override {
98 return ndk::ScopedAStatus::ok();
99 }
100
Kevin Chyn6e862c32020-09-16 18:27:37 -0700101 ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
102 return ndk::ScopedAStatus::ok();
103 }
104
105 ndk::ScopedAStatus onAuthenticatorIdInvalidated() override {
106 return ndk::ScopedAStatus::ok();
107 }
108
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700109 private:
110 std::promise<SessionCallbackInvocation> invocation_promise_;
111};
112
113class Fingerprint : public testing::TestWithParam<std::string> {
114 protected:
115 void SetUp() override {
116 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
117 ASSERT_NE(binder, nullptr);
118 hal_ = IFingerprint::fromBinder(ndk::SpAIBinder(binder));
119 }
120
121 std::shared_ptr<IFingerprint> hal_;
122};
123
124TEST_P(Fingerprint, AuthenticateTest) {
125 std::promise<SessionCallbackInvocation> invocation_promise;
126 std::future<SessionCallbackInvocation> invocation_future = invocation_promise.get_future();
127 std::shared_ptr<SessionCallback> session_cb =
128 ndk::SharedRefBase::make<SessionCallback>(std::move(invocation_promise));
129
130 std::shared_ptr<ISession> session;
131 ASSERT_TRUE(hal_->createSession(kSensorId, kUserId, session_cb, &session).isOk());
132
Kevin Chyne33abd62020-09-18 10:31:50 -0700133 std::shared_ptr<common::ICancellationSignal> cancel_cb;
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700134 ASSERT_TRUE(session->authenticate(0, 0, &cancel_cb).isOk());
135 ASSERT_EQ(invocation_future.wait_for(kCallbackTimeout), std::future_status::ready);
136
137 SessionCallbackInvocation invocation = invocation_future.get();
138 EXPECT_EQ(invocation.method_name, SessionCallbackMethodName::kOnStateChanged);
139 EXPECT_EQ(invocation.state, SessionState::AUTHENTICATING);
140}
141
142GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Fingerprint);
143INSTANTIATE_TEST_SUITE_P(
144 IFingerprint, Fingerprint,
145 testing::ValuesIn(::android::getAidlHalInstanceNames(IFingerprint::descriptor)),
146 ::android::PrintInstanceNameToString);
147
148} // namespace
149
150int main(int argc, char** argv) {
151 ::testing::InitGoogleTest(&argc, argv);
152 ABinderProcess_setThreadPoolMaxThreadCount(1);
153 ABinderProcess_startThreadPool();
154 return RUN_ALL_TESTS();
155}
156
157} // namespace aidl::android::hardware::biometrics::fingerprint