1 /*
2  * Copyright (C) 2023 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 
17 #include <android/binder_process.h>
18 #include <fingerprint.sysprop.h>
19 #include <gtest/gtest.h>
20 
21 #include <android-base/logging.h>
22 
23 #include <aidl/android/hardware/biometrics/fingerprint/BnSessionCallback.h>
24 
25 #include "Session.h"
26 #include "thread/WorkerThread.h"
27 #include "util/Util.h"
28 
29 using namespace ::android::fingerprint::virt;
30 using namespace ::aidl::android::hardware::biometrics::fingerprint;
31 
32 namespace aidl::android::hardware::biometrics::fingerprint {
33 
34 class TestSessionCallback : public BnSessionCallback {
35   public:
onChallengeGenerated(int64_t)36     ndk::ScopedAStatus onChallengeGenerated(int64_t /*challenge*/) override {
37         return ndk::ScopedAStatus::ok();
38     };
onChallengeRevoked(int64_t)39     ::ndk::ScopedAStatus onChallengeRevoked(int64_t /*challenge*/) override {
40         return ndk::ScopedAStatus::ok();
41     };
onError(fingerprint::Error,int32_t)42     ::ndk::ScopedAStatus onError(fingerprint::Error /*error*/, int32_t /*vendorCode*/) override {
43         return ndk::ScopedAStatus::ok();
44     };
onEnrollmentProgress(int32_t,int32_t)45     ::ndk::ScopedAStatus onEnrollmentProgress(int32_t /*enrollmentId*/,
46                                               int32_t /*remaining*/) override {
47         return ndk::ScopedAStatus::ok();
48     };
49 
onAuthenticationSucceeded(int32_t,const keymaster::HardwareAuthToken &)50     ::ndk::ScopedAStatus onAuthenticationSucceeded(int32_t /*enrollmentId*/,
51                                                    const keymaster::HardwareAuthToken&) override {
52         return ndk::ScopedAStatus::ok();
53     };
onAuthenticationFailed()54     ::ndk::ScopedAStatus onAuthenticationFailed() override { return ndk::ScopedAStatus::ok(); };
onInteractionDetected()55     ::ndk::ScopedAStatus onInteractionDetected() override { return ndk::ScopedAStatus::ok(); };
onAcquired(AcquiredInfo,int32_t)56     ndk::ScopedAStatus onAcquired(AcquiredInfo /*info*/, int32_t /*vendorCode*/) override {
57         return ndk::ScopedAStatus::ok();
58     }
onEnrollmentsEnumerated(const std::vector<int32_t> &)59     ::ndk::ScopedAStatus onEnrollmentsEnumerated(
60             const std::vector<int32_t>& /*enrollmentIds*/) override {
61         return ndk::ScopedAStatus::ok();
62     };
onEnrollmentsRemoved(const std::vector<int32_t> &)63     ::ndk::ScopedAStatus onEnrollmentsRemoved(
64             const std::vector<int32_t>& /*enrollmentIds*/) override {
65         return ndk::ScopedAStatus::ok();
66     };
onAuthenticatorIdRetrieved(int64_t)67     ::ndk::ScopedAStatus onAuthenticatorIdRetrieved(int64_t /*authenticatorId*/) override {
68         return ndk::ScopedAStatus::ok();
69     };
onAuthenticatorIdInvalidated(int64_t)70     ::ndk::ScopedAStatus onAuthenticatorIdInvalidated(int64_t /*authenticatorId*/) override {
71         return ndk::ScopedAStatus::ok();
72     };
onLockoutPermanent()73     ::ndk::ScopedAStatus onLockoutPermanent() override { return ndk::ScopedAStatus::ok(); };
onLockoutTimed(int64_t)74     ndk::ScopedAStatus onLockoutTimed(int64_t /* timeout */) override {
75         return ndk::ScopedAStatus::ok();
76     }
onLockoutCleared()77     ndk::ScopedAStatus onLockoutCleared() override { return ndk::ScopedAStatus::ok(); }
onSessionClosed()78     ndk::ScopedAStatus onSessionClosed() override {
79         mIsClosed = true;
80         return ndk::ScopedAStatus::ok();
81     }
82 
83     bool mIsClosed = false;
84 };
85 
86 class SessionTest : public ::testing::Test {
87   public:
SessionTest()88     SessionTest() : mWorker(2) {}
89 
90   protected:
SetUp()91     void SetUp() override {
92         mCb = ndk::SharedRefBase::make<TestSessionCallback>();
93         mSession = ndk::SharedRefBase::make<Session>(1, 2, mCb, &mFakeFingerprintEngine, &mWorker);
94         ASSERT_TRUE(mSession != nullptr);
95         mSession->linkToDeath(mCb->asBinder().get());
96     }
97 
TearDown()98     void TearDown() override {}
99 
100     std::shared_ptr<Session> mSession;
101     std::shared_ptr<TestSessionCallback> mCb;
102 
103   private:
104     FakeFingerprintEngine mFakeFingerprintEngine;
105     WorkerThread mWorker;
106 };
107 
TEST_F(SessionTest,close)108 TEST_F(SessionTest, close) {
109     ASSERT_TRUE(!mSession->isClosed());
110     ASSERT_TRUE(!mCb->mIsClosed);
111     onClientDeath(nullptr);
112     ASSERT_TRUE(!mSession->isClosed());
113     ASSERT_TRUE(!mCb->mIsClosed);
114     onClientDeath(static_cast<void*>(mSession.get()));
115     ASSERT_TRUE(mSession->isClosed());
116     ASSERT_TRUE(mCb->mIsClosed);
117 }
118 
119 }  //  namespace aidl::android::hardware::biometrics::fingerprint
120 
main(int argc,char ** argv)121 int main(int argc, char** argv) {
122     testing::InitGoogleTest(&argc, argv);
123     ABinderProcess_startThreadPool();
124     return RUN_ALL_TESTS();
125 }
126