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