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 #define protected public
17 #include "manage_auto_start_apps_plugin_test.h"
18 #undef protected
19
20 #include "bundle_info.h"
21 #include "bundle_mgr_interface.h"
22 #include "if_system_ability_manager.h"
23 #include "iremote_stub.h"
24 #include "iservice_registry.h"
25 #include "parameters.h"
26 #include "system_ability_definition.h"
27
28 #include "array_string_serializer.h"
29 #include "edm_constants.h"
30 #include "edm_log.h"
31 #include "edm_sys_manager.h"
32 #include "install_plugin.h"
33 #include "uninstall_plugin.h"
34 #include "utils.h"
35
36 using namespace testing::ext;
37
38 namespace OHOS {
39 namespace EDM {
40 namespace TEST {
41 const std::string RIGHT_TEST_BUNDLE = "com.example.l3jsdemo/com.example.l3jsdemo.MainAbility";
42 const std::string ERROR_TEST_BUNDLE = "com.example.l3jsdemo/com.example.l3jsdemo.ErrorAbility";
43 const std::string INVALID_TEST_BUNDLE = "com.example.l3jsdemo.com.example.l3jsdemo.ErrorAbility";
44 const std::string HAP_FILE_PATH = "/data/test/resource/enterprise_device_management/hap/right.hap";
45 const std::string BOOT_OEM_MODE = "const.boot.oemmode";
46 const std::string DEVELOP_PARAM = "rd";
47 const std::string USER_MODE = "user";
SetUpTestSuite(void)48 void ManageAutoStartAppsPluginTest::SetUpTestSuite(void)
49 {
50 Utils::SetEdmInitialEnv();
51 }
52
TearDownTestSuite(void)53 void ManageAutoStartAppsPluginTest::TearDownTestSuite(void)
54 {
55 Utils::ResetTokenTypeAndUid();
56 ASSERT_TRUE(Utils::IsOriginalUTEnv());
57 std::cout << "now ut process is orignal ut env : " << Utils::IsOriginalUTEnv() << std::endl;
58 }
59
60 /**
61 * @tc.name: TestOnSetPolicySucWithNullData
62 * @tc.desc: Test ManageAutoStartAppsPlugin::OnSetPolicy when data is empty.
63 * @tc.type: FUNC
64 */
65 HWTEST_F(ManageAutoStartAppsPluginTest, TestOnSetPolicySucWithNullData, TestSize.Level1)
66 {
67 ManageAutoStartAppsPlugin plugin;
68 plugin.maxListSize_ = EdmConstants::AUTO_START_APPS_MAX_SIZE;
69 std::vector<std::string> data;
70 std::vector<std::string> currentData;
71 std::vector<std::string> mergeData;
72 ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
73 ASSERT_TRUE(ret == ERR_OK);
74 }
75
76 /**
77 * @tc.name: TestOnSetPolicyFailWithbundleExceededLimit
78 * @tc.desc: Test ManageAutoStartAppsPlugin::OnSetPolicy when budle is not existed.
79 * @tc.type: FUNC
80 */
81 HWTEST_F(ManageAutoStartAppsPluginTest, TestOnSetPolicyFailWithbundleExceededLimit, TestSize.Level1)
82 {
83 ManageAutoStartAppsPlugin plugin;
84 plugin.maxListSize_ = EdmConstants::AUTO_START_APPS_MAX_SIZE;
85 std::vector<std::string> data;
86 for (int i = 0; i < 15; i++) {
87 std::string str = "test/test" + std::to_string(i);
88 data.push_back(str);
89 }
90 std::vector<std::string> currentData;
91 std::vector<std::string> mergeData;
92 ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
93 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
94 }
95
96 /**
97 * @tc.name: TestOnSetPolicyFailWithbundleNotExist
98 * @tc.desc: Test ManageAutoStartAppsPlugin::OnSetPolicy when budle is not existed.
99 * @tc.type: FUNC
100 */
101 HWTEST_F(ManageAutoStartAppsPluginTest, TestOnSetPolicyFailWithbundleNotExist, TestSize.Level1)
102 {
103 ManageAutoStartAppsPlugin plugin;
104 plugin.maxListSize_ = EdmConstants::AUTO_START_APPS_MAX_SIZE;
105 std::vector<std::string> data = {RIGHT_TEST_BUNDLE};
106 std::vector<std::string> currentData;
107 std::vector<std::string> mergeData;
108 ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
109 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
110 }
111
112 /**
113 * @tc.name: TestOnSetPolicyFailWithInvalidData
114 * @tc.desc: Test ManageAutoStartAppsPlugin::OnSetPolicy when data is invalid.
115 * @tc.type: FUNC
116 */
117 HWTEST_F(ManageAutoStartAppsPluginTest, TestOnSetPolicyFailWithInvalidData, TestSize.Level1)
118 {
119 ManageAutoStartAppsPlugin plugin;
120 plugin.maxListSize_ = EdmConstants::AUTO_START_APPS_MAX_SIZE;
121 std::vector<std::string> data = {INVALID_TEST_BUNDLE};
122 std::vector<std::string> currentData;
123 std::vector<std::string> mergeData;
124 ErrCode ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
125 ASSERT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
126 }
127
128 /**
129 * @tc.name: TestOnSetPolicySuc
130 * @tc.desc: Test ManageAutoStartAppsPlugin::OnSetPolicy when budle is existed.
131 * @tc.type: FUNC
132 */
133 HWTEST_F(ManageAutoStartAppsPluginTest, TestOnSetPolicySuc, TestSize.Level1)
134 {
135 std::string developDeviceParam = system::GetParameter(BOOT_OEM_MODE, USER_MODE);
136 if (developDeviceParam == DEVELOP_PARAM) {
137 InstallPlugin installPlugin;
138 InstallParam param = {{HAP_FILE_PATH}, DEFAULT_USER_ID, 0};
139 MessageParcel reply;
140 ErrCode ret = installPlugin.OnSetPolicy(param, reply);
141 EXPECT_TRUE(ret == ERR_OK);
142
143 ManageAutoStartAppsPlugin plugin;
144 plugin.maxListSize_ = EdmConstants::AUTO_START_APPS_MAX_SIZE;
145 std::vector<std::string> data = {RIGHT_TEST_BUNDLE, ERROR_TEST_BUNDLE, INVALID_TEST_BUNDLE};
146 std::vector<std::string> currentData;
147 std::vector<std::string> mergeData;
148 ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
149 EXPECT_TRUE(ret == ERR_OK);
150
151 std::string policyData;
152 ArrayStringSerializer::GetInstance()->Serialize(currentData, policyData);
153 MessageParcel parcel;
154 MessageParcel getReply;
155 ret = plugin.OnGetPolicy(policyData, parcel, getReply, DEFAULT_USER_ID);
156 std::vector<std::string> res;
157 EXPECT_TRUE(ret == ERR_OK);
158 EXPECT_TRUE(getReply.ReadInt32() == ERR_OK);
159 getReply.ReadStringVector(&res);
160 EXPECT_TRUE(res.size() >= 1);
161 EXPECT_TRUE(std::find(res.begin(), res.end(), RIGHT_TEST_BUNDLE) != res.end());
162
163 std::vector<std::string> removeData = {RIGHT_TEST_BUNDLE, ERROR_TEST_BUNDLE, INVALID_TEST_BUNDLE};
164 mergeData.clear();
165 ret = plugin.OnBasicRemovePolicy(removeData, currentData, mergeData, DEFAULT_USER_ID);
166 EXPECT_TRUE(ret == ERR_OK);
167
168 MessageParcel removeReply;
169 ArrayStringSerializer::GetInstance()->Serialize(currentData, policyData);
170 ret = plugin.OnGetPolicy(policyData, parcel, removeReply, DEFAULT_USER_ID);
171 std::vector<std::string> afterRemove;
172 EXPECT_TRUE(ret == ERR_OK);
173 EXPECT_TRUE(removeReply.ReadInt32() == ERR_OK);
174 removeReply.ReadStringVector(&afterRemove);
175 EXPECT_TRUE(afterRemove.size() == 0);
176
177 UninstallPlugin uninstallPlugin;
178 UninstallParam uninstallParam = {"com.example.l3jsdemo", DEFAULT_USER_ID, false};
179 MessageParcel uninstallReply;
180 ret = uninstallPlugin.OnSetPolicy(uninstallParam, uninstallReply);
181 EXPECT_TRUE(ret == ERR_OK);
182 }
183 }
184
185 /**
186 * @tc.name: TestOnGetPolicySuc
187 * @tc.desc: Test ManageAutoStartAppsPlugin::OnGetPolicy.
188 * @tc.type: FUNC
189 */
190 HWTEST_F(ManageAutoStartAppsPluginTest, TestOnGetPolicySuc, TestSize.Level1)
191 {
192 ManageAutoStartAppsPlugin plugin;
193 std::string policyData = RIGHT_TEST_BUNDLE;
194 MessageParcel data;
195 MessageParcel reply;
196 ErrCode ret = plugin.OnGetPolicy(policyData, data, reply, DEFAULT_USER_ID);
197 ASSERT_TRUE(ret == ERR_OK);
198 ASSERT_TRUE(reply.ReadInt32() == ERR_OK);
199 }
200
201 /**
202 * @tc.name: TestOnRemovePolicySucWithNullData
203 * @tc.desc: Test ManageAutoStartAppsPlugin::OnRemovePolicy when data is empty.
204 * @tc.type: FUNC
205 */
206 HWTEST_F(ManageAutoStartAppsPluginTest, TestOnRemovePolicySucWithNullData, TestSize.Level1)
207 {
208 ManageAutoStartAppsPlugin plugin;
209 plugin.maxListSize_ = EdmConstants::AUTO_START_APPS_MAX_SIZE;
210 std::vector<std::string> data;
211 std::vector<std::string> currentData;
212 std::vector<std::string> mergeData;
213 ErrCode ret = plugin.OnBasicRemovePolicy(data, currentData, mergeData, DEFAULT_USER_ID);
214 ASSERT_TRUE(ret == ERR_OK);
215 }
216
217 /**
218 * @tc.name: TestOnRemovePolicyFileWithErrBundle
219 * @tc.desc: Test ManageAutoStartAppsPlugin::OnRemovePolicy when budle is not existed.
220 * @tc.type: FUNC
221 */
222 HWTEST_F(ManageAutoStartAppsPluginTest, TestOnRemovePolicyFileWithErrBundle, TestSize.Level1)
223 {
224 ManageAutoStartAppsPlugin plugin;
225 plugin.maxListSize_ = EdmConstants::AUTO_START_APPS_MAX_SIZE;
226 std::vector<std::string> data = {ERROR_TEST_BUNDLE};
227 std::vector<std::string> currentData;
228 std::vector<std::string> mergeData;
229 ErrCode ret = plugin.OnBasicRemovePolicy(data, currentData, mergeData, DEFAULT_USER_ID);
230 ASSERT_TRUE(ret == ERR_OK);
231 }
232
233 /**
234 * @tc.name: TestOnRemovePolicySuc
235 * @tc.desc: Test ManageAutoStartAppsPlugin::OnSetPolicy when data is invalid.
236 * @tc.type: FUNC
237 */
238 HWTEST_F(ManageAutoStartAppsPluginTest, TestOnRemovePolicySuc, TestSize.Level1)
239 {
240 std::string developDeviceParam = system::GetParameter(BOOT_OEM_MODE, USER_MODE);
241 if (developDeviceParam == DEVELOP_PARAM) {
242 InstallPlugin installPlugin;
243 InstallParam param = {{HAP_FILE_PATH}, DEFAULT_USER_ID, 0};
244 MessageParcel reply;
245 ErrCode ret = installPlugin.OnSetPolicy(param, reply);
246 EXPECT_TRUE(ret == ERR_OK);
247
248 ManageAutoStartAppsPlugin plugin;
249 plugin.maxListSize_ = EdmConstants::AUTO_START_APPS_MAX_SIZE;
250 std::vector<std::string> data = {RIGHT_TEST_BUNDLE};
251 std::vector<std::string> currentData;
252 std::vector<std::string> mergeData;
253 ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
254 EXPECT_TRUE(ret == ERR_OK);
255
256 data = {INVALID_TEST_BUNDLE};
257 currentData = {INVALID_TEST_BUNDLE};
258 ret = plugin.OnBasicRemovePolicy(data, currentData, mergeData, DEFAULT_USER_ID);
259 EXPECT_TRUE(ret == EdmReturnErrCode::PARAM_ERROR);
260
261 data = {RIGHT_TEST_BUNDLE};
262 currentData = {RIGHT_TEST_BUNDLE};
263 ret = plugin.OnBasicRemovePolicy(data, currentData, mergeData, DEFAULT_USER_ID);
264 EXPECT_TRUE(ret == ERR_OK);
265
266 UninstallPlugin uninstallPlugin;
267 UninstallParam uninstallParam = {"com.example.l3jsdemo", DEFAULT_USER_ID, false};
268 MessageParcel uninstallReply;
269 ret = uninstallPlugin.OnSetPolicy(uninstallParam, uninstallReply);
270 EXPECT_TRUE(ret == ERR_OK);
271 }
272 }
273
274 /**
275 * @tc.name: TestOnRemovePolicySucAlreadyUninstall
276 * @tc.desc: Test ManageAutoStartAppsPlugin::OnSetPolicy when hap already uninstall.
277 * @tc.type: FUNC
278 */
279 HWTEST_F(ManageAutoStartAppsPluginTest, TestOnRemovePolicySucAlreadyUninstall, TestSize.Level1)
280 {
281 std::string developDeviceParam = system::GetParameter(BOOT_OEM_MODE, USER_MODE);
282 if (developDeviceParam == DEVELOP_PARAM) {
283 InstallPlugin installPlugin;
284 InstallParam param = {{HAP_FILE_PATH}, DEFAULT_USER_ID, 0};
285 MessageParcel reply;
286 ErrCode ret = installPlugin.OnSetPolicy(param, reply);
287 EXPECT_TRUE(ret == ERR_OK);
288
289 ManageAutoStartAppsPlugin plugin;
290 plugin.maxListSize_ = EdmConstants::AUTO_START_APPS_MAX_SIZE;
291 std::vector<std::string> data = {RIGHT_TEST_BUNDLE};
292 std::vector<std::string> currentData;
293 std::vector<std::string> mergeData;
294 ret = plugin.OnBasicSetPolicy(data, currentData, mergeData, DEFAULT_USER_ID);
295 EXPECT_TRUE(ret == ERR_OK);
296
297 UninstallPlugin uninstallPlugin;
298 UninstallParam uninstallParam = {"com.example.l3jsdemo", DEFAULT_USER_ID, false};
299 MessageParcel uninstallReply;
300 ret = uninstallPlugin.OnSetPolicy(uninstallParam, uninstallReply);
301 EXPECT_TRUE(ret == ERR_OK);
302
303 data = {RIGHT_TEST_BUNDLE};
304 mergeData.clear();
305 ret = plugin.OnBasicRemovePolicy(data, currentData, mergeData, DEFAULT_USER_ID);
306 EXPECT_TRUE(ret == ERR_OK);
307 }
308 }
309 } // namespace TEST
310 } // namespace EDM
311 } // namespace OHOS