diff --git a/frameworks/include/bundle_test_tool.h b/frameworks/include/bundle_test_tool.h index 25f84f17e8fae3d38fcfc482c09068f9c43b7f62..b7b33117850ced9e4cd944c0f5d1160de764a684 100644 --- a/frameworks/include/bundle_test_tool.h +++ b/frameworks/include/bundle_test_tool.h @@ -74,6 +74,8 @@ private: ErrCode RunAsSwitchQuickFix(); ErrCode RunAsDeleteQuickFix(); ErrCode RunAsSetDebugMode(); + ErrCode RunAsIsDebugMode(); + ErrCode RunAsGetModuleMetadata(); ErrCode RunAsGetBundleStats(); ErrCode RunAsGetAppProvisionInfo(); ErrCode RunAsGetContinueBundleName(); @@ -194,6 +196,7 @@ private: bool ProcessAppDistributionTypeEnums(std::vector appDistributionTypeStrings, std::set &appDistributionTypeEnums); void ReloadNativeTokenInfo(); + void ProcessBundleMetadata(const std::string &bundleName, int32_t bundlInfoFlag, int32_t userId); ErrCode InnerGetSimpleAppInfoForUid(const int32_t &option, std::vector &uids); ErrCode UninstallPreInstallBundleOperation( const std::string &bundleName, InstallParam &installParam) const; diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index 70b8cc14ec679bf6396d278d93baac986c5a7f19..46e7368656f0c90d70de9da2a19c4e81538bd04c 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -172,6 +172,8 @@ static const std::string HELP_MSG = " switchQuickFix switch a quick fix patch of an already installed bundle\n" " deleteQuickFix delete a quick fix patch of an already installed bundle\n" " setDebugMode enable signature debug mode\n" + " isDebugMode get is debug mode\n" + " getModuleMetadata get module metadata\n" " getBundleStats get bundle stats\n" " getAppProvisionInfo get appProvisionInfo\n" " getDistributedBundleName get distributedBundleName\n" @@ -435,6 +437,20 @@ const std::string HELP_MSG_SET_DEBUG_MODE = " -e, --enable enable signature debug mode, 1 represents enable debug mode and 0\n" " represents disable debug mode\n"; +const std::string HELP_MSG_IS_DEBUG_MODE = + "usage: bundle_test_tool isDebugMode \n" + "eg:bundle_test_tool isDebugMode -n \n" + "options list:\n" + " -h, --help list available commands\n" + " -n, --bundle-name specify bundle name of the application\n"; + +const std::string HELP_MSG_GET_MODULE_METADATA = + "usage: bundle_test_tool getModuleMetadata \n" + "eg:bundle_test_tool getModuleMetadata -n \n" + "options list:\n" + " -h, --help list available commands\n" + " -n, --bundle-name specify bundle name of the application\n"; + const std::string HELP_MSG_GET_BUNDLE_STATS = "usage: bundle_test_tool getBundleStats \n" "eg:bundle_test_tool getBundleStats -n \n" @@ -444,7 +460,6 @@ const std::string HELP_MSG_GET_BUNDLE_STATS = " -u, --user-id specify a user id\n" " -a, --app-index specify a app index\n"; - const std::string HELP_MSG_GET_APP_PROVISION_INFO = "usage: bundle_test_tool getAppProvisionInfo \n" "eg:bundle_test_tool getAppProvisionInfo -n \n" @@ -711,6 +726,12 @@ const std::string STRING_DELETE_QUICK_FIX_NG = "delete quick fix failed\n"; const std::string STRING_SET_DEBUG_MODE_OK = "set debug mode successfully\n"; const std::string STRING_SET_DEBUG_MODE_NG = "set debug mode failed\n"; +const std::string STRING_IS_DEBUG_MODE_OK = "get debug mode successfully\n"; +const std::string STRING_IS_DEBUG_MODE_NG = "get debug mode failed\n"; + +const std::string STRING_GET_MODULE_METADATA_OK = "get module metadata successfully\n"; +const std::string STRING_GET_MODULE_METADATA_NG = "get module metadata failed\n"; + const std::string STRING_GET_BUNDLE_STATS_OK = "get bundle stats successfully\n"; const std::string STRING_GET_BUNDLE_STATS_NG = "get bundle stats failed\n"; @@ -1071,6 +1092,20 @@ const struct option LONG_OPTIONS_GET_ASSET_ACCESS_GROUPS[] = { {nullptr, 0, nullptr, 0}, }; +const std::string SHORT_OPTIONS_IS_DEBUG_MODE_GROUPS = "hn:"; +const struct option LONG_OPTIONS_IS_DEBUG_MODE_GROUPS[] = { + {"help", no_argument, nullptr, 'h'}, + {"bundle-name", required_argument, nullptr, 'n'}, + {nullptr, 0, nullptr, 0}, +}; + +const std::string SHORT_OPTIONS_GET_MODULE_METADATA_GROUPS = "hn:"; +const struct option LONG_OPTIONS_GET_MODULE_METADATA_GROUPS[] = { + {"help", no_argument, nullptr, 'h'}, + {"bundle-name", required_argument, nullptr, 'n'}, + {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'}, @@ -1194,6 +1229,8 @@ ErrCode BundleTestTool::CreateCommandMap() {"switchQuickFix", std::bind(&BundleTestTool::RunAsSwitchQuickFix, this)}, {"deleteQuickFix", std::bind(&BundleTestTool::RunAsDeleteQuickFix, this)}, {"setDebugMode", std::bind(&BundleTestTool::RunAsSetDebugMode, this)}, + {"isDebugMode", std::bind(&BundleTestTool::RunAsIsDebugMode, this)}, + {"getModuleMetadata", std::bind(&BundleTestTool::RunAsGetModuleMetadata, this)}, {"getBundleStats", std::bind(&BundleTestTool::RunAsGetBundleStats, this)}, {"getAppProvisionInfo", std::bind(&BundleTestTool::RunAsGetAppProvisionInfo, this)}, {"getDistributedBundleName", std::bind(&BundleTestTool::RunAsGetDistributedBundleName, this)}, @@ -3714,6 +3751,127 @@ ErrCode BundleTestTool::RunAsSetDebugMode() return setResult; } +ErrCode BundleTestTool::RunAsIsDebugMode() +{ + APP_LOGI("RunAsIsDebugMode start"); + int result = OHOS::ERR_OK; + int counter = 0; + std::string commandName = "getModuleMetadata"; + std::string bundleName = ""; + auto bundlInfoFlag = + static_cast(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION); + int32_t userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID); + while (true) { + counter++; + int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_IS_DEBUG_MODE_GROUPS.c_str(), + LONG_OPTIONS_IS_DEBUG_MODE_GROUPS, 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_NO_GET_JSON_PROFILE_OPTION); + return OHOS::ERR_INVALID_VALUE; + } + break; + } + result = !CheckGetAssetAccessGroupsOption(option, commandName, bundleName) + ? OHOS::ERR_INVALID_VALUE : result; + APP_LOGE("bundleName = %{public}s", bundleName.c_str()); + break; + } + if (result != OHOS::ERR_OK) { + resultReceiver_.append(HELP_MSG_IS_DEBUG_MODE); + } else { + BundleInfo bundleInfo; + auto res = bundleMgrProxy_->GetBundleInfoV9(bundleName, bundlInfoFlag, bundleInfo, userId); + if (res != OHOS::ERR_OK) { + resultReceiver_.append(STRING_IS_DEBUG_MODE_NG); + return result; + } else { + bool isDebugMode = bundleInfo.applicationInfo.debug; + resultReceiver_.append(STRING_IS_DEBUG_MODE_OK + std::to_string(isDebugMode) + "\n"); + } + } + return result; +} + +ErrCode BundleTestTool::RunAsGetModuleMetadata() +{ + APP_LOGI("RunAsGetModuleMetadata start"); + int result = OHOS::ERR_OK; + int counter = 0; + std::string commandName = "getModuleMetadata"; + std::string bundleName = ""; + auto bundlInfoFlag = + static_cast(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_METADATA) | + static_cast(OHOS::AppExecFwk::GetBundleInfoFlag::GET_BUNDLE_INFO_WITH_APPLICATION); + int32_t userId = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID); + while (true) { + counter++; + int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_GET_MODULE_METADATA_GROUPS.c_str(), + LONG_OPTIONS_GET_MODULE_METADATA_GROUPS, 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_NO_GET_JSON_PROFILE_OPTION); + return OHOS::ERR_INVALID_VALUE; + } + break; + } + result = !CheckGetAssetAccessGroupsOption(option, commandName, bundleName) + ? OHOS::ERR_INVALID_VALUE : result; + APP_LOGE("bundleName = %{public}s", bundleName.c_str()); + break; + } + if (result != OHOS::ERR_OK) { + resultReceiver_.append(HELP_MSG_GET_MODULE_METADATA); + } else { + ProcessBundleMetadata(bundleName, bundlInfoFlag, userId); + } + return ERR_OK; +} + +void BundleTestTool::ProcessBundleMetadata(const std::string &bundleName, int32_t bundlInfoFlag, int32_t userId) +{ + BundleInfo bundleInfo = {}; + auto res = bundleMgrProxy_->GetBundleInfoV9(bundleName, bundlInfoFlag, bundleInfo, userId); + if (res != OHOS::ERR_OK) { + APP_LOGE("GetBundleInfo for module metadata failed"); + resultReceiver_.append(STRING_GET_MODULE_METADATA_NG); + return; + } + + if (bundleInfo.applicationInfo.metadata.empty()) { + APP_LOGE("applicationInfo metadata is empty"); + resultReceiver_.append(STRING_GET_MODULE_METADATA_OK + "metadata is empty" + "\n"); + return; + } + + for (const auto &metadata : bundleInfo.applicationInfo.metadata) { + if (metadata.second.size() <= 0) { + APP_LOGW("metadata size is 0"); + resultReceiver_.append(STRING_GET_MODULE_METADATA_OK + "moduleName :" + + metadata.first.c_str() + "metadata is empty" + "\n"); + continue; + } + + for (size_t j = 0; j < metadata.second.size(); ++j) { + std::string msg = metadata.first; + msg += "{name:" + metadata.second[j].name; + msg += ", resource:" + metadata.second[j].resource; + msg += ", value:" + metadata.second[j].value; + msg += ", valueId:" + std::to_string(metadata.second[j].valueId); + msg += "}\n"; + resultReceiver_.append(msg); + } + } +} + ErrCode BundleTestTool::SetDebugMode(int32_t debugMode) { if (debugMode != 0 && debugMode != 1) {