• 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: TestSetPolicyEmpty
78  * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy
79  * and param is empty.
80  * @tc.type: FUNC
81  */
82 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyEmpty, TestSize.Level1)
83 {
84     SetBrowserPoliciesPlugin plugin;
85     MessageParcel data1;
86     MessageParcel data2;
87     MessageParcel reply;
88     HandlePolicyData policyData;
89     int32_t userid = 0;
90     std::vector<std::string> params1{"", TEST_POLICY_NAME, TEST_POLICY_VALUE};
91     data1.WriteStringVector(params1);
92 
93     ErrCode ret1 = plugin.OnHandlePolicy(0, data1, reply, policyData, userid);
94     ASSERT_TRUE(ret1 == EdmReturnErrCode::PARAM_ERROR);
95 }
96 
97 /**
98  * @tc.name: TestSetPolicy
99  * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy
100  * and normal conditions.
101  * @tc.type: FUNC
102  */
103 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicy, TestSize.Level1)
104 {
105     SetBrowserPoliciesPlugin plugin;
106     MessageParcel data;
107     MessageParcel reply;
108     int32_t userid = 0;
109     std::vector<std::string> params{TEST_APP_ID, TEST_POLICY_NAME, TEST_POLICY_VALUE2};
110     data.WriteStringVector(params);
111 
112     auto serializer = CjsonSerializer::GetInstance();
113 
114     HandlePolicyData policyData;
115     policyData.policyData = TEST_POLICY_DATA1;
116     plugin.OnHandlePolicy(0, data, reply, policyData, userid);
117     cJSON* policies;
118     serializer->Deserialize(policyData.policyData, policies);
119     cJSON* policy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
120     cJSON* policyValue =  cJSON_GetObjectItem(policy, TEST_POLICY_NAME.c_str());
121     std::string testStr;
122     serializer->Serialize(policyValue, testStr);
123     ASSERT_TRUE(testStr == TEST_POLICY_VALUE2);
124     cJSON_Delete(policies);
125 }
126 
127 /**
128  * @tc.name: TestSetPolicyEmptyValue
129  * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy
130  * and empty policyValue.
131  * @tc.type: FUNC
132  */
133 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyEmptyValue, TestSize.Level1)
134 {
135     SetBrowserPoliciesPlugin plugin;
136     MessageParcel data;
137     MessageParcel reply;
138     int32_t userid = 0;
139 
140     std::vector<std::string> params{TEST_APP_ID, TEST_POLICY_NAME, ""};
141     data.WriteStringVector(params);
142 
143     auto serializer = CjsonSerializer::GetInstance();
144 
145     HandlePolicyData policyData;
146     policyData.policyData = TEST_POLICY_DATA1;
147     plugin.OnHandlePolicy(0, data, reply, policyData, userid);
148     cJSON* policies;
149     serializer->Deserialize(policyData.policyData, policies);
150     cJSON* policy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
151     cJSON* policyValue =  cJSON_GetObjectItem(policy, TEST_POLICY_NAME.c_str());
152     ASSERT_TRUE(policyValue == nullptr);
153     cJSON_Delete(policies);
154 }
155 
156 /**
157  * @tc.name: TestSetPolicyRoot
158  * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy
159  * and policyName is root.
160  * @tc.type: FUNC
161  */
162 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyRoot, TestSize.Level1)
163 {
164     SetBrowserPoliciesPlugin plugin;
165     MessageParcel data;
166     MessageParcel reply;
167     int32_t userid = 0;
168     std::vector<std::string> params{TEST_APP_ID, "", TEST_POLICY_VALUE2};
169     data.WriteStringVector(params);
170 
171     auto serializer = CjsonSerializer::GetInstance();
172 
173     HandlePolicyData policyData;
174     policyData.policyData = TEST_POLICY_DATA1;
175     plugin.OnHandlePolicy(0, data, reply, policyData, userid);
176     cJSON* policies;
177     serializer->Deserialize(policyData.policyData, policies);
178     cJSON* policy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
179     std::string policyValue;
180     serializer->Serialize(policy, policyValue);
181     ASSERT_TRUE(policyValue == TEST_POLICY_VALUE2);
182     cJSON_Delete(policies);
183 }
184 
185 /**
186  * @tc.name: TestSetPolicyRootEmptyValue
187  * @tc.desc: Test SetBrowserPoliciesPlugin::onHandlePolicy
188  * and policyName is root with empty policyValue.
189  * @tc.type: FUNC
190  */
191 HWTEST_F(SetBrowserPoliciesPluginTest, TestSetPolicyRootEmptyValue, TestSize.Level1)
192 {
193     SetBrowserPoliciesPlugin plugin;
194     MessageParcel data;
195     MessageParcel reply;
196     int32_t userid = 0;
197     std::vector<std::string> params{TEST_APP_ID, "", ""};
198     data.WriteStringVector(params);
199 
200     auto serializer = CjsonSerializer::GetInstance();
201 
202     HandlePolicyData policyData;
203     policyData.policyData = TEST_POLICY_DATA1;
204     plugin.OnHandlePolicy(0, data, reply, policyData, userid);
205     cJSON* policies;
206     serializer->Deserialize(policyData.policyData, policies);
207     cJSON* policy = cJSON_GetObjectItem(policies, TEST_APP_ID.c_str());
208     ASSERT_TRUE(policy == nullptr);
209     cJSON_Delete(policies);
210 }
211 
212 /**
213  * @tc.name: TestMergePolicyData
214  * @tc.desc: Test SetBrowserPoliciesPlugin::MergePolicyData
215  * @tc.type: FUNC
216  */
217 HWTEST_F(SetBrowserPoliciesPluginTest, TestMergePolicyData, TestSize.Level1)
218 {
219     SetBrowserPoliciesPlugin plugin;
220     std::string adminName = TEST_ADMIN_NAME1;
221     std::string policyData = TEST_POLICY_DATA1;
222     auto ret = plugin.GetOthersMergePolicyData(adminName, policyData);
223     ASSERT_TRUE(ret == ERR_OK);
224 }
225 
226 /**
227  * @tc.name: TestAddBrowserPoliciesToRootEmpty
228  * @tc.desc: Test SetBrowserPoliciesPlugin::AddBrowserPoliciesToRoot when policiesString is empty
229  * @tc.type: FUNC
230  */
231 HWTEST_F(SetBrowserPoliciesPluginTest, TestAddBrowserPoliciesToRootEmpty, TestSize.Level1)
232 {
233     SetBrowserPoliciesPlugin plugin;
234     cJSON* root = cJSON_CreateObject();
235     std::string policiesString;
236     auto ret = plugin.AddBrowserPoliciesToRoot(root, policiesString);
237     ASSERT_TRUE(ret);
238 }
239 
240 /**
241  * @tc.name: TestAddBrowserPoliciesToRootParseError
242  * @tc.desc: Test SetBrowserPoliciesPlugin::AddBrowserPoliciesToRoot when policiesString is parsed error
243  * @tc.type: FUNC
244  */
245 HWTEST_F(SetBrowserPoliciesPluginTest, TestAddBrowserPoliciesToRootParseError, TestSize.Level1)
246 {
247     SetBrowserPoliciesPlugin plugin;
248     cJSON* root = cJSON_CreateObject();
249     std::string policiesString = "{";
250     auto ret = plugin.AddBrowserPoliciesToRoot(root, policiesString);
251     ASSERT_FALSE(ret);
252 }
253 
254 /**
255  * @tc.name: TestAddBrowserPoliciesToRoot
256  * @tc.desc: Test SetBrowserPoliciesPlugin::AddBrowserPoliciesToRoot
257  * @tc.type: FUNC
258  */
259 HWTEST_F(SetBrowserPoliciesPluginTest, TestAddBrowserPoliciesToRoot, TestSize.Level1)
260 {
261     SetBrowserPoliciesPlugin plugin;
262     cJSON* root = cJSON_CreateObject();
263     cJSON_AddItemToObject(root, TEST_APP_ID.c_str(), cJSON_CreateObject());
264     std::string policiesString = TEST_POLICY_DATA1;
265     auto ret = plugin.AddBrowserPoliciesToRoot(root, policiesString);
266     ASSERT_TRUE(ret);
267 }
268 
269 } // namespace TEST
270 } // namespace EDM
271 } // namespace OHOS