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