From 996fbf8e61402c362c53e66339aea68073d57d93 Mon Sep 17 00:00:00 2001 From: 18710539902 Date: Fri, 17 May 2024 17:48:28 +0800 Subject: [PATCH] modify tests Signed-off-by: 18710539902 Change-Id: I631f23c2b64cdd6c64c52b9264c007f59bbc783d --- frameworks/BUILD.gn | 2 + frameworks/include/bundle_test_tool.h | 20 ++ frameworks/include/test_help_message.h | 77 +++++++ frameworks/src/bundle_test_tool.cpp | 89 +++++++- frameworks/src/test_batch_get_bundle_info.cpp | 212 ++++++++++++++++++ .../src/test_batch_query_ability_info.cpp | 187 +++++++++++++++ 6 files changed, 581 insertions(+), 6 deletions(-) create mode 100644 frameworks/include/test_help_message.h create mode 100644 frameworks/src/test_batch_get_bundle_info.cpp create mode 100644 frameworks/src/test_batch_query_ability_info.cpp diff --git a/frameworks/BUILD.gn b/frameworks/BUILD.gn index 283c3f1..f8b956a 100644 --- a/frameworks/BUILD.gn +++ b/frameworks/BUILD.gn @@ -100,6 +100,8 @@ ohos_source_set("tools_test_bm_source_set") { "src/main_test_tool.cpp", "src/shell_command.cpp", "src/status_receiver_impl.cpp", + "src/test_batch_get_bundle_info.cpp", + "src/test_batch_query_ability_info.cpp", ] public_configs = [ ":tools_bm_config" ] diff --git a/frameworks/include/bundle_test_tool.h b/frameworks/include/bundle_test_tool.h index bfe1094..504e176 100644 --- a/frameworks/include/bundle_test_tool.h +++ b/frameworks/include/bundle_test_tool.h @@ -95,6 +95,9 @@ private: ErrCode RunAsImplicitQuerySkillUriInfo(); ErrCode RunAsQueryAbilityInfoByContinueType(); + ErrCode RunAsBatchQueryAbilityInfo(); + ErrCode RunAsBatchGetBundleInfo(); + std::condition_variable cv_; std::mutex mutex_; bool dataReady_ {false}; @@ -162,6 +165,23 @@ private: ErrCode CheckGetGroupIdCorrectOption(int32_t option, std::string &dataGroupId); bool GetGroupDir(const std::string &dataGroupId, std::string& msg); ErrCode CheckGetBundleNameOption(int32_t option, std::string &bundleName); + + bool CheckBatchQueryAbilityInfoOption( + int option, const std::string &commandName, + std::string &wantsStr, int &flag, int &userId); + bool CheckBatchGetBundleInfoOption( + int option, const std::string &commandName, + std::string &wantsStr, std::vector &bundleNames, int &flag, int &userId); + + bool ParseBatchQueryAbilityInfoParams(const std::string &commandName, + std::string &wantsStr, int &flag, int &userId); + bool ParseBatchGetBundleInfoParams(const std::string &commandName, + std::string &wantsStr, std::vector &bundleNames, int &flag, int &userId); + + bool ParseWantsStr(const std::string &wantsStr, std::vector &wants); + std::string DumpBundleInfos(std::vector bundleInfos); + std::string DumpAbilityInfos(std::vector abilityInfos); + std::string BundleNamesToStr(const std::vector &bundleNames); }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/include/test_help_message.h b/frameworks/include/test_help_message.h new file mode 100644 index 0000000..33cf0cb --- /dev/null +++ b/frameworks/include/test_help_message.h @@ -0,0 +1,77 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef FOUNDATION_BUNDLEMANAGER_BUNDLE_FRAMEWORK_BUNDLE_TOOL_INCLUDE_TEST_HELP_MESSAGE_H +#define FOUNDATION_BUNDLEMANAGER_BUNDLE_FRAMEWORK_BUNDLE_TOOL_INCLUDE_TEST_HELP_MESSAGE_H + +#include + +namespace OHOS { +namespace AppExecFwk { +const std::string HELP_MSG_OF_HELP = + " -h, --help list available commands\n"; + +const std::string HELP_MSG_OF_BUNDLE_NAME = + " -n, --bundleName specify bundle name of the application\n"; + +const std::string HELP_MSG_OF_WANT_LIST = + " -w, --want specify json format about Want list of the application\n" + " This json string about want must contains the following fields:\n" + " deviceId : String\n" + " bundleName : String\n" + " abilityName : String\n" + " uri : String\n" + " type : String\n" + " flags : Number\n" + " action : String\n" + " parameters : String\n" + " entities : Array\n"; + +const std::string HELP_MSG_OF_GET_ABILITY_INFO_FLAG = + " -f, --flag specify flag of the application\n" + " flag can be the following value or combinations\n" + " 0: 0x00000000 : GET_ABILITY_INFO_DEFAULT\n" + " 1: 0x00000001 : GET_ABILITY_INFO_WITH_PERMISSION\n" + " 2: 0x00000002 : GET_ABILITY_INFO_WITH_APPLICATION\n" + " 4: 0x00000004 : GET_ABILITY_INFO_WITH_METADATA\n" + " 8: 0x00000008 : GET_ABILITY_INFO_WITH_DISABLE\n" + " 16: 0x00000010 : GET_ABILITY_INFO_ONLY_SYSTEM_APP\n" + " 32: 0x00000020 : GET_ABILITY_INFO_WITH_SKILL_URI\n" + " 64: 0x00000040 : GET_ABILITY_INFO_WITH_APP_LINKING\n" + " 128: 0x00000080 : GET_ABILITY_INFO_WITH_SKILL\n"; + +const std::string HELP_MSG_OF_GET_BUNDLE_INFO_FLAG = + " -f, --flag specify flag of the application\n" + " flag can be the following value or combinations\n" + " 0 = 0x00000000 : GET_BUNDLE_INFO_DEFAULT\n" + " 1 = 0x00000001 : GET_BUNDLE_INFO_WITH_APPLICATION\n" + " 2 = 0x00000002 : GET_BUNDLE_INFO_WITH_HAP_MODULE\n" + " 4 = 0x00000004 : GET_BUNDLE_INFO_WITH_ABILITY -- need 2\n" + " 8 = 0x00000008 : GET_BUNDLE_INFO_WITH_EXTENSION_ABILITY\n" + " 16 = 0x00000010 : GET_BUNDLE_INFO_WITH_REQUESTED_PERMISSION\n" + " 32 = 0x00000020 : GET_BUNDLE_INFO_WITH_METADATA\n" + " 64 = 0x00000040 : GET_BUNDLE_INFO_WITH_DISABLE\n" + " 128 = 0x00000080 : GET_BUNDLE_INFO_WITH_SIGNATURE_INFO\n" + " 256 = 0x00000100 : GET_BUNDLE_INFO_WITH_MENU\n" + " 512 = 0x00000200 : GET_BUNDLE_INFO_WITH_ROUTER_MAP\n" + " 1024 = 0x00000400 : GET_BUNDLE_INFO_WITH_CLONE_BUNDLE\n" + " 2048 = 0x00000800 : GET_BUNDLE_INFO_WITH_SKILL -- need 4\n"; + +const std::string HELP_MSG_OF_USER_ID = + " -u, --uid specify uid of the application\n"; +} // AppExecFwk +} // OHOS + +#endif // FOUNDATION_BUNDLEMANAGER_BUNDLE_FRAMEWORK_BUNDLE_TOOL_INCLUDE_TEST_HELP_MESSAGE_H \ No newline at end of file diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index 665afc2..b6c009c 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -44,6 +44,7 @@ #include "status_receiver_impl.h" #include "string_ex.h" #include "json_util.h" +#include "test_help_message.h" namespace OHOS { namespace AppExecFwk { @@ -166,7 +167,9 @@ static const std::string HELP_MSG = " getJsonProfile obtain the json string of the specified module\n" " getOdid obtain the odid of the application\n" " implicitQuerySkillUriInfo obtain the skill uri info of the implicit query ability\n" - " queryAbilityInfoByContinueType get ability info by continue type\n"; + " queryAbilityInfoByContinueType get ability info by continue type\n" + " batchQueryAbilityInfo batch get ability infos by given wants\n" + " batchGetBundleInfo batch get bundle infos by given wants\n"; const std::string HELP_MSG_GET_REMOVABLE = "usage: bundle_test_tool getrm \n" @@ -845,10 +848,11 @@ ErrCode BundleTestTool::CreateCommandMap() {"getJsonProfile", std::bind(&BundleTestTool::RunAsGetJsonProfile, this)}, {"getUninstalledBundleInfo", std::bind(&BundleTestTool::RunAsGetUninstalledBundleInfo, this)}, {"getOdid", std::bind(&BundleTestTool::RunAsGetOdid, this)}, - {"implicitQuerySkillUriInfo", - std::bind(&BundleTestTool::RunAsImplicitQuerySkillUriInfo, this)}, + {"implicitQuerySkillUriInfo", std::bind(&BundleTestTool::RunAsImplicitQuerySkillUriInfo, this)}, {"queryAbilityInfoByContinueType", - std::bind(&BundleTestTool::RunAsQueryAbilityInfoByContinueType, this)} + std::bind(&BundleTestTool::RunAsQueryAbilityInfoByContinueType, this)}, + {"batchQueryAbilityInfo", std::bind(&BundleTestTool::RunAsBatchQueryAbilityInfo, this)}, + {"batchGetBundleInfo", std::bind(&BundleTestTool::RunAsBatchGetBundleInfo, this)} }; return OHOS::ERR_OK; @@ -1719,7 +1723,6 @@ ErrCode BundleTestTool::RunAsGetAllProxyDataCommand() std::string commandName = "getProxyData"; std::string name = ""; int userId = Constants::ALL_USERID; - APP_LOGD("RunAsGetAllProxyDataCommand is start"); while (true) { counter++; int32_t option = getopt_long( @@ -3865,7 +3868,7 @@ ErrCode BundleTestTool::RunAsGetJsonProfile() ErrCode BundleTestTool::RunAsGetUninstalledBundleInfo() { - APP_LOGI("RunAsGetUninstalledBundleInfo start"); + APP_LOGI("## BUNDLE_TEST_TOOL ## >>>> GetUninstalledBundleInfo Start..."); int result = OHOS::ERR_OK; int counter = 0; std::string bundleName = ""; @@ -3887,11 +3890,13 @@ ErrCode BundleTestTool::RunAsGetUninstalledBundleInfo() result = CheckGetBundleNameOption(option, bundleName); APP_LOGD("getUninstalledBundleInfo optind: %{public}s", bundleName.c_str()); } + APP_LOGI("## BUNDLE_TEST_TOOL ## PARAM: bundleName=%{publilc}s", bundleName.c_str()); if (result != OHOS::ERR_OK) { resultReceiver_.append(HELP_MSG_NO_GET_UNINSTALLED_BUNDLE_INFO_OPTION); } else { BundleInfo bundleInfo; + APP_LOGI("## BUNDLE_TEST_TOOL ## Call proxy: GetUninstalledBundleInfo"); auto res = bundleMgrProxy_->GetUninstalledBundleInfo(bundleName, bundleInfo); if (res != OHOS::ERR_OK) { resultReceiver_.append(STRING_GET_UNINSTALLED_BUNDLE_INFO_NG); @@ -3903,6 +3908,7 @@ ErrCode BundleTestTool::RunAsGetUninstalledBundleInfo() resultReceiver_.append(results); resultReceiver_.append("\n"); } + APP_LOGI("## BUNDLE_TEST_TOOL ## <<<< GetUninstalledBundleInfo End!!!"); return result; } @@ -4121,5 +4127,76 @@ ErrCode BundleTestTool::RunAsQueryAbilityInfoByContinueType() } return result; } + +bool BundleTestTool::ParseWantsStr(const std::string &wantsStr, std::vector &wants) +{ + if (wantsStr.empty()) { + return false; + } + nlohmann::json wantsJson = nlohmann::json::parse(wantsStr); + APP_LOGI("## BUNDLE_TEST_TOOL ## wantsJson is_array=%{public}s", wantsJson.is_array() ? "true" : "false"); + if (!wantsJson.is_array()) { + APP_LOGE("## BUNDLE_TEST_TOOL ## wantsJson is not array"); + return false; + } + APP_LOGI("## BUNDLE_TEST_TOOL ## wantsJson array size=%{public}zu", wantsJson.size()); + size_t len = wantsJson.size(); + for (size_t i = 0; i < len; i++) { + if (!wantsJson[i].is_object()) { + APP_LOGE("## BUNDLE_TEST_TOOL ## wantsJson %{public}zu is not object", i); + return false; + } + std::string wantStr = wantsJson[i].dump(); + APP_LOGI("## BUNDLE_TEST_TOOL ## wantStr: %{public}s", wantStr.c_str()); + Want *want = Want::FromString(wantStr); + if (want == nullptr) { + APP_LOGE("## BUNDLE_TEST_TOOL ## want FromString returns nullptr"); + return false; + } + APP_LOGI("## BUNDLE_TEST_TOOL ## want.bundleName=%{public}s", want->GetBundle().c_str()); + APP_LOGI("## BUNDLE_TEST_TOOL ## want.moduleName=%{public}s", want->GetModuleName().c_str()); + APP_LOGI("## BUNDLE_TEST_TOOL ## want.abilityName=%{public}s", want->GetElement().GetAbilityName().c_str()); + wants.push_back(*want); + } + APP_LOGI("## BUNDLE_TEST_TOOL ## wants.size=%{public}zu", wants.size()); + return true; +} + +std::string BundleTestTool::DumpBundleInfos(std::vector bundleInfos) +{ + std::string result = std::string("BundleInfos count=") + + std::to_string(bundleInfos.size()) + std::string("\n"); + for (size_t i = 0; i < bundleInfos.size(); i++) { + nlohmann::json jsonObject = bundleInfos[i]; + result += jsonObject.dump(Constants::DUMP_INDENT); + } + return result; +} + +std::string BundleTestTool::DumpAbilityInfos(std::vector abilityInfos) +{ + std::string result = std::string("AbilityInfos count=") + + std::to_string(abilityInfos.size()) + std::string("\n"); + for (size_t i = 0; i < abilityInfos.size(); i++) { + nlohmann::json jsonObject = abilityInfos[i]; + result += jsonObject.dump(Constants::DUMP_INDENT); + } + return result; +} + +std::string BundleTestTool::BundleNamesToStr(const std::vector &bundleNames) +{ + std::string bundleNameStr = "["; + int i = 0; + for (const auto &bundleName : bundleNames) { + if (i > 0) { + bundleNameStr += ","; + } + bundleNameStr += "{" + bundleName + "}"; + i++; + } + bundleNameStr += "]"; + return bundleNameStr; +} } // AppExecFwk } // OHOS \ No newline at end of file diff --git a/frameworks/src/test_batch_get_bundle_info.cpp b/frameworks/src/test_batch_get_bundle_info.cpp new file mode 100644 index 0000000..8ceffdc --- /dev/null +++ b/frameworks/src/test_batch_get_bundle_info.cpp @@ -0,0 +1,212 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "bundle_test_tool.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "app_log_wrapper.h" +#include "appexecfwk_errors.h" +#include "bundle_command_common.h" +#include "bundle_death_recipient.h" +#include "bundle_mgr_client.h" +#include "bundle_mgr_proxy.h" +#include "bundle_tool_callback_stub.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "data_group_info.h" +#include "directory_ex.h" +#include "parameter.h" +#ifdef BUNDLE_FRAMEWORK_QUICK_FIX +#include "quick_fix_status_callback_host_impl.h" +#endif +#include "status_receiver_impl.h" +#include "string_ex.h" +#include "json_util.h" +#include "test_help_message.h" + +namespace OHOS { +namespace AppExecFwk { +namespace { +const std::string HELP_MSG_BATCH_GET_BUNDLE_INFO = + std::string("usage: bundle_test_tool batchGetBundleInfo \n") + + std::string("eg1: bundle_test_tool batchGetBundleInfo -w -f -u \n") + + std::string("eg2: bundle_test_tool batchGetBundleInfo -n [-n [...]]") + + std::string(" -f -u \n") + + std::string("options list:\n") + + HELP_MSG_OF_HELP + + HELP_MSG_OF_WANT_LIST + + HELP_MSG_OF_BUNDLE_NAME + + HELP_MSG_OF_GET_BUNDLE_INFO_FLAG + + HELP_MSG_OF_USER_ID; + +const std::string STRING_BATCH_GET_BUNDLE_INFO_OK = "BatchGetBundleInfo succeed\n"; +const std::string STRING_BATCH_GET_BUNDLE_INFO_NG = "BatchGetBundleInfo failed\n"; + +const std::string SHORT_OPTIONS_BATCH_GET_BUNDLE_INFO = "hw:n:f:u:"; +const struct option LONG_OPTIONS_BATCH_GET_BUNDLE_INFO[] = { + {"help", no_argument, nullptr, 'h'}, + {"want", required_argument, nullptr, 'w'}, + {"bundleName", required_argument, nullptr, 'n'}, + {"flag", required_argument, nullptr, 'f'}, + {"userId", required_argument, nullptr, 'u'}, + {nullptr, 0, nullptr, 0}, +}; +} // namespace + +bool BundleTestTool::CheckBatchGetBundleInfoOption(int option, const std::string &commandName, + std::string &wantsStr, std::vector &bundleNames, int &flag, int &userId) +{ + 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 'w': { + APP_LOGD("bundle_test_tool %{public}s -w want:%{public}s, %{public}s", + commandName.c_str(), optarg, argv_[optind - 1]); + wantsStr = optarg; + break; + } + case 'n': { + APP_LOGD("bundle_test_tool %{public}s -n bundleName:%{public}s, %{public}s", + commandName.c_str(), optarg, argv_[optind - 1]); + bundleNames.push_back(optarg); + break; + } + case 'f': { + APP_LOGD("bundle_test_tool %{public}s -f flag:%{public}s, %{public}s", + commandName.c_str(), optarg, argv_[optind - 1]); + StringToInt(optarg, commandName, flag, ret); + break; + } + case 'u': { + APP_LOGD("bundle_test_tool %{public}s -u userId:%{public}s, %{public}s", + commandName.c_str(), optarg, argv_[optind - 1]); + 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); + ret = false; + break; + } + } + return ret; +} + +bool BundleTestTool::ParseBatchGetBundleInfoParams(const std::string &commandName, + std::string &wantsStr, std::vector &bundleNames, int &flag, int &userId) +{ + APP_LOGI("## BUNDLE_TEST_TOOL ## >>>> ParseBatchGetBundleInfoParams Start..."); + int counter = 0; + while (true) { + counter++; + int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_BATCH_GET_BUNDLE_INFO.c_str(), + LONG_OPTIONS_BATCH_GET_BUNDLE_INFO, nullptr); + APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", + option, optopt, optind); + if (optind < 0 || optind > argc_) { + return false; + } + if (option == -1) { + if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) { + return false; + } + break; + } + if (!CheckBatchGetBundleInfoOption(option, commandName, wantsStr, bundleNames, flag, userId)) { + return false; + } + } + APP_LOGI("## BUNDLE_TEST_TOOL ## PARAM: wantsStr=%{public}s", wantsStr.c_str()); + APP_LOGI("## BUNDLE_TEST_TOOL ## PARAM: bundleNames=%{public}s", BundleNamesToStr(bundleNames).c_str()); + APP_LOGI("## BUNDLE_TEST_TOOL ## PARAM: flag=%{public}d", flag); + APP_LOGI("## BUNDLE_TEST_TOOL ## PARAM: userId=%{public}d", userId); + return true; +} + +ErrCode BundleTestTool::RunAsBatchGetBundleInfo() +{ + APP_LOGI("## BUNDLE_TEST_TOOL ## >>>> BatchGetBundleInfo Start..."); + int result = OHOS::ERR_OK; + std::string commandName = "batchGetBundleInfo"; + std::string wantsStr; + std::vector bundleNames; + int flag = 0; + int userId = 100; + const std::string &helpMsg = HELP_MSG_BATCH_GET_BUNDLE_INFO; + const std::string &ngMsg = STRING_BATCH_GET_BUNDLE_INFO_NG; + const std::string &okMsg = STRING_BATCH_GET_BUNDLE_INFO_OK; + + if (!ParseBatchGetBundleInfoParams(commandName, wantsStr, bundleNames, flag, userId)) { + APP_LOGE("## BUNDLE_TEST_TOOL ## ParseBatchGetBundleInfoParams failed!"); + resultReceiver_.append(helpMsg); + return OHOS::ERR_INVALID_VALUE; + } + + if (wantsStr.empty() && bundleNames.empty()) { + APP_LOGE("## BUNDLE_TEST_TOOL ## wantsStr empty or bundleNames empty!"); + resultReceiver_.append(ngMsg); + return OHOS::ERR_INVALID_VALUE; + } + + std::vector wants; + if (!wantsStr.empty()) { + if (!ParseWantsStr(wantsStr, wants)) { + APP_LOGE("## BUNDLE_TEST_TOOL ## ParseWantsStr Failed!"); + resultReceiver_.append(ngMsg); + return OHOS::ERR_INVALID_VALUE; + } + } + + std::vector bundleInfos; + if (flag >= 0 && userId >= 0) { + APP_LOGI("## BUNDLE_TEST_TOOL ## Call proxy: BatchGetBundleInfo"); + if (!wantsStr.empty()) { + result = bundleMgrProxy_->BatchGetBundleInfo(wants, flag, bundleInfos, userId); + } else { + result = bundleMgrProxy_->BatchGetBundleInfo(bundleNames, flag, bundleInfos, userId); + } + } else { + result = OHOS::ERR_INVALID_VALUE; + } + APP_LOGI("## BUNDLE_TEST_TOOL ## return result=%{public}d", result); + + if (result != OHOS::ERR_OK) { + resultReceiver_.append(ngMsg); + } else { + resultReceiver_.append(okMsg); + resultReceiver_.append(DumpBundleInfos(bundleInfos) + "\n"); + } + + APP_LOGI("## BUNDLE_TEST_TOOL ## <<<< BatchGetBundleInfo End!!!"); + return result; +} +} // AppExecFwk +} // OHOS \ No newline at end of file diff --git a/frameworks/src/test_batch_query_ability_info.cpp b/frameworks/src/test_batch_query_ability_info.cpp new file mode 100644 index 0000000..6ba0ffe --- /dev/null +++ b/frameworks/src/test_batch_query_ability_info.cpp @@ -0,0 +1,187 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include "bundle_test_tool.h" + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "app_log_wrapper.h" +#include "appexecfwk_errors.h" +#include "bundle_command_common.h" +#include "bundle_death_recipient.h" +#include "bundle_mgr_client.h" +#include "bundle_mgr_proxy.h" +#include "bundle_tool_callback_stub.h" +#include "common_event_manager.h" +#include "common_event_support.h" +#include "data_group_info.h" +#include "directory_ex.h" +#include "parameter.h" +#ifdef BUNDLE_FRAMEWORK_QUICK_FIX +#include "quick_fix_status_callback_host_impl.h" +#endif +#include "status_receiver_impl.h" +#include "string_ex.h" +#include "json_util.h" +#include "test_help_message.h" + +namespace OHOS { +namespace AppExecFwk { +namespace { +const std::string HELP_MSG_BATCH_QUERY_ABILITY_INFO = + std::string("usage: bundle_test_tool batchQueryAbilityInfo \n") + + std::string("eg: bundle_test_tool batchQueryAbilityInfo -w -f -u \n") + + std::string("options list:\n") + + HELP_MSG_OF_HELP + + HELP_MSG_OF_WANT_LIST + + HELP_MSG_OF_GET_ABILITY_INFO_FLAG + + HELP_MSG_OF_USER_ID; + +const std::string STRING_BATCH_QUERY_ABILITY_INFO_OK = "BatchQueryAbilityInfo succeed\n"; +const std::string STRING_BATCH_QUERY_ABILITY_INFO_NG = "BatchQueryAbilityInfo failed\n"; + +const std::string SHORT_OPTIONS_BATCH_QUERY_ABILITY_INFO = "hw:f:u:"; +const struct option LONG_OPTIONS_BATCH_QUERY_ABILITY_INFO[] = { + {"help", no_argument, nullptr, 'h'}, + {"want", required_argument, nullptr, 'w'}, + {"flag", required_argument, nullptr, 'f'}, + {"userId", required_argument, nullptr, 'u'}, + {nullptr, 0, nullptr, 0}, +}; +} // namespace + +bool BundleTestTool::CheckBatchQueryAbilityInfoOption(int option, const std::string &commandName, + std::string &wantsStr, int &flag, int &userId) +{ + 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 'w': { + APP_LOGD("bundle_test_tool %{public}s -w want:%{public}s, %{public}s", + commandName.c_str(), optarg, argv_[optind - 1]); + wantsStr = optarg; + break; + } + case 'f': { + APP_LOGD("bundle_test_tool %{public}s -f flag:%{public}s, %{public}s", + commandName.c_str(), optarg, argv_[optind - 1]); + StringToInt(optarg, commandName, flag, ret); + break; + } + case 'u': { + APP_LOGD("bundle_test_tool %{public}s -u userId:%{public}s, %{public}s", + commandName.c_str(), optarg, argv_[optind - 1]); + 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); + ret = false; + break; + } + } + return ret; +} + +bool BundleTestTool::ParseBatchQueryAbilityInfoParams(const std::string &commandName, + std::string &wantsStr, int &flag, int &userId) +{ + int counter = 0; + while (true) { + counter++; + int32_t option = getopt_long(argc_, argv_, SHORT_OPTIONS_BATCH_QUERY_ABILITY_INFO.c_str(), + LONG_OPTIONS_BATCH_QUERY_ABILITY_INFO, nullptr); + APP_LOGD("option: %{public}d, optopt: %{public}d, optind: %{public}d", + option, optopt, optind); + if (optind < 0 || optind > argc_) { + return false; + } + if (option == -1) { + if ((counter == 1) && (strcmp(argv_[optind], cmd_.c_str()) == 0)) { + return false; + } + break; + } + if (!CheckBatchQueryAbilityInfoOption(option, commandName, wantsStr, flag, userId)) { + return false; + } + } + APP_LOGI("## BUNDLE_TEST_TOOL ## PARAM: wantsStr=%{public}s", wantsStr.c_str()); + APP_LOGI("## BUNDLE_TEST_TOOL ## PARAM: flag=%{public}d", flag); + APP_LOGI("## BUNDLE_TEST_TOOL ## PARAM: userId=%{public}d", userId); + return true; +} + +ErrCode BundleTestTool::RunAsBatchQueryAbilityInfo() +{ + APP_LOGI("## BUNDLE_TEST_TOOL ## >>>> BatchQueryAbilityInfo Start..."); + int result = OHOS::ERR_OK; + std::string commandName = "batchQueryAbilityInfo"; + std::string wantsStr; + int flag = 0; + int userId = 100; + const std::string &helpMsg = HELP_MSG_BATCH_QUERY_ABILITY_INFO; + const std::string &ngMsg = STRING_BATCH_QUERY_ABILITY_INFO_NG; + const std::string &okMsg = STRING_BATCH_QUERY_ABILITY_INFO_OK; + + if (!ParseBatchQueryAbilityInfoParams(commandName, wantsStr, flag, userId)) { + resultReceiver_.append(helpMsg); + return OHOS::ERR_INVALID_VALUE; + } + + std::vector wants; + if (!ParseWantsStr(wantsStr, wants)) { + APP_LOGE("## BUNDLE_TEST_TOOL ## ParseWantsStr Failed!"); + resultReceiver_.append(ngMsg); + return OHOS::ERR_INVALID_VALUE; + } + + std::vector abilityInfos; + if (flag >= 0 && userId >= 0) { + APP_LOGI("## BUNDLE_TEST_TOOL ## Call proxy: BatchQueryAbilityInfos"); + result = bundleMgrProxy_->BatchQueryAbilityInfos(wants, flag, userId, abilityInfos); + } else { + result = OHOS::ERR_INVALID_VALUE; + } + APP_LOGI("## BUNDLE_TEST_TOOL ## return result=%{public}d", result); + + if (result != OHOS::ERR_OK) { + resultReceiver_.append(ngMsg); + } else { + resultReceiver_.append(okMsg); + resultReceiver_.append(DumpAbilityInfos(abilityInfos) + "\n"); + } + + APP_LOGI("## BUNDLE_TEST_TOOL ## <<<< BatchQueryAbilityInfo End!!!"); + return result; +} +} // AppExecFwk +} // OHOS \ No newline at end of file -- Gitee