Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 1 | /* |
| 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 Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 16 | |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 17 | #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 | |
| 27 | namespace aidl::android::hardware::biometrics::fingerprint { |
| 28 | namespace { |
| 29 | |
| 30 | constexpr int kSensorId = 0; |
| 31 | constexpr int kUserId = 0; |
| 32 | constexpr auto kCallbackTimeout = std::chrono::seconds(1); |
| 33 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 34 | enum class MethodName { |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 35 | kOnStateChanged, |
| 36 | }; |
| 37 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 38 | struct Invocation { |
| 39 | MethodName methodName; |
| 40 | int32_t cookie; |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 41 | SessionState state; |
| 42 | }; |
| 43 | |
| 44 | class SessionCallback : public BnSessionCallback { |
| 45 | public: |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 46 | explicit SessionCallback() : mIsPromiseValid(false) {} |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 47 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 48 | 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 Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 57 | invocation.state = state; |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 58 | mInvocations.push_back(invocation); |
| 59 | if (state == SessionState::IDLING) { |
| 60 | assert(mIsPromiseValid); |
| 61 | mPromise.set_value(mInvocations); |
| 62 | } |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 63 | return ndk::ScopedAStatus::ok(); |
| 64 | } |
| 65 | |
Ilya Matyukhin | 3d54f45 | 2020-10-15 17:32:09 -0700 | [diff] [blame] | 66 | 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 Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 74 | 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 Chyn | 1288c10 | 2020-09-18 16:42:44 -0700 | [diff] [blame] | 82 | ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/, |
| 83 | int32_t /*remaining*/) override { |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 84 | return ndk::ScopedAStatus::ok(); |
| 85 | } |
| 86 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 87 | ndk::ScopedAStatus onAuthenticationSucceeded( |
| 88 | int32_t /*enrollmentId*/, const keymaster::HardwareAuthToken& /*hat*/) override { |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 89 | return ndk::ScopedAStatus::ok(); |
| 90 | } |
| 91 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 92 | ndk::ScopedAStatus onAuthenticationFailed() override { return ndk::ScopedAStatus::ok(); } |
Kevin Chyn | 64c13a0 | 2020-09-21 12:37:56 -0700 | [diff] [blame] | 93 | |
Kevin Chyn | ef79d66 | 2020-09-22 12:14:44 -0700 | [diff] [blame] | 94 | ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override { |
| 95 | return ndk::ScopedAStatus::ok(); |
| 96 | } |
| 97 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 98 | ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); } |
Kevin Chyn | ef79d66 | 2020-09-22 12:14:44 -0700 | [diff] [blame] | 99 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 100 | ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); } |
Kevin Chyn | ef79d66 | 2020-09-22 12:14:44 -0700 | [diff] [blame] | 101 | |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 102 | 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 Chyn | 6e862c3 | 2020-09-16 18:27:37 -0700 | [diff] [blame] | 114 | ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override { |
| 115 | return ndk::ScopedAStatus::ok(); |
| 116 | } |
| 117 | |
Kevin Chyn | f7890cc | 2021-01-11 17:08:36 -0800 | [diff] [blame] | 118 | ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*newAuthenticatorId*/) override { |
Kevin Chyn | 6e862c3 | 2020-09-16 18:27:37 -0700 | [diff] [blame] | 119 | return ndk::ScopedAStatus::ok(); |
| 120 | } |
| 121 | |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 122 | private: |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 123 | bool mIsPromiseValid; |
| 124 | std::vector<Invocation> mInvocations; |
| 125 | std::promise<std::vector<Invocation>> mPromise; |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 126 | }; |
| 127 | |
| 128 | class 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 Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 133 | mHal = IFingerprint::fromBinder(ndk::SpAIBinder(binder)); |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 136 | std::shared_ptr<IFingerprint> mHal; |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | TEST_P(Fingerprint, AuthenticateTest) { |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 140 | // 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 Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 145 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 146 | // Create a session |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 147 | std::shared_ptr<ISession> session; |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 148 | ASSERT_TRUE(mHal->createSession(kSensorId, kUserId, cb, &session).isOk()); |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 149 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 150 | // Call authenticate |
| 151 | int32_t cookie = 123; |
| 152 | std::shared_ptr<common::ICancellationSignal> cancellationSignal; |
| 153 | ASSERT_TRUE(session->authenticate(cookie, 0, &cancellationSignal).isOk()); |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 154 | |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 155 | // 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 Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Fingerprint); |
| 170 | INSTANTIATE_TEST_SUITE_P( |
| 171 | IFingerprint, Fingerprint, |
| 172 | testing::ValuesIn(::android::getAidlHalInstanceNames(IFingerprint::descriptor)), |
| 173 | ::android::PrintInstanceNameToString); |
| 174 | |
| 175 | } // namespace |
Ilya Matyukhin | 5e09b82 | 2021-02-26 12:24:18 -0800 | [diff] [blame] | 176 | } // namespace aidl::android::hardware::biometrics::fingerprint |
Ilya Matyukhin | 14412df | 2020-08-27 14:26:06 -0700 | [diff] [blame] | 177 | |
| 178 | int main(int argc, char** argv) { |
| 179 | ::testing::InitGoogleTest(&argc, argv); |
| 180 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 181 | ABinderProcess_startThreadPool(); |
| 182 | return RUN_ALL_TESTS(); |
| 183 | } |