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 #include "cjson_serializer.h"
17 #include "edm_constants.h"
18 #include "set_browser_policies_plugin_test.h"
19 #include "map_string_serializer.h"
20 #include "utils.h"
21
22 using namespace testing::ext;
23
24 namespace OHOS {
25 namespace EDM {
26 namespace TEST {
27 const std::string TEST_APP_ID = "test_app_id";
28 const std::string TEST_APP_ID_1 = "test_app_id_1";
29 const std::string TEST_POLICIES = "test_policies";
30 const std::string TEST_POLICY_NAME = "test_policy_name";
31 const std::string TEST_POLICY_VALUE = "\"test_policy_value\"";
32 const std::string TEST_POLICY_VALUE2 = "\"test_policy_value2\"";
33 const std::string TEST_POLICY_DATA1 = "{\"test_app_id\": \"{\\\"test_policy_name\\\":\\\"test_policy_value\\\"}\"}";
34 static constexpr int32_t NULL_TYPE = 0;
SetUpTestSuite(void)35 void SetBrowserPoliciesPluginTest::SetUpTestSuite(void)
36 {
37 Utils::SetEdmInitialEnv();
38 }
39
TearDownTestSuite(void)40 void SetBrowserPoliciesPluginTest::TearDownTestSuite(void)
41 {
42 Utils::ResetTokenTypeAndUid();
43 ASSERT_TRUE(Utils::IsOriginalUTEnv());
44 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
45 }
46
47 /**
48 * @tc.name: TestSetPoliciesFail
49 * @tc.desc: Test SetBrowserPoliciesPlugin::OnHandlePolicy when type is null.
50 * @tc.type: FUNC
51 */
52 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPoliciesFail, TestSize.Level1)
53 {
54 SetBrowserPoliciesPlugin plugin;
55 MessageParcel data;
56 MessageParcel reply;
57 HandlePolicyData policyData;
58 int32_t userid = 0;
59 data.WriteInt32(NULL_TYPE);
60 std::vector<std::string> key{TEST_APP_ID};
61 std::vector<std::string> value{TEST_POLICIES};
62 data.WriteStringVector(key);
63 data.WriteStringVector(value);
64 ErrCode ret = plugin.OnHandlePolicy(0, data, reply, policyData, userid);
65 ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
66 }
67
68 /**
69 * @tc.name: TestSetPoliciesEmpty
70 * @tc.desc: Test SetBrowserPoliciesPlugin::OnHandlePolicy when type is SET_POLICIES_TYPE
71 * and policies is empty.
72 * @tc.type: FUNC
73 */
74 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPoliciesEmpty, TestSize.Level1)
75 {
76 SetBrowserPoliciesPlugin plugin;
77 MessageParcel data;
78 MessageParcel reply;
79 HandlePolicyData policyData;
80 int32_t userid = 0;
81 data.WriteInt32(EdmConstants::SET_POLICIES_TYPE);
82 std::vector<std::string> key;
83 std::vector<std::string> value;
84 data.WriteStringVector(key);
85 data.WriteStringVector(value);
86 ErrCode ret = plugin.OnHandlePolicy(0, data, reply, policyData, userid);
87 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
88 }
89
90 /**
91 * @tc.name: TestSetPoliciesValueEmpty
92 * @tc.desc: Test SetBrowserPoliciesPlugin::OnHandlePolicy when type is SET_POLICIES_TYPE
93 * and policies value is empty.
94 * @tc.type: FUNC
95 */
96 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPoliciesValueEmpty, TestSize.Level1)
97 {
98 SetBrowserPoliciesPlugin plugin;
99 MessageParcel data;
100 MessageParcel reply;
101 HandlePolicyData policyData;
102 int32_t userid = 0;
103 data.WriteInt32(EdmConstants::SET_POLICIES_TYPE);
104 std::vector<std::string> key{TEST_APP_ID};
105 std::vector<std::string> value{""};
106 data.WriteStringVector(key);
107 data.WriteStringVector(value);
108 ErrCode ret = plugin.OnHandlePolicy(0, data, reply, policyData, userid);
109 ASSERT_TRUE(ret == ERR_OK);
110 ASSERT_TRUE(policyData.policyData == "");
111 }
112
113 /**
114 * @tc.name: TestSetPoliciesSuc
115 * @tc.desc: Test SetBrowserPoliciesPlugin::OnHandlePolicy when type is SET_POLICIES_TYPE.
116 * @tc.type: FUNC
117 */
118 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPoliciesSuc, TestSize.Level1)
119 {
120 SetBrowserPoliciesPlugin plugin;
121 MessageParcel data;
122 MessageParcel reply;
123 HandlePolicyData policyData;
124 int32_t userid = 0;
125 data.WriteInt32(EdmConstants::SET_POLICIES_TYPE);
126 std::vector<std::string> key{TEST_APP_ID};
127 std::vector<std::string> value{TEST_POLICIES};
128 data.WriteStringVector(key);
129 data.WriteStringVector(value);
130 ErrCode ret = plugin.OnHandlePolicy(0, data, reply, policyData, userid);
131 ASSERT_TRUE(ret == ERR_OK);
132 auto serializer_ = MapStringSerializer::GetInstance();
133 std::map<std::string, std::string> currentData;
134 serializer_ ->Deserialize(policyData.policyData, currentData);
135 ASSERT_TRUE(currentData[TEST_APP_ID] == TEST_POLICIES);
136 }
137
138 /**
139 * @tc.name: TestOnSetPolicyDonePolicyChanged
140 * @tc.desc: Test SetBrowserPoliciesPlugin::OnSetPolicyDone when isGlobalChanged is true.
141 * @tc.type: FUNC
142 */
143 HWTEST_F(SetBrowserPoliciesPluginTest, TestOnSetPolicyDonePolicyChanged, TestSize.Level1)
144 {
145 SetBrowserPoliciesPlugin plugin;
146 std::string adminName = "";
147 int32_t userId = 0;
148 bool isGlobalChanged = true;
149 plugin.OnHandlePolicyDone(0, adminName, isGlobalChanged, userId);
150 ASSERT_TRUE(isGlobalChanged);
151 }
152
153 /**
154 * @tc.name: TestOnSetPolicyDonePolicyUnchanged
155 * @tc.desc: Test SetBrowserPoliciesPlugin::OnSetPolicyDone when isGlobalChanged is false.
156 * @tc.type: FUNC
157 */
158 HWTEST_F(SetBrowserPoliciesPluginTest, TestOnSetPolicyDonePolicyUnchanged, TestSize.Level1)
159 {
160 SetBrowserPoliciesPlugin plugin;
161 std::string adminName = "";
162 int32_t userId = 0;
163 bool isGlobalChanged = false;
164 plugin.OnHandlePolicyDone(0, adminName, isGlobalChanged, userId);
165 ASSERT_FALSE(isGlobalChanged);
166 }
167
168 /**
169 * @tc.name: TestOnGetPolicyAppIdEmpty
170 * @tc.desc: Test SetBrowserPoliciesPlugin::OnGetPolicy when appId is empty.
171 * @tc.type: FUNC
172 */
173 HWTEST_F(SetBrowserPoliciesPluginTest, TestOnGetPolicyAppIdEmpty, TestSize.Level1)
174 {
175 SetBrowserPoliciesPlugin plugin;
176 std::map<std::string, std::string> policies;
177 policies.insert(std::make_pair(TEST_APP_ID, TEST_POLICIES));
178 std::string jsonString;
179 MapStringSerializer::GetInstance()->Serialize(policies, jsonString);
180 std::map<std::string, std::string> currentData;
181 MessageParcel data;
182 MessageParcel reply;
183 data.WriteString(TEST_APP_ID_1);
184 plugin.OnGetPolicy(jsonString, data, reply, DEFAULT_USER_ID);
185 ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
186 ASSERT_TRUE(reply.ReadString().empty());
187 }
188
189 /**
190 * @tc.name: TestOnGetPolicySuc
191 * @tc.desc: Test SetBrowserPoliciesPlugin::OnGetPolicy.
192 * @tc.type: FUNC
193 */
194 HWTEST_F(SetBrowserPoliciesPluginTest, TestOnGetPolicySuc, TestSize.Level1)
195 {
196 SetBrowserPoliciesPlugin plugin;
197 std::map<std::string, std::string> policies;
198 policies.insert(std::make_pair(TEST_APP_ID, TEST_POLICIES));
199 std::string jsonString;
200 MapStringSerializer::GetInstance()->Serialize(policies, jsonString);
201 std::map<std::string, std::string> currentData;
202 MessageParcel data;
203 MessageParcel reply;
204 data.WriteString(TEST_APP_ID);
205 plugin.OnGetPolicy(jsonString, data, reply, DEFAULT_USER_ID);
206 ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
207 ASSERT_TRUE(reply.ReadString() == TEST_POLICIES);
208 }
209
210 /**
211 * @tc.name: TestSetPolicyEmpty
212 * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy when type is SET_POLICY_TYPE
213 * and param is empty.
214 * @tc.type: FUNC
215 */
216 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyEmpty, TestSize.Level1)
217 {
218 SetBrowserPoliciesPlugin plugin;
219 MessageParcel data1;
220 MessageParcel data2;
221 MessageParcel reply;
222 HandlePolicyData policyData;
223 int32_t userid = 0;
224 std::vector<std::string> params1{"", TEST_POLICY_NAME, TEST_POLICY_VALUE};
225 data1.WriteInt32(EdmConstants::SET_POLICY_TYPE);
226 data1.WriteStringVector(params1);
227
228 ErrCode ret1 = plugin.OnHandlePolicy(0, data1, reply, policyData, userid);
229 ASSERT_TRUE(ret1 == EdmReturnErrCode::PARAM_ERROR);
230 }
231
232 /**
233 * @tc.name: TestSetPolicy
234 * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy when type is SET_POLICY_TYPE
235 * and normal conditions.
236 * @tc.type: FUNC
237 */
238 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicy, TestSize.Level1)
239 {
240 SetBrowserPoliciesPlugin plugin;
241 MessageParcel data;
242 MessageParcel reply;
243 int32_t userid = 0;
244 std::vector<std::string> params{TEST_APP_ID, TEST_POLICY_NAME, TEST_POLICY_VALUE2};
245 data.WriteInt32(EdmConstants::SET_POLICY_TYPE);
246 data.WriteStringVector(params);
247
248 auto serializer_ = CjsonSerializer::GetInstance();
249
250 HandlePolicyData policyData;
251 policyData.policyData = TEST_POLICY_DATA1;
252 plugin.OnHandlePolicy(0, data, reply, policyData, userid);
253 cJSON* policies;
254 serializer_->Deserialize(policyData.policyData, policies);
255 cJSON* beforeParsedPolicy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
256 cJSON* policy;
257 serializer_->Deserialize(cJSON_GetStringValue(beforeParsedPolicy), policy);
258 cJSON* policyValue = cJSON_GetObjectItem(policy, TEST_POLICY_NAME.c_str());
259 char *cJsonstr = cJSON_Print(policyValue);
260 std::string testStr;
261 if (cJsonstr != nullptr) {
262 testStr = std::string(cJsonstr);
263 cJSON_free(cJsonstr);
264 }
265 ASSERT_TRUE(testStr == TEST_POLICY_VALUE2);
266 cJSON_Delete(policies);
267 cJSON_Delete(policy);
268 }
269
270 /**
271 * @tc.name: TestSetPolicyEmptyValue
272 * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy when type is SET_POLICY_TYPE
273 * and empty policyValue.
274 * @tc.type: FUNC
275 */
276 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyEmptyValue, TestSize.Level1)
277 {
278 SetBrowserPoliciesPlugin plugin;
279 MessageParcel data;
280 MessageParcel reply;
281 int32_t userid = 0;
282
283 std::vector<std::string> params{TEST_APP_ID, TEST_POLICY_NAME, ""};
284 data.WriteInt32(EdmConstants::SET_POLICY_TYPE);
285 data.WriteStringVector(params);
286
287 auto serializer_ = CjsonSerializer::GetInstance();
288
289 HandlePolicyData policyData;
290 policyData.policyData = TEST_POLICY_DATA1;
291 plugin.OnHandlePolicy(0, data, reply, policyData, userid);
292 cJSON* policies;
293 serializer_->Deserialize(policyData.policyData, policies);
294 cJSON* beforeParsedPolicy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
295 cJSON* policy;
296 serializer_->Deserialize(cJSON_GetStringValue(beforeParsedPolicy), policy);
297 cJSON* policyValue = cJSON_GetObjectItem(policy, TEST_POLICY_NAME.c_str());
298 ASSERT_TRUE(policyValue == nullptr);
299 cJSON_Delete(policies);
300 cJSON_Delete(policy);
301 }
302
303 /**
304 * @tc.name: TestSetPolicyRoot
305 * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy when type is SET_POLICY_TYPE
306 * and policyName is root.
307 * @tc.type: FUNC
308 */
309 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyRoot, TestSize.Level1)
310 {
311 SetBrowserPoliciesPlugin plugin;
312 MessageParcel data;
313 MessageParcel reply;
314 int32_t userid = 0;
315 std::vector<std::string> params{TEST_APP_ID, "", TEST_POLICY_VALUE2};
316 data.WriteInt32(EdmConstants::SET_POLICY_TYPE);
317 data.WriteStringVector(params);
318
319 auto serializer_ = CjsonSerializer::GetInstance();
320
321 HandlePolicyData policyData;
322 policyData.policyData = TEST_POLICY_DATA1;
323 plugin.OnHandlePolicy(0, data, reply, policyData, userid);
324 cJSON* policies;
325 serializer_->Deserialize(policyData.policyData, policies);
326 cJSON* policy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
327 ASSERT_TRUE(cJSON_GetStringValue(policy) == TEST_POLICY_VALUE2);
328 cJSON_Delete(policies);
329 }
330
331 /**
332 * @tc.name: TestSetPolicyRootEmptyValue
333 * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy when type is SET_POLICY_TYPE
334 * and policyName is root with empty policyValue.
335 * @tc.type: FUNC
336 */
337 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyRootEmptyValue, TestSize.Level1)
338 {
339 SetBrowserPoliciesPlugin plugin;
340 MessageParcel data;
341 MessageParcel reply;
342 int32_t userid = 0;
343 std::vector<std::string> params{TEST_APP_ID, "", ""};
344 data.WriteInt32(EdmConstants::SET_POLICY_TYPE);
345 data.WriteStringVector(params);
346
347 auto serializer_ = CjsonSerializer::GetInstance();
348
349 HandlePolicyData policyData;
350 policyData.policyData = TEST_POLICY_DATA1;
351 plugin.OnHandlePolicy(0, data, reply, policyData, userid);
352 cJSON* policies;
353 serializer_->Deserialize(policyData.policyData, policies);
354 cJSON* policy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
355 ASSERT_TRUE(policy == nullptr);
356 cJSON_Delete(policies);
357 }
358
359 } // namespace TEST
360 } // namespace EDM
361 } // namespace OHOS