diff --git a/interfaces/innerkits/service_control/service_control.c b/interfaces/innerkits/service_control/service_control.c index f7d74a7092803167325d6ee76fe660f421c9f04f..ffee64e291b673aa0de030b4cb53014c0b0d4bb2 100644 --- a/interfaces/innerkits/service_control/service_control.c +++ b/interfaces/innerkits/service_control/service_control.c @@ -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); } diff --git a/services/modules/init_hook/init_hook.c b/services/modules/init_hook/init_hook.c index 3c252b008c5570162bee8a98a5531abdfd85f11b..aa5c8c4f35aaf8b76482f020e50bb845f855a8eb 100755 --- a/services/modules/init_hook/init_hook.c +++ b/services/modules/init_hook/init_hook.c @@ -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}; diff --git a/services/modules/reboot/reboot_adp.h b/services/modules/reboot/reboot_adp.h index 87e4f66aa67ae0554d52301f5d4346a5305ac082..f8cbd99764fc3826cebbee45daafd767d8a1731e 100644 --- a/services/modules/reboot/reboot_adp.h +++ b/services/modules/reboot/reboot_adp.h @@ -17,7 +17,18 @@ #define MODULE_REBOOT_ADP_H #include +#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 */ diff --git a/services/modules/reboot/reboot_misc.c b/services/modules/reboot/reboot_misc.c index 8feed674a68c9b68d723e86fcaed61435e2f92d7..6a2cd55d4cc1e27e031aa10b3fb56a06878afff2 100644 --- a/services/modules/reboot/reboot_misc.c +++ b/services/modules/reboot/reboot_misc.c @@ -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; diff --git a/services/param/trigger/trigger_processor.c b/services/param/trigger/trigger_processor.c index 5bc67839d8f1378b62bc68975a4ef520d555f6cd..c2c659b4458d8edca8291b74c0fc5e45338750d9 100644 --- a/services/param/trigger/trigger_processor.c +++ b/services/param/trigger/trigger_processor.c @@ -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; diff --git a/test/unittest/init/loopevent_unittest.cpp b/test/unittest/init/loopevent_unittest.cpp index caa1deaa935a926577463b6ed64d747b4255ea04..e2b4afaaa0a1f6e23abb1d87917709a72d86a9d5 100644 --- a/test/unittest/init/loopevent_unittest.cpp +++ b/test/unittest/init/loopevent_unittest.cpp @@ -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 diff --git a/test/unittest/innerkits/innerkits_unittest.cpp b/test/unittest/innerkits/innerkits_unittest.cpp index 60da53222b7764a36c3d23f939aec58e0f811b99..dbf68df2db7615422f93f1ae90934e846fe3f1d7 100644 --- a/test/unittest/innerkits/innerkits_unittest.cpp +++ b/test/unittest/innerkits/innerkits_unittest.cpp @@ -14,7 +14,6 @@ */ #include -#include #include #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::min(), std::numeric_limits::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 diff --git a/test/unittest/innerkits/modulemgr_unittest.cpp b/test/unittest/innerkits/modulemgr_unittest.cpp index abb99e0aa626c4b989ea852ceda2199755f33ddb..d5e3c19d5cf69131d43576a32cbef98141c2e42f 100644 --- a/test/unittest/innerkits/modulemgr_unittest.cpp +++ b/test/unittest/innerkits/modulemgr_unittest.cpp @@ -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 diff --git a/test/unittest/param/param_stub.cpp b/test/unittest/param/param_stub.cpp index 3ca37a1c62b4a891c940de88685ed4ca0229ad74..53d8cd2aeada5fac0f157d4bbed03a05cfb4f2f3 100644 --- a/test/unittest/param/param_stub.cpp +++ b/test/unittest/param/param_stub.cpp @@ -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; } diff --git a/test/unittest/param/trigger_unittest.cpp b/test/unittest/param/trigger_unittest.cpp index 80128b08337d4f5d5bec2f55373a5a422315fa9d..86ced24a56fd135b264be6bb0e3147a3f2ca9fd9 100644 --- a/test/unittest/param/trigger_unittest.cpp +++ b/test/unittest/param/trigger_unittest.cpp @@ -14,6 +14,7 @@ */ #include +#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(); } diff --git a/test/unittest/syspara/syspara_unittest.cpp b/test/unittest/syspara/syspara_unittest.cpp index a446e29c18846d8db190e418d4308a0772f01ab5..c822bc03835c8638b4acd642d833b07ff3cae122 100644 --- a/test/unittest/syspara/syspara_unittest.cpp +++ b/test/unittest/syspara/syspara_unittest.cpp @@ -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(key2, 0, (uint8_t)255); // 255 max value + EXPECT_EQ(u1, TEST_VALUE); + uint16_t u2 = OHOS::system::GetUintParameter(key2, 0, (uint16_t)255); // 255 max value + EXPECT_EQ(u2, TEST_VALUE); + uint32_t u3 = OHOS::system::GetUintParameter(key2, 0, (uint32_t)255); // 255 max value + EXPECT_EQ(u3, TEST_VALUE); + uint64_t u4 = OHOS::system::GetUintParameter(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(key3, 0, (uint8_t)255); // 255 max value + EXPECT_EQ(u1, 0); + u1 = OHOS::system::GetUintParameter(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