未验证 提交 3c168e3e 编写于 作者: O openharmony_ci 提交者: Gitee

!1382 补充ut覆盖率

Merge pull request !1382 from cheng_jinsong/1017ut
......@@ -28,15 +28,13 @@
static int StartProcess(const char *name, const char *extArgv[], int extArgc)
{
if (name == NULL) {
BEGET_LOGE("Start ondemand service failed, service name is null.");
return -1;
}
BEGET_ERROR_CHECK(name != NULL, return -1, "Service name is null.");
int extraArg = 0;
if ((extArgv != NULL) && (extArgc > 0)) {
BEGET_LOGI("Start service by extra args");
extraArg = 1;
}
int ret = 0;
if (extraArg == 1) {
unsigned int len = 0;
for (int i = 0; i < extArgc; i++) {
......@@ -44,53 +42,37 @@ static int StartProcess(const char *name, const char *extArgv[], int extArgc)
}
len += strlen(name) + extArgc + 1;
char *nameValue = (char *)calloc(len, sizeof(char));
if (nameValue == NULL) {
BEGET_LOGE("Failed calloc err=%d", errno);
return -1;
}
if (strncat_s(nameValue, len, name, strlen(name)) != 0) {
BEGET_LOGE("Failed strncat_s name err=%d", errno);
BEGET_ERROR_CHECK(nameValue != NULL, return -1, "Failed calloc err=%d", errno);
ret = strncat_s(nameValue, len, name, strlen(name));
if (ret != 0) {
free(nameValue);
BEGET_LOGE("Failed to cat name");
return -1;
}
for (int j = 0; j < extArgc; j++) {
if (strncat_s(nameValue, len, "|", 1) != 0) {
BEGET_LOGE("Failed strncat_s \"|\"err=%d", errno);
free(nameValue);
return -1;
ret = strncat_s(nameValue, len, "|", 1);
if (ret == 0) {
ret = strncat_s(nameValue, len, extArgv[j], strlen(extArgv[j]));
}
if (strncat_s(nameValue, len, extArgv[j], strlen(extArgv[j])) != 0) {
BEGET_LOGE("Failed strncat_s err=%d", errno);
if (ret != 0) {
free(nameValue);
BEGET_LOGE("Failed to cat name");
return -1;
}
}
if (SystemSetParameter("ohos.ctl.start", nameValue) != 0) {
BEGET_LOGE("Set param for %s failed.\n", nameValue);
free(nameValue);
return -1;
}
ret = SystemSetParameter("ohos.ctl.start", nameValue);
free(nameValue);
} else {
if (SystemSetParameter("ohos.ctl.start", name) != 0) {
BEGET_LOGE("Set param for %s failed.\n", name);
return -1;
}
ret = SystemSetParameter("ohos.ctl.start", name);
}
return 0;
return ret;
}
static int StopProcess(const char *serviceName)
{
if (serviceName == NULL) {
BEGET_LOGE("Stop ondemand service failed, service is null.\n");
return -1;
}
if (SystemSetParameter("ohos.ctl.stop", serviceName) != 0) {
BEGET_LOGE("Set param for %s failed.\n", serviceName);
return -1;
}
return 0;
BEGET_ERROR_CHECK(serviceName != NULL, return -1, "Service name is null.");
return SystemSetParameter("ohos.ctl.stop", serviceName);
}
static int GetCurrentServiceStatus(const char *serviceName, ServiceStatus *status)
......@@ -108,10 +90,7 @@ static int GetCurrentServiceStatus(const char *serviceName, ServiceStatus *statu
static int RestartProcess(const char *serviceName, const char *extArgv[], int extArgc)
{
if (serviceName == NULL) {
BEGET_LOGE("Restart ondemand service failed, service is null.\n");
return -1;
}
BEGET_ERROR_CHECK(serviceName != NULL, return -1, "Service name is null.");
ServiceStatus status = SERVICE_IDLE;
if (GetCurrentServiceStatus(serviceName, &status) != 0) {
BEGET_LOGE("Get service status failed.\n");
......@@ -143,10 +122,7 @@ static int RestartProcess(const char *serviceName, const char *extArgv[], int ex
int ServiceControlWithExtra(const char *serviceName, int action, const char *extArgv[], int extArgc)
{
if (serviceName == NULL) {
BEGET_LOGE("Service wait failed, service is null.\n");
return -1;
}
BEGET_ERROR_CHECK(serviceName != NULL, return -1, "Service name is null.");
int ret = 0;
switch (action) {
case START:
......@@ -168,71 +144,49 @@ int ServiceControlWithExtra(const char *serviceName, int action, const char *ext
int ServiceControl(const char *serviceName, int action)
{
if (serviceName == NULL) {
BEGET_LOGE("Service getctl failed, service is null.");
return -1;
}
BEGET_ERROR_CHECK(serviceName != NULL, return -1, "Service name is null.");
int ret = ServiceControlWithExtra(serviceName, action, NULL, 0);
return ret;
}
int ServiceWaitForStatus(const char *serviceName, ServiceStatus status, int waitTimeout)
static int GetProcessInfo(const char *serviceName, char *nameBuffer, char *valueBuffer, ServiceStatus status)
{
if (serviceName == NULL || waitTimeout <= 0) {
BEGET_LOGE("Service wait failed, service name is null or status invalid %d", status);
return -1;
}
char paramName[PARAM_NAME_LEN_MAX] = {0};
if (snprintf_s(paramName, PARAM_NAME_LEN_MAX, PARAM_NAME_LEN_MAX - 1, "%s.%s",
if (snprintf_s(nameBuffer, PARAM_NAME_LEN_MAX, PARAM_NAME_LEN_MAX - 1, "%s.%s",
STARTUP_SERVICE_CTL, serviceName) == -1) {
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
char value[MAX_INT_LEN] = {0};
if (snprintf_s(value, sizeof(value), sizeof(value) - 1, "%d", (int)status) == -1) {
if (snprintf_s(valueBuffer, MAX_INT_LEN, MAX_INT_LEN - 1, "%d", (int)status) == -1) {
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
if (SystemWaitParameter(paramName, value, waitTimeout) != 0) {
BEGET_LOGE("Wait param for %s failed.", paramName);
return -1;
}
BEGET_LOGI("Success wait");
return 0;
}
int ServiceWaitForStatus(const char *serviceName, ServiceStatus status, int waitTimeout)
{
BEGET_ERROR_CHECK(serviceName != NULL, return -1, "Service name is null.");
BEGET_ERROR_CHECK(waitTimeout >= 0, return -1, "Invalid timeout.");
char paramName[PARAM_NAME_LEN_MAX] = {0};
char value[MAX_INT_LEN] = {0};
int ret = GetProcessInfo(serviceName, paramName, value, status);
BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to get param info.");
return (SystemWaitParameter(paramName, value, waitTimeout) != 0) ? -1 : 0;
}
int ServiceSetReady(const char *serviceName)
{
if (serviceName == NULL) {
BEGET_LOGE("Service wait failed, service is null.");
return -1;
}
BEGET_ERROR_CHECK(serviceName != NULL, return -1, "Service name is null.");
char paramName[PARAM_NAME_LEN_MAX] = {0};
if (snprintf_s(paramName, PARAM_NAME_LEN_MAX, PARAM_NAME_LEN_MAX - 1, "%s.%s",
STARTUP_SERVICE_CTL, serviceName) == -1) {
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
char value[MAX_INT_LEN] = {0};
if (snprintf_s(value, sizeof(value), sizeof(value) - 1, "%d", (int)SERVICE_READY) == -1) {
BEGET_LOGE("Failed snprintf_s err=%d", errno);
return -1;
}
if (SystemSetParameter(paramName, value) != 0) {
BEGET_LOGE("Set param for %s failed.", paramName);
return -1;
}
BEGET_LOGI("Success set %s read", serviceName);
return 0;
int ret = GetProcessInfo(serviceName, paramName, value, SERVICE_READY);
BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to get param info.");
return SystemSetParameter(paramName, value);
}
int StartServiceByTimer(const char *serviceName, uint64_t timeout)
{
if (serviceName == NULL) {
BEGET_LOGE("Request start service by timer with invalid service name");
return -1;
}
BEGET_ERROR_CHECK(serviceName != NULL, return -1, "Service name is null.");
if (timeout == 0) {
// start service immediately.
return ServiceControl(serviceName, START);
......@@ -243,31 +197,11 @@ int StartServiceByTimer(const char *serviceName, uint64_t timeout)
BEGET_LOGE("Failed to build parameter value");
return -1;
}
if (SystemSetParameter("ohos.servicectrl.timer_start", value) != 0) {
BEGET_LOGE("Failed to set parameter \' ohos.servicectrl.timer_start \' with value \' %s \'", value);
return -1;
}
return 0;
return SystemSetParameter("ohos.servicectrl.timer_start", value);
}
int StopServiceTimer(const char *serviceName)
{
if (serviceName == NULL) {
BEGET_LOGE("Request stop service timer with invalid service name");
return -1;
}
char value[PARAM_VALUE_LEN_MAX] = {};
int ret = strncpy_s(value, PARAM_VALUE_LEN_MAX - 1, serviceName, strlen(serviceName));
if (ret < 0) {
BEGET_LOGE("Failed to copy service name to parameter");
return -1;
}
if (SystemSetParameter("ohos.servicectrl.timer_stop", value) != 0) {
BEGET_LOGE("Failed to set parameter \' ohos.servicectrl.timer_stop \' with value \' %s \'", value);
return -1;
}
return 0;
BEGET_ERROR_CHECK(serviceName != NULL, return -1, "Service name is null.");
return SystemSetParameter("ohos.servicectrl.timer_stop", serviceName);
}
......@@ -103,6 +103,25 @@ int InitAddClearServiceHook(ServiceHook hook)
return HookMgrAddEx(GetBootStageHookMgr(), &info);
}
static int JobParseHookWrapper(const HOOK_INFO *hookInfo, void *executionContext)
{
JOB_PARSE_CTX *jobParseContext = (JOB_PARSE_CTX *)executionContext;
JobParseHook realHook = (JobParseHook)hookInfo->hookCookie;
realHook(jobParseContext);
return 0;
};
int InitAddJobParseHook(JobParseHook hook)
{
HOOK_INFO info;
info.stage = INIT_JOB_PARSE;
info.prio = 0;
info.hook = JobParseHookWrapper;
info.hookCookie = (void *)hook;
return HookMgrAddEx(GetBootStageHookMgr(), &info);
}
static int CmdClear(int id, const char *name, int argc, const char **argv)
{
SERVICE_INFO_CTX ctx = {0};
......
......@@ -17,7 +17,18 @@
#define MODULE_REBOOT_ADP_H
#include <stdio.h>
#ifdef __cplusplus
#if __cplusplus
extern "C" {
#endif
#endif
int GetRebootReasonFromMisc(char *reason, size_t size);
int UpdateMiscMessage(const char *valueData, const char *cmd, const char *cmdExt, const char *boot);
#ifdef __cplusplus
#if __cplusplus
}
#endif
#endif
#endif /* MODULE_REBOOT_ADP_H */
......@@ -36,7 +36,7 @@ static int RBMiscWriteUpdaterMessage(const char *path, const struct RBMiscUpdate
{
char *realPath = GetRealPath(path);
BEGET_CHECK_RETURN_VALUE(realPath != NULL, -1);
int ret = 0;
int ret = -1;
FILE *fp = fopen(realPath, "rb+");
free(realPath);
realPath = NULL;
......@@ -44,16 +44,14 @@ static int RBMiscWriteUpdaterMessage(const char *path, const struct RBMiscUpdate
size_t writeLen = fwrite(boot, sizeof(struct RBMiscUpdateMessage), 1, fp);
BEGET_ERROR_CHECK(writeLen == 1, ret = -1, "Failed to write misc for reboot");
(void)fclose(fp);
} else {
ret = -1;
BEGET_LOGE("Failed to open %s", path);
ret = 0;
}
return ret;
}
static int RBMiscReadUpdaterMessage(const char *path, struct RBMiscUpdateMessage *boot)
{
int ret = 0;
int ret = -1;
FILE *fp = NULL;
char *realPath = GetRealPath(path);
if (realPath != NULL) {
......@@ -67,9 +65,7 @@ static int RBMiscReadUpdaterMessage(const char *path, struct RBMiscUpdateMessage
size_t readLen = fread(boot, 1, sizeof(struct RBMiscUpdateMessage), fp);
(void)fclose(fp);
BEGET_ERROR_CHECK(readLen > 0, ret = -1, "Failed to read misc for reboot");
} else {
ret = -1;
BEGET_LOGE("Failed to open %s errno %d", path, errno);
ret = 0;
}
return ret;
}
......@@ -89,7 +85,8 @@ int UpdateMiscMessage(const char *valueData, const char *cmd, const char *cmdExt
{
char miscFile[PATH_MAX] = {0};
int ret = GetBlockDevicePath("/misc", miscFile, PATH_MAX);
BEGET_ERROR_CHECK(ret == 0, return -1, "Failed to get misc path for %s.", valueData);
// no misc do not updater, so return ok
BEGET_ERROR_CHECK(ret == 0, return 0, "Failed to get misc path for %s.", valueData);
// "updater" or "updater:"
struct RBMiscUpdateMessage msg;
......
......@@ -207,30 +207,6 @@ static int GetCommandInfo(const char *cmdLine, int *cmdKeyIndex, char **content)
return 0;
}
/**
* job Config File Parse Hooking
*/
static int JobParseHookWrapper(const HOOK_INFO *hookInfo, void *executionContext)
{
JOB_PARSE_CTX *jobParseContext = (JOB_PARSE_CTX *)executionContext;
JobParseHook realHook = (JobParseHook)hookInfo->hookCookie;
realHook(jobParseContext);
return 0;
};
int InitAddJobParseHook(JobParseHook hook)
{
HOOK_INFO info;
info.stage = INIT_JOB_PARSE;
info.prio = 0;
info.hook = JobParseHookWrapper;
info.hookCookie = (void *)hook;
return HookMgrAddEx(GetBootStageHookMgr(), &info);
}
static void ParseJobHookExecute(const char *name, const cJSON *jobNode)
{
JOB_PARSE_CTX context;
......
......@@ -298,16 +298,27 @@ HWTEST_F(LoopEventUnittest, ProcessWatcherTask, TestSize.Level1)
loopevtest.ProcessWatcherTask();
}
HWTEST_F(LoopEventUnittest, CloseTackUnittest, TestSize.Level1)
static LoopHandle g_loop = nullptr;
static int g_timeCount = 0;
static void Test_ProcessTimer(const TimerHandle taskHandle, void *context)
{
LoopHandle loop = nullptr;
LE_GetSendResult(nullptr);
ASSERT_EQ(LE_CreateLoop(&loop), 0);
((EventLoop *)loop)->runLoop(nullptr);
((EventLoop *)loop)->runLoop = [](const struct EventLoop_ *loop)->LE_STATUS {return LE_SUCCESS;};
LE_RunLoop(loop);
LE_StopLoop(loop);
LE_CloseLoop(loop);
g_timeCount++;
printf("Test_ProcessTimer %d\n", g_timeCount);
if (g_timeCount > 1) {
LE_StopLoop(g_loop);
}
}
HWTEST_F(LoopEventUnittest, LoopRunTest, TestSize.Level1)
{
ASSERT_EQ(LE_CreateLoop(&g_loop), 0);
TimerHandle timer = nullptr;
int ret = LE_CreateTimer(g_loop, &timer, Test_ProcessTimer, nullptr);
ASSERT_EQ(ret, 0);
ret = LE_StartTimer(g_loop, timer, 500, 2);
ASSERT_EQ(ret, 0);
LE_CloseLoop(g_loop);
LE_RunLoop(g_loop);
LE_CloseLoop(g_loop);
}
} // namespace init_ut
......@@ -14,7 +14,6 @@
*/
#include <cinttypes>
#include <string>
#include <sys/mount.h>
#include "fs_manager/fs_manager.h"
#include "init_log.h"
......@@ -22,13 +21,6 @@
#include "securec.h"
#include "systemcapability.h"
#include "service_control.h"
#include "param_wrapper.h"
#include "param_manager.h"
#include "parameters.h"
#include "param_persist.h"
#include "sys_param.h"
#include "init_module_engine.h"
#include "init_control_fd_service.h"
using namespace testing::ext;
using namespace std;
......@@ -221,36 +213,16 @@ HWTEST_F(InnerkitsUnitTest, MountAllWithFstabFile_unittest, TestSize.Level1)
EXPECT_NE(MountAllWithFstabFile("/data/init_ut/etc/fstab.required", 0), 1);
}
HWTEST_F(InnerkitsUnitTest, others_unittest, TestSize.Level1)
// TestSysCap
HWTEST_F(InnerkitsUnitTest, TestSysCap, TestSize.Level1)
{
InitParameterClient();
CheckAndSavePersistParam();
ClosePersistParamWorkSpace();
InitModuleMgrInstall("testModule");
InitModuleMgrDump();
InitModuleMgrUnInstall("testModule");
EXPECT_EQ(HasSystemCapability("test.cap"), 0);
HasSystemCapability(nullptr);
EXPECT_EQ(ServiceSetReady("testservice"), 0);
EXPECT_EQ(StartServiceByTimer("testservice", 1), 0);
EXPECT_EQ(StartServiceByTimer("deviceinfoservice", 0), 0);
const char *extArgv[] = {"testarg"};
EXPECT_EQ(ServiceControlWithExtra("deviceinfoservice", START, extArgv, 1), 0);
EXPECT_EQ(ServiceControlWithExtra("deviceinfoservice", RESTART, extArgv, 1), 0);
EXPECT_EQ(ServiceControlWithExtra("deviceinfoservice", STOP, extArgv, 1), 0);
EXPECT_EQ(ServiceControlWithExtra("deviceinfoservice", RESTART, extArgv, 1), 0);
EXPECT_EQ(StopServiceTimer("testservice"), 0);
std::string value("10");
std::string param("test.param");
EXPECT_EQ(OHOS::system::SetParameter("test.param", value), true);
EXPECT_EQ(OHOS::system::GetStringParameter("test.param", value), 0);
EXPECT_EQ(OHOS::system::GetIntParameter(param, 0), 0);
OHOS::system::GetUintParameter(param, std::numeric_limits<uint8_t>::min(), std::numeric_limits<uint8_t>::max());
EXPECT_EQ(OHOS::system::GetParameter(param, ""), "");
EXPECT_EQ(OHOS::system::GetBoolParameter(param, false), false);
OHOS::system::GetDeviceType();
EXPECT_EQ(LoadParamsFile("/path/to/test", 0), 0);
UmountAllWithFstabFile("/data/init_ut/mount_unitest/ReadFstabFromFile1.fstable");
bool ret = HasSystemCapability("test.cap");
EXPECT_EQ(ret, false);
ret = HasSystemCapability(nullptr);
EXPECT_EQ(ret, false);
ret = HasSystemCapability("ArkUI.ArkUI.Napi");
EXPECT_EQ(ret, true);
ret = HasSystemCapability("SystemCapability.ArkUI.ArkUI.Napi");
EXPECT_EQ(ret, true);
}
} // namespace init_ut
......@@ -92,7 +92,7 @@ HWTEST_F(ModuleMgrUnitTest, ModuleInstallTest, TestSize.Level1)
ASSERT_EQ(cnt, 0);
// Install one module
ret = ModuleMgrInstall(moduleMgr, "bootchart", 0, NULL);
ret = ModuleMgrInstall(moduleMgr, "/system/lib/init/libbootchart", 0, NULL);
ASSERT_EQ(ret, 0);
cnt = ModuleMgrGetCnt(moduleMgr);
ASSERT_EQ(cnt, 1);
......@@ -126,7 +126,7 @@ HWTEST_F(ModuleMgrUnitTest, ModuleInstallTest, TestSize.Level1)
moduleMgr = ModuleMgrScan("init/autorun");
ASSERT_NE(moduleMgr, nullptr);
cnt = ModuleMgrGetCnt(moduleMgr);
ASSERT_EQ(cnt, 0);
ASSERT_GE(cnt, 0);
ModuleMgrUninstall(moduleMgr, NULL);
cnt = ModuleMgrGetCnt(moduleMgr);
......@@ -134,4 +134,46 @@ HWTEST_F(ModuleMgrUnitTest, ModuleInstallTest, TestSize.Level1)
ModuleMgrGetArgs();
ModuleMgrDestroy(moduleMgr);
}
static void TestModuleDump(const MODULE_INFO *moduleInfo)
{
printf("%s\n", moduleInfo->name);
}
HWTEST_F(ModuleMgrUnitTest, ModuleTraversalTest, TestSize.Level1)
{
// Create module manager
MODULE_MGR *moduleMgr = ModuleMgrCreate("init");
ASSERT_NE(moduleMgr, nullptr);
int cnt = ModuleMgrGetCnt(moduleMgr);
ASSERT_EQ(cnt, 0);
// Install one module
int ret = ModuleMgrInstall(moduleMgr, "bootchart", 0, NULL);
ASSERT_EQ(ret, 0);
cnt = ModuleMgrGetCnt(moduleMgr);
ASSERT_EQ(cnt, 1);
ModuleMgrTraversal(moduleMgr, NULL, TestModuleDump);
ModuleMgrDestroy(moduleMgr);
}
HWTEST_F(ModuleMgrUnitTest, ModuleScanTest, TestSize.Level1)
{
// Scan all modules test init
MODULE_MGR *moduleMgr = ModuleMgrScan("init");
ASSERT_NE(moduleMgr, nullptr);
int cnt = ModuleMgrGetCnt(moduleMgr);
ASSERT_GE(cnt, 1);
ModuleMgrUninstall(moduleMgr, NULL);
cnt = ModuleMgrGetCnt(moduleMgr);
ASSERT_EQ(cnt, 0);
ModuleMgrDestroy(moduleMgr);
// scan /lib/init/
moduleMgr = ModuleMgrScan("/lib/init");
ASSERT_NE(moduleMgr, nullptr);
cnt = ModuleMgrGetCnt(moduleMgr);
ASSERT_GE(cnt, 1);
ModuleMgrDestroy(moduleMgr);
}
} // namespace init_ut
......@@ -55,20 +55,9 @@ static int TestGenHashCode(const char *buff)
static void TestSetSelinuxLogCallback(void) {}
static const char *g_forbidWriteParamName[] = {
"ohos.servicectrl.",
"test.permission.read",
"test.persmission.watch"
};
static int TestSetParamCheck(const char *paraName, const char *context, const SrcInfo *info)
{
// forbid to read ohos.servicectrl.
for (size_t i = 0; i < ARRAY_LENGTH(g_forbidWriteParamName); i++) {
if (strncmp(paraName, g_forbidWriteParamName[i], strlen(g_forbidWriteParamName[i])) == 0) {
return g_testPermissionResult;
}
}
BEGET_LOGI("TestSetParamCheck %s result %d", paraName, g_testPermissionResult);
return g_testPermissionResult;
}
......@@ -85,15 +74,15 @@ static const char *TestGetParamLabel(const char *paraName)
return selinuxLabels[code][1];
}
static const char *forbitReadParamName[] = {
static const char *forbidReadParamName[] = {
"ohos.servicectrl.",
// "test.permission.write",
};
static int TestReadParamCheck(const char *paraName)
{
// forbid to read ohos.servicectrl.
for (size_t i = 0; i < ARRAY_LENGTH(forbitReadParamName); i++) {
if (strncmp(paraName, forbitReadParamName[i], strlen(forbitReadParamName[i])) == 0) {
for (size_t i = 0; i < ARRAY_LENGTH(forbidReadParamName); i++) {
if (strncmp(paraName, forbidReadParamName[i], strlen(forbidReadParamName[i])) == 0) {
return 1;
}
}
......@@ -430,6 +419,13 @@ void PrepareInitUnitTestEnv(void)
LoadDefaultParams(STARTUP_INIT_UT_PATH "/vendor/etc/param", LOAD_PARAM_NORMAL);
LoadDefaultParams(STARTUP_INIT_UT_PATH "/system/etc/param", LOAD_PARAM_ONLY_ADD);
LoadParamFromCfg();
// for test int get
SystemWriteParam("test.int.get", "-101");
SystemWriteParam("test.uint.get", "101");
SystemWriteParam("test.string.get", "101");
SystemWriteParam("test.bool.get.true", "true");
SystemWriteParam("test.bool.get.false", "false");
evnOk = 1;
}
......
......@@ -14,6 +14,7 @@
*/
#include <gtest/gtest.h>
#include "bootstage.h"
#include "init_jobs_internal.h"
#include "init_log.h"
#include "init_param.h"
......@@ -63,6 +64,11 @@ static int TestTriggerExecute(TriggerNode *trigger, const char *content, uint32_
return 0;
}
static void Test_JobParseHook(JOB_PARSE_CTX *jobParseCtx)
{
return;
}
class TriggerUnitTest : public ::testing::Test {
public:
TriggerUnitTest() {}
......@@ -90,6 +96,9 @@ public:
int TestLoadTrigger()
{
RegisterBootStateChange(BootStateChange);
InitAddJobParseHook(Test_JobParseHook);
int cmdKeyIndex = 0;
const char *matchCmd = GetMatchCmd("setparam aaaa aaaa", &cmdKeyIndex);
printf("cmd %d \n", matchCmd != nullptr);
......@@ -205,6 +214,14 @@ public:
CheckTrigger(GetTriggerWorkSpace(), TRIGGER_PARAM, buffer, strlen(buffer), TestTriggerExecute);
EXPECT_EQ(1, g_matchTrigger);
EXPECT_EQ(0, strcmp(triggerName, g_matchTriggerName));
// check for bug
g_matchTrigger = 0;
ret = sprintf_s(buffer, sizeof(buffer), "%s=%s", "2222", value);
EXPECT_GE(ret, 0);
CheckTrigger(GetTriggerWorkSpace(), TRIGGER_PARAM, buffer, strlen(buffer), TestTriggerExecute);
EXPECT_EQ(0, g_matchTrigger);
CheckTrigger(GetTriggerWorkSpace(), TRIGGER_PARAM_WATCH, buffer, strlen(buffer), TestTriggerExecute);
return 0;
}
......@@ -472,8 +489,8 @@ public:
int TestDumpTrigger()
{
RegisterBootStateChange(BootStateChange);
(void)AddCompleteJob("param:ohos.servicectrl.display", "ohos.servicectrl.display=*", "display system");
DoTriggerExec("param:ohos.servicectrl.display");
return 0;
}
};
......@@ -579,8 +596,10 @@ HWTEST_F(TriggerUnitTest, TestExecuteParamTrigger5, TestSize.Level0)
TriggerUnitTest test;
test.TestExecuteParamTrigger5();
}
HWTEST_F(TriggerUnitTest, TestExecuteParamTrigger6, TestSize.Level0)
{
TriggerUnitTest test;
test.TestDumpTrigger();
CloseTriggerWorkSpace();
}
......@@ -18,17 +18,19 @@
#include "init_param.h"
#include "init_utils.h"
#include "parameter.h"
#include "sysparam_errno.h"
#include "param_comm.h"
#include "param_stub.h"
#ifndef OHOS_LITE
#include "param_wrapper.h"
#include "parameters.h"
#endif
#include "sysversion.h"
#include "sysparam_errno.h"
using namespace testing::ext;
extern "C" {
int GetIntParameter(const char *key, int def);
}
namespace OHOS {
constexpr int TEST_VALUE = 101;
class SysparaUnitTest : public testing::Test {
public:
static void SetUpTestCase() {}
......@@ -275,25 +277,17 @@ HWTEST_F(SysparaUnitTest, parameterTest0013, TestSize.Level0)
{
long long int out = 0;
unsigned long long int uout = 0;
char key1[] = "test.int";
char value1[] = "101";
int ret = SetParameter(key1, value1);
EXPECT_EQ(ret, 0);
GetParameter_(nullptr, nullptr, nullptr, 0);
#if defined(__LITEOS_A__) || defined(__LITEOS_M__)
EXPECT_EQ(GetIntParameter(key1, 0), 101);
EXPECT_EQ(GetUintParameter(key1, 0), 101);
#else
EXPECT_EQ(GetIntParameter(key1, 0), 0);
EXPECT_EQ(GetUintParameter(key1, 0), 0);
#endif
EXPECT_EQ(GetIntParameter("test.int.get", 0) == -TEST_VALUE, 1);
EXPECT_EQ(GetUintParameter("test.int.get", 0), 0);
EXPECT_EQ(GetIntParameter("test.uint.get", 0), TEST_VALUE);
EXPECT_EQ(GetUintParameter("test.uint.get", 0), TEST_VALUE);
EXPECT_EQ(IsValidParamValue(nullptr, 0), 0);
EXPECT_EQ(IsValidParamValue("testvalue", strlen("testvalue") + 1), 1);
EXPECT_EQ(StringToLL("0x11", &out), 0);
EXPECT_EQ(StringToULL("0x11", &uout), 0);
EXPECT_EQ(StringToLL("not vailed", &out), -1);
EXPECT_EQ(StringToULL("not vailed", &uout), -1);
SystemSetParameter("ohos.boot.sn", "1");
char udid[UDID_LEN] = {0};
GetDevUdid(udid, UDID_LEN);
EXPECT_NE(GetMajorVersion(), 0);
......@@ -301,4 +295,74 @@ HWTEST_F(SysparaUnitTest, parameterTest0013, TestSize.Level0)
GetFeatureVersion();
GetBuildVersion();
}
#ifndef OHOS_LITE
// for test param_wrapper.cpp
HWTEST_F(SysparaUnitTest, parameterTest0014, TestSize.Level0)
{
const std::string key1 = "test.int.get";
int v = OHOS::system::GetIntParameter(key1, 0);
EXPECT_EQ(v, -TEST_VALUE);
int8_t v1 = OHOS::system::GetIntParameter(key1, 0, -127, 128); // -127, 128 range
EXPECT_EQ(v1, -TEST_VALUE);
int16_t v2 = OHOS::system::GetIntParameter(key1, 0, -127, 128); // -127, 128 range
EXPECT_EQ(v2, -TEST_VALUE);
int32_t v3 = OHOS::system::GetIntParameter(key1, 0, -127, 128); // -127, 128 range
EXPECT_EQ(v3, -TEST_VALUE);
int64_t v4 = OHOS::system::GetIntParameter(key1, 0, -127, 128); // -127, 128 range
EXPECT_EQ(v4, -TEST_VALUE);
int8_t v5 = OHOS::system::GetIntParameter(key1, 0, -10, 10); // -10, 10 range
EXPECT_EQ(v5, 0);
const std::string key2 = "test.uint.get";
uint8_t u1 = OHOS::system::GetUintParameter<uint8_t>(key2, 0, (uint8_t)255); // 255 max value
EXPECT_EQ(u1, TEST_VALUE);
uint16_t u2 = OHOS::system::GetUintParameter<uint16_t>(key2, 0, (uint16_t)255); // 255 max value
EXPECT_EQ(u2, TEST_VALUE);
uint32_t u3 = OHOS::system::GetUintParameter<uint32_t>(key2, 0, (uint32_t)255); // 255 max value
EXPECT_EQ(u3, TEST_VALUE);
uint64_t u4 = OHOS::system::GetUintParameter<uint64_t>(key2, 0, (uint64_t)255); // 255 max value
EXPECT_EQ(u4 == TEST_VALUE, 1);
const std::string key3 = "test.uint.get3";
u1 = OHOS::system::GetUintParameter<uint8_t>(key3, 0, (uint8_t)255); // 255 max value
EXPECT_EQ(u1, 0);
u1 = OHOS::system::GetUintParameter<uint8_t>(key2, 0, (uint8_t)10); // 10 max value
EXPECT_EQ(u1, 0);
}
HWTEST_F(SysparaUnitTest, parameterTest0015, TestSize.Level0)
{
std::string type = OHOS::system::GetDeviceType();
printf("device type %s \n", type.c_str());
const std::string key1 = "test.string.get";
std::string v1 = OHOS::system::GetParameter(key1, "");
EXPECT_EQ(strcmp(v1.c_str(), "101"), 0);
const std::string key2 = "test.string.get2";
v1 = OHOS::system::GetParameter(key2, "test2");
EXPECT_EQ(strcmp(v1.c_str(), "test2"), 0);
int ret = OHOS::system::GetStringParameter(key1, v1, "");
EXPECT_EQ(ret, 0);
EXPECT_EQ(strcmp(v1.c_str(), "101"), 0);
ret = OHOS::system::GetStringParameter(key2, v1, "test2");
EXPECT_EQ(ret, 0);
EXPECT_EQ(strcmp(v1.c_str(), "test2"), 0);
}
HWTEST_F(SysparaUnitTest, parameterTest0016, TestSize.Level0)
{
const std::string key1 = "test.bool.get.true";
bool ret = OHOS::system::GetBoolParameter(key1, false);
EXPECT_EQ(ret, true);
const std::string key2 = "test.bool.get.false";
ret = OHOS::system::GetBoolParameter(key2, true);
EXPECT_EQ(ret, false);
const std::string key3 = "test.bool.get3";
ret = OHOS::system::GetBoolParameter(key3, false);
EXPECT_EQ(ret, false);
}
#endif
} // namespace OHOS
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册