From baa44272ec2e0a4ff9c7af7328ce7d8f774a0383 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E6=AC=A3=E5=AE=87?= Date: Fri, 5 Sep 2025 20:26:15 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E6=89=B9=E9=87=8F=E5=88=A0?= =?UTF-8?q?=E9=99=A4=E5=A4=84=E7=BD=AE=E8=A7=84=E5=88=99=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 张欣宇 Change-Id: I041e29a0b030b1aea779c85bcf023a0213a9306f --- frameworks/include/bundle_test_tool.h | 3 + frameworks/src/bundle_test_tool.cpp | 124 +++++++++++++++++++++++++- 2 files changed, 126 insertions(+), 1 deletion(-) diff --git a/frameworks/include/bundle_test_tool.h b/frameworks/include/bundle_test_tool.h index b6503e2..9655956 100644 --- a/frameworks/include/bundle_test_tool.h +++ b/frameworks/include/bundle_test_tool.h @@ -61,6 +61,9 @@ private: ErrCode RunAsDumpSandboxCommand(); ErrCode RunAsGetStringCommand(); ErrCode RunAsGetIconCommand(); + ErrCode RunAsDeleteDisposedRulesCommand(); + ErrCode CheckDeleteRulesCorrectOption(int option, const std::string &commandName, std::vector &appIds, + int &appIndex, int &userId, int &uid); ErrCode RunAsAddInstallRuleCommand(); ErrCode RunAsGetInstallRuleCommand(); ErrCode RunAsDeleteInstallRuleCommand(); diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index 4122c3d..a9207aa 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -193,6 +193,7 @@ static const std::string HELP_MSG = " getAllBundleDirs obtain all bundle dirs \n" " getAllBundleCacheStat obtain all bundle cache size \n" " cleanAllBundleCache clean all bundle cache \n" + " deleteDisposedRules delete disposed rules \n" " isBundleInstalled determine whether the bundle is installed based on bundleName user " "and appIndex\n" " getCompatibleDeviceType obtain the compatible device type based on bundleName\n" @@ -375,6 +376,12 @@ const std::string HELP_MSG_NO_ADD_INSTALL_RULE_OPTION = "and a control type with '-t' or '--control-rule-type' \n" "and a userid with '-u' or '--user-id' \n"; +const std::string HELP_MSG_DELETE_RULES_OPTION = + "error: you must specify a app id with '-a' or '--app-id' \n" + "and a app index with '-i' or '--app-index' \n" + "and a userid with '-u' or '--user-id' \n" + "and a uid with '-u' or '--uid' \n"; + const std::string HELP_MSG_NO_GET_INSTALL_RULE_OPTION = "error: you must specify a control type with '-t' or '--control-rule-type' \n" "and a userid with '-u' or '--user-id' \n"; @@ -886,6 +893,16 @@ const struct option LONG_OPTIONS_RULE[] = { {nullptr, 0, nullptr, 0}, }; +const std::string SHORT_OPTIONS_DELETE_RULES = "ha:i:u:c:"; +const struct option LONG_OPTIONS_DELETES_RULES[] = { + {"help", no_argument, nullptr, 'h'}, + {"app-id", required_argument, nullptr, 'a'}, + {"app-index", required_argument, nullptr, 'i'}, + {"user-id", required_argument, nullptr, 'u'}, + {"caller-uid", required_argument, nullptr, 'c'}, + {nullptr, 0, nullptr, 0}, +}; + const std::string SHORT_OPTIONS_AUTO_CLEAN_CACHE = "hs:"; const struct option LONG_OPTIONS_AUTO_CLEAN_CACHE[] = { {"help", no_argument, nullptr, 'h'}, @@ -1219,6 +1236,7 @@ ErrCode BundleTestTool::CreateCommandMap() {"dumpSandbox", std::bind(&BundleTestTool::RunAsDumpSandboxCommand, this)}, {"getStr", std::bind(&BundleTestTool::RunAsGetStringCommand, this)}, {"getIcon", std::bind(&BundleTestTool::RunAsGetIconCommand, this)}, + {"deleteDisposedRules", std::bind(&BundleTestTool::RunAsDeleteDisposedRulesCommand, this)}, {"addAppInstallRule", std::bind(&BundleTestTool::RunAsAddInstallRuleCommand, this)}, {"getAppInstallRule", std::bind(&BundleTestTool::RunAsGetInstallRuleCommand, this)}, {"deleteAppInstallRule", std::bind(&BundleTestTool::RunAsDeleteInstallRuleCommand, this)}, @@ -2579,6 +2597,109 @@ ErrCode BundleTestTool::CheckAddInstallRuleCorrectOption(int option, const std:: return OHOS::ERR_OK; } + +ErrCode BundleTestTool::RunAsDeleteDisposedRulesCommand() +{ + ReloadNativeTokenInfo(); + ErrCode result = OHOS::ERR_OK; + std::string commandName = "DisposedRulesCommand"; + int counter = 0; + std::vector appIds; + int uid = 0; + int userId = 100; + int appIndex = 0; + APP_LOGD("RunAsDeleteDisposedRulesCommand is start"); + while (true) { + counter++; + int option = getopt_long(argc_, argv_, SHORT_OPTIONS_DELETE_RULES.c_str(), LONG_OPTIONS_DELETES_RULES, nullptr); + if (optind < 0 || optind > argc_) { + return OHOS::ERR_INVALID_VALUE; + } + if (option == -1) { + if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) { + resultReceiver_.append(HELP_MSG_DELETE_RULES_OPTION); + return OHOS::ERR_INVALID_VALUE; + } + break; + } + result = CheckDeleteRulesCorrectOption(option, commandName, appIds, appIndex, userId, uid); + if (result != OHOS::ERR_OK) { + resultReceiver_.append(HELP_MSG_DELETE_RULES_OPTION); + return OHOS::ERR_INVALID_VALUE; + } + result = OHOS::ERR_INVALID_VALUE; + break; + } + auto appControlProxy = bundleMgrProxy_->GetAppControlProxy(); + if (!appControlProxy) { + APP_LOGE("fail to get app control proxy."); + return OHOS::ERR_INVALID_VALUE; + } + std::vector disposedRuleConfigurations; + std::string appIdParam = ""; + for (auto param : appIds) { + appIdParam = appIdParam.append(param) + ";"; + DisposedRuleConfiguration config; + config.appId = param; + config.appIndex = appIndex; + disposedRuleConfigurations.emplace_back(config); + } + APP_LOGI("appIds: %{public}s, appIndex: %{public}d, userId: %{public}d, uid: %{public}d", + appIdParam.c_str(), appIndex, userId, uid); + setuid(uid); + int32_t res = appControlProxy->DeleteDisposedRules(disposedRuleConfigurations, userId); + setuid(Constants::ROOT_UID); + APP_LOGI("DeleteDisposedRules return code: %{public}d", res); + if (res != OHOS::ERR_OK) { + resultReceiver_.append("error: failed to delete rules"); + return res; + } + resultReceiver_.append(std::to_string(res) + "\n"); + return result; +} + +ErrCode BundleTestTool::CheckDeleteRulesCorrectOption(int option, const std::string &commandName, + std::vector &appIds, int &appIndex, int &userId, int &uid) +{ + bool ret = true; + switch (option) { + case 'h': { + APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]); + return OHOS::ERR_INVALID_VALUE; + } + case 'a': { + std::string arrayAppId = optarg; + std::stringstream array(arrayAppId); + std::string object; + while (getline(array, object, ',')) { + appIds.emplace_back(object); + } + APP_LOGD("bundle_test_tool %{public}s -a %{public}s", commandName.c_str(), argv_[optind - 1]); + break; + } + case 'c': { + StringToInt(optarg, commandName, uid, ret); + break; + } + case 'i': { + StringToInt(optarg, commandName, appIndex, ret); + break; + } + case 'u': { + StringToInt(optarg, commandName, userId, ret); + break; + } + default: { + std::string unknownOption = ""; + std::string unknownOptionMsg = GetUnknownOptionMsg(unknownOption); + APP_LOGD("bundle_test_tool %{public}s with an unknown option.", commandName.c_str()); + resultReceiver_.append(unknownOptionMsg); + return OHOS::ERR_INVALID_VALUE; + } + } + return OHOS::ERR_OK; +} + // bundle_test_tool addAppInstallRule -a test1,test2 -t 1 -u 101 -e 3057 ErrCode BundleTestTool::RunAsAddInstallRuleCommand() { @@ -4712,10 +4833,11 @@ bool BundleTestTool::ProcessAppDistributionTypeEnums(std::vector ap void BundleTestTool::ReloadNativeTokenInfo() { - const int32_t permsNum = 1; + const int32_t permsNum = 2; uint64_t tokenId; const char *perms[permsNum]; perms[0] = "ohos.permission.MANAGE_EDM_POLICY"; + perms[1] = "ohos.permission.MANAGE_DISPOSED_APP_STATUS"; NativeTokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = permsNum, -- Gitee