blob: f1cfb178372ac4482abb2053dfb840dec41ca3d3 [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
Ilya Matyukhincaa67672021-03-23 19:00:36 -070025#include <chrono>
Ilya Matyukhin14412df2020-08-27 14:26:06 -070026#include <future>
27
28namespace aidl::android::hardware::biometrics::fingerprint {
29namespace {
30
Ilya Matyukhincaa67672021-03-23 19:00:36 -070031using namespace std::literals::chrono_literals;
32
Ilya Matyukhin14412df2020-08-27 14:26:06 -070033constexpr int kSensorId = 0;
34constexpr int kUserId = 0;
Ilya Matyukhin14412df2020-08-27 14:26:06 -070035
36class SessionCallback : public BnSessionCallback {
37 public:
Ilya Matyukhincaa67672021-03-23 19:00:36 -070038 explicit SessionCallback(std::promise<void>&& promise) : mPromise(std::move(promise)) {}
Ilya Matyukhin14412df2020-08-27 14:26:06 -070039
Ilya Matyukhin3d54f452020-10-15 17:32:09 -070040 ndk::ScopedAStatus onChallengeGenerated(int64_t /*challenge*/) override {
41 return ndk::ScopedAStatus::ok();
42 }
43
44 ndk::ScopedAStatus onChallengeRevoked(int64_t /*challenge*/) override {
45 return ndk::ScopedAStatus::ok();
46 }
47
Ilya Matyukhin14412df2020-08-27 14:26:06 -070048 ndk::ScopedAStatus onAcquired(AcquiredInfo /*info*/, int32_t /*vendorCode*/) override {
49 return ndk::ScopedAStatus::ok();
50 }
51
52 ndk::ScopedAStatus onError(Error /*error*/, int32_t /*vendorCode*/) override {
53 return ndk::ScopedAStatus::ok();
54 }
55
Kevin Chyn1288c102020-09-18 16:42:44 -070056 ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
57 int32_t /*remaining*/) override {
Ilya Matyukhin14412df2020-08-27 14:26:06 -070058 return ndk::ScopedAStatus::ok();
59 }
60
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080061 ndk::ScopedAStatus onAuthenticationSucceeded(
62 int32_t /*enrollmentId*/, const keymaster::HardwareAuthToken& /*hat*/) override {
Ilya Matyukhin14412df2020-08-27 14:26:06 -070063 return ndk::ScopedAStatus::ok();
64 }
65
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080066 ndk::ScopedAStatus onAuthenticationFailed() override { return ndk::ScopedAStatus::ok(); }
Kevin Chyn64c13a02020-09-21 12:37:56 -070067
Kevin Chynef79d662020-09-22 12:14:44 -070068 ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override {
69 return ndk::ScopedAStatus::ok();
70 }
71
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080072 ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); }
Kevin Chynef79d662020-09-22 12:14:44 -070073
Ilya Matyukhin5e09b822021-02-26 12:24:18 -080074 ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); }
Kevin Chynef79d662020-09-22 12:14:44 -070075
Ilya Matyukhin14412df2020-08-27 14:26:06 -070076 ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); }
77
78 ndk::ScopedAStatus onEnrollmentsEnumerated(
79 const std::vector<int32_t>& /*enrollmentIds*/) override {
80 return ndk::ScopedAStatus::ok();
81 }
82
83 ndk::ScopedAStatus onEnrollmentsRemoved(
84 const std::vector<int32_t>& /*enrollmentIds*/) override {
85 return ndk::ScopedAStatus::ok();
86 }
87
Kevin Chyn6e862c32020-09-16 18:27:37 -070088 ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
89 return ndk::ScopedAStatus::ok();
90 }
91
Kevin Chynf7890cc2021-01-11 17:08:36 -080092 ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*newAuthenticatorId*/) override {
Kevin Chyn6e862c32020-09-16 18:27:37 -070093 return ndk::ScopedAStatus::ok();
94 }
95
Ilya Matyukhincaa67672021-03-23 19:00:36 -070096 ndk::ScopedAStatus onSessionClosed() override {
97 mPromise.set_value();
98 return ndk::ScopedAStatus::ok();
99 }
Ilya Matyukhincbbfa932021-03-22 13:25:15 -0700100
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700101 private:
Ilya Matyukhincaa67672021-03-23 19:00:36 -0700102 std::promise<void> mPromise;
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700103};
104
105class Fingerprint : public testing::TestWithParam<std::string> {
106 protected:
107 void SetUp() override {
108 AIBinder* binder = AServiceManager_waitForService(GetParam().c_str());
109 ASSERT_NE(binder, nullptr);
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800110 mHal = IFingerprint::fromBinder(ndk::SpAIBinder(binder));
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700111 }
112
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800113 std::shared_ptr<IFingerprint> mHal;
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700114};
115
116TEST_P(Fingerprint, AuthenticateTest) {
Ilya Matyukhincaa67672021-03-23 19:00:36 -0700117 auto promise = std::promise<void>{};
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800118 auto future = promise.get_future();
Ilya Matyukhincaa67672021-03-23 19:00:36 -0700119 // Prepare the callback.
120 auto cb = ndk::SharedRefBase::make<SessionCallback>(std::move(promise));
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700121
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800122 // Create a session
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700123 std::shared_ptr<ISession> session;
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800124 ASSERT_TRUE(mHal->createSession(kSensorId, kUserId, cb, &session).isOk());
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700125
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800126 // Call authenticate
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800127 std::shared_ptr<common::ICancellationSignal> cancellationSignal;
Ilya Matyukhincaa67672021-03-23 19:00:36 -0700128 ASSERT_TRUE(session->authenticate(-1 /* operationId */, &cancellationSignal).isOk());
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700129
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800130 // Get the results
Ilya Matyukhincaa67672021-03-23 19:00:36 -0700131 // TODO(b/166799066): test authenticate.
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800132
133 // Close the session
Ilya Matyukhincaa67672021-03-23 19:00:36 -0700134 ASSERT_TRUE(session->close().isOk());
135 auto status = future.wait_for(1s);
136 ASSERT_EQ(status, std::future_status::ready);
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700137}
138
139GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Fingerprint);
140INSTANTIATE_TEST_SUITE_P(
141 IFingerprint, Fingerprint,
142 testing::ValuesIn(::android::getAidlHalInstanceNames(IFingerprint::descriptor)),
143 ::android::PrintInstanceNameToString);
144
145} // namespace
Ilya Matyukhin5e09b822021-02-26 12:24:18 -0800146} // namespace aidl::android::hardware::biometrics::fingerprint
Ilya Matyukhin14412df2020-08-27 14:26:06 -0700147
148int main(int argc, char** argv) {
149 ::testing::InitGoogleTest(&argc, argv);
150 ABinderProcess_setThreadPoolMaxThreadCount(1);
151 ABinderProcess_startThreadPool();
152 return RUN_ALL_TESTS();
153}