blob: 894fdfe362328d994e0be1f37350b315a69b1fef [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 */
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080016
Ilya Matyukhin14412df2020-08-27 14:26:06 -070017#include <aidl/Gtest.h>
18#include <aidl/Vintf.h>
19#include <aidl/android/hardware/biometrics/fingerprint/BnFingerprint.h>
20#include <aidl/android/hardware/biometrics/fingerprint/BnSessionCallback.h>
21
22#include <android/binder_manager.h>
23#include <android/binder_process.h>
24
25#include <future>
26
27namespace aidl::android::hardware::biometrics::fingerprint {
28namespace {
29
30constexpr int kSensorId = 0;
31constexpr int kUserId = 0;
32constexpr auto kCallbackTimeout = std::chrono::seconds(1);
33
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080034enum class MethodName {
Ilya Matyukhin14412df2020-08-27 14:26:06 -070035 kOnStateChanged,
36};
37
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080038struct Invocation {
39 MethodName methodName;
40 int32_t cookie;
Ilya Matyukhin14412df2020-08-27 14:26:06 -070041 SessionState state;
42};
43
44class SessionCallback : public BnSessionCallback {
45 public:
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080046 explicit SessionCallback() : mIsPromiseValid(false) {}
Ilya Matyukhin14412df2020-08-27 14:26:06 -070047
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080048 void setPromise(std::promise<std::vector<Invocation>>&& promise) {
49 mPromise = std::move(promise);
50 mIsPromiseValid = true;
51 }
52
53 ndk::ScopedAStatus onStateChanged(int32_t cookie, SessionState state) override {
54 Invocation invocation = {};
55 invocation.methodName = MethodName::kOnStateChanged;
56 invocation.cookie = cookie;
Ilya Matyukhin14412df2020-08-27 14:26:06 -070057 invocation.state = state;
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080058 mInvocations.push_back(invocation);
59 if (state == SessionState::IDLING) {
60 assert(mIsPromiseValid);
61 mPromise.set_value(mInvocations);
62 }
Ilya Matyukhin14412df2020-08-27 14:26:06 -070063 return ndk::ScopedAStatus::ok();
64 }
65
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070066 ndk::ScopedAStatus onChallengeGenerated(int64_t /*challenge*/) override {
67 return ndk::ScopedAStatus::ok();
68 }
69
70 ndk::ScopedAStatus onChallengeRevoked(int64_t /*challenge*/) override {
71 return ndk::ScopedAStatus::ok();
72 }
73
Ilya Matyukhin14412df2020-08-27 14:26:06 -070074 ndk::ScopedAStatus onAcquired(AcquiredInfo /*info*/, int32_t /*vendorCode*/) override {
75 return ndk::ScopedAStatus::ok();
76 }
77
78 ndk::ScopedAStatus onError(Error /*error*/, int32_t /*vendorCode*/) override {
79 return ndk::ScopedAStatus::ok();
80 }
81
Kevin Chyn1288c102020-09-18 16:42:44 -070082 ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
83 int32_t /*remaining*/) override {
Ilya Matyukhin14412df2020-08-27 14:26:06 -070084 return ndk::ScopedAStatus::ok();
85 }
86
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080087 ndk::ScopedAStatus onAuthenticationSucceeded(
88 int32_t /*enrollmentId*/, const keymaster::HardwareAuthToken& /*hat*/) override {
Ilya Matyukhin14412df2020-08-27 14:26:06 -070089 return ndk::ScopedAStatus::ok();
90 }
91
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080092 ndk::ScopedAStatus onAuthenticationFailed() override { return ndk::ScopedAStatus::ok(); }
Kevin Chyn64c13a02020-09-21 12:37:56 -070093
Kevin Chynef79d662020-09-22 12:14:44 -070094 ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override {
95 return ndk::ScopedAStatus::ok();
96 }
97
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080098 ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); }
Kevin Chynef79d662020-09-22 12:14:44 -070099
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800100 ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); }
Kevin Chynef79d662020-09-22 12:14:44 -0700101
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700102 ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); }
103
104 ndk::ScopedAStatus onEnrollmentsEnumerated(
105 const std::vector<int32_t>& /*enrollmentIds*/) override {
106 return ndk::ScopedAStatus::ok();
107 }
108
109 ndk::ScopedAStatus onEnrollmentsRemoved(
110 const std::vector<int32_t>& /*enrollmentIds*/) override {
111 return ndk::ScopedAStatus::ok();
112 }
113
Kevin Chyn6e862c32020-09-16 18:27:37 -0700114 ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
115 return ndk::ScopedAStatus::ok();
116 }
117
Kevin Chynf7890cc2021-01-11 17:08:36 -0800118 ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*newAuthenticatorId*/) override {
Kevin Chyn6e862c32020-09-16 18:27:37 -0700119 return ndk::ScopedAStatus::ok();
120 }
121
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700122 private:
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800123 bool mIsPromiseValid;
124 std::vector<Invocation> mInvocations;
125 std::promise<std::vector<Invocation>> mPromise;
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700126};
127
128class Fingerprint : public testing::TestWithParam<std::string> {
129 protected:
130 void SetUp() override {
131 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
132 ASSERT_NE(binder, nullptr);
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800133 mHal = IFingerprint::fromBinder(ndk::SpAIBinder(binder));
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700134 }
135
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800136 std::shared_ptr<IFingerprint> mHal;
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700137};
138
139TEST_P(Fingerprint, AuthenticateTest) {
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800140 // Prepare the callback
141 std::promise<std::vector<Invocation>> promise;
142 auto future = promise.get_future();
143 std::shared_ptr<SessionCallback> cb = ndk::SharedRefBase::make<SessionCallback>();
144 cb->setPromise(std::move(promise));
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700145
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800146 // Create a session
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700147 std::shared_ptr<ISession> session;
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800148 ASSERT_TRUE(mHal->createSession(kSensorId, kUserId, cb, &session).isOk());
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700149
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800150 // Call authenticate
151 int32_t cookie = 123;
152 std::shared_ptr<common::ICancellationSignal> cancellationSignal;
153 ASSERT_TRUE(session->authenticate(cookie, 0, &cancellationSignal).isOk());
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700154
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800155 // Get the results
156 ASSERT_TRUE(future.wait_for(kCallbackTimeout) == std::future_status::ready);
157 std::vector<Invocation> invocations = future.get();
158
159 // Close the session
160 ASSERT_TRUE(session->close(0).isOk());
161
162 ASSERT_FALSE(invocations.empty());
163 EXPECT_EQ(invocations.front().methodName, MethodName::kOnStateChanged);
164 EXPECT_EQ(invocations.front().state, SessionState::AUTHENTICATING);
165 EXPECT_EQ(invocations.back().methodName, MethodName::kOnStateChanged);
166 EXPECT_EQ(invocations.back().state, SessionState::IDLING);
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700167}
168
169GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Fingerprint);
170INSTANTIATE_TEST_SUITE_P(
171 IFingerprint, Fingerprint,
172 testing::ValuesIn(::android::getAidlHalInstanceNames(IFingerprint::descriptor)),
173 ::android::PrintInstanceNameToString);
174
175} // namespace
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800176} // namespace aidl::android::hardware::biometrics::fingerprint
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700177
178int main(int argc, char** argv) {
179 ::testing::InitGoogleTest(&argc, argv);
180 ABinderProcess_setThreadPoolMaxThreadCount(1);
181 ABinderProcess_startThreadPool();
182 return RUN_ALL_TESTS();
183}