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