From 5caaf8aa6e14c67529364b26fdbc8cdeb22154ec Mon Sep 17 00:00:00 2001 From: zhaogan Date: Thu, 22 May 2025 14:13:30 +0800 Subject: [PATCH] bm support dump label Signed-off-by: zhaogan --- frameworks/include/bundle_command.h | 2 + frameworks/src/bundle_command.cpp | 41 ++++++++++++- .../bm/bm_command_dump_system_test.cpp | 60 +++++++++++++++++++ 3 files changed, 100 insertions(+), 3 deletions(-) diff --git a/frameworks/include/bundle_command.h b/frameworks/include/bundle_command.h index bd418e6..a4a85f9 100644 --- a/frameworks/include/bundle_command.h +++ b/frameworks/include/bundle_command.h @@ -292,6 +292,8 @@ private: std::string DumpSharedDependencies(const std::string &bundleName, const std::string &moduleName) const; std::string DumpShared(const std::string &bundleName) const; std::string DumpSharedAll() const; + std::string DumpAllLabel(int32_t userId) const; + std::string DumpBundleLabel(const std::string &bundleName, int32_t userId) const; int32_t InstallOperation(const std::vector &bundlePaths, InstallParam &installParam, int32_t waittingTime, std::string &resultMsg) const; diff --git a/frameworks/src/bundle_command.cpp b/frameworks/src/bundle_command.cpp index 2fa734e..609b7fe 100644 --- a/frameworks/src/bundle_command.cpp +++ b/frameworks/src/bundle_command.cpp @@ -125,7 +125,7 @@ const struct option UNINSTALL_LONG_OPTIONS[] = { {nullptr, 0, nullptr, 0}, }; -const std::string SHORT_OPTIONS_DUMP = "hn:aisu:d:g"; +const std::string SHORT_OPTIONS_DUMP = "hn:aisu:d:gl"; const struct option LONG_OPTIONS_DUMP[] = { {"help", no_argument, nullptr, 'h'}, {"bundle-name", required_argument, nullptr, 'n'}, @@ -135,6 +135,7 @@ const struct option LONG_OPTIONS_DUMP[] = { {"user-id", required_argument, nullptr, 'u'}, {"device-id", required_argument, nullptr, 'd'}, {"debug-bundle", no_argument, nullptr, 'g'}, + {"label", no_argument, nullptr, 'l'}, {nullptr, 0, nullptr, 0}, }; @@ -1016,6 +1017,7 @@ ErrCode BundleManagerShellCommand::RunAsDumpCommand() bool bundleDumpInfo = false; bool bundleDumpShortcut = false; bool bundleDumpDistributedBundleInfo = false; + bool bundleDumpLabel = false; std::string deviceId = ""; const int32_t currentUser = BundleCommandCommon::GetCurrentUserId(Constants::UNSPECIFIED_USERID); int32_t userId = currentUser; @@ -1094,6 +1096,13 @@ ErrCode BundleManagerShellCommand::RunAsDumpCommand() bundleDumpAll = true; break; } + case 'l': { + // 'bm dump -l' + // 'bm dump --label' + APP_LOGD("'bm dump %{public}s'", argv_[optind - 1]); + bundleDumpLabel = true; + break; + } case 'g': { // 'bm dump -g' // 'bm dump --debug-bundle' @@ -1168,12 +1177,16 @@ ErrCode BundleManagerShellCommand::RunAsDumpCommand() dumpResults = DumpShortcutInfos(bundleName, userId); } else if (bundleDumpDistributedBundleInfo) { dumpResults = DumpDistributedBundleInfo(deviceId, bundleName); - } else if (bundleDumpAll) { + } else if (bundleDumpAll && !bundleDumpLabel) { dumpResults = DumpBundleList(userId); } else if (bundleDumpDebug) { dumpResults = DumpDebugBundleList(userId); - } else if (bundleDumpInfo) { + } else if (bundleDumpInfo && !bundleDumpLabel) { dumpResults = DumpBundleInfo(bundleName, userId); + } else if (bundleDumpAll && bundleDumpLabel) { + dumpResults = DumpAllLabel(userId); + } else if (bundleDumpInfo && bundleDumpLabel) { + dumpResults = DumpBundleLabel(bundleName, userId); } if (dumpResults.empty() || (dumpResults == "")) { dumpResults = HELP_MSG_DUMP_FAILED + "\n"; @@ -2179,6 +2192,28 @@ std::string BundleManagerShellCommand::DumpBundleList(int32_t userId) const return dumpResults; } +std::string BundleManagerShellCommand::DumpBundleLabel(const std::string &bundleName, int32_t userId) const +{ + std::string dumpResults; + bool dumpRet = bundleMgrProxy_->DumpInfos( + DumpFlag::DUMP_BUNDLE_LABEL, bundleName, userId, dumpResults); + if (!dumpRet) { + APP_LOGE("failed to dump bundle label."); + } + return dumpResults; +} + +std::string BundleManagerShellCommand::DumpAllLabel(int32_t userId) const +{ + std::string dumpResults; + bool dumpRet = bundleMgrProxy_->DumpInfos( + DumpFlag::DUMP_LABEL_LIST, BUNDLE_NAME_EMPTY, userId, dumpResults); + if (!dumpRet) { + APP_LOGE("failed to dump bundle label list."); + } + return dumpResults; +} + std::string BundleManagerShellCommand::DumpDebugBundleList(int32_t userId) const { std::string dumpResults; diff --git a/test/systemtest/bm/bm_command_dump_system_test.cpp b/test/systemtest/bm/bm_command_dump_system_test.cpp index 147b2da..f54f5fa 100644 --- a/test/systemtest/bm/bm_command_dump_system_test.cpp +++ b/test/systemtest/bm/bm_command_dump_system_test.cpp @@ -113,4 +113,64 @@ HWTEST_F(BmCommandDumpSystemTest, Bm_Command_Dump_SystemTest_0300, Function | Me EXPECT_EQ(commandResult, GET_FALSE + "\n"); } + +/** + * @tc.number: Bm_Command_Dump_SystemTest_0400 + * @tc.name: ExecCommand + * @tc.desc: Verify the "bm dump -n -l" command. + */ +HWTEST_F(BmCommandDumpSystemTest, Bm_Command_Dump_SystemTest_0400, Function | MediumTest | TestSize.Level1) +{ + // dump an invalid bundle + std::string command = "bm dump -l -n " + STRING_BUNDLE_NAME_INVALID; + std::string commandResult = ToolSystemTest::ExecuteCommand(command); + + EXPECT_EQ(commandResult, GET_FALSE + "\n"); +} + +/** + * @tc.number: Bm_Command_Dump_SystemTest_0500 + * @tc.name: ExecCommand + * @tc.desc: Verify the "bm dump -a -l" command. + */ +HWTEST_F(BmCommandDumpSystemTest, Bm_Command_Dump_SystemTest_0500, Function | MediumTest | TestSize.Level0) +{ + // uninstall the bundle + ToolSystemTest::UninstallBundle(STRING_BUNDLE_NAME); + + // install a valid bundle + ToolSystemTest::InstallBundle(STRING_BUNDLE_PATH, true); + + // dump all bundle + std::string command = "bm dump -a -l"; + std::string commandResult = ToolSystemTest::ExecuteCommand(command); + + EXPECT_NE(commandResult, ""); + + // uninstall the bundle + ToolSystemTest::UninstallBundle(STRING_BUNDLE_NAME); +} + +/** + * @tc.number: Bm_Command_Dump_SystemTest_0600 + * @tc.name: ExecCommand + * @tc.desc: Verify the "bm dump -n " command. + */ +HWTEST_F(BmCommandDumpSystemTest, Bm_Command_Dump_SystemTest_0600, Function | MediumTest | TestSize.Level0) +{ + // uninstall the bundle + ToolSystemTest::UninstallBundle(STRING_BUNDLE_NAME); + + // install a valid bundle + ToolSystemTest::InstallBundle(STRING_BUNDLE_PATH, true); + + // dump a valid bundle + std::string command = "bm dump -l -n " + STRING_BUNDLE_NAME; + std::string commandResult = ToolSystemTest::ExecuteCommand(command); + + EXPECT_NE(commandResult, ""); + + // uninstall the bundle + ToolSystemTest::UninstallBundle(STRING_BUNDLE_NAME); +} } // OHOS -- Gitee