mirror of
https://gitee.com/openharmony/startup_init
synced 2024-11-30 22:01:06 +00:00
sysparam API整改
Signed-off-by: chengjinsong2 <chengjinsong2@huawei.com>
This commit is contained in:
parent
b906991c4a
commit
7601863576
@ -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
|
||||
|
@ -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");
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
@ -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)
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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)
|
||||
|
@ -24,6 +24,7 @@ group("kitsgroup") {
|
||||
deps += [
|
||||
"jskits:deviceinfo",
|
||||
"jskits:systemparameter",
|
||||
"jskits:systemparameterv9",
|
||||
]
|
||||
}
|
||||
}
|
||||
|
@ -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"
|
||||
}
|
||||
|
424
interfaces/kits/jskits/src_v9/native_parameters_js.cpp
Normal file
424
interfaces/kits/jskits/src_v9/native_parameters_js.cpp
Normal file
@ -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<char> 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<char> 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<char> 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<char> 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<char> 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<char> 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);
|
||||
}
|
@ -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);
|
||||
|
@ -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;
|
||||
|
||||
|
@ -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;
|
||||
}
|
||||
|
@ -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;
|
||||
|
@ -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" ]
|
||||
}
|
||||
|
||||
|
424
test/moduletest/syspara_moduleTest.cpp
Normal file
424
test/moduletest/syspara_moduleTest.cpp
Normal file
@ -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 <thread>
|
||||
#include <gtest/gtest.h>
|
||||
#include <stdint.h>
|
||||
#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<unsigned int>(-1));
|
||||
uint32_t result = GetParameterCommitId(handle);
|
||||
EXPECT_NE(result, static_cast<unsigned int>(-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: "<<key<<"value: "<<value<<endl;
|
||||
}
|
||||
|
||||
static void TestParameterWatchChange(void)
|
||||
{
|
||||
size_t index = 1;
|
||||
int ret = WatchParameter("test.param.watcher.test1", TestParameterChange, (void *)index);
|
||||
EXPECT_EQ(ret, 0);
|
||||
ret = RemoveParameterWatcher("test.param.watcher.test1", TestParameterChange, (void *)index);
|
||||
EXPECT_EQ(ret, 0);
|
||||
}
|
||||
|
||||
class SysparaModuleTest : public testing::Test {
|
||||
public:
|
||||
static void SetUpTestCase(void) {};
|
||||
static void TearDownTestCase(void) {};
|
||||
void SetUp(void) {};
|
||||
void TearDown(void) {};
|
||||
};
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_SysVersion_test_001, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_SysVersion_test_001 start";
|
||||
GetMajorVersion();
|
||||
GetSeniorVersion();
|
||||
GetFeatureVersion();
|
||||
GetBuildVersion();
|
||||
GTEST_LOG_(INFO) << "Syspara_SysVersion_test_001 end";
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_GetParam_test_002, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_GetParam_test_002 start";
|
||||
for(int i = 0; i < THREAD_NUM; ++i) {
|
||||
std::thread(GetAllParameterTestFunc).join();
|
||||
}
|
||||
GTEST_LOG_(INFO) << "Syspara_GetParam_test_002 end";
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_GetUdid_test_003, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_GetUdid_test_003 start";
|
||||
for(int i = 0; i < THREAD_NUM; ++i) {
|
||||
char udid[UDID_LEN] = {0};
|
||||
std::thread(GetUdidTestFunc, udid, UDID_LEN).join();
|
||||
}
|
||||
GTEST_LOG_(INFO) << "Syspara_GetUdid_test_003 end";
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_SetParameter_test_004, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_SetParameter_test_004 start";
|
||||
char key1[] = "test1.param.version";
|
||||
char value1[] = "v10.1.1";
|
||||
char key2[] = "test2.param.version";
|
||||
char value2[] = "v10.2.2";
|
||||
char key3[] = "test3.param.version";
|
||||
char value3[] = "v10.3.3";
|
||||
std::thread(SetParameterTestFunc, key1, value1).join();
|
||||
std::thread(SetParameterTestFunc, key2, value2).join();
|
||||
std::thread(SetParameterTestFunc, key3, value3).join();
|
||||
GTEST_LOG_(INFO) << "Syspara_SetParameter_test_004 end";
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_SetParameter_test_005, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_SetParameter_test_005 start";
|
||||
//check param name length
|
||||
char key1[] = "test.param.name.xxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxxxxxx";
|
||||
char value[] = "test.value.xxxx";
|
||||
int ret = SetParameter(key1, value);
|
||||
EXPECT_EQ(ret, SYSPARAM_INVALID_INPUT);
|
||||
//check param name, Illegal param name
|
||||
char key2[] = ".test.param.name.xxxxx";
|
||||
ret = SetParameter(key2, value);
|
||||
EXPECT_EQ(ret, SYSPARAM_INVALID_INPUT);
|
||||
char key3[] = "test..param.name.xxxxx";
|
||||
ret = SetParameter(key3, value);
|
||||
EXPECT_EQ(ret, SYSPARAM_INVALID_INPUT);
|
||||
char key4[] = "test..param. .name";
|
||||
ret = SetParameter(key4, value);
|
||||
EXPECT_EQ(ret, SYSPARAM_INVALID_INPUT);
|
||||
//check param name, legal param name
|
||||
char key5[] = "test.param.name.--__.:::";
|
||||
ret = SetParameter(key5, value);
|
||||
EXPECT_EQ(ret, 0);
|
||||
EXPECT_STREQ(value, "test.value.xxxx");
|
||||
char key6[] = "test.param.name.@@@.---";
|
||||
ret = SetParameter(key6, value);
|
||||
EXPECT_EQ(ret, 0);
|
||||
EXPECT_STREQ(value, "test.value.xxxx");
|
||||
//not const param, check param value, bool 8, int 32, other 96
|
||||
char key7[] = "test.param.name.xxxx";
|
||||
char value1[] = "test.value.xxxxxxxxx.xxxxxxxxxxxxx.xxxxxxxxxxxx.xxxxxxxxxxxxxxx.xxxxxxxxxxxxxxxxxxxxxx.xxxxxxxxxx";
|
||||
ret = SetParameter(key7, value1);
|
||||
EXPECT_EQ(ret, SYSPARAM_INVALID_VALUE);
|
||||
char key8[] = "startup.service.ctl.test.int";
|
||||
char value2[] = "111111111111111111111111111111111";
|
||||
ret = SetParameter(key8, value2);
|
||||
EXPECT_EQ(ret, SYSPARAM_INVALID_VALUE);
|
||||
GTEST_LOG_(INFO) << "Syspara_SetParameter_test_005 end";
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_Getparameter_test_006, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_Getparameter_test_006 start";
|
||||
string key = "test.param.set.001";
|
||||
string value = "xxx.xxx.xxx";
|
||||
bool ret = system::SetParameter(key, value);
|
||||
EXPECT_TRUE(ret);
|
||||
string testValue = system::GetParameter(key, "");
|
||||
EXPECT_STREQ(testValue.c_str(), value.c_str());
|
||||
//not read param value,the result is default
|
||||
testValue = system::GetParameter("test.param.set.002", "aaa.aaa.aaa");
|
||||
EXPECT_STREQ(testValue.c_str(), "aaa.aaa.aaa");
|
||||
testValue = system::GetParameter("test.param.set.003", "");
|
||||
EXPECT_STREQ(testValue.c_str(), "");
|
||||
//correct set value
|
||||
string key1 = "test.param.set.bool";
|
||||
ret = system::SetParameter(key1, "1");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_TRUE(system::GetBoolParameter(key1, false));
|
||||
ret = system::SetParameter(key1, "y");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_TRUE(system::GetBoolParameter(key1, false));
|
||||
ret = system::SetParameter(key1, "yes");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_TRUE(system::GetBoolParameter(key1, false));
|
||||
ret = system::SetParameter(key1, "on");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_TRUE(system::GetBoolParameter(key1, false));
|
||||
ret = system::SetParameter(key1, "true");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_TRUE(system::GetBoolParameter(key1, false));
|
||||
ret = system::SetParameter(key1, "0");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_FALSE(system::GetBoolParameter(key1, true));
|
||||
ret = system::SetParameter(key1, "off");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_FALSE(system::GetBoolParameter(key1, true));
|
||||
ret = system::SetParameter(key1, "n");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_FALSE(system::GetBoolParameter(key1, true));
|
||||
ret = system::SetParameter(key1, "no");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_FALSE(system::GetBoolParameter(key1, true));
|
||||
ret = system::SetParameter(key1, "false");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_FALSE(system::GetBoolParameter(key1, true));
|
||||
//set value type not bool,the result get form def
|
||||
ret = system::SetParameter(key1, "test");
|
||||
EXPECT_TRUE(ret);
|
||||
EXPECT_TRUE(system::GetBoolParameter(key1, true));
|
||||
EXPECT_FALSE(system::GetBoolParameter(key1, false));
|
||||
GTEST_LOG_(INFO) << "Syspara_Getparameter_test_006 end";
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_SetParameter_test_007, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_SetParameter_test_007 start";
|
||||
string key1 = "const.param.test";
|
||||
string value1 = "test.param.value.001";
|
||||
|
||||
int ret = 0;
|
||||
string vRet = "";
|
||||
uint32_t handle1 = FindParameter(key1.c_str());
|
||||
cout<<"handle1 is: "<<handle1<<std::endl;
|
||||
if (handle1 == (uint32_t)-1) {
|
||||
ret = SetParameter(key1.c_str(), value1.c_str());
|
||||
EXPECT_EQ(ret, 0);
|
||||
vRet = system::GetParameter(key1, "");
|
||||
EXPECT_STREQ(vRet.c_str(), value1.c_str());
|
||||
}
|
||||
string value2 = "test.param.value.002";
|
||||
ret = SetParameter(key1.c_str(), value2.c_str());
|
||||
EXPECT_EQ(ret, SYSPARAM_INVALID_INPUT);
|
||||
|
||||
string key2 = "ro.param.test";
|
||||
string value3 = "test.param.value.003";
|
||||
uint32_t handle2 = FindParameter(key2.c_str());
|
||||
cout<<"handle2 is: "<<handle2<<std::endl;
|
||||
if (handle2 == (uint32_t)-1) {
|
||||
ret = SetParameter(key2.c_str(), value3.c_str());
|
||||
EXPECT_EQ(ret, 0);
|
||||
vRet = system::GetParameter(key2, "");
|
||||
EXPECT_STREQ(vRet.c_str(), value3.c_str());
|
||||
}
|
||||
string value4 = "test.param.value.004";
|
||||
ret = SetParameter(key2.c_str(), value4.c_str());
|
||||
EXPECT_EQ(ret, SYSPARAM_INVALID_INPUT);
|
||||
GTEST_LOG_(INFO) << "Syspara_SetParameter_test_007 end";
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_GetParameterReIntOrStr_test_008, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_GetParameterReIntOrStr_test_008 start";
|
||||
string key = "test1.param.version";
|
||||
string value = "v10.1.1";
|
||||
int ret = SetParameter(key.c_str(), value.c_str());
|
||||
EXPECT_EQ(ret, 0);
|
||||
char retValue[PARAM_VALUE_LEN_MAX] = {0};
|
||||
for(int i = 0; i < THREAD_NUM; ++i) {
|
||||
std::thread(GetParameterTestReInt, key.c_str(), "", retValue, PARAM_VALUE_LEN_MAX).join();
|
||||
}
|
||||
for(int j = 0; j < THREAD_NUM; ++j) {
|
||||
std::thread(GetParameterTestFuncReStr, key, "").join();
|
||||
}
|
||||
GTEST_LOG_(INFO) << "Syspara_GetParameterReIntOrStr_test_008 end";
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_WaitParameter_test_009, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_WaitParameter_test_009 start";
|
||||
//param already set succeed,waitParamter succeed.
|
||||
char key[] = "test1.param.wait";
|
||||
char value[] = "aaa.bbb.ccc";
|
||||
int ret = SetParameter(key, value);
|
||||
EXPECT_EQ(ret, 0);
|
||||
ret = WaitParameter(key, value, 5);
|
||||
EXPECT_EQ(ret, 0);
|
||||
// param not set,waitParamter will wait param set,return succeed.
|
||||
char key1[] = "test2.param.wait";
|
||||
char value1[] = "aaa.aaa.aaa";
|
||||
std::thread(ParamSetFun, key1, value1).join();
|
||||
ret = WaitParameter(key1, value1, 5);
|
||||
EXPECT_EQ(ret, 0);
|
||||
char key2[] = "test3.param.wait";
|
||||
std::thread(ParamSetFun, key2, "*****").join();
|
||||
ret = WaitParameter(key2, "*****", 5);
|
||||
EXPECT_EQ(ret, 0);
|
||||
// param not set,waitParamter will timeout,return failed.
|
||||
char key3[] = "test4.param.wait";
|
||||
ret = WaitParameter(key3, "*****", 5);
|
||||
EXPECT_EQ(ret, SYSPARAM_WAIT_TIMEOUT);
|
||||
GTEST_LOG_(INFO) << "Syspara_WaitParameter_test_009 end";
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_watcherParameter_test_010, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_watcherParameter_test_010 start";
|
||||
for(int i = 0; i < THREAD_NUM; ++i) {
|
||||
std::thread(TestParameterWatchChange).join();
|
||||
}
|
||||
|
||||
std::thread(ParamSetFun, "test.param.watcher.test1", "test.param.value.xxx").join();
|
||||
|
||||
GTEST_LOG_(INFO) << "Syspara_watcherParameter_test_010 end";
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaModuleTest, Syspara_GetParameter_test_011, TestSize.Level0)
|
||||
{
|
||||
GTEST_LOG_(INFO) << "Syspara_GetParameter_test_011 start";
|
||||
|
||||
char key1[] = "test.param.int1";
|
||||
char value1[] = "0x111111";
|
||||
int setRet = SetParameter(key1, value1);
|
||||
EXPECT_EQ(setRet, 0);
|
||||
int32_t getRet = GetIntParameter(key1, 0);
|
||||
EXPECT_EQ(getRet, 1118481); //0x111111 decimalism result
|
||||
uint32_t uRet = GetUintParameter(key1, 0);
|
||||
EXPECT_EQ(uRet, 1118481);
|
||||
|
||||
char key2[] = "test.param.int2";
|
||||
char value2[] = "-0x111111";
|
||||
setRet = SetParameter(key2, value2);
|
||||
EXPECT_EQ(setRet, 0);
|
||||
getRet = GetIntParameter(key2, 0);
|
||||
EXPECT_EQ(getRet, -1118481); //0x111111 decimalism result
|
||||
|
||||
uRet = GetUintParameter(key2, 0);
|
||||
|
||||
char key3[] = "test.param.int3";
|
||||
char value3[] = "9999999";
|
||||
setRet = SetParameter(key3, value3);
|
||||
EXPECT_EQ(setRet, 0);
|
||||
getRet = GetIntParameter(key3, 0);
|
||||
EXPECT_EQ(getRet, 9999999); //value3 int result
|
||||
uRet = GetUintParameter(key3, 0);
|
||||
EXPECT_EQ(uRet, 9999999); //value3 uint result
|
||||
|
||||
char key4[] = "test.param.int4";
|
||||
char value4[] = "-9999999";
|
||||
setRet = SetParameter(key4, value4);
|
||||
EXPECT_EQ(setRet, 0);
|
||||
getRet = GetIntParameter(key4, 0);
|
||||
EXPECT_EQ(getRet, -9999999); //value4 int result
|
||||
uRet = GetUintParameter(key4, 0);
|
||||
EXPECT_EQ(uRet, 0);
|
||||
|
||||
char key5[] = "test.param.int5";
|
||||
char value5[] = "-2147483648"; //INT32_MIN
|
||||
setRet = SetParameter(key5, value5);
|
||||
EXPECT_EQ(setRet, 0);
|
||||
getRet = GetIntParameter(key5, 0);
|
||||
EXPECT_EQ(getRet, 0);
|
||||
|
||||
char key6[] = "test.param.int6";
|
||||
char value6[] = "2147483647"; //INT32_MAX
|
||||
setRet = SetParameter(key6, value6);
|
||||
EXPECT_EQ(setRet, 0);
|
||||
getRet = GetIntParameter(key6, 0);
|
||||
EXPECT_EQ(getRet, 0);
|
||||
|
||||
char key7[] = "test.param.uint7";
|
||||
char value7[] = "4294967295"; //UINT32_MAX
|
||||
setRet = SetParameter(key7, value7);
|
||||
EXPECT_EQ(setRet, 0);
|
||||
uRet = GetUintParameter(key7, 0);
|
||||
EXPECT_EQ(uRet, 0);
|
||||
|
||||
GTEST_LOG_(INFO) << "Syspara_GetParameter_test_011 end";
|
||||
}
|
||||
}
|
@ -135,7 +135,7 @@ HWTEST_F(SysparaUnitTest, parameterTest005, TestSize.Level0)
|
||||
char value4[] = "rw.sys.version.version.version.version flash_offset = *(hi_u32 *)DT_SetGetU32(&g_Element[0], 0)a\
|
||||
size = *(hi_u32 *)DT_SetGetU32(&g_Element[1], 0)a";
|
||||
int ret = SetParameter(key4, value4);
|
||||
EXPECT_EQ(ret, EC_INVALID);
|
||||
EXPECT_EQ(ret, SYSPARAM_INVALID_VALUE);
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaUnitTest, parameterTest006, TestSize.Level0)
|
||||
@ -171,7 +171,7 @@ HWTEST_F(SysparaUnitTest, parameterTest009, TestSize.Level0)
|
||||
char value5[] = "rw.sys.version.version.version.version \
|
||||
flash_offset = *(hi_u32 *)DT_SetGetU32(&g_Element[0], 0)";
|
||||
int ret = SetParameter(key5, value5);
|
||||
EXPECT_EQ(ret, EC_INVALID);
|
||||
EXPECT_EQ(ret, SYSPARAM_INVALID_VALUE);
|
||||
char valueGet[2] = {0};
|
||||
char defValue3[] = "value of key > 32 ...";
|
||||
ret = GetParameter(key5, defValue3, valueGet, 2);
|
||||
@ -235,7 +235,7 @@ HWTEST_F(SysparaUnitTest, parameterTest0011, TestSize.Level0)
|
||||
EXPECT_EQ(ret, 0);
|
||||
char key2[] = "test.rw.sys.version.wait2";
|
||||
ret = WaitParameter(key2, "*", 1);
|
||||
EXPECT_EQ(ret, 105);
|
||||
EXPECT_EQ(ret, SYSPARAM_WAIT_TIMEOUT);
|
||||
}
|
||||
|
||||
HWTEST_F(SysparaUnitTest, parameterTest0012, TestSize.Level0)
|
||||
@ -262,10 +262,11 @@ HWTEST_F(SysparaUnitTest, parameterTest0012, TestSize.Level0)
|
||||
handle = FindParameter(key2);
|
||||
EXPECT_EQ(handle, static_cast<unsigned int>(-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;
|
||||
|
Loading…
Reference in New Issue
Block a user