diff --git a/frameworks/include/bundle_test_tool.h b/frameworks/include/bundle_test_tool.h index b6503e25cdef57d9208a0df103fcff127b02b936..e19a97c31cab33d962787b4c085b4934180b0393 100644 --- a/frameworks/include/bundle_test_tool.h +++ b/frameworks/include/bundle_test_tool.h @@ -103,6 +103,7 @@ private: ErrCode RunAsGetDirByBundleNameAndAppIndex(); ErrCode RunAsGetAllBundleDirs(); ErrCode GetAllBundleDirs(int32_t userId, std::string& msg); + ErrCode RunAsSwitchUninstallStateByUserId(); ErrCode RunAsGetAllBundleCacheStat(); ErrCode GetAllBundleCacheStat(std::string& msg); ErrCode RunAsCleanAllBundleCache(); diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index 02e8edd551f757033f8293444a3a0dc438806a56..4ae3d582e1c558ad1004c872a2d69ca5b9aa40b0 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -191,6 +191,7 @@ static const std::string HELP_MSG = " updateAppEncryptedStatus update app encrypted status\n" " getDirByBundleNameAndAppIndex obtain the dir by bundleName and appIndex\n" " getAllBundleDirs obtain all bundle dirs \n" + " switchUninstallStateByUserId switch uninstall state by userId \n" " getAllBundleCacheStat obtain all bundle cache size \n" " cleanAllBundleCache clean all bundle cache \n" " isBundleInstalled determine whether the bundle is installed based on bundleName user " @@ -568,6 +569,15 @@ const std::string HELP_MSG_GET_ALL_BUNDLE_DIRS = " -h, --help list available commands\n" " -u, --user-id specify a user id\n"; +const std::string HELP_MSG_SWITCH_UNINSTALL_STATE_BY_USER_ID = + "usage: bundle_test_tool switchUninstallStateByUserId \n" + "eg:bundle_test_tool switchUninstallStateByUserId -n -s -u \n" + "options list:\n" + " -h, --help list available commands\n" + " -n, --bundle-name specify bundle name of the application\n" + " -s, --state specify uninstall state\n" + " -u, --user-id specify a user id\n"; + const std::string HELP_MSG_GET_ALL_BUNDLE_CACHE_STAT = "usage: bundle_test_tool getAllBundleCacheStat \n" "eg:bundle_test_tool getAllBundleCacheStat\n" @@ -758,6 +768,9 @@ const std::string STRING_GET_DIR_NG = "getDirByBundleNameAndAppIndex failed\n"; const std::string STRING_GET_ALL_BUNDLE_DIRS_OK = "getAllBundleDirs successfully\n"; const std::string STRING_GET_ALL_BUNDLE_DIRS_NG = "getAllBundleDirs failed\n"; +const std::string STRING_SWITCH_UNINSTALL_STATE_BY_USER_ID_OK = "switchUninstallStateByUserId successfully\n"; +const std::string STRING_SWITCH_UNINSTALL_STATE_BY_USER_ID_NG = "switchUninstallStateByUserId failed\n"; + const std::string STRING_GET_ALL_BUNDLE_CACHE_STAT_OK = "getAllBundleCacheStat successfully\n"; const std::string STRING_GET_ALL_BUNDLE_CACHE_STAT_NG = "getAllBundleCacheStat failed\n"; @@ -1084,6 +1097,15 @@ const struct option LONG_OPTIONS_GET_DIR[] = { {nullptr, 0, nullptr, 0}, }; +const std::string SHORT_OPTIONS_SWITCH_UNINSTALL_STATE_BY_USER_ID = "hn:s:u:"; +const struct option LONG_OPTIONS_SWITCH_UNINSTALL_STATE_BY_USER_ID[] = { + {"help", no_argument, nullptr, 'h'}, + {"bundle-name", required_argument, nullptr, 'n'}, + {"state", required_argument, nullptr, 's'}, + {"user-id", required_argument, nullptr, 'u'}, + {nullptr, 0, nullptr, 0}, +}; + const std::string SHORT_OPTIONS_PREINSTALL = "hn:m:u:f:"; const struct option LONG_OPTIONS_PREINSTALL[] = { {"help", no_argument, nullptr, 'h'}, @@ -1264,6 +1286,8 @@ ErrCode BundleTestTool::CreateCommandMap() std::bind(&BundleTestTool::RunAsGetDirByBundleNameAndAppIndex, this)}, {"getAllBundleDirs", std::bind(&BundleTestTool::RunAsGetAllBundleDirs, this)}, + {"switchUninstallStateByUserId", + std::bind(&BundleTestTool::RunAsSwitchUninstallStateByUserId, this)}, {"getAllBundleCacheStat", std::bind(&BundleTestTool::RunAsGetAllBundleCacheStat, this)}, {"cleanAllBundleCache", @@ -5174,6 +5198,56 @@ ErrCode BundleTestTool::RunAsQueryAbilityInfoByContinueType() return result; } +ErrCode BundleTestTool::RunAsSwitchUninstallStateByUserId() +{ + APP_LOGI("RunAsSwitchUninstallStateByUserId start"); + std::string commandName = "switchUninstallStateByUserId"; + int32_t result = OHOS::ERR_OK; + int32_t counter = 0; + std::string name = ""; + std::string bundleName = ""; + bool state = true; + int32_t userId = 100; + while (true) { + counter++; + int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_SWITCH_UNINSTALL_STATE_BY_USER_ID.c_str(), + LONG_OPTIONS_SWITCH_UNINSTALL_STATE_BY_USER_ID, nullptr); + APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", option, optopt, optind); + if (optind < 0 || optind > argc_) { + return OHOS::ERR_INVALID_VALUE; + } + if (option == -1) { + // When scanning the first argument + if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) { + APP_LOGD("bundle_test_tool switchUninstallStateByUserId with no option."); + resultReceiver_.append(HELP_MSG_SWITCH_UNINSTALL_STATE_BY_USER_ID); + return OHOS::ERR_INVALID_VALUE; + } + break; + } + int temp = 0; + result = !CheckGetStringCorrectOption(option, commandName, temp, name) + ? OHOS::ERR_INVALID_VALUE : result; + bundleName = option == 'n' ? name : bundleName; + state = option == 's' ? name : state; + userId = option == 'u' ? temp : userId; + } + APP_LOGI("bundleName: %{public}s, state: %{public}d, userId: %{public}d", bundleName.c_str(), state, userId); + if (result != OHOS::ERR_OK) { + resultReceiver_.append(HELP_MSG_SWITCH_UNINSTALL_STATE_BY_USER_ID); + } else { + result = bundleMgrProxy_->SwitchUninstallStateByUserId(bundleName, state, userId); + if (result == ERR_OK) { + resultReceiver_.append(STRING_SWITCH_UNINSTALL_STATE_BY_USER_ID_OK); + } + } else { + resultReceiver_.append(STRING_SWITCH_UNINSTALL_STATE_BY_USER_ID_NG + + "errCode is "+ std::to_string(result) + "\n"); + } + } + return result; +} + ErrCode BundleTestTool::RunAsGetDirByBundleNameAndAppIndex() { APP_LOGI("RunAsGetDirByBundleNameAndAppIndex start");