diff --git a/bundle.json b/bundle.json index a33e05268a261c60778f6285e0114e294aad6267..c2baa2b99f9308bdc374971e3e84f121bd7bb6fd 100644 --- a/bundle.json +++ b/bundle.json @@ -46,7 +46,8 @@ "libxml2", "preferences", "safwk", - "selinux_adapter" + "selinux_adapter", + "config_policy" ] }, "build": { diff --git a/services/samgr/native/BUILD.gn b/services/samgr/native/BUILD.gn index 9df0fad0421e59e2d727145ff942ccba00988762..96b4291bc6d2a54839776ea3d19a3b4e1c183648 100644 --- a/services/samgr/native/BUILD.gn +++ b/services/samgr/native/BUILD.gn @@ -97,6 +97,7 @@ ohos_executable("samgr") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "ffrt:libffrt", "hilog:libhilog", "hisysevent:libhisysevent", diff --git a/services/samgr/native/include/system_ability_manager_util.h b/services/samgr/native/include/system_ability_manager_util.h index eefc65f5ab88d0106a8175bc07db5be4abdcc526..86272369039c53a0a9ebd852c92d1c9ef6a47e3c 100644 --- a/services/samgr/native/include/system_ability_manager_util.h +++ b/services/samgr/native/include/system_ability_manager_util.h @@ -19,6 +19,8 @@ #include #include +#include "config_policy_utils.h" +#include "directory_ex.h" #include "sa_profiles.h" #include "system_ability_on_demand_event.h" #include "ffrt_handler.h" @@ -46,6 +48,7 @@ public: static void InvalidateSACache(); static void FilterCommonSaProfile(const SaProfile& oldProfile, CommonSaProfile& newProfile); static bool CheckPengLai(); + static void GetFilesByPriority(const std::string& path, std::vector& files); private: static std::shared_ptr setParmHandler_; }; diff --git a/services/samgr/native/source/system_ability_manager.cpp b/services/samgr/native/source/system_ability_manager.cpp index 563ea99c6eafe429c523e41071ec7264aad3496a..9143363a5222301acaf7c70c0f138c2c19c3e4e0 100644 --- a/services/samgr/native/source/system_ability_manager.cpp +++ b/services/samgr/native/source/system_ability_manager.cpp @@ -18,11 +18,11 @@ #include #include #include +#include #include "ability_death_recipient.h" #include "accesstoken_kit.h" #include "datetime_ex.h" -#include "directory_ex.h" #include "errors.h" #include "file_ex.h" #include "hisysevent_adapter.h" @@ -47,7 +47,7 @@ #include "device_manager.h" using namespace OHOS::DistributedHardware; #endif - +namespace fs = std::filesystem; using namespace std; namespace OHOS { @@ -55,7 +55,8 @@ namespace { #ifdef SUPPORT_DEVICE_MANAGER constexpr const char* PKG_NAME = "Samgr_Networking"; #endif -constexpr const char* PREFIX = "/system/profile/"; +constexpr const char* PREFIX = "profile"; +constexpr const char* SYSTEM_PREFIX = "/system/profile"; constexpr const char* LOCAL_DEVICE = "local"; constexpr const char* ONDEMAND_PARAM = "persist.samgr.perf.ondemand"; constexpr const char* DYNAMIC_CACHE_PARAM = "persist.samgr.cache.sa"; @@ -314,9 +315,12 @@ void SystemAbilityManager::InitSaProfile() { int64_t begin = GetTickCount(); std::vector fileNames; - GetDirFiles(PREFIX, fileNames); + SamgrUtil::GetFilesByPriority(PREFIX, fileNames); auto parser = std::make_shared(); for (const auto& file : fileNames) { + if (fs::path(file).parent_path().string() != SYSTEM_PREFIX) { + HILOGI("InitSaProfile file : %{public}s!", file.c_str()); + } if (file.empty() || file.find(".json") == std::string::npos || file.find("_trust.json") != std::string::npos) { continue; @@ -337,7 +341,7 @@ void SystemAbilityManager::InitSaProfile() for (const auto& saInfo : saInfos) { SamgrUtil::FilterCommonSaProfile(saInfo, saProfileMap_[saInfo.saId]); if (!saInfo.runOnCreate) { - HILOGI("InitProfile saId %{public}d", saInfo.saId); + HILOGD("InitProfile saId %{public}d", saInfo.saId); onDemandSaIdsSet_.insert(saInfo.saId); } } diff --git a/services/samgr/native/source/system_ability_manager_util.cpp b/services/samgr/native/source/system_ability_manager_util.cpp index 106d90df8fde2084aadd2a2584872a289f47a86f..f725718cc9c2ce3f7751ed8f21abccf5cbb603c3 100644 --- a/services/samgr/native/source/system_ability_manager_util.cpp +++ b/services/samgr/native/source/system_ability_manager_util.cpp @@ -25,6 +25,7 @@ #include "sam_log.h" namespace OHOS { +namespace fs = std::filesystem; using namespace std; constexpr int32_t MAX_NAME_SIZE = 200; constexpr int32_t SPLIT_NAME_VECTOR_SIZE = 2; @@ -39,6 +40,7 @@ constexpr const char* EVENT_EXTRA_DATA_ID = "extraDataId"; constexpr const char* MODULE_UPDATE_PARAM = "persist.samgr.moduleupdate"; constexpr const char* PENG_LAI_PARAM = "ohos.boot.minisys.mode"; constexpr const char* PENG_LAI = "penglai"; +constexpr const char* PENGLAI_PATH = "/sys_prod/profile/penglai"; std::shared_ptr SamgrUtil::setParmHandler_ = make_shared("setParmHandler"); bool SamgrUtil::IsNameInValid(const std::u16string& name) @@ -240,4 +242,33 @@ bool SamgrUtil::CheckPengLai() std::string paramValue = system::GetParameter(PENG_LAI_PARAM, defaultValue); return paramValue == PENG_LAI; } + +void SamgrUtil::GetFilesByPriority(const std::string& path, std::vector& fileNames) +{ + if (SamgrUtil::CheckPengLai()) { + HILOGI("GetFilesByPriority penglai!"); + GetDirFiles(PENGLAI_PATH, fileNames); + } else { + std::map fileNamesMap; + CfgFiles* filePaths = GetCfgFiles(path.c_str()); + for (int i = 0; filePaths && i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (filePaths->paths[i] != nullptr) { + HILOGI("GetFilesByPriority filePaths : %{public}s!", filePaths->paths[i]); + std::vector files; + GetDirFiles(filePaths->paths[i], files); + for (const auto& file : files) { + HILOGD("GetFilesByPriority file : %{public}s!", file.c_str()); + fileNamesMap[fs::path(file).filename().string()] = file; + } + } + } + + for (const auto& pair : fileNamesMap) { + HILOGD("GetFilesByPriority files : %{public}s!", pair.second.c_str()); + fileNames.push_back(pair.second); + } + + FreeCfgFiles(filePaths); + } +} } diff --git a/services/samgr/native/test/unittest/BUILD.gn b/services/samgr/native/test/unittest/BUILD.gn index 3ee0c5a4cb3f314483475a0a71d345ff94a9dd40..9182f21b99b5dca2ce30f4391600092bad12e9e6 100644 --- a/services/samgr/native/test/unittest/BUILD.gn +++ b/services/samgr/native/test/unittest/BUILD.gn @@ -103,6 +103,7 @@ ohos_unittest("SystemAbilityMgrTest") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "googletest:gtest_main", @@ -222,6 +223,7 @@ ohos_unittest("SystemAbilityMgrCollectTest") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "googletest:gtest_main", @@ -351,6 +353,7 @@ ohos_unittest("SystemAbilityMgrDeviceNetworkingTest") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "googletest:gtest_main", @@ -452,6 +455,7 @@ ohos_unittest("SystemAbilityMgrStubTest") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "googletest:gtest_main", @@ -653,6 +657,7 @@ ohos_unittest("SystemAbilityStateSchedulerTest") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "googletest:gtest_main", @@ -732,6 +737,7 @@ ohos_unittest("SystemAbilityMgrDumperTest") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "ffrt:libffrt", "googletest:gtest_main", "hilog:libhilog", @@ -995,6 +1001,7 @@ ohos_executable("TestTool") { external_deps += [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "hisysevent:libhisysevent", diff --git a/services/samgr/native/test/unittest/src/system_ability_mgr_util_test.cpp b/services/samgr/native/test/unittest/src/system_ability_mgr_util_test.cpp index 2718e814b773f71d5b9cf0ab163c6104ade79ade..56eeb5b1863a9eebb5b845e2e36a52f1fe98d45f 100644 --- a/services/samgr/native/test/unittest/src/system_ability_mgr_util_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_mgr_util_test.cpp @@ -37,6 +37,14 @@ namespace system { const std::u16string PROCESS_NAME = u"test_process_name"; constexpr const char* PENG_LAI = "penglai"; +struct CfgFiles +{ + const char* paths[MAX_CFG_POLICY_DIRS_CNT]; +}; + +CfgFiles* mockCfgFiles = nullptr; +std::vector mockDirFiles; + void InitSaMgr(sptr& saMgr) { saMgr->abilityDeath_ = sptr(new AbilityDeathRecipient()); @@ -49,6 +57,16 @@ void InitSaMgr(sptr& saMgr) saMgr->abilityStateScheduler_ = std::make_shared(); } +void GetDirFiles(const char* path, std::vector& files) +{ + files = mockDirFiles; +} + +CfgFiles* GetCfgFiles(const char* path) +{ + return mockCfgFiles; +} + void SamgrUtilTest::SetUpTestCase() { DTEST_LOG << "SetUpTestCase" << std::endl; @@ -63,6 +81,8 @@ void SamgrUtilTest::SetUp() { SamMockPermission::MockPermission(); system::mockValue = ""; + mockDirFiles.clear(); + mockCfgFiles = nullptr; DTEST_LOG << "SetUp" << std::endl; } @@ -388,4 +408,38 @@ HWTEST_F(SamgrUtilTest, CheckPengLai003, TestSize.Level3) system::mockValue = ""; EXPECT_FALSE(SamgrUtil::CheckPengLai()); } + +/** + * @tc.name: TestGetFilesByPriority001 + * @tc.desc: test penglai mode + * @tc.type: FUNC + */ +HWTEST_F(SamgrUtilTest, TestGetFilesByPriority001, TestSize.Level3) +{ + system::mockValue = PENG_LAI; + mockDirFiles = {"/sys_prod/profile/penglai/file1", "/sys_prod/profile/penglai/file2"}; + + std::vector result; + SamgrUtil::GetFilesByPriority("test_path", result); + + ASSERT_TRUE(result.empty()); + system::mockValue = ""; +} + +/** + * @tc.name: TestGetFilesByPriority002 + * @tc.desc: test empty paths + * @tc.type: FUNC + */ +HWTEST_F(SamgrUtilTest, TestGetFilesByPriority002, TestSize.Level3) +{ + system::mockValue = ""; + mockCfgFiles = new CfgFiles(); + + std::vector result; + SamgrUtil::GetFilesByPriority("test_path", result); + + ASSERT_TRUE(result.empty()); + delete mockCfgFiles; +} } \ No newline at end of file diff --git a/test/fuzztest/samgr_fuzzer/BUILD.gn b/test/fuzztest/samgr_fuzzer/BUILD.gn index 66181bb78d399202fd2c8c0e057cd6c037f8b42b..c204a9c51af14e126a477829260576e2368ce445 100644 --- a/test/fuzztest/samgr_fuzzer/BUILD.gn +++ b/test/fuzztest/samgr_fuzzer/BUILD.gn @@ -172,6 +172,7 @@ foreach(item, samgr_fuzztests) { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/samgrcoverage_fuzzer/BUILD.gn b/test/fuzztest/samgrcoverage_fuzzer/BUILD.gn index 20ee4009002b6360218529daf362e848dc9d33ba..5e03c4048048b2d596037522f5cc660c45df3ed5 100644 --- a/test/fuzztest/samgrcoverage_fuzzer/BUILD.gn +++ b/test/fuzztest/samgrcoverage_fuzzer/BUILD.gn @@ -75,6 +75,7 @@ ohos_fuzztest("SamgrCoverageFuzzTest") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/samgrdumper_fuzzer/BUILD.gn b/test/fuzztest/samgrdumper_fuzzer/BUILD.gn index 7048c5dd90ac3bef7e2e5f3093956a2756b72993..d8ff0e5d3414e5adee412e55f98100e086331e4b 100644 --- a/test/fuzztest/samgrdumper_fuzzer/BUILD.gn +++ b/test/fuzztest/samgrdumper_fuzzer/BUILD.gn @@ -74,6 +74,7 @@ ohos_fuzztest("SamgrDumperFuzzTest") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog", diff --git a/test/fuzztest/systemabilitymanager_fuzzer/BUILD.gn b/test/fuzztest/systemabilitymanager_fuzzer/BUILD.gn index d3181f4ea867b06d85899feac41e73ffa6ca845e..c85c65866dda6ff59a7a36de47144de3bb49101e 100644 --- a/test/fuzztest/systemabilitymanager_fuzzer/BUILD.gn +++ b/test/fuzztest/systemabilitymanager_fuzzer/BUILD.gn @@ -74,6 +74,7 @@ ohos_fuzztest("SystemAbilityManagerFuzzTest") { external_deps = [ "access_token:libaccesstoken_sdk", "c_utils:utils", + "config_policy:configpolicy_util", "eventhandler:libeventhandler", "ffrt:libffrt", "hilog:libhilog",