diff --git a/README_zh.md b/README_zh.md index ae7d939b2bf25212b6fd216b65d61c2b851a5643..9b168b41ec4098a0cc6c0e94fe95538bd7d26dbc 100644 --- a/README_zh.md +++ b/README_zh.md @@ -38,9 +38,16 @@ ``` #include "config_policy_utils.h" -const char *testPathSuffix = "user.xml"; //设置配置文件名称 +CfgDir *cfgDir = GetCfgDirList(); // 获取配置层级目录列表 +FreeCfgDirList(cfgDir); // 获取完成后需要释放内存 + +const char *cfgPath = "etc/xml/cfg.xml"; // 设置配置文件相对路径及文件名 +CfgFiles *cfgFiles = GetCfgFiles(cfgPath); // 获取所有配置层级的配置文件路径 +FreeCfgFiles(cfgFiles); // 获取完成后需要释放内存 + +const char *userPath = "etc/xml/user.xml"; // 设置配置文件相对路径及文件名 char buf[MAX_PATH_LEN] = {0}; -char *filePath = GetOneCfgFile(testPathSuffix, buf, MAX_PATH_LEN); //获取最高优先级的配置文件路径 +char *filePath = GetOneCfgFile(userPath, buf, MAX_PATH_LEN); // 获取最高优先级的配置文件路径 ``` ## 约束 diff --git a/frameworks/config_policy/BUILD.gn b/frameworks/config_policy/BUILD.gn index 91b2529c7f9a0e52fa8fc07186be4f9f5441eb10..2fef5e04bac6d8c3187ed5c35d86d85b72977fb7 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 eb3539a525b305e018f5b344e17de1761a7fbfb3..fd322f03bd46ade4fdeaeba38e5226a0048304a9 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 8a0d98acc8e400ec19d1f1a544df322ea4f19c6c..d90dec907880099ff6c487a3ca13f2abdb4f6837 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 d3c9f674f47d38f912436e92a087d7dcb7bd9371..1c014e6867cb400ed27b72c0d4dc1acf290665a0 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 7623d033c9ee763352bd12a14fd22f2943cb679e..b487b765a252581af2d2bbffe597fea15a8678e1 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -19,9 +19,15 @@ 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" ] +config_policy_include_dirs = [ + "//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 5e3c0db796518c9f4243d9a7e16c192a630e607b..f5aa2a92b185185bebf13ff17d4c88fc86757dc6 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