• 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 #include "cjson_serializer.h"
17 #include "set_browser_policies_plugin_test.h"
18 #include "map_string_serializer.h"
19 #include "utils.h"
20 
21 using namespace testing::ext;
22 
23 namespace OHOS {
24 namespace EDM {
25 namespace TEST {
26 const std::string TEST_APP_ID = "test_app_id";
27 const std::string TEST_APP_ID_1 = "test_app_id_1";
28 const std::string TEST_POLICIES = "test_policies";
29 const std::string TEST_POLICY_NAME = "test_policy_name";
30 const std::string TEST_POLICY_VALUE = "\"test_policy_value\"";
31 const std::string TEST_POLICY_VALUE2 = "\"test_policy_value2\"";
32 const std::string TEST_POLICY_DATA1 = "{\"test_app_id\": {\"test_policy_name\":\"test_policy_value\"}}";
33 const std::string TEST_ADMIN_NAME1 = "testAdminName1";
SetUpTestSuite(void)34 void SetBrowserPoliciesPluginTest::SetUpTestSuite(void)
35 {
36     Utils::SetEdmInitialEnv();
37 }
38 
TearDownTestSuite(void)39 void SetBrowserPoliciesPluginTest::TearDownTestSuite(void)
40 {
41     Utils::ResetTokenTypeAndUid();
42     ASSERT_TRUE(Utils::IsOriginalUTEnv());
43     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
44 }
45 
46 /**
47  * @tc.name: TestOnSetPolicyDonePolicyChanged
48  * @tc.desc: Test SetBrowserPoliciesPlugin::OnSetPolicyDone when isGlobalChanged is true.
49  * @tc.type: FUNC
50  */
51 HWTEST_F(SetBrowserPoliciesPluginTest, TestOnSetPolicyDonePolicyChanged, TestSize.Level1)
52 {
53     SetBrowserPoliciesPlugin plugin;
54     std::string adminName = "";
55     int32_t userId = 0;
56     bool isGlobalChanged = true;
57     plugin.OnHandlePolicyDone(0, adminName, isGlobalChanged, userId);
58     ASSERT_TRUE(isGlobalChanged);
59 }
60 
61 /**
62  * @tc.name: TestOnSetPolicyDonePolicyUnchanged
63  * @tc.desc: Test SetBrowserPoliciesPlugin::OnSetPolicyDone when isGlobalChanged is false.
64  * @tc.type: FUNC
65  */
66 HWTEST_F(SetBrowserPoliciesPluginTest, TestOnSetPolicyDonePolicyUnchanged, TestSize.Level1)
67 {
68     SetBrowserPoliciesPlugin plugin;
69     std::string adminName = "";
70     int32_t userId = 0;
71     bool isGlobalChanged = false;
72     plugin.OnHandlePolicyDone(0, adminName, isGlobalChanged, userId);
73     ASSERT_FALSE(isGlobalChanged);
74 }
75 
76 /**
77  * @tc.name: TestOnGetPolicySuc
78  * @tc.desc: Test SetBrowserPoliciesPlugin::OnGetPolicy.
79  * @tc.type: FUNC
80  */
81 HWTEST_F(SetBrowserPoliciesPluginTest, TestOnGetPolicySuc, TestSize.Level1)
82 {
83     SetBrowserPoliciesPlugin plugin;
84     MessageParcel data;
85     MessageParcel reply;
86     data.WriteString(TEST_APP_ID);
87     std::string policyData = TEST_POLICY_DATA1;
88     plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
89     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
90 }
91 
92 /**
93  * @tc.name: TestSetPolicyEmpty
94  * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy
95  * and param is empty.
96  * @tc.type: FUNC
97  */
98 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyEmpty, TestSize.Level1)
99 {
100     SetBrowserPoliciesPlugin plugin;
101     MessageParcel data1;
102     MessageParcel data2;
103     MessageParcel reply;
104     HandlePolicyData policyData;
105     int32_t userid = 0;
106     std::vector<std::string> params1{"", TEST_POLICY_NAME, TEST_POLICY_VALUE};
107     data1.WriteStringVector(params1);
108 
109     ErrCode ret1 = plugin.OnHandlePolicy(0, data1, reply, policyData, userid);
110     ASSERT_TRUE(ret1 == EdmReturnErrCode::PARAM_ERROR);
111 }
112 
113 /**
114  * @tc.name: TestSetPolicy
115  * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy
116  * and normal conditions.
117  * @tc.type: FUNC
118  */
119 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicy, TestSize.Level1)
120 {
121     SetBrowserPoliciesPlugin plugin;
122     MessageParcel data;
123     MessageParcel reply;
124     int32_t userid = 0;
125     std::vector<std::string> params{TEST_APP_ID, TEST_POLICY_NAME, TEST_POLICY_VALUE2};
126     data.WriteStringVector(params);
127 
128     auto serializer = CjsonSerializer::GetInstance();
129 
130     HandlePolicyData policyData;
131     policyData.policyData = TEST_POLICY_DATA1;
132     plugin.OnHandlePolicy(0, data, reply, policyData, userid);
133     cJSON* policies;
134     serializer->Deserialize(policyData.policyData, policies);
135     cJSON* policy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
136     cJSON* policyValue =  cJSON_GetObjectItem(policy, TEST_POLICY_NAME.c_str());
137     std::string testStr;
138     serializer->Serialize(policyValue, testStr);
139     ASSERT_TRUE(testStr == TEST_POLICY_VALUE2);
140     cJSON_Delete(policies);
141 }
142 
143 /**
144  * @tc.name: TestSetPolicyEmptyValue
145  * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy
146  * and empty policyValue.
147  * @tc.type: FUNC
148  */
149 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyEmptyValue, TestSize.Level1)
150 {
151     SetBrowserPoliciesPlugin plugin;
152     MessageParcel data;
153     MessageParcel reply;
154     int32_t userid = 0;
155 
156     std::vector<std::string> params{TEST_APP_ID, TEST_POLICY_NAME, ""};
157     data.WriteStringVector(params);
158 
159     auto serializer = CjsonSerializer::GetInstance();
160 
161     HandlePolicyData policyData;
162     policyData.policyData = TEST_POLICY_DATA1;
163     plugin.OnHandlePolicy(0, data, reply, policyData, userid);
164     cJSON* policies;
165     serializer->Deserialize(policyData.policyData, policies);
166     cJSON* policy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
167     cJSON* policyValue =  cJSON_GetObjectItem(policy, TEST_POLICY_NAME.c_str());
168     ASSERT_TRUE(policyValue == nullptr);
169     cJSON_Delete(policies);
170 }
171 
172 /**
173  * @tc.name: TestSetPolicyRoot
174  * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy
175  * and policyName is root.
176  * @tc.type: FUNC
177  */
178 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyRoot, TestSize.Level1)
179 {
180     SetBrowserPoliciesPlugin plugin;
181     MessageParcel data;
182     MessageParcel reply;
183     int32_t userid = 0;
184     std::vector<std::string> params{TEST_APP_ID, "", TEST_POLICY_VALUE2};
185     data.WriteStringVector(params);
186 
187     auto serializer = CjsonSerializer::GetInstance();
188 
189     HandlePolicyData policyData;
190     policyData.policyData = TEST_POLICY_DATA1;
191     plugin.OnHandlePolicy(0, data, reply, policyData, userid);
192     cJSON* policies;
193     serializer->Deserialize(policyData.policyData, policies);
194     cJSON* policy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
195     std::string policyValue;
196     serializer->Serialize(policy, policyValue);
197     ASSERT_TRUE(policyValue == TEST_POLICY_VALUE2);
198     cJSON_Delete(policies);
199 }
200 
201 /**
202  * @tc.name: TestSetPolicyRootEmptyValue
203  * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy
204  * and policyName is root with empty policyValue.
205  * @tc.type: FUNC
206  */
207 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyRootEmptyValue, TestSize.Level1)
208 {
209     SetBrowserPoliciesPlugin plugin;
210     MessageParcel data;
211     MessageParcel reply;
212     int32_t userid = 0;
213     std::vector<std::string> params{TEST_APP_ID, "", ""};
214     data.WriteStringVector(params);
215 
216     auto serializer = CjsonSerializer::GetInstance();
217 
218     HandlePolicyData policyData;
219     policyData.policyData = TEST_POLICY_DATA1;
220     plugin.OnHandlePolicy(0, data, reply, policyData, userid);
221     cJSON* policies;
222     serializer->Deserialize(policyData.policyData, policies);
223     cJSON* policy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
224     ASSERT_TRUE(policy == nullptr);
225     cJSON_Delete(policies);
226 }
227 
228 /**
229  * @tc.name: TestMergePolicyData
230  * @tc.desc: Test SetBrowserPoliciesPlugin::MergePolicyData
231  * @tc.type: FUNC
232  */
233 HWTEST_F(SetBrowserPoliciesPluginTest, TestMergePolicyData, TestSize.Level1)
234 {
235     SetBrowserPoliciesPlugin plugin;
236     std::string adminName = TEST_ADMIN_NAME1;
237     std::string policyData = TEST_POLICY_DATA1;
238     int32_t userId100 = 100;
239     auto ret = plugin.GetOthersMergePolicyData(adminName, userId100, policyData);
240     ASSERT_TRUE(ret == ERR_OK);
241 }
242 
243 /**
244  * @tc.name: TestAddBrowserPoliciesToRootEmpty
245  * @tc.desc: Test SetBrowserPoliciesPlugin::AddBrowserPoliciesToRoot when policiesString is empty
246  * @tc.type: FUNC
247  */
248 HWTEST_F(SetBrowserPoliciesPluginTest, TestAddBrowserPoliciesToRootEmpty, TestSize.Level1)
249 {
250     SetBrowserPoliciesPlugin plugin;
251     cJSON* root = cJSON_CreateObject();
252     std::string policiesString;
253     auto ret = plugin.AddBrowserPoliciesToRoot(root, policiesString);
254     ASSERT_TRUE(ret);
255 }
256 
257 /**
258  * @tc.name: TestAddBrowserPoliciesToRootParseError
259  * @tc.desc: Test SetBrowserPoliciesPlugin::AddBrowserPoliciesToRoot when policiesString is parsed error
260  * @tc.type: FUNC
261  */
262 HWTEST_F(SetBrowserPoliciesPluginTest, TestAddBrowserPoliciesToRootParseError, TestSize.Level1)
263 {
264     SetBrowserPoliciesPlugin plugin;
265     cJSON* root = cJSON_CreateObject();
266     std::string policiesString = "{";
267     auto ret = plugin.AddBrowserPoliciesToRoot(root, policiesString);
268     ASSERT_FALSE(ret);
269 }
270 
271 /**
272  * @tc.name: TestAddBrowserPoliciesToRoot
273  * @tc.desc: Test SetBrowserPoliciesPlugin::AddBrowserPoliciesToRoot
274  * @tc.type: FUNC
275  */
276 HWTEST_F(SetBrowserPoliciesPluginTest, TestAddBrowserPoliciesToRoot, TestSize.Level1)
277 {
278     SetBrowserPoliciesPlugin plugin;
279     cJSON* root = cJSON_CreateObject();
280     cJSON_AddItemToObject(root, TEST_APP_ID.c_str(), cJSON_CreateObject());
281     std::string policiesString = TEST_POLICY_DATA1;
282     auto ret = plugin.AddBrowserPoliciesToRoot(root, policiesString);
283     ASSERT_TRUE(ret);
284 }
285 
286 } // namespace TEST
287 } // namespace EDM
288 } // namespace OHOS