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 | |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 24 | #include <chrono> |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 25 | #include <future> |
| 26 | |
| 27 | namespace aidl::android::hardware::biometrics::face { |
| 28 | namespace { |
| 29 | |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 30 | using namespace std::literals::chrono_literals; |
| 31 | |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 32 | constexpr int kSensorId = 0; |
| 33 | constexpr int kUserId = 0; |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 34 | |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 35 | enum class MethodName { |
| 36 | kOnError, |
| 37 | kOnSessionClosed, |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 38 | }; |
| 39 | |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 40 | struct Invocation { |
| 41 | MethodName methodName; |
| 42 | Error error; |
| 43 | int32_t vendorCode; |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 44 | }; |
| 45 | |
| 46 | class SessionCallback : public BnSessionCallback { |
| 47 | public: |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 48 | explicit SessionCallback(Invocation* inv) : mInv(inv) {} |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 49 | |
Kevin Chyn | 0d13e04 | 2020-11-17 15:14:23 -0800 | [diff] [blame] | 50 | ndk::ScopedAStatus onChallengeGenerated(int64_t /*challenge*/) override { |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 51 | return ndk::ScopedAStatus::ok(); |
| 52 | } |
| 53 | |
Kevin Chyn | 0d13e04 | 2020-11-17 15:14:23 -0800 | [diff] [blame] | 54 | ndk::ScopedAStatus onChallengeRevoked(int64_t /*challenge*/) override { |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 55 | return ndk::ScopedAStatus::ok(); |
| 56 | } |
| 57 | |
Ilya Matyukhin | e13bc81 | 2021-01-19 16:04:04 -0800 | [diff] [blame] | 58 | ndk::ScopedAStatus onAuthenticationFrame(const AuthenticationFrame& /*frame*/) override { |
| 59 | return ndk::ScopedAStatus::ok(); |
| 60 | } |
| 61 | |
| 62 | ndk::ScopedAStatus onEnrollmentFrame(const EnrollmentFrame& /*frame*/) override { |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 63 | return ndk::ScopedAStatus::ok(); |
| 64 | } |
| 65 | |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 66 | ndk::ScopedAStatus onError(Error error, int32_t vendorCode) override { |
| 67 | *mInv = {}; |
| 68 | mInv->methodName = MethodName::kOnError; |
| 69 | mInv->error = error; |
| 70 | mInv->vendorCode = vendorCode; |
| 71 | |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 72 | return ndk::ScopedAStatus::ok(); |
| 73 | } |
| 74 | |
| 75 | ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/, |
| 76 | int32_t /*remaining*/) override { |
| 77 | return ndk::ScopedAStatus::ok(); |
| 78 | } |
| 79 | |
| 80 | ndk::ScopedAStatus onAuthenticationSucceeded( |
| 81 | int32_t /*enrollmentId*/, const keymaster::HardwareAuthToken& /*hat*/) override { |
| 82 | return ndk::ScopedAStatus::ok(); |
| 83 | } |
| 84 | |
| 85 | ndk::ScopedAStatus onAuthenticationFailed() override { return ndk::ScopedAStatus::ok(); } |
| 86 | |
| 87 | ndk::ScopedAStatus onLockoutTimed(int64_t /*durationMillis*/) override { |
| 88 | return ndk::ScopedAStatus::ok(); |
| 89 | } |
| 90 | |
| 91 | ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); } |
| 92 | |
| 93 | ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); } |
| 94 | |
| 95 | ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); } |
| 96 | |
| 97 | ndk::ScopedAStatus onEnrollmentsEnumerated( |
| 98 | const std::vector<int32_t>& /*enrollmentIds*/) override { |
| 99 | return ndk::ScopedAStatus::ok(); |
| 100 | } |
| 101 | |
| 102 | ndk::ScopedAStatus onEnrollmentsRemoved( |
| 103 | const std::vector<int32_t>& /*enrollmentIds*/) override { |
| 104 | return ndk::ScopedAStatus::ok(); |
| 105 | } |
| 106 | |
Ilya Matyukhin | f2d3886 | 2021-01-22 11:48:59 -0800 | [diff] [blame] | 107 | ndk::ScopedAStatus onFeaturesRetrieved(const std::vector<Feature>& /*features*/, |
| 108 | int32_t /*enrollmentId*/) override { |
| 109 | return ndk::ScopedAStatus::ok(); |
| 110 | } |
| 111 | |
| 112 | ndk::ScopedAStatus onFeatureSet(int32_t /*enrollmentId*/, Feature /*feature*/) override { |
| 113 | return ndk::ScopedAStatus::ok(); |
| 114 | } |
| 115 | |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 116 | ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override { |
| 117 | return ndk::ScopedAStatus::ok(); |
| 118 | } |
| 119 | |
Kevin Chyn | f7890cc | 2021-01-11 17:08:36 -0800 | [diff] [blame] | 120 | ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*newAuthenticatorId*/) override { |
| 121 | return ndk::ScopedAStatus::ok(); |
| 122 | } |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 123 | |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 124 | ndk::ScopedAStatus onSessionClosed() override { |
| 125 | *mInv = {}; |
| 126 | mInv->methodName = MethodName::kOnSessionClosed; |
| 127 | |
| 128 | return ndk::ScopedAStatus::ok(); |
| 129 | } |
Ilya Matyukhin | cbbfa93 | 2021-03-22 13:25:15 -0700 | [diff] [blame] | 130 | |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 131 | private: |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 132 | Invocation* mInv; |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | class Face : public testing::TestWithParam<std::string> { |
| 136 | protected: |
| 137 | void SetUp() override { |
| 138 | AIBinder* binder = AServiceManager_waitForService(GetParam().c_str()); |
| 139 | ASSERT_NE(binder, nullptr); |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 140 | mHal = IFace::fromBinder(ndk::SpAIBinder(binder)); |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 141 | } |
| 142 | |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 143 | std::shared_ptr<IFace> mHal; |
| 144 | Invocation mInv; |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 145 | }; |
| 146 | |
| 147 | TEST_P(Face, AuthenticateTest) { |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 148 | // Prepare the callback. |
| 149 | auto cb = ndk::SharedRefBase::make<SessionCallback>(&mInv); |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 150 | |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 151 | // Create a session |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 152 | std::shared_ptr<ISession> session; |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 153 | ASSERT_TRUE(mHal->createSession(kSensorId, kUserId, cb, &session).isOk()); |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 154 | |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 155 | // Call authenticate |
| 156 | std::shared_ptr<common::ICancellationSignal> cancellationSignal; |
| 157 | ASSERT_TRUE(session->authenticate(0 /* operationId */, &cancellationSignal).isOk()); |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 158 | |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 159 | // Get the results |
| 160 | EXPECT_EQ(mInv.methodName, MethodName::kOnError); |
| 161 | EXPECT_EQ(mInv.error, Error::UNABLE_TO_PROCESS); |
| 162 | EXPECT_EQ(mInv.vendorCode, 0); |
| 163 | |
| 164 | // Close the session |
| 165 | ASSERT_TRUE(session->close().isOk()); |
| 166 | |
| 167 | EXPECT_EQ(mInv.methodName, MethodName::kOnSessionClosed); |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(Face); |
| 171 | INSTANTIATE_TEST_SUITE_P(IFace, Face, |
| 172 | testing::ValuesIn(::android::getAidlHalInstanceNames(IFace::descriptor)), |
| 173 | ::android::PrintInstanceNameToString); |
| 174 | |
| 175 | } // namespace |
Ilya Matyukhin | cc2b694 | 2021-03-23 23:08:41 -0700 | [diff] [blame] | 176 | } // namespace aidl::android::hardware::biometrics::face |
Ilya Matyukhin | 3b542cd | 2020-10-12 18:23:46 -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 | } |
| 184 | |