xref: /aosp_15_r20/hardware/interfaces/biometrics/fingerprint/aidl/default/tests/FakeLockoutTrackerTest.cpp (revision 4d7e907c777eeecc4c5bd7cf640a754fac206ff7)
1 /*
2  * Copyright (C) 2022 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 "FakeLockoutTracker.h"
24 #include "Fingerprint.h"
25 #include "util/Util.h"
26 
27 using namespace ::android::fingerprint::virt;
28 using namespace ::aidl::android::hardware::biometrics::fingerprint;
29 
30 namespace aidl::android::hardware::biometrics::fingerprint {
31 
32 class FakeLockoutTrackerTest : public ::testing::Test {
33   protected:
34     static constexpr int32_t LOCKOUT_TIMED_THRESHOLD = 3;
35     static constexpr int32_t LOCKOUT_PERMANENT_THRESHOLD = 5;
36     static constexpr int32_t LOCKOUT_TIMED_DURATION = 100;
37 
SetUp()38     void SetUp() override {
39         Fingerprint::cfg().set<std::int32_t>("lockout_timed_threshold", LOCKOUT_TIMED_THRESHOLD);
40         Fingerprint::cfg().set<std::int32_t>("lockout_timed_duration", LOCKOUT_TIMED_DURATION);
41         Fingerprint::cfg().set<std::int32_t>("lockout_permanent_threshold",
42                                              LOCKOUT_PERMANENT_THRESHOLD);
43     }
44 
TearDown()45     void TearDown() override {
46         // reset to default
47         Fingerprint::cfg().set<std::int32_t>("lockout_timed_threshold", 5);
48         Fingerprint::cfg().set<std::int32_t>("lockout_timed_duration", 20);
49         Fingerprint::cfg().set<std::int32_t>("lockout_permanent_threshold", 10000);
50         Fingerprint::cfg().set<bool>("lockout_enable", false);
51         Fingerprint::cfg().set<bool>("lockout", false);
52     }
53 
54     FakeLockoutTracker mLockoutTracker;
55 };
56 
TEST_F(FakeLockoutTrackerTest,addFailedAttemptDisable)57 TEST_F(FakeLockoutTrackerTest, addFailedAttemptDisable) {
58     Fingerprint::cfg().set<bool>("lockout_enable", false);
59     for (int i = 0; i < LOCKOUT_TIMED_THRESHOLD + 1; i++) mLockoutTracker.addFailedAttempt();
60     ASSERT_EQ(mLockoutTracker.getMode(), FakeLockoutTracker::LockoutMode::kNone);
61     mLockoutTracker.reset();
62 }
63 
TEST_F(FakeLockoutTrackerTest,addFailedAttemptLockoutTimed)64 TEST_F(FakeLockoutTrackerTest, addFailedAttemptLockoutTimed) {
65     Fingerprint::cfg().set<bool>("lockout_enable", true);
66     for (int i = 0; i < LOCKOUT_TIMED_THRESHOLD; i++) mLockoutTracker.addFailedAttempt();
67     ASSERT_EQ(mLockoutTracker.getMode(), FakeLockoutTracker::LockoutMode::kTimed);
68     // time left
69     int N = 5;
70     int64_t prevTimeLeft = INT_MAX;
71     for (int i = 0; i < N; i++) {
72         SLEEP_MS(LOCKOUT_TIMED_DURATION / N + 1);
73         int64_t currTimeLeft = mLockoutTracker.getLockoutTimeLeft();
74         ASSERT_TRUE(currTimeLeft < prevTimeLeft);
75         prevTimeLeft = currTimeLeft;
76     }
77     ASSERT_EQ(mLockoutTracker.getMode(), FakeLockoutTracker::LockoutMode::kNone);
78     mLockoutTracker.reset(true);
79 }
80 
TEST_F(FakeLockoutTrackerTest,addFailedAttemptPermanent)81 TEST_F(FakeLockoutTrackerTest, addFailedAttemptPermanent) {
82     Fingerprint::cfg().set<bool>("lockout_enable", true);
83     for (int i = 0; i < LOCKOUT_PERMANENT_THRESHOLD - 1; i++) mLockoutTracker.addFailedAttempt();
84     ASSERT_NE(mLockoutTracker.getMode(), FakeLockoutTracker::LockoutMode::kPermanent);
85     mLockoutTracker.addFailedAttempt();
86     ASSERT_EQ(mLockoutTracker.getMode(), FakeLockoutTracker::LockoutMode::kPermanent);
87     ASSERT_TRUE(Fingerprint::cfg().get<bool>("lockout"));
88     mLockoutTracker.reset();
89 }
90 
91 }  // namespace aidl::android::hardware::biometrics::fingerprint
92 
main(int argc,char ** argv)93 int main(int argc, char** argv) {
94     testing::InitGoogleTest(&argc, argv);
95     ABinderProcess_startThreadPool();
96     return RUN_ALL_TESTS();
97 }
98