1 /*
2  * Copyright (c) 2024 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 #include "password_policy_plugin_test.h"
16 #include "password_policy_serializer.h"
17 #include "utils.h"
18 
19 using namespace testing::ext;
20 
21 namespace OHOS {
22 namespace EDM {
23 namespace TEST {
24 const std::string TEST_POLICY_DATA = "{\"complexityReg\":\"^(?=.*[a-zA-Z]).{1,9}$\", \"validityPeriod\": 2,"
25     "\"additionalDescription\": \"testDescription\"}";
26 const std::string TEST_POLICY_ERR_DATA = "{\"comple\":\"^(?=.*[a-zA-Z]).{1,9}$\", \"validityPeriod\": 2,"
27     "\"additionalDescription\": \"testDescription\"}";
28 const std::string TEST_VALUE_COMPLEXITYREG = "^(?=.*[a-zA-Z]).{1,9}$";
29 const int TEST_VALUE_VALIDITY_PERIOD = 2;
30 const std::string TEST_VALUE_ADDITIONAL_DESCRIPTION = "testDescription";
SetUpTestSuite(void)31 void PasswordPolicyPluginTest::SetUpTestSuite(void)
32 {
33     Utils::SetEdmInitialEnv();
34 }
35 
TearDownTestSuite(void)36 void PasswordPolicyPluginTest::TearDownTestSuite(void)
37 {
38     Utils::ResetTokenTypeAndUid();
39     ASSERT_TRUE(Utils::IsOriginalUTEnv());
40     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
41 }
42 
43 /**
44  * @tc.name: TestOnGetPolicyEmpty
45  * @tc.desc: Test PasswordPolicyPlugin::OnGetPolicy when policyData is empty
46  * and policies is empty.
47  * @tc.type: FUNC
48  */
49 HWTEST_F(PasswordPolicyPluginTest, TestOnGetPolicyEmpty, TestSize.Level1)
50 {
51     PasswordPolicyPlugin plugin;
52     MessageParcel data;
53     MessageParcel reply;
54     std::string policyData;
55     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, 0);
56     ASSERT_TRUE(ret == ERR_OK);
57     ASSERT_TRUE(policyData.empty());
58 }
59 
60 /**
61  * @tc.name: TestOnGetPolicyWithErrData
62  * @tc.desc: Test PasswordPolicyPlugin::OnGetPolicy when policyData is err
63  * and policies is err.
64  * @tc.type: FUNC
65  */
66 HWTEST_F(PasswordPolicyPluginTest, TestOnGetPolicyWithErrData, TestSize.Level1)
67 {
68     PasswordPolicyPlugin plugin;
69     MessageParcel data;
70     MessageParcel reply;
71     std::string policyData = TEST_POLICY_ERR_DATA;
72     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, 0);
73     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
74 }
75 
76 /**
77  * @tc.name: TestOnGetPolicy
78  * @tc.desc: Test PasswordPolicyPlugin::OnGetPolicy
79  * and policies is empty.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(PasswordPolicyPluginTest, TestOnGetPolicy, TestSize.Level1)
83 {
84     PasswordPolicyPlugin plugin;
85     MessageParcel data;
86     MessageParcel reply;
87     std::string policyData = TEST_POLICY_DATA;
88     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, 0);
89     ASSERT_TRUE(ret == ERR_OK);
90     auto serializer_ = PasswordSerializer::GetInstance();
91     PasswordPolicy policy;
92     serializer_->Deserialize(policyData, policy);
93     ASSERT_TRUE(policy.additionalDescription == TEST_VALUE_ADDITIONAL_DESCRIPTION);
94     ASSERT_TRUE(policy.validityPeriod == TEST_VALUE_VALIDITY_PERIOD);
95     ASSERT_TRUE(policy.complexityReg == TEST_VALUE_COMPLEXITYREG);
96 }
97 
98 /**
99  * @tc.name: TestOnSetPolicyEmpty
100  * @tc.desc: Test PasswordPolicyPlugin::OnSetPolicy
101  * @tc.type: FUNC
102  */
103 HWTEST_F(PasswordPolicyPluginTest, TestOnSetPolicy, TestSize.Level1)
104 {
105     PasswordPolicyPlugin plugin;
106     PasswordPolicy policy;
107     ErrCode ret = plugin.OnSetPolicy(policy);
108     ASSERT_TRUE(ret == ERR_OK);
109 }
110 } // namespace TEST
111 } // namespace EDM
112 } // namespace OHOS