• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include <cstdio>
17 #include <fstream>
18 #include "managed_browser_policy_plugin_test.h"
19 
20 #include "edm_constants.h"
21 #include "edm_log.h"
22 #include "edm_sys_manager.h"
23 #include "managed_browser_policy_serializer.h"
24 #include "utils.h"
25 
26 using namespace testing::ext;
27 
28 namespace OHOS {
29 namespace EDM {
30 namespace TEST {
31 const std::string TEST_POLICY_DATA = "{\"test_bundle_name\":{\"version\":1,\"policyNames\":[\"test_policy_name\"]}}";
32 const std::string TEST_BUNDLE_NAME = "test_bundle_name";
33 const std::string TEST_POLICY_NAME = "test_policy_name";
34 const std::string TEST_POLICY_VALUE = "\"test_policy_value\"";
35 const char* const MANAGED_BROWSER_POLICY_DIR = "/data/service/el1/public/edm/browser/";
36 const char* const MANAGED_BROWSER_POLICY_SUFFIX = ".dat";
37 const std::string URL = MANAGED_BROWSER_POLICY_DIR + TEST_BUNDLE_NAME + MANAGED_BROWSER_POLICY_SUFFIX;
SetUpTestSuite(void)38 void ManagedBrowserPolicyPluginTest::SetUpTestSuite(void)
39 {
40     Utils::SetEdmInitialEnv();
41 }
42 
TearDownTestSuite(void)43 void ManagedBrowserPolicyPluginTest::TearDownTestSuite(void)
44 {
45     Utils::ResetTokenTypeAndUid();
46     ASSERT_TRUE(Utils::IsOriginalUTEnv());
47     std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
48 }
49 
50 /**
51  * @tc.name: TestOnHandlePolicyFailWithEmptyBundleName
52  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnHandlePolicy when BundleName is empty.
53  * @tc.type: FUNC
54  */
55 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnHandlePolicyFailWithEmptyBundleName, TestSize.Level1)
56 {
57     ManagedBrowserPolicyPlugin plugin;
58     std::uint32_t funcCode = 0;
59     std::string emptyBundleName;
60     MessageParcel data;
61     data.WriteString(emptyBundleName);
62     data.WriteString(TEST_POLICY_NAME);
63     data.WriteString(TEST_POLICY_VALUE);
64     MessageParcel reply;
65     HandlePolicyData policyData;
66     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
67     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
68 }
69 
70 /**
71  * @tc.name: TestOnHandlePolicyFailWithInvalidBundleName
72  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnHandlePolicy when BundleName is invalid.
73  * @tc.type: FUNC
74  */
75 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnHandlePolicyFailWithInvalidBundleName, TestSize.Level1)
76 {
77     ManagedBrowserPolicyPlugin plugin;
78     std::uint32_t funcCode = 0;
79     std::string invalidBundleName = "../invalidBundleName";
80     MessageParcel data;
81     data.WriteString(invalidBundleName);
82     data.WriteString(TEST_POLICY_NAME);
83     data.WriteString(TEST_POLICY_VALUE);
84     MessageParcel reply;
85     HandlePolicyData policyData;
86     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
87     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
88 }
89 
90 /**
91  * @tc.name: TestOnHandlePolicyFailWithEmptyPolicyName
92  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnHandlePolicy when PolicyName is empty.
93  * @tc.type: FUNC
94  */
95 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnHandlePolicyFailWithEmptyPolicyName, TestSize.Level1)
96 {
97     ManagedBrowserPolicyPlugin plugin;
98     std::uint32_t funcCode = 0;
99     std::string emptyPolicyName;
100     MessageParcel data;
101     data.WriteString(TEST_BUNDLE_NAME);
102     data.WriteString(emptyPolicyName);
103     data.WriteString(TEST_POLICY_VALUE);
104     MessageParcel reply;
105     HandlePolicyData policyData;
106     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
107     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
108 }
109 
110 /**
111  * @tc.name: TestOnHandlePolicySucForAddPolicy
112  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnHandlePolicy Success.
113  * @tc.type: FUNC
114  */
115 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnHandlePolicySucAddPolicy, TestSize.Level1)
116 {
117     ManagedBrowserPolicyPlugin plugin;
118     std::uint32_t funcCode = 0;
119     MessageParcel data;
120     data.WriteString(TEST_BUNDLE_NAME);
121     data.WriteString(TEST_POLICY_NAME);
122     data.WriteString(TEST_POLICY_VALUE);
123     MessageParcel reply;
124     HandlePolicyData policyData;
125     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
126     ASSERT_TRUE(ret == ERR_OK);
127 }
128 
129 /**
130  * @tc.name: TestOnGetPolicyFailWithInvalidType
131  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnGetPolicy when type is invalid.
132  * @tc.type: FUNC
133  */
134 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnGetPolicyFailWithInvalidType, TestSize.Level1)
135 {
136     ManagedBrowserPolicyPlugin plugin;
137     std::string policyData;
138     std::string type = "invalidType";
139     MessageParcel data;
140     data.WriteString(type);
141     MessageParcel reply;
142 
143     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
144     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
145 }
146 
147 /**
148  * @tc.name: TestOnGetPolicyFailWithEmptyBundleName
149  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnGetPolicy with empty bundle name.
150  * @tc.type: FUNC
151  */
152 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnGetPolicyFailWithEmptyBundleName, TestSize.Level1)
153 {
154     ManagedBrowserPolicyPlugin plugin;
155     std::string policyData;
156     std::string type = EdmConstants::Browser::GET_MANAGED_BROWSER_FILE_DATA;
157     std::string bundleName;
158     MessageParcel data;
159     data.WriteString(type);
160     data.WriteString(bundleName);
161     MessageParcel reply;
162     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
163     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
164 }
165 
166 /**
167  * @tc.name: TestOnGetPolicyFailWithInvalidBundleName
168  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnGetPolicy with invalid bundle name.
169  * @tc.type: FUNC
170  */
171 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnGetPolicyFailWithInvalidBundleName, TestSize.Level1)
172 {
173     ManagedBrowserPolicyPlugin plugin;
174     std::string policyData;
175     std::string type = EdmConstants::Browser::GET_MANAGED_BROWSER_FILE_DATA;
176     std::string bundleName = "../invalidbundleName";
177     MessageParcel data;
178     data.WriteString(type);
179     data.WriteString(bundleName);
180     MessageParcel reply;
181     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
182     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
183 }
184 
185 /**
186  * @tc.name: TestOnGetPolicySucForGetPolicyData
187  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnGetPolicy success for get policy data.
188  * @tc.type: FUNC
189  */
190 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnGetPolicySucForGetPolicyData, TestSize.Level1)
191 {
192     ManagedBrowserPolicyPlugin plugin;
193     MessageParcel data;
194     data.WriteString(EdmConstants::Browser::GET_MANAGED_BROWSER_FILE_DATA);
195     data.WriteString(TEST_BUNDLE_NAME);
196     MessageParcel reply;
197     std::string policyData = TEST_POLICY_DATA;
198     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
199     ASSERT_TRUE(ret == ERR_OK);
200     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
201     ASSERT_TRUE(reply.ReadInt32() > 0);
202 }
203 
204 /**
205  * @tc.name: TestGetManagedBrowserPolicyVersionSuc
206  * @tc.desc: Test ManagedBrowserPolicyPluginTest::GetManagedBrowserPolicyVersion success.
207  * @tc.type: FUNC
208  */
209 HWTEST_F(ManagedBrowserPolicyPluginTest, TestGetManagedBrowserPolicyVersionSuc, TestSize.Level1)
210 {
211     ManagedBrowserPolicyPlugin plugin;
212     MessageParcel reply;
213     std::string policyData = TEST_POLICY_DATA;
214     ErrCode ret = plugin.GetManagedBrowserPolicyVersion(policyData, TEST_BUNDLE_NAME, reply);
215     ASSERT_TRUE(ret == ERR_OK);
216     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
217     ASSERT_TRUE(reply.ReadInt32() == 1);
218 }
219 
220 /**
221  * @tc.name: TestGetSelfManagedBrowserPolicyData
222  * @tc.desc: Test ManagedBrowserPolicyPluginTest::GetManagedBrowserPolicyFileData success.
223  * @tc.type: FUNC
224  */
225 HWTEST_F(ManagedBrowserPolicyPluginTest, TestGetSelfManagedBrowserPolicyData, TestSize.Level1)
226 {
227     ManagedBrowserPolicyPlugin plugin;
228     MessageParcel reply;
229 
230     ErrCode ret = plugin.GetManagedBrowserPolicyFileData(TEST_BUNDLE_NAME, reply);
231     ASSERT_TRUE(ret == ERR_OK);
232     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
233     ASSERT_TRUE(reply.ReadInt32() > 0);
234 }
235 
236 /**
237  * @tc.name: TestOnHandlePolicySucForModifyOrRemovePolicy
238  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnHandlePolicy success.
239  * @tc.type: FUNC
240  */
241 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnHandlePolicySucForModifyOrRemovePolicy, TestSize.Level1)
242 {
243     ManagedBrowserPolicyPlugin plugin;
244     std::uint32_t funcCode = 0;
245     std::string emptyPolicyValue;
246     MessageParcel data;
247     data.WriteString(TEST_BUNDLE_NAME);
248     data.WriteString(TEST_POLICY_NAME);
249     data.WriteString(emptyPolicyValue);
250     MessageParcel reply;
251     std::map<std::string, ManagedBrowserPolicyType> policies;
252     policies[TEST_BUNDLE_NAME].version = 1;
253     policies[TEST_BUNDLE_NAME].policyNames.push_back(TEST_POLICY_NAME);
254     HandlePolicyData policyData;
255     auto serializer = ManagedBrowserPolicySerializer::GetInstance();
256     serializer->Serialize(policies, policyData.policyData);
257     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
258     ASSERT_TRUE(ret == ERR_OK);
259 }
260 
261 /**
262  * @tc.name: TestModifyOrRemoveManagedBrowserPolicyFailWithInvalidUrl
263  * @tc.desc: Test ManagedBrowserPolicyPluginTest::ModifyOrRemoveManagedBrowserPolicy failed with invalid url.
264  * @tc.type: FUNC
265  */
266 HWTEST_F(ManagedBrowserPolicyPluginTest, TestModifyOrRemoveManagedBrowserPolicyFailWithInvalidUrl, TestSize.Level1)
267 {
268     ManagedBrowserPolicyPlugin plugin;
269     std::map<std::string, ManagedBrowserPolicyType> policies;
270     const std::string bundleName = "com.not.existed";
271     const std::string policyName;
272     const std::string policyValue;
273     ErrCode ret = plugin.ModifyOrRemoveManagedBrowserPolicy(policies, bundleName, policyName, policyValue);
274     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
275 }
276 
277 /**
278  * @tc.name: TestModifyOrRemoveManagedBrowserPolicyFailWithInvalidFile
279  * @tc.desc: Test ManagedBrowserPolicyPluginTest::ModifyOrRemoveManagedBrowserPolicy failed with invalid bundle name.
280  * @tc.type: FUNC
281  */
282 HWTEST_F(ManagedBrowserPolicyPluginTest, TestModifyOrRemoveManagedBrowserPolicyFailWithInvalidFile, TestSize.Level1)
283 {
284     ManagedBrowserPolicyPlugin plugin;
285     std::map<std::string, ManagedBrowserPolicyType> policies;
286     const std::string bundleName = TEST_BUNDLE_NAME;
287     std::ofstream policyFile(URL, std::ios::app);
288     if (policyFile.fail()) {
289         ASSERT_TRUE(false);
290     }
291     policyFile << "test" << std::endl;
292     policyFile.close();
293     ErrCode ret = plugin.ModifyOrRemoveManagedBrowserPolicy(policies, bundleName, TEST_POLICY_NAME, TEST_POLICY_VALUE);
294     std::string tempUrl = MANAGED_BROWSER_POLICY_DIR + bundleName + "_tmp" + MANAGED_BROWSER_POLICY_SUFFIX;
295     ASSERT_TRUE(remove(URL.c_str()) == ERR_OK);
296     ASSERT_TRUE(ret == EdmReturnErrCode::SYSTEM_ABNORMALLY);
297 }
298 } // namespace TEST
299 } // namespace EDM
300 } // namespace OHOS