diff --git a/README_zh.md b/README_zh.md index ae7d939b2bf25212b6fd216b65d61c2b851a5643..c31adcf80742ebb16bf00b6ad65f5372c64c2092 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 5317390dac936550e1b89c397785b75432eb034b..fbade73b5778f58e7062f31eea5ce3d080cb36de 100644 --- a/frameworks/config_policy/BUILD.gn +++ b/frameworks/config_policy/BUILD.gn @@ -23,6 +23,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 92b7c45def4ae039cc739187989b126e7d99bdf7..e8070a635be080dc376700554825eb429cbecfe3 100644 --- a/frameworks/config_policy/src/config_policy_utils.c +++ b/frameworks/config_policy/src/config_policy_utils.c @@ -24,8 +24,11 @@ #ifndef OHOS_LITE #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 @@ -98,9 +101,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; } } @@ -177,7 +180,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 b13d4a98c343f9fd021c039fdfcb94a6299bee07..d6de604832d2fa747ab9972d75555e6b39062f4b 100644 --- a/interfaces/inner_api/include/config_policy_impl.h +++ b/interfaces/inner_api/include/config_policy_impl.h @@ -25,10 +25,6 @@ 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" #ifdef __cplusplus #if __cplusplus diff --git a/interfaces/kits/js/src/config_policy_napi.cpp b/interfaces/kits/js/src/config_policy_napi.cpp index 81287eba6924ac839a77d0debdfc6f1e770aed91..3a5c8339b98550e9648060dd1cafc350840f6453 100644 --- a/interfaces/kits/js/src/config_policy_napi.cpp +++ b/interfaces/kits/js/src/config_policy_napi.cpp @@ -60,7 +60,7 @@ napi_value ConfigPolicyNapi::NAPIGetOneCfgFile(napi_env env, napi_callback_info auto asyncContext = std::make_unique(); ParseRelPath(env, asyncContext->relPath_, argv[ARR_INDEX_ZERO]); - if (argc == ARGS_SIZE_TWO) { + if (argc >= ARGS_SIZE_TWO) { bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ONE], napi_function); if (!matchFlag) { return ThrowNapiError(env, PARAM_ERROR, "Parameter error. The second parameter must be Callback."); @@ -84,7 +84,7 @@ napi_value ConfigPolicyNapi::NAPIGetCfgFiles(napi_env env, napi_callback_info in auto asyncContext = std::make_unique(); ParseRelPath(env, asyncContext->relPath_, argv[ARR_INDEX_ZERO]); - if (argc == ARGS_SIZE_TWO) { + if (argc >= ARGS_SIZE_TWO) { bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ONE], napi_function); if (!matchFlag) { return ThrowNapiError(env, PARAM_ERROR, "Parameter error. The second parameter must be Callback."); @@ -103,7 +103,7 @@ napi_value ConfigPolicyNapi::NAPIGetCfgDirList(napi_env env, napi_callback_info napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); auto asyncContext = std::make_unique(); - if (argc == ARGS_SIZE_ONE) { + if (argc >= ARGS_SIZE_ONE) { bool matchFlag = MatchValueType(env, argv[ARR_INDEX_ZERO], napi_function); if (!matchFlag) { return ThrowNapiError(env, PARAM_ERROR, "Parameter error. The first parameter must be Callback."); 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..30e13de08e24eb22c0b5a0e5832ee8af1ddb9115 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 5e3c0db796518c9f4243d9a7e16c192a630e607b..578269174a0820b87e0b35a3314de948557b4b9d 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