1 /*
2 * Copyright (c) 2022 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 #include "bundle_test_tool.h"
16
17 #include <chrono>
18 #include <cstdlib>
19 #include <cstring>
20 #include <future>
21 #include <getopt.h>
22 #include <iostream>
23 #include <set>
24 #include <sstream>
25 #include <thread>
26 #include <unistd.h>
27 #include <vector>
28
29 #include "app_log_wrapper.h"
30 #include "appexecfwk_errors.h"
31 #include "bundle_command_common.h"
32 #include "bundle_death_recipient.h"
33 #include "bundle_mgr_client.h"
34 #include "bundle_mgr_proxy.h"
35 #include "bundle_tool_callback_stub.h"
36 #include "data_group_info.h"
37 #include "directory_ex.h"
38 #include "parameter.h"
39 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
40 #include "quick_fix_status_callback_host_impl.h"
41 #endif
42 #include "status_receiver_impl.h"
43 #include "string_ex.h"
44 #include "json_util.h"
45
46 namespace OHOS {
47 namespace AppExecFwk {
48 namespace {
49 const std::string LINE_BREAK = "\n";
50 constexpr int32_t SLEEP_SECONDS = 20;
51 // param
52 const int32_t INDEX_OFFSET = 2;
53 // quick fix error code
54 const int32_t ERR_BUNDLEMANAGER_FEATURE_IS_NOT_SUPPORTED = 801;
55 // quick fix error message
56 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR = "error: quick fix internal error.\n";
57 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PARAM_ERROR = "error: param error.\n";
58 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PROFILE_PARSE_FAILED = "error: profile parse failed.\n";
59 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_BUNDLE_NAME_NOT_SAME = "error: not same bundle name.\n";
60 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_VERSION_CODE_NOT_SAME = "error: not same version code.\n";
61 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_VERSION_NAME_NOT_SAME = "error: not same version name.\n";
62 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_VERSION_CODE_NOT_SAME =
63 "error: not same patch version code.\n";
64 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_VERSION_NAME_NOT_SAME =
65 "error: not same patch version name.\n";
66 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_TYPE_NOT_SAME = "error: not same patch type.\n";
67 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_UNKNOWN_QUICK_FIX_TYPE = "error: unknown quick fix type.\n";
68 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_SO_INCOMPATIBLE = "error: patch so incompatible.\n";
69 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_MODULE_NAME_SAME = "error: same moduleName.\n";
70 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_BUNDLE_NAME_NOT_EXIST = "error: bundle name is not existed.\n";
71 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_MODULE_NAME_NOT_EXIST = "error: module name is not existed.\n";
72 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_SIGNATURE_INFO_NOT_SAME = "error: signature is not existed.\n";
73 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_ADD_HQF_FAILED = "error: quick fix add hqf failed.\n";
74 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_SAVE_APP_QUICK_FIX_FAILED =
75 "error: quick fix save innerAppQuickFix failed.\n";
76 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_VERSION_CODE_ERROR =
77 "error: quick fix version code require greater than original hqf.\n";
78 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_NO_PATCH_IN_DATABASE = "error: no this quick fix info in database.\n";
79 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_INVALID_PATCH_STATUS = "error: wrong quick fix status.\n";
80 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_NOT_EXISTED_BUNDLE_INFO =
81 "error: cannot obtain the bundleInfo from data mgr.\n";
82 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_REMOVE_PATCH_PATH_FAILED = "error: quick fix remove path failed.\n";
83 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_EXTRACT_DIFF_FILES_FAILED = "error: extract diff files failed.\n";
84 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_APPLY_DIFF_PATCH_FAILED = "error: apply diff patch failed.\n";
85 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_UNKOWN = "error: unknown.\n";
86 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_FEATURE_IS_NOT_SUPPORTED = "feature is not supported.\n";
87 const std::string MSG_ERR_BUNDLEMANAGER_OPERATION_TIME_OUT = "error: quick fix operation time out.\n";
88 const std::string MSG_ERR_BUNDLEMANAGER_FAILED_SERVICE_DIED = "error: bundleMgr service is dead.\n";
89 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_HOT_RELOAD_NOT_SUPPORT_RELEASE_BUNDLE =
90 "error: hotreload not support release bundle.\n";
91 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_ALREADY_EXISTED = "error: patch type already existed.\n";
92 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_HOT_RELOAD_ALREADY_EXISTED =
93 "error: hotreload type already existed.\n";
94 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_NO_PATCH_INFO_IN_BUNDLE_INFO =
95 "error: no patch info in bundleInfo.\n";
96 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_MOVE_PATCH_FILE_FAILED = "error: quick fix move hqf file failed.\n";
97 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_CREATE_PATCH_PATH_FAILED = "error: quick fix create path failed.\n";
98 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_OLD_PATCH_OR_HOT_RELOAD_IN_DB =
99 "error: old patch or hot reload in db.\n";
100 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_SEND_REQUEST_FAILED = "error: send request failed.\n";
101 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_REAL_PATH_FAILED = "error: obtain realpath failed.\n";
102 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_INVALID_PATH = "error: input invalid path.\n";
103 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_OPEN_SOURCE_FILE_FAILED = "error: open source file failed.\n";
104 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_CREATE_FD_FAILED = "error: create file descriptor failed.\n";
105 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_INVALID_TARGET_DIR = "error: invalid designated target dir\n";
106 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_CREATE_TARGET_DIR_FAILED = "error: create target dir failed.\n";
107 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PERMISSION_DENIED = "error: quick fix permission denied.\n";
108 const std::string MSG_ERR_BUNDLEMANAGER_QUICK_FIX_WRITE_FILE_FAILED = "error: write file to target dir failed.\n";
109 const std::string MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_INVALID_PARAM =
110 "error: invalid param for setting debug mode.\n";
111 const std::string MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_INTERNAL_ERROR =
112 "error: internal error for setting debug mode.\n";
113 const std::string MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_PARCEL_ERROR = "error: parcel error for setting debug mode.\n";
114 const std::string MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_SEND_REQUEST_ERROR = "error: send request error.\n";
115 const std::string MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_UID_CHECK_FAILED = "error: uid check failed.\n";
116
117 static const std::string TOOL_NAME = "bundle_test_tool";
118 static const std::string HELP_MSG = "usage: bundle_test_tool <command> <options>\n"
119 "These are common bundle_test_tool commands list:\n"
120 " help list available commands\n"
121 " setrm set module isRemovable by given bundle name and module name\n"
122 " getrm obtain the value of isRemovable by given bundle name and module name\n"
123 " installSandbox indicates install sandbox\n"
124 " uninstallSandbox indicates uninstall sandbox\n"
125 " dumpSandbox indicates dump sandbox info\n"
126 " getStr obtain the value of label by given bundle name, module name and label id\n"
127 " getIcon obtain the value of icon by given bundle name, module name,\n"
128 " density and icon id\n"
129 " addAppInstallRule obtain the value of install controlRule by given some app id\n"
130 " control rule type, user id and euid\n"
131 " getAppInstallRule obtain the value of install controlRule by given some app id\n"
132 " rule type, user id and euid\n"
133 " deleteAppInstallRule obtain the value of install controlRule by given some app id\n"
134 " user id and euid\n"
135 " cleanAppInstallRule obtain the value of install controlRule by given rule type\n"
136 " user id and euid\n"
137 " addAppRunningRule obtain the value of app running control rule\n"
138 " by given controlRule user id and euidn"
139 " deleteAppRunningRule obtain the value of app running control rule\n"
140 " by given controlRule user id and euid\n"
141 " cleanAppRunningRule obtain the value of app running control\n"
142 " rule by given user id and euid\n"
143 " getAppRunningControlRule obtain the value of app running control rule\n"
144 " by given user id and euid and some app id\n"
145 " getAppRunningControlRuleResult obtain the value of app running control rule\n"
146 " by given bundleName user id, euid and controlRuleResult\n"
147 " deployQuickFix deploy a quick fix patch of an already installed bundle\n"
148 " switchQuickFix switch a quick fix patch of an already installed bundle\n"
149 " deleteQuickFix delete a quick fix patch of an already installed bundle\n"
150 " setDebugMode enable signature debug mode\n"
151 " getBundleStats get bundle stats\n"
152 " getAppProvisionInfo get appProvisionInfo\n"
153 " getDistributedBundleName get distributedBundleName\n"
154 " eventCB register then unregister bundle event callback\n";
155
156 const std::string HELP_MSG_GET_REMOVABLE =
157 "usage: bundle_test_tool getrm <options>\n"
158 "eg:bundle_test_tool getrm -m <module-name> -n <bundle-name> \n"
159 "options list:\n"
160 " -h, --help list available commands\n"
161 " -n, --bundle-name <bundle-name> get isRemovable by moduleNmae and bundleName\n"
162 " -m, --module-name <module-name> get isRemovable by moduleNmae and bundleName\n";
163
164 const std::string HELP_MSG_NO_REMOVABLE_OPTION =
165 "error: you must specify a bundle name with '-n' or '--bundle-name' \n"
166 "and a module name with '-m' or '--module-name' \n";
167
168 const std::string HELP_MSG_SET =
169 "usage: bundle_test_tool setrm <options>\n"
170 "eg:bundle_test_tool setrm -m <module-name> -n <bundle-name> -i 1\n"
171 "options list:\n"
172 " -h, --help list available commands\n"
173 " -n, --bundle-name <bundle-name> set isRemovable by moduleNmae and bundleName\n"
174 " -i, --is-removable <is-removable> set isRemovable 0 or 1\n"
175 " -m, --module-name <module-name> set isRemovable by moduleNmae and bundleName\n";
176
177 const std::string HELP_MSG_INSTALL_SANDBOX =
178 "usage: bundle_test_tool installSandbox <options>\n"
179 "options list:\n"
180 " -h, --help list available commands\n"
181 " -u, --user-id <user-id> specify a user id\n"
182 " -n, --bundle-name <bundle-name> install a sandbox of a bundle\n"
183 " -d, --dlp-type <dlp-type> specify type of the sandbox application\n";
184
185 const std::string HELP_MSG_UNINSTALL_SANDBOX =
186 "usage: bundle_test_tool uninstallSandbox <options>\n"
187 "options list:\n"
188 " -h, --help list available commands\n"
189 " -u, --user-id <user-id> specify a user id\n"
190 " -a, --app-index <app-index> specify a app index\n"
191 " -n, --bundle-name <bundle-name> install a sandbox of a bundle\n";
192
193 const std::string HELP_MSG_DUMP_SANDBOX =
194 "usage: bundle_test_tool dumpSandbox <options>\n"
195 "options list:\n"
196 " -h, --help list available commands\n"
197 " -u, --user-id <user-id> specify a user id\n"
198 " -a, --app-index <app-index> specify a app index\n"
199 " -n, --bundle-name <bundle-name> install a sandbox of a bundle\n";
200
201 const std::string HELP_MSG_GET_STRING =
202 "usage: bundle_test_tool getStr <options>\n"
203 "eg:bundle_test_tool getStr -m <module-name> -n <bundle-name> -u <user-id> -i --id <id> \n"
204 "options list:\n"
205 " -h, --help list available commands\n"
206 " -n, --bundle-name <bundle-name> specify bundle name of the application\n"
207 " -m, --module-name <module-name> specify module name of the application\n"
208 " -u, --user-id <user-id> specify a user id\n"
209 " -i, --id <id> specify a label id of the application\n";
210
211 const std::string HELP_MSG_GET_ICON =
212 "usage: bundle_test_tool getIcon <options>\n"
213 "eg:bundle_test_tool getIcon -m <module-name> -n <bundle-name> -u <user-id> -d --density <density> -i --id <id> \n"
214 "options list:\n"
215 " -h, --help list available commands\n"
216 " -n, --bundle-name <bundle-name> specify bundle name of the application\n"
217 " -m, --module-name <module-name> specify module name of the application\n"
218 " -u, --user-id <user-id> specify a user id\n"
219 " -d, --density <density> specify a density\n"
220 " -i, --id <id> specify a icon id of the application\n";
221
222 const std::string HELP_MSG_NO_GETSTRING_OPTION =
223 "error: you must specify a bundle name with '-n' or '--bundle-name' \n"
224 "and a module name with '-m' or '--module-name' \n"
225 "and a userid with '-u' or '--user-id' \n"
226 "and a labelid with '-i' or '--id' \n";
227
228 const std::string HELP_MSG_NO_GETICON_OPTION =
229 "error: you must specify a bundle name with '-n' or '--bundle-name' \n"
230 "and a module name with '-m' or '--module-name' \n"
231 "and a userid with '-u' or '--user-id' \n"
232 "and a density with '-d' or '--density' \n"
233 "and a iconid with '-i' or '--id' \n";
234
235 const std::string HELP_MSG_ADD_INSTALL_RULE =
236 "usage: bundle_test_tool <options>\n"
237 "eg:bundle_test_tool addAppInstallRule -a <app-id> -t <control-rule-type> -u <user-id> \n"
238 "options list:\n"
239 " -h, --help list available commands\n"
240 " -a, --app-id <app-id> specify app id of the application\n"
241 " -e, --euid <eu-id> default euid value is 3057\n"
242 " -t, --control-rule-type specify control type of the application\n"
243 " -u, --user-id <user-id> specify a user id\n";
244
245 const std::string HELP_MSG_GET_INSTALL_RULE =
246 "usage: bundle_test_tool <options>\n"
247 "eg:bundle_test_tool getAppInstallRule -t <control-rule-type> -u <user-id> \n"
248 "options list:\n"
249 " -h, --help list available commands\n"
250 " -e, --euid <eu-id> default euid value is 3057\n"
251 " -t, --control-rule-type specify control type of the application\n"
252 " -u, --user-id <user-id> specify a user id\n";
253
254 const std::string HELP_MSG_DELETE_INSTALL_RULE =
255 "usage: bundle_test_tool <options>\n"
256 "eg:bundle_test_tool deleteAppInstallRule -a <app-id> -t <control-rule-type> -u <user-id> \n"
257 "options list:\n"
258 " -h, --help list available commands\n"
259 " -e, --euid <eu-id> default euid value is 3057\n"
260 " -a, --app-id <app-id> specify app id of the application\n"
261 " -t, --control-rule-type specify control type of the application\n"
262 " -u, --user-id <user-id> specify a user id\n";
263
264 const std::string HELP_MSG_CLEAN_INSTALL_RULE =
265 "usage: bundle_test_tool <options>\n"
266 "eg:bundle_test_tool cleanAppInstallRule -t <control-rule-type> -u <user-id> \n"
267 "options list:\n"
268 " -h, --help list available commands\n"
269 " -e, --euid <eu-id> default euid value is 3057\n"
270 " -t, --control-rule-type specify control type of the application\n"
271 " -u, --user-id <user-id> specify a user id\n";
272
273 const std::string HELP_MSG_ADD_APP_RUNNING_RULE =
274 "usage: bundle_test_tool <options>\n"
275 "eg:bundle_test_tool addAppRunningRule -c <control-rule> -u <user-id> \n"
276 "options list:\n"
277 " -h, --help list available commands\n"
278 " -e, --euid <eu-id> default euid value is 3057\n"
279 " -c, --control-rule specify control rule of the application\n"
280 " -u, --user-id <user-id> specify a user id\n";
281
282 const std::string HELP_MSG_DELETE_APP_RUNNING_RULE =
283 "usage: bundle_test_tool <options>\n"
284 "eg:bundle_test_tool deleteAppRunningRule -c <control-rule> -u <user-id> \n"
285 "options list:\n"
286 " -h, --help list available commands\n"
287 " -e, --euid <eu-id> default euid value is 3057\n"
288 " -c, --control-rule specify control rule of the application\n"
289 " -u, --user-id <user-id> specify a user id\n";
290
291 const std::string HELP_MSG_CLEAN_APP_RUNNING_RULE =
292 "usage: bundle_test_tool <options>\n"
293 "eg:bundle_test_tool cleanAppRunningRule -u <user-id> \n"
294 "options list:\n"
295 " -h, --help list available commands\n"
296 " -e, --euid <eu-id> default euid value is 3057\n"
297 " -u, --user-id <user-id> specify a user id\n";
298
299 const std::string HELP_MSG_GET_APP_RUNNING_RULE =
300 "usage: bundle_test_tool <options>\n"
301 "eg:bundle_test_tool getAppRunningControlRule -u <user-id> \n"
302 "options list:\n"
303 " -h, --help list available commands\n"
304 " -e, --euid <eu-id> default euid value is 3057\n"
305 " -u, --user-id <user-id> specify a user id\n";
306
307 const std::string HELP_MSG_GET_APP_RUNNING_RESULT_RULE =
308 "usage: bundle_test_tool <options>\n"
309 "eg:bundle_test_tool getAppRunningControlRuleResult -n <bundle-name> \n"
310 "options list:\n"
311 " -h, --help list available commands\n"
312 " -e, --euid <eu-id> default euid value is 3057\n"
313 " -n, --bundle-name <bundle-name> specify bundle name of the application\n"
314 " -u, --user-id <user-id> specify a user id\n";
315
316 const std::string HELP_MSG_NO_ADD_INSTALL_RULE_OPTION =
317 "error: you must specify a app id with '-a' or '--app-id' \n"
318 "and a control type with '-t' or '--control-rule-type' \n"
319 "and a userid with '-u' or '--user-id' \n";
320
321 const std::string HELP_MSG_NO_GET_INSTALL_RULE_OPTION =
322 "error: you must specify a control type with '-t' or '--control-rule-type' \n"
323 "and a userid with '-u' or '--user-id' \n";
324
325 const std::string HELP_MSG_NO_DELETE_INSTALL_RULE_OPTION =
326 "error: you must specify a control type with '-a' or '--app-id' \n"
327 "and a userid with '-u' or '--user-id' \n";
328
329 const std::string HELP_MSG_NO_CLEAN_INSTALL_RULE_OPTION =
330 "error: you must specify a control type with '-t' or '--control-rule-type' \n"
331 "and a userid with '-u' or '--user-id' \n";
332
333 const std::string HELP_MSG_NO_APP_RUNNING_RULE_OPTION =
334 "error: you must specify a app running type with '-c' or '--control-rule' \n"
335 "and a userid with '-u' or '--user-id' \n";
336
337 const std::string HELP_MSG_NO_CLEAN_APP_RUNNING_RULE_OPTION =
338 "error: you must specify a app running type with a userid '-u' or '--user-id \n";
339
340 const std::string HELP_MSG_NO_GET_ALL_APP_RUNNING_RULE_OPTION =
341 "error: you must specify a app running type with '-a' or '--app-id' \n"
342 "and a userid with '-u' or '--user-id' \n";
343
344 const std::string HELP_MSG_NO_GET_APP_RUNNING_RULE_OPTION =
345 "error: you must specify a app running type with '-n' or '--bundle-name' \n"
346 "and a userid with '-u' or '--user-id' \n";
347
348 const std::string HELP_MSG_DEPLOY_QUICK_FIX =
349 "usage: bundle_test_tool deploy quick fix <options>\n"
350 "eg:bundle_test_tool deployQuickFix -p <quickFixPath> \n"
351 "options list:\n"
352 " -h, --help list available commands\n"
353 " -p, --patch-path <patch-path> specify patch path of the patch\n";
354
355 const std::string HELP_MSG_SWITCH_QUICK_FIX =
356 "usage: bundle_test_tool switch quick fix <options>\n"
357 "eg:bundle_test_tool switchQuickFix -n <bundle-name> \n"
358 "options list:\n"
359 " -h, --help list available commands\n"
360 " -n, --bundle-name <bundle-name> specify bundleName of the patch\n"
361 " -e, --enbale <enable> enable a deployed patch of disable an under using patch,\n"
362 " 1 represents enable and 0 represents disable\n";
363
364 const std::string HELP_MSG_DELETE_QUICK_FIX =
365 "usage: bundle_test_tool delete quick fix <options>\n"
366 "eg:bundle_test_tool deleteQuickFix -n <bundle-name> \n"
367 "options list:\n"
368 " -h, --help list available commands\n"
369 " -n, --bundle-name <bundle-name> specify bundleName of the patch\n";
370
371 const std::string HELP_MSG_SET_DEBUG_MODE =
372 "usage: bundle_test_tool setDebugMode <options>\n"
373 "eg:bundle_test_tool setDebugMode -e <0/1>\n"
374 "options list:\n"
375 " -h, --help list available commands\n"
376 " -e, --enable <enable> enable signature debug mode, 1 represents enable debug mode and 0\n"
377 " represents disable debug mode\n";
378
379 const std::string HELP_MSG_GET_BUNDLE_STATS =
380 "usage: bundle_test_tool getBundleStats <options>\n"
381 "eg:bundle_test_tool getBundleStats -n <bundle-name>\n"
382 "options list:\n"
383 " -h, --help list available commands\n"
384 " -n, --bundle-name <bundle-name> specify bundle name of the application\n"
385 " -u, --user-id <user-id> specify a user id\n";
386
387
388 const std::string HELP_MSG_GET_APP_PROVISION_INFO =
389 "usage: bundle_test_tool getAppProvisionInfo <options>\n"
390 "eg:bundle_test_tool getAppProvisionInfo -n <bundle-name>\n"
391 "options list:\n"
392 " -h, --help list available commands\n"
393 " -n, --bundle-name <bundle-name> specify bundle name of the application\n"
394 " -u, --user-id <user-id> specify a user id\n";
395
396 const std::string HELP_MSG_GET_DISTRIBUTED_BUNDLE_NAME =
397 "usage: bundle_test_tool getDistributedBundleName <options>\n"
398 "eg:bundle_test_tool getDistributedBundleName -n <network-id> -a <access-token-id>\n"
399 "options list:\n"
400 " -h, --help list available commands\n"
401 " -n, --network-id <network-id> specify networkId of the application\n"
402 " -a, --access-token-id <access-token-id> specify a accessTokenId of the application \n";
403
404 const std::string HELP_MSG_BUNDLE_EVENT_CALLBACK =
405 "usage: bundle_test_tool eventCB <options>\n"
406 "options list:\n"
407 " -h, --help list available commands\n"
408 " -o, --onlyUnregister only call unregister, default will call register then unregister\n"
409 " -u, --uid specify a uid, default is foundation uid\n";
410
411 const std::string HELP_MSG_GET_PROXY_DATA =
412 "usage: bundle_test_tool getProxyDataInfos <options>\n"
413 "eg:bundle_test_tool getProxyDataInfos -m <module-name> -n <bundle-name> -u <user-id>\n"
414 "options list:\n"
415 " -h, --help list available commands\n"
416 " -n, --bundle-name <bundle-name> specify bundle name of the application\n"
417 " -m, --module-name <module-name> specify module name of the application\n"
418 " -u, --user-id <user-id> specify a user id\n";
419
420 const std::string HELP_MSG_GET_ALL_PROXY_DATA =
421 "usage: bundle_test_tool getAllProxyDataInfos <options>\n"
422 "eg:bundle_test_tool getProxyDataInfos -u <user-id>\n"
423 "options list:\n"
424 " -h, --help list available commands\n"
425 " -u, --user-id <user-id> specify a user id\n";
426
427 const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION =
428 "error: you must specify a bundle name with '-n' or '--bundle-name' \n";
429
430 const std::string HELP_MSG_NO_NETWORK_ID_OPTION =
431 "error: you must specify a network id with '-n' or '--network-id' \n";
432
433 const std::string HELP_MSG_NO_ACCESS_TOKEN_ID_OPTION =
434 "error: you must specify a access token id with '-n' or '--access-token-id' \n";
435
436 const std::string HELP_MSG_SET_EXT_NAME_OR_MIME_TYPE =
437 "usage: bundle_test_tool setExtNameOrMimeTypeToApp <options>\n"
438 "eg:bundle_test_tool getProxyDataInfos -m <module-name> -n <bundle-name> -a <ability-name>\n"
439 "options list:\n"
440 " -h, --help list available commands\n"
441 " -n, --bundle-name <bundle-name> specify bundle name of the application\n"
442 " -m, --module-name <module-name> specify module name of the application\n"
443 " -a, --ability-name <ability-name> specify ability name of the application\n"
444 " -e, --ext-name <ext-name> specify the ext-name\n"
445 " -t, --mime-type <mime-type> specify the mime-type\n";
446
447 const std::string HELP_MSG_DEL_EXT_NAME_OR_MIME_TYPE =
448 "usage: bundle_test_tool setExtNameOrMimeTypeToApp <options>\n"
449 "eg:bundle_test_tool getProxyDataInfos -m <module-name> -n <bundle-name> -a <ability-name>\n"
450 "options list:\n"
451 " -h, --help list available commands\n"
452 " -n, --bundle-name <bundle-name> specify bundle name of the application\n"
453 " -m, --module-name <module-name> specify module name of the application\n"
454 " -a, --ability-name <ability-name> specify ability name of the application\n"
455 " -e, --ext-name <ext-name> specify the ext-name\n"
456 " -t, --mime-type <mime-type> specify the mime-type\n";
457
458 const std::string HELP_MSG_QUERY_DATA_GROUP_INFOS =
459 "usage: bundle_test_tool queryDataGroupInfos <options>\n"
460 "eg:bundle_test_tool queryDataGroupInfos -n <bundle-name> -u <user-id>\n"
461 "options list:\n"
462 " -h, --help list available commands\n"
463 " -n, --bundle-name <bundle-name> specify bundle name of the application\n"
464 " -u, --user-id <user-id> specify a user id\n";
465
466 const std::string HELP_MSG_GET_GROUP_DIR =
467 "usage: bundle_test_tool getGroupDir <options>\n"
468 "eg:bundle_test_tool getGroupDir -d <data-group-id>\n"
469 "options list:\n"
470 " -h, --help list available commands\n"
471 " -d, --data-group-id <data-group-id> specify bundle name of the application\n";
472
473 const std::string STRING_SET_REMOVABLE_OK = "set removable is ok \n";
474 const std::string STRING_SET_REMOVABLE_NG = "error: failed to set removable \n";
475 const std::string STRING_GET_REMOVABLE_OK = "get removable is ok \n";
476 const std::string STRING_GET_REMOVABLE_NG = "error: failed to get removable \n";
477 const std::string STRING_REQUIRE_CORRECT_VALUE =
478 "error: option requires a correct value or note that\n"
479 "the difference in expressions between short option and long option. \n";
480
481 const std::string STRING_INSTALL_SANDBOX_SUCCESSFULLY = "install sandbox app successfully \n";
482 const std::string STRING_INSTALL_SANDBOX_FAILED = "install sandbox app failed \n";
483
484 const std::string STRING_UNINSTALL_SANDBOX_SUCCESSFULLY = "uninstall sandbox app successfully\n";
485 const std::string STRING_UNINSTALL_SANDBOX_FAILED = "uninstall sandbox app failed\n";
486
487 const std::string STRING_DUMP_SANDBOX_FAILED = "dump sandbox app info failed\n";
488
489 const std::string STRING_GET_STRING_NG = "error: failed to get label \n";
490
491 const std::string STRING_GET_ICON_NG = "error: failed to get icon \n";
492
493 const std::string STRING_ADD_RULE_NG = "error: failed to add rule \n";
494 const std::string STRING_GET_RULE_NG = "error: failed to get rule \n";
495 const std::string STRING_DELETE_RULE_NG = "error: failed to delete rule \n";
496
497 const std::string STRING_DEPLOY_QUICK_FIX_OK = "deploy quick fix successfully\n";
498 const std::string STRING_DEPLOY_QUICK_FIX_NG = "deploy quick fix failed\n";
499 const std::string HELP_MSG_NO_QUICK_FIX_PATH_OPTION = "need a quick fix patch path\n";
500 const std::string STRING_SWITCH_QUICK_FIX_OK = "switch quick fix successfully\n";
501 const std::string STRING_SWITCH_QUICK_FIX_NG = "switch quick fix failed\n";
502 const std::string STRING_DELETE_QUICK_FIX_OK = "delete quick fix successfully\n";
503 const std::string STRING_DELETE_QUICK_FIX_NG = "delete quick fix failed\n";
504
505 const std::string STRING_SET_DEBUG_MODE_OK = "set debug mode successfully\n";
506 const std::string STRING_SET_DEBUG_MODE_NG = "set debug mode failed\n";
507
508 const std::string STRING_GET_BUNDLE_STATS_OK = "get bundle stats successfully\n";
509 const std::string STRING_GET_BUNDLE_STATS_NG = "get bundle stats failed\n";
510
511 const std::string STRING_GET_APP_PROVISION_INFO_OK = "get appProvisionInfo successfully\n";
512 const std::string STRING_GET_APP_PROVISION_INFO_NG = "get appProvisionInfo failed\n";
513
514 const std::string STRING_QUERY_DATA_GROUP_INFOS_OK = "queryDataGroupInfos successfully\n";
515 const std::string STRING_QUERY_DATA_GROUP_INFOS_NG = "queryDataGroupInfos failed\n";
516
517 const std::string STRING_GET_GROUP_DIR_OK = "getGroupDir successfully\n";
518 const std::string STRING_GET_GROUP_DIR_NG = "getGroupDir failed\n";
519
520 const std::string HELP_MSG_NO_GET_DISTRIBUTED_BUNDLE_NAME_OPTION =
521 "error: you must specify a control type with '-n' or '--network-id' \n"
522 "and a accessTokenId with '-a' or '--access-token-id' \n";
523
524 const std::string GET_DISTRIBUTED_BUNDLE_NAME_COMMAND_NAME = "getDistributedBundleName";
525
526 const std::string STRING_GET_DISTRIBUTED_BUNDLE_NAME_OK = "get distributedBundleName successfully\n";
527 const std::string STRING_GET_DISTRIBUTED_BUNDLE_NAME_NG = "get distributedBundleName failed\n";
528
529 const std::string STRING_GET_PROXY_DATA_NG = "get proxyData failed";
530
531 const std::string GET_BUNDLE_STATS_ARRAY[] = {
532 "app data size: ",
533 "user data size: ",
534 "distributed data size: ",
535 "database size: ",
536 "cache size: "
537 };
538
539 const std::string GET_RM = "getrm";
540 const std::string SET_RM = "setrm";
541 const std::string INSTALL_SANDBOX = "installSandbox";
542 const std::string UNINSTALL_SANDBOX = "uninstallSandbox";
543 const std::string DUMP_SANDBOX = "dumpSandbox";
544
545 const std::string SHORT_OPTIONS = "hn:m:a:d:u:i:";
546 const struct option LONG_OPTIONS[] = {
547 {"help", no_argument, nullptr, 'h'},
548 {"bundle-name", required_argument, nullptr, 'n'},
549 {"module-name", required_argument, nullptr, 'm'},
550 {"ability-name", required_argument, nullptr, 'a'},
551 {"device-id", required_argument, nullptr, 'd'},
552 {"user-id", required_argument, nullptr, 'u'},
553 {"is-removable", required_argument, nullptr, 'i'},
554 {nullptr, 0, nullptr, 0},
555 };
556
557 const std::string SHORT_OPTIONS_SANDBOX = "hn:d:u:a:";
558 const struct option LONG_OPTIONS_SANDBOX[] = {
559 {"help", no_argument, nullptr, 'h'},
560 {"bundle-name", required_argument, nullptr, 'n'},
561 {"user-id", required_argument, nullptr, 'u'},
562 {"dlp-type", required_argument, nullptr, 'd'},
563 {"app-index", required_argument, nullptr, 'a'},
564 {nullptr, 0, nullptr, 0},
565 };
566
567 const std::string SHORT_OPTIONS_GET = "hn:m:u:i:d:";
568 const struct option LONG_OPTIONS_GET[] = {
569 {"help", no_argument, nullptr, 'h'},
570 {"bundle-name", required_argument, nullptr, 'n'},
571 {"module-name", required_argument, nullptr, 'm'},
572 {"user-id", required_argument, nullptr, 'u'},
573 {"id", required_argument, nullptr, 'i'},
574 {"density", required_argument, nullptr, 'd'},
575 {nullptr, 0, nullptr, 0},
576 };
577
578 const std::string SHORT_OPTIONS_RULE = "ha:c:n:e:r:t:u:";
579 const struct option LONG_OPTIONS_RULE[] = {
580 {"help", no_argument, nullptr, 'h'},
581 {"app-id", required_argument, nullptr, 'a'},
582 {"control-rule", required_argument, nullptr, 'c'},
583 {"bundle-name", required_argument, nullptr, 'n'},
584 {"bundle-name", required_argument, nullptr, 'n'},
585 {"euid", required_argument, nullptr, 'e'},
586 {"control-rule-type", required_argument, nullptr, 't'},
587 {"user-id", required_argument, nullptr, 'u'},
588 {nullptr, 0, nullptr, 0},
589 };
590
591 const std::string SHORT_OPTIONS_QUICK_FIX = "hp:n:e:";
592 const struct option LONG_OPTIONS_QUICK_FIX[] = {
593 {"help", no_argument, nullptr, 'h'},
594 {"patch-path", required_argument, nullptr, 'p'},
595 {"bundle-name", required_argument, nullptr, 'n'},
596 {"enable", required_argument, nullptr, 'e'},
597 {nullptr, 0, nullptr, 0},
598 };
599
600 const std::string SHORT_OPTIONS_DEBUG_MODE = "he:";
601 const struct option LONG_OPTIONS_DEBUG_MODE[] = {
602 {"help", no_argument, nullptr, 'h'},
603 {"enable", required_argument, nullptr, 'e'},
604 {nullptr, 0, nullptr, 0},
605 };
606
607 const std::string SHORT_OPTIONS_GET_BUNDLE_STATS = "hn:u:";
608 const struct option LONG_OPTIONS_GET_BUNDLE_STATS[] = {
609 {"help", no_argument, nullptr, 'h'},
610 {"bundle-name", required_argument, nullptr, 'n'},
611 {"user-id", required_argument, nullptr, 'u'},
612 {nullptr, 0, nullptr, 0},
613 };
614 const std::string SHORT_OPTIONS_GET_DISTRIBUTED_BUNDLE_NAME = "hn:a:";
615 const struct option LONG_OPTIONS_GET_DISTRIBUTED_BUNDLE_NAME[] = {
616 {"help", no_argument, nullptr, 'h'},
617 {"network-id", required_argument, nullptr, 'n'},
618 {"access-token-id", required_argument, nullptr, 'a'},
619 {nullptr, 0, nullptr, 0},
620 };
621
622 const std::string SHORT_OPTIONS_BUNDLE_EVENT_CALLBACK = "hou:";
623 const struct option LONG_OPTIONS_BUNDLE_EVENT_CALLBACK[] = {
624 {"help", no_argument, nullptr, 'h'},
625 {"onlyUnregister", no_argument, nullptr, 'o'},
626 {"uid", required_argument, nullptr, 'u'},
627 {nullptr, 0, nullptr, 0},
628 };
629
630 const std::string SHORT_OPTIONS_PROXY_DATA = "hn:m:u:";
631 const struct option LONG_OPTIONS_PROXY_DATA[] = {
632 {"help", no_argument, nullptr, 'h'},
633 {"bundle-name", required_argument, nullptr, 'n'},
634 {"module-name", required_argument, nullptr, 'm'},
635 {"user-id", required_argument, nullptr, 'u'},
636 {nullptr, 0, nullptr, 0},
637 };
638
639 const std::string SHORT_OPTIONS_ALL_PROXY_DATA = "hu:";
640 const struct option LONG_OPTIONS_ALL_PROXY_DATA[] = {
641 {"help", no_argument, nullptr, 'h'},
642 {"user-id", required_argument, nullptr, 'u'},
643 {nullptr, 0, nullptr, 0},
644 };
645
646 const std::string SHORT_OPTIONS_MIME = "ha:e:m:n:t:";
647 const struct option LONG_OPTIONS_MIME[] = {
648 {"help", no_argument, nullptr, 'h'},
649 {"ability-name", required_argument, nullptr, 'a'},
650 {"ext-name", required_argument, nullptr, 'e'},
651 {"module-name", required_argument, nullptr, 'm'},
652 {"bundle-name", required_argument, nullptr, 'n'},
653 {"mime-type", required_argument, nullptr, 't'},
654 {nullptr, 0, nullptr, 0},
655 };
656
657 const std::string SHORT_OPTIONS_GET_GROUP_DIR = "hd:";
658 const struct option LONG_OPTIONS_GET_GROUP_DIR[] = {
659 {"help", no_argument, nullptr, 'h'},
660 {"data-group-id", required_argument, nullptr, 'd'},
661 {nullptr, 0, nullptr, 0},
662 };
663 } // namespace
664
BundleEventCallbackImpl()665 BundleEventCallbackImpl::BundleEventCallbackImpl()
666 {
667 APP_LOGI("create BundleEventCallbackImpl");
668 }
669
~BundleEventCallbackImpl()670 BundleEventCallbackImpl::~BundleEventCallbackImpl()
671 {
672 APP_LOGI("destroy BundleEventCallbackImpl");
673 }
674
OnReceiveEvent(const EventFwk::CommonEventData eventData)675 void BundleEventCallbackImpl::OnReceiveEvent(const EventFwk::CommonEventData eventData)
676 {
677 const Want &want = eventData.GetWant();
678 std::string bundleName = want.GetElement().GetBundleName();
679 std::string moduleName = want.GetElement().GetModuleName();
680 APP_LOGI("OnReceiveEvent, bundleName:%{public}s, moduleName:%{public}s", bundleName.c_str(), moduleName.c_str());
681 }
682
BundleTestTool(int argc,char * argv[])683 BundleTestTool::BundleTestTool(int argc, char *argv[]) : ShellCommand(argc, argv, TOOL_NAME)
684 {}
685
~BundleTestTool()686 BundleTestTool::~BundleTestTool()
687 {}
688
CreateCommandMap()689 ErrCode BundleTestTool::CreateCommandMap()
690 {
691 commandMap_ = {
692 {"help", std::bind(&BundleTestTool::RunAsHelpCommand, this)},
693 {"check", std::bind(&BundleTestTool::RunAsCheckCommand, this)},
694 {"setrm", std::bind(&BundleTestTool::RunAsSetRemovableCommand, this)},
695 {"getrm", std::bind(&BundleTestTool::RunAsGetRemovableCommand, this)},
696 {"installSandbox", std::bind(&BundleTestTool::RunAsInstallSandboxCommand, this)},
697 {"uninstallSandbox", std::bind(&BundleTestTool::RunAsUninstallSandboxCommand, this)},
698 {"dumpSandbox", std::bind(&BundleTestTool::RunAsDumpSandboxCommand, this)},
699 {"getStr", std::bind(&BundleTestTool::RunAsGetStringCommand, this)},
700 {"getIcon", std::bind(&BundleTestTool::RunAsGetIconCommand, this)},
701 {"addAppInstallRule", std::bind(&BundleTestTool::RunAsAddInstallRuleCommand, this)},
702 {"getAppInstallRule", std::bind(&BundleTestTool::RunAsGetInstallRuleCommand, this)},
703 {"deleteAppInstallRule", std::bind(&BundleTestTool::RunAsDeleteInstallRuleCommand, this)},
704 {"cleanAppInstallRule", std::bind(&BundleTestTool::RunAsCleanInstallRuleCommand, this)},
705 {"addAppRunningRule", std::bind(&BundleTestTool::RunAsAddAppRunningRuleCommand, this)},
706 {"deleteAppRunningRule", std::bind(&BundleTestTool::RunAsDeleteAppRunningRuleCommand, this)},
707 {"cleanAppRunningRule", std::bind(&BundleTestTool::RunAsCleanAppRunningRuleCommand, this)},
708 {"getAppRunningControlRule", std::bind(&BundleTestTool::RunAsGetAppRunningControlRuleCommand, this)},
709 {"getAppRunningControlRuleResult",
710 std::bind(&BundleTestTool::RunAsGetAppRunningControlRuleResultCommand, this)},
711 {"deployQuickFix", std::bind(&BundleTestTool::RunAsDeployQuickFix, this)},
712 {"switchQuickFix", std::bind(&BundleTestTool::RunAsSwitchQuickFix, this)},
713 {"deleteQuickFix", std::bind(&BundleTestTool::RunAsDeleteQuickFix, this)},
714 {"setDebugMode", std::bind(&BundleTestTool::RunAsSetDebugMode, this)},
715 {"getBundleStats", std::bind(&BundleTestTool::RunAsGetBundleStats, this)},
716 {"getAppProvisionInfo", std::bind(&BundleTestTool::RunAsGetAppProvisionInfo, this)},
717 {"getDistributedBundleName", std::bind(&BundleTestTool::RunAsGetDistributedBundleName, this)},
718 {"eventCB", std::bind(&BundleTestTool::HandleBundleEventCallback, this)},
719 {"getProxyDataInfos", std::bind(&BundleTestTool::RunAsGetProxyDataCommand, this)},
720 {"getAllProxyDataInfos", std::bind(&BundleTestTool::RunAsGetAllProxyDataCommand, this)},
721 {"setExtNameOrMimeToApp", std::bind(&BundleTestTool::RunAsSetExtNameOrMIMEToAppCommand, this)},
722 {"delExtNameOrMimeToApp", std::bind(&BundleTestTool::RunAsDelExtNameOrMIMEToAppCommand, this)},
723 {"queryDataGroupInfos", std::bind(&BundleTestTool::RunAsQueryDataGroupInfos, this)},
724 {"getGroupDir", std::bind(&BundleTestTool::RunAsGetGroupDir, this)},
725 };
726
727 return OHOS::ERR_OK;
728 }
729
CreateMessageMap()730 ErrCode BundleTestTool::CreateMessageMap()
731 {
732 messageMap_ = BundleCommandCommon::bundleMessageMap_;
733
734 return OHOS::ERR_OK;
735 }
736
Init()737 ErrCode BundleTestTool::Init()
738 {
739 APP_LOGI("BundleTestTool Init()");
740 ErrCode result = OHOS::ERR_OK;
741 if (bundleMgrProxy_ == nullptr) {
742 bundleMgrProxy_ = BundleCommandCommon::GetBundleMgrProxy();
743 if (bundleMgrProxy_ != nullptr) {
744 if (bundleInstallerProxy_ == nullptr) {
745 bundleInstallerProxy_ = bundleMgrProxy_->GetBundleInstaller();
746 }
747 }
748 }
749
750 if ((bundleMgrProxy_ == nullptr) || (bundleInstallerProxy_ == nullptr) ||
751 (bundleInstallerProxy_->AsObject() == nullptr)) {
752 result = OHOS::ERR_INVALID_VALUE;
753 }
754
755 #ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
756 if (distributedBmsProxy_ == nullptr) {
757 distributedBmsProxy_ = BundleCommandCommon::GetDistributedBundleMgrService();
758 }
759 #endif
760
761 return result;
762 }
763
CreateQuickFixMsgMap(std::unordered_map<int32_t,std::string> & quickFixMsgMap)764 void BundleTestTool::CreateQuickFixMsgMap(std::unordered_map<int32_t, std::string> &quickFixMsgMap)
765 {
766 quickFixMsgMap = {
767 { ERR_OK, Constants::EMPTY_STRING },
768 { ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR },
769 { ERR_BUNDLEMANAGER_QUICK_FIX_PARAM_ERROR, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PARAM_ERROR },
770 { ERR_BUNDLEMANAGER_QUICK_FIX_PROFILE_PARSE_FAILED, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PROFILE_PARSE_FAILED },
771 { ERR_BUNDLEMANAGER_QUICK_FIX_BUNDLE_NAME_NOT_SAME, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_BUNDLE_NAME_NOT_SAME },
772 { ERR_BUNDLEMANAGER_QUICK_FIX_VERSION_CODE_NOT_SAME, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_VERSION_CODE_NOT_SAME },
773 { ERR_BUNDLEMANAGER_QUICK_FIX_VERSION_NAME_NOT_SAME, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_VERSION_NAME_NOT_SAME },
774 { ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_VERSION_CODE_NOT_SAME,
775 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_VERSION_CODE_NOT_SAME },
776 { ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_VERSION_NAME_NOT_SAME,
777 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_VERSION_NAME_NOT_SAME },
778 { ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_TYPE_NOT_SAME, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_TYPE_NOT_SAME },
779 { ERR_BUNDLEMANAGER_QUICK_FIX_UNKNOWN_QUICK_FIX_TYPE, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_UNKNOWN_QUICK_FIX_TYPE },
780 { ERR_BUNDLEMANAGER_QUICK_FIX_SO_INCOMPATIBLE, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_SO_INCOMPATIBLE },
781 { ERR_BUNDLEMANAGER_QUICK_FIX_BUNDLE_NAME_NOT_EXIST, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_BUNDLE_NAME_NOT_EXIST },
782 { ERR_BUNDLEMANAGER_QUICK_FIX_MODULE_NAME_NOT_EXIST, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_MODULE_NAME_NOT_EXIST },
783 { ERR_BUNDLEMANAGER_QUICK_FIX_SIGNATURE_INFO_NOT_SAME,
784 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_SIGNATURE_INFO_NOT_SAME },
785 { ERR_BUNDLEMANAGER_QUICK_FIX_EXTRACT_DIFF_FILES_FAILED,
786 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_EXTRACT_DIFF_FILES_FAILED },
787 { ERR_BUNDLEMANAGER_QUICK_FIX_APPLY_DIFF_PATCH_FAILED,
788 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_APPLY_DIFF_PATCH_FAILED },
789 { ERR_BUNDLEMANAGER_FEATURE_IS_NOT_SUPPORTED, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_FEATURE_IS_NOT_SUPPORTED },
790 { ERR_APPEXECFWK_OPERATION_TIME_OUT, MSG_ERR_BUNDLEMANAGER_OPERATION_TIME_OUT },
791 { ERR_APPEXECFWK_FAILED_SERVICE_DIED, MSG_ERR_BUNDLEMANAGER_FAILED_SERVICE_DIED },
792 { ERR_BUNDLEMANAGER_QUICK_FIX_HOT_RELOAD_NOT_SUPPORT_RELEASE_BUNDLE,
793 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_HOT_RELOAD_NOT_SUPPORT_RELEASE_BUNDLE },
794 { ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_ALREADY_EXISTED, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_PATCH_ALREADY_EXISTED },
795 { ERR_BUNDLEMANAGER_QUICK_FIX_HOT_RELOAD_ALREADY_EXISTED,
796 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_HOT_RELOAD_ALREADY_EXISTED },
797 { ERR_BUNDLEMANAGER_QUICK_FIX_MODULE_NAME_SAME, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_MODULE_NAME_SAME },
798 { ERR_BUNDLEMANAGER_QUICK_FIX_NO_PATCH_INFO_IN_BUNDLE_INFO,
799 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_NO_PATCH_INFO_IN_BUNDLE_INFO },
800 { ERR_BUNDLEMANAGER_SET_DEBUG_MODE_INVALID_PARAM, MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_INVALID_PARAM },
801 { ERR_BUNDLEMANAGER_SET_DEBUG_MODE_INTERNAL_ERROR, MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_INTERNAL_ERROR },
802 { ERR_BUNDLEMANAGER_SET_DEBUG_MODE_PARCEL_ERROR, MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_PARCEL_ERROR },
803 { ERR_BUNDLEMANAGER_SET_DEBUG_MODE_SEND_REQUEST_ERROR,
804 MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_SEND_REQUEST_ERROR },
805 { ERR_BUNDLEMANAGER_SET_DEBUG_MODE_UID_CHECK_FAILED, MSG_ERR_BUNDLEMANAGER_SET_DEBUG_MODE_UID_CHECK_FAILED },
806 { ERR_BUNDLEMANAGER_QUICK_FIX_ADD_HQF_FAILED, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_ADD_HQF_FAILED },
807 { ERR_BUNDLEMANAGER_QUICK_FIX_SAVE_APP_QUICK_FIX_FAILED,
808 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_SAVE_APP_QUICK_FIX_FAILED },
809 { ERR_BUNDLEMANAGER_QUICK_FIX_VERSION_CODE_ERROR, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_VERSION_CODE_ERROR },
810 { ERR_BUNDLEMANAGER_QUICK_FIX_NO_PATCH_IN_DATABASE, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_NO_PATCH_IN_DATABASE },
811 { ERR_BUNDLEMANAGER_QUICK_FIX_INVALID_PATCH_STATUS, MSG_ERR_BUNDLEMANAGER_QUICK_FIX_INVALID_PATCH_STATUS },
812 { ERR_BUNDLEMANAGER_QUICK_FIX_NOT_EXISTED_BUNDLE_INFO,
813 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_NOT_EXISTED_BUNDLE_INFO },
814 { ERR_BUNDLEMANAGER_QUICK_FIX_REMOVE_PATCH_PATH_FAILED,
815 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_REMOVE_PATCH_PATH_FAILED },
816 { ERR_BUNDLEMANAGER_QUICK_FIX_MOVE_PATCH_FILE_FAILED,
817 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_MOVE_PATCH_FILE_FAILED },
818 { ERR_BUNDLEMANAGER_QUICK_FIX_CREATE_PATCH_PATH_FAILED,
819 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_CREATE_PATCH_PATH_FAILED },
820 { ERR_BUNDLEMANAGER_QUICK_FIX_OLD_PATCH_OR_HOT_RELOAD_IN_DB,
821 MSG_ERR_BUNDLEMANAGER_QUICK_FIX_OLD_PATCH_OR_HOT_RELOAD_IN_DB }
822 };
823 }
824
RunAsHelpCommand()825 ErrCode BundleTestTool::RunAsHelpCommand()
826 {
827 resultReceiver_.append(HELP_MSG);
828
829 return OHOS::ERR_OK;
830 }
831
CheckOperation(int userId,std::string deviceId,std::string bundleName,std::string moduleName,std::string abilityName)832 ErrCode BundleTestTool::CheckOperation(int userId, std::string deviceId, std::string bundleName,
833 std::string moduleName, std::string abilityName)
834 {
835 std::unique_lock<std::mutex> lock(mutex_);
836 sptr<BundleToolCallbackStub> bundleToolCallbackStub =
837 new(std::nothrow) BundleToolCallbackStub(cv_, mutex_, dataReady_);
838 if (bundleToolCallbackStub == nullptr) {
839 APP_LOGE("bundleToolCallbackStub is null");
840 return OHOS::ERR_INVALID_VALUE;
841 }
842 APP_LOGI("CheckAbilityEnableInstall param: userId:%{public}d, bundleName:%{public}s, moduleName:%{public}s," \
843 "abilityName:%{public}s", userId, bundleName.c_str(), moduleName.c_str(), abilityName.c_str());
844 AAFwk::Want want;
845 want.SetElementName(deviceId, bundleName, abilityName, moduleName);
846 bool ret = bundleMgrProxy_->CheckAbilityEnableInstall(want, 1, userId, bundleToolCallbackStub);
847 if (!ret) {
848 APP_LOGE("CheckAbilityEnableInstall failed");
849 return OHOS::ERR_OK;
850 }
851 APP_LOGI("CheckAbilityEnableInstall wait");
852 cv_.wait(lock, [this] { return dataReady_; });
853 dataReady_ = false;
854 return OHOS::ERR_OK;
855 }
856
RunAsCheckCommand()857 ErrCode BundleTestTool::RunAsCheckCommand()
858 {
859 int counter = 0;
860 int userId = 100;
861 std::string deviceId = "";
862 std::string bundleName = "";
863 std::string moduleName = "";
864 std::string abilityName = "";
865 while (true) {
866 counter++;
867 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
868 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
869 if (optind < 0 || optind > argc_) {
870 return OHOS::ERR_INVALID_VALUE;
871 }
872 if (option == -1) {
873 // When scanning the first argument
874 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
875 // 'CheckAbilityEnableInstall' with no option: CheckAbilityEnableInstall
876 // 'CheckAbilityEnableInstall' with a wrong argument: CheckAbilityEnableInstall
877 APP_LOGD("'CheckAbilityEnableInstall' with no option.");
878 return OHOS::ERR_INVALID_VALUE;
879 }
880 break;
881 }
882 switch (option) {
883 case 'n': {
884 bundleName = optarg;
885 break;
886 }
887 case 'm': {
888 moduleName = optarg;
889 break;
890 }
891 case 'a': {
892 abilityName = optarg;
893 break;
894 }
895 case 'd': {
896 deviceId = optarg;
897 break;
898 }
899 case 'u': {
900 userId = std::stoi(optarg);
901 break;
902 }
903 default: {
904 return OHOS::ERR_INVALID_VALUE;
905 }
906 }
907 }
908 return CheckOperation(userId, deviceId, bundleName, moduleName, abilityName);
909 }
910
SetIsRemovableOperation(const std::string & bundleName,const std::string & moduleName,int isRemovable) const911 bool BundleTestTool::SetIsRemovableOperation(
912 const std::string &bundleName, const std::string &moduleName, int isRemovable) const
913 {
914 bool enable = true;
915 if (isRemovable == 0) {
916 enable = false;
917 }
918 APP_LOGD("bundleName: %{public}s, moduleName:%{public}s, enable:%{public}d", bundleName.c_str(), moduleName.c_str(),
919 enable);
920 auto ret = bundleMgrProxy_->SetModuleRemovable(bundleName, moduleName, enable);
921 APP_LOGD("SetModuleRemovable end bundleName: %{public}d", ret);
922 if (!ret) {
923 APP_LOGE("SetIsRemovableOperation failed");
924 return false;
925 }
926 return ret;
927 }
928
GetIsRemovableOperation(const std::string & bundleName,const std::string & moduleName,std::string & result) const929 bool BundleTestTool::GetIsRemovableOperation(
930 const std::string &bundleName, const std::string &moduleName, std::string &result) const
931 {
932 APP_LOGD("bundleName: %{public}s, moduleName:%{public}s", bundleName.c_str(), moduleName.c_str());
933 bool isRemovable = false;
934 auto ret = bundleMgrProxy_->IsModuleRemovable(bundleName, moduleName, isRemovable);
935 APP_LOGD("IsModuleRemovable end bundleName: %{public}s, isRemovable:%{public}d", bundleName.c_str(), isRemovable);
936 result.append("isRemovable: " + std::to_string(isRemovable) + "\n");
937 if (ret != ERR_OK) {
938 APP_LOGE("IsModuleRemovable failed, ret: %{public}d", ret);
939 return false;
940 }
941 return true;
942 }
943
CheckRemovableErrorOption(int option,int counter,const std::string & commandName)944 bool BundleTestTool::CheckRemovableErrorOption(int option, int counter, const std::string &commandName)
945 {
946 if (option == -1) {
947 if (counter == 1) {
948 if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
949 // 'bundle_test_tool setrm/getrm' with no option: bundle_test_tool setrm/getrm
950 // 'bundle_test_tool setrm/getrm' with a wrong argument: bundle_test_tool setrm/getrm xxx
951 APP_LOGD("'bundle_test_tool %{public}s' with no option.", commandName.c_str());
952 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
953 return false;
954 }
955 }
956 return true;
957 } else if (option == '?') {
958 switch (optopt) {
959 case 'i': {
960 if (commandName == GET_RM) {
961 std::string unknownOption = "";
962 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
963 APP_LOGD("'bundle_test_tool %{public}s' with an unknown option.", commandName.c_str());
964 resultReceiver_.append(unknownOptionMsg);
965 } else {
966 APP_LOGD("'bundle_test_tool %{public}s -i' with no argument.", commandName.c_str());
967 resultReceiver_.append("error: -i option requires a value.\n");
968 }
969 break;
970 }
971 case 'm': {
972 APP_LOGD("'bundle_test_tool %{public}s -m' with no argument.", commandName.c_str());
973 resultReceiver_.append("error: -m option requires a value.\n");
974 break;
975 }
976 case 'n': {
977 APP_LOGD("'bundle_test_tool %{public}s -n' with no argument.", commandName.c_str());
978 resultReceiver_.append("error: -n option requires a value.\n");
979 break;
980 }
981 default: {
982 std::string unknownOption = "";
983 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
984 APP_LOGD("'bundle_test_tool %{public}s' with an unknown option.", commandName.c_str());
985 resultReceiver_.append(unknownOptionMsg);
986 break;
987 }
988 }
989 }
990 return false;
991 }
992
CheckRemovableCorrectOption(int option,const std::string & commandName,int & isRemovable,std::string & name)993 bool BundleTestTool::CheckRemovableCorrectOption(
994 int option, const std::string &commandName, int &isRemovable, std::string &name)
995 {
996 bool ret = true;
997 switch (option) {
998 case 'h': {
999 APP_LOGD("'bundle_test_tool %{public}s %{public}s'", commandName.c_str(), argv_[optind - 1]);
1000 ret = false;
1001 break;
1002 }
1003 case 'n': {
1004 name = optarg;
1005 APP_LOGD("'bundle_test_tool %{public}s -n %{public}s'", commandName.c_str(), argv_[optind - 1]);
1006 break;
1007 }
1008 case 'i': {
1009 if (commandName == GET_RM) {
1010 std::string unknownOption = "";
1011 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1012 APP_LOGD("'bundle_test_tool %{public}s' with an unknown option.", commandName.c_str());
1013 resultReceiver_.append(unknownOptionMsg);
1014 ret = false;
1015 } else if (OHOS::StrToInt(optarg, isRemovable)) {
1016 APP_LOGD("'bundle_test_tool %{public}s -i isRemovable:%{public}d, %{public}s'",
1017 commandName.c_str(), isRemovable, argv_[optind - 1]);
1018 } else {
1019 APP_LOGE("bundle_test_tool setrm with error %{private}s", optarg);
1020 resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1021 ret = false;
1022 }
1023 break;
1024 }
1025 case 'm': {
1026 name = optarg;
1027 APP_LOGD("'bundle_test_tool %{public}s -m module-name:%{public}s, %{public}s'",
1028 commandName.c_str(), name.c_str(), argv_[optind - 1]);
1029 break;
1030 }
1031 default: {
1032 std::string unknownOption = "";
1033 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1034 APP_LOGD("'bundle_test_tool %{public}s' with an unknown option.", commandName.c_str());
1035 resultReceiver_.append(unknownOptionMsg);
1036 ret = false;
1037 break;
1038 }
1039 }
1040 return ret;
1041 }
1042
RunAsSetRemovableCommand()1043 ErrCode BundleTestTool::RunAsSetRemovableCommand()
1044 {
1045 int result = OHOS::ERR_OK;
1046 int counter = 0;
1047 int isRemovable = 0;
1048 std::string commandName = SET_RM;
1049 std::string name = "";
1050 std::string bundleName = "";
1051 std::string moduleName = "";
1052 APP_LOGD("RunAsSetCommand is start");
1053 while (true) {
1054 counter++;
1055 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
1056 if (optind < 0 || optind > argc_) {
1057 return OHOS::ERR_INVALID_VALUE;
1058 }
1059 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d, argv_[optind - 1]:%{public}s", option,
1060 optopt, optind, argv_[optind - 1]);
1061 if (option == -1 || option == '?') {
1062 result = !CheckRemovableErrorOption(option, counter, commandName)? OHOS::ERR_INVALID_VALUE : result;
1063 break;
1064 }
1065 result = !CheckRemovableCorrectOption(option, commandName, isRemovable, name)
1066 ? OHOS::ERR_INVALID_VALUE : result;
1067 moduleName = option == 'm' ? name : moduleName;
1068 bundleName = option == 'n' ? name : bundleName;
1069 }
1070 if (result == OHOS::ERR_OK) {
1071 if (resultReceiver_ == "" && (bundleName.size() == 0 || moduleName.size() == 0)) {
1072 APP_LOGD("'bundle_test_tool setrm' with not enough option.");
1073 resultReceiver_.append(HELP_MSG_NO_REMOVABLE_OPTION);
1074 result = OHOS::ERR_INVALID_VALUE;
1075 }
1076 }
1077 if (result != OHOS::ERR_OK) {
1078 resultReceiver_.append(HELP_MSG_SET);
1079 } else {
1080 bool setResult = false;
1081 setResult = SetIsRemovableOperation(bundleName, moduleName, isRemovable);
1082 APP_LOGD("'bundle_test_tool setrm' isRemovable is %{public}d", isRemovable);
1083 resultReceiver_ = setResult ? STRING_SET_REMOVABLE_OK : STRING_SET_REMOVABLE_NG;
1084 }
1085 return result;
1086 }
1087
RunAsGetRemovableCommand()1088 ErrCode BundleTestTool::RunAsGetRemovableCommand()
1089 {
1090 int result = OHOS::ERR_OK;
1091 int counter = 0;
1092 std::string commandName = GET_RM;
1093 std::string name = "";
1094 std::string bundleName = "";
1095 std::string moduleName = "";
1096 APP_LOGD("RunAsGetRemovableCommand is start");
1097 while (true) {
1098 counter++;
1099 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS.c_str(), LONG_OPTIONS, nullptr);
1100 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1101 if (optind < 0 || optind > argc_) {
1102 return OHOS::ERR_INVALID_VALUE;
1103 }
1104 if (option == -1 || option == '?') {
1105 result = !CheckRemovableErrorOption(option, counter, commandName) ? OHOS::ERR_INVALID_VALUE : result;
1106 break;
1107 }
1108 int tempIsRem = 0;
1109 result = !CheckRemovableCorrectOption(option, commandName, tempIsRem, name)
1110 ? OHOS::ERR_INVALID_VALUE : result;
1111 moduleName = option == 'm' ? name : moduleName;
1112 bundleName = option == 'n' ? name : bundleName;
1113 }
1114
1115 if (result == OHOS::ERR_OK) {
1116 if (resultReceiver_ == "" && (bundleName.size() == 0 || moduleName.size() == 0)) {
1117 APP_LOGD("'bundle_test_tool getrm' with no option.");
1118 resultReceiver_.append(HELP_MSG_NO_REMOVABLE_OPTION);
1119 result = OHOS::ERR_INVALID_VALUE;
1120 }
1121 }
1122
1123 if (result != OHOS::ERR_OK) {
1124 resultReceiver_.append(HELP_MSG_GET_REMOVABLE);
1125 } else {
1126 std::string results = "";
1127 GetIsRemovableOperation(bundleName, moduleName, results);
1128 if (results.empty()) {
1129 resultReceiver_.append(STRING_GET_REMOVABLE_NG);
1130 return result;
1131 }
1132 resultReceiver_.append(results);
1133 }
1134 return result;
1135 }
1136
CheckSandboxErrorOption(int option,int counter,const std::string & commandName)1137 bool BundleTestTool::CheckSandboxErrorOption(int option, int counter, const std::string &commandName)
1138 {
1139 if (option == -1) {
1140 if (counter == 1) {
1141 if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
1142 APP_LOGD("'bundle_test_tool %{public}s' with no option.", commandName.c_str());
1143 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
1144 return false;
1145 }
1146 }
1147 return true;
1148 } else if (option == '?') {
1149 switch (optopt) {
1150 case 'n':
1151 case 'u':
1152 case 'd':
1153 case 'a': {
1154 if ((commandName != INSTALL_SANDBOX && optopt == 'd') ||
1155 (commandName == INSTALL_SANDBOX && optopt == 'a')) {
1156 std::string unknownOption = "";
1157 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1158 APP_LOGD("'bundle_test_tool %{public}s' with an unknown option.", commandName.c_str());
1159 resultReceiver_.append(unknownOptionMsg);
1160 break;
1161 }
1162 APP_LOGD("'bundle_test_tool %{public}s' -%{public}c with no argument.", commandName.c_str(), optopt);
1163 resultReceiver_.append("error: option requires a value.\n");
1164 break;
1165 }
1166 default: {
1167 std::string unknownOption = "";
1168 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1169 APP_LOGD("'bundle_test_tool %{public}s' with an unknown option.", commandName.c_str());
1170 resultReceiver_.append(unknownOptionMsg);
1171 break;
1172 }
1173 }
1174 }
1175 return false;
1176 }
1177
CheckSandboxCorrectOption(int option,const std::string & commandName,int & data,std::string & bundleName)1178 bool BundleTestTool::CheckSandboxCorrectOption(
1179 int option, const std::string &commandName, int &data, std::string &bundleName)
1180 {
1181 bool ret = true;
1182 switch (option) {
1183 case 'h': {
1184 APP_LOGD("'bundle_test_tool %{public}s %{public}s'", commandName.c_str(), argv_[optind - 1]);
1185 ret = false;
1186 break;
1187 }
1188 case 'n': {
1189 APP_LOGD("'bundle_test_tool %{public}s %{public}s'", commandName.c_str(), argv_[optind - 1]);
1190 bundleName = optarg;
1191 break;
1192 }
1193 case 'u':
1194 case 'a':
1195 case 'd': {
1196 if ((commandName != INSTALL_SANDBOX && option == 'd') ||
1197 (commandName == INSTALL_SANDBOX && option == 'a')) {
1198 std::string unknownOption = "";
1199 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1200 APP_LOGD("'bundle_test_tool %{public}s' with an unknown option.", commandName.c_str());
1201 resultReceiver_.append(unknownOptionMsg);
1202 ret = false;
1203 break;
1204 }
1205
1206 APP_LOGD("'bundle_test_tool %{public}s %{public}s %{public}s'", commandName.c_str(),
1207 argv_[optind - OFFSET_REQUIRED_ARGUMENT], optarg);
1208
1209 if (!OHOS::StrToInt(optarg, data)) {
1210 if (option == 'u') {
1211 APP_LOGE("bundle_test_tool %{public}s with error -u %{private}s", commandName.c_str(), optarg);
1212 } else if (option == 'a') {
1213 APP_LOGE("bundle_test_tool %{public}s with error -a %{private}s", commandName.c_str(), optarg);
1214 } else {
1215 APP_LOGE("bundle_test_tool %{public}s with error -d %{private}s", commandName.c_str(), optarg);
1216 }
1217 resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
1218 ret = false;
1219 }
1220 break;
1221 }
1222 default: {
1223 ret = false;
1224 break;
1225 }
1226 }
1227 return ret;
1228 }
1229
InstallSandboxOperation(const std::string & bundleName,const int32_t userId,const int32_t dlpType,int32_t & appIndex) const1230 ErrCode BundleTestTool::InstallSandboxOperation(
1231 const std::string &bundleName, const int32_t userId, const int32_t dlpType, int32_t &appIndex) const
1232 {
1233 APP_LOGD("InstallSandboxOperation of bundleName %{public}s, dipType is %{public}d", bundleName.c_str(), dlpType);
1234 return bundleInstallerProxy_->InstallSandboxApp(bundleName, dlpType, userId, appIndex);
1235 }
1236
RunAsInstallSandboxCommand()1237 ErrCode BundleTestTool::RunAsInstallSandboxCommand()
1238 {
1239 int result = OHOS::ERR_OK;
1240 int counter = 0;
1241 std::string commandName = INSTALL_SANDBOX;
1242 std::string bundleName = "";
1243 int32_t userId = 100;
1244 int32_t dlpType = 0;
1245 while (true) {
1246 counter++;
1247 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_SANDBOX.c_str(), LONG_OPTIONS_SANDBOX, nullptr);
1248 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1249 if (optind < 0 || optind > argc_) {
1250 return OHOS::ERR_INVALID_VALUE;
1251 }
1252 if (option == -1 || option == '?') {
1253 result = !CheckSandboxErrorOption(option, counter, commandName) ? OHOS::ERR_INVALID_VALUE : result;
1254 break;
1255 } else if (option == 'u') {
1256 result = !CheckSandboxCorrectOption(option, commandName, userId, bundleName) ?
1257 OHOS::ERR_INVALID_VALUE : result;
1258 } else {
1259 result = !CheckSandboxCorrectOption(option, commandName, dlpType, bundleName) ?
1260 OHOS::ERR_INVALID_VALUE : result;
1261 }
1262 }
1263
1264 if (result == OHOS::ERR_OK && bundleName == "") {
1265 resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION);
1266 result = OHOS::ERR_INVALID_VALUE;
1267 } else {
1268 APP_LOGD("installSandbox app bundleName is %{public}s", bundleName.c_str());
1269 }
1270
1271 if (result != OHOS::ERR_OK) {
1272 resultReceiver_.append(HELP_MSG_INSTALL_SANDBOX);
1273 return result;
1274 }
1275
1276 int32_t appIndex = 0;
1277 auto ret = InstallSandboxOperation(bundleName, userId, dlpType, appIndex);
1278 if (ret == OHOS::ERR_OK) {
1279 resultReceiver_.append(STRING_INSTALL_SANDBOX_SUCCESSFULLY);
1280 } else {
1281 resultReceiver_.append(STRING_INSTALL_SANDBOX_FAILED + "errCode is "+ std::to_string(ret) + "\n");
1282 }
1283 return result;
1284 }
1285
UninstallSandboxOperation(const std::string & bundleName,const int32_t appIndex,const int32_t userId) const1286 ErrCode BundleTestTool::UninstallSandboxOperation(const std::string &bundleName,
1287 const int32_t appIndex, const int32_t userId) const
1288 {
1289 APP_LOGD("UninstallSandboxOperation of bundleName %{public}s_%{public}d", bundleName.c_str(), appIndex);
1290 return bundleInstallerProxy_->UninstallSandboxApp(bundleName, appIndex, userId);
1291 }
1292
RunAsUninstallSandboxCommand()1293 ErrCode BundleTestTool::RunAsUninstallSandboxCommand()
1294 {
1295 int result = OHOS::ERR_OK;
1296 int counter = 0;
1297 std::string bundleName = "";
1298 std::string commandName = UNINSTALL_SANDBOX;
1299 int32_t userId = 100;
1300 int32_t appIndex = -1;
1301 while (true) {
1302 counter++;
1303 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_SANDBOX.c_str(), LONG_OPTIONS_SANDBOX, nullptr);
1304 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1305 if (optind < 0 || optind > argc_) {
1306 return OHOS::ERR_INVALID_VALUE;
1307 }
1308
1309 if (option == -1 || option == '?') {
1310 result = !CheckSandboxErrorOption(option, counter, commandName) ? OHOS::ERR_INVALID_VALUE : result;
1311 break;
1312 } else if (option == 'u') {
1313 result = !CheckSandboxCorrectOption(option, commandName, userId, bundleName) ?
1314 OHOS::ERR_INVALID_VALUE : result;
1315 } else {
1316 result = !CheckSandboxCorrectOption(option, commandName, appIndex, bundleName) ?
1317 OHOS::ERR_INVALID_VALUE : result;
1318 }
1319 }
1320
1321 if (result == OHOS::ERR_OK && bundleName == "") {
1322 resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION);
1323 result = OHOS::ERR_INVALID_VALUE;
1324 } else {
1325 APP_LOGD("uninstallSandbox app bundleName is %{private}s", bundleName.c_str());
1326 }
1327
1328 if (result != OHOS::ERR_OK) {
1329 resultReceiver_.append(HELP_MSG_UNINSTALL_SANDBOX);
1330 return result;
1331 }
1332
1333 auto ret = UninstallSandboxOperation(bundleName, appIndex, userId);
1334 if (ret == ERR_OK) {
1335 resultReceiver_.append(STRING_UNINSTALL_SANDBOX_SUCCESSFULLY);
1336 } else {
1337 resultReceiver_.append(STRING_UNINSTALL_SANDBOX_FAILED + "errCode is " + std::to_string(ret) + "\n");
1338 }
1339 return result;
1340 }
1341
DumpSandboxBundleInfo(const std::string & bundleName,const int32_t appIndex,const int32_t userId,std::string & dumpResults)1342 ErrCode BundleTestTool::DumpSandboxBundleInfo(const std::string &bundleName, const int32_t appIndex,
1343 const int32_t userId, std::string &dumpResults)
1344 {
1345 APP_LOGD("DumpSandboxBundleInfo of bundleName %{public}s_%{public}d", bundleName.c_str(), appIndex);
1346 BundleInfo bundleInfo;
1347 BundleMgrClient client;
1348 auto dumpRet = client.GetSandboxBundleInfo(bundleName, appIndex, userId, bundleInfo);
1349 if (dumpRet == ERR_OK) {
1350 nlohmann::json jsonObject = bundleInfo;
1351 jsonObject["applicationInfo"] = bundleInfo.applicationInfo;
1352 dumpResults= jsonObject.dump(Constants::DUMP_INDENT);
1353 }
1354 return dumpRet;
1355 }
1356
RunAsDumpSandboxCommand()1357 ErrCode BundleTestTool::RunAsDumpSandboxCommand()
1358 {
1359 int result = OHOS::ERR_OK;
1360 int counter = 0;
1361 std::string bundleName = "";
1362 std::string commandName = DUMP_SANDBOX;
1363 int32_t userId = 100;
1364 int32_t appIndex = -1;
1365 while (true) {
1366 counter++;
1367 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_SANDBOX.c_str(), LONG_OPTIONS_SANDBOX, nullptr);
1368 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1369 if (optind < 0 || optind > argc_) {
1370 return OHOS::ERR_INVALID_VALUE;
1371 }
1372 if (option == -1 || option == '?') {
1373 result = !CheckSandboxErrorOption(option, counter, commandName) ? OHOS::ERR_INVALID_VALUE : result;
1374 break;
1375 } else if (option == 'u') {
1376 result = !CheckSandboxCorrectOption(option, commandName, userId, bundleName) ?
1377 OHOS::ERR_INVALID_VALUE : result;
1378 } else {
1379 result = !CheckSandboxCorrectOption(option, commandName, appIndex, bundleName) ?
1380 OHOS::ERR_INVALID_VALUE : result;
1381 }
1382 }
1383
1384 if (result == OHOS::ERR_OK && bundleName == "") {
1385 resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION);
1386 result = OHOS::ERR_INVALID_VALUE;
1387 } else {
1388 APP_LOGD("dumpSandbox app bundleName is %{public}s", bundleName.c_str());
1389 }
1390
1391 if (result != OHOS::ERR_OK) {
1392 resultReceiver_.append(HELP_MSG_DUMP_SANDBOX);
1393 return result;
1394 }
1395
1396 std::string dumpRes = "";
1397 ErrCode ret = DumpSandboxBundleInfo(bundleName, appIndex, userId, dumpRes);
1398 if (ret == ERR_OK) {
1399 resultReceiver_.append(dumpRes + "\n");
1400 } else {
1401 resultReceiver_.append(STRING_DUMP_SANDBOX_FAILED + "errCode is "+ std::to_string(ret) + "\n");
1402 }
1403 return result;
1404 }
1405
StringToInt(std::string optarg,const std::string & commandName,int & temp,bool & result)1406 ErrCode BundleTestTool::StringToInt(
1407 std::string optarg, const std::string &commandName, int &temp, bool &result)
1408 {
1409 try {
1410 temp = std::stoi(optarg);
1411 APP_LOGD("bundle_test_tool %{public}s -u user-id:%{public}d, %{public}s",
1412 commandName.c_str(), temp, argv_[optind - 1]);
1413 } catch (const std::exception& e) {
1414 std::cerr << e.what() << std::endl;
1415 result = false;
1416 }
1417 return OHOS::ERR_OK;
1418 }
1419
CheckGetStringCorrectOption(int option,const std::string & commandName,int & temp,std::string & name)1420 bool BundleTestTool::CheckGetStringCorrectOption(
1421 int option, const std::string &commandName, int &temp, std::string &name)
1422 {
1423 bool ret = true;
1424 switch (option) {
1425 case 'h': {
1426 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
1427 ret = false;
1428 break;
1429 }
1430 case 'n': {
1431 name = optarg;
1432 APP_LOGD("bundle_test_tool %{public}s -n %{public}s", commandName.c_str(), argv_[optind - 1]);
1433 break;
1434 }
1435 case 'm': {
1436 name = optarg;
1437 APP_LOGD("bundle_test_tool %{public}s -m module-name:%{public}s, %{public}s",
1438 commandName.c_str(), name.c_str(), argv_[optind - 1]);
1439 break;
1440 }
1441 case 'u': {
1442 StringToInt(optarg, commandName, temp, ret);
1443 break;
1444 }
1445 case 'i': {
1446 StringToInt(optarg, commandName, temp, ret);
1447 break;
1448 }
1449 default: {
1450 std::string unknownOption = "";
1451 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1452 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
1453 resultReceiver_.append(unknownOptionMsg);
1454 ret = false;
1455 break;
1456 }
1457 }
1458 return ret;
1459 }
1460
CheckGetProxyDataCorrectOption(int option,const std::string & commandName,int & temp,std::string & name)1461 bool BundleTestTool::CheckGetProxyDataCorrectOption(
1462 int option, const std::string &commandName, int &temp, std::string &name)
1463 {
1464 bool ret = true;
1465 switch (option) {
1466 case 'h': {
1467 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
1468 ret = false;
1469 break;
1470 }
1471 case 'n': {
1472 name = optarg;
1473 APP_LOGD("bundle_test_tool %{public}s -n %{public}s", commandName.c_str(), argv_[optind - 1]);
1474 break;
1475 }
1476 case 'm': {
1477 name = optarg;
1478 APP_LOGD("bundle_test_tool %{public}s -m module-name:%{public}s, %{public}s",
1479 commandName.c_str(), name.c_str(), argv_[optind - 1]);
1480 break;
1481 }
1482 case 'u': {
1483 StringToInt(optarg, commandName, temp, ret);
1484 break;
1485 }
1486 default: {
1487 std::string unknownOption = "";
1488 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1489 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
1490 resultReceiver_.append(unknownOptionMsg);
1491 ret = false;
1492 break;
1493 }
1494 }
1495 return ret;
1496 }
1497
CheckGetAllProxyDataCorrectOption(int option,const std::string & commandName,int & temp,std::string & name)1498 bool BundleTestTool::CheckGetAllProxyDataCorrectOption(
1499 int option, const std::string &commandName, int &temp, std::string &name)
1500 {
1501 bool ret = true;
1502 switch (option) {
1503 case 'h': {
1504 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
1505 ret = false;
1506 break;
1507 }
1508 case 'u': {
1509 StringToInt(optarg, commandName, temp, ret);
1510 break;
1511 }
1512 default: {
1513 std::string unknownOption = "";
1514 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1515 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
1516 resultReceiver_.append(unknownOptionMsg);
1517 ret = false;
1518 break;
1519 }
1520 }
1521 return ret;
1522 }
1523
RunAsGetProxyDataCommand()1524 ErrCode BundleTestTool::RunAsGetProxyDataCommand()
1525 {
1526 int result = OHOS::ERR_OK;
1527 int counter = 0;
1528 std::string commandName = "getProxyData";
1529 std::string name = "";
1530 std::string bundleName = "";
1531 std::string moduleName = "";
1532 int userId = Constants::ALL_USERID;
1533 APP_LOGD("RunAsGetProxyDataCommand is start");
1534 while (true) {
1535 counter++;
1536 int32_t option = getopt_long(
1537 argc_, argv_, SHORT_OPTIONS_PROXY_DATA.c_str(), LONG_OPTIONS_PROXY_DATA, nullptr);
1538 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1539 if (optind < 0 || optind > argc_) {
1540 return OHOS::ERR_INVALID_VALUE;
1541 }
1542 if (option == -1) {
1543 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
1544 APP_LOGD("bundle_test_tool getProxyData with no option.");
1545 resultReceiver_.append(HELP_MSG_GET_PROXY_DATA);
1546 return OHOS::ERR_INVALID_VALUE;
1547 }
1548 break;
1549 }
1550 int temp = 0;
1551 result = !CheckGetProxyDataCorrectOption(option, commandName, temp, name)
1552 ? OHOS::ERR_INVALID_VALUE : result;
1553 moduleName = option == 'm' ? name : moduleName;
1554 bundleName = option == 'n' ? name : bundleName;
1555 userId = option == 'u' ? temp : userId;
1556 }
1557
1558 if (result != OHOS::ERR_OK) {
1559 resultReceiver_.append(HELP_MSG_GET_PROXY_DATA);
1560 } else {
1561 std::vector<ProxyData> proxyDatas;
1562 result = bundleMgrProxy_->GetProxyDataInfos(bundleName, moduleName, proxyDatas, userId);
1563 if (result == ERR_OK) {
1564 nlohmann::json jsonObject = proxyDatas;
1565 std::string results = jsonObject.dump(Constants::DUMP_INDENT);
1566 resultReceiver_.append(results);
1567 } else {
1568 resultReceiver_.append(STRING_GET_PROXY_DATA_NG + " errCode is "+ std::to_string(result) + "\n");
1569 }
1570 }
1571 return result;
1572 }
1573
RunAsGetAllProxyDataCommand()1574 ErrCode BundleTestTool::RunAsGetAllProxyDataCommand()
1575 {
1576 int result = OHOS::ERR_OK;
1577 int counter = 0;
1578 std::string commandName = "getProxyData";
1579 std::string name = "";
1580 int userId = Constants::ALL_USERID;
1581 APP_LOGD("RunAsGetAllProxyDataCommand is start");
1582 while (true) {
1583 counter++;
1584 int32_t option = getopt_long(
1585 argc_, argv_, SHORT_OPTIONS_ALL_PROXY_DATA.c_str(), LONG_OPTIONS_ALL_PROXY_DATA, nullptr);
1586 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1587 if (optind < 0 || optind > argc_) {
1588 return OHOS::ERR_INVALID_VALUE;
1589 }
1590 if (option == -1) {
1591 break;
1592 }
1593
1594 int temp = 0;
1595 result = !CheckGetAllProxyDataCorrectOption(option, commandName, temp, name)
1596 ? OHOS::ERR_INVALID_VALUE : result;
1597 userId = option == 'u' ? temp : userId;
1598 }
1599
1600 if (result != OHOS::ERR_OK) {
1601 resultReceiver_.append(HELP_MSG_GET_ALL_PROXY_DATA);
1602 } else {
1603 std::vector<ProxyData> proxyDatas;
1604 result = bundleMgrProxy_->GetAllProxyDataInfos(proxyDatas, userId);
1605 if (result == ERR_OK) {
1606 nlohmann::json jsonObject = proxyDatas;
1607 std::string results = jsonObject.dump(Constants::DUMP_INDENT);
1608 resultReceiver_.append(results);
1609 } else {
1610 resultReceiver_.append(STRING_GET_PROXY_DATA_NG + " errCode is "+ std::to_string(result) + "\n");
1611 }
1612 }
1613 return result;
1614 }
1615
RunAsGetStringCommand()1616 ErrCode BundleTestTool::RunAsGetStringCommand()
1617 {
1618 int result = OHOS::ERR_OK;
1619 int counter = 0;
1620 std::string commandName = "getStr";
1621 std::string name = "";
1622 std::string bundleName = "";
1623 std::string moduleName = "";
1624 int userId = 100;
1625 int labelId = 0;
1626 APP_LOGD("RunAsGetStringCommand is start");
1627 while (true) {
1628 counter++;
1629 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_GET.c_str(), LONG_OPTIONS_GET, nullptr);
1630 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1631 if (optind < 0 || optind > argc_) {
1632 return OHOS::ERR_INVALID_VALUE;
1633 }
1634 if (option == -1) {
1635 // When scanning the first argument
1636 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
1637 // 'GetStringById' with no option: GetStringById
1638 // 'GetStringById' with a wrong argument: GetStringById
1639 APP_LOGD("bundle_test_tool getStr with no option.");
1640 resultReceiver_.append(HELP_MSG_NO_GETSTRING_OPTION);
1641 return OHOS::ERR_INVALID_VALUE;
1642 }
1643 break;
1644 }
1645 int temp = 0;
1646 result = !CheckGetStringCorrectOption(option, commandName, temp, name)
1647 ? OHOS::ERR_INVALID_VALUE : result;
1648 moduleName = option == 'm' ? name : moduleName;
1649 bundleName = option == 'n' ? name : bundleName;
1650 userId = option == 'u' ? temp : userId;
1651 labelId = option == 'i' ? temp : labelId;
1652 }
1653
1654 if (result != OHOS::ERR_OK) {
1655 resultReceiver_.append(HELP_MSG_GET_STRING);
1656 } else {
1657 std::string results = "";
1658 results = bundleMgrProxy_->GetStringById(bundleName, moduleName, labelId, userId);
1659 if (results.empty()) {
1660 resultReceiver_.append(STRING_GET_STRING_NG);
1661 return result;
1662 }
1663 resultReceiver_.append(results);
1664 }
1665 return result;
1666 }
1667
CheckExtOrMimeCorrectOption(int option,const std::string & commandName,int & temp,std::string & name)1668 bool BundleTestTool::CheckExtOrMimeCorrectOption(
1669 int option, const std::string &commandName, int &temp, std::string &name)
1670 {
1671 bool ret = true;
1672 switch (option) {
1673 case 'h': {
1674 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
1675 ret = false;
1676 break;
1677 }
1678 case 'n': {
1679 name = optarg;
1680 APP_LOGD("bundle_test_tool %{public}s -n %{public}s", commandName.c_str(), argv_[optind - 1]);
1681 break;
1682 }
1683 case 'm': {
1684 name = optarg;
1685 APP_LOGD("bundle_test_tool %{public}s -m module-name:%{public}s, %{public}s",
1686 commandName.c_str(), name.c_str(), argv_[optind - 1]);
1687 break;
1688 }
1689 case 'a': {
1690 name = optarg;
1691 APP_LOGD("bundle_test_tool %{public}s -m ability-name:%{public}s, %{public}s",
1692 commandName.c_str(), name.c_str(), argv_[optind - 1]);
1693 break;
1694 }
1695 case 'e': {
1696 name = optarg;
1697 APP_LOGD("bundle_test_tool %{public}s -m ext-name:%{public}s, %{public}s",
1698 commandName.c_str(), name.c_str(), argv_[optind - 1]);
1699 break;
1700 }
1701 case 't': {
1702 name = optarg;
1703 APP_LOGD("bundle_test_tool %{public}s -m mime-type:%{public}s, %{public}s",
1704 commandName.c_str(), name.c_str(), argv_[optind - 1]);
1705 break;
1706 }
1707 default: {
1708 std::string unknownOption = "";
1709 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1710 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
1711 resultReceiver_.append(unknownOptionMsg);
1712 ret = false;
1713 break;
1714 }
1715 }
1716 return ret;
1717 }
1718
RunAsSetExtNameOrMIMEToAppCommand()1719 ErrCode BundleTestTool::RunAsSetExtNameOrMIMEToAppCommand()
1720 {
1721 int result = OHOS::ERR_OK;
1722 int counter = 0;
1723 std::string commandName = "setExtNameOrMimeToApp";
1724 std::string name = "";
1725 std::string bundleName = "";
1726 std::string moduleName = "";
1727 std::string abilityName = "";
1728 std::string extName = "";
1729 std::string mimeType = "";
1730 APP_LOGD("RunAsSetExtNameOrMIMEToAppCommand is start");
1731 while (true) {
1732 counter++;
1733 int32_t option = getopt_long(
1734 argc_, argv_, SHORT_OPTIONS_MIME.c_str(), LONG_OPTIONS_MIME, nullptr);
1735 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1736 if (optind < 0 || optind > argc_) {
1737 return OHOS::ERR_INVALID_VALUE;
1738 }
1739 if (option == -1) {
1740 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
1741 APP_LOGD("bundle_test_tool RunAsSetExtNameOrMIMEToAppCommand with no option.");
1742 resultReceiver_.append(HELP_MSG_SET_EXT_NAME_OR_MIME_TYPE);
1743 return OHOS::ERR_INVALID_VALUE;
1744 }
1745 break;
1746 }
1747 int temp = 0;
1748 result = !CheckExtOrMimeCorrectOption(option, commandName, temp, name)
1749 ? OHOS::ERR_INVALID_VALUE : result;
1750 moduleName = option == 'm' ? name : moduleName;
1751 bundleName = option == 'n' ? name : bundleName;
1752 abilityName = option == 'a' ? name : abilityName;
1753 extName = option == 'e' ? name : extName;
1754 mimeType = option == 't' ? name : mimeType;
1755 }
1756
1757 if (result != OHOS::ERR_OK) {
1758 resultReceiver_.append(HELP_MSG_GET_PROXY_DATA);
1759 } else {
1760 result = bundleMgrProxy_->SetExtNameOrMIMEToApp(bundleName, moduleName, abilityName, extName, mimeType);
1761 if (result == ERR_OK) {
1762 resultReceiver_.append("SetExtNameOrMIMEToApp succeeded,");
1763 } else {
1764 resultReceiver_.append("SetExtNameOrMIMEToApp failed, errCode is "+ std::to_string(result) + "\n");
1765 }
1766 }
1767 return result;
1768 }
1769
RunAsDelExtNameOrMIMEToAppCommand()1770 ErrCode BundleTestTool::RunAsDelExtNameOrMIMEToAppCommand()
1771 {
1772 int result = OHOS::ERR_OK;
1773 int counter = 0;
1774 std::string commandName = "delExtNameOrMimeToApp";
1775 std::string name = "";
1776 std::string bundleName = "";
1777 std::string moduleName = "";
1778 std::string abilityName = "";
1779 std::string extName = "";
1780 std::string mimeType = "";
1781 APP_LOGD("RunAsDelExtNameOrMIMEToAppCommand is start");
1782 while (true) {
1783 counter++;
1784 int32_t option = getopt_long(
1785 argc_, argv_, SHORT_OPTIONS_MIME.c_str(), LONG_OPTIONS_MIME, nullptr);
1786 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1787 if (optind < 0 || optind > argc_) {
1788 return OHOS::ERR_INVALID_VALUE;
1789 }
1790 if (option == -1) {
1791 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
1792 APP_LOGD("bundle_test_tool RunAsDelExtNameOrMIMEToAppCommand with no option.");
1793 resultReceiver_.append(HELP_MSG_SET_EXT_NAME_OR_MIME_TYPE);
1794 return OHOS::ERR_INVALID_VALUE;
1795 }
1796 break;
1797 }
1798 int temp = 0;
1799 result = !CheckExtOrMimeCorrectOption(option, commandName, temp, name)
1800 ? OHOS::ERR_INVALID_VALUE : result;
1801 moduleName = option == 'm' ? name : moduleName;
1802 bundleName = option == 'n' ? name : bundleName;
1803 abilityName = option == 'a' ? name : abilityName;
1804 extName = option == 'e' ? name : extName;
1805 mimeType = option == 't' ? name : mimeType;
1806 }
1807
1808 if (result != OHOS::ERR_OK) {
1809 resultReceiver_.append(HELP_MSG_GET_PROXY_DATA);
1810 } else {
1811 result = bundleMgrProxy_->DelExtNameOrMIMEToApp(bundleName, moduleName, abilityName, extName, mimeType);
1812 if (result == ERR_OK) {
1813 resultReceiver_.append("DelExtNameOrMIMEToApp succeeded");
1814 } else {
1815 resultReceiver_.append("DelExtNameOrMIMEToApp failed, errCode is "+ std::to_string(result) + "\n");
1816 }
1817 }
1818 return result;
1819 }
1820
CheckGetIconCorrectOption(int option,const std::string & commandName,int & temp,std::string & name)1821 bool BundleTestTool::CheckGetIconCorrectOption(
1822 int option, const std::string &commandName, int &temp, std::string &name)
1823 {
1824 bool ret = true;
1825 switch (option) {
1826 case 'h': {
1827 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
1828 ret = false;
1829 break;
1830 }
1831 case 'n': {
1832 name = optarg;
1833 APP_LOGD("bundle_test_tool %{public}s -n %{public}s", commandName.c_str(), argv_[optind - 1]);
1834 break;
1835 }
1836 case 'm': {
1837 name = optarg;
1838 APP_LOGD("bundle_test_tool %{public}s -m module-name:%{public}s, %{public}s",
1839 commandName.c_str(), name.c_str(), argv_[optind - 1]);
1840 break;
1841 }
1842 case 'u': {
1843 StringToInt(optarg, commandName, temp, ret);
1844 break;
1845 }
1846 case 'i': {
1847 StringToInt(optarg, commandName, temp, ret);
1848 break;
1849 }
1850 case 'd': {
1851 StringToInt(optarg, commandName, temp, ret);
1852 break;
1853 }
1854 default: {
1855 std::string unknownOption = "";
1856 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1857 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
1858 resultReceiver_.append(unknownOptionMsg);
1859 ret = false;
1860 break;
1861 }
1862 }
1863 return ret;
1864 }
1865
RunAsGetIconCommand()1866 ErrCode BundleTestTool::RunAsGetIconCommand()
1867 {
1868 int result = OHOS::ERR_OK;
1869 int counter = 0;
1870 std::string commandName = "getIcon";
1871 std::string name = "";
1872 std::string bundleName = "";
1873 std::string moduleName = "";
1874 int userId = 100;
1875 int iconId = 0;
1876 int density = 0;
1877 APP_LOGD("RunAsGetIconCommand is start");
1878 while (true) {
1879 counter++;
1880 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_GET.c_str(), LONG_OPTIONS_GET, nullptr);
1881 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
1882 if (optind < 0 || optind > argc_) {
1883 return OHOS::ERR_INVALID_VALUE;
1884 }
1885 if (option == -1) {
1886 // When scanning the first argument
1887 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
1888 // 'GetIconById' with no option: GetStringById
1889 // 'GetIconById' with a wrong argument: GetStringById
1890 APP_LOGD("bundle_test_tool getIcon with no option.");
1891 resultReceiver_.append(HELP_MSG_NO_GETICON_OPTION);
1892 return OHOS::ERR_INVALID_VALUE;
1893 }
1894 break;
1895 }
1896 int temp = 0;
1897 result = !CheckGetIconCorrectOption(option, commandName, temp, name)
1898 ? OHOS::ERR_INVALID_VALUE : result;
1899 moduleName = option == 'm' ? name : moduleName;
1900 bundleName = option == 'n' ? name : bundleName;
1901 userId = option == 'u' ? temp : userId;
1902 iconId = option == 'i' ? temp : iconId;
1903 density = option == 'd' ? temp : density;
1904 }
1905
1906 if (result != OHOS::ERR_OK) {
1907 resultReceiver_.append(HELP_MSG_GET_ICON);
1908 } else {
1909 std::string results = "";
1910 results = bundleMgrProxy_->GetIconById(bundleName, moduleName, iconId, density, userId);
1911 if (results.empty()) {
1912 resultReceiver_.append(STRING_GET_ICON_NG);
1913 return result;
1914 }
1915 resultReceiver_.append(results);
1916 }
1917 return result;
1918 }
1919
CheckAddInstallRuleCorrectOption(int option,const std::string & commandName,std::vector<std::string> & appIds,int & controlRuleType,int & userId,int & euid)1920 ErrCode BundleTestTool::CheckAddInstallRuleCorrectOption(int option, const std::string &commandName,
1921 std::vector<std::string> &appIds, int &controlRuleType, int &userId, int &euid)
1922 {
1923 bool ret = true;
1924 switch (option) {
1925 case 'h': {
1926 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
1927 return OHOS::ERR_INVALID_VALUE;
1928 }
1929 case 'a': {
1930 std::string arrayAppId = optarg;
1931 std::stringstream array(arrayAppId);
1932 std::string object;
1933 while (getline(array, object, ',')) {
1934 appIds.emplace_back(object);
1935 }
1936 APP_LOGD("bundle_test_tool %{public}s -a %{public}s", commandName.c_str(), argv_[optind - 1]);
1937 break;
1938 }
1939 case 'e': {
1940 StringToInt(optarg, commandName, euid, ret);
1941 break;
1942 }
1943 case 't': {
1944 StringToInt(optarg, commandName, controlRuleType, ret);
1945 break;
1946 }
1947 case 'u': {
1948 StringToInt(optarg, commandName, userId, ret);
1949 break;
1950 }
1951 default: {
1952 std::string unknownOption = "";
1953 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
1954 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
1955 resultReceiver_.append(unknownOptionMsg);
1956 return OHOS::ERR_INVALID_VALUE;
1957 }
1958 }
1959 return OHOS::ERR_OK;
1960 }
1961
1962 // bundle_test_tool addAppInstallRule -a test1,test2 -t 1 -u 101 -e 3057
RunAsAddInstallRuleCommand()1963 ErrCode BundleTestTool::RunAsAddInstallRuleCommand()
1964 {
1965 ErrCode result = OHOS::ERR_OK;
1966 int counter = 0;
1967 std::string commandName = "addAppInstallRule";
1968 std::vector<std::string> appIds;
1969 int euid = 3057;
1970 int userId = 100;
1971 int ruleType = 0;
1972 APP_LOGD("RunAsAddInstallRuleCommand is start");
1973 while (true) {
1974 counter++;
1975 int option = getopt_long(argc_, argv_, SHORT_OPTIONS_RULE.c_str(), LONG_OPTIONS_RULE, nullptr);
1976 if (optind < 0 || optind > argc_) {
1977 return OHOS::ERR_INVALID_VALUE;
1978 }
1979 if (option == -1) {
1980 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
1981 resultReceiver_.append(HELP_MSG_NO_ADD_INSTALL_RULE_OPTION);
1982 return OHOS::ERR_INVALID_VALUE;
1983 }
1984 break;
1985 }
1986 result = CheckAddInstallRuleCorrectOption(option, commandName, appIds, ruleType, userId, euid);
1987 if (result != OHOS::ERR_OK) {
1988 resultReceiver_.append(HELP_MSG_ADD_INSTALL_RULE);
1989 return OHOS::ERR_INVALID_VALUE;
1990 }
1991 }
1992 seteuid(euid);
1993 auto rule = static_cast<AppInstallControlRuleType>(ruleType);
1994 auto appControlProxy = bundleMgrProxy_->GetAppControlProxy();
1995 if (!appControlProxy) {
1996 APP_LOGE("fail to get app control proxy.");
1997 return OHOS::ERR_INVALID_VALUE;
1998 }
1999 std::string appIdParam = "";
2000 for (auto param : appIds) {
2001 appIdParam = appIdParam.append(param) + ";";
2002 }
2003 APP_LOGI("appIds: %{public}s, controlRuleType: %{public}d, userId: %{public}d",
2004 appIdParam.c_str(), ruleType, userId);
2005 int32_t res = appControlProxy->AddAppInstallControlRule(appIds, rule, userId);
2006 APP_LOGI("AddAppInstallControlRule return code: %{public}d", res);
2007 if (res != OHOS::ERR_OK) {
2008 resultReceiver_.append(STRING_ADD_RULE_NG);
2009 return res;
2010 }
2011 resultReceiver_.append(std::to_string(res) + "\n");
2012 return result;
2013 }
2014
CheckGetInstallRuleCorrectOption(int option,const std::string & commandName,int & controlRuleType,int & userId,int & euid)2015 ErrCode BundleTestTool::CheckGetInstallRuleCorrectOption(int option, const std::string &commandName,
2016 int &controlRuleType, int &userId, int &euid)
2017 {
2018 bool ret = true;
2019 switch (option) {
2020 case 'h': {
2021 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
2022 return OHOS::ERR_INVALID_VALUE;
2023 }
2024 case 'e': {
2025 StringToInt(optarg, commandName, euid, ret);
2026 break;
2027 }
2028 case 't': {
2029 StringToInt(optarg, commandName, controlRuleType, ret);
2030 break;
2031 }
2032 case 'u': {
2033 StringToInt(optarg, commandName, userId, ret);
2034 break;
2035 }
2036 default: {
2037 std::string unknownOption = "";
2038 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
2039 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
2040 resultReceiver_.append(unknownOptionMsg);
2041 return OHOS::ERR_INVALID_VALUE;
2042 }
2043 }
2044 return OHOS::ERR_OK;
2045 }
2046
2047 // bundle_test_tool getAppInstallRule -t 1 -u 101 -e 3057
RunAsGetInstallRuleCommand()2048 ErrCode BundleTestTool::RunAsGetInstallRuleCommand()
2049 {
2050 ErrCode result = OHOS::ERR_OK;
2051 int counter = 0;
2052 std::string commandName = "getAppInstallRule";
2053 int euid = 3057;
2054 int userId = 100;
2055 int ruleType = 0;
2056 APP_LOGD("RunAsGetInstallRuleCommand is start");
2057 while (true) {
2058 counter++;
2059 int option = getopt_long(argc_, argv_, SHORT_OPTIONS_RULE.c_str(), LONG_OPTIONS_RULE, nullptr);
2060 if (optind < 0 || optind > argc_) {
2061 return OHOS::ERR_INVALID_VALUE;
2062 }
2063 if (option == -1) {
2064 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
2065 resultReceiver_.append(HELP_MSG_NO_GET_INSTALL_RULE_OPTION);
2066 return OHOS::ERR_INVALID_VALUE;
2067 }
2068 break;
2069 }
2070 result = CheckGetInstallRuleCorrectOption(option, commandName, ruleType, userId, euid);
2071 if (result != OHOS::ERR_OK) {
2072 resultReceiver_.append(HELP_MSG_GET_INSTALL_RULE);
2073 return OHOS::ERR_INVALID_VALUE;
2074 }
2075 }
2076 seteuid(euid);
2077 auto appControlProxy = bundleMgrProxy_->GetAppControlProxy();
2078 if (!appControlProxy) {
2079 APP_LOGE("fail to get app control proxy.");
2080 return OHOS::ERR_INVALID_VALUE;
2081 }
2082 APP_LOGI("controlRuleType: %{public}d, userId: %{public}d", ruleType, userId);
2083 auto rule = static_cast<AppInstallControlRuleType>(ruleType);
2084 std::vector<std::string> appIds;
2085 int32_t res = appControlProxy->GetAppInstallControlRule(rule, userId, appIds);
2086 APP_LOGI("GetAppInstallControlRule return code: %{public}d", res);
2087 if (res != OHOS::ERR_OK) {
2088 resultReceiver_.append(STRING_GET_RULE_NG);
2089 return res;
2090 }
2091 std::string appIdParam = "";
2092 for (auto param : appIds) {
2093 appIdParam = appIdParam.append(param) + "; ";
2094 }
2095 resultReceiver_.append("appId : " + appIdParam + "\n");
2096 return result;
2097 }
2098
CheckDeleteInstallRuleCorrectOption(int option,const std::string & commandName,int & controlRuleType,std::vector<std::string> & appIds,int & userId,int & euid)2099 ErrCode BundleTestTool::CheckDeleteInstallRuleCorrectOption(int option, const std::string &commandName,
2100 int &controlRuleType, std::vector<std::string> &appIds, int &userId, int &euid)
2101 {
2102 bool ret = true;
2103 switch (option) {
2104 case 'h': {
2105 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
2106 return OHOS::ERR_INVALID_VALUE;
2107 }
2108 case 'a': {
2109 std::string arrayAppId = optarg;
2110 std::stringstream array(arrayAppId);
2111 std::string object;
2112 while (getline(array, object, ',')) {
2113 appIds.emplace_back(object);
2114 }
2115 APP_LOGD("bundle_test_tool %{public}s -a %{public}s", commandName.c_str(), argv_[optind - 1]);
2116 break;
2117 }
2118 case 'e': {
2119 StringToInt(optarg, commandName, euid, ret);
2120 break;
2121 }
2122 case 't': {
2123 StringToInt(optarg, commandName, controlRuleType, ret);
2124 break;
2125 }
2126 case 'u': {
2127 StringToInt(optarg, commandName, userId, ret);
2128 break;
2129 }
2130 default: {
2131 std::string unknownOption = "";
2132 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
2133 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
2134 resultReceiver_.append(unknownOptionMsg);
2135 return OHOS::ERR_INVALID_VALUE;
2136 }
2137 }
2138 return OHOS::ERR_OK;
2139 }
2140
2141 // bundle_test_tool deleteAppInstallRule -a test1 -t 1 -u 101 -e 3057
RunAsDeleteInstallRuleCommand()2142 ErrCode BundleTestTool::RunAsDeleteInstallRuleCommand()
2143 {
2144 ErrCode result = OHOS::ERR_OK;
2145 int counter = 0;
2146 int euid = 3057;
2147 std::string commandName = "deleteAppInstallRule";
2148 std::vector<std::string> appIds;
2149 int ruleType = 0;
2150 int userId = 100;
2151 APP_LOGD("RunAsDeleteInstallRuleCommand is start");
2152 while (true) {
2153 counter++;
2154 int option = getopt_long(argc_, argv_, SHORT_OPTIONS_RULE.c_str(), LONG_OPTIONS_RULE, nullptr);
2155 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2156 if (optind < 0 || optind > argc_) {
2157 return OHOS::ERR_INVALID_VALUE;
2158 }
2159 if (option == -1) {
2160 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
2161 resultReceiver_.append(HELP_MSG_NO_DELETE_INSTALL_RULE_OPTION);
2162 return OHOS::ERR_INVALID_VALUE;
2163 }
2164 break;
2165 }
2166 result = CheckDeleteInstallRuleCorrectOption(option, commandName, ruleType, appIds, userId, euid);
2167 if (result != OHOS::ERR_OK) {
2168 resultReceiver_.append(HELP_MSG_DELETE_INSTALL_RULE);
2169 return OHOS::ERR_INVALID_VALUE;
2170 }
2171 }
2172 seteuid(euid);
2173 auto appControlProxy = bundleMgrProxy_->GetAppControlProxy();
2174 if (!appControlProxy) {
2175 APP_LOGE("fail to get app control proxy.");
2176 return OHOS::ERR_INVALID_VALUE;
2177 }
2178 std::string appIdParam = "";
2179 for (auto param : appIds) {
2180 appIdParam = appIdParam.append(param) + ";";
2181 }
2182 APP_LOGI("appIds: %{public}s, userId: %{public}d", appIdParam.c_str(), userId);
2183 auto rule = static_cast<AppInstallControlRuleType>(ruleType);
2184 int32_t res = appControlProxy->DeleteAppInstallControlRule(rule, appIds, userId);
2185 APP_LOGI("DeleteAppInstallControlRule return code: %{public}d", res);
2186 if (res != OHOS::ERR_OK) {
2187 resultReceiver_.append(STRING_DELETE_RULE_NG);
2188 return res;
2189 }
2190 resultReceiver_.append(std::to_string(res) + "\n");
2191 return result;
2192 }
2193
CheckCleanInstallRuleCorrectOption(int option,const std::string & commandName,int & controlRuleType,int & userId,int & euid)2194 ErrCode BundleTestTool::CheckCleanInstallRuleCorrectOption(
2195 int option, const std::string &commandName, int &controlRuleType, int &userId, int &euid)
2196 {
2197 bool ret = true;
2198 switch (option) {
2199 case 'h': {
2200 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
2201 return OHOS::ERR_INVALID_VALUE;
2202 }
2203 case 'e': {
2204 StringToInt(optarg, commandName, euid, ret);
2205 break;
2206 }
2207 case 't': {
2208 StringToInt(optarg, commandName, controlRuleType, ret);
2209 break;
2210 }
2211 case 'u': {
2212 StringToInt(optarg, commandName, userId, ret);
2213 break;
2214 }
2215 default: {
2216 std::string unknownOption = "";
2217 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
2218 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
2219 resultReceiver_.append(unknownOptionMsg);
2220 return OHOS::ERR_INVALID_VALUE;
2221 }
2222 }
2223 return OHOS::ERR_OK;
2224 }
2225
2226 // bundle_test_tool cleanAppInstallRule -t 1 -u 101 -e 3057
RunAsCleanInstallRuleCommand()2227 ErrCode BundleTestTool::RunAsCleanInstallRuleCommand()
2228 {
2229 ErrCode result = OHOS::ERR_OK;
2230 int counter = 0;
2231 int euid = 3057;
2232 std::string commandName = "cleanAppInstallRule";
2233 int userId = 100;
2234 int ruleType = 0;
2235 APP_LOGD("RunAsCleanInstallRuleCommand is start");
2236 while (true) {
2237 counter++;
2238 int option = getopt_long(argc_, argv_, SHORT_OPTIONS_RULE.c_str(), LONG_OPTIONS_RULE, nullptr);
2239 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2240 if (optind < 0 || optind > argc_) {
2241 return OHOS::ERR_INVALID_VALUE;
2242 }
2243 if (option == -1) {
2244 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
2245 APP_LOGD("bundle_test_tool getRule with no option.");
2246 resultReceiver_.append(HELP_MSG_NO_CLEAN_INSTALL_RULE_OPTION);
2247 return OHOS::ERR_INVALID_VALUE;
2248 }
2249 break;
2250 }
2251 result = CheckCleanInstallRuleCorrectOption(option, commandName, ruleType, userId, euid);
2252 if (result != OHOS::ERR_OK) {
2253 resultReceiver_.append(HELP_MSG_NO_CLEAN_INSTALL_RULE_OPTION);
2254 return OHOS::ERR_INVALID_VALUE;
2255 }
2256 }
2257 seteuid(euid);
2258 auto rule = static_cast<AppInstallControlRuleType>(ruleType);
2259 auto appControlProxy = bundleMgrProxy_->GetAppControlProxy();
2260 if (!appControlProxy) {
2261 APP_LOGE("fail to get app control proxy.");
2262 return OHOS::ERR_INVALID_VALUE;
2263 }
2264 APP_LOGI("controlRuleType: %{public}d, userId: %{public}d", ruleType, userId);
2265 int32_t res = appControlProxy->DeleteAppInstallControlRule(rule, userId);
2266 APP_LOGI("DeleteAppInstallControlRule clean return code: %{public}d", res);
2267 if (res != OHOS::ERR_OK) {
2268 resultReceiver_.append(STRING_DELETE_RULE_NG);
2269 return res;
2270 }
2271 resultReceiver_.append(std::to_string(res) + "\n");
2272 return result;
2273 }
2274
CheckAppRunningRuleCorrectOption(int option,const std::string & commandName,std::vector<AppRunningControlRule> & controlRule,int & userId,int & euid)2275 ErrCode BundleTestTool::CheckAppRunningRuleCorrectOption(int option, const std::string &commandName,
2276 std::vector<AppRunningControlRule> &controlRule, int &userId, int &euid)
2277 {
2278 bool ret = true;
2279 switch (option) {
2280 case 'h': {
2281 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
2282 return OHOS::ERR_INVALID_VALUE;
2283 }
2284 case 'c': {
2285 std::string arrayJsonRule = optarg;
2286 std::stringstream array(arrayJsonRule);
2287 std::string object;
2288 while (getline(array, object, ';')) {
2289 size_t pos1 = object.find("appId");
2290 size_t pos2 = object.find("controlMessage");
2291 size_t pos3 = object.find(":", pos2);
2292 if ((pos1 == std::string::npos) || (pos2 == std::string::npos)) {
2293 return OHOS::ERR_INVALID_VALUE;
2294 }
2295 std::string appId = object.substr(pos1+6, pos2-pos1-7);
2296 std::string controlMessage = object.substr(pos3+1);
2297 AppRunningControlRule rule;
2298 rule.appId = appId;
2299 rule.controlMessage = controlMessage;
2300 controlRule.emplace_back(rule);
2301 }
2302 break;
2303 }
2304 case 'e': {
2305 StringToInt(optarg, commandName, euid, ret);
2306 break;
2307 }
2308 case 'u': {
2309 StringToInt(optarg, commandName, userId, ret);
2310 break;
2311 }
2312 default: {
2313 std::string unknownOption = "";
2314 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
2315 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
2316 resultReceiver_.append(unknownOptionMsg);
2317 return OHOS::ERR_INVALID_VALUE;
2318 }
2319 }
2320 return OHOS::ERR_OK;
2321 }
2322
2323 // bundle_test_tool addAppRunningRule -c appId:id1,controlMessage:msg1;appId:id2,controlMessage:msg2
2324 // -u 101 -e 3057
RunAsAddAppRunningRuleCommand()2325 ErrCode BundleTestTool::RunAsAddAppRunningRuleCommand()
2326 {
2327 ErrCode result = OHOS::ERR_OK;
2328 int counter = 0;
2329 int euid = 3057;
2330 std::string commandName = "addAppRunningRule";
2331 int userId = 100;
2332 std::vector<AppRunningControlRule> controlRule;
2333 APP_LOGD("RunAsAddAppRunningRuleCommand is start");
2334 while (true) {
2335 counter++;
2336 int option = getopt_long(argc_, argv_, SHORT_OPTIONS_RULE.c_str(), LONG_OPTIONS_RULE, nullptr);
2337 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2338 if (optind < 0 || optind > argc_) {
2339 return OHOS::ERR_INVALID_VALUE;
2340 }
2341 if (option == -1) {
2342 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
2343 APP_LOGD("bundle_test_tool getRule with no option.");
2344 resultReceiver_.append(HELP_MSG_NO_APP_RUNNING_RULE_OPTION);
2345 return OHOS::ERR_INVALID_VALUE;
2346 }
2347 break;
2348 }
2349 result = CheckAppRunningRuleCorrectOption(option, commandName, controlRule, userId, euid);
2350 if (result != OHOS::ERR_OK) {
2351 resultReceiver_.append(HELP_MSG_ADD_APP_RUNNING_RULE);
2352 return OHOS::ERR_INVALID_VALUE;
2353 }
2354 }
2355 seteuid(euid);
2356 auto appControlProxy = bundleMgrProxy_->GetAppControlProxy();
2357 if (!appControlProxy) {
2358 APP_LOGE("fail to get app control proxy.");
2359 return OHOS::ERR_INVALID_VALUE;
2360 }
2361 std::string appIdParam = "";
2362 for (auto param : controlRule) {
2363 appIdParam = appIdParam.append("appId:"+ param.appId + ":" + "message" + param.controlMessage);
2364 }
2365 APP_LOGI("appRunningControlRule: %{public}s, userId: %{public}d", appIdParam.c_str(), userId);
2366 int32_t res = appControlProxy->AddAppRunningControlRule(controlRule, userId);
2367 if (res != OHOS::ERR_OK) {
2368 resultReceiver_.append(STRING_ADD_RULE_NG);
2369 return res;
2370 }
2371 resultReceiver_.append(std::to_string(res) + "\n");
2372 return result;
2373 }
2374
2375 // bundle_test_tool deleteAppRunningRule -c appId:101,controlMessage:msg1 -u 101 -e 3057
RunAsDeleteAppRunningRuleCommand()2376 ErrCode BundleTestTool::RunAsDeleteAppRunningRuleCommand()
2377 {
2378 ErrCode result = OHOS::ERR_OK;
2379 int counter = 0;
2380 int euid = 3057;
2381 std::string commandName = "addAppRunningRule";
2382 int userId = 100;
2383 std::vector<AppRunningControlRule> controlRule;
2384 APP_LOGD("RunAsDeleteAppRunningRuleCommand is start");
2385 while (true) {
2386 counter++;
2387 int option = getopt_long(argc_, argv_, SHORT_OPTIONS_RULE.c_str(), LONG_OPTIONS_RULE, nullptr);
2388 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2389 if (optind < 0 || optind > argc_) {
2390 return OHOS::ERR_INVALID_VALUE;
2391 }
2392 if (option == -1) {
2393 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
2394 APP_LOGD("bundle_test_tool getRule with no option.");
2395 resultReceiver_.append(HELP_MSG_NO_APP_RUNNING_RULE_OPTION);
2396 return OHOS::ERR_INVALID_VALUE;
2397 }
2398 break;
2399 }
2400 result = CheckAppRunningRuleCorrectOption(option, commandName, controlRule, userId, euid);
2401 if (result != OHOS::ERR_OK) {
2402 resultReceiver_.append(HELP_MSG_DELETE_APP_RUNNING_RULE);
2403 return OHOS::ERR_INVALID_VALUE;
2404 }
2405 }
2406 seteuid(euid);
2407 auto appControlProxy = bundleMgrProxy_->GetAppControlProxy();
2408 if (!appControlProxy) {
2409 APP_LOGE("fail to get app control proxy.");
2410 return OHOS::ERR_INVALID_VALUE;
2411 }
2412 std::string appIdParam = "";
2413 for (auto param : controlRule) {
2414 appIdParam = appIdParam.append("appId:"+ param.appId + ":" + "message" + param.controlMessage);
2415 }
2416 APP_LOGI("appRunningControlRule: %{public}s, userId: %{public}d", appIdParam.c_str(), userId);
2417 int32_t res = appControlProxy->DeleteAppRunningControlRule(controlRule, userId);
2418 if (res != OHOS::ERR_OK) {
2419 resultReceiver_.append(STRING_DELETE_RULE_NG);
2420 return res;
2421 }
2422 resultReceiver_.append(std::to_string(res) + "\n");
2423 return result;
2424 }
2425
CheckCleanAppRunningRuleCorrectOption(int option,const std::string & commandName,int & userId,int & euid)2426 ErrCode BundleTestTool::CheckCleanAppRunningRuleCorrectOption(
2427 int option, const std::string &commandName, int &userId, int &euid)
2428 {
2429 bool ret = true;
2430 switch (option) {
2431 case 'h': {
2432 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
2433 return OHOS::ERR_INVALID_VALUE;
2434 }
2435 case 'e': {
2436 StringToInt(optarg, commandName, euid, ret);
2437 break;
2438 }
2439 case 'u': {
2440 StringToInt(optarg, commandName, userId, ret);
2441 break;
2442 }
2443 default: {
2444 std::string unknownOption = "";
2445 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
2446 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
2447 resultReceiver_.append(unknownOptionMsg);
2448 return OHOS::ERR_INVALID_VALUE;
2449 }
2450 }
2451 return OHOS::ERR_OK;
2452 }
2453
2454 // bundle_test_tool cleanAppRunningRule -u 101 -e 3057
RunAsCleanAppRunningRuleCommand()2455 ErrCode BundleTestTool::RunAsCleanAppRunningRuleCommand()
2456 {
2457 ErrCode result = OHOS::ERR_OK;
2458 int counter = 0;
2459 int euid = 3057;
2460 std::string commandName = "addAppRunningRule";
2461 int userId = 100;
2462 APP_LOGD("RunAsCleanAppRunningRuleCommand is start");
2463 while (true) {
2464 counter++;
2465 int option = getopt_long(argc_, argv_, SHORT_OPTIONS_RULE.c_str(), LONG_OPTIONS_RULE, nullptr);
2466 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2467 if (optind < 0 || optind > argc_) {
2468 return OHOS::ERR_INVALID_VALUE;
2469 }
2470 if (option == -1) {
2471 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
2472 APP_LOGD("bundle_test_tool getRule with no option.");
2473 resultReceiver_.append(HELP_MSG_NO_CLEAN_APP_RUNNING_RULE_OPTION);
2474 return OHOS::ERR_INVALID_VALUE;
2475 }
2476 break;
2477 }
2478 result = CheckCleanAppRunningRuleCorrectOption(option, commandName, userId, euid);
2479 if (result != OHOS::ERR_OK) {
2480 resultReceiver_.append(HELP_MSG_CLEAN_APP_RUNNING_RULE);
2481 return OHOS::ERR_INVALID_VALUE;
2482 }
2483 }
2484 seteuid(euid);
2485 auto appControlProxy = bundleMgrProxy_->GetAppControlProxy();
2486 if (!appControlProxy) {
2487 APP_LOGE("fail to get app control proxy.");
2488 return OHOS::ERR_INVALID_VALUE;
2489 }
2490 APP_LOGI("userId: %{public}d", userId);
2491 int32_t res = appControlProxy->DeleteAppRunningControlRule(userId);
2492 if (res != OHOS::ERR_OK) {
2493 resultReceiver_.append(STRING_DELETE_RULE_NG);
2494 return res;
2495 }
2496 resultReceiver_.append(std::to_string(res) + "\n");
2497 return result;
2498 }
2499
CheckGetAppRunningRuleCorrectOption(int option,const std::string & commandName,int32_t & userId,int & euid)2500 ErrCode BundleTestTool::CheckGetAppRunningRuleCorrectOption(int option, const std::string &commandName,
2501 int32_t &userId, int &euid)
2502 {
2503 bool ret = true;
2504 switch (option) {
2505 case 'h': {
2506 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
2507 return OHOS::ERR_INVALID_VALUE;
2508 }
2509 case 'e': {
2510 StringToInt(optarg, commandName, euid, ret);
2511 break;
2512 }
2513 case 'u': {
2514 StringToInt(optarg, commandName, userId, ret);
2515 break;
2516 }
2517 default: {
2518 std::string unknownOption = "";
2519 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
2520 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
2521 resultReceiver_.append(unknownOptionMsg);
2522 return OHOS::ERR_INVALID_VALUE;
2523 }
2524 }
2525 return OHOS::ERR_OK ;
2526 }
2527
2528 // bundle_test_tool getAppRunningControlRule -u 101 -e 3057
RunAsGetAppRunningControlRuleCommand()2529 ErrCode BundleTestTool::RunAsGetAppRunningControlRuleCommand()
2530 {
2531 ErrCode result = OHOS::ERR_OK;
2532 int counter = 0;
2533 int euid = 3057;
2534 std::string commandName = "addAppRunningRule";
2535 int userId = 100;
2536 APP_LOGD("RunAsGetAppRunningControlRuleCommand is start");
2537 while (true) {
2538 counter++;
2539 int option = getopt_long(argc_, argv_, SHORT_OPTIONS_RULE.c_str(), LONG_OPTIONS_RULE, nullptr);
2540 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2541 if (optind < 0 || optind > argc_) {
2542 return OHOS::ERR_INVALID_VALUE;
2543 }
2544 if (option == -1) {
2545 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
2546 APP_LOGD("bundle_test_tool getRule with no option.");
2547 resultReceiver_.append(HELP_MSG_NO_GET_ALL_APP_RUNNING_RULE_OPTION);
2548 return OHOS::ERR_INVALID_VALUE;
2549 }
2550 break;
2551 }
2552 result = CheckGetAppRunningRuleCorrectOption(option, commandName, userId, euid);
2553 if (result != OHOS::ERR_OK) {
2554 resultReceiver_.append(HELP_MSG_GET_APP_RUNNING_RULE);
2555 return OHOS::ERR_INVALID_VALUE;
2556 }
2557 }
2558 seteuid(euid);
2559 auto appControlProxy = bundleMgrProxy_->GetAppControlProxy();
2560 if (!appControlProxy) {
2561 APP_LOGE("fail to get app control proxy.");
2562 return OHOS::ERR_INVALID_VALUE;
2563 }
2564 APP_LOGI("userId: %{public}d", userId);
2565 std::vector<std::string> appIds;
2566 int32_t res = appControlProxy->GetAppRunningControlRule(userId, appIds);
2567 if (res != OHOS::ERR_OK) {
2568 resultReceiver_.append(STRING_GET_RULE_NG);
2569 return res;
2570 }
2571 std::string appIdParam = "";
2572 for (auto param : appIds) {
2573 appIdParam = appIdParam.append(param) + "; ";
2574 }
2575 resultReceiver_.append("appId : " + appIdParam + "\n");
2576 return result;
2577 }
2578
CheckGetAppRunningRuleResultCorrectOption(int option,const std::string & commandName,std::string & bundleName,int32_t & userId,int & euid)2579 ErrCode BundleTestTool::CheckGetAppRunningRuleResultCorrectOption(int option, const std::string &commandName,
2580 std::string &bundleName, int32_t &userId, int &euid)
2581 {
2582 bool ret = true;
2583 switch (option) {
2584 case 'h': {
2585 APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]);
2586 return OHOS::ERR_INVALID_VALUE;
2587 }
2588 case 'e': {
2589 StringToInt(optarg, commandName, euid, ret);
2590 break;
2591 }
2592 case 'n': {
2593 APP_LOGD("'bundle_test_tool %{public}s %{public}s'", commandName.c_str(), argv_[optind - 1]);
2594 bundleName = optarg;
2595 break;
2596 }
2597 case 'u': {
2598 StringToInt(optarg, commandName, userId, ret);
2599 break;
2600 }
2601 default: {
2602 std::string unknownOption = "";
2603 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
2604 APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str());
2605 resultReceiver_.append(unknownOptionMsg);
2606 return OHOS::ERR_INVALID_VALUE;
2607 }
2608 }
2609 return OHOS::ERR_OK;
2610 }
2611
2612 // bundle_test_tool getAppRunningControlRuleResult -n com.ohos.example -e 3057
RunAsGetAppRunningControlRuleResultCommand()2613 ErrCode BundleTestTool::RunAsGetAppRunningControlRuleResultCommand()
2614 {
2615 ErrCode result = OHOS::ERR_OK;
2616 int counter = 0;
2617 int euid = 3057;
2618 std::string commandName = "addAppRunningRule";
2619 int userId = 100;
2620 std::string bundleName;
2621 APP_LOGD("RunAsGetAppRunningControlRuleResultCommand is start");
2622 while (true) {
2623 counter++;
2624 int option = getopt_long(argc_, argv_, SHORT_OPTIONS_RULE.c_str(), LONG_OPTIONS_RULE, nullptr);
2625 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2626 if (optind < 0 || optind > argc_) {
2627 return OHOS::ERR_INVALID_VALUE;
2628 }
2629 if (option == -1) {
2630 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
2631 APP_LOGD("bundle_test_tool getRule with no option.");
2632 resultReceiver_.append(HELP_MSG_NO_GET_APP_RUNNING_RULE_OPTION);
2633 return OHOS::ERR_INVALID_VALUE;
2634 }
2635 break;
2636 }
2637 result = CheckGetAppRunningRuleResultCorrectOption(option, commandName, bundleName, userId, euid);
2638 if (result != OHOS::ERR_OK) {
2639 resultReceiver_.append(HELP_MSG_GET_APP_RUNNING_RESULT_RULE);
2640 return OHOS::ERR_INVALID_VALUE;
2641 }
2642 }
2643 seteuid(euid);
2644 auto appControlProxy = bundleMgrProxy_->GetAppControlProxy();
2645 if (!appControlProxy) {
2646 APP_LOGE("fail to get app control proxy.");
2647 return OHOS::ERR_INVALID_VALUE;
2648 }
2649 AppRunningControlRuleResult ruleResult;
2650 APP_LOGI("bundleName: %{public}s, userId: %{public}d", bundleName.c_str(), userId);
2651 int32_t res = appControlProxy->GetAppRunningControlRule(bundleName, userId, ruleResult);
2652 if (res != OHOS::ERR_OK) {
2653 APP_LOGI("GetAppRunningControlRule result: %{public}d", res);
2654 resultReceiver_.append("message:" + ruleResult.controlMessage + " bundle:notFind" + "\n");
2655 return res;
2656 }
2657 resultReceiver_.append("message:" + ruleResult.controlMessage + "\n");
2658 if (ruleResult.controlWant != nullptr) {
2659 resultReceiver_.append("controlWant:" + ruleResult.controlWant->ToString() + "\n");
2660 } else {
2661 resultReceiver_.append("controlWant: nullptr \n");
2662 }
2663 return result;
2664 }
2665
RunAsDeployQuickFix()2666 ErrCode BundleTestTool::RunAsDeployQuickFix()
2667 {
2668 int32_t result = OHOS::ERR_OK;
2669 int32_t counter = 0;
2670 int32_t index = 0;
2671 std::vector<std::string> quickFixPaths;
2672 while (true) {
2673 counter++;
2674 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_QUICK_FIX.c_str(), LONG_OPTIONS_QUICK_FIX, nullptr);
2675 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2676 if (optind < 0 || optind > argc_) {
2677 return OHOS::ERR_INVALID_VALUE;
2678 }
2679
2680 if (option == -1 || option == '?') {
2681 if (counter == 1 && strcmp(argv_[optind], cmd_.c_str()) == 0) {
2682 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
2683 result = OHOS::ERR_INVALID_VALUE;
2684 break;
2685 }
2686 if (optopt == 'p') {
2687 // 'bm deployQuickFix --patch-path' with no argument
2688 resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
2689 result = OHOS::ERR_INVALID_VALUE;
2690 break;
2691 }
2692 break;
2693 }
2694
2695 if (option == 'p') {
2696 APP_LOGD("'bm deployQuickFix -p %{public}s'", argv_[optind - 1]);
2697 quickFixPaths.emplace_back(optarg);
2698 index = optind;
2699 continue;
2700 }
2701 result = OHOS::ERR_INVALID_VALUE;
2702 break;
2703 }
2704
2705 if (result != OHOS::ERR_OK || GetQuickFixPath(index, quickFixPaths) != OHOS::ERR_OK) {
2706 resultReceiver_.append(HELP_MSG_DEPLOY_QUICK_FIX);
2707 return result;
2708 }
2709
2710 std::shared_ptr<QuickFixResult> deployRes = nullptr;
2711 result = DeployQuickFix(quickFixPaths, deployRes);
2712 resultReceiver_ = (result == OHOS::ERR_OK) ? STRING_DEPLOY_QUICK_FIX_OK : STRING_DEPLOY_QUICK_FIX_NG;
2713 resultReceiver_ += GetResMsg(result, deployRes);
2714
2715 return result;
2716 }
2717
GetQuickFixPath(int32_t index,std::vector<std::string> & quickFixPaths) const2718 ErrCode BundleTestTool::GetQuickFixPath(int32_t index, std::vector<std::string>& quickFixPaths) const
2719 {
2720 APP_LOGI("GetQuickFixPath start");
2721 for (; index < argc_ && index >= INDEX_OFFSET; ++index) {
2722 if (argList_[index - INDEX_OFFSET] == "-p" || argList_[index - INDEX_OFFSET] == "--patch-path") {
2723 break;
2724 }
2725
2726 std::string innerPath = argList_[index - INDEX_OFFSET];
2727 if (innerPath.empty() || innerPath == "-p" || innerPath == "--patch-path") {
2728 quickFixPaths.clear();
2729 return OHOS::ERR_INVALID_VALUE;
2730 }
2731 APP_LOGD("GetQuickFixPath is %{public}s'", innerPath.c_str());
2732 quickFixPaths.emplace_back(innerPath);
2733 }
2734 return OHOS::ERR_OK;
2735 }
2736
RunAsSwitchQuickFix()2737 ErrCode BundleTestTool::RunAsSwitchQuickFix()
2738 {
2739 int32_t result = OHOS::ERR_OK;
2740 int32_t counter = 0;
2741 int32_t enable = -1;
2742 std::string bundleName;
2743 while (true) {
2744 counter++;
2745 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_QUICK_FIX.c_str(), LONG_OPTIONS_QUICK_FIX, nullptr);
2746 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2747 if (optind < 0 || optind > argc_) {
2748 return OHOS::ERR_INVALID_VALUE;
2749 }
2750
2751 if (option == -1 || option == '?') {
2752 if (counter == 1 && strcmp(argv_[optind], cmd_.c_str()) == 0) {
2753 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
2754 result = OHOS::ERR_INVALID_VALUE;
2755 break;
2756 }
2757 if (optopt == 'n' || optopt == 'e') {
2758 // 'bm switchQuickFix -n -e' with no argument
2759 resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
2760 result = OHOS::ERR_INVALID_VALUE;
2761 break;
2762 }
2763 break;
2764 }
2765
2766 if (option == 'n') {
2767 APP_LOGD("'bm switchQuickFix -n %{public}s'", argv_[optind - 1]);
2768 bundleName = optarg;
2769 continue;
2770 }
2771 if (option == 'e' && OHOS::StrToInt(optarg, enable)) {
2772 APP_LOGD("'bm switchQuickFix -e %{public}s'", argv_[optind - 1]);
2773 continue;
2774 }
2775 result = OHOS::ERR_INVALID_VALUE;
2776 break;
2777 }
2778
2779 if ((result != OHOS::ERR_OK) || (enable < 0) || (enable > 1)) {
2780 resultReceiver_.append(HELP_MSG_SWITCH_QUICK_FIX);
2781 return result;
2782 }
2783 std::shared_ptr<QuickFixResult> switchRes = nullptr;
2784 result = SwitchQuickFix(bundleName, enable, switchRes);
2785 resultReceiver_ = (result == OHOS::ERR_OK) ? STRING_SWITCH_QUICK_FIX_OK : STRING_SWITCH_QUICK_FIX_NG;
2786 resultReceiver_ += GetResMsg(result, switchRes);
2787
2788 return result;
2789 }
2790
RunAsDeleteQuickFix()2791 ErrCode BundleTestTool::RunAsDeleteQuickFix()
2792 {
2793 int32_t result = OHOS::ERR_OK;
2794 int32_t counter = 0;
2795 std::string bundleName;
2796 while (true) {
2797 counter++;
2798 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_QUICK_FIX.c_str(), LONG_OPTIONS_QUICK_FIX, nullptr);
2799 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2800 if (optind < 0 || optind > argc_) {
2801 return OHOS::ERR_INVALID_VALUE;
2802 }
2803
2804 if (option == -1 || option == '?') {
2805 if (counter == 1 && strcmp(argv_[optind], cmd_.c_str()) == 0) {
2806 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
2807 result = OHOS::ERR_INVALID_VALUE;
2808 break;
2809 }
2810 if (optopt == 'n') {
2811 // 'bm deleteQuickFix -n' with no argument
2812 resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
2813 result = OHOS::ERR_INVALID_VALUE;
2814 break;
2815 }
2816 break;
2817 }
2818
2819 if (option == 'n') {
2820 APP_LOGD("'bm deleteQuickFix -n %{public}s'", argv_[optind - 1]);
2821 bundleName = optarg;
2822 continue;
2823 }
2824 result = OHOS::ERR_INVALID_VALUE;
2825 break;
2826 }
2827
2828 if (result != OHOS::ERR_OK) {
2829 resultReceiver_.append(HELP_MSG_DELETE_QUICK_FIX);
2830 return result;
2831 }
2832 std::shared_ptr<QuickFixResult> deleteRes = nullptr;
2833 result = DeleteQuickFix(bundleName, deleteRes);
2834 resultReceiver_ = (result == OHOS::ERR_OK) ? STRING_DELETE_QUICK_FIX_OK : STRING_DELETE_QUICK_FIX_NG;
2835 resultReceiver_ += GetResMsg(result, deleteRes);
2836
2837 return result;
2838 }
2839
DeployQuickFix(const std::vector<std::string> & quickFixPaths,std::shared_ptr<QuickFixResult> & quickFixRes)2840 ErrCode BundleTestTool::DeployQuickFix(const std::vector<std::string> &quickFixPaths,
2841 std::shared_ptr<QuickFixResult> &quickFixRes)
2842 {
2843 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
2844 std::set<std::string> realPathSet;
2845 for (const auto &quickFixPath : quickFixPaths) {
2846 std::string realPath;
2847 if (!PathToRealPath(quickFixPath, realPath)) {
2848 APP_LOGW("quickFixPath %{public}s is invalid", quickFixPath.c_str());
2849 continue;
2850 }
2851 APP_LOGD("realPath is %{public}s", realPath.c_str());
2852 realPathSet.insert(realPath);
2853 }
2854 std::vector<std::string> pathVec(realPathSet.begin(), realPathSet.end());
2855
2856 sptr<QuickFixStatusCallbackHostlmpl> callback(new (std::nothrow) QuickFixStatusCallbackHostlmpl());
2857 if (callback == nullptr || bundleMgrProxy_ == nullptr) {
2858 APP_LOGE("callback or bundleMgrProxy is null");
2859 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2860 }
2861 sptr<BundleDeathRecipient> recipient(new (std::nothrow) BundleDeathRecipient(nullptr, callback));
2862 if (recipient == nullptr) {
2863 APP_LOGE("recipient is null");
2864 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2865 }
2866 bundleMgrProxy_->AsObject()->AddDeathRecipient(recipient);
2867 auto quickFixProxy = bundleMgrProxy_->GetQuickFixManagerProxy();
2868 if (quickFixProxy == nullptr) {
2869 APP_LOGE("quickFixProxy is null");
2870 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2871 }
2872 std::vector<std::string> destFiles;
2873 auto res = quickFixProxy->CopyFiles(pathVec, destFiles);
2874 if (res != ERR_OK) {
2875 APP_LOGE("Copy files failed with %{public}d.", res);
2876 return res;
2877 }
2878 res = quickFixProxy->DeployQuickFix(destFiles, callback);
2879 if (res != ERR_OK) {
2880 APP_LOGE("DeployQuickFix failed");
2881 return res;
2882 }
2883
2884 return callback->GetResultCode(quickFixRes);
2885 #else
2886 return ERR_BUNDLEMANAGER_FEATURE_IS_NOT_SUPPORTED;
2887 #endif
2888 }
2889
SwitchQuickFix(const std::string & bundleName,int32_t enable,std::shared_ptr<QuickFixResult> & quickFixRes)2890 ErrCode BundleTestTool::SwitchQuickFix(const std::string &bundleName, int32_t enable,
2891 std::shared_ptr<QuickFixResult> &quickFixRes)
2892 {
2893 APP_LOGD("SwitchQuickFix bundleName: %{public}s, enable: %{public}d", bundleName.c_str(), enable);
2894 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
2895 sptr<QuickFixStatusCallbackHostlmpl> callback(new (std::nothrow) QuickFixStatusCallbackHostlmpl());
2896 if (callback == nullptr || bundleMgrProxy_ == nullptr) {
2897 APP_LOGE("callback or bundleMgrProxy is null");
2898 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2899 }
2900 sptr<BundleDeathRecipient> recipient(new (std::nothrow) BundleDeathRecipient(nullptr, callback));
2901 if (recipient == nullptr) {
2902 APP_LOGE("recipient is null");
2903 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2904 }
2905 bundleMgrProxy_->AsObject()->AddDeathRecipient(recipient);
2906 auto quickFixProxy = bundleMgrProxy_->GetQuickFixManagerProxy();
2907 if (quickFixProxy == nullptr) {
2908 APP_LOGE("quickFixProxy is null");
2909 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2910 }
2911 bool enableFlag = (enable == 0) ? false : true;
2912 auto res = quickFixProxy->SwitchQuickFix(bundleName, enableFlag, callback);
2913 if (res != ERR_OK) {
2914 APP_LOGE("SwitchQuickFix failed");
2915 return res;
2916 }
2917 return callback->GetResultCode(quickFixRes);
2918 #else
2919 return ERR_BUNDLEMANAGER_FEATURE_IS_NOT_SUPPORTED;
2920 #endif
2921 }
2922
DeleteQuickFix(const std::string & bundleName,std::shared_ptr<QuickFixResult> & quickFixRes)2923 ErrCode BundleTestTool::DeleteQuickFix(const std::string &bundleName,
2924 std::shared_ptr<QuickFixResult> &quickFixRes)
2925 {
2926 APP_LOGD("DeleteQuickFix bundleName: %{public}s", bundleName.c_str());
2927 #ifdef BUNDLE_FRAMEWORK_QUICK_FIX
2928 sptr<QuickFixStatusCallbackHostlmpl> callback(new (std::nothrow) QuickFixStatusCallbackHostlmpl());
2929 if (callback == nullptr || bundleMgrProxy_ == nullptr) {
2930 APP_LOGE("callback or bundleMgrProxy is null");
2931 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2932 }
2933 sptr<BundleDeathRecipient> recipient(new (std::nothrow) BundleDeathRecipient(nullptr, callback));
2934 if (recipient == nullptr) {
2935 APP_LOGE("recipient is null");
2936 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2937 }
2938 bundleMgrProxy_->AsObject()->AddDeathRecipient(recipient);
2939 auto quickFixProxy = bundleMgrProxy_->GetQuickFixManagerProxy();
2940 if (quickFixProxy == nullptr) {
2941 APP_LOGE("quickFixProxy is null");
2942 return ERR_BUNDLEMANAGER_QUICK_FIX_INTERNAL_ERROR;
2943 }
2944 auto res = quickFixProxy->DeleteQuickFix(bundleName, callback);
2945 if (res != ERR_OK) {
2946 APP_LOGE("DeleteQuickFix failed");
2947 return res;
2948 }
2949 return callback->GetResultCode(quickFixRes);
2950 #else
2951 return ERR_BUNDLEMANAGER_FEATURE_IS_NOT_SUPPORTED;
2952 #endif
2953 }
2954
GetResMsg(int32_t code)2955 std::string BundleTestTool::GetResMsg(int32_t code)
2956 {
2957 std::unordered_map<int32_t, std::string> quickFixMsgMap;
2958 CreateQuickFixMsgMap(quickFixMsgMap);
2959 if (quickFixMsgMap.find(code) != quickFixMsgMap.end()) {
2960 return quickFixMsgMap.at(code);
2961 }
2962 return MSG_ERR_BUNDLEMANAGER_QUICK_FIX_UNKOWN;
2963 }
2964
GetResMsg(int32_t code,const std::shared_ptr<QuickFixResult> & quickFixRes)2965 std::string BundleTestTool::GetResMsg(int32_t code, const std::shared_ptr<QuickFixResult> &quickFixRes)
2966 {
2967 std::string resMsg;
2968 std::unordered_map<int32_t, std::string> quickFixMsgMap;
2969 CreateQuickFixMsgMap(quickFixMsgMap);
2970 if (quickFixMsgMap.find(code) != quickFixMsgMap.end()) {
2971 resMsg += quickFixMsgMap.at(code);
2972 } else {
2973 resMsg += MSG_ERR_BUNDLEMANAGER_QUICK_FIX_UNKOWN;
2974 }
2975 if (quickFixRes != nullptr) {
2976 resMsg += quickFixRes->ToString() + "\n";
2977 }
2978 return resMsg;
2979 }
2980
RunAsSetDebugMode()2981 ErrCode BundleTestTool::RunAsSetDebugMode()
2982 {
2983 int32_t result = OHOS::ERR_OK;
2984 int32_t counter = 0;
2985 int32_t enable = -1;
2986 while (true) {
2987 counter++;
2988 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_DEBUG_MODE.c_str(), LONG_OPTIONS_DEBUG_MODE, nullptr);
2989 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
2990 if (optind < 0 || optind > argc_) {
2991 return OHOS::ERR_INVALID_VALUE;
2992 }
2993
2994 if (option == -1 || option == '?') {
2995 if (counter == 1 && strcmp(argv_[optind], cmd_.c_str()) == 0) {
2996 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
2997 result = OHOS::ERR_INVALID_VALUE;
2998 break;
2999 }
3000 if (optopt == 'e') {
3001 // 'bundle_test_tool setDebugMode -e' with no argument
3002 resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
3003 result = OHOS::ERR_INVALID_VALUE;
3004 break;
3005 }
3006 break;
3007 }
3008
3009 if (option == 'e' && OHOS::StrToInt(optarg, enable)) {
3010 APP_LOGD("'bundle_test_tool setDebugMode -e %{public}s'", argv_[optind - 1]);
3011 continue;
3012 }
3013 result = OHOS::ERR_INVALID_VALUE;
3014 break;
3015 }
3016
3017 if (result != OHOS::ERR_OK) {
3018 resultReceiver_.append(HELP_MSG_SET_DEBUG_MODE);
3019 return result;
3020 }
3021 ErrCode setResult = SetDebugMode(enable);
3022 if (setResult == OHOS::ERR_OK) {
3023 resultReceiver_ = STRING_SET_DEBUG_MODE_OK;
3024 } else {
3025 resultReceiver_ = STRING_SET_DEBUG_MODE_NG + GetResMsg(setResult);
3026 }
3027 return setResult;
3028 }
3029
SetDebugMode(int32_t debugMode)3030 ErrCode BundleTestTool::SetDebugMode(int32_t debugMode)
3031 {
3032 if (debugMode != 0 && debugMode != 1) {
3033 APP_LOGE("SetDebugMode param is invalid");
3034 return ERR_BUNDLEMANAGER_SET_DEBUG_MODE_INVALID_PARAM;
3035 }
3036 bool enable = debugMode == 0 ? false : true;
3037 if (bundleMgrProxy_ == nullptr) {
3038 APP_LOGE("bundleMgrProxy_ is nullptr");
3039 return ERR_BUNDLEMANAGER_SET_DEBUG_MODE_INTERNAL_ERROR;
3040 }
3041 return bundleMgrProxy_->SetDebugMode(enable);
3042 }
3043
BundleNameAndUserIdCommonFunc(std::string & bundleName,int32_t & userId)3044 ErrCode BundleTestTool::BundleNameAndUserIdCommonFunc(std::string &bundleName, int32_t &userId)
3045 {
3046 int32_t result = OHOS::ERR_OK;
3047 int32_t counter = 0;
3048 userId = Constants::UNSPECIFIED_USERID;
3049 while (true) {
3050 counter++;
3051 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_GET_BUNDLE_STATS.c_str(),
3052 LONG_OPTIONS_GET_BUNDLE_STATS, nullptr);
3053 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
3054 if (optind < 0 || optind > argc_) {
3055 return OHOS::ERR_INVALID_VALUE;
3056 }
3057 if (option == -1) {
3058 if (counter == 1) {
3059 // When scanning the first argument
3060 if (strcmp(argv_[optind], cmd_.c_str()) == 0) {
3061 resultReceiver_.append(HELP_MSG_NO_OPTION + "\n");
3062 result = OHOS::ERR_INVALID_VALUE;
3063 }
3064 }
3065 break;
3066 }
3067
3068 if (option == '?') {
3069 switch (optopt) {
3070 case 'n': {
3071 resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
3072 result = OHOS::ERR_INVALID_VALUE;
3073 break;
3074 }
3075 case 'u': {
3076 resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
3077 result = OHOS::ERR_INVALID_VALUE;
3078 break;
3079 }
3080 default: {
3081 std::string unknownOption = "";
3082 std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption);
3083 resultReceiver_.append(unknownOptionMsg);
3084 result = OHOS::ERR_INVALID_VALUE;
3085 break;
3086 }
3087 }
3088 break;
3089 }
3090
3091 switch (option) {
3092 case 'h': {
3093 result = OHOS::ERR_INVALID_VALUE;
3094 break;
3095 }
3096 case 'n': {
3097 bundleName = optarg;
3098 break;
3099 }
3100 case 'u': {
3101 if (!OHOS::StrToInt(optarg, userId) || userId < 0) {
3102 resultReceiver_.append(STRING_REQUIRE_CORRECT_VALUE);
3103 return OHOS::ERR_INVALID_VALUE;
3104 }
3105 break;
3106 }
3107 default: {
3108 result = OHOS::ERR_INVALID_VALUE;
3109 break;
3110 }
3111 }
3112 }
3113
3114 if (result == OHOS::ERR_OK) {
3115 if (resultReceiver_ == "" && bundleName.size() == 0) {
3116 resultReceiver_.append(HELP_MSG_NO_BUNDLE_NAME_OPTION + "\n");
3117 result = OHOS::ERR_INVALID_VALUE;
3118 }
3119 }
3120 return result;
3121 }
3122
RunAsGetBundleStats()3123 ErrCode BundleTestTool::RunAsGetBundleStats()
3124 {
3125 std::string bundleName;
3126 int32_t userId;
3127 int32_t result = BundleNameAndUserIdCommonFunc(bundleName, userId);
3128 if (result != OHOS::ERR_OK) {
3129 resultReceiver_.append(HELP_MSG_GET_BUNDLE_STATS);
3130 } else {
3131 std::string msg;
3132 bool ret = GetBundleStats(bundleName, userId, msg);
3133 if (ret) {
3134 resultReceiver_ = STRING_GET_BUNDLE_STATS_OK + msg;
3135 } else {
3136 resultReceiver_ = STRING_GET_BUNDLE_STATS_NG + "\n";
3137 }
3138 }
3139
3140 return result;
3141 }
3142
GetBundleStats(const std::string & bundleName,int32_t userId,std::string & msg)3143 bool BundleTestTool::GetBundleStats(const std::string &bundleName, int32_t userId,
3144 std::string& msg)
3145 {
3146 if (bundleMgrProxy_ == nullptr) {
3147 APP_LOGE("bundleMgrProxy_ is nullptr");
3148 return false;
3149 }
3150 userId = BundleCommandCommon::GetCurrentUserId(userId);
3151 std::vector<std::int64_t> bundleStats;
3152 bool ret = bundleMgrProxy_->GetBundleStats(bundleName, userId, bundleStats);
3153 if (ret) {
3154 for (size_t index = 0; index < bundleStats.size(); ++index) {
3155 msg += GET_BUNDLE_STATS_ARRAY[index] + std::to_string(bundleStats[index]) + "\n";
3156 }
3157 }
3158 return ret;
3159 }
3160
RunAsGetAppProvisionInfo()3161 ErrCode BundleTestTool::RunAsGetAppProvisionInfo()
3162 {
3163 std::string bundleName;
3164 int32_t userId;
3165 int32_t result = BundleNameAndUserIdCommonFunc(bundleName, userId);
3166 if (result != OHOS::ERR_OK) {
3167 resultReceiver_.append(HELP_MSG_GET_APP_PROVISION_INFO);
3168 } else {
3169 std::string msg;
3170 result = GetAppProvisionInfo(bundleName, userId, msg);
3171 if (result == OHOS::ERR_OK) {
3172 resultReceiver_ = STRING_GET_APP_PROVISION_INFO_OK + msg;
3173 } else {
3174 resultReceiver_ = STRING_GET_APP_PROVISION_INFO_NG + "\n";
3175 }
3176 }
3177
3178 return result;
3179 }
3180
GetAppProvisionInfo(const std::string & bundleName,int32_t userId,std::string & msg)3181 ErrCode BundleTestTool::GetAppProvisionInfo(const std::string &bundleName,
3182 int32_t userId, std::string& msg)
3183 {
3184 if (bundleMgrProxy_ == nullptr) {
3185 APP_LOGE("bundleMgrProxy_ is nullptr");
3186 return OHOS::ERR_INVALID_VALUE;
3187 }
3188 userId = BundleCommandCommon::GetCurrentUserId(userId);
3189 AppProvisionInfo info;
3190 auto ret = bundleMgrProxy_->GetAppProvisionInfo(bundleName, userId, info);
3191 if (ret == ERR_OK) {
3192 msg = "{\n";
3193 msg += " versionCode: " + std::to_string(info.versionCode) + "\n";
3194 msg += " versionName: " + info.versionName+ "\n";
3195 msg += " uuid: " + info.uuid + "\n";
3196 msg += " type: " + info.type + "\n";
3197 msg += " appDistributionType: " + info.appDistributionType + "\n";
3198 msg += " developerId: " + info.developerId + "\n";
3199 msg += " certificate: " + info.certificate + "\n";
3200 msg += " apl: " + info.apl + "\n";
3201 msg += " issuer: " + info.issuer + "\n";
3202 msg += " validity: {\n";
3203 msg += " notBefore: " + std::to_string(info.validity.notBefore) + "\n";
3204 msg += " notAfter: " + std::to_string(info.validity.notAfter) + "\n";
3205 msg += " }\n";
3206 msg += "}\n";
3207 }
3208 return ret;
3209 }
3210
RunAsGetDistributedBundleName()3211 ErrCode BundleTestTool::RunAsGetDistributedBundleName()
3212 {
3213 ErrCode result;
3214 std::string networkId;
3215 int32_t counter = 0;
3216 int32_t accessTokenId = 0;
3217 while (true) {
3218 counter++;
3219 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_GET_DISTRIBUTED_BUNDLE_NAME.c_str(),
3220 LONG_OPTIONS_GET_DISTRIBUTED_BUNDLE_NAME, nullptr);
3221 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
3222 if (optind < 0 || optind > argc_) {
3223 return OHOS::ERR_INVALID_VALUE;
3224 }
3225 if (option == -1) {
3226 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
3227 resultReceiver_.append(HELP_MSG_NO_GET_DISTRIBUTED_BUNDLE_NAME_OPTION);
3228 return OHOS::ERR_INVALID_VALUE;
3229 }
3230 break;
3231 }
3232 result = CheckGetDistributedBundleNameCorrectOption(option, GET_DISTRIBUTED_BUNDLE_NAME_COMMAND_NAME,
3233 networkId, accessTokenId);
3234 if (result != OHOS::ERR_OK) {
3235 resultReceiver_.append(HELP_MSG_GET_DISTRIBUTED_BUNDLE_NAME);
3236 return OHOS::ERR_INVALID_VALUE;
3237 }
3238 }
3239 if (accessTokenId == 0 || networkId.size() == 0) {
3240 resultReceiver_.append(HELP_MSG_NO_GET_DISTRIBUTED_BUNDLE_NAME_OPTION);
3241 return OHOS::ERR_INVALID_VALUE;
3242 }
3243 std::string msg;
3244 result = GetDistributedBundleName(networkId, accessTokenId, msg);
3245 if (result == OHOS::ERR_OK) {
3246 resultReceiver_ = STRING_GET_DISTRIBUTED_BUNDLE_NAME_OK + msg;
3247 } else {
3248 resultReceiver_ = STRING_GET_DISTRIBUTED_BUNDLE_NAME_NG + "\n";
3249 APP_LOGE("RunAsGetDistributedBundleName fail result %{public}d.", result);
3250 }
3251 return result;
3252 }
3253
CheckGetDistributedBundleNameCorrectOption(int32_t option,const std::string & commandName,std::string & networkId,int32_t & accessTokenId)3254 ErrCode BundleTestTool::CheckGetDistributedBundleNameCorrectOption(int32_t option, const std::string &commandName,
3255 std::string &networkId, int32_t &accessTokenId)
3256 {
3257 ErrCode result = OHOS::ERR_OK;
3258 switch (option) {
3259 case 'h': {
3260 result = OHOS::ERR_INVALID_VALUE;
3261 break;
3262 }
3263 case 'n': {
3264 networkId = optarg;
3265 if (networkId.size() == 0) {
3266 return OHOS::ERR_INVALID_VALUE;
3267 }
3268 break;
3269 }
3270 case 'a': {
3271 if (!OHOS::StrToInt(optarg, accessTokenId) || accessTokenId < 0) {
3272 return OHOS::ERR_INVALID_VALUE;
3273 }
3274 break;
3275 }
3276 default: {
3277 result = OHOS::ERR_INVALID_VALUE;
3278 break;
3279 }
3280 }
3281 return result;
3282 }
3283
GetDistributedBundleName(const std::string & networkId,int32_t accessTokenId,std::string & msg)3284 ErrCode BundleTestTool::GetDistributedBundleName(const std::string &networkId,
3285 int32_t accessTokenId, std::string& msg)
3286 {
3287 #ifdef DISTRIBUTED_BUNDLE_FRAMEWORK
3288 if (distributedBmsProxy_ == nullptr) {
3289 APP_LOGE("distributedBmsProxy_ is nullptr");
3290 return OHOS::ERR_INVALID_VALUE;
3291 }
3292 std::string bundleName;
3293 auto ret = distributedBmsProxy_->GetDistributedBundleName(networkId, accessTokenId, bundleName);
3294 if (ret == OHOS::NO_ERROR) {
3295 msg = "\n";
3296 if (bundleName.size() == 0) {
3297 msg += "no match found \n";
3298 } else {
3299 msg += bundleName + "\n";
3300 }
3301 msg += "\n";
3302 } else {
3303 APP_LOGE("distributedBmsProxy_ GetDistributedBundleName fail errcode %{public}d.", ret);
3304 return OHOS::ERR_INVALID_VALUE;
3305 }
3306 return OHOS::ERR_OK;
3307 #else
3308 return OHOS::ERR_INVALID_VALUE;
3309 #endif
3310 }
3311
ParseEventCallbackOptions(bool & onlyUnregister,int32_t & uid)3312 bool BundleTestTool::ParseEventCallbackOptions(bool &onlyUnregister, int32_t &uid)
3313 {
3314 int32_t opt;
3315 while ((opt = getopt_long(argc_, argv_, SHORT_OPTIONS_BUNDLE_EVENT_CALLBACK.c_str(),
3316 LONG_OPTIONS_BUNDLE_EVENT_CALLBACK, nullptr)) != -1) {
3317 switch (opt) {
3318 case 'o': {
3319 onlyUnregister = true;
3320 break;
3321 }
3322 case 'u': {
3323 if (!OHOS::StrToInt(optarg, uid)) {
3324 std::string msg = "invalid param, uid should be int";
3325 resultReceiver_.append(msg).append(LINE_BREAK);
3326 APP_LOGE("%{public}s", msg.c_str());
3327 return false;
3328 }
3329 break;
3330 }
3331 case 'h': {
3332 resultReceiver_.append(HELP_MSG_BUNDLE_EVENT_CALLBACK);
3333 return false;
3334 }
3335 default: {
3336 std::string msg = "unsupported option";
3337 resultReceiver_.append(msg).append(LINE_BREAK);
3338 APP_LOGE("%{public}s", msg.c_str());
3339 return false;
3340 }
3341 }
3342 }
3343 APP_LOGI("ParseEventCallbackOptions success");
3344 return true;
3345 }
3346
Sleep(int32_t seconds)3347 void BundleTestTool::Sleep(int32_t seconds)
3348 {
3349 APP_LOGI("begin to sleep %{public}d seconds", seconds);
3350 std::this_thread::sleep_for(std::chrono::seconds(seconds));
3351 APP_LOGI("sleep done");
3352 }
3353
CallRegisterBundleEventCallback(sptr<BundleEventCallbackImpl> bundleEventCallback)3354 ErrCode BundleTestTool::CallRegisterBundleEventCallback(sptr<BundleEventCallbackImpl> bundleEventCallback)
3355 {
3356 APP_LOGI("begin to call RegisterBundleEventCallback");
3357 std::string msg;
3358 bool ret = bundleMgrProxy_->RegisterBundleEventCallback(bundleEventCallback);
3359 if (!ret) {
3360 msg = "RegisterBundleEventCallback failed";
3361 resultReceiver_.append(msg).append(LINE_BREAK);
3362 APP_LOGE("%{public}s", msg.c_str());
3363 return OHOS::ERR_INVALID_VALUE;
3364 }
3365 msg = "RegisterBundleEventCallback success";
3366 resultReceiver_.append(msg).append(LINE_BREAK);
3367 APP_LOGI("%{public}s", msg.c_str());
3368 return OHOS::ERR_OK;
3369 }
3370
CallUnRegisterBundleEventCallback(sptr<BundleEventCallbackImpl> bundleEventCallback)3371 ErrCode BundleTestTool::CallUnRegisterBundleEventCallback(sptr<BundleEventCallbackImpl> bundleEventCallback)
3372 {
3373 APP_LOGI("begin to call UnregisterBundleEventCallback");
3374 std::string msg;
3375 bool ret = bundleMgrProxy_->UnregisterBundleEventCallback(bundleEventCallback);
3376 if (!ret) {
3377 msg = "UnregisterBundleEventCallback failed";
3378 resultReceiver_.append(msg).append(LINE_BREAK);
3379 APP_LOGE("%{public}s", msg.c_str());
3380 return OHOS::ERR_INVALID_VALUE;
3381 }
3382 msg = "UnregisterBundleEventCallback success";
3383 resultReceiver_.append(msg).append(LINE_BREAK);
3384 APP_LOGI("%{public}s", msg.c_str());
3385 return OHOS::ERR_OK;
3386 }
3387
HandleBundleEventCallback()3388 ErrCode BundleTestTool::HandleBundleEventCallback()
3389 {
3390 APP_LOGI("begin to HandleBundleEventCallback");
3391 bool onlyUnregister = false;
3392 int32_t uid = Constants::FOUNDATION_UID;
3393 if (!ParseEventCallbackOptions(onlyUnregister, uid)) {
3394 APP_LOGE("ParseEventCallbackOptions failed");
3395 return OHOS::ERR_INVALID_VALUE;
3396 }
3397 APP_LOGI("onlyUnregister : %{public}d, uid : %{public}d", onlyUnregister, uid);
3398 if (bundleMgrProxy_ == nullptr) {
3399 std::string msg = "bundleMgrProxy_ is nullptr";
3400 resultReceiver_.append(msg).append(LINE_BREAK);
3401 APP_LOGE("%{public}s", msg.c_str());
3402 return OHOS::ERR_INVALID_VALUE;
3403 }
3404 seteuid(uid);
3405 ErrCode ret;
3406 sptr<BundleEventCallbackImpl> bundleEventCallback = new (std::nothrow) BundleEventCallbackImpl();
3407 if (onlyUnregister) {
3408 // only call UnRegisterBundleEventCallback
3409 return CallUnRegisterBundleEventCallback(bundleEventCallback);
3410 }
3411 // call RegisterBundleEventCallback then call UnRegisterBundleEventCallback
3412 ret = CallRegisterBundleEventCallback(bundleEventCallback);
3413 if (ret != OHOS::ERR_OK) {
3414 return ret;
3415 }
3416 Sleep(SLEEP_SECONDS);
3417 ret = CallUnRegisterBundleEventCallback(bundleEventCallback);
3418 if (ret != OHOS::ERR_OK) {
3419 return ret;
3420 }
3421 Sleep(SLEEP_SECONDS);
3422 return OHOS::ERR_OK;
3423 }
3424
RunAsQueryDataGroupInfos()3425 ErrCode BundleTestTool::RunAsQueryDataGroupInfos()
3426 {
3427 APP_LOGI("RunAsQueryDataGroupInfos start");
3428 std::string bundleName;
3429 int32_t userId;
3430 int32_t result = BundleNameAndUserIdCommonFunc(bundleName, userId);
3431 if (result != OHOS::ERR_OK) {
3432 resultReceiver_.append(HELP_MSG_QUERY_DATA_GROUP_INFOS);
3433 } else {
3434 std::string msg;
3435 result = QueryDataGroupInfos(bundleName, userId, msg);
3436 if (result) {
3437 resultReceiver_ = STRING_QUERY_DATA_GROUP_INFOS_OK + msg;
3438 return ERR_OK;
3439 } else {
3440 resultReceiver_ = STRING_QUERY_DATA_GROUP_INFOS_NG + "\n";
3441 }
3442 }
3443 return OHOS::ERR_INVALID_VALUE;
3444 }
3445
QueryDataGroupInfos(const std::string & bundleName,int32_t userId,std::string & msg)3446 bool BundleTestTool::QueryDataGroupInfos(const std::string &bundleName,
3447 int32_t userId, std::string& msg)
3448 {
3449 if (bundleMgrProxy_ == nullptr) {
3450 APP_LOGE("bundleMgrProxy_ is nullptr");
3451 return OHOS::ERR_INVALID_VALUE;
3452 }
3453 std::vector<DataGroupInfo> infos;
3454 bool ret = bundleMgrProxy_->QueryDataGroupInfos(bundleName, userId, infos);
3455 if (ret) {
3456 msg = "dataGroupInfos:\n{\n";
3457 for (const auto &dataGroupInfo : infos) {
3458 msg +=" ";
3459 msg += dataGroupInfo.ToString();
3460 msg += "\n";
3461 }
3462 msg += "}\n";
3463 return true;
3464 }
3465
3466 return false;
3467 }
3468
RunAsGetGroupDir()3469 ErrCode BundleTestTool::RunAsGetGroupDir()
3470 {
3471 APP_LOGI("RunAsGetGroupDir start");
3472 ErrCode result;
3473 std::string dataGroupId;
3474 int32_t counter = 0;
3475 while (true) {
3476 counter++;
3477 int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_GET_GROUP_DIR.c_str(),
3478 LONG_OPTIONS_GET_GROUP_DIR, nullptr);
3479 APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind);
3480 if (optind < 0 || optind > argc_) {
3481 return OHOS::ERR_INVALID_VALUE;
3482 }
3483 if (option == -1) {
3484 if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) {
3485 resultReceiver_.append(HELP_MSG_GET_GROUP_DIR);
3486 return OHOS::ERR_INVALID_VALUE;
3487 }
3488 break;
3489 }
3490 result = CheckGetGroupIdCorrectOption(option, dataGroupId);
3491 if (result != OHOS::ERR_OK) {
3492 resultReceiver_.append(HELP_MSG_GET_GROUP_DIR);
3493 return OHOS::ERR_INVALID_VALUE;
3494 }
3495 }
3496 std::string msg;
3497 bool ret = GetGroupDir(dataGroupId, msg);
3498 if (ret) {
3499 resultReceiver_ = STRING_GET_GROUP_DIR_OK + msg;
3500 } else {
3501 resultReceiver_ = STRING_GET_GROUP_DIR_NG + "\n";
3502 APP_LOGE("RunAsGetGroupDir fail");
3503 return OHOS::ERR_INVALID_VALUE;
3504 }
3505 return result;
3506 }
3507
CheckGetGroupIdCorrectOption(int32_t option,std::string & dataGroupId)3508 ErrCode BundleTestTool::CheckGetGroupIdCorrectOption(int32_t option, std::string &dataGroupId)
3509 {
3510 ErrCode result = OHOS::ERR_OK;
3511 switch (option) {
3512 case 'h': {
3513 result = OHOS::ERR_INVALID_VALUE;
3514 break;
3515 }
3516 case 'd': {
3517 dataGroupId = optarg;
3518 if (dataGroupId.size() == 0) {
3519 return OHOS::ERR_INVALID_VALUE;
3520 }
3521 break;
3522 }
3523 default: {
3524 result = OHOS::ERR_INVALID_VALUE;
3525 break;
3526 }
3527 }
3528 return result;
3529 }
3530
GetGroupDir(const std::string & dataGroupId,std::string & msg)3531 bool BundleTestTool::GetGroupDir(const std::string &dataGroupId, std::string& msg)
3532 {
3533 if (bundleMgrProxy_ == nullptr) {
3534 APP_LOGE("bundleMgrProxy_ is nullptr");
3535 return OHOS::ERR_INVALID_VALUE;
3536 }
3537 std::string dir;
3538 bool ret = bundleMgrProxy_->GetGroupDir(dataGroupId, dir);
3539 if (ret) {
3540 msg = "group dir:\n";
3541 msg += dir;
3542 msg += "\n";
3543 return true;
3544 }
3545
3546 return false;
3547 }
3548 } // AppExecFwk
3549 } // OHOS