blob: 9590d68000e92f659f32b20a9d5e361b8d7aaaa1 [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
63 ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/, int32_t /*remaining*/,
64 int32_t /*vendorCode*/) override {
65 return ndk::ScopedAStatus::ok();
66 }
67
68 ndk::ScopedAStatus onAuthenticated(int32_t /*enrollmentId*/,
69 const keymaster::HardwareAuthToken& /*hat*/) override {
70 return ndk::ScopedAStatus::ok();
71 }
72
73 ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); }
74
75 ndk::ScopedAStatus onEnrollmentsEnumerated(
76 const std::vector<int32_t>& /*enrollmentIds*/) override {
77 return ndk::ScopedAStatus::ok();
78 }
79
80 ndk::ScopedAStatus onEnrollmentsRemoved(
81 const std::vector<int32_t>& /*enrollmentIds*/) override {
82 return ndk::ScopedAStatus::ok();
83 }
84
Kevin Chyn6e862c32020-09-16 18:27:37 -070085 ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
86 return ndk::ScopedAStatus::ok();
87 }
88
89 ndk::ScopedAStatus onAuthenticatorIdInvalidated() override {
90 return ndk::ScopedAStatus::ok();
91 }
92
Ilya Matyukhin14412df2020-08-27 14:26:06 -070093 private:
94 std::promise<SessionCallbackInvocation> invocation_promise_;
95};
96
97class Fingerprint : public testing::TestWithParam<std::string> {
98 protected:
99 void SetUp() override {
100 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
101 ASSERT_NE(binder, nullptr);
102 hal_ = IFingerprint::fromBinder(ndk::SpAIBinder(binder));
103 }
104
105 std::shared_ptr<IFingerprint> hal_;
106};
107
108TEST_P(Fingerprint, AuthenticateTest) {
109 std::promise<SessionCallbackInvocation> invocation_promise;
110 std::future<SessionCallbackInvocation> invocation_future = invocation_promise.get_future();
111 std::shared_ptr<SessionCallback> session_cb =
112 ndk::SharedRefBase::make<SessionCallback>(std::move(invocation_promise));
113
114 std::shared_ptr<ISession> session;
115 ASSERT_TRUE(hal_->createSession(kSensorId, kUserId, session_cb, &session).isOk());
116
Kevin Chyne33abd62020-09-18 10:31:50 -0700117 std::shared_ptr<common::ICancellationSignal> cancel_cb;
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700118 ASSERT_TRUE(session->authenticate(0, 0, &cancel_cb).isOk());
119 ASSERT_EQ(invocation_future.wait_for(kCallbackTimeout), std::future_status::ready);
120
121 SessionCallbackInvocation invocation = invocation_future.get();
122 EXPECT_EQ(invocation.method_name, SessionCallbackMethodName::kOnStateChanged);
123 EXPECT_EQ(invocation.state, SessionState::AUTHENTICATING);
124}
125
126GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Fingerprint);
127INSTANTIATE_TEST_SUITE_P(
128 IFingerprint, Fingerprint,
129 testing::ValuesIn(::android::getAidlHalInstanceNames(IFingerprint::descriptor)),
130 ::android::PrintInstanceNameToString);
131
132} // namespace
133
134int main(int argc, char** argv) {
135 ::testing::InitGoogleTest(&argc, argv);
136 ABinderProcess_setThreadPoolMaxThreadCount(1);
137 ABinderProcess_startThreadPool();
138 return RUN_ALL_TESTS();
139}
140
141} // namespace aidl::android::hardware::biometrics::fingerprint