From bc94475bbe0af3d92ed3e20a2b240f5b31f50add Mon Sep 17 00:00:00 2001 From: daiyujia Date: Sat, 3 May 2025 16:30:32 +0800 Subject: [PATCH] =?UTF-8?q?IssueNo:#IC5E54=20Description:OTA=E6=97=B6?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E4=B8=80=E6=AC=A1=E6=9D=83=E9=99=90=20Sig:bu?= =?UTF-8?q?ndleManager=20Feature=20or=20Bugfix:Feature=20Binary=20Source:N?= =?UTF-8?q?o?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: daiyujia Change-Id: I15e9e6d16136cc89c2f432b6c5beb8a83229e713 --- .../bundle_mgr_service_event_handler.h | 4 + .../include/bundle_service_constants.h | 4 + .../src/bundle_mgr_service_event_handler.cpp | 74 +++++++++++++++ .../bms_event_handler_test.cpp | 92 +++++++++++++++++++ 4 files changed, 174 insertions(+) diff --git a/services/bundlemgr/include/bundle_mgr_service_event_handler.h b/services/bundlemgr/include/bundle_mgr_service_event_handler.h index 8482fbd7ef..ed761cc6c2 100644 --- a/services/bundlemgr/include/bundle_mgr_service_event_handler.h +++ b/services/bundlemgr/include/bundle_mgr_service_event_handler.h @@ -682,6 +682,10 @@ private: std::unordered_map &infos); void ConvertToOnDemandInstallBundleInfo(const std::unordered_map &infos, PreInstallBundleInfo &preInstallBundleInfo); + void ProcessUpdatePermissions( + const std::unordered_map> &needInstallMap); + bool IsPermissionsUpdated(); + void SaveUpdatePermissionsFlag(); // Used to mark Whether trigger OTA check bool needRebootOta_ = false; // Used to notify bundle scan status diff --git a/services/bundlemgr/include/bundle_service_constants.h b/services/bundlemgr/include/bundle_service_constants.h index 55dc328ca9..5ded730b6a 100644 --- a/services/bundlemgr/include/bundle_service_constants.h +++ b/services/bundlemgr/include/bundle_service_constants.h @@ -233,6 +233,10 @@ constexpr const char* BMS_SYSTEM_TIME_FOR_SHORTCUT = "SystemTimeForShortcut"; constexpr const char* PRODUCT_SUFFIX = "/etc/app"; constexpr const char* DEFAULT_DATA_PRE_BUNDLE_DIR = "/app_list.json"; constexpr const char* DATA_PRELOAD_APP = "/data/preload/app/"; + +//ota update permissions +constexpr const char* UPDATE_PERMISSIONS_FLAG = "updatePermissionsFlag"; +constexpr const char* UPDATE_PERMISSIONS_FLAG_UPDATED = "updated"; } // namespace ServiceConstants } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/src/bundle_mgr_service_event_handler.cpp b/services/bundlemgr/src/bundle_mgr_service_event_handler.cpp index c99254fefd..4f05d18db7 100644 --- a/services/bundlemgr/src/bundle_mgr_service_event_handler.cpp +++ b/services/bundlemgr/src/bundle_mgr_service_event_handler.cpp @@ -2138,6 +2138,7 @@ void BMSEventHandler::InnerProcessRebootBundleInstall( LOG_E(BMS_TAG_DEFAULT, "multi install failed"); } UpdatePreinstallDB(needInstallMap); + ProcessUpdatePermissions(needInstallMap); } bool BMSEventHandler::CheckIsBundleUpdatedByHapPath(const BundleInfo &bundleInfo) @@ -4746,5 +4747,78 @@ void BMSEventHandler::ConvertToOnDemandInstallBundleInfo(const std::unordered_ma } } } + +void BMSEventHandler::ProcessUpdatePermissions( + const std::unordered_map> &needInstallMap) +{ + auto dataMgr = DelayedSingleton::GetInstance()->GetDataMgr(); + if (dataMgr == nullptr) { + LOG_E(BMS_TAG_DEFAULT, "DataMgr is nullptr"); + return; + } + if (IsPermissionsUpdated()) { + return; + } + bool updatePermissionsFlag = true; + std::map infos = dataMgr->GetAllInnerBundleInfos(); + for (auto &item : infos) { + std::string bundleName = item.first; + if (needInstallMap.find(bundleName) != needInstallMap.end()) { + continue; + } + auto &innerBundleInfo = item.second; + auto &userInfos = innerBundleInfo.GetInnerBundleUserInfos(); + AppProvisionInfo appProvisionInfo; + if (dataMgr->GetAppProvisionInfo(bundleName, userInfos.begin()->second.bundleUserInfo.userId, + appProvisionInfo) != ERR_OK) { + LOG_W(BMS_TAG_DEFAULT, "GetAppProvisionInfo failed -n:%{public}s", bundleName.c_str()); + } + for (auto &uerInfo : userInfos) { + if (uerInfo.second.accessTokenId == 0) { + continue; + } + int32_t userId = uerInfo.second.bundleUserInfo.userId; + Security::AccessToken::AccessTokenIDEx accessTokenIdEx; + accessTokenIdEx.tokenIDEx = uerInfo.second.accessTokenIdEx; + Security::AccessToken::HapInfoCheckResult checkResult; + if (BundlePermissionMgr::UpdateHapToken(accessTokenIdEx, innerBundleInfo, userId, checkResult, + appProvisionInfo.appServiceCapabilities) != ERR_OK) { + LOG_W(BMS_TAG_DEFAULT, "UpdateHapToken failed %{public}s", bundleName.c_str()); + updatePermissionsFlag = false; + } + } + } + if (updatePermissionsFlag) { + SaveUpdatePermissionsFlag(); + } +} + +bool BMSEventHandler::IsPermissionsUpdated() +{ + auto bmsParam = DelayedSingleton::GetInstance()->GetBmsParam(); + if (bmsParam == nullptr) { + LOG_W(BMS_TAG_DEFAULT, "bmsParam is nullptr"); + return false; + } + std::string value; + if (bmsParam->GetBmsParam(ServiceConstants::UPDATE_PERMISSIONS_FLAG, value)) { + LOG_I(BMS_TAG_DEFAULT, "already update permissions"); + return true; + } + return false; +} + +void BMSEventHandler::SaveUpdatePermissionsFlag() +{ + auto bmsPara = DelayedSingleton::GetInstance()->GetBmsParam(); + if (bmsPara == nullptr) { + LOG_E(BMS_TAG_DEFAULT, "bmsPara is nullptr"); + return; + } + if (!bmsPara->SaveBmsParam(ServiceConstants::UPDATE_PERMISSIONS_FLAG, + std::string{ ServiceConstants::UPDATE_PERMISSIONS_FLAG_UPDATED })) { + LOG_E(BMS_TAG_DEFAULT, "save updatePermissionsFlag failed"); + } +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/bundlemgr/test/unittest/bms_event_handler_test/bms_event_handler_test.cpp b/services/bundlemgr/test/unittest/bms_event_handler_test/bms_event_handler_test.cpp index 61806b4030..4be6cec517 100644 --- a/services/bundlemgr/test/unittest/bms_event_handler_test/bms_event_handler_test.cpp +++ b/services/bundlemgr/test/unittest/bms_event_handler_test/bms_event_handler_test.cpp @@ -15,8 +15,10 @@ #include #include +#include #include "app_log_wrapper.h" +#include "bundle_service_constants.h" #define private public #include "bundle_mgr_service.h" #include "bundle_mgr_service_event_handler.h" @@ -2416,4 +2418,94 @@ HWTEST_F(BmsEventHandlerTest, InnerBundleInfo_0200, Function | SmallTest | Level innerBundleInfo.UpdateRemovable(true, true); EXPECT_TRUE(innerBundleInfo.IsRemovable()); } + +/** + * @tc.number: ProcessUpdatePermissions_0100 + * @tc.name: ProcessUpdatePermissions + * @tc.desc: test ProcessUpdatePermissions + */ +HWTEST_F(BmsEventHandlerTest, ProcessUpdatePermissions_0100, Function | SmallTest | Level0) + { + std::shared_ptr handler = std::make_shared(); + EXPECT_NE(handler, nullptr); + if (handler) { + DelayedSingleton::GetInstance()->bmsParam_ = nullptr; + bool ret = handler->IsPermissionsUpdated(); + EXPECT_FALSE(ret); + auto bmsParam = std::make_shared(); + EXPECT_NE(bmsParam, nullptr); + if (bmsParam) { + ret = handler->IsPermissionsUpdated(); + EXPECT_FALSE(ret); + std::string value; + ret = bmsParam->SaveBmsParam(ServiceConstants::UPDATE_PERMISSIONS_FLAG, + ServiceConstants::UPDATE_PERMISSIONS_FLAG_UPDATED); + EXPECT_FALSE(ret); + DelayedSingleton::GetInstance()->bmsParam_ = std::make_shared(); + ret = handler->IsPermissionsUpdated(); + EXPECT_TRUE(ret); + ret = bmsParam->DeleteBmsParam(ServiceConstants::UPDATE_PERMISSIONS_FLAG); + EXPECT_TRUE(ret); + } + } +} + +/** + * @tc.number: IsPermissionsUpdated_0100 + * @tc.name: IsPermissionsUpdated + * @tc.desc: test IsPermissionsUpdated + */ +HWTEST_F(BmsEventHandlerTest, IsPermissionsUpdated_0100, Function | SmallTest | Level0) +{ + std::shared_ptr handler = std::make_shared(); + EXPECT_NE(handler, nullptr); + if (handler) { + DelayedSingleton::GetInstance()->bmsParam_ = nullptr; + bool ret = handler->IsPermissionsUpdated(); + EXPECT_FALSE(ret); + auto bmsParam = std::make_shared(); + EXPECT_NE(bmsParam, nullptr); + if (bmsParam) { + ret = handler->IsPermissionsUpdated(); + EXPECT_FALSE(ret); + std::string value; + ret = bmsParam->SaveBmsParam(ServiceConstants::UPDATE_PERMISSIONS_FLAG, + ServiceConstants::UPDATE_PERMISSIONS_FLAG_UPDATED); + EXPECT_FALSE(ret); + DelayedSingleton::GetInstance()->bmsParam_ = std::make_shared(); + ret = handler->IsPermissionsUpdated(); + EXPECT_TRUE(ret); + ret = bmsParam->DeleteBmsParam(ServiceConstants::UPDATE_PERMISSIONS_FLAG); + EXPECT_TRUE(ret); + } + } +} + +/** + * @tc.number: SaveUpdatePermissionsFlag_0100 + * @tc.name: SaveUpdatePermissionsFlag + * @tc.desc: test SaveUpdatePermissionsFlag + */ +HWTEST_F(BmsEventHandlerTest, SaveUpdatePermissionsFlag_0100, Function | SmallTest | Level0) +{ + std::shared_ptr handler = std::make_shared(); + EXPECT_NE(handler, nullptr); + if (handler) { + DelayedSingleton::GetInstance()->bmsParam_ = nullptr; + handler->SaveUpdatePermissionsFlag(); + auto bmsParam = std::make_shared(); + EXPECT_NE(bmsParam, nullptr); + if (bmsParam) { + std::string value; + bool ret = bmsParam->GetBmsParam(ServiceConstants::UPDATE_PERMISSIONS_FLAG, value); + EXPECT_FALSE(ret); + DelayedSingleton::GetInstance()->bmsParam_ = std::make_shared(); + handler->SaveUpdatePermissionsFlag(); + ret = bmsParam->GetBmsParam(ServiceConstants::UPDATE_PERMISSIONS_FLAG, value); + EXPECT_TRUE(ret); + ret = bmsParam->DeleteBmsParam(ServiceConstants::UPDATE_PERMISSIONS_FLAG); + EXPECT_TRUE(ret); + } + } +} } // OHOS -- Gitee