From 760186357677545c09170d50b4422f93fea2e9ab Mon Sep 17 00:00:00 2001 From: chengjinsong2 Date: Mon, 10 Oct 2022 10:34:17 +0800 Subject: [PATCH] =?UTF-8?q?sysparam=20=20API=E6=95=B4=E6=94=B9?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: chengjinsong2 --- device_info/device_info.cpp | 3 +- device_info/device_info_stub.cpp | 11 +- device_info/idevice_info.h | 1 - .../include/syspara/sysparam_errno.h | 7 + interfaces/innerkits/syspara/param_comm.c | 38 +- interfaces/innerkits/syspara/param_comm.h | 1 + interfaces/innerkits/syspara/parameter.c | 19 +- interfaces/kits/BUILD.gn | 1 + interfaces/kits/jskits/BUILD.gn | 21 + .../jskits/src_v9/native_parameters_js.cpp | 424 ++++++++++++++++++ services/param/base/param_comm.c | 4 +- services/param/linux/param_message.h | 2 +- services/param/linux/param_request.c | 5 +- services/param/manager/param_manager.c | 2 +- test/moduletest/BUILD.gn | 3 +- test/moduletest/syspara_moduleTest.cpp | 424 ++++++++++++++++++ test/unittest/syspara/syspara_unittest.cpp | 11 +- 17 files changed, 948 insertions(+), 29 deletions(-) create mode 100644 interfaces/kits/jskits/src_v9/native_parameters_js.cpp create mode 100644 test/moduletest/syspara_moduleTest.cpp diff --git a/device_info/device_info.cpp b/device_info/device_info.cpp index 3c13411fe..0aaa7c3f5 100644 --- a/device_info/device_info.cpp +++ b/device_info/device_info.cpp @@ -18,6 +18,7 @@ #endif #include "param_comm.h" #include "securec.h" +#include "sysparam_errno.h" #ifdef __cplusplus #if __cplusplus @@ -28,7 +29,7 @@ extern "C" { int AclGetDevUdid(char *udid, int size) { if (udid == nullptr || size < UDID_LEN) { - return -1; + return SYSPARAM_INVALID_INPUT; } (void)memset_s(udid, size, 0, size); #ifdef PARAM_FEATURE_DEVICEINFO diff --git a/device_info/device_info_stub.cpp b/device_info/device_info_stub.cpp index 23420bb82..ead28085d 100644 --- a/device_info/device_info_stub.cpp +++ b/device_info/device_info_stub.cpp @@ -23,6 +23,7 @@ #include "string_ex.h" #include "system_ability_definition.h" #include "param_comm.h" +#include "sysparam_errno.h" using std::u16string; namespace OHOS { @@ -35,15 +36,15 @@ REGISTER_SYSTEM_ABILITY_BY_ID(DeviceInfoService, SYSPARAM_DEVICE_SERVICE_ID, tru int32_t DeviceInfoStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { - std::u16string myDescripter = IDeviceInfo::GetDescriptor(); - std::u16string remoteDescripter = data.ReadInterfaceToken(); - DINFO_CHECK(myDescripter == remoteDescripter, return ERR_INVALD_DESC, "Invalid remoteDescripter"); + std::u16string myDescriptor = IDeviceInfo::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + DINFO_CHECK(myDescriptor == remoteDescriptor, return ERR_FAIL, "Invalid remoteDescriptor"); int ret = ERR_FAIL; switch (code) { case COMMAND_GET_UDID: { if (!CheckPermission(data, PERMISSION_UDID)) { - return ERR_FAIL; + return SYSPARAM_PERMISSION_DENIED; } char localDeviceInfo[UDID_LEN] = {0}; ret = GetDevUdid_(localDeviceInfo, UDID_LEN); @@ -53,7 +54,7 @@ int32_t DeviceInfoStub::OnRemoteRequest(uint32_t code, } case COMMAND_GET_SERIAL_ID: { if (!CheckPermission(data, PERMISSION_UDID)) { - return ERR_FAIL; + return SYSPARAM_PERMISSION_DENIED; } const char *serialNumber = GetSerial_(); DINFO_CHECK(serialNumber != nullptr, break, "Failed to get serialNumber"); diff --git a/device_info/idevice_info.h b/device_info/idevice_info.h index d80bf6080..7653f9032 100644 --- a/device_info/idevice_info.h +++ b/device_info/idevice_info.h @@ -33,7 +33,6 @@ public: static constexpr int COMMAND_GET_UDID = MIN_TRANSACTION_ID + 0; static constexpr int COMMAND_GET_SERIAL_ID = MIN_TRANSACTION_ID + 1; static constexpr int ERR_FAIL = -1; - static constexpr int ERR_INVALD_DESC = -1; const std::string PERMISSION_UDID = "ohos.permission.sec.ACCESS_UDID"; }; } // namespace device_info diff --git a/interfaces/innerkits/include/syspara/sysparam_errno.h b/interfaces/innerkits/include/syspara/sysparam_errno.h index a8f740408..0fa4127bd 100755 --- a/interfaces/innerkits/include/syspara/sysparam_errno.h +++ b/interfaces/innerkits/include/syspara/sysparam_errno.h @@ -28,6 +28,13 @@ enum OHOSStartUpSysParamErrorCode { EC_FAILURE = -1, /* Execution failed */ EC_INVALID = -9, /* Invalid argument */ EC_SYSTEM_ERR = -10, /* system error */ + + SYSPARAM_INVALID_INPUT = -401, /*Input parameter is missing or invalid*/ + SYSPARAM_NOT_FOUND = -14700101, /*System parameter can not be found.*/ + SYSPARAM_INVALID_VALUE = -14700102, /*System parameter value is invalid*/ + SYSPARAM_PERMISSION_DENIED = -14700103, /*System permission operation permission denied*/ + SYSPARAM_SYSTEM_ERROR = -14700104, /* System internal error including out of memory, deadlock etc.*/ + SYSPARAM_WAIT_TIMEOUT = -14700105, /* System wait parameter value change time out.*/ }; #ifdef __cplusplus diff --git a/interfaces/innerkits/syspara/param_comm.c b/interfaces/innerkits/syspara/param_comm.c index 23946fcaa..477483178 100644 --- a/interfaces/innerkits/syspara/param_comm.c +++ b/interfaces/innerkits/syspara/param_comm.c @@ -32,6 +32,35 @@ #include "securec.h" #include "beget_ext.h" +INIT_LOCAL_API int GetSystemError(int err) +{ + switch(err) { + case 0: + return 0; + case PARAM_CODE_INVALID_PARAM: + case PARAM_CODE_INVALID_NAME: + case PARAM_CODE_READ_ONLY: + return EC_INVALID; + case PARAM_CODE_INVALID_VALUE: + return SYSPARAM_INVALID_VALUE; + case PARAM_CODE_NOT_FOUND: + case PARAM_CODE_NODE_EXIST: + return SYSPARAM_NOT_FOUND; + case DAC_RESULT_FORBIDED: + return SYSPARAM_PERMISSION_DENIED; + case PARAM_CODE_REACHED_MAX: + case PARAM_CODE_FAIL_CONNECT: + case PARAM_CODE_INVALID_SOCKET: + case PARAM_CODE_NOT_SUPPORT: + return SYSPARAM_SYSTEM_ERROR; + case PARAM_CODE_TIMEOUT: + return SYSPARAM_WAIT_TIMEOUT; + default: + return SYSPARAM_SYSTEM_ERROR; + } + return 0; +} + INIT_LOCAL_API int IsValidParamValue(const char *value, uint32_t len) { if ((value == NULL) || (strlen(value) + 1 > len)) { @@ -48,7 +77,10 @@ INIT_LOCAL_API int GetParameter_(const char *key, const char *def, char *value, uint32_t size = len; int ret = SystemGetParameter(key, NULL, &size); if (ret != 0) { - if (def == NULL || strlen(def) > len) { + if (def == NULL) { + return GetSystemError(ret); + } + if (strlen(def) > len) { return EC_INVALID; } ret = strcpy_s(value, len, def); @@ -56,8 +88,10 @@ INIT_LOCAL_API int GetParameter_(const char *key, const char *def, char *value, } else if (size > len) { return EC_INVALID; } + size = len; - return (SystemGetParameter(key, value, &size) == 0) ? EC_SUCCESS : EC_FAILURE; + ret = SystemGetParameter(key, value, &size); + return GetSystemError(ret); } INIT_LOCAL_API const char *GetProperty(const char *key, const char **paramHolder) diff --git a/interfaces/innerkits/syspara/param_comm.h b/interfaces/innerkits/syspara/param_comm.h index 608341df6..5960b15cb 100755 --- a/interfaces/innerkits/syspara/param_comm.h +++ b/interfaces/innerkits/syspara/param_comm.h @@ -40,6 +40,7 @@ INIT_LOCAL_API int GetDevUdid_(char *udid, int size); INIT_LOCAL_API int IsValidParamValue(const char *value, uint32_t len); INIT_LOCAL_API const char *GetFullName_(void); +INIT_LOCAL_API int GetSystemError(int err); #ifdef __cplusplus #if __cplusplus } diff --git a/interfaces/innerkits/syspara/parameter.c b/interfaces/innerkits/syspara/parameter.c index 2c640823d..b7c914df2 100644 --- a/interfaces/innerkits/syspara/parameter.c +++ b/interfaces/innerkits/syspara/parameter.c @@ -28,7 +28,8 @@ int WaitParameter(const char *key, const char *value, int timeout) { BEGET_CHECK(!(key == NULL || value == NULL), return EC_INVALID); - return SystemWaitParameter(key, value, timeout); + int ret = SystemWaitParameter(key, value, timeout); + return GetSystemError(ret); } uint32_t FindParameter(const char *key) @@ -56,7 +57,10 @@ int GetParameterName(uint32_t handle, char *name, uint32_t len) return EC_INVALID; } int ret = SystemGetParameterName(handle, name, len); - return (ret != 0) ? EC_FAILURE : strlen(name); + if (ret == 0) { + return strlen(name); + } + return GetSystemError(ret); } int GetParameterValue(uint32_t handle, char *value, uint32_t len) @@ -66,7 +70,10 @@ int GetParameterValue(uint32_t handle, char *value, uint32_t len) } uint32_t size = len; int ret = SystemGetParameterValue(handle, value, &size); - return (ret != 0) ? EC_FAILURE : strlen(value); + if (ret == 0) { + return strlen(value); + } + return GetSystemError(ret); } int GetParameter(const char *key, const char *def, char *value, uint32_t len) @@ -84,11 +91,7 @@ int SetParameter(const char *key, const char *value) return EC_INVALID; } int ret = SystemSetParameter(key, value); - if (ret == PARAM_CODE_INVALID_NAME || ret == DAC_RESULT_FORBIDED || - ret == PARAM_CODE_INVALID_PARAM || ret == PARAM_CODE_INVALID_VALUE) { - return EC_INVALID; - } - return (ret == 0) ? EC_SUCCESS : EC_FAILURE; + return GetSystemError(ret); } const char *GetDeviceType(void) diff --git a/interfaces/kits/BUILD.gn b/interfaces/kits/BUILD.gn index ea0ac6f9a..531a7c58b 100755 --- a/interfaces/kits/BUILD.gn +++ b/interfaces/kits/BUILD.gn @@ -24,6 +24,7 @@ group("kitsgroup") { deps += [ "jskits:deviceinfo", "jskits:systemparameter", + "jskits:systemparameterv9", ] } } diff --git a/interfaces/kits/jskits/BUILD.gn b/interfaces/kits/jskits/BUILD.gn index bc451637b..5fc5d5984 100755 --- a/interfaces/kits/jskits/BUILD.gn +++ b/interfaces/kits/jskits/BUILD.gn @@ -49,3 +49,24 @@ ohos_shared_library("systemparameter") { subsystem_name = "startup" part_name = "init" } + +ohos_shared_library("systemparameterv9") { + include_dirs = [ + "src", + "//third_party/libuv/include", + ] + + sources = [ + "src_v9/native_parameters_js.cpp", + ] + + deps = [ + "//base/startup/init/interfaces/innerkits:libbeget_proxy", + "//base/startup/init/interfaces/innerkits:libbegetutil", + ] + + external_deps = [ "napi:ace_napi" ] + relative_install_dir = "module" + subsystem_name = "startup" + part_name = "init" +} diff --git a/interfaces/kits/jskits/src_v9/native_parameters_js.cpp b/interfaces/kits/jskits/src_v9/native_parameters_js.cpp new file mode 100644 index 000000000..16168307f --- /dev/null +++ b/interfaces/kits/jskits/src_v9/native_parameters_js.cpp @@ -0,0 +1,424 @@ +/* + * Copyright (c) 2021 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 "native_parameters_js.h" +#include "sysparam_errno.h" + +static constexpr int ARGC_NUMBER = 2; +static constexpr int ARGC_THREE_NUMBER = 3; +static constexpr int MAX_NAME_LENGTH = 128; +static constexpr int MAX_VALUE_LENGTH = PARAM_CONST_VALUE_LEN_MAX; + +using StorageAsyncContext = struct StorageAsyncContext { + napi_env env = nullptr; + napi_async_work work = nullptr; + + char key[MAX_NAME_LENGTH] = { 0 }; + size_t keyLen = 0; + char value[MAX_VALUE_LENGTH] = { 0 }; + size_t valueLen = 0; + napi_deferred deferred = nullptr; + napi_ref callbackRef = nullptr; + + int status = -1; + std::string getValue; +}; + +using StorageAsyncContextPtr = StorageAsyncContext *; + +static int GetErrorInfo(int status, std::string &errMsg) +{ + switch(status) { + case EC_FAILURE: + case EC_SYSTEM_ERR: + case SYSPARAM_SYSTEM_ERROR: + errMsg = "System internal error including out of memory, deadlock etc"; + return -SYSPARAM_SYSTEM_ERROR; + case EC_INVALID: + case SYSPARAM_INVALID_INPUT: + errMsg = "Input parameter is missing or invalid"; + return -SYSPARAM_INVALID_INPUT; + case SYSPARAM_PERMISSION_DENIED: + errMsg = "System permission operation permission denied"; + return -SYSPARAM_PERMISSION_DENIED; + case SYSPARAM_NOT_FOUND: + errMsg = "System parameter can not be found"; + return -SYSPARAM_NOT_FOUND; + case SYSPARAM_INVALID_VALUE: + errMsg = "System parameter value is invalid"; + return -SYSPARAM_INVALID_VALUE; + default: + errMsg = "System internal error including out of memory, deadlock etc"; + return -SYSPARAM_SYSTEM_ERROR; + } + return 0; +} + +static napi_value BusinessErrorCreate(napi_env env, int status) +{ + + std::string errMsg = ""; + int ret = GetErrorInfo(status, errMsg); + PARAM_JS_LOGV("BusinessErrorCreate status %d err %d msg: %s", status, ret, errMsg.c_str()); + napi_value code = nullptr; + napi_create_int32(env, ret, &code); + napi_value msg = nullptr; + napi_create_string_utf8(env, errMsg.c_str(), NAPI_AUTO_LENGTH, &msg); + napi_value businessError = nullptr; + napi_create_error(env, nullptr, msg, &businessError); + napi_set_named_property(env, businessError, "code", code); + return businessError; +} + +#define PARAM_NAPI_ASSERT(env, assertion, result, info) \ + do { \ + if (!(assertion)) { \ + napi_value d_err = BusinessErrorCreate(env, result); \ + napi_throw(env, d_err); \ + return nullptr; \ + } \ + } while (0) + +static int GetParamString(napi_env env, napi_value arg, char *buffer, size_t maxBuff, size_t *keySize) +{ + (void)napi_get_value_string_utf8(env, arg, nullptr, maxBuff - 1, keySize); + if (*keySize >= maxBuff || *keySize == 0) { + return SYSPARAM_INVALID_INPUT; + } + (void)napi_get_value_string_utf8(env, arg, buffer, maxBuff - 1, keySize); + return 0; +} + +static void SetCallbackWork(napi_env env, StorageAsyncContextPtr asyncContext) +{ + napi_value resource = nullptr; + napi_create_string_utf8(env, "JSStartupSet", NAPI_AUTO_LENGTH, &resource); + napi_create_async_work( + env, nullptr, resource, + [](napi_env env, void *data) { + StorageAsyncContext *asyncContext = (StorageAsyncContext *)data; + asyncContext->status = SetParameter(asyncContext->key, asyncContext->value); + PARAM_JS_LOGV("JSApp set status: %d, key: '%s', value: '%s'.", + asyncContext->status, asyncContext->key, asyncContext->value); + }, + [](napi_env env, napi_status status, void *data) { + StorageAsyncContext *asyncContext = (StorageAsyncContext *)data; + napi_value result[ARGC_NUMBER] = { 0 }; + if (asyncContext->status == 0) { + napi_get_undefined(env, &result[0]); + napi_get_undefined(env, &result[1]); + } else { + result[0] = BusinessErrorCreate(env, asyncContext->status); + napi_get_undefined(env, &result[1]); + } + + if (asyncContext->deferred) { + if (asyncContext->status == 0) { + napi_resolve_deferred(env, asyncContext->deferred, result[1]); + } else { + napi_reject_deferred(env, asyncContext->deferred, result[0]); + } + } else { + napi_value callback = nullptr; + napi_value callResult = nullptr; + napi_get_reference_value(env, asyncContext->callbackRef, &callback); + napi_call_function(env, nullptr, callback, ARGC_NUMBER, result, &callResult); + napi_delete_reference(env, asyncContext->callbackRef); + } + napi_delete_async_work(env, asyncContext->work); + delete asyncContext; + }, + (void *)asyncContext, &asyncContext->work); + napi_queue_async_work(env, asyncContext->work); +} + +static napi_value Set(napi_env env, napi_callback_info info) +{ + size_t argc = ARGC_THREE_NUMBER; + napi_value argv[ARGC_THREE_NUMBER] = { nullptr }; + napi_value thisVar = nullptr; + void *data = nullptr; + napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + PARAM_NAPI_ASSERT(env, argc >= ARGC_NUMBER, SYSPARAM_INVALID_INPUT, "requires 2 parameter"); + StorageAsyncContextPtr asyncContext = new StorageAsyncContext(); + asyncContext->env = env; + for (size_t i = 0; i < argc; i++) { + napi_valuetype valueType = napi_null; + napi_typeof(env, argv[i], &valueType); + int ret = 0; + if (i == 0 && valueType == napi_string) { + ret = GetParamString(env, argv[i], asyncContext->key, MAX_NAME_LENGTH, &asyncContext->keyLen); + } else if (i == 1 && valueType == napi_string) { + ret = GetParamString(env, argv[i], asyncContext->value, MAX_VALUE_LENGTH, &asyncContext->valueLen); + } else if (i == ARGC_NUMBER && valueType == napi_function) { + napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef); + } else { + delete asyncContext; + ret = SYSPARAM_INVALID_INPUT; + } + if (ret != 0) { + ret = (i == 1) ? SYSPARAM_INVALID_VALUE : ret; + napi_value err = BusinessErrorCreate(env, ret); + napi_throw(env, err); + return nullptr; + } + } + PARAM_JS_LOGV("JSApp set key: %s(%d), value: %s(%d).", + asyncContext->key, asyncContext->keyLen, asyncContext->value, asyncContext->valueLen); + + napi_value result = nullptr; + if (asyncContext->callbackRef == nullptr) { + napi_create_promise(env, &asyncContext->deferred, &result); + } else { + napi_get_undefined(env, &result); + } + + SetCallbackWork(env, asyncContext); + return result; +} + +static napi_value SetSync(napi_env env, napi_callback_info info) +{ + size_t argc = ARGC_NUMBER; + napi_value args[ARGC_NUMBER] = { nullptr }; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + PARAM_NAPI_ASSERT(env, argc == ARGC_NUMBER, SYSPARAM_INVALID_INPUT, "Wrong number of arguments"); + napi_valuetype valuetype0 = napi_null; + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); + napi_valuetype valuetype1 = napi_null; + NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1)); + PARAM_NAPI_ASSERT(env, valuetype0 == napi_string && valuetype1 == napi_string, + SYSPARAM_INVALID_INPUT, "Wrong argument type. string expected."); + + size_t keySize = 0; + std::vector keyBuf(MAX_NAME_LENGTH, 0); + int ret = GetParamString(env, args[0], keyBuf.data(), MAX_NAME_LENGTH, &keySize); + if (ret != 0) { + napi_value err = BusinessErrorCreate(env, SYSPARAM_INVALID_INPUT); + napi_throw(env, err); + return nullptr; + } + std::vector value(MAX_VALUE_LENGTH, 0); + size_t valueSize = 0; + ret = GetParamString(env, args[1], value.data(), MAX_VALUE_LENGTH, &valueSize); + if (ret != 0) { + napi_value err = BusinessErrorCreate(env, SYSPARAM_INVALID_VALUE); + napi_throw(env, err); + return nullptr; + } + ret = SetParameter(keyBuf.data(), value.data()); + PARAM_JS_LOGV("JSApp SetSync result:%d, key: '%s'.", ret, keyBuf.data()); + napi_value napiValue = nullptr; + if (ret != 0) { // set failed + napi_value err = BusinessErrorCreate(env, ret); + napi_throw(env, err); + } else { + napi_get_undefined(env, &napiValue); + } + return napiValue; +} + +static napi_value GetSync(napi_env env, napi_callback_info info) +{ + size_t argc = ARGC_NUMBER; + napi_value args[ARGC_NUMBER] = { nullptr }; + NAPI_CALL(env, napi_get_cb_info(env, info, &argc, args, nullptr, nullptr)); + PARAM_NAPI_ASSERT(env, argc == 1 || argc == ARGC_NUMBER, SYSPARAM_INVALID_INPUT, "Wrong number of arguments"); + napi_valuetype valuetype0 = napi_null; + NAPI_CALL(env, napi_typeof(env, args[0], &valuetype0)); + PARAM_NAPI_ASSERT(env, valuetype0 == napi_string, + SYSPARAM_INVALID_INPUT, "Wrong argument type. Numbers expected."); + if (argc == ARGC_NUMBER) { + napi_valuetype valuetype1 = napi_null; + NAPI_CALL(env, napi_typeof(env, args[1], &valuetype1)); + PARAM_NAPI_ASSERT(env, valuetype1 == napi_string, + SYSPARAM_INVALID_INPUT, "Wrong argument type. string expected."); + } + + size_t keySize = 0; + std::vector keyBuf(MAX_NAME_LENGTH, 0); + int ret = GetParamString(env, args[0], keyBuf.data(), MAX_NAME_LENGTH, &keySize); + if (ret != 0) { + napi_value err = BusinessErrorCreate(env, SYSPARAM_INVALID_INPUT); + napi_throw(env, err); + return nullptr; + } + std::vector defValue(MAX_VALUE_LENGTH, 0); + size_t valueSize = 0; + if (argc == ARGC_NUMBER) { + ret = GetParamString(env, args[1], defValue.data(), MAX_VALUE_LENGTH, &valueSize); + if (ret != 0) { + napi_value err = BusinessErrorCreate(env, SYSPARAM_INVALID_INPUT); + napi_throw(env, err); + return nullptr; + } + } + std::vector value(MAX_VALUE_LENGTH, 0); + ret = GetParameter(keyBuf.data(), (valueSize == 0) ? nullptr : defValue.data(), value.data(), MAX_VALUE_LENGTH); + PARAM_JS_LOGV("JSApp get status: %d, key: '%s', value: '%s', defValue: '%s'.", + ret, keyBuf.data(), value.data(), defValue.data()); + if (ret < 0) { + napi_value err = BusinessErrorCreate(env, ret); + napi_throw(env, err); + return nullptr; + } + napi_value napiValue = nullptr; + NAPI_CALL(env, napi_create_string_utf8(env, value.data(), strlen(value.data()), &napiValue)); + return napiValue; +} + +static void GetCallbackWork(napi_env env, StorageAsyncContextPtr asyncContext) +{ + napi_value resource = nullptr; + napi_create_string_utf8(env, "JSStartupGet", NAPI_AUTO_LENGTH, &resource); + napi_create_async_work( + env, nullptr, resource, + [](napi_env env, void *data) { + StorageAsyncContext *asyncContext = (StorageAsyncContext *)data; + std::vector value(MAX_VALUE_LENGTH, 0); + asyncContext->status = GetParameter(asyncContext->key, + (asyncContext->valueLen == 0) ? nullptr : asyncContext->value, value.data(), MAX_VALUE_LENGTH); + asyncContext->getValue = std::string(value.begin(), value.end()); + PARAM_JS_LOGV("JSApp get asyncContext status: %d, key: '%s', value: '%s', defValue: '%s'.", + asyncContext->status, asyncContext->key, asyncContext->getValue.c_str(), asyncContext->value); + }, + [](napi_env env, napi_status status, void *data) { + StorageAsyncContext *asyncContext = (StorageAsyncContext *)data; + napi_value result[ARGC_NUMBER] = { 0 }; + if (asyncContext->status > 0) { + napi_get_undefined(env, &result[0]); + napi_create_string_utf8(env, + asyncContext->getValue.c_str(), strlen(asyncContext->getValue.c_str()), &result[1]); + } else { + result[0] = BusinessErrorCreate(env, asyncContext->status); + napi_get_undefined(env, &result[1]); + } + + if (asyncContext->deferred) { + if (asyncContext->status > 0) { + napi_resolve_deferred(env, asyncContext->deferred, result[1]); + } else { + napi_reject_deferred(env, asyncContext->deferred, result[0]); + } + } else { + napi_value callback = nullptr; + napi_value callResult = nullptr; + napi_get_reference_value(env, asyncContext->callbackRef, &callback); + napi_call_function(env, nullptr, callback, ARGC_NUMBER, result, &callResult); + napi_delete_reference(env, asyncContext->callbackRef); + } + napi_delete_async_work(env, asyncContext->work); + delete asyncContext; + }, + (void *)asyncContext, &asyncContext->work); + napi_queue_async_work(env, asyncContext->work); +} + +static napi_value Get(napi_env env, napi_callback_info info) +{ + size_t argc = ARGC_THREE_NUMBER; + napi_value argv[ARGC_THREE_NUMBER] = { nullptr }; + napi_value thisVar = nullptr; + void *data = nullptr; + napi_get_cb_info(env, info, &argc, argv, &thisVar, &data); + PARAM_NAPI_ASSERT(env, argc >= 1, SYSPARAM_INVALID_INPUT, "requires 1 parameter"); + StorageAsyncContextPtr asyncContext = new StorageAsyncContext(); + asyncContext->env = env; + for (size_t i = 0; i < argc; i++) { + napi_valuetype valueType = napi_null; + napi_typeof(env, argv[i], &valueType); + + int ret = 0; + if (i == 0 && valueType == napi_string) { + ret = GetParamString(env, argv[i], asyncContext->key, MAX_NAME_LENGTH, &asyncContext->keyLen); + } else if (i == 1 && valueType == napi_string) { + ret = GetParamString(env, argv[i], asyncContext->value, MAX_VALUE_LENGTH, &asyncContext->valueLen); + } else if (i == 1 && valueType == napi_function) { + napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef); + break; + } else if (i == ARGC_NUMBER && valueType == napi_function) { + napi_create_reference(env, argv[i], 1, &asyncContext->callbackRef); + } else { + delete asyncContext; + ret = SYSPARAM_INVALID_INPUT; + } + if (ret != 0) { + ret = (i == 1) ? SYSPARAM_INVALID_VALUE : ret; + napi_value err = BusinessErrorCreate(env, ret); + napi_throw(env, err); + return nullptr; + } + } + PARAM_JS_LOGV("JSApp Get key: '%s'(%d), def value: '%s'(%d).", + asyncContext->key, asyncContext->keyLen, asyncContext->value, asyncContext->valueLen); + + napi_value result = nullptr; + if (asyncContext->callbackRef == nullptr) { + napi_create_promise(env, &asyncContext->deferred, &result); + } else { + napi_get_undefined(env, &result); + } + + GetCallbackWork(env, asyncContext); + return result; +} + +EXTERN_C_START +/* + * Module init + */ +static napi_value Init(napi_env env, napi_value exports) +{ + /* + * Attribute definition + */ + napi_property_descriptor desc[] = { + DECLARE_NAPI_FUNCTION("set", Set), + DECLARE_NAPI_FUNCTION("setSync", SetSync), + DECLARE_NAPI_FUNCTION("get", Get), + DECLARE_NAPI_FUNCTION("getSync", GetSync), +#ifdef PARAM_SUPPORT_WAIT + DECLARE_NAPI_FUNCTION("wait", ParamWait), + DECLARE_NAPI_FUNCTION("getWatcher", GetWatcher) +#endif + }; + NAPI_CALL(env, napi_define_properties(env, exports, sizeof(desc) / sizeof(napi_property_descriptor), desc)); +#ifdef PARAM_SUPPORT_WAIT + return RegisterWatcher(env, exports); +#else + return exports; +#endif +} +EXTERN_C_END + +/* + * Module definition + */ +static napi_module _module = { + .nm_version = 1, + .nm_flags = 0, + .nm_filename = NULL, + .nm_register_func = Init, + .nm_modname = "systemParameterV9", + .nm_priv = ((void *)0), + .reserved = { 0 } +}; + +/* + * Module registration function + */ +extern "C" __attribute__((constructor)) void RegisterModule(void) +{ + napi_module_register(&_module); +} diff --git a/services/param/base/param_comm.c b/services/param/base/param_comm.c index 3afe0e3c3..17f2dc2ad 100644 --- a/services/param/base/param_comm.c +++ b/services/param/base/param_comm.c @@ -491,7 +491,7 @@ INIT_LOCAL_API int ReadParamValue(ParamHandle handle, char *value, uint32_t *len PARAM_CHECK(length != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param"); ParamNode *entry = (ParamNode *)GetTrieNodeByHandle(handle); if (entry == NULL) { - return -1; + return PARAM_CODE_NOT_FOUND; } if (value == NULL) { *length = entry->valueLength + 1; @@ -516,7 +516,7 @@ INIT_LOCAL_API int ReadParamName(ParamHandle handle, char *name, uint32_t length PARAM_CHECK(name != NULL, return PARAM_CODE_INVALID_PARAM, "Invalid param"); ParamNode *entry = (ParamNode *)GetTrieNodeByHandle(handle); if (entry == NULL) { - return -1; + return PARAM_CODE_NOT_FOUND; } PARAM_CHECK(length > entry->keyLength, return -1, "Invalid param size %u %u", entry->keyLength, length); int ret = ParamMemcpy(name, length, entry->data, entry->keyLength); diff --git a/services/param/linux/param_message.h b/services/param/linux/param_message.h index 287bffaaf..c344eab6c 100755 --- a/services/param/linux/param_message.h +++ b/services/param/linux/param_message.h @@ -61,7 +61,7 @@ typedef struct { uint32_t watcherId; uint32_t waitId; } id; - char key[PARAM_NAME_LEN_MAX]; + char key[PARAM_NAME_LEN_MAX + 4]; char data[0]; } ParamMessage; diff --git a/services/param/linux/param_request.c b/services/param/linux/param_request.c index 19a6f69df..27568e19c 100644 --- a/services/param/linux/param_request.c +++ b/services/param/linux/param_request.c @@ -132,6 +132,7 @@ static int GetClientSocket(int timeout) static int StartRequest(int clientFd, ParamMessage *request, int timeout) { + errno = 0; ssize_t sendLen = send(clientFd, (char *)request, request->msgSize, 0); if (errno == EINVAL || errno == EACCES) { return PARAM_CODE_INVALID_SOCKET; @@ -173,7 +174,7 @@ int SystemSetParameter(const char *name, const char *value) g_clientFd = GetClientSocket(DEFAULT_PARAM_SET_TIMEOUT); } if (g_clientFd < 0) { - ret = PARAM_CODE_INVALID_PARAM; + ret = DAC_RESULT_FORBIDED; break; } ret = StartRequest(g_clientFd, request, DEFAULT_PARAM_SET_TIMEOUT); @@ -181,7 +182,7 @@ int SystemSetParameter(const char *name, const char *value) close(g_clientFd); g_clientFd = INVALID_SOCKET; retryCount++; - ret = 0; + ret = DAC_RESULT_FORBIDED; } else { break; } diff --git a/services/param/manager/param_manager.c b/services/param/manager/param_manager.c index 3d4ee16c1..ea0e7b15d 100644 --- a/services/param/manager/param_manager.c +++ b/services/param/manager/param_manager.c @@ -365,7 +365,7 @@ int SystemGetParameterCommitId(ParamHandle handle, uint32_t *commitId) ParamNode *entry = (ParamNode *)GetTrieNodeByHandle(handle); if (entry == NULL) { - return -1; + return PARAM_CODE_NOT_FOUND; } *commitId = ReadCommitId(entry); return 0; diff --git a/test/moduletest/BUILD.gn b/test/moduletest/BUILD.gn index 200fadcb6..8d30419a3 100755 --- a/test/moduletest/BUILD.gn +++ b/test/moduletest/BUILD.gn @@ -51,6 +51,7 @@ ohos_moduletest("InitModuleTest") { "modulemgr_moduletest.cpp", "service_control_test.cpp", "service_watcher_moduleTest.cpp", + "syspara_moduleTest.cpp", "test_utils.cpp", ] @@ -65,13 +66,13 @@ ohos_moduletest("InitModuleTest") { deps = [ "//base/startup/init/interfaces/innerkits:libbeget_proxy", "//base/startup/init/interfaces/innerkits:libbegetutil", + "//third_party/bounds_checking_function:libsec_shared", "//third_party/googletest:gtest_main", ] } group("moduletest") { testonly = true - deps = [ ":InitModuleTest" ] } diff --git a/test/moduletest/syspara_moduleTest.cpp b/test/moduletest/syspara_moduleTest.cpp new file mode 100644 index 000000000..7b768eb2e --- /dev/null +++ b/test/moduletest/syspara_moduleTest.cpp @@ -0,0 +1,424 @@ +/* + * 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. + */ + +#include +#include +#include +#include "param_wrapper.h" +#include "parameter.h" +#include "parameters.h" +#include "sysversion.h" +#include "param_comm.h" +#include "init_param.h" +#include "sysparam_errno.h" + +using namespace std; +using namespace testing::ext; +using namespace OHOS; + +const int THREAD_NUM = 5; + +namespace initModuleTest { +static void GetAllParameterTestFunc(void) +{ + EXPECT_STRNE(GetSecurityPatchTag(), nullptr); + EXPECT_STRNE(GetOSFullName(), nullptr); + EXPECT_STRNE(GetVersionId(), nullptr); + EXPECT_STRNE(GetBuildRootHash(), nullptr); + EXPECT_STRNE(GetOsReleaseType(), nullptr); + GetSdkApiVersion(); + EXPECT_STRNE(GetDeviceType(), nullptr); + EXPECT_STRNE(GetProductModel(), nullptr); + EXPECT_STRNE(GetManufacture(), nullptr); + EXPECT_STRNE(GetBrand(), nullptr); + EXPECT_STRNE(GetMarketName(), nullptr); + EXPECT_STRNE(GetProductSeries(), nullptr); + EXPECT_STRNE(GetSoftwareModel(), nullptr); + EXPECT_STRNE(GetHardwareModel(), nullptr); + EXPECT_STRNE(GetHardwareProfile(), nullptr); + EXPECT_STRNE(GetSerial(), nullptr); + EXPECT_STRNE(GetAbiList(), nullptr); + EXPECT_STRNE(GetDisplayVersion(), nullptr); + EXPECT_STRNE(GetIncrementalVersion(), nullptr); + EXPECT_STRNE(GetBootloaderVersion(), nullptr); + EXPECT_STRNE(GetBuildType(), nullptr); + EXPECT_STRNE(GetBuildUser(), nullptr); + EXPECT_STRNE(GetBuildHost(), nullptr); + EXPECT_STRNE(GetBuildTime(), nullptr); + GetFirstApiVersion(); + EXPECT_STRNE(system::GetDeviceType().c_str(), nullptr); +} + +static void GetUdidTestFunc(char* udid, int size) +{ + int ret = GetDevUdid(udid, size); + EXPECT_EQ(ret, 0); + EXPECT_STRNE(udid, nullptr); +} + +static void SetParameterTestFunc(const char *key, const char *value) +{ + EXPECT_EQ(SetParameter(key, value), 0); + uint32_t handle = FindParameter(key); + EXPECT_NE(handle, static_cast(-1)); + uint32_t result = GetParameterCommitId(handle); + EXPECT_NE(result, static_cast(-1)); + char nameGet[64] = {0}; + int ret = GetParameterName(handle, nameGet, 64); + EXPECT_EQ(ret, strlen(nameGet)); + EXPECT_STREQ(key, nameGet); + char valueGet[64] = {0}; + ret = GetParameterValue(handle, valueGet, 64); + EXPECT_EQ(ret, strlen(valueGet)); + EXPECT_STREQ(value, valueGet); + EXPECT_NE(GetSystemCommitId(), 0); +} + +static void GetParameterTestReInt(const char *key, const char *def, char *value, uint32_t len) +{ + int ret = GetParameter(key, def, value, len); + EXPECT_EQ(ret, strlen(value)); + EXPECT_STREQ(value, "v10.1.1"); +} + +static void GetParameterTestFuncReStr(string key, string def) +{ + string ret = system::GetParameter(key, def); + EXPECT_STREQ(ret.c_str(), "v10.1.1"); +} + +static void ParamSetFun(string key, string value) +{ + bool ret = system::SetParameter(key, value); + EXPECT_TRUE(ret); + string testValue = system::GetParameter(key, ""); + EXPECT_STREQ(testValue.c_str(), value.c_str()); +} + +static void TestParameterChange(const char *key, const char *value, void *context) +{ + std::cout<<"TestParameterChange key: "<(-1)); ret = GetParameterValue(handle, valueGet1, 32); - EXPECT_EQ(ret, -1); + EXPECT_EQ(ret, SYSPARAM_NOT_FOUND); ret = GetParameterName(handle, nameGet1, 32); - EXPECT_EQ(ret, -1); + EXPECT_EQ(ret, SYSPARAM_SYSTEM_ERROR); } + HWTEST_F(SysparaUnitTest, parameterTest0013, TestSize.Level0) { long long int out = 0;