• 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 empty.
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: TestOnGetPolicySucForGetPolicyData
168  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnGetPolicy success for get policy data.
169  * @tc.type: FUNC
170  */
171 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnGetPolicySucForGetPolicyData, TestSize.Level1)
172 {
173     ManagedBrowserPolicyPlugin plugin;
174     MessageParcel data;
175     data.WriteString(EdmConstants::Browser::GET_MANAGED_BROWSER_FILE_DATA);
176     data.WriteString(TEST_BUNDLE_NAME);
177     MessageParcel reply;
178     std::string policyData = TEST_POLICY_DATA;
179     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
180     ASSERT_TRUE(ret == ERR_OK);
181     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
182     ASSERT_TRUE(reply.ReadInt32() > 0);
183 }
184 
185 /**
186  * @tc.name: TestOnGetPolicyFailWithInvalidBundleName
187  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnGetPolicy success for get policy data.
188  * @tc.type: FUNC
189  */
190 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnGetPolicyFailWithInvalidBundleName, TestSize.Level1)
191 {
192     ManagedBrowserPolicyPlugin plugin;
193     std::string policyData;
194     std::string type = EdmConstants::Browser::GET_MANAGED_BROWSER_FILE_DATA;
195     std::string bundleName = "../invalidBundleName";
196     MessageParcel data;
197     data.WriteString(type);
198     data.WriteString(bundleName);
199     MessageParcel reply;
200 
201     ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
202     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
203 }
204 
205 /**
206  * @tc.name: TestGetManagedBrowserPolicyVersionSuc
207  * @tc.desc: Test ManagedBrowserPolicyPluginTest::GetManagedBrowserPolicyVersion success.
208  * @tc.type: FUNC
209  */
210 HWTEST_F(ManagedBrowserPolicyPluginTest, TestGetManagedBrowserPolicyVersionSuc, TestSize.Level1)
211 {
212     ManagedBrowserPolicyPlugin plugin;
213     MessageParcel reply;
214     std::string policyData = TEST_POLICY_DATA;
215     ErrCode ret = plugin.GetManagedBrowserPolicyVersion(policyData, TEST_BUNDLE_NAME, reply);
216     ASSERT_TRUE(ret == ERR_OK);
217     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
218     ASSERT_TRUE(reply.ReadInt32() == 1);
219 }
220 
221 /**
222  * @tc.name: TestGetSelfManagedBrowserPolicyData
223  * @tc.desc: Test ManagedBrowserPolicyPluginTest::GetManagedBrowserPolicyFileData success.
224  * @tc.type: FUNC
225  */
226 HWTEST_F(ManagedBrowserPolicyPluginTest, TestGetSelfManagedBrowserPolicyData, TestSize.Level1)
227 {
228     ManagedBrowserPolicyPlugin plugin;
229     MessageParcel reply;
230 
231     ErrCode ret = plugin.GetManagedBrowserPolicyFileData(TEST_BUNDLE_NAME, reply);
232     ASSERT_TRUE(ret == ERR_OK);
233     ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
234     ASSERT_TRUE(reply.ReadInt32() > 0);
235 }
236 
237 /**
238  * @tc.name: TestOnHandlePolicySucForModifyOrRemovePolicy
239  * @tc.desc: Test ManagedBrowserPolicyPluginTest::OnHandlePolicy success.
240  * @tc.type: FUNC
241  */
242 HWTEST_F(ManagedBrowserPolicyPluginTest, TestOnHandlePolicySucForModifyOrRemovePolicy, TestSize.Level1)
243 {
244     ManagedBrowserPolicyPlugin plugin;
245     std::uint32_t funcCode = 0;
246     std::string emptyPolicyValue;
247     MessageParcel data;
248     data.WriteString(TEST_BUNDLE_NAME);
249     data.WriteString(TEST_POLICY_NAME);
250     data.WriteString(emptyPolicyValue);
251     MessageParcel reply;
252     std::map<std::string, ManagedBrowserPolicyType> policies;
253     policies[TEST_BUNDLE_NAME].version = 1;
254     policies[TEST_BUNDLE_NAME].policyNames.push_back(TEST_POLICY_NAME);
255     HandlePolicyData policyData;
256     auto serializer = ManagedBrowserPolicySerializer::GetInstance();
257     serializer->Serialize(policies, policyData.policyData);
258     ErrCode ret = plugin.OnHandlePolicy(funcCode, data, reply, policyData, DEFAULT_USER_ID);
259     ASSERT_TRUE(ret == ERR_OK);
260 }
261 
262 /**
263  * @tc.name: TestModifyOrRemoveManagedBrowserPolicyFailWithInvalidUrl
264  * @tc.desc: Test ManagedBrowserPolicyPluginTest::ModifyOrRemoveManagedBrowserPolicy failed with invalid url.
265  * @tc.type: FUNC
266  */
267 HWTEST_F(ManagedBrowserPolicyPluginTest, TestModifyOrRemoveManagedBrowserPolicyFailWithInvalidUrl, TestSize.Level1)
268 {
269     ManagedBrowserPolicyPlugin plugin;
270     std::map<std::string, ManagedBrowserPolicyType> policies;
271     const std::string bundleName = "com.not.existed";
272     const std::string policyName;
273     const std::string policyValue;
274     ErrCode ret = plugin.ModifyOrRemoveManagedBrowserPolicy(policies, bundleName, policyName, policyValue);
275     ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
276 }
277 } // namespace TEST
278 } // namespace EDM
279 } // namespace OHOS