diff --git a/bundle.json b/bundle.json index be90aac4e572e0bfd12baf1b512c06ebdde17ede..cf2c9933ec771c0651d7a5e771c6c9b379b32783 100644 --- a/bundle.json +++ b/bundle.json @@ -22,7 +22,6 @@ "ram": "50KB", "deps": { "components": [ - "core_service", "hisysevent", "hilog", "napi", @@ -48,7 +47,8 @@ } ], "test": [ - "//base/customization/config_policy/test/unittest:ConfigPolicyUtilsTest" + "//base/customization/config_policy/test/unittest:ConfigPolicyUtilsTest", + "//base/customization/config_policy/test/fuzztest:fuzztest" ] } } diff --git a/common/config/BUILD.gn b/common/config/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..b8f1b882224bd271010bab13144eb9ab357e36a3 --- /dev/null +++ b/common/config/BUILD.gn @@ -0,0 +1,24 @@ +# Copyright (c) 2022 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. + +declare_args() { + config_policy_feature_coverage = false +} + +config("coverage_flags") { + if (config_policy_feature_coverage) { + cflags = [ "--coverage" ] + cflags_cc = [ "--coverage" ] + ldflags = [ "--coverage" ] + } +} diff --git a/frameworks/config_policy/BUILD.gn b/frameworks/config_policy/BUILD.gn index cd9a8bb49067d05cb42626bc7acde27bbfb8eda4..978cbc6e6ce811b21f0a4d19461399fbe228963c 100644 --- a/frameworks/config_policy/BUILD.gn +++ b/frameworks/config_policy/BUILD.gn @@ -23,7 +23,7 @@ config("config_policy_config") { include_dirs = [ "../../interfaces/inner_api/include", "//third_party/bounds_checking_function/include", - "$STARTUP_INIT_ROOT_DIR/services/include/param", + "$STARTUP_INIT_ROOT_DIR/interfaces/innerkits/include/param", "$TEL_CORESERVICE_ROOT_DIR/utils/common/include", ] } @@ -62,6 +62,7 @@ if (defined(ohos_lite) && ohos_kernel_type == "liteos_m") { innerapi_tags = [ "chipsetsdk", "platformsdk", + "sasdk", ] subsystem_name = "customization" part_name = "config_policy" diff --git a/frameworks/config_policy/src/config_policy_utils.c b/frameworks/config_policy/src/config_policy_utils.c index c75626a9e0fb213d3520c8b5a9f72d55315b67fe..6f14f82b0c805eba745c4f62005382d333382c4a 100644 --- a/frameworks/config_policy/src/config_policy_utils.c +++ b/frameworks/config_policy/src/config_policy_utils.c @@ -97,7 +97,7 @@ static char *CustGetSystemParam(const char *name) char *value = NULL; unsigned int len = 0; - if (SystemGetParameter(name, NULL, &len) != 0 || len == 0) { + if (SystemGetParameter(name, NULL, &len) != 0 || len <= 0 || len > PARAM_CONST_VALUE_LEN_MAX) { return NULL; } value = (char *)calloc(len, sizeof(char)); @@ -213,7 +213,7 @@ static char *GetFollowXRule(const char *relPath, int *mode) modeStr++; *mode = atoi(modeStr); } - if (*mode == FOLLOWX_MODE_USER_DEFINE && modeStr) { + if (*mode == FOLLOWX_MODE_USER_DEFINED && modeStr) { addPath = strchr(modeStr, SEP_FOR_X_PARAM); if (addPath) { addPath++; // skip ',' get extra info @@ -234,7 +234,7 @@ static SplitedStr *GetFollowXPathByMode(const char *relPath, int followMode, con const char *extraPath = extra; if (followMode == FOLLOWX_MODE_DEFAULT) { modePathFromCfg = GetFollowXRule(relPath, &followMode); - if (followMode == FOLLOWX_MODE_USER_DEFINE && modePathFromCfg && *modePathFromCfg) { + if (followMode == FOLLOWX_MODE_USER_DEFINED && modePathFromCfg && *modePathFromCfg) { extraPath = modePathFromCfg; } } @@ -249,7 +249,7 @@ static SplitedStr *GetFollowXPathByMode(const char *relPath, int followMode, con case FOLLOWX_MODE_SIM_2: followXPath = GetOpkeyPath(FOLLOWX_MODE_SIM_2); break; - case FOLLOWX_MODE_USER_DEFINE: + case FOLLOWX_MODE_USER_DEFINED: followXPath = (extraPath && *extraPath) ? strdup(extraPath) : NULL; break; default: @@ -290,8 +290,11 @@ static char *TrimInplace(char *str, bool moveToStart) static bool EnsureHolderSpace(StringHolder *holder, size_t leastSize) { + if (holder == NULL) { + return false; + } if (holder->size < leastSize) { - size_t allocSize = Max(leastSize * 2, MIN_APPEND_LEN); + size_t allocSize = Min(Max(leastSize * 2, MIN_APPEND_LEN), PARAM_CONST_VALUE_LEN_MAX); char *newPtr = (char *)calloc(allocSize, sizeof(char)); if (newPtr == NULL) { allocSize = leastSize; @@ -313,6 +316,9 @@ static bool EnsureHolderSpace(StringHolder *holder, size_t leastSize) static bool AppendStr(StringHolder *holder, const char *s) { + if (holder == NULL || s == NULL) { + return false; + } size_t leastSize = holder->strLen + strlen(s) + 1; if (!EnsureHolderSpace(holder, leastSize)) { return false; @@ -490,7 +496,7 @@ CfgFiles *GetCfgFilesEx(const char *pathSuffix, int followMode, const char *extr access(buf, F_OK) == 0) { files->paths[index++] = strdup(buf); } - for (int j = 0; result && j < result->segCount; j++) { + for (int j = 0; result && j < result->segCount && index < MAX_CFG_POLICY_DIRS_CNT; j++) { if (result->segs[j] && snprintf_s(buf, MAX_PATH_LEN, MAX_PATH_LEN - 1, "%s/%s/%s", dirs->paths[i], result->segs[j], pathSuffix) > 0 && diff --git a/interfaces/inner_api/include/config_policy_utils.h b/interfaces/inner_api/include/config_policy_utils.h index ec6fcb5a18491703a6280f1eb487ec5338e23d7c..8d16fca27d4f6b5f45265cbb0f20edd5b9d8a748 100644 --- a/interfaces/inner_api/include/config_policy_utils.h +++ b/interfaces/inner_api/include/config_policy_utils.h @@ -23,7 +23,7 @@ extern "C" { #endif // __cplusplus #define MAX_CFG_POLICY_DIRS_CNT 32 // max number of directories -#define MAX_PATH_LEN 128 // max length of a filepath +#define MAX_PATH_LEN 256 // max length of a filepath // follow X(carrier/network/PLMN/...) usage // 1. etc/cust/followx_file_list.cfg can be in any layer, except follow x dir @@ -42,8 +42,8 @@ extern "C" { // follow_rule,etc/xml/config2.xml,100,etc/carrier/${keyname:-value} // Follow rule in followx_file_list.cfg, which stored in param variant #define FOLLOWX_MODE_DEFAULT 0 -// Not use any follow rule, even exsit followx_file_list.cfg -#define FOLLOWX_MODE_NO_FOLLOW 1 +// Not use any follow rule, even exist followx_file_list.cfg +#define FOLLOWX_MODE_NO_RULE_FOLLOWED 1 // Follow rule by default slot #define FOLLOWX_MODE_SIM_DEFAULT 10 // Follow rule by slot 1 @@ -52,7 +52,7 @@ extern "C" { #define FOLLOWX_MODE_SIM_2 12 // User defined follow rule, get follow_rule_add_path from @param extra // Notice: Follow rule in followx_file_list.cfg will be ignored. -#define FOLLOWX_MODE_USER_DEFINE 100 +#define FOLLOWX_MODE_USER_DEFINED 100 // Config Files struct CfgFiles { diff --git a/interfaces/kits/js/include/config_policy_napi.h b/interfaces/kits/js/include/config_policy_napi.h index edc0f9edb8e522091d1a49a124b434e7689ae8e0..458f1ff3893c51fc93b33bfddbbcd2fe09a4b3e6 100644 --- a/interfaces/kits/js/include/config_policy_napi.h +++ b/interfaces/kits/js/include/config_policy_napi.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -33,6 +33,8 @@ struct ConfigAsyncContext { CreateNapiValue createValueFunc_; std::string relPath_; + int32_t followMode_; + std::string extra_; std::string pathValue_; std::vector paths_; }; @@ -44,20 +46,35 @@ public: static napi_value Init(napi_env env, napi_value exports); private: + typedef napi_value (*NapiFunction)(napi_env env, std::shared_ptr context); + static napi_value NAPIGetOneCfgFile(napi_env env, napi_callback_info info); static napi_value NAPIGetCfgFiles(napi_env env, napi_callback_info info); + static napi_value NAPIGetOneCfgFileSync(napi_env env, napi_callback_info info); + static napi_value NAPIGetCfgFilesSync(napi_env env, napi_callback_info info); static napi_value NAPIGetCfgDirList(napi_env env, napi_callback_info info); + static napi_value NAPIGetCfgDirListSync(napi_env env, napi_callback_info info); static napi_value CreateUndefined(napi_env env); static std::string GetStringFromNAPI(napi_env env, napi_value value); static napi_value HandleAsyncWork(napi_env env, ConfigAsyncContext *context, std::string workName, napi_async_execute_callback execute, napi_async_complete_callback complete); + static napi_value GetOneCfgFileOrAllCfgFilesSync(napi_env env, napi_callback_info info, NapiFunction func); + static napi_value GetOneCfgFileOrAllCfgFiles(napi_env env, napi_callback_info info, + const std::string &workName, napi_async_execute_callback execute); static bool MatchValueType(napi_env env, napi_value value, napi_valuetype targetType); static void NativeGetOneCfgFile(napi_env env, void *data); static void NativeGetCfgFiles(napi_env env, void *data); static void NativeGetCfgDirList(napi_env env, void *data); + static napi_value NativeGetOneCfgFileSync(napi_env env, std::shared_ptr context); + static napi_value NativeGetCfgFilesSync(napi_env env, std::shared_ptr context); + static napi_value NativeGetCfgDirListSync(napi_env env, std::shared_ptr context); static void NativeCallbackComplete(napi_env env, napi_status status, void *data); static napi_value ParseRelPath(napi_env env, std::string ¶m, napi_value args); + static napi_value ParseFollowMode(napi_env env, int32_t ¶m, napi_value args, bool hasExtra); + static napi_value ParseExtra(napi_env env, std::string ¶m, napi_value args); + static napi_value CreateArraysValue(napi_env env, std::shared_ptr context); static void CreateArraysValueFunc(ConfigAsyncContext &context); + static void CreateFollowXModeObject(napi_env env, napi_value value); static napi_value ThrowNapiError(napi_env env, int32_t errCode, const std::string &errMessage); }; } // namespace ConfigPolicy diff --git a/interfaces/kits/js/src/config_policy_napi.cpp b/interfaces/kits/js/src/config_policy_napi.cpp index 60b371cb86cdefcd75834b98a4c78c6ff4e6da5a..b2710b70d6721a4c98acd1c82f8f26ec05792a01 100644 --- a/interfaces/kits/js/src/config_policy_napi.cpp +++ b/interfaces/kits/js/src/config_policy_napi.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022-2023 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 @@ -28,8 +28,12 @@ using namespace OHOS::HiviewDFX; static constexpr size_t ARGS_SIZE_ONE = 1; static constexpr size_t ARGS_SIZE_TWO = 2; +static constexpr size_t ARGS_SIZE_THREE = 3; +static constexpr size_t ARGS_SIZE_FOUR = 4; static constexpr int32_t ARR_INDEX_ZERO = 0; static constexpr int32_t ARR_INDEX_ONE = 1; +static constexpr int32_t ARR_INDEX_TWO = 2; +static constexpr int32_t ARR_INDEX_THREE = 3; static constexpr int32_t NAPI_RETURN_ZERO = 0; static constexpr int32_t NAPI_RETURN_ONE = 1; // Param Error Code @@ -38,19 +42,65 @@ static constexpr HiLogLabel LABEL = { LOG_CORE, 0xD001E00, "ConfigPolicyJs" }; napi_value ConfigPolicyNapi::Init(napi_env env, napi_value exports) { + napi_value nFollowXMode = nullptr; + NAPI_CALL(env, napi_create_object(env, &nFollowXMode)); + CreateFollowXModeObject(env, nFollowXMode); + napi_property_descriptor property[] = { DECLARE_NAPI_FUNCTION("getOneCfgFile", ConfigPolicyNapi::NAPIGetOneCfgFile), + DECLARE_NAPI_FUNCTION("getOneCfgFileSync", ConfigPolicyNapi::NAPIGetOneCfgFileSync), DECLARE_NAPI_FUNCTION("getCfgFiles", ConfigPolicyNapi::NAPIGetCfgFiles), - DECLARE_NAPI_FUNCTION("getCfgDirList", ConfigPolicyNapi::NAPIGetCfgDirList) + DECLARE_NAPI_FUNCTION("getCfgFilesSync", ConfigPolicyNapi::NAPIGetCfgFilesSync), + DECLARE_NAPI_FUNCTION("getCfgDirList", ConfigPolicyNapi::NAPIGetCfgDirList), + DECLARE_NAPI_FUNCTION("getCfgDirListSync", ConfigPolicyNapi::NAPIGetCfgDirListSync), + + DECLARE_NAPI_PROPERTY("FollowXMode", nFollowXMode) }; NAPI_CALL(env, napi_define_properties(env, exports, sizeof(property) / sizeof(property[0]), property)); return exports; } +void ConfigPolicyNapi::CreateFollowXModeObject(napi_env env, napi_value value) +{ + napi_value nDefaultMode; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast(FOLLOWX_MODE_DEFAULT), &nDefaultMode)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "DEFAULT", nDefaultMode)); + napi_value nNoFollowMode; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast(FOLLOWX_MODE_NO_RULE_FOLLOWED), + &nNoFollowMode)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "NO_RULE_FOLLOWED", nNoFollowMode)); + napi_value nSimDefaultMode; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast(FOLLOWX_MODE_SIM_DEFAULT), + &nSimDefaultMode)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "SIM_DEFAULT", nSimDefaultMode)); + napi_value nSim1Mode; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast(FOLLOWX_MODE_SIM_1), &nSim1Mode)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "SIM_1", nSim1Mode)); + napi_value nSim2Mode; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast(FOLLOWX_MODE_SIM_2), &nSim2Mode)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "SIM_2", nSim2Mode)); + napi_value nUserDefineMode; + NAPI_CALL_RETURN_VOID(env, napi_create_int32(env, static_cast(FOLLOWX_MODE_USER_DEFINED), + &nUserDefineMode)); + NAPI_CALL_RETURN_VOID(env, napi_set_named_property(env, value, "USER_DEFINED", nUserDefineMode)); +} + napi_value ConfigPolicyNapi::NAPIGetOneCfgFile(napi_env env, napi_callback_info info) { - size_t argc = ARGS_SIZE_TWO; - napi_value argv[ARGS_SIZE_TWO] = {nullptr}; + HiLog::Debug(LABEL, "NAPIGetOneCfgFile start."); + return GetOneCfgFileOrAllCfgFiles(env, info, "NAPIGetOneCfgFile", NativeGetOneCfgFile); +} + +napi_value ConfigPolicyNapi::NAPIGetOneCfgFileSync(napi_env env, napi_callback_info info) +{ + HiLog::Debug(LABEL, "NAPIGetOneCfgFileSync start."); + return GetOneCfgFileOrAllCfgFilesSync(env, info, NativeGetOneCfgFileSync); +} + +napi_value ConfigPolicyNapi::GetOneCfgFileOrAllCfgFilesSync(napi_env env, napi_callback_info info, NapiFunction func) +{ + size_t argc = ARGS_SIZE_THREE; + napi_value argv[ARGS_SIZE_THREE] = {nullptr}; napi_value thisVar = nullptr; void *data = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); @@ -58,23 +108,33 @@ napi_value ConfigPolicyNapi::NAPIGetOneCfgFile(napi_env env, napi_callback_info return ThrowNapiError(env, PARAM_ERROR, "Parameter error. The number of parameters is incorrect."); } - auto asyncContext = std::make_unique(); - ParseRelPath(env, asyncContext->relPath_, argv[ARR_INDEX_ZERO]); + auto asyncContext = std::make_shared(); + asyncContext->followMode_ = FOLLOWX_MODE_DEFAULT; + bool hasExtra = false; + if (ParseRelPath(env, asyncContext->relPath_, argv[ARR_INDEX_ZERO]) == nullptr) { + return nullptr; + } + 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."); + if (ParseFollowMode(env, asyncContext->followMode_, argv[ARR_INDEX_ONE], hasExtra) == nullptr) { + return nullptr; } - napi_create_reference(env, argv[ARR_INDEX_ONE], NAPI_RETURN_ONE, &asyncContext->callbackRef_); } - return HandleAsyncWork(env, asyncContext.release(), "NAPIGetOneCfgFile", NativeGetOneCfgFile, - NativeCallbackComplete); + if (argc >= ARGS_SIZE_THREE) { + hasExtra = true; + if (ParseFollowMode(env, asyncContext->followMode_, argv[ARR_INDEX_ONE], hasExtra) == nullptr || + ParseExtra(env, asyncContext->extra_, argv[ARR_INDEX_TWO]) == nullptr) { + return nullptr; + } + } + return func(env, asyncContext); } -napi_value ConfigPolicyNapi::NAPIGetCfgFiles(napi_env env, napi_callback_info info) +napi_value ConfigPolicyNapi::GetOneCfgFileOrAllCfgFiles(napi_env env, napi_callback_info info, + const std::string &workName, napi_async_execute_callback execute) { - size_t argc = ARGS_SIZE_TWO; - napi_value argv[ARGS_SIZE_TWO] = {nullptr}; + size_t argc = ARGS_SIZE_FOUR; + napi_value argv[ARGS_SIZE_FOUR] = {nullptr}; napi_value thisVar = nullptr; void *data = nullptr; napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); @@ -83,15 +143,59 @@ napi_value ConfigPolicyNapi::NAPIGetCfgFiles(napi_env env, napi_callback_info in } auto asyncContext = std::make_unique(); - ParseRelPath(env, asyncContext->relPath_, argv[ARR_INDEX_ZERO]); + asyncContext->followMode_ = FOLLOWX_MODE_DEFAULT; + bool hasExtra = false; + if (ParseRelPath(env, asyncContext->relPath_, argv[ARR_INDEX_ZERO]) == nullptr) { + return nullptr; + } 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."); + if (MatchValueType(env, argv[ARR_INDEX_ONE], napi_function)) { + napi_create_reference(env, argv[ARR_INDEX_ONE], NAPI_RETURN_ONE, &asyncContext->callbackRef_); + } else { + if (ParseFollowMode(env, asyncContext->followMode_, argv[ARR_INDEX_ONE], hasExtra) == nullptr) { + return nullptr; + } } - napi_create_reference(env, argv[ARR_INDEX_ONE], NAPI_RETURN_ONE, &asyncContext->callbackRef_); } - return HandleAsyncWork(env, asyncContext.release(), "NAPIGetCfgFiles", NativeGetCfgFiles, NativeCallbackComplete); + + if (argc == ARGS_SIZE_THREE) { + if (MatchValueType(env, argv[ARR_INDEX_TWO], napi_function)) { + napi_create_reference(env, argv[ARR_INDEX_TWO], NAPI_RETURN_ONE, &asyncContext->callbackRef_); + } else { + if (ParseExtra(env, asyncContext->extra_, argv[ARR_INDEX_TWO]) == nullptr) { + return nullptr; + } + hasExtra = true; + } + if (ParseFollowMode(env, asyncContext->followMode_, argv[ARR_INDEX_ONE], hasExtra) == nullptr) { + return nullptr; + } + } + + if (argc == ARGS_SIZE_FOUR) { + hasExtra = true; + if (ParseFollowMode(env, asyncContext->followMode_, argv[ARR_INDEX_ONE], hasExtra) == nullptr || + ParseExtra(env, asyncContext->extra_, argv[ARR_INDEX_TWO]) == nullptr) { + return nullptr; + } + if (!MatchValueType(env, argv[ARR_INDEX_THREE], napi_function)) { + return ThrowNapiError(env, PARAM_ERROR, "Parameter error. The fourth parameter must be Callback."); + } + napi_create_reference(env, argv[ARR_INDEX_THREE], NAPI_RETURN_ONE, &asyncContext->callbackRef_); + } + return HandleAsyncWork(env, asyncContext.release(), workName, execute, NativeCallbackComplete); +} + +napi_value ConfigPolicyNapi::NAPIGetCfgFiles(napi_env env, napi_callback_info info) +{ + HiLog::Debug(LABEL, "NAPIGetCfgFiles start."); + return GetOneCfgFileOrAllCfgFiles(env, info, "NAPIGetCfgFiles", NativeGetCfgFiles); +} + +napi_value ConfigPolicyNapi::NAPIGetCfgFilesSync(napi_env env, napi_callback_info info) +{ + HiLog::Debug(LABEL, "NAPIGetCfgFilesSync start."); + return GetOneCfgFileOrAllCfgFilesSync(env, info, NativeGetCfgFilesSync); } napi_value ConfigPolicyNapi::NAPIGetCfgDirList(napi_env env, napi_callback_info info) @@ -114,6 +218,12 @@ napi_value ConfigPolicyNapi::NAPIGetCfgDirList(napi_env env, napi_callback_info NativeCallbackComplete); } +napi_value ConfigPolicyNapi::NAPIGetCfgDirListSync(napi_env env, napi_callback_info info) +{ + auto asyncContext = std::make_shared(); + return NativeGetCfgDirListSync(env, asyncContext); +} + napi_value ConfigPolicyNapi::CreateUndefined(napi_env env) { napi_value result = nullptr; @@ -142,6 +252,7 @@ std::string ConfigPolicyNapi::GetStringFromNAPI(napi_env env, napi_value value) napi_value ConfigPolicyNapi::HandleAsyncWork(napi_env env, ConfigAsyncContext *context, std::string workName, napi_async_execute_callback execute, napi_async_complete_callback complete) { + HiLog::Debug(LABEL, "HandleAsyncWork start."); if (context == nullptr) { HiLog::Error(LABEL, "context is nullptr"); return nullptr; @@ -158,7 +269,8 @@ napi_value ConfigPolicyNapi::HandleAsyncWork(napi_env env, ConfigAsyncContext *c napi_create_string_utf8(env, workName.data(), NAPI_AUTO_LENGTH, &resourceName); napi_create_async_work(env, resource, resourceName, execute, complete, static_cast(context), &context->work_); - napi_queue_async_work_with_qos(env, context->work_, napi_qos_default); + napi_queue_async_work_with_qos(env, context->work_, napi_qos_user_initiated); + HiLog::Debug(LABEL, "HandleAsyncWork end."); return result; } @@ -171,14 +283,20 @@ bool ConfigPolicyNapi::MatchValueType(napi_env env, napi_value value, napi_value void ConfigPolicyNapi::NativeGetOneCfgFile(napi_env env, void *data) { + HiLog::Debug(LABEL, "NativeGetOneCfgFile start."); if (data == nullptr) { HiLog::Error(LABEL, "data is nullptr"); return; } ConfigAsyncContext *asyncCallbackInfo = static_cast(data); char outBuf[MAX_PATH_LEN] = {0}; - GetOneCfgFile(asyncCallbackInfo->relPath_.c_str(), outBuf, MAX_PATH_LEN); - asyncCallbackInfo->pathValue_ = std::string(outBuf); + char *filePath = GetOneCfgFileEx(asyncCallbackInfo->relPath_.c_str(), outBuf, MAX_PATH_LEN, + asyncCallbackInfo->followMode_, asyncCallbackInfo->extra_.c_str()); + if (filePath == nullptr) { + HiLog::Info(LABEL, "GetOneCfgFileEx result is nullptr."); + } else { + asyncCallbackInfo->pathValue_ = std::string(filePath); + } ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getOneCfgFile", ""); asyncCallbackInfo->createValueFunc_ = [](napi_env env, ConfigAsyncContext &context) -> napi_value { napi_value result; @@ -187,27 +305,66 @@ void ConfigPolicyNapi::NativeGetOneCfgFile(napi_env env, void *data) }; } +napi_value ConfigPolicyNapi::NativeGetOneCfgFileSync(napi_env env, std::shared_ptr context) +{ + HiLog::Debug(LABEL, "NativeGetOneCfgFileSync start."); + char outBuf[MAX_PATH_LEN] = {0}; + char *filePath = GetOneCfgFileEx(context->relPath_.c_str(), outBuf, MAX_PATH_LEN, + context->followMode_, context->extra_.c_str()); + if (filePath == nullptr) { + HiLog::Info(LABEL, "GetOneCfgFileEx result is nullptr."); + } else { + context->pathValue_ = std::string(filePath); + } + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getOneCfgFileSync", ""); + napi_value result = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, context->pathValue_.c_str(), NAPI_AUTO_LENGTH, &result)); + return result; +} + void ConfigPolicyNapi::NativeGetCfgFiles(napi_env env, void *data) { + HiLog::Debug(LABEL, "NativeGetCfgFiles start."); if (data == nullptr) { HiLog::Error(LABEL, "data is nullptr"); return; } ConfigAsyncContext *asyncCallbackInfo = static_cast(data); - CfgFiles *cfgFiles = GetCfgFiles(asyncCallbackInfo->relPath_.c_str()); - for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { - if (cfgFiles != nullptr && cfgFiles->paths[i] != nullptr) { - asyncCallbackInfo->paths_.push_back(cfgFiles->paths[i]); + CfgFiles *cfgFiles = GetCfgFilesEx(asyncCallbackInfo->relPath_.c_str(), asyncCallbackInfo->followMode_, + asyncCallbackInfo->extra_.c_str()); + if (cfgFiles != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgFiles->paths[i] != nullptr) { + asyncCallbackInfo->paths_.push_back(cfgFiles->paths[i]); + } } + FreeCfgFiles(cfgFiles); } - FreeCfgFiles(cfgFiles); CreateArraysValueFunc(*asyncCallbackInfo); ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgFiles", ""); } +napi_value ConfigPolicyNapi::NativeGetCfgFilesSync(napi_env env, std::shared_ptr context) +{ + HiLog::Debug(LABEL, "NativeGetCfgFiles start."); + CfgFiles *cfgFiles = GetCfgFilesEx(context->relPath_.c_str(), context->followMode_, + context->extra_.c_str()); + if (cfgFiles != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgFiles->paths[i] != nullptr) { + context->paths_.push_back(cfgFiles->paths[i]); + } + } + FreeCfgFiles(cfgFiles); + } + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgFilesSync", ""); + return CreateArraysValue(env, context); +} + void ConfigPolicyNapi::NativeGetCfgDirList(napi_env env, void *data) { + HiLog::Debug(LABEL, "NativeGetCfgDirList start."); if (data == nullptr) { HiLog::Error(LABEL, "data is nullptr."); return; @@ -225,6 +382,34 @@ void ConfigPolicyNapi::NativeGetCfgDirList(napi_env env, void *data) ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgDirList", ""); } +napi_value ConfigPolicyNapi::NativeGetCfgDirListSync(napi_env env, std::shared_ptr context) +{ + HiLog::Debug(LABEL, "NativeGetCfgDirListSync start."); + CfgDir *cfgDir = GetCfgDirList(); + if (cfgDir != nullptr) { + for (size_t i = 0; i < MAX_CFG_POLICY_DIRS_CNT; i++) { + if (cfgDir->paths[i] != nullptr) { + context->paths_.push_back(cfgDir->paths[i]); + } + } + FreeCfgDirList(cfgDir); + } + ReportConfigPolicyEvent(ReportType::CONFIG_POLICY_EVENT, "getCfgDirListSync", ""); + return CreateArraysValue(env, context); +} + +napi_value ConfigPolicyNapi::CreateArraysValue(napi_env env, std::shared_ptr context) +{ + napi_value result = nullptr; + NAPI_CALL(env, napi_create_array_with_length(env, context->paths_.size(), &result)); + for (size_t i = 0; i < context->paths_.size(); i++) { + napi_value element = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, context->paths_[i].c_str(), NAPI_AUTO_LENGTH, &element)); + NAPI_CALL(env, napi_set_element(env, result, i, element)); + } + return result; +} + void ConfigPolicyNapi::CreateArraysValueFunc(ConfigAsyncContext &asyncCallbackInfo) { asyncCallbackInfo.createValueFunc_ = [](napi_env env, ConfigAsyncContext &context) -> napi_value { @@ -241,6 +426,7 @@ void ConfigPolicyNapi::CreateArraysValueFunc(ConfigAsyncContext &asyncCallbackIn void ConfigPolicyNapi::NativeCallbackComplete(napi_env env, napi_status status, void *data) { + HiLog::Debug(LABEL, "NativeCallbackComplete start."); if (data == nullptr) { HiLog::Error(LABEL, "data is nullptr"); return; @@ -267,6 +453,7 @@ void ConfigPolicyNapi::NativeCallbackComplete(napi_env env, napi_status status, } napi_delete_async_work(env, asyncContext->work_); delete asyncContext; + HiLog::Debug(LABEL, "NativeCallbackComplete end."); } napi_value ConfigPolicyNapi::ParseRelPath(napi_env env, std::string ¶m, napi_value args) @@ -281,6 +468,56 @@ napi_value ConfigPolicyNapi::ParseRelPath(napi_env env, std::string ¶m, napi return result; } +napi_value ConfigPolicyNapi::ParseExtra(napi_env env, std::string ¶m, napi_value args) +{ + bool matchFlag = MatchValueType(env, args, napi_string); + if (!matchFlag) { + return ThrowNapiError(env, PARAM_ERROR, "Parameter error. The type of extra must be string."); + } + param = GetStringFromNAPI(env, args); + napi_value result = nullptr; + NAPI_CALL(env, napi_create_int32(env, NAPI_RETURN_ONE, &result)); + return result; +} + +napi_value ConfigPolicyNapi::ParseFollowMode(napi_env env, int32_t ¶m, napi_value args, bool hasExtra) +{ + bool matchFlag = MatchValueType(env, args, napi_number); + if (!matchFlag) { + return ThrowNapiError(env, PARAM_ERROR, "Parameter error. The type of followMode must be number."); + } + if (napi_get_value_int32(env, args, ¶m) != napi_ok) { + HiLog::Error(LABEL, "can not get int32 value."); + return ThrowNapiError(env, PARAM_ERROR, "Parameter error. Get the value of followMode failed."); + } + + switch (param) { + case FOLLOWX_MODE_DEFAULT: + [[fallthrough]]; + case FOLLOWX_MODE_NO_RULE_FOLLOWED: + [[fallthrough]]; + case FOLLOWX_MODE_SIM_DEFAULT: + [[fallthrough]]; + case FOLLOWX_MODE_SIM_1: + [[fallthrough]]; + case FOLLOWX_MODE_SIM_2: + break; + case FOLLOWX_MODE_USER_DEFINED: + if (!hasExtra) { + return ThrowNapiError(env, PARAM_ERROR, + "Parameter error. The followMode is USER_DEFINED, extra must be set."); + } + break; + default: + return ThrowNapiError(env, PARAM_ERROR, + "Parameter error. The value of followMode should be in the enumeration value of FollowXMode."); + } + + napi_value result = nullptr; + NAPI_CALL(env, napi_create_int32(env, NAPI_RETURN_ONE, &result)); + return result; +} + napi_value ConfigPolicyNapi::ThrowNapiError(napi_env env, int32_t errCode, const std::string &errMessage) { napi_throw_error(env, std::to_string(errCode).c_str(), errMessage.c_str()); diff --git a/test/fuzztest/BUILD.gn b/test/fuzztest/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..76cc189e8623279a4ed602e8809280a5dc892297 --- /dev/null +++ b/test/fuzztest/BUILD.gn @@ -0,0 +1,22 @@ +# Copyright (c) 2023 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. + +group("fuzztest") { + testonly = true + deps = [ + "getcfgfiles_fuzzer:GetCfgFilesFuzzTest", + "getcfgfilesex_fuzzer:GetCfgFilesExFuzzTest", + "getonecfgfile_fuzzer:GetOneCfgFileFuzzTest", + "getonecfgfileex_fuzzer:GetOneCfgFileExFuzzTest", + ] +} diff --git a/test/fuzztest/getcfgfiles_fuzzer/BUILD.gn b/test/fuzztest/getcfgfiles_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..8ba4aa461cb4f4e33647ae070cf2f4c68085ae26 --- /dev/null +++ b/test/fuzztest/getcfgfiles_fuzzer/BUILD.gn @@ -0,0 +1,42 @@ +# Copyright (c) 2023 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "customization/config_policy" + +##############################fuzztest########################################## +ohos_fuzztest("GetCfgFilesFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + include_dirs = [ "../../../interfaces/inner_api/include" ] + configs = [ "../../../common/config:coverage_flags" ] + sources = [ "getcfgfiles_fuzzer.cpp" ] + + deps = [ "../../../frameworks/config_policy:configpolicy_util" ] + + external_deps = [] + + subsystem_name = "customization" + part_name = "config_policy" +} diff --git a/test/fuzztest/getcfgfiles_fuzzer/corpus/init b/test/fuzztest/getcfgfiles_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/test/fuzztest/getcfgfiles_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/getcfgfiles_fuzzer/getcfgfiles_fuzzer.cpp b/test/fuzztest/getcfgfiles_fuzzer/getcfgfiles_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f238cc1f7c5ad729eb40ae299ead9d137d5d26d8 --- /dev/null +++ b/test/fuzztest/getcfgfiles_fuzzer/getcfgfiles_fuzzer.cpp @@ -0,0 +1,45 @@ +/* + * Copyright (c) 2023 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 + +#include "config_policy_utils.h" + +#define FUZZ_PROJECT_NAME "getcfgfiles_fuzzer" + + +namespace OHOS { + bool FuzzGetCfgFiles(const uint8_t* data, size_t size) + { + std::string cfgPath(reinterpret_cast(data), size); + CfgFiles *cfgFiles = GetCfgFiles(cfgPath.c_str()); + bool result = cfgFiles != nullptr; + FreeCfgFiles(cfgFiles); + return result; + } +} + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + // Run your code on data. + OHOS::FuzzGetCfgFiles(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/getcfgfiles_fuzzer/project.xml b/test/fuzztest/getcfgfiles_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..4fdbc407f205680885fa42663163b5c987f123a6 --- /dev/null +++ b/test/fuzztest/getcfgfiles_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/getcfgfilesex_fuzzer/BUILD.gn b/test/fuzztest/getcfgfilesex_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..abe72f3c0b1112fe87dec0d7e218c9b6c6f716a3 --- /dev/null +++ b/test/fuzztest/getcfgfilesex_fuzzer/BUILD.gn @@ -0,0 +1,42 @@ +# Copyright (c) 2023 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "customization/config_policy" + +##############################fuzztest########################################## +ohos_fuzztest("GetCfgFilesExFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + include_dirs = [ "../../../interfaces/inner_api/include" ] + configs = [ "../../../common/config:coverage_flags" ] + sources = [ "getcfgfilesex_fuzzer.cpp" ] + + deps = [ "../../../frameworks/config_policy:configpolicy_util" ] + + external_deps = [] + + subsystem_name = "customization" + part_name = "config_policy" +} diff --git a/test/fuzztest/getcfgfilesex_fuzzer/corpus/init b/test/fuzztest/getcfgfilesex_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/test/fuzztest/getcfgfilesex_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/getcfgfilesex_fuzzer/getcfgfilesex_fuzzer.cpp b/test/fuzztest/getcfgfilesex_fuzzer/getcfgfilesex_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..7a7cc16b0b0dc09a25b38c9b00be63338cbbf0b5 --- /dev/null +++ b/test/fuzztest/getcfgfilesex_fuzzer/getcfgfilesex_fuzzer.cpp @@ -0,0 +1,48 @@ +/* + * Copyright (c) 2023 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 + +#include "config_policy_utils.h" + +#define FUZZ_PROJECT_NAME "getcfgfilesex_fuzzer" + +constexpr size_t MIN_SIZE = 4; + +namespace OHOS { + bool FuzzGetCfgFilesEx(const uint8_t* data, size_t size) + { + std::string cfgPath(reinterpret_cast(data), size / 2); + std::string extra(reinterpret_cast(data) + size / 2, size - size / 2); + int followMode = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + CfgFiles *cfgFiles = GetCfgFilesEx(cfgPath.c_str(), followMode, extra.c_str()); + bool result = cfgFiles != nullptr; + FreeCfgFiles(cfgFiles); + return result; + } +} + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < MIN_SIZE) { + return 0; + } + // Run your code on data. + OHOS::FuzzGetCfgFilesEx(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/getcfgfilesex_fuzzer/project.xml b/test/fuzztest/getcfgfilesex_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..4fdbc407f205680885fa42663163b5c987f123a6 --- /dev/null +++ b/test/fuzztest/getcfgfilesex_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/getonecfgfile_fuzzer/BUILD.gn b/test/fuzztest/getonecfgfile_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..0582d5fa8cb0f88bf7e8499ee76f35080863a5d0 --- /dev/null +++ b/test/fuzztest/getonecfgfile_fuzzer/BUILD.gn @@ -0,0 +1,42 @@ +# Copyright (c) 2023 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "customization/config_policy" + +##############################fuzztest########################################## +ohos_fuzztest("GetOneCfgFileFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + include_dirs = [ "../../../interfaces/inner_api/include" ] + configs = [ "../../../common/config:coverage_flags" ] + sources = [ "getonecfgfile_fuzzer.cpp" ] + + deps = [ "../../../frameworks/config_policy:configpolicy_util" ] + + external_deps = [] + + subsystem_name = "customization" + part_name = "config_policy" +} diff --git a/test/fuzztest/getonecfgfile_fuzzer/corpus/init b/test/fuzztest/getonecfgfile_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/test/fuzztest/getonecfgfile_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/getonecfgfile_fuzzer/getonecfgfile_fuzzer.cpp b/test/fuzztest/getonecfgfile_fuzzer/getonecfgfile_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..47b94c2b21a0e3872c3b60d39b61a68241fe6997 --- /dev/null +++ b/test/fuzztest/getonecfgfile_fuzzer/getonecfgfile_fuzzer.cpp @@ -0,0 +1,44 @@ +/* + * Copyright (c) 2023 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 + +#include "config_policy_utils.h" + +#define FUZZ_PROJECT_NAME "getonecfgfile_fuzzer" + + +namespace OHOS { + bool FuzzGetOneCfgFile(const uint8_t* data, size_t size) + { + std::string userPath(reinterpret_cast(data), size); + char buf[MAX_PATH_LEN] = {0}; + char *filePath = GetOneCfgFile(userPath.c_str(), buf, MAX_PATH_LEN); + return filePath != nullptr; + } +} + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr) { + return 0; + } + // Run your code on data. + OHOS::FuzzGetOneCfgFile(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/getonecfgfile_fuzzer/project.xml b/test/fuzztest/getonecfgfile_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..4fdbc407f205680885fa42663163b5c987f123a6 --- /dev/null +++ b/test/fuzztest/getonecfgfile_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/fuzztest/getonecfgfileex_fuzzer/BUILD.gn b/test/fuzztest/getonecfgfileex_fuzzer/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..df69bd41eee341e02b067660182fc42066ffaf70 --- /dev/null +++ b/test/fuzztest/getonecfgfileex_fuzzer/BUILD.gn @@ -0,0 +1,42 @@ +# Copyright (c) 2023 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. + +#####################hydra-fuzz################### +import("//build/config/features.gni") +import("//build/ohos.gni") +import("//build/test.gni") +module_output_path = "customization/config_policy" + +##############################fuzztest########################################## +ohos_fuzztest("GetOneCfgFileExFuzzTest") { + module_out_path = module_output_path + + fuzz_config_file = "." + + cflags = [ + "-g", + "-O0", + "-Wno-unused-variable", + "-fno-omit-frame-pointer", + ] + include_dirs = [ "../../../interfaces/inner_api/include" ] + configs = [ "../../../common/config:coverage_flags" ] + sources = [ "getonecfgfileex_fuzzer.cpp" ] + + deps = [ "../../../frameworks/config_policy:configpolicy_util" ] + + external_deps = [] + + subsystem_name = "customization" + part_name = "config_policy" +} diff --git a/test/fuzztest/getonecfgfileex_fuzzer/corpus/init b/test/fuzztest/getonecfgfileex_fuzzer/corpus/init new file mode 100644 index 0000000000000000000000000000000000000000..e4ceac1bcd4e3b3427eb63cea0c28304064333cc --- /dev/null +++ b/test/fuzztest/getonecfgfileex_fuzzer/corpus/init @@ -0,0 +1,14 @@ +# Copyright (c) 2023 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. + +FUZZ \ No newline at end of file diff --git a/test/fuzztest/getonecfgfileex_fuzzer/getonecfgfileex_fuzzer.cpp b/test/fuzztest/getonecfgfileex_fuzzer/getonecfgfileex_fuzzer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2439cd7ff333b1216415d9072715f66990ebff74 --- /dev/null +++ b/test/fuzztest/getonecfgfileex_fuzzer/getonecfgfileex_fuzzer.cpp @@ -0,0 +1,47 @@ +/* + * Copyright (c) 2023 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 + +#include "config_policy_utils.h" + +#define FUZZ_PROJECT_NAME "getonecfgfileex_fuzzer" + +constexpr size_t MIN_SIZE = 4; + +namespace OHOS { + bool FuzzGetOneCfgFileEx(const uint8_t* data, size_t size) + { + std::string userPath(reinterpret_cast(data), size / 2); + std::string extra(reinterpret_cast(data) + size / 2, size - size / 2); + int followMode = (data[0] << 24) | (data[1] << 16) | (data[2] << 8) | data[3]; + char buf[MAX_PATH_LEN] = {0}; + char *filePath = GetOneCfgFileEx(userPath.c_str(), buf, MAX_PATH_LEN, followMode, extra.c_str()); + return filePath != nullptr; + } +} + +// Fuzzer entry point. +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) +{ + if (data == nullptr || size < MIN_SIZE) { + return 0; + } + // Run your code on data. + OHOS::FuzzGetOneCfgFileEx(data, size); + return 0; +} \ No newline at end of file diff --git a/test/fuzztest/getonecfgfileex_fuzzer/project.xml b/test/fuzztest/getonecfgfileex_fuzzer/project.xml new file mode 100644 index 0000000000000000000000000000000000000000..4fdbc407f205680885fa42663163b5c987f123a6 --- /dev/null +++ b/test/fuzztest/getonecfgfileex_fuzzer/project.xml @@ -0,0 +1,25 @@ + + + + + + 1000 + + 300 + + 4096 + + diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index d5285a35aee3cffcb8e6b008c73f4e8dcfc3ca24..10642eaffdaadc5da286516c8e0bd61ea8b8599e 100644 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -22,7 +22,7 @@ if (defined(ohos_lite)) { config_policy_sources = [ "config_policy_utils_test.cpp" ] config_policy_include_dirs = [ "../../interfaces/inner_api/include", - "$STARTUP_INIT_ROOT_DIR/services/include/param", + "$STARTUP_INIT_ROOT_DIR/interfaces/innerkits/include/param", "$TEL_CORESERVICE_ROOT_DIR/utils/common/include", ] config_policy_deps = [ "../../frameworks/config_policy:configpolicy_util" ] @@ -44,6 +44,6 @@ if (defined(ohos_lite)) { include_dirs = config_policy_include_dirs deps = config_policy_deps external_deps = [ "init:libbegetutil" ] - resource_config_file = "../resource/ohos_test.xml" + resource_config_file = "./resource/ohos_test.xml" } } diff --git a/test/unittest/config_policy_utils_test.cpp b/test/unittest/config_policy_utils_test.cpp index f5aa2a92b185185bebf13ff17d4c88fc86757dc6..f38d113549847bbc9de20aef5cc263b84ee8e0d4 100644 --- a/test/unittest/config_policy_utils_test.cpp +++ b/test/unittest/config_policy_utils_test.cpp @@ -70,7 +70,7 @@ bool TestGetCfgFile(const char *testPathSuffix, int type, const char *extra) HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest001, TestSize.Level1) { const char *testPathSuffix = "etc/custxmltest/none.xml"; - EXPECT_FALSE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_FOLLOW, NULL)); + EXPECT_FALSE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL)); } /** @@ -82,7 +82,7 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest001, TestSize.Level1) HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest002, TestSize.Level1) { const char *testPathSuffix = "etc/custxmltest/system.xml"; - EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_FOLLOW, NULL)); + EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL)); } /** @@ -94,7 +94,7 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest002, TestSize.Level1) HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest003, TestSize.Level1) { const char *testPathSuffix = "etc/custxmltest/user.xml"; - EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_FOLLOW, NULL)); + EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL)); } /** @@ -106,7 +106,7 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest003, TestSize.Level1) HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest004, TestSize.Level1) { const char *testPathSuffix = "etc/custxmltest/both.xml"; - EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_FOLLOW, NULL)); + EXPECT_TRUE(TestGetCfgFile(testPathSuffix, FOLLOWX_MODE_NO_RULE_FOLLOWED, NULL)); } /** @@ -191,7 +191,7 @@ HWTEST_F(ConfigPolicyUtilsTest, CfgPolicyUtilsFuncTest010, TestSize.Level1) 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())); + EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/user.xml", FOLLOWX_MODE_USER_DEFINED, extraString.c_str())); + EXPECT_TRUE(TestGetCfgFile("etc/custxmltest/both.xml", FOLLOWX_MODE_USER_DEFINED, extraString.c_str())); } } // namespace OHOS diff --git a/test/resource/ohos_test.xml b/test/unittest/resource/ohos_test.xml similarity index 100% rename from test/resource/ohos_test.xml rename to test/unittest/resource/ohos_test.xml