diff --git a/common/utils/include/bundle_file_util.h b/common/utils/include/bundle_file_util.h index 468bbde7537d861eef63cb4300c65bf4f116cb76..6a2c9ebe962e9550300dbf5fd88e0b9f01932362 100644 --- a/common/utils/include/bundle_file_util.h +++ b/common/utils/include/bundle_file_util.h @@ -32,6 +32,11 @@ public: static bool DeleteDir(const std::string &path); static bool IsExistFile(const std::string &filePath); static bool IsExistDir(const std::string &dirPath); + static uint64_t GetFileSize(const std::string &filePath); + static uint64_t GetFreeSpaceInBytes(const std::string &partitionName); + static uint64_t GetFolderSizeInBytes(const std::string &pathName); + static bool IsReportDataPartitionUsageEvent(const std::string &path); + static bool ReportDataPartitionUsageEvent(const std::string &path); }; } // AppExecFwk } // OHOS diff --git a/common/utils/src/bundle_file_util.cpp b/common/utils/src/bundle_file_util.cpp index e50d36f6a15134f26f6ffc230e2536bc60653bff..c8e139b4aae88489a3876710afff85e6dec8699f 100644 --- a/common/utils/src/bundle_file_util.cpp +++ b/common/utils/src/bundle_file_util.cpp @@ -41,6 +41,7 @@ constexpr int64_t MAX_HAP_SIZE = ONE_GB * 4; // 4GB constexpr int PATH_MAX_SIZE = 256; const char FILE_SEPARATOR_CHAR = '/'; constexpr uint8_t MAX_HAP_NUMBER = 128; +constexpr double LOW_PARTITION_SPACE_THRESHOLD = 0.1; } // namespace namespace OHOS { @@ -255,5 +256,61 @@ bool BundleFileUtil::IsExistDir(const std::string &dirPath) return S_ISDIR(result.st_mode); } + +uint64_t BundleFileUtil::GetFileSize(const std::string &filePath) +{ + if (filePath.empty()) { + APP_LOGE("input file path is empty."); + return UINT64_MAX; + } + + struct stat fileInfo = { 0 }; + if (stat(filePath.c_str(), &fileInfo) != 0) { + APP_LOGE("call stat error: %{public}d %{public}s", errno, filePath.c_str()); + return UINT64_MAX; + } + if (!S_ISREG(fileInfo.st_mode)) { + APP_LOGE("path is not a regular file: %{public}s", filePath.c_str()); + return UINT64_MAX; + } + + return fileInfo.st_size; +} + +uint64_t BundleFileUtil::GetFreeSpaceInBytes(const std::string &partitionName) +{ + struct statvfs stst; + if (statvfs(partitionName.c_str(), &stst) != 0) { + APP_LOGI("Failed to get free space for partition %{public}s", partitionName.c_str()); + return UINT64_MAX; + } + + return static_cast(stst.f_bavail) * stst.f_frsize; +} + +uint64_t BundleFileUtil::GetFolderSizeInBytes(const std::string &pathName) +{ + struct statvfs stst; + if (statvfs(pathName.c_str(), &stst) != 0) { + APP_LOGE("Failed to get space info for partition %{public}s", + pathName.c_str()); + return UINT64_MAX; + } + + uint64_t usedBlocks = static_cast(stst.f_blocks) - stst.f_bfree; + return usedBlocks * stst.f_frsize; +} + +bool BundleFileUtil::IsReportDataPartitionUsageEvent(const std::string &path) +{ + int64_t RemainPartitionSize = BundleFileUtil::GetFreeSpaceInBytes(path); + int64_t FileOrFolderSize = BundleFileUtil::GetFolderSizeInBytes(path); + double ratio = (FileOrFolderSize == 0) ? 0 : static_cast(RemainPartitionSize) / FileOrFolderSize; + if (ratio < LOW_PARTITION_SPACE_THRESHOLD) { + APP_LOGW("start hisysevent and partitionsize is smaller then 10%%"); + return true; + } + return false; +} } // AppExecFwk } // OHOS \ No newline at end of file diff --git a/hisysevent.yaml b/hisysevent.yaml index 03ef0c2025015e57b9235ad46d672c19cd9bb9b9..144365076c9a54158f2523e7148a6acb605901f5 100644 --- a/hisysevent.yaml +++ b/hisysevent.yaml @@ -273,4 +273,12 @@ APP_CONTROL_RULE: OPERATION_TYPE: {type: INT32, desc: operation type} ACTION_TYPE: {type: INT32, desc: action type} RULE: {type: STRING, desc: rule} - APP_INDEX: {type: INT32, desc: app index} \ No newline at end of file + APP_INDEX: {type: INT32, desc: app index} + +USER_DATA_SIZE: + __BASE: {type: STATISTIC, level: MINOR , desc: data partition management} + COMPONENT_NAME: {type: STRING, desc: name of component} + PARTITION_NAME: {type: STRING, desc: name of partition} + REMAIN_PARTITION_SIZE: {type: UINT64, desc: Remaining size of the partition} + FILE_OR_FOLDER_PATH: {type: STRING, arrsize: 100, desc: File or folder path} + FILE_OR_FOLDER_SIZE: {type: UINT64, arrsize: 100, desc: File or folder size} \ No newline at end of file diff --git a/services/bundlemgr/BUILD.gn b/services/bundlemgr/BUILD.gn index 06bfcd3977cd10a7b73553e08883604f0f76ee5b..49da9b54dac62ac5bce8436ff5331a67ff167157 100644 --- a/services/bundlemgr/BUILD.gn +++ b/services/bundlemgr/BUILD.gn @@ -427,6 +427,9 @@ ohos_shared_library("libbms") { external_deps += [ "hisysevent:libhisysevent" ] defines += [ "HISYSEVENT_ENABLE" ] } + if (current_cpu == "arm64") { + defines += [ "USE_EXTENSION_DATA" ] + } if (use_pre_bundle_profile) { defines += [ "USE_PRE_BUNDLE_PROFILE" ] diff --git a/services/bundlemgr/include/bundle_installer_manager.h b/services/bundlemgr/include/bundle_installer_manager.h index 04fd35e7810d555586b755e4941395ab12ae4c69..0749ab49556fbefbfc70db002d71c0eacac69694 100644 --- a/services/bundlemgr/include/bundle_installer_manager.h +++ b/services/bundlemgr/include/bundle_installer_manager.h @@ -34,6 +34,12 @@ class BundleInstallerManager : public std::enable_shared_from_this& dataDirs); /** * @brief Create a bundle installer object for installing a bundle. * @param bundleFilePath Indicates the path for storing the HAP of the bundle to install or update. diff --git a/services/bundlemgr/include/event_report.h b/services/bundlemgr/include/event_report.h index 1d1151c74cc38e7f6656b8574c72d2c496650bca..48db899fe6494e0f0bd49958bd1fecce18a9d8a9 100644 --- a/services/bundlemgr/include/event_report.h +++ b/services/bundlemgr/include/event_report.h @@ -51,7 +51,8 @@ enum class BMSEventType : uint8_t { FREE_INSTALL_EVENT, BMS_DISK_SPACE, APP_CONTROL_RULE, - DB_ERROR + DB_ERROR, + DATA_PARTITION_USAGE_EVENT }; enum class BundleEventType : uint8_t { @@ -155,6 +156,7 @@ struct EventInfo { // only used in fault event ErrCode errCode = ERR_OK; int64_t costTimeSeconds = 0; + uint64_t partitionSize = 0; int64_t timeStamp = 0; int64_t freeSize = 0; int32_t errorCode = 0; @@ -178,6 +180,7 @@ struct EventInfo { std::string continueType; std::string fileName; std::string callingName; + std::vector fileSize; std::string rule; std::vector filePath; std::vector hashValue; @@ -205,6 +208,7 @@ struct EventInfo { callingAppId.clear(); callingBundleName.clear(); filePath.clear(); + fileSize.clear(); hashValue.clear(); fingerprint.clear(); hideDesktopIcon = false; @@ -216,6 +220,7 @@ struct EventInfo { compileResult = false; failureReason.clear(); costTimeSeconds = 0; + partitionSize = 0; continueType.clear(); sceneId = 0; processName.clear(); diff --git a/services/bundlemgr/include/inner_event_report.h b/services/bundlemgr/include/inner_event_report.h index 7f22bce8ee36311cd6803e4a72a917f74d4110a0..9abd73d75850578f011512fc18473fff8b571e66 100644 --- a/services/bundlemgr/include/inner_event_report.h +++ b/services/bundlemgr/include/inner_event_report.h @@ -61,6 +61,7 @@ private: static void InnerSendBmsDiskSpaceEvent(const EventInfo& eventInfo); static void InnerSendAppControlRule(const EventInfo& eventInfo); static void InnerSendDbErrorEvent(const EventInfo& eventInfo); + static void InnerSendDataPartitionUsageEvent(const EventInfo& eventInfo); template static void InnerEventWrite(const std::string &eventName, diff --git a/services/bundlemgr/src/bundle_data_storage_rdb.cpp b/services/bundlemgr/src/bundle_data_storage_rdb.cpp index be6564eb5ae545c6222f7f772618bcbfed35c30a..b4d01ae968604faae0320dcafdd212c5cfbe823a 100644 --- a/services/bundlemgr/src/bundle_data_storage_rdb.cpp +++ b/services/bundlemgr/src/bundle_data_storage_rdb.cpp @@ -16,11 +16,41 @@ #include "bundle_data_storage_rdb.h" #include "bundle_exception_handler.h" +#include "bundle_file_util.h" +#include "event_report.h" namespace OHOS { namespace AppExecFwk { namespace { constexpr const char* BUNDLE_RDB_TABLE_NAME = "installed_bundle"; +constexpr const char* PARTITION_NAME = "/data"; +} + +static void ReportReportRdbPartitionUsageEvent() +{ + if (!BundleFileUtil::IsReportDataPartitionUsageEvent(PARTITION_NAME)) { + APP_LOGE("fialed obtained the timing for HisEvent"); + return; + } + const std::vector paths = { + "/data/service/el1/public/bms/bundle_manager_service/bmsdb.db" + }; + EventInfo eventInfo; + eventInfo.partitionSize = BundleFileUtil::GetFreeSpaceInBytes(PARTITION_NAME); + if (eventInfo.partitionSize == UINT64_MAX) { + APP_LOGE("fialed get free space for Hisevent condition"); + return; + } + for (auto item : paths) { + auto useSize = BundleFileUtil::GetFileSize(item); + if (useSize == UINT64_MAX) { + APP_LOGE("fialed get the size of file or folder"); + continue; + } + eventInfo.filePath.push_back(item); + eventInfo.fileSize.push_back(useSize); + } + EventReport::SendSystemEvent(BMSEventType::DATA_PARTITION_USAGE_EVENT, eventInfo); } BundleDataStorageRdb::BundleDataStorageRdb() { @@ -107,6 +137,7 @@ void BundleDataStorageRdb::TransformStrToInfo( void BundleDataStorageRdb::UpdateDataBase(std::map &infos) { APP_LOGD("Begin to update database"); + ReportReportRdbPartitionUsageEvent(); if (rdbDataManager_ == nullptr) { APP_LOGE("rdbDataManager is null"); return; @@ -122,6 +153,7 @@ void BundleDataStorageRdb::UpdateDataBase(std::map bool BundleDataStorageRdb::SaveStorageBundleInfo(const InnerBundleInfo &innerBundleInfo) { + ReportReportRdbPartitionUsageEvent(); if (rdbDataManager_ == nullptr) { APP_LOGE("rdbDataManager is null"); return false; diff --git a/services/bundlemgr/src/bundle_installer_manager.cpp b/services/bundlemgr/src/bundle_installer_manager.cpp index 824132091f29eddd2752a2e1847ef911d640c695..53c9d628594f61cc76c27ca50e43a7cdca8eb412 100644 --- a/services/bundlemgr/src/bundle_installer_manager.cpp +++ b/services/bundlemgr/src/bundle_installer_manager.cpp @@ -22,6 +22,7 @@ #include "ipc_skeleton.h" #include "parameters.h" #include "xcollie_helper.h" +#include "bundle_file_util.h" namespace OHOS { namespace AppExecFwk { @@ -30,18 +31,112 @@ constexpr const char* INSTALL_TASK = "Install_Task"; constexpr const char* UNINSTALL_TASK = "Uninstall_Task"; constexpr const char* RECOVER_TASK = "Recover_Task"; constexpr const char* THREAD_POOL_NAME = "InstallerThreadPool"; +constexpr const char* RETAIL_MODE_KEY = "const.dfx.enable_retail"; +constexpr const char* PARTITION_NAME = "/data"; +constexpr const char* DIR_DATA_SERVICE = "/data/service"; +constexpr const char* DIR_ELS[] = {"/el1/", "/el2/", "/el3/", "/el4/", "/el5/"}; +constexpr const char* DIR_BACKUP = "/backup/bundles"; +constexpr const char* DIR_SHARE = "/share"; +constexpr const char* DIR_ACCOUNT_DATA = "/hmdfs/account/data"; +constexpr const char* DIR_NON_ACCOUNT = "/hmdfs/non_account/data"; +constexpr const char* DIR_CLOUD_DATA = "/hmdfs/cloud/data"; +constexpr const char* DIR_LOCAL = "/data/local/shader_cache/local"; +constexpr const char* DIR_DATA_APP = "/data/app"; +constexpr const char* DIR_APP_BUNDLE = "/bundle/public"; +constexpr const char* DIR_BASE = "/base"; +constexpr const char* DIR_DATA_BASE = "/database"; +constexpr const char* DIR_ARK_PRO = "/ark-profile"; +constexpr const char* DIR_SHADER_CACHE = "/shader_cache"; +constexpr const char* DIR_LOG = "/log"; +constexpr const char* DIR_SHARE_FILES = "/sharefiles"; +constexpr const char* DIR_GROUP = "/group"; +constexpr const char* DIR_APP_LOCAL = "/app/local"; +constexpr const char* DIR_CLOUD = "/cloud"; +constexpr const char* DIR_CLOUD_COMMON = "/cloud/common"; +constexpr const char* DIR_DATA_LOCAL_ARK = "/data/local/ark-cache"; constexpr unsigned int TIME_OUT_SECONDS = 60 * 25; constexpr int8_t MAX_TASK_NUMBER = 10; constexpr int8_t RETAIL_MODE_THREAD_NUMBER = 1; constexpr int8_t DELAY_INTERVAL_SECONDS = 60; static std::atomic g_taskCounter = 0; +constexpr int32_t DATA_USER_ID = 0; +} + +void GetDataPartitionUsageDirs(std::vector& dataDirs) +{ + std::set userIds; + userIds.insert(DATA_USER_ID); + AccountHelper::QueryAllCreatedOsAccounts(userIds); + dataDirs.push_back(DIR_LOCAL); + dataDirs.push_back(DIR_DATA_LOCAL_ARK); + for (auto userId : userIds) { + for (auto eli : DIR_ELS) { + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(std::to_string(userId)).append(DIR_BASE)); + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(std::to_string(userId)).append(DIR_DATA_BASE)); + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(std::to_string(userId)).append(DIR_GROUP)); + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(DIR_APP_BUNDLE)); + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(std::to_string(userId)).append(DIR_LOG)); + dataDirs.push_back( + std::string(DIR_DATA_APP).append(eli).append(std::to_string(userId)).append(DIR_SHARE_FILES)); + dataDirs.push_back( + std::string(DIR_DATA_SERVICE).append(eli).append(std::to_string(userId)).append(DIR_BACKUP)); + dataDirs.push_back( + std::string(DIR_DATA_SERVICE).append(eli).append(std::to_string(userId)).append(DIR_ACCOUNT_DATA)); + dataDirs.push_back( + std::string(DIR_DATA_SERVICE).append(eli).append(std::to_string(userId)).append(DIR_ACCOUNT_DATA)); + dataDirs.push_back( + std::string(DIR_DATA_SERVICE).append(eli).append(std::to_string(userId)).append(DIR_NON_ACCOUNT)); + dataDirs.push_back( + std::string(DIR_DATA_SERVICE).append(eli).append(std::to_string(userId)).append(DIR_SHARE)); + dataDirs.push_back( + std::string(DIR_APP_LOCAL).append(DIR_SHADER_CACHE).append(DIR_CLOUD)); + dataDirs.push_back( + std::string(DIR_APP_LOCAL).append(DIR_SHADER_CACHE).append(DIR_CLOUD_COMMON)); + dataDirs.push_back( + std::string(DIR_APP_LOCAL).append(DIR_ARK_PRO).append(std::to_string(userId))); + } + } + APP_LOGI("Get data dirs count is %{public}zu", dataDirs.size()); +} +static void ReportReportDataPartitionUsageEvent() +{ + if (!BundleFileUtil::IsReportDataPartitionUsageEvent(PARTITION_NAME)) { + APP_LOGE("get the timming of hisysevent fialed"); + return; + } + + EventInfo eventInfo; + eventInfo.partitionSize = BundleFileUtil::GetFreeSpaceInBytes(PARTITION_NAME); + if (eventInfo.partitionSize == UINT64_MAX) { + APP_LOGE("get the RemainPartitionSize of hisysevent fialed"); + return; + } + std::vector dataDirs; + GetDataPartitionUsageDirs(dataDirs); + for(auto& item : dataDirs) { + auto useSize = BundleFileUtil::GetFolderSizeInBytes(item); + if (useSize == UINT64_MAX) { + APP_LOGE("get the FileOrFolderSize of hisysevent fialed"); + continue; + } + + eventInfo.filePath.push_back(item); + eventInfo.fileSize.push_back(useSize); + } + + EventReport::SendSystemEvent(BMSEventType::DATA_PARTITION_USAGE_EVENT, eventInfo); } BundleInstallerManager::BundleInstallerManager() { - if (system::GetBoolParameter(ServiceConstants::RETAIL_MODE_KEY, false)) { - LOG_NOFUNC_I(BMS_TAG_INSTALLER, "RETAIL_MODE"); - threadNum_ = RETAIL_MODE_THREAD_NUMBER; + maxTaskNum_ = MAX_TASK_NUMBER; + if (system::GetBoolParameter(RETAIL_MODE_KEY, false)) { + maxTaskNum_ = MAX_TASK_NUMBER / 2; } LOG_NOFUNC_I(BMS_TAG_INSTALLER, "create bundle installer manager instance"); } @@ -62,6 +157,7 @@ void BundleInstallerManager::CreateInstallTask( auto task = [installer, bundleFilePath, installParam] { BundleMemoryGuard memoryGuard; int32_t timerId = XCollieHelper::SetTimer(INSTALL_TASK, TIME_OUT_SECONDS, nullptr, nullptr); + ReportReportDataPartitionUsageEvent(); installer->Install(bundleFilePath, installParam); g_taskCounter--; XCollieHelper::CancelTimer(timerId); diff --git a/services/bundlemgr/src/inner_event_report.cpp b/services/bundlemgr/src/inner_event_report.cpp index ffa9b65d4ba75d3bd70158e87694cd367d5540c7..be0de855b781ed0940c243411deadc0ad95b1a18 100644 --- a/services/bundlemgr/src/inner_event_report.cpp +++ b/services/bundlemgr/src/inner_event_report.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -47,6 +47,7 @@ constexpr const char* AOT_COMPILE_RECORD = "AOT_COMPILE_RECORD"; constexpr const char* QUERY_OF_CONTINUE_TYPE = "QUERY_OF_CONTINUE_TYPE"; constexpr const char* BMS_DISK_SPACE = "BMS_DISK_SPACE"; constexpr const char* APP_CONTROL_RULE = "APP_CONTROL_RULE"; +constexpr const char* USER_DATA_SIZE = "USER_DATA_SIZE"; constexpr const char* DB_ERROR = "DB_ERROR"; // event params @@ -86,6 +87,12 @@ const char* EVENT_PARAM_ACTION_TYPE = "ACTION_TYPE"; const char* EVENT_PARAM_RULE = "ACTION_RULE"; const char* EVENT_PARAM_APP_INDEX = "APP_INDEX"; const char* EVENT_PARAM_IS_PATCH = "IS_PATCH"; +const char* FILE_OR_FOLDER_PATH = "FILE_OR_FOLDER_PATH"; +const char* FILE_OR_FOLDER_SIZE = "FILE_OR_FOLDER_SIZE"; +const char* COMPONENT_NAME_KEY = "COMPONENT_NAME"; +const char* PARTITION_NAME_KEY = "PARTITION_NAME"; +const char* REMAIN_PARTITION_SIZE_KEY = "REMAIN_PARTITION_SIZE"; + const char* FREE_INSTALL_TYPE = "FreeInstall"; const char* PRE_BUNDLE_INSTALL_TYPE = "PreBundleInstall"; @@ -123,6 +130,9 @@ const char* OPERATION_TYPE = "operationType"; const char* DB_NAME = "dbName"; const char* ERROR_CODE = "errorCode"; const char* REBUILD_TYPE = "rebuildType"; +const char* COMPONENT_NAME = "hisevent"; +const char* PARTITION_NAME = "/data"; + const InstallScene INSTALL_SCENE_STR_MAP_KEY[] = { InstallScene::NORMAL, @@ -291,6 +301,10 @@ std::unordered_map [](const EventInfo& eventInfo) { InnerSendAppControlRule(eventInfo); } }, + { BMSEventType::DATA_PARTITION_USAGE_EVENT, + [](const EventInfo& eventInfo) { + InnerSendDataPartitionUsageEvent(eventInfo); + } }, { BMSEventType::DB_ERROR, [](const EventInfo& eventInfo) { InnerSendDbErrorEvent(eventInfo); @@ -663,6 +677,23 @@ void InnerEventReport::InnerSendDbErrorEvent(const EventInfo& eventInfo) ERROR_CODE, eventInfo.errorCode); } +void InnerEventReport::InnerSendDataPartitionUsageEvent(const EventInfo& eventInfo) +{ + HiSysEventWrite( +#ifdef USE_EXTENSION_DATA + OHOS::HiviewDFX::HiSysEvent::Domain::FILEMANAGEMENT, +#else + OHOS::HiviewDFX::HiSysEvent::Domain::BUNDLEMANAGER_UE, +#endif + USER_DATA_SIZE, + HiviewDFX::HiSysEvent::EventType::STATISTIC, + COMPONENT_NAME_KEY, COMPONENT_NAME, + PARTITION_NAME_KEY, PARTITION_NAME, + REMAIN_PARTITION_SIZE_KEY, eventInfo.partitionSize, + FILE_OR_FOLDER_PATH, eventInfo.filePath, + FILE_OR_FOLDER_SIZE, eventInfo.fileSize); +} + template void InnerEventReport::InnerEventWrite( const std::string &eventName, diff --git a/services/bundlemgr/test/unittest/bms_bundle_installer_manager_test/bms_bundle_installer_manager_test.cpp b/services/bundlemgr/test/unittest/bms_bundle_installer_manager_test/bms_bundle_installer_manager_test.cpp index 8a8d2a122ff4c9ecd6b76bd3835c9b9b59fdc2c5..89d746020c568d1473938a97624bb4f47eecaaf2 100644 --- a/services/bundlemgr/test/unittest/bms_bundle_installer_manager_test/bms_bundle_installer_manager_test.cpp +++ b/services/bundlemgr/test/unittest/bms_bundle_installer_manager_test/bms_bundle_installer_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2023 Huawei Device Co., Ltd. + * Copyright (c) 2023-2025 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 @@ -16,6 +16,8 @@ #include #include #include +#include +#include #include "mock_status_receiver.h" #define private public @@ -25,6 +27,7 @@ #include "bundle_mgr_service.h" #include "event_runner.h" #include "inner_event.h" +#include "bundle_file_util.h" #undef public #undef protected using namespace testing::ext; @@ -39,6 +42,8 @@ const std::string BUNDLE_NAME = "com.ohos.launcher"; const std::string MODULE_PACKAGE = "entry"; const int32_t USERID = 100; const std::string EMPTY_STRING = ""; +const std::string PARTITION_NAME = "/data"; +const std::string PATH = "/data/test"; } // namespace class BundleInstallerManagerTest : public testing::Test { @@ -206,5 +211,20 @@ HWTEST_F(BundleInstallerManagerTest, BundleInstallerManagerTest_020, TestSize.Le ErrCode result = receiver->GetResultCode(); EXPECT_NE(ERR_OK, result); } + +/** + * @tc.number: BundleInstallerManagerTest_021 + * @tc.name: test ReportReportDataPartitionUsageEvent + * @tc.desc: Verify function ReportReportDataPartitionUsageEvent is called, + * receiver->GetResultCode() return value is ERR_OK + */ +HWTEST_F(BundleInstallerManagerTest, BundleInstallerManagerTest_021, TestSize.Level1) +{ + bool result = BundleFileUtil::IsReportDataPartitionUsageEvent(PARTITION_NAME); + EXPECT_EQ(result, false); + uint64_t bytes = BundleFileUtil::GetFreeSpaceInBytes(PARTITION_NAME); + uint64_t size = BundleFileUtil::GetFolderSizeInBytes(PATH); + EXPECT_EQ(size, (static_cast(stat.f_blocks) - stat.f_bfree) * stat.f_frsize); +} } // AppExecFwk } // OHOS \ No newline at end of file diff --git a/services/bundlemgr/test/unittest/bms_hap_module_info_test/bms_hap_module_info_test.cpp b/services/bundlemgr/test/unittest/bms_hap_module_info_test/bms_hap_module_info_test.cpp index d210bd12ac8b6c258536e70bcad139c8319eb673..485af96dea38c576515674d98677cf7c1f054fce 100644 --- a/services/bundlemgr/test/unittest/bms_hap_module_info_test/bms_hap_module_info_test.cpp +++ b/services/bundlemgr/test/unittest/bms_hap_module_info_test/bms_hap_module_info_test.cpp @@ -1,760 +1,760 @@ -/* - * Copyright (c) 2024 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. - */ -#define private public -#define protected public -#include -#include -#include - -#include "app_jump_control_rule.h" -#include "application_info.h" -#include "bundle_file_util.h" -#include "bundle_pack_info.h" -#include "code_protect_bundle_info.h" -#include "data_group_info.h" -#include "disposed_rule.h" -#include "extension_ability_info.h" -#include "form_info.h" -#include "hap_module_info.h" -#include "json_util.h" -#include "preinstalled_application_info.h" -#include "nlohmann/json.hpp" -#include "skill.h" -#include "recoverable_application_info.h" -#include "want.h" - -using namespace testing::ext; -using OHOS::AAFwk::Want; - -namespace OHOS { -namespace AppExecFwk { - -const char* PRELOAD_ITEM_MODULE_NAME = "moduleName"; -const uint32_t HAP_MODULE_INFO_VERSION_CODE = 0; -constexpr const char* BUNDLE_NAME = "bundleName"; -constexpr const char* MODULE_NAME = "moduleName"; -const char* ROUTER_ITEM_KEY_NAME = "name"; -const char* ROUTER_ITEM_KEY_BUILD_FUNCTION = "buildFunction"; -const char* APP_ENVIRONMENTS_NAME = "name"; -const char* APP_ENVIRONMENTS_VALUE = "value"; -const uint32_t HAP_MODULE_INFO_LABEL_ID = 10; -const char* HAP_MODULE_INFO_HAP_PATH = "hapPath"; -const uint32_t JSON_KEY_LABEL_ID = 20; -const uint32_t JSON_KEY_ICON_ID = 30; -const std::string DATAGROUPID = "dataGroupId"; -const std::string UUID = "uuid"; -const uint32_t UID = 2; -const uint32_t GID = 3; -const uint32_t USERID = 4; -const int32_t ZERO_SIZE = 0; -const int32_t INVALIED_ID = -1; -const std::string HOST = "host"; -const std::string PORT = "port"; -const std::string PATH = "path"; -const std::string SCHEME = "scheme"; -const std::string PATHREGEX = "pathRegex"; -const std::string PORT1 = "port"; -const std::string PACKAGE = "package"; -const std::string TYPE = "type"; -const std::string NAME = "name"; -const std::string VALUE = "value"; -const std::string CALLERPKG = "caller"; -const std::string TARGETPKG = "target"; -const std::string CONTROLMESSAGE = "control"; -const std::string BUNDLENAME = "bundleName"; -const std::string MODULENAME = "moduleName"; -const std::uint32_t LABEL_ID = 10; -const std::uint32_t ICON_ID = 20; -constexpr int32_t JUMP_MODE = static_cast(AbilityJumpMode::DIRECT); -const std::string BUNDLE_NAME1 = "testBundleName"; -constexpr int32_t UID1 = 100; -constexpr int32_t APPINDEX = 10; -constexpr uint32_t VERSIONCODE = 20; -constexpr uint32_t APPLICATIONRESERVEDFLAG = 30; -const std::string BUNDLE_NAME2 = "testBundleName"; -const std::string MODULE_NAME2 = "testModuleName"; -const std::string ABILITY_NAME = "testAbilityName"; -const char* DEVICE_ID = "deviceId"; - -void to_json(nlohmann::json &jsonObject, const PreloadItem &preloadItem); -void from_json(const nlohmann::json &jsonObject, PreloadItem &preloadItem); -void to_json(nlohmann::json &jsonObject, const Dependency &dependency); -void from_json(const nlohmann::json &jsonObject, Dependency &dependency); -void to_json(nlohmann::json &jsonObject, const RouterItem &routerItem); -void from_json(const nlohmann::json &jsonObject, RouterItem &routerItem); -void to_json(nlohmann::json &jsonObject, const AppEnvironment &appEnvironment); -void from_json(const nlohmann::json &jsonObject, AppEnvironment &appEnvironment); -void to_json(nlohmann::json &jsonObject, const HapModuleInfo &hapModuleInfo); -void to_json(nlohmann::json &jsonObject, const RecoverableApplicationInfo &recoverableApplicationInfo); -void from_json(const nlohmann::json &jsonObject, RecoverableApplicationInfo &recoverableApplicationInfo); -void to_json(nlohmann::json &jsonObject, const ApplicationEnvironment &applicationEnvironment); -void from_json(const nlohmann::json &jsonObject, ApplicationEnvironment &applicationEnvironment); -void to_json(nlohmann::json &jsonObject, const HnpPackage &hnpPackage); -void from_json(const nlohmann::json &jsonObject, HnpPackage &hnpPackage); -void to_json(nlohmann::json &jsonObject, const FormCustomizeData &customizeDatas); -void to_json(nlohmann::json &jsonObject, const CodeProtectBundleInfo &CodeProtectBundleInfo); -void from_json(const nlohmann::json &jsonObject, CodeProtectBundleInfo &CodeProtectBundleInfo); -void to_json(nlohmann::json &jsonObject, const ElementName &elementName); -void from_json(const nlohmann::json &jsonObject, ElementName &elementName); -void to_json(nlohmann::json &jsonObject, const DisposedRule &disposedRule); -void from_json(const nlohmann::json &jsonObject, DisposedRule &disposedRule); - -class BmsHapModuleInfoTest : public testing::Test { -public: - static void SetUpTestCase(); - static void TearDownTestCase(); - void SetUp(); - void TearDown(); -}; - -void BmsHapModuleInfoTest::SetUpTestCase() -{} - -void BmsHapModuleInfoTest::TearDownTestCase() -{} - -void BmsHapModuleInfoTest::SetUp() -{} - -void BmsHapModuleInfoTest::TearDown() -{} - -/** - * @tc.number: BmsHapModuleInfoTest_0100 - * @tc.name: test to_json interface in HapModuleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_0100, Function | SmallTest | Level0) -{ - PreloadItem preloadItem; - preloadItem.moduleName = "moduleName"; - nlohmann::json jsonObject; - to_json(jsonObject, preloadItem); - EXPECT_EQ(preloadItem.moduleName, PRELOAD_ITEM_MODULE_NAME); -} - -/** - * @tc.number: BmsHapModuleInfoTest_0200 - * @tc.name: test from_json interface in HapModuleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, from_json_0100, Function | SmallTest | Level0) -{ - PreloadItem preloadItem; - preloadItem.moduleName = "moduleName"; - nlohmann::json jsonObject; - PreloadItem result; - from_json(jsonObject, result); - EXPECT_NE(preloadItem.moduleName, result.moduleName); -} - -/** - * @tc.number: BmsHapModuleInfoTest_0300 - * @tc.name: test to_json interface in HapModuleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_0200, Function | SmallTest | Level0) -{ - Dependency dependency; - dependency.bundleName = "bundleName"; - dependency.moduleName = "moduleName"; - dependency.versionCode = 0; - nlohmann::json jsonObject; - to_json(jsonObject, dependency); - EXPECT_EQ(dependency.bundleName, BUNDLE_NAME); - EXPECT_EQ(dependency.moduleName, MODULE_NAME); - EXPECT_EQ(dependency.versionCode, HAP_MODULE_INFO_VERSION_CODE); -} - -/** - * @tc.number: BmsHapModuleInfoTest_0400 - * @tc.name: test from_json interface in HapModuleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, from_json_0200, Function | SmallTest | Level0) -{ - Dependency dependency; - dependency.bundleName = "bundleName"; - dependency.moduleName = "moduleName"; - dependency.versionCode = 0; - nlohmann::json jsonObject; - Dependency result; - from_json(jsonObject, result); - EXPECT_NE(dependency.bundleName, result.bundleName); - EXPECT_NE(dependency.moduleName, result.moduleName); - EXPECT_EQ(dependency.versionCode, result.versionCode); -} - -/** - * @tc.number: BmsHapModuleInfoTest_0500 - * @tc.name: test to_json interface in HapModuleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_0300, Function | SmallTest | Level0) -{ - RouterItem routerItem; - routerItem.name = "name"; - routerItem.buildFunction = "buildFunction"; - nlohmann::json jsonObject; - to_json(jsonObject, routerItem); - EXPECT_EQ(routerItem.name, ROUTER_ITEM_KEY_NAME); - EXPECT_EQ(routerItem.buildFunction, ROUTER_ITEM_KEY_BUILD_FUNCTION); -} - -/** - * @tc.number: BmsHapModuleInfoTest_0600 - * @tc.name: test from_json interface in HapModuleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, from_json_0300, Function | SmallTest | Level0) -{ - RouterItem routerItem; - routerItem.name = "name"; - routerItem.buildFunction = "buildFunction"; - nlohmann::json jsonObject; - RouterItem result; - from_json(jsonObject, result); - EXPECT_NE(routerItem.name, result.name); - EXPECT_NE(routerItem.buildFunction, result.buildFunction); -} - -/** - * @tc.number: BmsHapModuleInfoTest_0700 - * @tc.name: test Marshalling interface in HapModuleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, Marshalling_0100, Function | SmallTest | Level0) -{ - AppEnvironment appEnvironment; - Parcel parcel; - std::string name = "IsName"; - std::string value = "value"; - parcel.WriteString16(Str8ToStr16(name)); - parcel.WriteString16(Str8ToStr16(value)); - auto ret = appEnvironment.Marshalling(parcel); - EXPECT_TRUE(ret); -} - -/** - * @tc.number: BmsHapModuleInfoTest_0800 - * @tc.name: test to_json interface in HapModuleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_0400, Function | SmallTest | Level0) -{ - AppEnvironment appEnvironment; - appEnvironment.name = "name"; - appEnvironment.value = "value"; - nlohmann::json jsonObject; - to_json(jsonObject, appEnvironment); - EXPECT_EQ(appEnvironment.name, APP_ENVIRONMENTS_NAME); - EXPECT_EQ(appEnvironment.value, APP_ENVIRONMENTS_VALUE); -} - -/** - * @tc.number: BmsHapModuleInfoTest_0900 - * @tc.name: test from_json interface in HapModuleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, from_json_0400, Function | SmallTest | Level0) -{ - AppEnvironment appEnvironment; - appEnvironment.name = "name"; - appEnvironment.value = "value"; - nlohmann::json jsonObject; - AppEnvironment result; - from_json(jsonObject, result); - EXPECT_NE(appEnvironment.name, result.name); - EXPECT_NE(appEnvironment.value, result.value); -} - -/** - * @tc.number: BmsHapModuleInfoTest_1000 - * @tc.name: test to_json interface in HapModuleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_0500, Function | SmallTest | Level0) -{ - HapModuleInfo hapModuleInfo; - hapModuleInfo.labelId = 10; - hapModuleInfo.hapPath = "hapPath"; - nlohmann::json jsonObject; - to_json(jsonObject, hapModuleInfo); - EXPECT_EQ(hapModuleInfo.labelId, HAP_MODULE_INFO_LABEL_ID); - EXPECT_EQ(hapModuleInfo.hapPath, HAP_MODULE_INFO_HAP_PATH); -} - -/** - * @tc.number: BmsHapModuleInfoTest_1100 - * @tc.name: test to_json interface in RecoverableApplicationInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_0600, Function | SmallTest | Level0) -{ - RecoverableApplicationInfo recoverableApplicationInfo; - recoverableApplicationInfo.labelId = 20; - recoverableApplicationInfo.iconId = 30; - nlohmann::json jsonObject; - to_json(jsonObject, recoverableApplicationInfo); - EXPECT_EQ(recoverableApplicationInfo.labelId, JSON_KEY_LABEL_ID); - EXPECT_EQ(recoverableApplicationInfo.iconId, JSON_KEY_ICON_ID); -} - -/** - * @tc.number: BmsHapModuleInfoTest_1200 - * @tc.name: test from_json interface in RecoverableApplicationInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, from_json_0500, Function | SmallTest | Level0) -{ - RecoverableApplicationInfo recoverableApplicationInfo; - recoverableApplicationInfo.labelId = 20; - recoverableApplicationInfo.iconId = 30; - nlohmann::json jsonObject; - RecoverableApplicationInfo result; - from_json(jsonObject, result); - EXPECT_NE(recoverableApplicationInfo.labelId, result.labelId); - EXPECT_NE(recoverableApplicationInfo.iconId, result.iconId); -} - -/** - * @tc.number: BmsHapModuleInfoTest_1300 - * @tc.name: test ReadFromParcel interface in DataGroupInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, ReadFromParcel_0100, Function | SmallTest | Level0) -{ - DataGroupInfo dataGroupInfo; - Parcel parcel; - parcel.WriteString16(Str8ToStr16(DATAGROUPID)); - parcel.WriteString16(Str8ToStr16(UUID)); - parcel.WriteInt32(UID); - parcel.WriteInt32(GID); - parcel.WriteInt32(USERID); - bool result = dataGroupInfo.ReadFromParcel(parcel); - EXPECT_TRUE(result); -} - -/** - * @tc.number: BmsHapModuleInfoTest_1400 - * @tc.name: test Marshalling interface in DataGroupInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, Marshalling_0200, Function | SmallTest | Level0) -{ - DataGroupInfo dataGroupInfo; - Parcel parcel; - parcel.WriteString16(Str8ToStr16(DATAGROUPID)); - parcel.WriteString16(Str8ToStr16(UUID)); - parcel.WriteInt32(UID); - parcel.WriteInt32(GID); - parcel.WriteInt32(USERID); - auto ret = dataGroupInfo.Marshalling(parcel); - EXPECT_TRUE(ret); -} - -/** - * @tc.number: BmsHapModuleInfoTest_1500 - * @tc.name: test Unmarshalling interface in DataGroupInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, Unmarshalling_0100, Function | SmallTest | Level0) -{ - DataGroupInfo dataGroupInfo; - std::shared_ptr info = - std::make_shared(); - ASSERT_NE(info, nullptr); - Parcel parcel; - parcel.WriteString16(Str8ToStr16(DATAGROUPID)); - parcel.WriteString16(Str8ToStr16(UUID)); - parcel.WriteInt32(UID); - parcel.WriteInt32(GID); - parcel.WriteInt32(USERID); - auto result = dataGroupInfo.Unmarshalling(parcel); - ASSERT_NE(result, nullptr); -} - -/** - * @tc.number: BmsHapModuleInfoTest_1600 - * @tc.name: test Marshalling interface in Skill. - */ -HWTEST_F(BmsHapModuleInfoTest, Marshalling_0300, Function | SmallTest | Level0) -{ - Skill skill; - Parcel parcel; - parcel.WriteString16(Str8ToStr16(HOST)); - parcel.WriteString16(Str8ToStr16(PATH)); - parcel.WriteString16(Str8ToStr16(PORT)); - auto ret = skill.Marshalling(parcel); - EXPECT_TRUE(ret); -} - -/** - * @tc.number: BmsHapModuleInfoTest_1700 - * @tc.name: test Dump interface in Skill. - */ -HWTEST_F(BmsHapModuleInfoTest, BundleMgrHostImpl_0500, Function | MediumTest | Level1) -{ - Skill skill; - std::string prefix = "prefix"; - int fd = 2; - skill.Dump(prefix, fd); - long length = lseek(fd, ZERO_SIZE, SEEK_END); - EXPECT_EQ(length, INVALIED_ID); -} - -/** - * @tc.number: BmsHapModuleInfoTest_1800 - * @tc.name: test MarshallingSkillUri interface in ExtensionAbilityInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, MarshallingSkillUri_0100, Function | SmallTest | Level0) -{ - ExtensionAbilityInfo extensionAbilityInfo; - Parcel parcel; - SkillUriForAbilityAndExtension uri; - uri.maxFileSupported = 0; - parcel.WriteString16(Str8ToStr16(SCHEME)); - parcel.WriteString16(Str8ToStr16(PATHREGEX)); - parcel.WriteString16(Str8ToStr16(PORT1)); - auto ret = extensionAbilityInfo.MarshallingSkillUri(parcel, uri); - EXPECT_TRUE(ret); -} - -/** - * @tc.number: BmsHapModuleInfoTest_1900 - * @tc.name: test Marshalling interface in BundlePackInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, Marshalling_0400, Function | SmallTest | Level1) -{ - BundlePackInfo bundlePackInfo; - Parcel parcel; - auto moduleType = static_cast(ModuleType::UNKNOWN); - parcel.WriteInt32(moduleType); - auto ret = bundlePackInfo.Marshalling(parcel); - EXPECT_TRUE(ret); -} - -/** - * @tc.number: BmsHapModuleInfoTest_2000 - * @tc.name: test to_json interface in ApplicationInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_0700, Function | SmallTest | Level0) -{ - ApplicationEnvironment applicationEnvironment; - applicationEnvironment.name = "name"; - applicationEnvironment.value = "value"; - nlohmann::json jsonObject; - to_json(jsonObject, applicationEnvironment); - EXPECT_EQ(applicationEnvironment.name, NAME); - EXPECT_EQ(applicationEnvironment.value, VALUE); -} - -/** - * @tc.number: BmsHapModuleInfoTest_2100 - * @tc.name: test from_json interface in ApplicationInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, from_json_0600, Function | SmallTest | Level0) -{ - ApplicationEnvironment applicationEnvironment; - applicationEnvironment.name = "name"; - applicationEnvironment.value = "value"; - nlohmann::json jsonObject; - ApplicationEnvironment result; - from_json(jsonObject, result); - EXPECT_NE(applicationEnvironment.name, result.name); - EXPECT_NE(applicationEnvironment.value, result.value); -} - -/** - * @tc.number: BmsHapModuleInfoTest_2200 - * @tc.name: test to_json interface in ApplicationInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_0800, Function | SmallTest | Level0) -{ - HnpPackage hnpPackage; - hnpPackage.package = "package"; - hnpPackage.type = "type"; - nlohmann::json jsonObject; - to_json(jsonObject, hnpPackage); - EXPECT_EQ(hnpPackage.package, PACKAGE); - EXPECT_EQ(hnpPackage.type, TYPE); -} - -/** - * @tc.number: BmsHapModuleInfoTest_2300 - * @tc.name: test from_json interface in ApplicationInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, from_json_0700, Function | SmallTest | Level0) -{ - HnpPackage hnpPackage; - hnpPackage.package = "package"; - hnpPackage.type = "type"; - nlohmann::json jsonObject; - HnpPackage result; - from_json(jsonObject, result); - EXPECT_NE(hnpPackage.package, result.package); - EXPECT_NE(hnpPackage.type, result.type); -} - -/** - * @tc.number: BmsHapModuleInfoTest_2400 - * @tc.name: test to_json interface in FormInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_0900, Function | SmallTest | Level0) -{ - FormCustomizeData customizeDatas; - customizeDatas.name = "name"; - customizeDatas.value = "value"; - nlohmann::json jsonObject; - to_json(jsonObject, customizeDatas); - EXPECT_EQ(customizeDatas.name, NAME); - EXPECT_EQ(customizeDatas.value, VALUE); -} - -/** - * @tc.number: BmsHapModuleInfoTest_2500 - * @tc.name: test IsExistDir interface in BundleFileUtil. - */ -HWTEST_F(BmsHapModuleInfoTest, IsExistDir_0100, Function | SmallTest | Level0) -{ - std::string dirPath = "PATH"; - auto ret = BundleFileUtil::IsExistDir(dirPath); - EXPECT_FALSE(ret); -} - -/** - * @tc.number: BmsAppJumpControlRuleTest_0100 - * @tc.name: test to_json interface in DisposedRule. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_1000, Function | SmallTest | Level0) -{ - AppJumpControlRule appJumpControlRule; - Parcel parcel; - parcel.WriteString16(Str8ToStr16(CALLERPKG)); - parcel.WriteString16(Str8ToStr16(TARGETPKG)); - parcel.WriteString16(Str8ToStr16(CONTROLMESSAGE)); - parcel.WriteInt32(JUMP_MODE); - bool result = appJumpControlRule.ReadFromParcel(parcel); - EXPECT_TRUE(result); -} - -/** - * @tc.number: BmsAppJumpControlRuleTest_0200 - * @tc.name: test Unmarshalling interface in DisposedRule. - */ -HWTEST_F(BmsHapModuleInfoTest, Unmarshalling_0200, Function | SmallTest | Level0) -{ - AppJumpControlRule appJumpControlRule; - std::shared_ptr info = - std::make_shared(); - ASSERT_NE(info, nullptr); - Parcel parcel; - parcel.WriteString16(Str8ToStr16(CALLERPKG)); - parcel.WriteString16(Str8ToStr16(TARGETPKG)); - parcel.WriteString16(Str8ToStr16(CONTROLMESSAGE)); - parcel.WriteInt32(JUMP_MODE); - auto result = appJumpControlRule.Unmarshalling(parcel); - ASSERT_NE(result, nullptr); -} - -/** - * @tc.number: BmsAppJumpControlRuleTest_0300 - * @tc.name: test Marshalling interface in DisposedRule. - */ -HWTEST_F(BmsHapModuleInfoTest, Marshalling_0500, Function | SmallTest | Level0) -{ - PreinstalledApplicationInfo preinstalledApplicationInfo; - Parcel parcel; - parcel.WriteString16(Str8ToStr16(BUNDLENAME)); - parcel.WriteString16(Str8ToStr16(MODULENAME)); - parcel.WriteUint32(LABEL_ID); - parcel.WriteUint32(ICON_ID); - auto ret = preinstalledApplicationInfo.Marshalling(parcel); - EXPECT_TRUE(ret); -} - -/** - * @tc.number: BmsCodeProtectBundleInfoTest_0100 - * @tc.name: test ReadFromParcel interface in CodeProtectBundleInfo. - * @tc.desc: 1.construct parcel. - * 2.calling ReadFromParcel interface by using input parameter parcel. - */ -HWTEST_F(BmsHapModuleInfoTest, ReadFromParcel_0200, Function | SmallTest | Level0) -{ - CodeProtectBundleInfo codeProtectBundleInfo; - Parcel parcel; - parcel.WriteString16(Str8ToStr16(BUNDLE_NAME1)); - parcel.WriteInt32(UID1); - parcel.WriteInt32(APPINDEX); - parcel.WriteUint32(VERSIONCODE); - parcel.WriteInt32(APPLICATIONRESERVEDFLAG); - bool result = codeProtectBundleInfo.ReadFromParcel(parcel); - EXPECT_TRUE(result); -} - -/** - * @tc.number: BmsCodeProtectBundleInfoTest_0200 - * @tc.name: test to_json interface in CodeProtectBundleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_1100, Function | SmallTest | Level0) -{ - CodeProtectBundleInfo codeProtectBundleInfo; - codeProtectBundleInfo.bundleName = "TestBundleName"; - codeProtectBundleInfo.uid = 10; - codeProtectBundleInfo.appIndex = 20; - codeProtectBundleInfo.versionCode = 30; - codeProtectBundleInfo.applicationReservedFlag = 40; - nlohmann::json jsonObject; - to_json(jsonObject, codeProtectBundleInfo); - CodeProtectBundleInfo result; - from_json(jsonObject, result); - EXPECT_EQ(result.bundleName, "TestBundleName"); - EXPECT_EQ(result.uid, 10); - EXPECT_EQ(result.appIndex, 20); - EXPECT_EQ(result.versionCode, 30); - EXPECT_EQ(result.applicationReservedFlag, 40); -} - -/** - * @tc.number: BmsCodeProtectBundleInfoTest_0300 - * @tc.name: test to_json interface in CodeProtectBundleInfo. - */ -HWTEST_F(BmsHapModuleInfoTest, from_json_0800, Function | SmallTest | Level0) -{ - CodeProtectBundleInfo codeProtectBundleInfo; - codeProtectBundleInfo.bundleName = "TestBundleName"; - codeProtectBundleInfo.uid = 10; - codeProtectBundleInfo.appIndex = 20; - codeProtectBundleInfo.versionCode = 30; - codeProtectBundleInfo.applicationReservedFlag = 40; - nlohmann::json jsonObject; - CodeProtectBundleInfo result; - from_json(jsonObject, result); - EXPECT_NE(codeProtectBundleInfo.bundleName, result.bundleName); - EXPECT_NE(codeProtectBundleInfo.uid, result.uid); - EXPECT_NE(codeProtectBundleInfo.appIndex, result.appIndex); - EXPECT_NE(codeProtectBundleInfo.versionCode, result.versionCode); - EXPECT_NE(codeProtectBundleInfo.applicationReservedFlag, result.applicationReservedFlag); -} - -/** - * @tc.number: BmsDisposedRuleTest_0100 - * @tc.name: test to_json interface in DisposedRule. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_1200, Function | SmallTest | Level0) -{ - DisposedRule disPosedRule; - ElementName element; - element.SetBundleName(BUNDLE_NAME); - element.SetModuleName(MODULE_NAME); - element.SetAbilityName(ABILITY_NAME); - element.SetDeviceID(DEVICE_ID); - nlohmann::json jsonObject; - to_json(jsonObject, element); - EXPECT_EQ(element.GetBundleName(), BUNDLE_NAME); - EXPECT_EQ(element.GetDeviceID(), DEVICE_ID); - EXPECT_EQ(element.GetAbilityName(), ABILITY_NAME); - EXPECT_EQ(element.GetModuleName(), MODULE_NAME); -} - -/** - * @tc.number: BmsDisposedRuleTest_0200 - * @tc.name: test to_json interface in DisposedRule. - */ -HWTEST_F(BmsHapModuleInfoTest, to_json_1300, Function | SmallTest | Level0) -{ - DisposedRule disPosedRule; - nlohmann::json jsonObject; - std::vector elementList; - std::shared_ptr want = std::make_shared(); - ASSERT_NE(want, nullptr); - disPosedRule.want = want; - elementList.emplace_back(BUNDLE_NAME, ABILITY_NAME, DEVICE_ID); - to_json(jsonObject, disPosedRule); - EXPECT_EQ(disPosedRule.componentType, ComponentType::UI_ABILITY); - EXPECT_EQ(disPosedRule.controlType, ControlType::ALLOWED_LIST); - EXPECT_EQ(disPosedRule.disposedType, DisposedType::BLOCK_APPLICATION); -} - -/** - * @tc.number: BmsDisposedRuleTest_0300 - * @tc.name: test from_json interface in DisposedRule. - */ -HWTEST_F(BmsHapModuleInfoTest, from_json_0900, Function | SmallTest | Level0) -{ - DisposedRule disPosedRule; - ElementName element; - element.SetBundleName(BUNDLE_NAME); - element.SetModuleName(MODULE_NAME); - element.SetAbilityName(ABILITY_NAME); - element.SetDeviceID(DEVICE_ID); - nlohmann::json jsonObject; - ElementName result; - from_json(jsonObject, result); - EXPECT_NE(element.GetBundleName(), result.bundleName_); - EXPECT_NE(element.GetDeviceID(), result.deviceId_); - EXPECT_NE(element.GetAbilityName(), result.abilityName_); - EXPECT_NE(element.GetModuleName(), result.moduleName_); -} - -/** - * @tc.number: BmsDisposedRuleTest_0400 - * @tc.name: test from_json interface in DisposedRule. - */ -HWTEST_F(BmsHapModuleInfoTest, from_json_1000, Function | SmallTest | Level0) -{ - DisposedRule disPosedRule; - nlohmann::json jsonObject; - std::shared_ptr want = std::make_shared(); - ASSERT_NE(want, nullptr); - disPosedRule.want = want; - std::vector elementList; - elementList.emplace_back(BUNDLE_NAME, ABILITY_NAME, DEVICE_ID); - DisposedRule result; - from_json(jsonObject, result); - EXPECT_EQ(result.componentType, ComponentType::UI_ABILITY); - EXPECT_EQ(result.controlType, ControlType::ALLOWED_LIST); - EXPECT_EQ(result.disposedType, DisposedType::BLOCK_APPLICATION); -} - -/** - * @tc.number: BmsDisposedRuleTest_0500 - * @tc.name: test FromString interface in DisposedRule. - */ -HWTEST_F(BmsHapModuleInfoTest, FromString_0100, Function | SmallTest | Level0) -{ - DisposedRule disPosedRule; - nlohmann::json jsonObject; - std::string ruleString = "rule"; - DisposedRule rule; - bool result = disPosedRule.FromString(ruleString, rule); - EXPECT_FALSE(result); -} - -/** - * @tc.number: BmsHapModuleInfoTest_DeleteDir_0100 - * @tc.name: test DeleteDir when path is a valid directory. - */ -HWTEST_F(BmsHapModuleInfoTest, DeleteDir_0100, Function | SmallTest | Level0) -{ - std::string dirPath = "/tmp/testdir"; - mkdir(dirPath.c_str(), 0777); - auto ret = BundleFileUtil::DeleteDir(dirPath); - EXPECT_TRUE(ret); - EXPECT_FALSE(BundleFileUtil::IsExistDir(dirPath)); -} - -/** - * @tc.number: BmsHapModuleInfoTest_DeleteDir_0200 - * @tc.name: test DeleteDir when path is invalid or directory deletion fails. - */ -HWTEST_F(BmsHapModuleInfoTest, DeleteDir_0200, Function | SmallTest | Level0) -{ - std::string invalidPath = "/tmp/nonexistentpath"; - auto ret = BundleFileUtil::DeleteDir(invalidPath); - EXPECT_TRUE(ret); - std::string dirPath = "/tmp/testdir"; - mkdir(dirPath.c_str(), 0777); - errno = EACCES; - auto ret1 = BundleFileUtil::DeleteDir(dirPath); - EXPECT_TRUE(ret1); -} -} +/* + * Copyright (c) 2024 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. + */ +#define private public +#define protected public +#include +#include +#include + +#include "app_jump_control_rule.h" +#include "application_info.h" +#include "bundle_file_util.h" +#include "bundle_pack_info.h" +#include "code_protect_bundle_info.h" +#include "data_group_info.h" +#include "disposed_rule.h" +#include "extension_ability_info.h" +#include "form_info.h" +#include "hap_module_info.h" +#include "json_util.h" +#include "preinstalled_application_info.h" +#include "nlohmann/json.hpp" +#include "skill.h" +#include "recoverable_application_info.h" +#include "want.h" + +using namespace testing::ext; +using OHOS::AAFwk::Want; + +namespace OHOS { +namespace AppExecFwk { + +const char* PRELOAD_ITEM_MODULE_NAME = "moduleName"; +const uint32_t HAP_MODULE_INFO_VERSION_CODE = 0; +constexpr const char* BUNDLE_NAME = "bundleName"; +constexpr const char* MODULE_NAME = "moduleName"; +const char* ROUTER_ITEM_KEY_NAME = "name"; +const char* ROUTER_ITEM_KEY_BUILD_FUNCTION = "buildFunction"; +const char* APP_ENVIRONMENTS_NAME = "name"; +const char* APP_ENVIRONMENTS_VALUE = "value"; +const uint32_t HAP_MODULE_INFO_LABEL_ID = 10; +const char* HAP_MODULE_INFO_HAP_PATH = "hapPath"; +const uint32_t JSON_KEY_LABEL_ID = 20; +const uint32_t JSON_KEY_ICON_ID = 30; +const std::string DATAGROUPID = "dataGroupId"; +const std::string UUID = "uuid"; +const uint32_t UID = 2; +const uint32_t GID = 3; +const uint32_t USERID = 4; +const int32_t ZERO_SIZE = 0; +const int32_t INVALIED_ID = -1; +const std::string HOST = "host"; +const std::string PORT = "port"; +const std::string PATH = "path"; +const std::string SCHEME = "scheme"; +const std::string PATHREGEX = "pathRegex"; +const std::string PORT1 = "port"; +const std::string PACKAGE = "package"; +const std::string TYPE = "type"; +const std::string NAME = "name"; +const std::string VALUE = "value"; +const std::string CALLERPKG = "caller"; +const std::string TARGETPKG = "target"; +const std::string CONTROLMESSAGE = "control"; +const std::string BUNDLENAME = "bundleName"; +const std::string MODULENAME = "moduleName"; +const std::uint32_t LABEL_ID = 10; +const std::uint32_t ICON_ID = 20; +constexpr int32_t JUMP_MODE = static_cast(AbilityJumpMode::DIRECT); +const std::string BUNDLE_NAME1 = "testBundleName"; +constexpr int32_t UID1 = 100; +constexpr int32_t APPINDEX = 10; +constexpr uint32_t VERSIONCODE = 20; +constexpr uint32_t APPLICATIONRESERVEDFLAG = 30; +const std::string BUNDLE_NAME2 = "testBundleName"; +const std::string MODULE_NAME2 = "testModuleName"; +const std::string ABILITY_NAME = "testAbilityName"; +const char* DEVICE_ID = "deviceId"; + +void to_json(nlohmann::json &jsonObject, const PreloadItem &preloadItem); +void from_json(const nlohmann::json &jsonObject, PreloadItem &preloadItem); +void to_json(nlohmann::json &jsonObject, const Dependency &dependency); +void from_json(const nlohmann::json &jsonObject, Dependency &dependency); +void to_json(nlohmann::json &jsonObject, const RouterItem &routerItem); +void from_json(const nlohmann::json &jsonObject, RouterItem &routerItem); +void to_json(nlohmann::json &jsonObject, const AppEnvironment &appEnvironment); +void from_json(const nlohmann::json &jsonObject, AppEnvironment &appEnvironment); +void to_json(nlohmann::json &jsonObject, const HapModuleInfo &hapModuleInfo); +void to_json(nlohmann::json &jsonObject, const RecoverableApplicationInfo &recoverableApplicationInfo); +void from_json(const nlohmann::json &jsonObject, RecoverableApplicationInfo &recoverableApplicationInfo); +void to_json(nlohmann::json &jsonObject, const ApplicationEnvironment &applicationEnvironment); +void from_json(const nlohmann::json &jsonObject, ApplicationEnvironment &applicationEnvironment); +void to_json(nlohmann::json &jsonObject, const HnpPackage &hnpPackage); +void from_json(const nlohmann::json &jsonObject, HnpPackage &hnpPackage); +void to_json(nlohmann::json &jsonObject, const FormCustomizeData &customizeDatas); +void to_json(nlohmann::json &jsonObject, const CodeProtectBundleInfo &CodeProtectBundleInfo); +void from_json(const nlohmann::json &jsonObject, CodeProtectBundleInfo &CodeProtectBundleInfo); +void to_json(nlohmann::json &jsonObject, const ElementName &elementName); +void from_json(const nlohmann::json &jsonObject, ElementName &elementName); +void to_json(nlohmann::json &jsonObject, const DisposedRule &disposedRule); +void from_json(const nlohmann::json &jsonObject, DisposedRule &disposedRule); + +class BmsHapModuleInfoTest : public testing::Test { +public: + static void SetUpTestCase(); + static void TearDownTestCase(); + void SetUp(); + void TearDown(); +}; + +void BmsHapModuleInfoTest::SetUpTestCase() +{} + +void BmsHapModuleInfoTest::TearDownTestCase() +{} + +void BmsHapModuleInfoTest::SetUp() +{} + +void BmsHapModuleInfoTest::TearDown() +{} + +/** + * @tc.number: BmsHapModuleInfoTest_0100 + * @tc.name: test to_json interface in HapModuleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_0100, Function | SmallTest | Level0) +{ + PreloadItem preloadItem; + preloadItem.moduleName = "moduleName"; + nlohmann::json jsonObject; + to_json(jsonObject, preloadItem); + EXPECT_EQ(preloadItem.moduleName, PRELOAD_ITEM_MODULE_NAME); +} + +/** + * @tc.number: BmsHapModuleInfoTest_0200 + * @tc.name: test from_json interface in HapModuleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, from_json_0100, Function | SmallTest | Level0) +{ + PreloadItem preloadItem; + preloadItem.moduleName = "moduleName"; + nlohmann::json jsonObject; + PreloadItem result; + from_json(jsonObject, result); + EXPECT_NE(preloadItem.moduleName, result.moduleName); +} + +/** + * @tc.number: BmsHapModuleInfoTest_0300 + * @tc.name: test to_json interface in HapModuleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_0200, Function | SmallTest | Level0) +{ + Dependency dependency; + dependency.bundleName = "bundleName"; + dependency.moduleName = "moduleName"; + dependency.versionCode = 0; + nlohmann::json jsonObject; + to_json(jsonObject, dependency); + EXPECT_EQ(dependency.bundleName, BUNDLE_NAME); + EXPECT_EQ(dependency.moduleName, MODULE_NAME); + EXPECT_EQ(dependency.versionCode, HAP_MODULE_INFO_VERSION_CODE); +} + +/** + * @tc.number: BmsHapModuleInfoTest_0400 + * @tc.name: test from_json interface in HapModuleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, from_json_0200, Function | SmallTest | Level0) +{ + Dependency dependency; + dependency.bundleName = "bundleName"; + dependency.moduleName = "moduleName"; + dependency.versionCode = 0; + nlohmann::json jsonObject; + Dependency result; + from_json(jsonObject, result); + EXPECT_NE(dependency.bundleName, result.bundleName); + EXPECT_NE(dependency.moduleName, result.moduleName); + EXPECT_EQ(dependency.versionCode, result.versionCode); +} + +/** + * @tc.number: BmsHapModuleInfoTest_0500 + * @tc.name: test to_json interface in HapModuleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_0300, Function | SmallTest | Level0) +{ + RouterItem routerItem; + routerItem.name = "name"; + routerItem.buildFunction = "buildFunction"; + nlohmann::json jsonObject; + to_json(jsonObject, routerItem); + EXPECT_EQ(routerItem.name, ROUTER_ITEM_KEY_NAME); + EXPECT_EQ(routerItem.buildFunction, ROUTER_ITEM_KEY_BUILD_FUNCTION); +} + +/** + * @tc.number: BmsHapModuleInfoTest_0600 + * @tc.name: test from_json interface in HapModuleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, from_json_0300, Function | SmallTest | Level0) +{ + RouterItem routerItem; + routerItem.name = "name"; + routerItem.buildFunction = "buildFunction"; + nlohmann::json jsonObject; + RouterItem result; + from_json(jsonObject, result); + EXPECT_NE(routerItem.name, result.name); + EXPECT_NE(routerItem.buildFunction, result.buildFunction); +} + +/** + * @tc.number: BmsHapModuleInfoTest_0700 + * @tc.name: test Marshalling interface in HapModuleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, Marshalling_0100, Function | SmallTest | Level0) +{ + AppEnvironment appEnvironment; + Parcel parcel; + std::string name = "IsName"; + std::string value = "value"; + parcel.WriteString16(Str8ToStr16(name)); + parcel.WriteString16(Str8ToStr16(value)); + auto ret = appEnvironment.Marshalling(parcel); + EXPECT_TRUE(ret); +} + +/** + * @tc.number: BmsHapModuleInfoTest_0800 + * @tc.name: test to_json interface in HapModuleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_0400, Function | SmallTest | Level0) +{ + AppEnvironment appEnvironment; + appEnvironment.name = "name"; + appEnvironment.value = "value"; + nlohmann::json jsonObject; + to_json(jsonObject, appEnvironment); + EXPECT_EQ(appEnvironment.name, APP_ENVIRONMENTS_NAME); + EXPECT_EQ(appEnvironment.value, APP_ENVIRONMENTS_VALUE); +} + +/** + * @tc.number: BmsHapModuleInfoTest_0900 + * @tc.name: test from_json interface in HapModuleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, from_json_0400, Function | SmallTest | Level0) +{ + AppEnvironment appEnvironment; + appEnvironment.name = "name"; + appEnvironment.value = "value"; + nlohmann::json jsonObject; + AppEnvironment result; + from_json(jsonObject, result); + EXPECT_NE(appEnvironment.name, result.name); + EXPECT_NE(appEnvironment.value, result.value); +} + +/** + * @tc.number: BmsHapModuleInfoTest_1000 + * @tc.name: test to_json interface in HapModuleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_0500, Function | SmallTest | Level0) +{ + HapModuleInfo hapModuleInfo; + hapModuleInfo.labelId = 10; + hapModuleInfo.hapPath = "hapPath"; + nlohmann::json jsonObject; + to_json(jsonObject, hapModuleInfo); + EXPECT_EQ(hapModuleInfo.labelId, HAP_MODULE_INFO_LABEL_ID); + EXPECT_EQ(hapModuleInfo.hapPath, HAP_MODULE_INFO_HAP_PATH); +} + +/** + * @tc.number: BmsHapModuleInfoTest_1100 + * @tc.name: test to_json interface in RecoverableApplicationInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_0600, Function | SmallTest | Level0) +{ + RecoverableApplicationInfo recoverableApplicationInfo; + recoverableApplicationInfo.labelId = 20; + recoverableApplicationInfo.iconId = 30; + nlohmann::json jsonObject; + to_json(jsonObject, recoverableApplicationInfo); + EXPECT_EQ(recoverableApplicationInfo.labelId, JSON_KEY_LABEL_ID); + EXPECT_EQ(recoverableApplicationInfo.iconId, JSON_KEY_ICON_ID); +} + +/** + * @tc.number: BmsHapModuleInfoTest_1200 + * @tc.name: test from_json interface in RecoverableApplicationInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, from_json_0500, Function | SmallTest | Level0) +{ + RecoverableApplicationInfo recoverableApplicationInfo; + recoverableApplicationInfo.labelId = 20; + recoverableApplicationInfo.iconId = 30; + nlohmann::json jsonObject; + RecoverableApplicationInfo result; + from_json(jsonObject, result); + EXPECT_NE(recoverableApplicationInfo.labelId, result.labelId); + EXPECT_NE(recoverableApplicationInfo.iconId, result.iconId); +} + +/** + * @tc.number: BmsHapModuleInfoTest_1300 + * @tc.name: test ReadFromParcel interface in DataGroupInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, ReadFromParcel_0100, Function | SmallTest | Level0) +{ + DataGroupInfo dataGroupInfo; + Parcel parcel; + parcel.WriteString16(Str8ToStr16(DATAGROUPID)); + parcel.WriteString16(Str8ToStr16(UUID)); + parcel.WriteInt32(UID); + parcel.WriteInt32(GID); + parcel.WriteInt32(USERID); + bool result = dataGroupInfo.ReadFromParcel(parcel); + EXPECT_TRUE(result); +} + +/** + * @tc.number: BmsHapModuleInfoTest_1400 + * @tc.name: test Marshalling interface in DataGroupInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, Marshalling_0200, Function | SmallTest | Level0) +{ + DataGroupInfo dataGroupInfo; + Parcel parcel; + parcel.WriteString16(Str8ToStr16(DATAGROUPID)); + parcel.WriteString16(Str8ToStr16(UUID)); + parcel.WriteInt32(UID); + parcel.WriteInt32(GID); + parcel.WriteInt32(USERID); + auto ret = dataGroupInfo.Marshalling(parcel); + EXPECT_TRUE(ret); +} + +/** + * @tc.number: BmsHapModuleInfoTest_1500 + * @tc.name: test Unmarshalling interface in DataGroupInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, Unmarshalling_0100, Function | SmallTest | Level0) +{ + DataGroupInfo dataGroupInfo; + std::shared_ptr info = + std::make_shared(); + ASSERT_NE(info, nullptr); + Parcel parcel; + parcel.WriteString16(Str8ToStr16(DATAGROUPID)); + parcel.WriteString16(Str8ToStr16(UUID)); + parcel.WriteInt32(UID); + parcel.WriteInt32(GID); + parcel.WriteInt32(USERID); + auto result = dataGroupInfo.Unmarshalling(parcel); + ASSERT_NE(result, nullptr); +} + +/** + * @tc.number: BmsHapModuleInfoTest_1600 + * @tc.name: test Marshalling interface in Skill. + */ +HWTEST_F(BmsHapModuleInfoTest, Marshalling_0300, Function | SmallTest | Level0) +{ + Skill skill; + Parcel parcel; + parcel.WriteString16(Str8ToStr16(HOST)); + parcel.WriteString16(Str8ToStr16(PATH)); + parcel.WriteString16(Str8ToStr16(PORT)); + auto ret = skill.Marshalling(parcel); + EXPECT_TRUE(ret); +} + +/** + * @tc.number: BmsHapModuleInfoTest_1700 + * @tc.name: test Dump interface in Skill. + */ +HWTEST_F(BmsHapModuleInfoTest, BundleMgrHostImpl_0500, Function | MediumTest | Level1) +{ + Skill skill; + std::string prefix = "prefix"; + int fd = 2; + skill.Dump(prefix, fd); + long length = lseek(fd, ZERO_SIZE, SEEK_END); + EXPECT_EQ(length, INVALIED_ID); +} + +/** + * @tc.number: BmsHapModuleInfoTest_1800 + * @tc.name: test MarshallingSkillUri interface in ExtensionAbilityInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, MarshallingSkillUri_0100, Function | SmallTest | Level0) +{ + ExtensionAbilityInfo extensionAbilityInfo; + Parcel parcel; + SkillUriForAbilityAndExtension uri; + uri.maxFileSupported = 0; + parcel.WriteString16(Str8ToStr16(SCHEME)); + parcel.WriteString16(Str8ToStr16(PATHREGEX)); + parcel.WriteString16(Str8ToStr16(PORT1)); + auto ret = extensionAbilityInfo.MarshallingSkillUri(parcel, uri); + EXPECT_TRUE(ret); +} + +/** + * @tc.number: BmsHapModuleInfoTest_1900 + * @tc.name: test Marshalling interface in BundlePackInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, Marshalling_0400, Function | SmallTest | Level1) +{ + BundlePackInfo bundlePackInfo; + Parcel parcel; + auto moduleType = static_cast(ModuleType::UNKNOWN); + parcel.WriteInt32(moduleType); + auto ret = bundlePackInfo.Marshalling(parcel); + EXPECT_TRUE(ret); +} + +/** + * @tc.number: BmsHapModuleInfoTest_2000 + * @tc.name: test to_json interface in ApplicationInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_0700, Function | SmallTest | Level0) +{ + ApplicationEnvironment applicationEnvironment; + applicationEnvironment.name = "name"; + applicationEnvironment.value = "value"; + nlohmann::json jsonObject; + to_json(jsonObject, applicationEnvironment); + EXPECT_EQ(applicationEnvironment.name, NAME); + EXPECT_EQ(applicationEnvironment.value, VALUE); +} + +/** + * @tc.number: BmsHapModuleInfoTest_2100 + * @tc.name: test from_json interface in ApplicationInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, from_json_0600, Function | SmallTest | Level0) +{ + ApplicationEnvironment applicationEnvironment; + applicationEnvironment.name = "name"; + applicationEnvironment.value = "value"; + nlohmann::json jsonObject; + ApplicationEnvironment result; + from_json(jsonObject, result); + EXPECT_NE(applicationEnvironment.name, result.name); + EXPECT_NE(applicationEnvironment.value, result.value); +} + +/** + * @tc.number: BmsHapModuleInfoTest_2200 + * @tc.name: test to_json interface in ApplicationInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_0800, Function | SmallTest | Level0) +{ + HnpPackage hnpPackage; + hnpPackage.package = "package"; + hnpPackage.type = "type"; + nlohmann::json jsonObject; + to_json(jsonObject, hnpPackage); + EXPECT_EQ(hnpPackage.package, PACKAGE); + EXPECT_EQ(hnpPackage.type, TYPE); +} + +/** + * @tc.number: BmsHapModuleInfoTest_2300 + * @tc.name: test from_json interface in ApplicationInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, from_json_0700, Function | SmallTest | Level0) +{ + HnpPackage hnpPackage; + hnpPackage.package = "package"; + hnpPackage.type = "type"; + nlohmann::json jsonObject; + HnpPackage result; + from_json(jsonObject, result); + EXPECT_NE(hnpPackage.package, result.package); + EXPECT_NE(hnpPackage.type, result.type); +} + +/** + * @tc.number: BmsHapModuleInfoTest_2400 + * @tc.name: test to_json interface in FormInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_0900, Function | SmallTest | Level0) +{ + FormCustomizeData customizeDatas; + customizeDatas.name = "name"; + customizeDatas.value = "value"; + nlohmann::json jsonObject; + to_json(jsonObject, customizeDatas); + EXPECT_EQ(customizeDatas.name, NAME); + EXPECT_EQ(customizeDatas.value, VALUE); +} + +/** + * @tc.number: BmsHapModuleInfoTest_2500 + * @tc.name: test IsExistDir interface in BundleFileUtil. + */ +HWTEST_F(BmsHapModuleInfoTest, IsExistDir_0100, Function | SmallTest | Level0) +{ + std::string dirPath = "PATH"; + auto ret = BundleFileUtil::IsExistDir(dirPath); + EXPECT_FALSE(ret); +} + +/** + * @tc.number: BmsAppJumpControlRuleTest_0100 + * @tc.name: test to_json interface in DisposedRule. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_1000, Function | SmallTest | Level0) +{ + AppJumpControlRule appJumpControlRule; + Parcel parcel; + parcel.WriteString16(Str8ToStr16(CALLERPKG)); + parcel.WriteString16(Str8ToStr16(TARGETPKG)); + parcel.WriteString16(Str8ToStr16(CONTROLMESSAGE)); + parcel.WriteInt32(JUMP_MODE); + bool result = appJumpControlRule.ReadFromParcel(parcel); + EXPECT_TRUE(result); +} + +/** + * @tc.number: BmsAppJumpControlRuleTest_0200 + * @tc.name: test Unmarshalling interface in DisposedRule. + */ +HWTEST_F(BmsHapModuleInfoTest, Unmarshalling_0200, Function | SmallTest | Level0) +{ + AppJumpControlRule appJumpControlRule; + std::shared_ptr info = + std::make_shared(); + ASSERT_NE(info, nullptr); + Parcel parcel; + parcel.WriteString16(Str8ToStr16(CALLERPKG)); + parcel.WriteString16(Str8ToStr16(TARGETPKG)); + parcel.WriteString16(Str8ToStr16(CONTROLMESSAGE)); + parcel.WriteInt32(JUMP_MODE); + auto result = appJumpControlRule.Unmarshalling(parcel); + ASSERT_NE(result, nullptr); +} + +/** + * @tc.number: BmsAppJumpControlRuleTest_0300 + * @tc.name: test Marshalling interface in DisposedRule. + */ +HWTEST_F(BmsHapModuleInfoTest, Marshalling_0500, Function | SmallTest | Level0) +{ + PreinstalledApplicationInfo preinstalledApplicationInfo; + Parcel parcel; + parcel.WriteString16(Str8ToStr16(BUNDLENAME)); + parcel.WriteString16(Str8ToStr16(MODULENAME)); + parcel.WriteUint32(LABEL_ID); + parcel.WriteUint32(ICON_ID); + auto ret = preinstalledApplicationInfo.Marshalling(parcel); + EXPECT_TRUE(ret); +} + +/** + * @tc.number: BmsCodeProtectBundleInfoTest_0100 + * @tc.name: test ReadFromParcel interface in CodeProtectBundleInfo. + * @tc.desc: 1.construct parcel. + * 2.calling ReadFromParcel interface by using input parameter parcel. + */ +HWTEST_F(BmsHapModuleInfoTest, ReadFromParcel_0200, Function | SmallTest | Level0) +{ + CodeProtectBundleInfo codeProtectBundleInfo; + Parcel parcel; + parcel.WriteString16(Str8ToStr16(BUNDLE_NAME1)); + parcel.WriteInt32(UID1); + parcel.WriteInt32(APPINDEX); + parcel.WriteUint32(VERSIONCODE); + parcel.WriteInt32(APPLICATIONRESERVEDFLAG); + bool result = codeProtectBundleInfo.ReadFromParcel(parcel); + EXPECT_TRUE(result); +} + +/** + * @tc.number: BmsCodeProtectBundleInfoTest_0200 + * @tc.name: test to_json interface in CodeProtectBundleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_1100, Function | SmallTest | Level0) +{ + CodeProtectBundleInfo codeProtectBundleInfo; + codeProtectBundleInfo.bundleName = "TestBundleName"; + codeProtectBundleInfo.uid = 10; + codeProtectBundleInfo.appIndex = 20; + codeProtectBundleInfo.versionCode = 30; + codeProtectBundleInfo.applicationReservedFlag = 40; + nlohmann::json jsonObject; + to_json(jsonObject, codeProtectBundleInfo); + CodeProtectBundleInfo result; + from_json(jsonObject, result); + EXPECT_EQ(result.bundleName, "TestBundleName"); + EXPECT_EQ(result.uid, 10); + EXPECT_EQ(result.appIndex, 20); + EXPECT_EQ(result.versionCode, 30); + EXPECT_EQ(result.applicationReservedFlag, 40); +} + +/** + * @tc.number: BmsCodeProtectBundleInfoTest_0300 + * @tc.name: test to_json interface in CodeProtectBundleInfo. + */ +HWTEST_F(BmsHapModuleInfoTest, from_json_0800, Function | SmallTest | Level0) +{ + CodeProtectBundleInfo codeProtectBundleInfo; + codeProtectBundleInfo.bundleName = "TestBundleName"; + codeProtectBundleInfo.uid = 10; + codeProtectBundleInfo.appIndex = 20; + codeProtectBundleInfo.versionCode = 30; + codeProtectBundleInfo.applicationReservedFlag = 40; + nlohmann::json jsonObject; + CodeProtectBundleInfo result; + from_json(jsonObject, result); + EXPECT_NE(codeProtectBundleInfo.bundleName, result.bundleName); + EXPECT_NE(codeProtectBundleInfo.uid, result.uid); + EXPECT_NE(codeProtectBundleInfo.appIndex, result.appIndex); + EXPECT_NE(codeProtectBundleInfo.versionCode, result.versionCode); + EXPECT_NE(codeProtectBundleInfo.applicationReservedFlag, result.applicationReservedFlag); +} + +/** + * @tc.number: BmsDisposedRuleTest_0100 + * @tc.name: test to_json interface in DisposedRule. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_1200, Function | SmallTest | Level0) +{ + DisposedRule disPosedRule; + ElementName element; + element.SetBundleName(BUNDLE_NAME); + element.SetModuleName(MODULE_NAME); + element.SetAbilityName(ABILITY_NAME); + element.SetDeviceID(DEVICE_ID); + nlohmann::json jsonObject; + to_json(jsonObject, element); + EXPECT_EQ(element.GetBundleName(), BUNDLE_NAME); + EXPECT_EQ(element.GetDeviceID(), DEVICE_ID); + EXPECT_EQ(element.GetAbilityName(), ABILITY_NAME); + EXPECT_EQ(element.GetModuleName(), MODULE_NAME); +} + +/** + * @tc.number: BmsDisposedRuleTest_0200 + * @tc.name: test to_json interface in DisposedRule. + */ +HWTEST_F(BmsHapModuleInfoTest, to_json_1300, Function | SmallTest | Level0) +{ + DisposedRule disPosedRule; + nlohmann::json jsonObject; + std::vector elementList; + std::shared_ptr want = std::make_shared(); + ASSERT_NE(want, nullptr); + disPosedRule.want = want; + elementList.emplace_back(BUNDLE_NAME, ABILITY_NAME, DEVICE_ID); + to_json(jsonObject, disPosedRule); + EXPECT_EQ(disPosedRule.componentType, ComponentType::UI_ABILITY); + EXPECT_EQ(disPosedRule.controlType, ControlType::ALLOWED_LIST); + EXPECT_EQ(disPosedRule.disposedType, DisposedType::BLOCK_APPLICATION); +} + +/** + * @tc.number: BmsDisposedRuleTest_0300 + * @tc.name: test from_json interface in DisposedRule. + */ +HWTEST_F(BmsHapModuleInfoTest, from_json_0900, Function | SmallTest | Level0) +{ + DisposedRule disPosedRule; + ElementName element; + element.SetBundleName(BUNDLE_NAME); + element.SetModuleName(MODULE_NAME); + element.SetAbilityName(ABILITY_NAME); + element.SetDeviceID(DEVICE_ID); + nlohmann::json jsonObject; + ElementName result; + from_json(jsonObject, result); + EXPECT_NE(element.GetBundleName(), result.bundleName_); + EXPECT_NE(element.GetDeviceID(), result.deviceId_); + EXPECT_NE(element.GetAbilityName(), result.abilityName_); + EXPECT_NE(element.GetModuleName(), result.moduleName_); +} + +/** + * @tc.number: BmsDisposedRuleTest_0400 + * @tc.name: test from_json interface in DisposedRule. + */ +HWTEST_F(BmsHapModuleInfoTest, from_json_1000, Function | SmallTest | Level0) +{ + DisposedRule disPosedRule; + nlohmann::json jsonObject; + std::shared_ptr want = std::make_shared(); + ASSERT_NE(want, nullptr); + disPosedRule.want = want; + std::vector elementList; + elementList.emplace_back(BUNDLE_NAME, ABILITY_NAME, DEVICE_ID); + DisposedRule result; + from_json(jsonObject, result); + EXPECT_EQ(result.componentType, ComponentType::UI_ABILITY); + EXPECT_EQ(result.controlType, ControlType::ALLOWED_LIST); + EXPECT_EQ(result.disposedType, DisposedType::BLOCK_APPLICATION); +} + +/** + * @tc.number: BmsDisposedRuleTest_0500 + * @tc.name: test FromString interface in DisposedRule. + */ +HWTEST_F(BmsHapModuleInfoTest, FromString_0100, Function | SmallTest | Level0) +{ + DisposedRule disPosedRule; + nlohmann::json jsonObject; + std::string ruleString = "rule"; + DisposedRule rule; + bool result = disPosedRule.FromString(ruleString, rule); + EXPECT_FALSE(result); +} + +/** + * @tc.number: BmsHapModuleInfoTest_DeleteDir_0100 + * @tc.name: test DeleteDir when path is a valid directory. + */ +HWTEST_F(BmsHapModuleInfoTest, DeleteDir_0100, Function | SmallTest | Level0) +{ + std::string dirPath = "/tmp/testdir"; + mkdir(dirPath.c_str(), 0777); + auto ret = BundleFileUtil::DeleteDir(dirPath); + EXPECT_TRUE(ret); + EXPECT_FALSE(BundleFileUtil::IsExistDir(dirPath)); +} + +/** + * @tc.number: BmsHapModuleInfoTest_DeleteDir_0200 + * @tc.name: test DeleteDir when path is invalid or directory deletion fails. + */ +HWTEST_F(BmsHapModuleInfoTest, DeleteDir_0200, Function | SmallTest | Level0) +{ + std::string invalidPath = "/tmp/nonexistentpath"; + auto ret = BundleFileUtil::DeleteDir(invalidPath); + EXPECT_TRUE(ret); + std::string dirPath = "/tmp/testdir"; + mkdir(dirPath.c_str(), 0777); + errno = EACCES; + auto ret1 = BundleFileUtil::DeleteDir(dirPath); + EXPECT_TRUE(ret1); +} +} } \ No newline at end of file diff --git a/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp b/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp index 694d47aa282cbfe834919be4226f91968dfc36e5..76c013b4c42114926581e863786a7ee5652f9442 100755 --- a/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp +++ b/services/bundlemgr/test/unittest/bms_rdb_data_manager_test/bms_rdb_data_manager_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022-2024 Huawei Device Co., Ltd. + * Copyright (c) 2022-2025 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 @@ -15,6 +15,8 @@ #define private public #include +#include +#include #include "bundle_constants.h" #include "bundle_data_storage_rdb.h" @@ -25,6 +27,7 @@ #include "first_install_data_mgr_storage_rdb.h" #include "preinstall_data_storage_rdb.h" #include "rdb_data_manager.h" +#include "bundle_file_util.h" using namespace testing::ext; using namespace OHOS::AppExecFwk; @@ -46,6 +49,7 @@ const uint32_t TEST_VERSION = 1; const std::string TEST_BUNDLE_NAME_TWO = "com.test.rdbtwo"; const std::string TEST_NAME_TWO = "NameTwo"; const uint32_t TEST_VERSION_TWO = 2; +constexpr const char* PARTITION_NAME = "/data"; #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP const int32_t TEST_USERID = 500; const std::string TEST_DEFAULT_APP_TYPE = "IMAGE"; @@ -692,6 +696,24 @@ HWTEST_F(BmsRdbDataManagerTest, RdbDataManager_1200, Function | SmallTest | Leve ASSERT_NE(rdbStore, nullptr); } + +/** + * @tc.number: RdbDataManager_1300 + * @tc.name: test ReportReportRdbPartitionUsageEvent + * @tc.desc: 1.test ReportReportRdbPartitionUsageEvent + */ +HWTEST_F(BmsRdbDataManagerTest, ReportReportRdbPartitionUsageEvent_020, Function | SmallTest | Level1) +{ + bool result = BundleFileUtil::IsReportDataPartitionUsageEvent(PARTITION_NAME); + EXPECT_EQ(result, false); + uint64_t bytes = BundleFileUtil::GetFreeSpaceInBytes(PARTITION_NAME); + struct statvfs stat; + EXPECT_EQ (bytes, static_cast(stat.f_bavail) * stat.f_frsize); + uint64_t size = BundleFileUtil::GetFileSize(PARTITION_NAME); + struct stat fileInfo = { 0 }; + EXPECT_EQ(size, fileInfo.st_size); +} + #ifdef BUNDLE_FRAMEWORK_DEFAULT_APP /** * @tc.number: DefaultAppRdb_0100