From f5db514421ab5ed70aef95934d11ee20990eed4d Mon Sep 17 00:00:00 2001 From: xsz233 Date: Tue, 25 Apr 2023 16:58:03 +0800 Subject: [PATCH 1/3] test_tool Signed-off-by: xsz233 --- frameworks/include/bundle_test_tool.h | 2 ++ frameworks/src/bundle_test_tool.cpp | 37 +++++++++++++++++++++++++++ 2 files changed, 39 insertions(+) diff --git a/frameworks/include/bundle_test_tool.h b/frameworks/include/bundle_test_tool.h index b19bcab..20b3e71 100644 --- a/frameworks/include/bundle_test_tool.h +++ b/frameworks/include/bundle_test_tool.h @@ -77,6 +77,7 @@ private: ErrCode RunAsGetAppProvisionInfo(); ErrCode RunAsGetDistributedBundleName(); ErrCode HandleBundleEventCallback(); + ErrCode RunAsGetBundleInfo(); std::condition_variable cv_; std::mutex mutex_; @@ -136,6 +137,7 @@ private: void Sleep(int32_t seconds); ErrCode CallRegisterBundleEventCallback(sptr bundleEventCallback); ErrCode CallUnRegisterBundleEventCallback(sptr bundleEventCallback); + bool GetBundleInfo(const std::string &bundleName, int32_t flag, std::string &msg); }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index 48a9e1e..754607a 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -2768,6 +2768,43 @@ ErrCode BundleTestTool::RunAsGetAppProvisionInfo() return result; } +ErrCode BundleTestTool::RunAsGetBundleInfo() +{ + std::string bundleName; + int32_t flag; + int32_t result = BundleNameAndUserIdCommonFunc(bundleName, flag); + if (result != OHOS::ERR_OK){ + resultReceiver_.append("wrong"); + } else { + std::string msg; + bool ret = GetBundleInfo(bundleName, flag, msg); + if (!ret) { + resultReceiver_ = "wrong2"; + } else { + resultReceiver_ = msg; + } + } +} + +bool BundleTestTool::GetBundleInfo(const std::string &bundleName, int32_t flag, std::string &msg) +{ + if (bundleMgrProxy_ == nullptr) { + APP_LOGE("bundleMgrProxy_ is nullptr"); + return OHOS::ERR_INVALID_VALUE; + } + BundleInfo bundleInfo; + bool ret = bundleMgrProxy_->GetBundleInfo(bundleName, flag, bundleInfo, 100); + if (!ret) { + msg = "wrong3"; + } else { + nlohmann::json jsonObject = bundleInfo; + jsonObject["applicationInfo"] = bundleInfo.applicationInfo; + dumpResults= jsonObject.dump(Constants::DUMP_INDENT); + } + return ret; +} + + ErrCode BundleTestTool::GetAppProvisionInfo(const std::string &bundleName, int32_t userId, std::string& msg) { -- Gitee From dcc14c31e1656af0999147fd2a7068cdcbf06f7c Mon Sep 17 00:00:00 2001 From: xushizhe Date: Fri, 5 May 2023 09:21:26 +0800 Subject: [PATCH 2/3] add proxyData test tool Signed-off-by: xushizhe --- frameworks/include/bundle_test_tool.h | 2 + frameworks/src/bundle_test_tool.cpp | 105 ++++++++++++++++++++++++++ 2 files changed, 107 insertions(+) diff --git a/frameworks/include/bundle_test_tool.h b/frameworks/include/bundle_test_tool.h index b19bcab..a091c81 100644 --- a/frameworks/include/bundle_test_tool.h +++ b/frameworks/include/bundle_test_tool.h @@ -77,6 +77,7 @@ private: ErrCode RunAsGetAppProvisionInfo(); ErrCode RunAsGetDistributedBundleName(); ErrCode HandleBundleEventCallback(); + ErrCode RunAsGetProxyDataCommand(); std::condition_variable cv_; std::mutex mutex_; @@ -112,6 +113,7 @@ private: ErrCode CheckGetAppRunningRuleResultCorrectOption(int option, const std::string &commandName, std::string &bundleName, int32_t &userId, int &euid); bool CheckSandboxCorrectOption(int option, const std::string &commandName, int &data, std::string &bundleName); + bool CheckGetProxyDataCorrectOption(int option, const std::string &commandName, int &temp, std::string &name); ErrCode InstallSandboxOperation( const std::string &bundleName, const int32_t userId, const int32_t dlpType, int32_t &appIndex) const; ErrCode UninstallSandboxOperation( diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index 48a9e1e..ce43b75 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -312,6 +312,15 @@ const std::string HELP_MSG_GET_APP_RUNNING_RESULT_RULE = " -n, --bundle-name specify bundle name of the application\n" " -u, --user-id specify a user id\n"; +const std::string HELP_MSG_PROXY_DATA_STRING = + "usage: bundle_test_tool getStr \n" + "eg:bundle_test_tool getProxyDataInfos -m -n -u \n" + "options list:\n" + " -h, --help list available commands\n" + " -n, --bundle-name specify bundle name of the application\n" + " -m, --module-name specify module name of the application\n" + " -u, --user-id specify a user id\n"; + const std::string HELP_MSG_NO_ADD_INSTALL_RULE_OPTION = "error: you must specify a app id with '-a' or '--app-id' \n" "and a control type with '-t' or '--control-rule-type' \n" @@ -564,6 +573,15 @@ const struct option LONG_OPTIONS_BUNDLE_EVENT_CALLBACK[] = { {"uid", required_argument, nullptr, 'u'}, {nullptr, 0, nullptr, 0}, }; + +const std::string SHORT_OPTIONS_PROXY_DATA = "hn:m:u:"; +const struct option LONG_OPTIONS_PROXY_DATA[] = { + {"help", no_argument, nullptr, 'h'}, + {"bundle-name", required_argument, nullptr, 'n'}, + {"module-name", required_argument, nullptr, 'm'}, + {"user-id", required_argument, nullptr, 'u'}, + {nullptr, 0, nullptr, 0}, +}; } // namespace BundleEventCallbackImpl::BundleEventCallbackImpl() @@ -620,6 +638,7 @@ ErrCode BundleTestTool::CreateCommandMap() {"getAppProvisionInfo", std::bind(&BundleTestTool::RunAsGetAppProvisionInfo, this)}, {"getDistributedBundleName", std::bind(&BundleTestTool::RunAsGetDistributedBundleName, this)}, {"eventCB", std::bind(&BundleTestTool::HandleBundleEventCallback, this)}, + {"getProxyData", std::bind(&BundleTestTool::RunAsGetProxyDataInfos, this)}, }; return OHOS::ERR_OK; @@ -1356,6 +1375,92 @@ bool BundleTestTool::CheckGetStringCorrectOption( return ret; } +bool BundleTestTool::CheckGetProxyDataCorrectOption( + int option, const std::string &commandName, int &temp, std::string &name) +{ + 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 'n': { + name = optarg; + APP_LOGD("bundle_test_tool %{public}s -n %{public}s", commandName.c_str(), argv_[optind - 1]); + break; + } + case 'm': { + name = optarg; + APP_LOGD("bundle_test_tool %{public}s -m module-name:%{public}s, %{public}s", + commandName.c_str(), name.c_str(), argv_[optind - 1]); + break; + } + case 'u': { + StringToInt(optarg, commandName, temp, 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; +} + +ErrCode BundleTestTool::RunAsGetProxyDataCommand() +{ + int result = OHOS::ERR_OK; + int counter = 0; + std::string commandName = "getProxyData"; + std::string name = ""; + std::string bundleName = ""; + std::string moduleName = ""; + int userId = 100; + APP_LOGD("RunAsGetProxyDataCommand is start"); + while (true) { + counter++; + int32_t option = getopt_long( + argc_, argv_, SHORT_OPTIONS_PROXY_DATA.c_str(), LONG_OPTIONS_PROXY_DATA, 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)) { + APP_LOGD("bundle_test_tool getStr with no option."); + resultReceiver_.append(HELP_MSG_NO_GETSTRING_OPTION); + return OHOS::ERR_INVALID_VALUE; + } + break; + } + int temp = 0; + result = !CheckGetProxyDataCorrectOption(option, commandName, temp, name) + ? OHOS::ERR_INVALID_VALUE : result; + moduleName = option == 'm' ? name : moduleName; + bundleName = option == 'n' ? name : bundleName; + userId = option == 'u' ? temp : userId; + } + + if (result != OHOS::ERR_OK) { + resultReceiver_.append(HELP_MSG_GET_STRING); + } else { + std::string results = ""; + + results = bundleMgrProxy_->GetProxyDataInfos(bundleName, moduleName, userId); + if (results.empty()) { + resultReceiver_.append(STRING_GET_STRING_NG); + return result; + } + resultReceiver_.append(results); + } + return result; +} + ErrCode BundleTestTool::RunAsGetStringCommand() { int result = OHOS::ERR_OK; -- Gitee From 062220035a3b23c936f8631323d07a3ad74ced33 Mon Sep 17 00:00:00 2001 From: xsz233 Date: Fri, 5 May 2023 11:37:04 +0000 Subject: [PATCH 3/3] add proxy data testtool Signed-off-by: xsz233 --- frameworks/include/bundle_test_tool.h | 3 +- frameworks/src/bundle_command_common.cpp | 2 +- frameworks/src/bundle_test_tool.cpp | 158 +++++++++++++++-------- 3 files changed, 106 insertions(+), 57 deletions(-) diff --git a/frameworks/include/bundle_test_tool.h b/frameworks/include/bundle_test_tool.h index 5832a1b..8191eb5 100644 --- a/frameworks/include/bundle_test_tool.h +++ b/frameworks/include/bundle_test_tool.h @@ -78,6 +78,7 @@ private: ErrCode RunAsGetDistributedBundleName(); ErrCode HandleBundleEventCallback(); ErrCode RunAsGetProxyDataCommand(); + ErrCode RunAsGetAllProxyDataCommand(); std::condition_variable cv_; std::mutex mutex_; @@ -114,6 +115,7 @@ private: std::string &bundleName, int32_t &userId, int &euid); bool CheckSandboxCorrectOption(int option, const std::string &commandName, int &data, std::string &bundleName); bool CheckGetProxyDataCorrectOption(int option, const std::string &commandName, int &temp, std::string &name); + bool CheckGetAllProxyDataCorrectOption(int option, const std::string &commandName, int &temp, std::string &name); ErrCode InstallSandboxOperation( const std::string &bundleName, const int32_t userId, const int32_t dlpType, int32_t &appIndex) const; ErrCode UninstallSandboxOperation( @@ -138,7 +140,6 @@ private: void Sleep(int32_t seconds); ErrCode CallRegisterBundleEventCallback(sptr bundleEventCallback); ErrCode CallUnRegisterBundleEventCallback(sptr bundleEventCallback); - bool GetBundleInfo(const std::string &bundleName, int32_t flag, std::string &msg); }; } // namespace AppExecFwk } // namespace OHOS diff --git a/frameworks/src/bundle_command_common.cpp b/frameworks/src/bundle_command_common.cpp index fa53ebd..0521d54 100644 --- a/frameworks/src/bundle_command_common.cpp +++ b/frameworks/src/bundle_command_common.cpp @@ -616,7 +616,7 @@ std::map BundleCommandCommon::bundleMessageMap_ = { }, { IStatusReceiver::ERR_INSATLL_CHECK_PROXY_DATA_URI_FAILED, - "error, uri in proxy data uri is wrong", + "error, uri in proxy data is wrong", }, { IStatusReceiver::ERR_INSATLL_CHECK_PROXY_DATA_PERMISSION_FAILED, diff --git a/frameworks/src/bundle_test_tool.cpp b/frameworks/src/bundle_test_tool.cpp index c4ab892..965c910 100644 --- a/frameworks/src/bundle_test_tool.cpp +++ b/frameworks/src/bundle_test_tool.cpp @@ -312,15 +312,6 @@ const std::string HELP_MSG_GET_APP_RUNNING_RESULT_RULE = " -n, --bundle-name specify bundle name of the application\n" " -u, --user-id specify a user id\n"; -const std::string HELP_MSG_PROXY_DATA_STRING = - "usage: bundle_test_tool getStr \n" - "eg:bundle_test_tool getProxyDataInfos -m -n -u \n" - "options list:\n" - " -h, --help list available commands\n" - " -n, --bundle-name specify bundle name of the application\n" - " -m, --module-name specify module name of the application\n" - " -u, --user-id specify a user id\n"; - const std::string HELP_MSG_NO_ADD_INSTALL_RULE_OPTION = "error: you must specify a app id with '-a' or '--app-id' \n" "and a control type with '-t' or '--control-rule-type' \n" @@ -416,6 +407,22 @@ const std::string HELP_MSG_BUNDLE_EVENT_CALLBACK = " -o, --onlyUnregister only call unregister, default will call register then unregister\n" " -u, --uid specify a uid, default is foundation uid\n"; +const std::string HELP_MSG_GET_PROXY_DATA = + "usage: bundle_test_tool getProxyDataInfos \n" + "eg:bundle_test_tool getProxyDataInfos -m -n -u \n" + "options list:\n" + " -h, --help list available commands\n" + " -n, --bundle-name specify bundle name of the application\n" + " -m, --module-name specify module name of the application\n" + " -u, --user-id specify a user id\n"; + +const std::string HELP_MSG_GET_ALL_PROXY_DATA = + "usage: bundle_test_tool getAllProxyDataInfos \n" + "eg:bundle_test_tool getProxyDataInfos -u \n" + "options list:\n" + " -h, --help list available commands\n" + " -u, --user-id specify a user id\n"; + const std::string HELP_MSG_NO_BUNDLE_NAME_OPTION = "error: you must specify a bundle name with '-n' or '--bundle-name' \n"; @@ -584,6 +591,13 @@ const struct option LONG_OPTIONS_PROXY_DATA[] = { {"user-id", required_argument, nullptr, 'u'}, {nullptr, 0, nullptr, 0}, }; + +const std::string SHORT_OPTIONS_ALL_PROXY_DATA = "hu:"; +const struct option LONG_OPTIONS_ALL_PROXY_DATA[] = { + {"help", no_argument, nullptr, 'h'}, + {"user-id", required_argument, nullptr, 'u'}, + {nullptr, 0, nullptr, 0}, +}; } // namespace BundleEventCallbackImpl::BundleEventCallbackImpl() @@ -640,7 +654,8 @@ ErrCode BundleTestTool::CreateCommandMap() {"getAppProvisionInfo", std::bind(&BundleTestTool::RunAsGetAppProvisionInfo, this)}, {"getDistributedBundleName", std::bind(&BundleTestTool::RunAsGetDistributedBundleName, this)}, {"eventCB", std::bind(&BundleTestTool::HandleBundleEventCallback, this)}, - {"getProxyData", std::bind(&BundleTestTool::RunAsGetProxyDataCommand, this)}, + {"getProxyDataInfos", std::bind(&BundleTestTool::RunAsGetProxyDataCommand, this)}, + {"getAllProxyDataInfos", std::bind(&BundleTestTool::RunAsGetAllProxyDataCommand, this)}, }; return OHOS::ERR_OK; @@ -1414,6 +1429,32 @@ bool BundleTestTool::CheckGetProxyDataCorrectOption( return ret; } +bool BundleTestTool::CheckGetAllProxyDataCorrectOption( + int option, const std::string &commandName, int &temp, std::string &name) +{ + 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 'u': { + StringToInt(optarg, commandName, temp, 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; +} + ErrCode BundleTestTool::RunAsGetProxyDataCommand() { int result = OHOS::ERR_OK; @@ -1422,20 +1463,20 @@ ErrCode BundleTestTool::RunAsGetProxyDataCommand() std::string name = ""; std::string bundleName = ""; std::string moduleName = ""; - int userId = 100; + int userId = Constants::ALL_USERID; APP_LOGD("RunAsGetProxyDataCommand is start"); while (true) { counter++; int32_t option = getopt_long( - argc_, argv_, SHORT_OPTIONS_PROXY_DATA.c_str(), LONG_OPTIONS_PROXY_DATA, nullptr); + argc_, argv_, SHORT_OPTIONS_PROXY_DATA.c_str(), LONG_OPTIONS_PROXY_DATA, 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)) { - APP_LOGD("bundle_test_tool getStr with no option."); - resultReceiver_.append(HELP_MSG_NO_GETSTRING_OPTION); + APP_LOGD("bundle_test_tool getProxyData with no option."); + resultReceiver_.append(HELP_MSG_GET_PROXY_DATA); return OHOS::ERR_INVALID_VALUE; } break; @@ -1449,16 +1490,60 @@ ErrCode BundleTestTool::RunAsGetProxyDataCommand() } if (result != OHOS::ERR_OK) { - resultReceiver_.append(HELP_MSG_GET_STRING); + resultReceiver_.append(HELP_MSG_GET_PROXY_DATA); } else { std::vector proxyDatas; - result = bundleMgrProxy_->GetProxyDataInfos(bundleName, moduleName, userId, proxyDatas); + result = bundleMgrProxy_->GetProxyDataInfos(bundleName, moduleName, proxyDatas, userId); if (result == ERR_OK) { nlohmann::json jsonObject = proxyDatas; - results= jsonObject.dump(Constants::DUMP_INDENT); + std::string results = jsonObject.dump(Constants::DUMP_INDENT); resultReceiver_.append(results); } else { - resultReceiver_.append(STRING_GET_PROXY_DATA_NG + " errCode is "+ std::to_string(ret) + "\n"); + resultReceiver_.append(STRING_GET_PROXY_DATA_NG + " errCode is "+ std::to_string(result) + "\n"); + } + } + return result; +} + +ErrCode BundleTestTool::RunAsGetAllProxyDataCommand() +{ + int result = OHOS::ERR_OK; + int counter = 0; + std::string commandName = "getProxyData"; + std::string name = ""; + std::string bundleName = ""; + std::string moduleName = ""; + int userId = Constants::ALL_USERID; + APP_LOGD("RunAsGetAllProxyDataCommand is start"); + while (true) { + counter++; + int32_t option = getopt_long( + argc_, argv_, SHORT_OPTIONS_ALL_PROXY_DATA.c_str(), LONG_OPTIONS_ALL_PROXY_DATA, 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) { + break; + } + + int temp = 0; + result = !CheckGetAllProxyDataCorrectOption(option, commandName, temp, name) + ? OHOS::ERR_INVALID_VALUE : result; + userId = option == 'u' ? temp : userId; + } + + if (result != OHOS::ERR_OK) { + resultReceiver_.append(HELP_MSG_GET_ALL_PROXY_DATA); + } else { + std::vector proxyDatas; + result = bundleMgrProxy_->GetAllProxyDataInfos(proxyDatas, userId); + if (result == ERR_OK) { + nlohmann::json jsonObject = proxyDatas; + std::string results = jsonObject.dump(Constants::DUMP_INDENT); + resultReceiver_.append(results); + } else { + resultReceiver_.append(STRING_GET_PROXY_DATA_NG + " errCode is "+ std::to_string(result) + "\n"); } } return result; @@ -2876,43 +2961,6 @@ ErrCode BundleTestTool::RunAsGetAppProvisionInfo() return result; } -ErrCode BundleTestTool::RunAsGetBundleInfo() -{ - std::string bundleName; - int32_t flag; - int32_t result = BundleNameAndUserIdCommonFunc(bundleName, flag); - if (result != OHOS::ERR_OK){ - resultReceiver_.append("wrong"); - } else { - std::string msg; - bool ret = GetBundleInfo(bundleName, flag, msg); - if (!ret) { - resultReceiver_ = "wrong2"; - } else { - resultReceiver_ = msg; - } - } -} - -bool BundleTestTool::GetBundleInfo(const std::string &bundleName, int32_t flag, std::string &msg) -{ - if (bundleMgrProxy_ == nullptr) { - APP_LOGE("bundleMgrProxy_ is nullptr"); - return OHOS::ERR_INVALID_VALUE; - } - BundleInfo bundleInfo; - bool ret = bundleMgrProxy_->GetBundleInfo(bundleName, flag, bundleInfo, 100); - if (!ret) { - msg = "wrong3"; - } else { - nlohmann::json jsonObject = bundleInfo; - jsonObject["applicationInfo"] = bundleInfo.applicationInfo; - dumpResults= jsonObject.dump(Constants::DUMP_INDENT); - } - return ret; -} - - ErrCode BundleTestTool::GetAppProvisionInfo(const std::string &bundleName, int32_t userId, std::string& msg) { -- Gitee