From b824a1e35009e98496768d0cd23a3ed0333f20c0 Mon Sep 17 00:00:00 2001 From: xiekaiming Date: Thu, 10 Nov 2022 10:54:20 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=9B=B4=E6=96=B0UT=E7=94=A8=E4=BE=8B?= =?UTF-8?q?=EF=BC=8C=E6=B7=BB=E5=8A=A0etc=E7=9B=AE=E5=BD=95=202.=20?= =?UTF-8?q?=E6=9B=B4=E6=96=B0=E8=B5=84=E6=96=99=203.=20=E4=BC=98=E5=8C=96G?= =?UTF-8?q?etOpkeyPath=E9=80=BB=E8=BE=91=204.=20=E4=BC=98=E5=8C=96napi?= =?UTF-8?q?=E5=8F=82=E6=95=B0=E8=BE=93=E5=85=A5?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: xiekaiming --- README_zh.md | 10 ++++- frameworks/config_policy/BUILD.gn | 1 + .../config_policy/src/config_policy_utils.c | 9 ++-- .../inner_api/include/config_policy_impl.h | 6 +-- test/resource/ohos_test.xml | 41 ++++++++--------- test/unittest/BUILD.gn | 7 ++- test/unittest/config_policy_utils_test.cpp | 44 +++++++++++++------ 7 files changed, 70 insertions(+), 48 deletions(-) diff --git a/README_zh.md b/README_zh.md index ae7d939..c31adcf 100644 --- a/README_zh.md +++ b/README_zh.md @@ -38,9 +38,15 @@ ``` #include "config_policy_utils.h" -const char *testPathSuffix = "user.xml"; //设置配置文件名称 +CfgDir *cfgDir = GetCfgDirList(); // 获取配置层级目录列表 +FreeCfgDirList(cfgDir); // 获取完成后需要释放内存 + +const char *pathSuffix = "etc/xml/user.xml"; // 设置配置文件相对路径及文件名 char buf[MAX_PATH_LEN] = {0}; -char *filePath = GetOneCfgFile(testPathSuffix, buf, MAX_PATH_LEN); //获取最高优先级的配置文件路径 +CfgFiles *cfgFiles = GetCfgFiles(pathSuffix); // 获取所有配置层级的配置文件路径 +FreeCfgFiles(cfgFiles); // 获取完成后需要释放内存 + +char *filePath = GetOneCfgFile(pathSuffix, buf, MAX_PATH_LEN); // 获取最高优先级的配置文件路径 ``` ## 约束 diff --git a/frameworks/config_policy/BUILD.gn b/frameworks/config_policy/BUILD.gn index 91b2529..2fef5e0 100644 --- a/frameworks/config_policy/BUILD.gn +++ b/frameworks/config_policy/BUILD.gn @@ -28,6 +28,7 @@ config("config_policy_config") { "//base/customization/config_policy/interfaces/inner_api/include", "//third_party/bounds_checking_function/include", "//base/startup/init/services/include/param", + "//base/telephony/core_service/utils/common/include", ] } diff --git a/frameworks/config_policy/src/config_policy_utils.c b/frameworks/config_policy/src/config_policy_utils.c index eb3539a..fd322f0 100644 --- a/frameworks/config_policy/src/config_policy_utils.c +++ b/frameworks/config_policy/src/config_policy_utils.c @@ -24,8 +24,11 @@ #ifndef __LITEOS__ #include "init_param.h" #endif +#include "telephony_config_c.h" static const size_t MIN_APPEND_LEN = 32; +// set min opkey length +static const unsigned int MIN_OPKEY_LEN = 3; // ':' split different x rules, example:":relPath,mode[,extra][:]" // ',' split different param for x rules // ":-" split for key:-value @@ -116,9 +119,9 @@ static char *GetOpkeyPath(int type) opKeyName = CUST_OPKEY1; } else { unsigned int len = 0; - if (SystemGetParameter(CUST_OPKEY0, NULL, &len) == 0 && len > 0) { + if (SystemGetParameter(CUST_OPKEY0, NULL, &len) == 0 && len > MIN_OPKEY_LEN) { opKeyName = CUST_OPKEY0; - } else if (SystemGetParameter(CUST_OPKEY1, NULL, &len) == 0 && len > 0) { + } else if (SystemGetParameter(CUST_OPKEY1, NULL, &len) == 0 && len > MIN_OPKEY_LEN) { opKeyName = CUST_OPKEY1; } } @@ -197,7 +200,7 @@ static char *GetFollowXRule(const char *relPath, int *mode) item++; // skip delim ':', goto ":relPath,mode[,extra][:]" char *endItem = strchr(item, SEP_FOR_X_RULE); char *nextItem = endItem + 1; - while (endItem && nextItem && *nextItem == '-') { + while (endItem && *nextItem == '-') { endItem = strchr(nextItem, SEP_FOR_X_RULE); nextItem = endItem + 1; } diff --git a/interfaces/inner_api/include/config_policy_impl.h b/interfaces/inner_api/include/config_policy_impl.h index 8a0d98a..4716e67 100644 --- a/interfaces/inner_api/include/config_policy_impl.h +++ b/interfaces/inner_api/include/config_policy_impl.h @@ -25,16 +25,12 @@ extern "C" { // these name is used for write data, init may use it #define CUST_KEY_POLICY_LAYER "const.cust.config_dir_layer" #define CUST_FOLLOW_X_RULES "const.cust.follow_x_rules" -// opkey info for sim1 -#define CUST_OPKEY0 "telephony.sim.opkey0" -// opkey info for sim1 -#define CUST_OPKEY1 "telephony.sim.opkey1" // if fs need path prefix, set ROOT_PREFIX before include current file #ifndef ROOT_PREFIX #define ROOT_PREFIX "" #endif -#define DEFAULT_LAYER ROOT_PREFIX"/system:"ROOT_PREFIX"/chipset:"ROOT_PREFIX"/sys_prod:"ROOT_PREFIX"/chip_prod" +#define DEFAULT_LAYER ROOT_PREFIX "/system:" ROOT_PREFIX" /chipset:" ROOT_PREFIX" /sys_prod:" ROOT_PREFIX "/chip_prod" #ifdef __LITEOS_M__ #define MINI_CONFIG_POLICY_BUF_SIZE 256 diff --git a/test/resource/ohos_test.xml b/test/resource/ohos_test.xml index d3c9f67..1c014e6 100644 --- a/test/resource/ohos_test.xml +++ b/test/resource/ohos_test.xml @@ -17,30 +17,27 @@ - diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index 7623d03..30e13de 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -20,8 +20,11 @@ if (defined(ohos_lite)) { config_policy_sources = [ "config_policy_utils_test.cpp" ] config_policy_include_dirs = - [ "//base/customization/config_policy/interfaces/inner_api/include" ] -config_policy_deps = [ "//base/customization/config_policy/frameworks/config_policy:configpolicy_util" ] + [ "//base/customization/config_policy/interfaces/inner_api/include", + "//base/startup/init/services/include/param", + "//base/telephony/core_service/utils/common/include", ] +config_policy_deps = [ "//base/customization/config_policy/frameworks/config_policy:configpolicy_util", + "//base/startup/init/interfaces/innerkits:libbegetutil", ] if (defined(ohos_lite)) { unittest("ConfigPolicyUtilsTest") { diff --git a/test/unittest/config_policy_utils_test.cpp b/test/unittest/config_policy_utils_test.cpp index 5e3c0db..5782691 100644 --- a/test/unittest/config_policy_utils_test.cpp +++ b/test/unittest/config_policy_utils_test.cpp @@ -18,11 +18,25 @@ #include #include "config_policy_utils.h" +#include "config_policy_impl.h" + +#include "init_param.h" +#include "telephony_config_c.h" using namespace testing::ext; namespace OHOS { -class ConfigPolicyUtilsTest : public testing::Test {}; +class ConfigPolicyUtilsTest : public testing::Test { + public: + static void SetUpTestCase(void); +}; + +void ConfigPolicyUtilsTest::SetUpTestCase(void) { + SystemSetParameter(CUST_OPKEY0, "46060"); + SystemSetParameter(CUST_OPKEY1, "46061"); + SystemSetParameter(CUST_FOLLOW_X_RULES, + ":etc/custxmltest/user.xml,10:etc/custxmltest/both.xml,100,etc/carrier/${test:-46061}"); +} bool TestGetCfgFile(const char *testPathSuffix, int type, const char *extra) { @@ -55,7 +69,7 @@ bool TestGetCfgFile(const char *testPathSuffix, int type, const char *extra) */ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest001, TestSize.Level1) { - const char *testPathSuffix = "custxmltest/none.xml"; + const char *testPathSuffix = "etc/custxmltest/none.xml"; EXPECT_FALSE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_FOLLOW, NULL)); } @@ -67,7 +81,7 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest001, TestSize.Level1) */ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest002, TestSize.Level1) { - const char *testPathSuffix = "custxmltest/system.xml"; + const char *testPathSuffix = "etc/custxmltest/system.xml"; EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_FOLLOW, NULL)); } @@ -79,7 +93,7 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest002, TestSize.Level1) */ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest003, TestSize.Level1) { - const char *testPathSuffix = "custxmltest/user.xml"; + const char *testPathSuffix = "etc/custxmltest/user.xml"; EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_FOLLOW, NULL)); } @@ -91,7 +105,7 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest003, TestSize.Level1) */ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest004, TestSize.Level1) { - const char *testPathSuffix = "custxmltest/both.xml"; + const char *testPathSuffix = "etc/custxmltest/both.xml"; EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_FOLLOW, NULL)); } @@ -125,7 +139,7 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest005, TestSize.Level1) */ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest006, TestSize.Level1) { - const char *testPathSuffix = "custxmltest/user.xml"; + const char *testPathSuffix = "etc/custxmltest/user.xml"; EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_SIM_DEFAULT, NULL)); } @@ -137,7 +151,7 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest006, TestSize.Level1) */ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest007, TestSize.Level1) { - const char *testPathSuffix = "custxmltest/user.xml"; + const char *testPathSuffix = "etc/custxmltest/user.xml"; EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_SIM_1, NULL)); } @@ -149,7 +163,7 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest007, TestSize.Level1) */ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest008, TestSize.Level1) { - const char *testPathSuffix = "custxmltest/user.xml"; + const char *testPathSuffix = "etc/custxmltest/user.xml"; EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_SIM_2, NULL)); } @@ -161,8 +175,8 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest008, TestSize.Level1) */ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest009, TestSize.Level1) { - EXPECT_TRUE(TestGetCfgFile("custxmltest/user.xml", FOLLOWX_MODE_DEFAULT, NULL)); - EXPECT_TRUE(TestGetCfgFile("custxmltest/both.xml", FOLLOWX_MODE_DEFAULT, NULL)); + EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/user.xml", FOLLOWX_MODE_DEFAULT, NULL)); + EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/both.xml", FOLLOWX_MODE_DEFAULT, NULL)); } /** @@ -173,9 +187,11 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest009, TestSize.Level1) */ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest010, TestSize.Level1) { - const char *extra = "etc/carrier/${persist.telephony.opkey1:-46060},etc/carrier/${persist.telephony.opkey0}/" - "${testkey:-custxmltest}"; - EXPECT_TRUE(TestGetCfgFile("custxmltest/user.xml", FOLLOWX_MODE_USER_DEFINE, extra)); - EXPECT_TRUE(TestGetCfgFile("custxmltest/both.xml", FOLLOWX_MODE_USER_DEFINE, extra)); + std::string extraString; + extraString.append("etc/carrier/${").append(CUST_OPKEY1).append(":-46060},etc/carrier/${") + .append(CUST_OPKEY0).append("}/etc/${testkey:-custxmltest}"); + std::cout << "extra: " << extraString << std::endl; + EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/user.xml", FOLLOWX_MODE_USER_DEFINE, extraString.c_str())); + EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/both.xml", FOLLOWX_MODE_USER_DEFINE, extraString.c_str())); } } // namespace OHOS -- Gitee