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