From 672680fe5b555df1bc770fb76d9cbbb2a6e9ce1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E8=B0=A2=E5=87=AF=E6=98=8E?= Date: Sat, 21 Jun 2025 10:47:39 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E6=94=B9customconfig=EF=BC=8C?= =?UTF-8?q?=E5=BC=B1=E8=80=A6=E5=90=88ability=5Fruntime?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 谢凯明 --- bundle.json | 2 +- config_policy.gni | 5 + .../config_policy/src/custom_config.cpp | 99 ++++++++++ interfaces/inner_api/include/custom_config.h | 36 ++++ interfaces/kits/js/BUILD.gn | 19 +- .../kits/js/include/custom_config_napi.h | 6 +- interfaces/kits/js/src/custom_config_napi.cpp | 99 ++-------- test/unittest/BUILD.gn | 28 ++- test/unittest/custom_config_test.cpp | 173 ++++++++++++++++++ 9 files changed, 363 insertions(+), 104 deletions(-) create mode 100644 frameworks/config_policy/src/custom_config.cpp create mode 100644 interfaces/inner_api/include/custom_config.h create mode 100644 test/unittest/custom_config_test.cpp diff --git a/bundle.json b/bundle.json index 3be48ec..7a2fc96 100644 --- a/bundle.json +++ b/bundle.json @@ -58,7 +58,7 @@ } ], "test": [ - "//base/customization/config_policy/test/unittest:ConfigPolicyUtilsTest", + "//base/customization/config_policy/test/unittest:unittest", "//base/customization/config_policy/test/fuzztest:fuzztest" ] } diff --git a/config_policy.gni b/config_policy.gni index c86e5cb..604ec7f 100644 --- a/config_policy.gni +++ b/config_policy.gni @@ -17,4 +17,9 @@ declare_args() { # Whether support api so config_policy_api_support = true + + ability_runtime_support = false + if (defined(global_parts_info) && defined(global_parts_info.ability_ability_runtime)) { + ability_runtime_support = true + } } diff --git a/frameworks/config_policy/src/custom_config.cpp b/frameworks/config_policy/src/custom_config.cpp new file mode 100644 index 0000000..6713926 --- /dev/null +++ b/frameworks/config_policy/src/custom_config.cpp @@ -0,0 +1,99 @@ +/* + * Copyright (c) 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 + * + * 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. + */ + +#include "custom_config.h" + +#ifndef CUSTOM_CONFIG_UT +#include "application_context.h" +#endif +#include "parameters.h" +#include "hilog/log.h" +#undef LOG_DOMAIN +#define LOG_DOMAIN 0xD001E00 + +#undef LOG_TAG +#define LOG_TAG "CustomConfig" + +namespace OHOS { +namespace Customization { +namespace ConfigPolicy { +static const std::string EMPTY_STRING = ""; +static const std::string CHANNEL_ID_PREFIX = "const.channelid."; +static const std::string CUSTOM_PRELOAD_LIST_PARA = "persist.custom.preload.list"; +static const char CHAR_ZERO = '0'; +static const char CHAR_NINE = '9'; +static const char CHAR_COMMA = ','; + +static const int MIN_PRELOAD_LIST_VALUE_LEN = 2; + +int CustomConfig::GetBundleName(std::string &bundleName) +{ +#ifdef CUSTOM_CONFIG_UT + bundleName = "ohos.test.customconfig"; +#else + std::shared_ptr abilityContext = + AbilityRuntime::Context::GetApplicationContext(); + if (abilityContext == nullptr) { + HILOG_ERROR(LOG_CORE, "get abilityContext failed."); + return -1; + } + bundleName = abilityContext->GetBundleName(); +#endif + return 0; +} + +bool CustomConfig::IsInPreloadList(std::string bundleName) +{ + std::string preloadList = system::GetParameter(CUSTOM_PRELOAD_LIST_PARA, ""); + if (preloadList.empty()) { + HILOG_WARN(LOG_CORE, "get preload list fail."); + return false; + } + if (preloadList.length() <= MIN_PRELOAD_LIST_VALUE_LEN) { + HILOG_WARN(LOG_CORE, "preload list length error."); + return false; + } + if (preloadList[0] <= CHAR_ZERO || preloadList[0] > CHAR_NINE || preloadList[1] != CHAR_COMMA) { + HILOG_ERROR(LOG_CORE, "preload list param format error."); + return false; + } + int listlen = preloadList[0] - CHAR_ZERO; + std::string preloadListResult(preloadList.substr(1)); // skip list length + + for (int i = 1; i < listlen; i++) { + std::string tempPreloadListPara = CUSTOM_PRELOAD_LIST_PARA + std::to_string(i); + std::string tempList = system::GetParameter(tempPreloadListPara, ""); + if (tempList.empty() || preloadList.length() <= MIN_PRELOAD_LIST_VALUE_LEN) { + HILOG_ERROR(LOG_CORE, "preload list len error."); + return false; + } + preloadListResult.append(tempList.substr(1)); // skip listlen + } + preloadListResult.append(","); + return preloadListResult.find("," + bundleName + ",") != std::string::npos; +} + +std::string CustomConfig::GetChannelId() +{ + std::string bundleName; + if (GetBundleName(bundleName) != 0 || bundleName.empty() || IsInPreloadList(bundleName)) { + return EMPTY_STRING; + } + std::string channelKey = CHANNEL_ID_PREFIX + bundleName; + return system::GetParameter(channelKey, ""); +} +} // namespace ConfigPolicy +} // namespace Customization +} // namespace OHOS diff --git a/interfaces/inner_api/include/custom_config.h b/interfaces/inner_api/include/custom_config.h new file mode 100644 index 0000000..7f06da0 --- /dev/null +++ b/interfaces/inner_api/include/custom_config.h @@ -0,0 +1,36 @@ +/* + * Copyright (c) 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 + * + * 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. + */ + +#ifndef CUSTOM_CONFIG_H +#define CUSTOM_CONFIG_H + +#include + +namespace OHOS { +namespace Customization { +namespace ConfigPolicy { +class CustomConfig { + +public: + static std::string GetChannelId(); + +private: + static int GetBundleName(std::string &bundleName); + static bool IsInPreloadList(std::string bundleName); +}; +} // namespace ConfigPolicy +} // namespace Customization +} // namespace OHOS +#endif diff --git a/interfaces/kits/js/BUILD.gn b/interfaces/kits/js/BUILD.gn index d0d5343..a9d37a2 100644 --- a/interfaces/kits/js/BUILD.gn +++ b/interfaces/kits/js/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos.gni") +import("../../../config_policy.gni") ohos_shared_library("configpolicy") { include_dirs = [ @@ -41,19 +42,21 @@ ohos_shared_library("customconfig") { "include", "../../../interfaces/inner_api/include", ] - - sources = [ - "src/custom_config_napi.cpp", - ] - + sources = [ "src/custom_config_napi.cpp" ] external_deps = [ - "ability_runtime:abilitykit_native", - "ability_runtime:app_context", "c_utils:utils", "hilog:libhilog", - "init:libbegetutil", "napi:ace_napi", ] + if (ability_runtime_support) { + sources += [ "../../../frameworks/config_policy/src/custom_config.cpp" ] + external_deps += [ + "ability_runtime:abilitykit_native", + "ability_runtime:app_context", + "init:libbegetutil", + ] + defines = [ "ABILITY_RUNTIME_SUPPORT" ] + } relative_install_dir = "module/customization" subsystem_name = "customization" part_name = "config_policy" diff --git a/interfaces/kits/js/include/custom_config_napi.h b/interfaces/kits/js/include/custom_config_napi.h index 49c6b1d..888f57e 100644 --- a/interfaces/kits/js/include/custom_config_napi.h +++ b/interfaces/kits/js/include/custom_config_napi.h @@ -32,12 +32,8 @@ public: static napi_value Init(napi_env env, napi_value exports); private: - static int GetBundleName(std::string &bundleName); - static bool IsInPreloadList(std::string bundleName); static napi_value NAPIGetChannelId(napi_env env, napi_callback_info info); - static napi_value NativeGetChannelId(napi_env env, std::string channelKey); - static napi_value CreateNapiStringValue(napi_env env, const char *str); - static char *CustGetSystemParam(const char *name); + static napi_value CreateNapiStringValue(napi_env env, std::string str); }; } // namespace ConfigPolicy } // namespace Customization diff --git a/interfaces/kits/js/src/custom_config_napi.cpp b/interfaces/kits/js/src/custom_config_napi.cpp index 29c2425..76d1499 100644 --- a/interfaces/kits/js/src/custom_config_napi.cpp +++ b/interfaces/kits/js/src/custom_config_napi.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2024 Huawei Device Co., Ltd. + * Copyright (c) 2024-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 @@ -14,10 +14,11 @@ */ #include "custom_config_napi.h" +#ifdef ABILITY_RUNTIME_SUPPORT +#include "custom_config.h" +#endif -#include "application_context.h" #include "hilog/log.h" -#include "init_param.h" #undef LOG_DOMAIN #define LOG_DOMAIN 0xD001E00 @@ -30,9 +31,6 @@ namespace Customization { namespace ConfigPolicy { using namespace OHOS::HiviewDFX; -static const std::string CHANNEL_ID_PREFIX = "const.channelid."; -static const std::string CUSTOM_PRELOAD_LIST_PARA = "persist.custom.preload.list"; - napi_value CustomConfigNapi::Init(napi_env env, napi_value exports) { napi_property_descriptor property[] = { @@ -42,93 +40,20 @@ napi_value CustomConfigNapi::Init(napi_env env, napi_value exports) return exports; } -int CustomConfigNapi::GetBundleName(std::string &bundleName) -{ - std::shared_ptr abilityContext = - AbilityRuntime::Context::GetApplicationContext(); - if (abilityContext == nullptr) { - HILOG_ERROR(LOG_CORE, "get abilityContext failed."); - return -1; - } - bundleName = abilityContext->GetBundleName(); - return 0; -} - - bool CustomConfigNapi::IsInPreloadList(std::string bundleName) - { - char *preloadList = CustGetSystemParam(CUSTOM_PRELOAD_LIST_PARA.c_str()); - if (preloadList == nullptr) { - HILOG_WARN(LOG_CORE, "get preload list fail."); - return false; - } - if (preloadList[0] < '0' || preloadList[0] > '9' || preloadList[1] != ',') { - HILOG_ERROR(LOG_CORE, "preload list param format error."); - free(preloadList); - return false; - } - int listlen = preloadList[0] - '0'; - std::string preloadListResult(preloadList + 1); // skip listlen - free(preloadList); - - for (int i = 1; i < listlen; i++) { - std::string tempPreloadListPara = CUSTOM_PRELOAD_LIST_PARA + std::to_string(i); - char *tempList = CustGetSystemParam(tempPreloadListPara.c_str()); - if (tempList == nullptr) { - HILOG_ERROR(LOG_CORE, "preload list len error."); - return false; - } - preloadListResult.append(tempList + 1); // skip listlen - free(tempList); - } - preloadListResult.append(","); - return preloadListResult.find("," + bundleName + ",") != std::string::npos; -} - napi_value CustomConfigNapi::NAPIGetChannelId(napi_env env, napi_callback_info info) { - std::string bundleName; - if (GetBundleName(bundleName) != 0 || bundleName.empty() || IsInPreloadList(bundleName)) { - return CreateNapiStringValue(env, ""); - } - std::string channelKey = CHANNEL_ID_PREFIX + bundleName; - return NativeGetChannelId(env, channelKey); -} - -char *CustomConfigNapi::CustGetSystemParam(const char *name) -{ - char *value = nullptr; - unsigned int len = 0; - - if (SystemGetParameter(name, nullptr, &len) != 0 || len <= 0 || len > PARAM_CONST_VALUE_LEN_MAX) { - return nullptr; - } - value = (char *)calloc(len, sizeof(char)); - if (value != nullptr && SystemGetParameter(name, value, &len) == 0 && value[0]) { - return value; - } - if (value != nullptr) { - free(value); - } - return nullptr; -} - -napi_value CustomConfigNapi::NativeGetChannelId(napi_env env, std::string channelKey) -{ - char *channelId = CustGetSystemParam(channelKey.c_str()); - napi_value result = nullptr; - if (channelId == nullptr) { - HILOG_WARN(LOG_CORE, "get channelId failed."); - return CreateNapiStringValue(env, ""); - } - result = CreateNapiStringValue(env, channelId); - free(channelId); - return result; +#ifdef ABILITY_RUNTIME_SUPPORT + return CreateNapiStringValue(env, CustomConfig::GetChannelId()); +#else + HILOG_WARN(LOG_CORE, "not support."); + return CreateNapiStringValue(env, ""); +#endif } -napi_value CustomConfigNapi::CreateNapiStringValue(napi_env env, const char *str) +napi_value CustomConfigNapi::CreateNapiStringValue(napi_env env, std::string str) { napi_value result = nullptr; - NAPI_CALL(env, napi_create_string_utf8(env, str, NAPI_AUTO_LENGTH, &result)); + NAPI_CALL(env, napi_create_string_utf8(env, str.c_str(), NAPI_AUTO_LENGTH, &result)); return result; } diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 262a1e5..207e397 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -19,6 +19,14 @@ if (defined(ohos_lite)) { import("//build/test.gni") } +group("unittest") { + testonly = true + deps = [ ":ConfigPolicyUtilsTest" ] + if (ability_runtime_support) { + deps += [ ":CustomConfigTest" ] + } +} + config_policy_sources = [ "config_policy_utils_test.cpp" ] config_policy_include_dirs = [ "../../interfaces/inner_api/include" ] config_policy_deps = [ "../../frameworks/config_policy:configpolicy_util" ] @@ -30,9 +38,6 @@ if (defined(ohos_lite)) { include_dirs = config_policy_include_dirs deps = config_policy_deps } - group("unittest") { - deps = [ ":ConfigPolicyUtilsTest" ] - } } else { ohos_unittest("ConfigPolicyUtilsTest") { module_out_path = "config_policy/config_policy" @@ -43,3 +48,20 @@ if (defined(ohos_lite)) { resource_config_file = "./resource/ohos_test.xml" } } + +if (ability_runtime_support) { + ohos_unittest("CustomConfigTest") { + module_out_path = "config_policy/config_policy" + sources = [ + "../../frameworks/config_policy/src/custom_config.cpp", + "custom_config_test.cpp" + ] + include_dirs = config_policy_include_dirs + defines = [ "CUSTOM_CONFIG_UT" ] + external_deps = [ + "c_utils:utils", + "hilog:libhilog", + "init:libbegetutil" + ] + } +} \ No newline at end of file diff --git a/test/unittest/custom_config_test.cpp b/test/unittest/custom_config_test.cpp new file mode 100644 index 0000000..eceab4f --- /dev/null +++ b/test/unittest/custom_config_test.cpp @@ -0,0 +1,173 @@ +/* + * Copyright (c) 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 + * + * 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. + */ + +#include +#include + +#include "parameters.h" + +#include "custom_config.h" + +using namespace testing::ext; +using namespace OHOS::Customization::ConfigPolicy; + +static const std::string TEST_BUNDLE_NAME = "ohos.test.customconfig"; +static const std::string EMPTY_STRING = ""; +static const std::string CHANNEL_ID_BUNDLE_KEY = "const.channelid." + TEST_BUNDLE_NAME; +static const std::string CHANNEL_ID_BUNDLE_VALUE = "Test-ohos"; +static const std::string CUSTOM_PRELOAD_LIST_PARA = "persist.custom.preload.list"; + +namespace OHOS { +class CustomConfigTest : public testing::Test { + public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); +}; + +void CustomConfigTest::SetUpTestCase(void) +{ + std::string channelId = system::GetParameter(CHANNEL_ID_BUNDLE_KEY, EMPTY_STRING); + if (channelId.empty()) { + bool retSetParam = system::SetParameter(CHANNEL_ID_BUNDLE_KEY, CHANNEL_ID_BUNDLE_VALUE); + EXPECT_TRUE(retSetParam); + } +} + +void CustomConfigTest::TearDownTestCase(void) +{ + bool retSetParam = system::SetParameter(CUSTOM_PRELOAD_LIST_PARA, " "); + EXPECT_TRUE(retSetParam); + retSetParam = system::SetParameter(CUSTOM_PRELOAD_LIST_PARA + "1", " "); + EXPECT_TRUE(retSetParam); +} + +/** + * @tc.name: CustomConfigFuncTest001 + * @tc.desc: Test GetChannelId() function, not set preload list. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(CustomConfigTest, CustomConfigFuncTest001, TestSize.Level1) +{ + std::string channelId = CustomConfig::GetChannelId(); + EXPECT_EQ(channelId, CHANNEL_ID_BUNDLE_VALUE); +} + +/** + * @tc.name: CustomConfigFuncTest002 + * @tc.desc: Test GetChannelId() function, param format error. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(CustomConfigTest, CustomConfigFuncTest002, TestSize.Level1) +{ + + std::vector errFormatValues; + errFormatValues.emplace_back("ab"); + errFormatValues.emplace_back(":,bcd"); + errFormatValues.emplace_back("/,efg"); + errFormatValues.emplace_back("0,test"); + errFormatValues.emplace_back("1tetest"); + + for (std::string errFormatValue : errFormatValues) { + bool retSetParam = system::SetParameter(CUSTOM_PRELOAD_LIST_PARA, errFormatValue); + EXPECT_TRUE(retSetParam); + std::string channelId = CustomConfig::GetChannelId(); + EXPECT_EQ(channelId, CHANNEL_ID_BUNDLE_VALUE); + } +} + +/** + * @tc.name: CustomConfigFuncTest003 + * @tc.desc: Test GetChannelId() function, the parameter value does not include the package name. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(CustomConfigTest, CustomConfigFuncTest003, TestSize.Level1) +{ + std::string testValue = "1,com.ohos.test0,com.ohos.test2,com.ohos.test3"; + bool retSetParam = system::SetParameter(CUSTOM_PRELOAD_LIST_PARA, testValue); + EXPECT_TRUE(retSetParam); + std::string channelId = CustomConfig::GetChannelId(); + EXPECT_EQ(channelId, CHANNEL_ID_BUNDLE_VALUE); +} + +/** + * @tc.name: CustomConfigFuncTest004 + * @tc.desc: Test GetChannelId() function, the parameter value with the package name. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(CustomConfigTest, CustomConfigFuncTest004, TestSize.Level1) +{ + std::string testValue = "1,com.ohos.test0,com.ohos.test2,com.ohos.test3," + TEST_BUNDLE_NAME; + bool retSetParam = system::SetParameter(CUSTOM_PRELOAD_LIST_PARA, testValue); + EXPECT_TRUE(retSetParam); + std::string channelId = CustomConfig::GetChannelId(); + EXPECT_EQ(channelId, EMPTY_STRING); +} + +/** + * @tc.name: CustomConfigFuncTest005 + * @tc.desc: Test GetChannelId() function, the parameter value without the package name. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(CustomConfigTest, CustomConfigFuncTest005, TestSize.Level1) +{ + std::string testValue = "2,com.ohos.test0,com.ohos.test2,com.ohos.test3"; + bool retSetParam = system::SetParameter(CUSTOM_PRELOAD_LIST_PARA, testValue); + EXPECT_TRUE(retSetParam); + std::string channelId = CustomConfig::GetChannelId(); + EXPECT_EQ(channelId, CHANNEL_ID_BUNDLE_VALUE); +} + +/** + * @tc.name: CustomConfigFuncTest006 + * @tc.desc: Test GetChannelId() function, the parameter length test with the package name. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(CustomConfigTest, CustomConfigFuncTest006, TestSize.Level1) +{ + std::string testValue = "2,com.ohos.test0,com.ohos.test2,com.ohos.test3"; + std::string testValue2 = "2,com.ohos.test3," + TEST_BUNDLE_NAME + ",com.ohos.test5,com.ohos.test6"; + bool retSetParam = system::SetParameter(CUSTOM_PRELOAD_LIST_PARA, testValue); + EXPECT_TRUE(retSetParam); + retSetParam = system::SetParameter(CUSTOM_PRELOAD_LIST_PARA + "1", testValue2); + EXPECT_TRUE(retSetParam); + std::string channelId = CustomConfig::GetChannelId(); + EXPECT_EQ(channelId, EMPTY_STRING); +} + +/** + * @tc.name: CustomConfigFuncTest007 + * @tc.desc: Test GetChannelId() function, the parameter format error. + * @tc.type: FUNC + * @tc.require: + */ +HWTEST_F(CustomConfigTest, CustomConfigFuncTest007, TestSize.Level1) +{ + std::string testValue = "2,com.ohos.test0,com.ohos.test2,com.ohos.test3"; + std::string testValue2 = "2,"; + bool retSetParam = system::SetParameter(CUSTOM_PRELOAD_LIST_PARA, testValue); + EXPECT_TRUE(retSetParam); + retSetParam = system::SetParameter(CUSTOM_PRELOAD_LIST_PARA + "1", testValue2); + EXPECT_TRUE(retSetParam); + std::string channelId = CustomConfig::GetChannelId(); + EXPECT_EQ(channelId, CHANNEL_ID_BUNDLE_VALUE); +} + +} // namespace OHOS -- Gitee