blob: 88980bf8c9fd1e931ea5804c6b1acecbe0415d1a [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
85 private:
86 std::promise<SessionCallbackInvocation> invocation_promise_;
87};
88
89class Fingerprint : public testing::TestWithParam<std::string> {
90 protected:
91 void SetUp() override {
92 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
93 ASSERT_NE(binder, nullptr);
94 hal_ = IFingerprint::fromBinder(ndk::SpAIBinder(binder));
95 }
96
97 std::shared_ptr<IFingerprint> hal_;
98};
99
100TEST_P(Fingerprint, AuthenticateTest) {
101 std::promise<SessionCallbackInvocation> invocation_promise;
102 std::future<SessionCallbackInvocation> invocation_future = invocation_promise.get_future();
103 std::shared_ptr<SessionCallback> session_cb =
104 ndk::SharedRefBase::make<SessionCallback>(std::move(invocation_promise));
105
106 std::shared_ptr<ISession> session;
107 ASSERT_TRUE(hal_->createSession(kSensorId, kUserId, session_cb, &session).isOk());
108
109 std::shared_ptr<ICancellationSignal> cancel_cb;
110 ASSERT_TRUE(session->authenticate(0, 0, &cancel_cb).isOk());
111 ASSERT_EQ(invocation_future.wait_for(kCallbackTimeout), std::future_status::ready);
112
113 SessionCallbackInvocation invocation = invocation_future.get();
114 EXPECT_EQ(invocation.method_name, SessionCallbackMethodName::kOnStateChanged);
115 EXPECT_EQ(invocation.state, SessionState::AUTHENTICATING);
116}
117
118GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Fingerprint);
119INSTANTIATE_TEST_SUITE_P(
120 IFingerprint, Fingerprint,
121 testing::ValuesIn(::android::getAidlHalInstanceNames(IFingerprint::descriptor)),
122 ::android::PrintInstanceNameToString);
123
124} // namespace
125
126int main(int argc, char** argv) {
127 ::testing::InitGoogleTest(&argc, argv);
128 ABinderProcess_setThreadPoolMaxThreadCount(1);
129 ABinderProcess_startThreadPool();
130 return RUN_ALL_TESTS();
131}
132
133} // namespace aidl::android::hardware::biometrics::fingerprint