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