From 8b45e21ba57373dafa42cb056d81e2f9c85ff825 Mon Sep 17 00:00:00 2001 From: jiangminsen Date: Wed, 19 Feb 2025 16:22:14 +0800 Subject: [PATCH 1/4] bundle_test_tool add setAppDistributionTypes Signed-off-by: jiangminsen --- bundle.json | 1 + frameworks/BUILD.gn | 4 + frameworks/include/bundle_test_tool.h | 6 + frameworks/src/bundle_command_common.cpp | 4 + frameworks/src/bundle_test_tool.cpp | 166 ++++++++++++++++++++++- 5 files changed, 179 insertions(+), 2 deletions(-) diff --git a/bundle.json b/bundle.json index 012bbff..77f22d9 100644 --- a/bundle.json +++ b/bundle.json @@ -19,6 +19,7 @@ "deps": { "components": [ "ability_base", + "access_token", "ability_runtime", "bundle_framework", "common_event_service", diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 9694c0b..618f99b 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -140,6 +140,10 @@ ohos_source_set("tools_test_bm_source_set") { external_deps = [ "ability_base:want", + "access_token:libaccesstoken_sdk", + "access_token:libnativetoken", + "access_token:libtoken_setproc", + "access_token:libtokenid_sdk", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", "bundle_framework:bundle_tool_libs", diff --git a/frameworks/include/bundle_test_tool.h b/frameworks/include/bundle_test_tool.h index 4c63b30..80be211 100644 --- a/frameworks/include/bundle_test_tool.h +++ b/frameworks/include/bundle_test_tool.h @@ -110,6 +110,7 @@ private: ErrCode RunAsGetSimpleAppInfoForUid(); ErrCode RunAsGetBundleNameByAppId(); ErrCode RunAsGetAssetAccessGroups(); + ErrCode RunAsSetAppDistributionTypes(); std::condition_variable cv_; std::mutex mutex_; @@ -186,6 +187,11 @@ private: ErrCode GetContinueBundleName(const std::string &bundleName, int32_t userId, std::string& msg); bool CheckGetAssetAccessGroupsOption(int32_t option, const std::string &commandName, std::string &bundleName); + bool CheckSetAppDistributionTypesOption(int32_t option, const std::string &commandName, + std::string &appDistributionTypes); + bool ProcessAppDistributionTypeEnums(std::vector appDistributionTypeStrings, + std::set &appDistributionTypeEnums); + void ReloadNativeTokenInfo(); ErrCode InnerGetSimpleAppInfoForUid(const int32_t &option, std::vector &uids); }; } // namespace AppExecFwk diff --git a/frameworks/src/bundle_command_common.cpp b/frameworks/src/bundle_command_common.cpp index 800cfc7..142ac93 100644 --- a/frameworks/src/bundle_command_common.cpp +++ b/frameworks/src/bundle_command_common.cpp @@ -719,6 +719,10 @@ std::map BundleCommandCommon::bundleMessageMap_ = { IStatusReceiver::ERR_APPEXECFWK_INSTALL_OS_INTEGRATION_BUNDLE_NOT_ALLOWED_FOR_SHELL, "error: os_integration Bundle is not allowed to install for shell." }, + { + IStatusReceiver::ERR_APP_DISTRIBUTION_TYPE_NOT_ALLOW_INSTALL_ISR, + "error: Failed to install the HAP or HSP because the app distribution type is not allowed." + }, }; } // AppExecFwk } // OHOS \ No newline at end of file diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index f6fea56..7b0a796 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -26,6 +26,7 @@ #include #include +#include "accesstoken_kit.h" #include "app_log_wrapper.h" #include "appexecfwk_errors.h" #include "bundle_command_common.h" @@ -36,10 +37,15 @@ #include "bundle_tool_callback_stub.h" #include "common_event_manager.h" #include "common_event_support.h" +#include "permission_define.h" +#include "iservice_registry.h" #include "data_group_info.h" #include "directory_ex.h" #include "parameter.h" #include "process_cache_callback_host.h" +#include "nativetoken_kit.h" +#include "token_setproc.h" +#include "system_ability_definition.h" #ifdef BUNDLE_FRAMEWORK_QUICK_FIX #include "quick_fix_status_callback_host_impl.h" #endif @@ -185,7 +191,8 @@ static const std::string HELP_MSG = " getCompatibleDeviceType obtain the compatible device type based on bundleName\n" " getSimpleAppInfoForUid get bundlename list and appIndex list by uid list\n" " getBundleNameByAppId get bundlename by appid or appIdentifier\n" - " getAssetAccessGroups get asset access groups by bundlename\n"; + " getAssetAccessGroups get asset access groups by bundlename\n" + " setAppDistributionTypes set white list of appDistributionType\n"; const std::string HELP_MSG_GET_REMOVABLE = "usage: bundle_test_tool getrm \n" @@ -625,6 +632,13 @@ const std::string HELP_MSG_GET_ASSET_ACCESS_GROUPS = " -h, --help list available commands\n" " -n, --bundle-name specify bundle name of the application\n"; +const std::string HELP_MSG_SET_APP_DISTRIBUTION_TYPES = + "usage: bundle_test_tool setAppDistributionTypes \n" + "eg:bundle_test_tool setAppDistributionTypes -a \n" + "options list:\n" + " -h, --help list available commands\n" + " -a, --app_distribution_types specify app distribution type list\n"; + const std::string STRING_IS_BUNDLE_INSTALLED_OK = "IsBundleInstalled is ok \n"; const std::string STRING_IS_BUNDLE_INSTALLED_NG = "error: failed to IsBundleInstalled \n"; @@ -730,6 +744,9 @@ const std::string STRING_GET_PROXY_DATA_NG = "get proxyData failed"; const std::string STRING_GET_ASSET_ACCESS_GROUPS_OK = "getAssetAccessGroups successfully\n"; const std::string STRING_GET_ASSET_ACCESS_GROUPS_NG = "getAssetAccessGroups failed\n"; +const std::string STRING_SET_APP_DISTRIBUTION_TYPES_OK = "setAppDistributionTypes successfully\n"; +const std::string STRING_SET_APP_DISTRIBUTION_TYPES_NG = "setAppDistributionTypes failed\n"; + const std::string GET_BUNDLE_STATS_ARRAY[] = { "app data size: ", "user data size: ", @@ -1010,6 +1027,13 @@ const struct option LONG_OPTIONS_GET_ASSET_ACCESS_GROUPS[] = { {nullptr, 0, nullptr, 0}, }; +const std::string SHORT_OPTIONS_SET_APP_DISTRIBUTION_TYPES = "ha:"; +const struct option LONG_OPTIONS_SET_APP_DISTRIBUTION_TYPES[] = { + {"help", no_argument, nullptr, 'h'}, + {"app-distribution-types", required_argument, nullptr, 'a'}, + {nullptr, 0, nullptr, 0}, +}; + } // namespace class ProcessCacheCallbackImpl : public ProcessCacheCallbackHost { @@ -1169,7 +1193,9 @@ ErrCode BundleTestTool::CreateCommandMap() {"getBundleNameByAppId", std::bind(&BundleTestTool::RunAsGetBundleNameByAppId, this)}, {"getAssetAccessGroups", - std::bind(&BundleTestTool::RunAsGetAssetAccessGroups, this)} + std::bind(&BundleTestTool::RunAsGetAssetAccessGroups, this)}, + {"setAppDistributionTypes", + std::bind(&BundleTestTool::RunAsSetAppDistributionTypes, this)} }; return OHOS::ERR_OK; @@ -4366,6 +4392,142 @@ ErrCode BundleTestTool::RunAsGetAssetAccessGroups() return result; } +bool BundleTestTool::CheckSetAppDistributionTypesOption(int32_t option, const std::string &commandName, + std::string &appDistributionTypes) +{ + bool ret = true; + switch (option) { + case 'h': { + APP_LOGD("bundle_test_tool %{public}s %{public}s", commandName.c_str(), argv_[optind - 1]); + ret = false; + break; + } + case 'a': { + APP_LOGD("'bundle_test_tool %{public}s %{public}s'", commandName.c_str(), argv_[optind - 1]); + appDistributionTypes = optarg; + 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); + ret = false; + break; + } + } + return ret; +} + +bool BundleTestTool::ProcessAppDistributionTypeEnums(std::vector appDistributionTypeStrings, + std::set &appDistributionTypeEnums) +{ + for (const std::string& item : appDistributionTypeStrings) { + if (item == + std::to_string(static_cast(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_APP_GALLERY))) { + appDistributionTypeEnums.insert(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_APP_GALLERY); + continue; + } + if (item == + std::to_string(static_cast(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_ENTERPRISE))) { + appDistributionTypeEnums.insert(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_ENTERPRISE); + continue; + } + if (item == + std::to_string(static_cast(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_ENTERPRISE_NORMAL))) { + appDistributionTypeEnums.insert(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_ENTERPRISE_NORMAL); + continue; + } + if (item == + std::to_string(static_cast(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_ENTERPRISE_MDM))) { + appDistributionTypeEnums.insert(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_ENTERPRISE_MDM); + continue; + } + if (item == + std::to_string(static_cast(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_INTERNALTESTING))) { + appDistributionTypeEnums.insert(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_INTERNALTESTING); + continue; + } + if (item == + std::to_string(static_cast(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_CROWDTESTING))) { + appDistributionTypeEnums.insert(AppDistributionTypeEnum::APP_DISTRIBUTION_TYPE_CROWDTESTING); + continue; + } + return false; + } + return true; +} + +void BundleTestTool::ReloadNativeTokenInfo() +{ + const int32_t permsNum = 1; + uint64_t tokenId; + const char *perms[permsNum]; + perms[0] = "ohos.permission.GET_DEFAULT_APPLICATION"; + NativeTokenInfoParams infoInstance = { + .dcapsNum = 0, + .permsNum = permsNum, + .aclsNum = 0, + .dcaps = NULL, + .perms = perms, + .acls = NULL, + .processName = "kit_system_test", + .aplStr = "system_core", + }; + tokenId = GetAccessTokenId(&infoInstance); + SetSelfTokenID(tokenId); + OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); +} + +ErrCode BundleTestTool::RunAsSetAppDistributionTypes() +{ + APP_LOGI("RunAsSetAppDistributionTypes start"); + int result = OHOS::ERR_OK; + int counter = 0; + std::string commandName = "setAppDistributionTypes"; + std::string appDistributionTypes = ""; + while (true) { + counter++; + int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_SET_APP_DISTRIBUTION_TYPES.c_str(), + LONG_OPTIONS_SET_APP_DISTRIBUTION_TYPES, 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) { + if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) { + resultReceiver_.append(HELP_MSG_SET_APP_DISTRIBUTION_TYPES); + return OHOS::ERR_INVALID_VALUE; + } + break; + } + result = !CheckSetAppDistributionTypesOption(option, commandName, appDistributionTypes) + ? OHOS::ERR_INVALID_VALUE : result; + APP_LOGE("appDistributionTypes = %{public}s", appDistributionTypes.c_str()); + } + if (result != OHOS::ERR_OK) { + resultReceiver_.append(HELP_MSG_SET_APP_DISTRIBUTION_TYPES); + } else { + std::string results = ""; + std::vector appDistributionTypeStrings; + OHOS::SplitStr(appDistributionTypes, ",", appDistributionTypeStrings); + std::set appDistributionTypeEnums; + if (!ProcessAppDistributionTypeEnums(appDistributionTypeStrings, appDistributionTypeEnums)) { + APP_LOGE("appDistributionTypes param %{public}d failed", appDistributionTypes.c_str()); + resultReceiver_.append(STRING_SET_APP_DISTRIBUTION_TYPES_NG); + return OHOS::ERR_INVALID_VALUE; + } + auto res = bundleMgrProxy_->SetAppDistributionTypes(appDistributionTypeEnums); + if (res != OHOS::ERR_OK) { + resultReceiver_.append(STRING_SET_APP_DISTRIBUTION_TYPES_NG); + return result; + } else { + resultReceiver_.append(STRING_SET_APP_DISTRIBUTION_TYPES_OK); + } + } + return result; +} + ErrCode BundleTestTool::RunAsGetJsonProfile() { APP_LOGI("RunAsGetJsonProfile start"); -- Gitee From e1d0a68466d8692ddb9f6965a12e3ca7930aae1e Mon Sep 17 00:00:00 2001 From: jiangminsen Date: Thu, 20 Feb 2025 11:46:53 +0800 Subject: [PATCH 2/4] bundle_test_tool add setAppDistributionTypes Signed-off-by: jiangminsen --- frameworks/src/bundle_test_tool.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index 7b0a796..ba5265a 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -4476,12 +4476,14 @@ void BundleTestTool::ReloadNativeTokenInfo() }; tokenId = GetAccessTokenId(&infoInstance); SetSelfTokenID(tokenId); + APP_LOGI("RunAsSetAppDistributionTypes ReloadNativeTokenInfo"); OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo(); } ErrCode BundleTestTool::RunAsSetAppDistributionTypes() { APP_LOGI("RunAsSetAppDistributionTypes start"); + ReloadNativeTokenInfo(); int result = OHOS::ERR_OK; int counter = 0; std::string commandName = "setAppDistributionTypes"; -- Gitee From 07b2cc46a481905386ceb4ad4795cd18bd30ab3d Mon Sep 17 00:00:00 2001 From: jiangminsen Date: Thu, 20 Feb 2025 11:57:11 +0800 Subject: [PATCH 3/4] bundle_test_tool add setAppDistributionTypes Signed-off-by: jiangminsen --- frameworks/src/bundle_test_tool.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index ba5265a..8b0889e 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -4515,7 +4515,7 @@ ErrCode BundleTestTool::RunAsSetAppDistributionTypes() OHOS::SplitStr(appDistributionTypes, ",", appDistributionTypeStrings); std::set appDistributionTypeEnums; if (!ProcessAppDistributionTypeEnums(appDistributionTypeStrings, appDistributionTypeEnums)) { - APP_LOGE("appDistributionTypes param %{public}d failed", appDistributionTypes.c_str()); + APP_LOGE("appDistributionTypes param %{public}s failed", appDistributionTypes.c_str()); resultReceiver_.append(STRING_SET_APP_DISTRIBUTION_TYPES_NG); return OHOS::ERR_INVALID_VALUE; } -- Gitee From f02725a78875e4e37ba24f578c597aa0898ed4f3 Mon Sep 17 00:00:00 2001 From: jiangminsen Date: Thu, 20 Feb 2025 14:22:31 +0800 Subject: [PATCH 4/4] bundle_test_tool add setAppDistributionTypes Signed-off-by: jiangminsen --- frameworks/src/bundle_test_tool.cpp | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index 8b0889e..ca19bd1 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -4463,7 +4463,7 @@ void BundleTestTool::ReloadNativeTokenInfo() const int32_t permsNum = 1; uint64_t tokenId; const char *perms[permsNum]; - perms[0] = "ohos.permission.GET_DEFAULT_APPLICATION"; + perms[0] = "ohos.permission.MANAGE_EDM_POLICY"; NativeTokenInfoParams infoInstance = { .dcapsNum = 0, .permsNum = permsNum, @@ -4471,7 +4471,7 @@ void BundleTestTool::ReloadNativeTokenInfo() .dcaps = NULL, .perms = perms, .acls = NULL, - .processName = "kit_system_test", + .processName = "bundleTestToolProcess", .aplStr = "system_core", }; tokenId = GetAccessTokenId(&infoInstance); -- Gitee