• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 #include <gtest/gtest.h>
17 #include <string>
18 #include <vector>
19 #include "cmd_utils.h"
20 #include "policy_manager.h"
21 #include "array_string_serializer.h"
22 
23 using namespace testing::ext;
24 
25 namespace OHOS {
26 namespace EDM {
27 namespace TEST {
28 const std::string TEST_ADMIN_NAME = "com.edm.test.demo";
29 const std::string TEST_ADMIN_NAME1 = "com.edm.test.demo1";
30 const std::string TEST_BOOL_POLICY_NAME = "testBoolPolicy";
31 const std::string TEST_STRING_POLICY_NAME = "testStringPolicy";
32 constexpr int HUGE_POLICY_SIZE = 65537;
33 const std::string TEAR_DOWN_CMD = "rm /data/service/el1/public/edm/device_policies.json";
34 
35 class PolicyManagerTest : public testing::Test {
36 public:
SetUpTestCase()37     static void SetUpTestCase()
38     {
39         PolicyManager::GetInstance()->Init();
40     }
41 
42 protected:
TearDown()43     void TearDown() override
44     {
45         PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME, TEST_BOOL_POLICY_NAME, "", "");
46         PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME1, TEST_BOOL_POLICY_NAME, "", "");
47 
48         PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME, TEST_STRING_POLICY_NAME, "", "");
49         PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME1, TEST_STRING_POLICY_NAME, "", "");
50 
51         CmdUtils::ExecCmdSync(TEAR_DOWN_CMD);
52     }
53 };
54 
55 /**
56  * @tc.name: TestSetPolicy
57  * @tc.desc: Test PolicyManager SetPolicy func.
58  * @tc.type: FUNC
59  */
60 HWTEST_F(PolicyManagerTest, TestSetPolicy, TestSize.Level1)
61 {
62     ErrCode res;
63     std::string adminName = TEST_ADMIN_NAME;
64     std::string policyName = TEST_BOOL_POLICY_NAME;
65     std::string adminName1 = TEST_ADMIN_NAME1;
66     std::string adminPolicy;
67     std::string mergedPolicy;
68     res = PolicyManager::GetInstance()->SetPolicy(adminName, policyName, "false", "true");
69     ASSERT_TRUE(res == ERR_OK);
70 
71     std::string policyValue;
72     res = PolicyManager::GetInstance()->GetPolicy(adminName, policyName, policyValue);
73     ASSERT_TRUE(res == ERR_OK);
74     ASSERT_TRUE(policyValue == "false");
75     res = PolicyManager::GetInstance()->GetPolicy("", policyName, policyValue);
76     ASSERT_TRUE(res == ERR_OK);
77     ASSERT_TRUE(policyValue == "true");
78 
79     res = PolicyManager::GetInstance()->SetPolicy(adminName1, policyName, "true", "true");
80     ASSERT_TRUE(res == ERR_OK);
81     res = PolicyManager::GetInstance()->GetPolicy(adminName, policyName, policyValue);
82     ASSERT_TRUE(res == ERR_OK);
83     ASSERT_TRUE(policyValue == "false");
84     res = PolicyManager::GetInstance()->GetPolicy("", policyName, policyValue);
85     ASSERT_TRUE(res == ERR_OK);
86     ASSERT_TRUE(policyValue == "true");
87 }
88 
89 /**
90  * @tc.name: TestGetPolicy
91  * @tc.desc: Test PolicyManager GetPolicy func.
92  * @tc.type: FUNC
93  */
94 HWTEST_F(PolicyManagerTest, TestGetPolicy, TestSize.Level1)
95 {
96     ErrCode res;
97     std::string adminName = TEST_ADMIN_NAME;
98     std::string policyName = TEST_STRING_POLICY_NAME;
99     std::string adminPolicy = "adminPolicy";
100     std::string mergedPolicy = "mergedValue";
101     res = PolicyManager::GetInstance()->SetPolicy(adminName, policyName, adminPolicy, mergedPolicy);
102     ASSERT_TRUE(res == ERR_OK);
103 
104     std::string policyValue;
105     res = PolicyManager::GetInstance()->GetPolicy(adminName, policyName, policyValue);
106     ASSERT_TRUE(res == ERR_OK);
107     ASSERT_TRUE(policyValue == "adminPolicy");
108     res = PolicyManager::GetInstance()->GetPolicy("", policyName, policyValue);
109     ASSERT_TRUE(res == ERR_OK);
110     ASSERT_TRUE(policyValue == "mergedValue");
111 
112     res = PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME1, policyName, "", "mergedValue1");
113     ASSERT_TRUE(res == ERR_OK);
114 
115     res = PolicyManager::GetInstance()->GetPolicy(adminName, policyName, policyValue);
116     ASSERT_TRUE(res == ERR_OK);
117     ASSERT_TRUE(policyValue == "adminPolicy");
118     res = PolicyManager::GetInstance()->GetPolicy("", policyName, policyValue);
119     ASSERT_TRUE(res == ERR_OK);
120     ASSERT_TRUE(policyValue == "mergedValue1");
121 }
122 
123 /**
124  * @tc.name: GetAllPolicyByAdmin
125  * @tc.desc: Test PolicyManager GetAllPolicyByAdmin func.
126  * @tc.type: FUNC
127  */
128 HWTEST_F(PolicyManagerTest, TestGetAdminAllPolicy, TestSize.Level1)
129 {
130     ErrCode res;
131     res = PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME, TEST_STRING_POLICY_NAME,
132         "adminPolicy", "mergedValue");
133     ASSERT_TRUE(res == ERR_OK);
134     res = PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME, TEST_BOOL_POLICY_NAME,
135         "true", "true");
136     ASSERT_TRUE(res == ERR_OK);
137 
138     std::unordered_map<std::string, std::string> allPolicyValue;
139     res = PolicyManager::GetInstance()->GetAllPolicyByAdmin(TEST_ADMIN_NAME, allPolicyValue);
140     ASSERT_TRUE(res == ERR_OK);
141     ASSERT_TRUE(allPolicyValue.size() == 2);
142     auto stringEntry = allPolicyValue.find(TEST_STRING_POLICY_NAME);
143     ASSERT_TRUE(stringEntry != allPolicyValue.end() && stringEntry->second == "adminPolicy");
144     auto boolEntry = allPolicyValue.find(TEST_BOOL_POLICY_NAME);
145     ASSERT_TRUE(boolEntry != allPolicyValue.end() && boolEntry->second == "true");
146 }
147 
148 /**
149  * @tc.name: TestGetAdminByPolicyName
150  * @tc.desc: Test PolicyManager GetAdminByPolicyName func.
151  * @tc.type: FUNC
152  */
153 HWTEST_F(PolicyManagerTest, TestGetAdminByPolicyName, TestSize.Level1)
154 {
155     ErrCode res;
156     res = PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME, TEST_BOOL_POLICY_NAME,
157         "false", "true");
158     ASSERT_TRUE(res == ERR_OK);
159     res = PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME1, TEST_BOOL_POLICY_NAME,
160         "true", "true");
161     ASSERT_TRUE(res == ERR_OK);
162 
163     std::unordered_map<std::string, std::string> adminPolicyValue;
164     res = PolicyManager::GetInstance()->GetAdminByPolicyName(TEST_BOOL_POLICY_NAME, adminPolicyValue);
165     ASSERT_TRUE(res == ERR_OK);
166     ASSERT_TRUE(adminPolicyValue.size() == 2);
167     auto adminEntry = adminPolicyValue.find(TEST_ADMIN_NAME);
168     ASSERT_TRUE(adminEntry != adminPolicyValue.end() && adminEntry->second == "false");
169     auto adminEntry1 = adminPolicyValue.find(TEST_ADMIN_NAME1);
170     ASSERT_TRUE(adminEntry1 != adminPolicyValue.end() && adminEntry1->second == "true");
171 }
172 
173 /**
174  * @tc.name: TestSetPolicyHuge
175  * @tc.desc: Test PolicyManager SetPolicy func.
176  * @tc.type: FUNC
177  */
178 HWTEST_F(PolicyManagerTest, TestSetPolicyHuge, TestSize.Level1)
179 {
180     std::vector<std::string> hugeArrayPolicy;
181     for (int i = 0; i < HUGE_POLICY_SIZE; ++i) {
182         std::string s1 = "hugeArrayPolicyValue:" + std::to_string(i);
183         hugeArrayPolicy.emplace_back(s1);
184     }
185     ErrCode res;
186     std::string policyValue;
187     ASSERT_TRUE(ArrayStringSerializer::GetInstance()->Serialize(hugeArrayPolicy, policyValue));
188     res = PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME, TEST_STRING_POLICY_NAME,
189         policyValue, policyValue);
190     ASSERT_TRUE(res == ERR_OK);
191     res = PolicyManager::GetInstance()->SetPolicy(TEST_ADMIN_NAME1, TEST_STRING_POLICY_NAME,
192         policyValue, policyValue);
193     ASSERT_TRUE(res == ERR_OK);
194 }
195 } // namespace TEST
196 } // namespace EDM
197 } // namespace OHOS
198