From 6d6ad5ce2619cc0341a4d732944c609e6a52ef3a Mon Sep 17 00:00:00 2001 From: lanhaoyu Date: Sat, 10 May 2025 12:12:12 +0800 Subject: [PATCH] add clone TDD Signed-off-by: lanhaoyu --- .../include/bundle_backup/bundle_backup_mgr.h | 6 +- .../bundle_backup/bundle_backup_service.h | 6 +- .../src/bundle_backup/bundle_backup_mgr.cpp | 1 + services/bundlemgr/src/bundle_mgr_service.cpp | 6 +- .../bms_data_mgr_test/bms_data_mgr_test.cpp | 70 +++++++++++++++++++ 5 files changed, 80 insertions(+), 9 deletions(-) diff --git a/services/bundlemgr/include/bundle_backup/bundle_backup_mgr.h b/services/bundlemgr/include/bundle_backup/bundle_backup_mgr.h index 541ad428d0..81d06e80dc 100644 --- a/services/bundlemgr/include/bundle_backup/bundle_backup_mgr.h +++ b/services/bundlemgr/include/bundle_backup/bundle_backup_mgr.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHORTCUT_CLONE_MGR_H -#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHORTCUT_CLONE_MGR_H +#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_BACKUP_MGR_H +#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_BACKUP_MGR_H #include "iremote_stub.h" #include "nlohmann/json.hpp" @@ -40,4 +40,4 @@ private: }; } // namespace AppExecFwk } // namespace OHOS -#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHORTCUT_CLONE_MGR_H \ No newline at end of file +#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_BACKUP_MGR_H \ No newline at end of file diff --git a/services/bundlemgr/include/bundle_backup/bundle_backup_service.h b/services/bundlemgr/include/bundle_backup/bundle_backup_service.h index 7b8d7f5be8..d9e03dd9ad 100644 --- a/services/bundlemgr/include/bundle_backup/bundle_backup_service.h +++ b/services/bundlemgr/include/bundle_backup/bundle_backup_service.h @@ -13,8 +13,8 @@ * limitations under the License. */ -#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHORTCUT_CLONE_SERVICE_H -#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHORTCUT_CLONE_SERVICE_H +#ifndef FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_BACKUP_SERVICE_H +#define FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_BACKUP_SERVICE_H #include "appexecfwk_errors.h" #include "nlohmann/json.hpp" @@ -36,4 +36,4 @@ private: }; } // namespace AppExecFwk } // namespace OHOS -#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_SHORTCUT_CLONE_SERVICE_H \ No newline at end of file +#endif // FOUNDATION_APPEXECFWK_SERVICES_BUNDLEMGR_INCLUDE_BUNDLE_BACKUP_SERVICE_H \ No newline at end of file diff --git a/services/bundlemgr/src/bundle_backup/bundle_backup_mgr.cpp b/services/bundlemgr/src/bundle_backup/bundle_backup_mgr.cpp index b7b2c72bd9..685dad7fe9 100644 --- a/services/bundlemgr/src/bundle_backup/bundle_backup_mgr.cpp +++ b/services/bundlemgr/src/bundle_backup/bundle_backup_mgr.cpp @@ -107,6 +107,7 @@ ErrCode BundleBackupMgr::SaveToFile(const std::string& config) int32_t ret = static_cast(fwrite(config.c_str(), 1, config.length(), fp)); if (ret != (int)config.length()) { APP_LOGE("Save config file: %{public}s, fwrite %{public}d failed", BACKUP_FILE_PATH, ret); + return ERR_APPEXECFWK_BACKUP_FILE_IO_ERROR; } (void)fflush(fp); (void)fsync(fileno(fp)); diff --git a/services/bundlemgr/src/bundle_mgr_service.cpp b/services/bundlemgr/src/bundle_mgr_service.cpp index 32c8436410..354e5a5d9b 100644 --- a/services/bundlemgr/src/bundle_mgr_service.cpp +++ b/services/bundlemgr/src/bundle_mgr_service.cpp @@ -588,16 +588,16 @@ bool BundleMgrService::IsBrokerServiceStarted() const int32_t BundleMgrService::OnExtension(const std::string& extension, MessageParcel& data, MessageParcel& reply) { APP_LOGI("extension is %{public}s.", extension.c_str()); - auto BundleBackupMgr = BundleBackupMgr::GetInstance(); + auto bundleBackupMgr = BundleBackupMgr::GetInstance(); ErrCode ret = ERR_OK; if (extension == EXTENSION_BACKUP) { - ret = BundleBackupMgr.OnBackup(data, reply); + ret = bundleBackupMgr.OnBackup(data, reply); if (ret != ERR_OK) { APP_LOGE("OnBackup failed, err is %{public}d.", ret); return -1; } } else if (extension == EXTENSION_RESTORE) { - ret = BundleBackupMgr.OnRestore(data, reply); + ret = bundleBackupMgr.OnRestore(data, reply); if (ret != ERR_OK) { APP_LOGE("OnRestore failed, err is %{public}d.", ret); return -1; diff --git a/services/bundlemgr/test/unittest/bms_data_mgr_test/bms_data_mgr_test.cpp b/services/bundlemgr/test/unittest/bms_data_mgr_test/bms_data_mgr_test.cpp index 5aaa1c31fe..2db04ec37e 100644 --- a/services/bundlemgr/test/unittest/bms_data_mgr_test/bms_data_mgr_test.cpp +++ b/services/bundlemgr/test/unittest/bms_data_mgr_test/bms_data_mgr_test.cpp @@ -22,6 +22,7 @@ #include "ability_manager_helper.h" #include "app_log_wrapper.h" #include "appexecfwk_errors.h" +#include "bundle_backup_mgr.h" #include "bundle_data_storage_interface.h" #include "bundle_data_mgr.h" #include "bundle_mgr_service.h" @@ -3167,5 +3168,74 @@ HWTEST_F(BmsDataMgrTest, OnExtension_0010, Function | SmallTest | Level1) std::vector vecShortcutInfo; shortcutDataStorageRdb->GetAllDesktopShortcutInfo(USERID, vecShortcutInfo); EXPECT_GE(vecShortcutInfo.size(), 0); + + ret = shortcutDataStorageRdb->DeleteDesktopShortcutInfo(shortcutInfo, USERID); + EXPECT_TRUE(ret); +} + +/** + * @tc.number: BundleBackupMgr_0100 + * @tc.name: test BundleBackupMgr + * @tc.desc: 1.test OnExtension backup + */ +HWTEST_F(BmsDataMgrTest, BundleBackupMgr_0100, Function | MediumTest | Level1) +{ + std::shared_ptr shortcutDataStorageRdb = std::make_shared(); + ASSERT_NE(shortcutDataStorageRdb, nullptr); + ShortcutInfo shortcutInfo = BmsDataMgrTest::InitShortcutInfo(); + int32_t USERID = 100; + bool isIdIllegal = false; + shortcutDataStorageRdb->AddDesktopShortcutInfo(shortcutInfo, USERID, isIdIllegal); + + MessageParcel data; + MessageParcel reply; + auto ret = BundleBackupMgr::GetInstance().OnBackup(data, reply); + EXPECT_EQ(ret, ERR_OK); + EXPECT_GE(reply.ReadFileDescriptor(), 0); + + bool result = shortcutDataStorageRdb->DeleteDesktopShortcutInfo(shortcutInfo, USERID); + EXPECT_TRUE(result); +} + +/** + * @tc.number: BundleBackupMgr_0200 + * @tc.name: test BundleBackupMgr + * @tc.desc: 1.test OnExtension restore with invalid fd + */ +HWTEST_F(BmsDataMgrTest, BundleBackupMgr_0200, Function | MediumTest | Level1) +{ + MessageParcel data; + MessageParcel reply; + data.WriteFileDescriptor(-1); + auto ret = BundleBackupMgr::GetInstance().OnRestore(data, reply); + EXPECT_EQ(ret, ERR_APPEXECFWK_BACKUP_INVALID_PARAMETER); +} + +/** + * @tc.number: BundleBackupMgr_0300 + * @tc.name: test BundleBackupMgr + * @tc.desc: 1.test OnExtension restore with valid fd + */ +HWTEST_F(BmsDataMgrTest, BundleBackupMgr_0300, Function | MediumTest | Level1) +{ + std::shared_ptr shortcutDataStorageRdb = std::make_shared(); + ASSERT_NE(shortcutDataStorageRdb, nullptr); + ShortcutInfo shortcutInfo = BmsDataMgrTest::InitShortcutInfo(); + int32_t USERID = 100; + bool isIdIllegal = false; + shortcutDataStorageRdb->AddDesktopShortcutInfo(shortcutInfo, USERID, isIdIllegal); + + const char* BACKUP_FILE_PATH = "/data/service/el1/public/bms/bundle_manager_service/backup_config.conf"; + MessageParcel data; + MessageParcel reply; + FILE* filePtr = fopen(BACKUP_FILE_PATH, "re"); + EXPECT_NE(filePtr, nullptr); + int32_t fd = fileno(filePtr); + data.WriteFileDescriptor(fd); + auto ret = BundleBackupMgr::GetInstance().OnRestore(data, reply); + (void)close(fd); + EXPECT_EQ(ret, ERR_OK); + bool result = shortcutDataStorageRdb->DeleteDesktopShortcutInfo(shortcutInfo, USERID); + EXPECT_TRUE(result); } } // OHOS \ No newline at end of file -- Gitee