• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #define private public
17 #include "fingerprint_auth_plugin.h"
18 #undef private
19 
20 #include <gtest/gtest.h>
21 
22 #include "edm_ipc_interface_code.h"
23 #include "utils.h"
24 
25 using namespace testing::ext;
26 using namespace testing;
27 
28 namespace OHOS {
29 namespace EDM {
30 namespace TEST {
31 const std::string PERSIST_FINGERPRINT_AUTH_CONTROL = "persist.useriam.enable.fingerprintauth";
32 class FingerprintAuthPluginTest : public testing::Test {
33 protected:
34     static void SetUpTestSuite(void);
35 
36     static void TearDownTestSuite(void);
37 };
38 
SetUpTestSuite(void)39 void FingerprintAuthPluginTest::SetUpTestSuite(void)
40 {
41     Utils::SetEdmInitialEnv();
42     setegid(Utils::USERIAM_UID);
43     ASSERT_TRUE(getegid() == Utils::USERIAM_UID);
44 }
45 
TearDownTestSuite(void)46 void FingerprintAuthPluginTest::TearDownTestSuite(void)
47 {
48     Utils::ResetTokenTypeAndUid();
49     setegid(Utils::EDM_UID);
50     ASSERT_TRUE(getegid() == Utils::EDM_UID);
51     ASSERT_TRUE(Utils::IsOriginalUTEnv());
52     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
53 }
54 
55 /**
56  * @tc.name: TestOnHandlePolicyFingerprintAuthType
57  * @tc.desc: Test FingerprintAuthPluginTest::OnHandlePolicy function.
58  * @tc.type: FUNC
59  */
60 HWTEST_F(FingerprintAuthPluginTest, TestOnHandlePolicyFingerprintAuthType, TestSize.Level1)
61 {
62     MessageParcel data;
63     data.WriteString(EdmConstants::FINGERPRINT_AUTH_TYPE);
64     data.WriteBool(false);
65     MessageParcel reply;
66     HandlePolicyData policyData;
67     policyData.policyData = "true";
68     FingerprintAuthPlugin plugin;
69     ErrCode ret = plugin.OnHandlePolicy(0, data, reply, policyData, 100);
70     ASSERT_TRUE(ret == ERR_OK);
71     ASSERT_TRUE(policyData.policyData.empty());
72 }
73 
74 /**
75  * @tc.name: TestOnHandlePolicyDisallowForAccountType
76  * @tc.desc: Test FingerprintAuthPluginTest::OnHandlePolicy function.
77  * @tc.type: FUNC
78  */
79 HWTEST_F(FingerprintAuthPluginTest, TestOnHandlePolicyDisallowForAccountType, TestSize.Level1)
80 {
81     MessageParcel data;
82     data.WriteString(EdmConstants::DISALLOW_FOR_ACCOUNT_TYPE);
83     data.WriteBool(false);
84     data.WriteInt32(100);
85     MessageParcel reply;
86     HandlePolicyData policyData;
87     policyData.policyData = "[100]";
88     FingerprintAuthPlugin plugin;
89     ErrCode ret = plugin.OnHandlePolicy(0, data, reply, policyData, 100);
90     ASSERT_TRUE(ret == ERR_OK);
91     ASSERT_TRUE(policyData.policyData.empty());
92 }
93 
94 /**
95  * @tc.name: TestHandleFingerprintAuthPolicy
96  * @tc.desc: Test FingerprintAuthPluginTest::HandleFingerprintAuthPolicy function.
97  * @tc.type: FUNC
98  */
99 HWTEST_F(FingerprintAuthPluginTest, TestHandleFingerprintAuthPolicy, TestSize.Level1)
100 {
101     FingerprintAuthPlugin plugin;
102     FingerprintPolicy policy;
103     FingerprintPolicy mergePolicy;
104     policy.accountIds.insert(100);
105     ErrCode ret = plugin.HandleFingerprintAuthPolicy(true, policy, mergePolicy);
106     ASSERT_TRUE(ret == ERR_OK);
107     ASSERT_TRUE(policy.accountIds.empty());
108     ASSERT_TRUE(policy.globalDisallow);
109 
110     policy.accountIds.insert(100);
111     policy.globalDisallow = false;
112     ret = plugin.HandleFingerprintAuthPolicy(false, policy, mergePolicy);
113     ASSERT_TRUE(ret == EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED);
114 
115     policy.accountIds.clear();
116     ret = plugin.HandleFingerprintAuthPolicy(false, policy, mergePolicy);
117     ASSERT_TRUE(ret == ERR_OK);
118     ASSERT_TRUE(policy.accountIds.empty());
119     ASSERT_FALSE(policy.globalDisallow);
120 }
121 
122 /**
123  * @tc.name: TestHandleFingerprintForAccountPolicy
124  * @tc.desc: Test FingerprintAuthPluginTest::HandleFingerprintForAccountPolicy function.
125  * @tc.type: FUNC
126  */
127 HWTEST_F(FingerprintAuthPluginTest, TestHandleFingerprintForAccountPolicy, TestSize.Level1)
128 {
129     FingerprintAuthPlugin plugin;
130     FingerprintPolicy policy;
131     FingerprintPolicy mergePolicy;
132     policy.globalDisallow = true;
133     ErrCode ret = plugin.HandleFingerprintForAccountPolicy(true, 100, policy, mergePolicy);
134     ASSERT_TRUE(ret == EdmReturnErrCode::CONFIGURATION_CONFLICT_FAILED);
135 
136     policy.globalDisallow = false;
137     ret = plugin.HandleFingerprintForAccountPolicy(true, 100, policy, mergePolicy);
138     ASSERT_TRUE(ret == ERR_OK);
139     ASSERT_TRUE(policy.accountIds.find(100) != policy.accountIds.end());
140     ASSERT_FALSE(policy.globalDisallow);
141 
142     ret = plugin.HandleFingerprintForAccountPolicy(false, 100, policy, mergePolicy);
143     ASSERT_TRUE(ret == ERR_OK);
144     ASSERT_TRUE(policy.accountIds.empty());
145     ASSERT_FALSE(policy.globalDisallow);
146 }
147 
148 } // namespace TEST
149 } // namespace EDM
150 } // namespace OHOS