Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -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 | */ |
| 16 | #include <aidl/Gtest.h> |
| 17 | #include <aidl/Vintf.h> |
| 18 | #include <aidl/android/hardware/biometrics/face/BnFace.h> |
| 19 | #include <aidl/android/hardware/biometrics/face/BnSessionCallback.h> |
| 20 | |
| 21 | #include <android/binder_manager.h> |
| 22 | #include <android/binder_process.h> |
| 23 | |
| 24 | #include <future> |
| 25 | |
| 26 | namespace aidl::android::hardware::biometrics::face { |
| 27 | namespace { |
| 28 | |
| 29 | constexpr int kSensorId = 0; |
| 30 | constexpr int kUserId = 0; |
| 31 | constexpr auto kCallbackTimeout = std::chrono::seconds(1); |
| 32 | |
| 33 | enum class SessionCallbackMethodName { |
| 34 | kOnStateChanged, |
| 35 | }; |
| 36 | |
| 37 | struct SessionCallbackInvocation { |
| 38 | SessionCallbackMethodName method_name; |
| 39 | SessionState state; |
| 40 | }; |
| 41 | |
| 42 | class SessionCallback : public BnSessionCallback { |
| 43 | public: |
| 44 | explicit SessionCallback(std::promise<SessionCallbackInvocation> invocation_promise) |
| 45 | : invocation_promise_(std::move(invocation_promise)) {} |
| 46 | ndk::ScopedAStatus onStateChanged(int32_t /*cookie*/, SessionState state) override { |
| 47 | SessionCallbackInvocation invocation = {}; |
| 48 | invocation.method_name = SessionCallbackMethodName::kOnStateChanged; |
| 49 | invocation.state = state; |
| 50 | invocation_promise_.set_value(invocation); |
| 51 | return ndk::ScopedAStatus::ok(); |
| 52 | } |
| 53 | |
Kevin Chyn | 0d13e04 | 2020-11-17 15:14:23 -0800 | [diff] [blame] | 54 | ndk::ScopedAStatus onChallengeGenerated(int64_t /*challenge*/) override { |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 55 | return ndk::ScopedAStatus::ok(); |
| 56 | } |
| 57 | |
Kevin Chyn | 0d13e04 | 2020-11-17 15:14:23 -0800 | [diff] [blame] | 58 | ndk::ScopedAStatus onChallengeRevoked(int64_t /*challenge*/) override { |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 59 | return ndk::ScopedAStatus::ok(); |
| 60 | } |
| 61 | |
Ilya Matyukhin | e13bc81 | 2021-01-19 16:04:04 -0800 | [diff] [blame] | 62 | ndk::ScopedAStatus onAuthenticationFrame(const AuthenticationFrame& /*frame*/) override { |
| 63 | return ndk::ScopedAStatus::ok(); |
| 64 | } |
| 65 | |
| 66 | ndk::ScopedAStatus onEnrollmentFrame(const EnrollmentFrame& /*frame*/) override { |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 67 | return ndk::ScopedAStatus::ok(); |
| 68 | } |
| 69 | |
| 70 | ndk::ScopedAStatus onError(Error /*error*/, int32_t /*vendorCode*/) override { |
| 71 | return ndk::ScopedAStatus::ok(); |
| 72 | } |
| 73 | |
| 74 | ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/, |
| 75 | int32_t /*remaining*/) override { |
| 76 | return ndk::ScopedAStatus::ok(); |
| 77 | } |
| 78 | |
| 79 | ndk::ScopedAStatus onAuthenticationSucceeded( |
| 80 | int32_t /*enrollmentId*/, const keymaster::HardwareAuthToken& /*hat*/) override { |
| 81 | return ndk::ScopedAStatus::ok(); |
| 82 | } |
| 83 | |
| 84 | ndk::ScopedAStatus onAuthenticationFailed() override { return ndk::ScopedAStatus::ok(); } |
| 85 | |
| 86 | ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override { |
| 87 | return ndk::ScopedAStatus::ok(); |
| 88 | } |
| 89 | |
| 90 | ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); } |
| 91 | |
| 92 | ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); } |
| 93 | |
| 94 | ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); } |
| 95 | |
| 96 | ndk::ScopedAStatus onEnrollmentsEnumerated( |
| 97 | const std::vector<int32_t>& /*enrollmentIds*/) override { |
| 98 | return ndk::ScopedAStatus::ok(); |
| 99 | } |
| 100 | |
| 101 | ndk::ScopedAStatus onEnrollmentsRemoved( |
| 102 | const std::vector<int32_t>& /*enrollmentIds*/) override { |
| 103 | return ndk::ScopedAStatus::ok(); |
| 104 | } |
| 105 | |
Ilya Matyukhin | f2d3886 | 2021-01-22 11:48:59 -0800 | [diff] [blame] | 106 | ndk::ScopedAStatus onFeaturesRetrieved(const std::vector<Feature>& /*features*/, |
| 107 | int32_t /*enrollmentId*/) override { |
| 108 | return ndk::ScopedAStatus::ok(); |
| 109 | } |
| 110 | |
| 111 | ndk::ScopedAStatus onFeatureSet(int32_t /*enrollmentId*/, Feature /*feature*/) override { |
| 112 | return ndk::ScopedAStatus::ok(); |
| 113 | } |
| 114 | |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 115 | ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override { |
| 116 | return ndk::ScopedAStatus::ok(); |
| 117 | } |
| 118 | |
Kevin Chyn | f7890cc | 2021-01-11 17:08:36 -0800 | [diff] [blame] | 119 | ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*newAuthenticatorId*/) override { |
| 120 | return ndk::ScopedAStatus::ok(); |
| 121 | } |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 122 | |
| 123 | private: |
| 124 | std::promise<SessionCallbackInvocation> invocation_promise_; |
| 125 | }; |
| 126 | |
| 127 | class Face : public testing::TestWithParam<std::string> { |
| 128 | protected: |
| 129 | void SetUp() override { |
| 130 | AIBinder* binder = AServiceManager_waitForService(GetParam().c_str()); |
| 131 | ASSERT_NE(binder, nullptr); |
| 132 | hal_ = IFace::fromBinder(ndk::SpAIBinder(binder)); |
| 133 | } |
| 134 | |
| 135 | std::shared_ptr<IFace> hal_; |
| 136 | }; |
| 137 | |
| 138 | TEST_P(Face, AuthenticateTest) { |
| 139 | std::promise<SessionCallbackInvocation> invocation_promise; |
| 140 | std::future<SessionCallbackInvocation> invocation_future = invocation_promise.get_future(); |
| 141 | std::shared_ptr<SessionCallback> session_cb = |
| 142 | ndk::SharedRefBase::make<SessionCallback>(std::move(invocation_promise)); |
| 143 | |
| 144 | std::shared_ptr<ISession> session; |
| 145 | ASSERT_TRUE(hal_->createSession(kSensorId, kUserId, session_cb, &session).isOk()); |
| 146 | |
| 147 | std::shared_ptr<common::ICancellationSignal> cancel_cb; |
| 148 | ASSERT_TRUE(session->authenticate(0, 0, &cancel_cb).isOk()); |
| 149 | ASSERT_EQ(invocation_future.wait_for(kCallbackTimeout), std::future_status::ready); |
| 150 | |
| 151 | SessionCallbackInvocation invocation = invocation_future.get(); |
| 152 | EXPECT_EQ(invocation.method_name, SessionCallbackMethodName::kOnStateChanged); |
| 153 | EXPECT_EQ(invocation.state, SessionState::AUTHENTICATING); |
| 154 | } |
| 155 | |
| 156 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Face); |
| 157 | INSTANTIATE_TEST_SUITE_P(IFace, Face, |
| 158 | testing::ValuesIn(::android::getAidlHalInstanceNames(IFace::descriptor)), |
| 159 | ::android::PrintInstanceNameToString); |
| 160 | |
| 161 | } // namespace |
| 162 | |
| 163 | int main(int argc, char** argv) { |
| 164 | ::testing::InitGoogleTest(&argc, argv); |
| 165 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 166 | ABinderProcess_startThreadPool(); |
| 167 | return RUN_ALL_TESTS(); |
| 168 | } |
| 169 | |
| 170 | } // namespace aidl::android::hardware::biometrics::face |