add unittest cases

Signed-off-by: laiguizhong <laiguizhong@huawei.com>
Change-Id: I90d980be86a0e1a63f59340f65338d07a2fcb50c
This commit is contained in:
laiguizhong 2022-05-30 05:13:14 -07:00
parent a1b4438b36
commit d89ae528ea
28 changed files with 270 additions and 354 deletions

View File

@ -19,6 +19,7 @@
#include <string.h>
#include "list.h"
#include "beget_ext.h"
#include "hookmgr.h"
// Forward declaration
@ -58,17 +59,11 @@ static HOOK_MGR *defaultHookMgr = NULL;
static HOOK_MGR *getHookMgr(HOOK_MGR *hookMgr, int autoCreate)
{
if (hookMgr != NULL) {
return hookMgr;
}
BEGET_CHECK(hookMgr == NULL, return hookMgr);
// Use default HOOK_MGR if possible
if (defaultHookMgr != NULL) {
return defaultHookMgr;
}
BEGET_CHECK(defaultHookMgr == NULL, return defaultHookMgr);
if (!autoCreate) {
return NULL;
}
BEGET_CHECK(autoCreate, return NULL);
// Create default HOOK_MGR if not created
defaultHookMgr = HookMgrCreate("default");
@ -88,9 +83,7 @@ static void hookStageDestroy(ListNode *node)
{
HOOK_STAGE *stage;
if (node == NULL) {
return;
}
BEGET_CHECK(node != NULL, return);
stage = (HOOK_STAGE *)node;
ListRemoveAll(&(stage->hooks), NULL);
@ -103,19 +96,13 @@ static HOOK_STAGE *getHookStage(HOOK_MGR *hookMgr, int stage, int createIfNotFou
HOOK_STAGE *stageItem;
stageItem = (HOOK_STAGE *)ListFind(&(hookMgr->stages), (void *)(&stage), hookStageCompare);
if (stageItem != NULL) {
return stageItem;
}
BEGET_CHECK(stageItem == NULL, return stageItem);
if (!createIfNotFound) {
return NULL;
}
BEGET_CHECK(createIfNotFound, return NULL);
// Not found, create it
stageItem = (HOOK_STAGE *)malloc(sizeof(HOOK_STAGE));
if (stageItem == NULL) {
return NULL;
}
BEGET_CHECK(stageItem != NULL, return NULL);
stageItem->stage = stage;
ListInit(&(stageItem->hooks));
ListAddTail(&(hookMgr->stages), (ListNode *)stageItem);
@ -143,12 +130,8 @@ static int hookItemCompareValue(ListNode *node, void *data)
struct HOOKITEM_COMPARE_VAL *compareVal = (struct HOOKITEM_COMPARE_VAL *)data;
hookItem = (const HOOK_ITEM *)node;
if (hookItem->prio != compareVal->prio) {
return (hookItem->prio - compareVal->prio);
}
if (hookItem->hook == compareVal->hook) {
return 0;
}
BEGET_CHECK(hookItem->prio == compareVal->prio, return (hookItem->prio - compareVal->prio));
BEGET_CHECK(hookItem->hook != compareVal->hook, return 0);
return -1;
}
@ -162,15 +145,11 @@ static int addHookToStage(HOOK_STAGE *hookStage, int prio, OhosHook hook)
compareVal.prio = prio;
compareVal.hook = hook;
hookItem = (HOOK_ITEM *)ListFind(&(hookStage->hooks), (void *)(&compareVal), hookItemCompareValue);
if (hookItem != NULL) {
return 0;
}
BEGET_CHECK(hookItem == NULL, return 0);
// Create new item
hookItem = (HOOK_ITEM *)malloc(sizeof(HOOK_ITEM));
if (hookItem == NULL) {
return -1;
}
BEGET_CHECK(hookItem != NULL, return -1);
hookItem->prio = prio;
hookItem->hook = hook;
hookItem->stage = hookStage;
@ -183,21 +162,15 @@ static int addHookToStage(HOOK_STAGE *hookStage, int prio, OhosHook hook)
int HookMgrAdd(HOOK_MGR *hookMgr, int stage, int prio, OhosHook hook)
{
HOOK_STAGE *stageItem;
if (hook == NULL) {
return -1;
}
BEGET_CHECK(hook != NULL, return -1);
// Get HOOK_MGR
hookMgr = getHookMgr(hookMgr, true);
if (hookMgr == NULL) {
return -1;
}
BEGET_CHECK(hookMgr != NULL, return -1);
// Get HOOK_STAGE list
stageItem = getHookStage(hookMgr, stage, true);
if (stageItem == NULL) {
return -1;
}
BEGET_CHECK(stageItem != NULL, return -1);
// Add hook to stage
return addHookToStage(stageItem, prio, hook);
@ -208,9 +181,7 @@ static int hookTraversalDelProc(ListNode *node, void *cookie)
HOOK_ITEM *hookItem = (HOOK_ITEM *)node;
// Not equal, just return
if ((void *)hookItem->hook != cookie) {
return 0;
}
BEGET_CHECK((void *)hookItem->hook == cookie, return 0);
// Remove from the list
ListRemove(node);
@ -230,15 +201,11 @@ void HookMgrDel(HOOK_MGR *hookMgr, int stage, OhosHook hook)
// Get HOOK_MGR
hookMgr = getHookMgr(hookMgr, 0);
if (hookMgr == NULL) {
return;
}
BEGET_CHECK(hookMgr != NULL, return);
// Get HOOK_STAGE list
stageItem = getHookStage(hookMgr, stage, false);
if (stageItem == NULL) {
return;
}
BEGET_CHECK(stageItem != NULL, return);
if (hook != NULL) {
ListTraversal(&(stageItem->hooks), hook, hookTraversalDelProc, 0);
@ -289,15 +256,11 @@ int HookMgrExecute(HOOK_MGR *hookMgr, int stage, const HOOK_EXEC_ARGS *args)
// Get HOOK_MGR
hookMgr = getHookMgr(hookMgr, 0);
if (hookMgr == NULL) {
return -1;
}
BEGET_CHECK(hookMgr != NULL, return -1)
// Get HOOK_STAGE list
stageItem = getHookStage(hookMgr, stage, false);
if (stageItem == NULL) {
return -1;
}
BEGET_CHECK(stageItem != NULL, return -1);
flags = 0;
if (args != NULL) {
@ -313,13 +276,9 @@ HOOK_MGR *HookMgrCreate(const char *name)
{
HOOK_MGR *ret;
if (name == NULL) {
return NULL;
}
BEGET_CHECK(name != NULL, return NULL);
ret = (HOOK_MGR *)malloc(sizeof(HOOK_MGR));
if (ret == NULL) {
return NULL;
}
BEGET_CHECK(ret != NULL, return NULL);
ret->name = strdup(name);
if (ret->name == NULL) {
@ -333,9 +292,7 @@ HOOK_MGR *HookMgrCreate(const char *name)
void HookMgrDestroy(HOOK_MGR *hookMgr)
{
hookMgr = getHookMgr(hookMgr, 0);
if (hookMgr == NULL) {
return;
}
BEGET_CHECK(hookMgr != NULL, return);
ListRemoveAll(&(hookMgr->stages), hookStageDestroy);
@ -390,14 +347,10 @@ void HookMgrTraversal(HOOK_MGR *hookMgr, void *cookie, OhosHookTraversal travers
{
HOOK_TRAVERSAL_ARGS stageArgs;
if (traversal == NULL) {
return;
}
BEGET_CHECK(traversal != NULL, return);
hookMgr = getHookMgr(hookMgr, 0);
if (hookMgr == NULL) {
return;
}
BEGET_CHECK(hookMgr != NULL, return);
// Prepare common args
stageArgs.hookInfo.cookie = cookie;
@ -414,15 +367,11 @@ int HookMgrGetHooksCnt(HOOK_MGR *hookMgr, int stage)
HOOK_STAGE *stageItem;
hookMgr = getHookMgr(hookMgr, 0);
if (hookMgr == NULL) {
return 0;
}
BEGET_CHECK(hookMgr != NULL, return 0);
// Get HOOK_STAGE list
stageItem = getHookStage(hookMgr, stage, false);
if (stageItem == NULL) {
return 0;
}
BEGET_CHECK(stageItem != NULL, return 0);
return ListGetCnt(&(stageItem->hooks));
}
@ -433,9 +382,7 @@ int HookMgrGetHooksCnt(HOOK_MGR *hookMgr, int stage)
int HookMgrGetStagesCnt(HOOK_MGR *hookMgr)
{
hookMgr = getHookMgr(hookMgr, 0);
if (hookMgr == NULL) {
return 0;
}
BEGET_CHECK(hookMgr != NULL, return 0);
return ListGetCnt(&(hookMgr->stages));
}

View File

@ -13,8 +13,8 @@
* limitations under the License.
*/
#ifndef STARTUP_SYSVERSION_API_H
#define STARTUP_SYSVERSION_API_H
#ifndef PARAM_WRAPPER_H
#define PARAM_WRAPPER_H
#include <string>
@ -25,4 +25,4 @@ int GetIntParameter(const std::string key, int def);
}
}
#endif // STARTUP_SYSVERSION_API_H
#endif // PARAM_WRAPPER_H

View File

@ -44,14 +44,10 @@ MODULE_MGR *ModuleMgrCreate(const char *name)
{
MODULE_MGR *moduleMgr;
if (name == NULL) {
return NULL;
}
BEGET_CHECK(name != NULL, return NULL);
moduleMgr = (MODULE_MGR *)malloc(sizeof(MODULE_MGR));
if (moduleMgr == NULL) {
return NULL;
}
BEGET_CHECK(moduleMgr != NULL, return NULL);
ListInit(&(moduleMgr->modules));
moduleMgr->name = strdup(name);
if (moduleMgr->name == NULL) {
@ -66,14 +62,10 @@ MODULE_MGR *ModuleMgrCreate(const char *name)
void ModuleMgrDestroy(MODULE_MGR *moduleMgr)
{
if (moduleMgr == NULL) {
return;
}
BEGET_CHECK(moduleMgr != NULL, return);
ModuleMgrUninstall(moduleMgr, NULL);
if (moduleMgr->name != NULL) {
free((void *)moduleMgr->name);
}
BEGET_CHECK(moduleMgr->name == NULL, free((void *)moduleMgr->name));
free((void *)moduleMgr);
}
@ -93,17 +85,11 @@ static void moduleDestroy(ListNode *node)
{
MODULE_ITEM *module;
if (node == NULL) {
return;
}
BEGET_CHECK(node != NULL, return);
module = (MODULE_ITEM *)node;
if (module->name != NULL) {
free((void *)module->name);
}
if (module->handle != NULL) {
dlclose(module->handle);
}
BEGET_CHECK(module->name == NULL, free((void *)module->name));
BEGET_CHECK(module->handle == NULL, dlclose(module->handle));
free((void *)module);
}
@ -132,9 +118,7 @@ static void *moduleInstall(MODULE_ITEM *module, int argc, const char *argv[])
currentInstallArgs = &(module->moduleMgr->installArgs);
handle = dlopen(path, RTLD_LAZY | RTLD_GLOBAL);
currentInstallArgs = NULL;
if (handle == NULL) {
BEGET_LOGE("moduleInstall path %s fail %d", path, errno);
}
BEGET_CHECK_ONLY_ELOG(handle != NULL, "moduleInstall path %s fail %d", path, errno);
return handle;
}
@ -147,15 +131,10 @@ int ModuleMgrInstall(MODULE_MGR *moduleMgr, const char *moduleName,
MODULE_ITEM *module;
// Get module manager
if ((moduleMgr == NULL) || (moduleName == NULL)) {
return -1;
}
BEGET_CHECK(!(moduleMgr == NULL || moduleName == NULL), return -1);
// Create module item
module = (MODULE_ITEM *)malloc(sizeof(MODULE_ITEM));
if (module == NULL) {
return -1;
}
BEGET_CHECK(module != NULL, return -1);
module->handle = NULL;
module->moduleMgr = moduleMgr;
@ -189,14 +168,10 @@ static int stringEndsWith(const char *srcStr, const char *endStr)
int srcStrLen = strlen(srcStr);
int endStrLen = strlen(endStr);
if (srcStrLen < endStrLen) {
return -1;
}
BEGET_CHECK(!(srcStrLen < endStrLen), return -1);
srcStr += (srcStrLen - endStrLen);
if (strcmp(srcStr, endStr) == 0) {
return (srcStrLen - endStrLen);
}
BEGET_CHECK(strcmp(srcStr, endStr) != 0, return (srcStrLen - endStrLen));
return -1;
}
@ -208,9 +183,7 @@ static void scanModules(MODULE_MGR *moduleMgr, const char *path)
struct dirent *file;
dir = opendir(path);
if (dir == NULL) {
return;
}
BEGET_CHECK(dir != NULL, return);
while (1) {
file = readdir(dir);
@ -243,18 +216,13 @@ MODULE_MGR *ModuleMgrScan(const char *modulePath)
char path[PATH_MAX];
moduleMgr = ModuleMgrCreate(modulePath);
if (moduleMgr == NULL) {
return NULL;
}
BEGET_CHECK(moduleMgr != NULL, return NULL);
if (modulePath[0] == '/') {
if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "%s", modulePath) < 0) {
return NULL;
}
BEGET_CHECK(!(snprintf_s(path, sizeof(path), sizeof(path) - 1, "%s", modulePath) < 0), return NULL);
} else {
if (snprintf_s(path, sizeof(path), sizeof(path) - 1, "/system/" MODULE_LIB_NAME "/%s", modulePath) < 0) {
return NULL;
};
BEGET_CHECK(!(snprintf_s(path, sizeof(path), sizeof(path) - 1,
"/system/" MODULE_LIB_NAME "/%s", modulePath) < 0), return NULL);
}
scanModules(moduleMgr, path);
@ -275,11 +243,7 @@ static int moduleCompare(ListNode *node, void *data)
void ModuleMgrUninstall(MODULE_MGR *moduleMgr, const char *name)
{
MODULE_ITEM *module;
if (moduleMgr == NULL) {
return;
}
BEGET_CHECK(moduleMgr != NULL, return);
// Uninstall all modules if no name specified
if (name == NULL) {
ListRemoveAll(&(moduleMgr->modules), moduleDestroy);
@ -288,9 +252,7 @@ void ModuleMgrUninstall(MODULE_MGR *moduleMgr, const char *name)
// Find module by name
module = (MODULE_ITEM *)ListFind(&(moduleMgr->modules), (void *)name, moduleCompare);
if (module == NULL) {
return;
}
BEGET_CHECK(module != NULL, return);
// Remove from the list
ListRemove((ListNode *)module);
@ -300,9 +262,6 @@ void ModuleMgrUninstall(MODULE_MGR *moduleMgr, const char *name)
int ModuleMgrGetCnt(const MODULE_MGR *moduleMgr)
{
if (moduleMgr == NULL) {
return 0;
}
BEGET_CHECK(moduleMgr != NULL, return 0);
return ListGetCnt(&(moduleMgr->modules));
}

View File

@ -28,6 +28,7 @@
#include "openssl/sha.h"
#endif
#include "securec.h"
#include "beget_ext.h"
static const char *g_emptyStr = "";
@ -55,9 +56,7 @@ INIT_LOCAL_API int GetParameter_(const char *key, const char *def, char *value,
INIT_LOCAL_API const char *GetProperty(const char *key, const char **paramHolder)
{
if (paramHolder == NULL) {
return NULL;
}
BEGET_CHECK(paramHolder != NULL, return NULL);
if (*paramHolder != NULL) {
return *paramHolder;
}
@ -65,9 +64,7 @@ INIT_LOCAL_API const char *GetProperty(const char *key, const char **paramHolder
int ret = SystemGetParameter(key, NULL, &len);
if (ret == 0 && len > 0) {
char *res = (char *)malloc(len + 1);
if (res == NULL) {
return g_emptyStr;
}
BEGET_CHECK(res != NULL, return g_emptyStr);
ret = SystemGetParameter(key, res, &len);
if (ret != 0) {
free(res);
@ -95,9 +92,7 @@ INIT_LOCAL_API int StringToLL(const char *str, long long int *out)
if (errno != 0) {
return -1;
}
if (s == end || *end != '\0') {
return -1;
}
BEGET_CHECK(!(s == end || *end != '\0'), return -1);
return 0;
}
@ -107,11 +102,7 @@ INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out)
while (isspace(*s)) {
s++;
}
if (s[0] == '-') {
return -1;
}
BEGET_CHECK(s[0] != '-', return -1);
int base = (s[0] == '0' && (s[1] == 'x' || s[1] == 'X')) ? HEX : DECIMAL;
char* end = NULL;
errno = 0;
@ -119,12 +110,8 @@ INIT_LOCAL_API int StringToULL(const char *str, unsigned long long int *out)
if (errno != 0) {
return -1;
}
if (end == s) {
return -1;
}
if (*end != '\0') {
return -1;
}
BEGET_CHECK(end != s, return -1);
BEGET_CHECK(*end == '\0', return -1);
return 0;
}
@ -201,9 +188,7 @@ INIT_LOCAL_API const char *GetSerial_(void)
static char ohos_serial[PARAM_VALUE_LEN_MAX] = {0};
uint32_t len = PARAM_VALUE_LEN_MAX;
int ret = SystemGetParameter("ohos.boot.sn", ohos_serial, &len);
if (ret != 0) {
return NULL;
}
BEGET_CHECK(ret == 0, return NULL);
return ohos_serial;
#endif
}
@ -223,10 +208,8 @@ INIT_LOCAL_API int GetDevUdid_(char *udid, int size)
if (tmpSize <= 0 || tmpSize > DEV_BUF_MAX_LENGTH) {
return -1;
}
char *tmp = (char *)malloc(tmpSize);
if (tmp == NULL) {
return -1;
}
char *tmp = NULL;
BEGET_CHECK((tmp = (char *)malloc(tmpSize)) != NULL, return -1);
(void)memset_s(tmp, tmpSize, 0, tmpSize);
if ((strcat_s(tmp, tmpSize, manufacture) != 0) || (strcat_s(tmp, tmpSize, model) != 0) ||

View File

@ -23,20 +23,17 @@
#include "sysparam_errno.h"
#include "securec.h"
#include "sysversion.h"
#include "beget_ext.h"
int WaitParameter(const char *key, const char *value, int timeout)
{
if ((key == NULL) || (value == NULL)) {
return EC_INVALID;
}
BEGET_CHECK(!(key == NULL || value == NULL), return EC_INVALID);
return SystemWaitParameter(key, value, timeout);
}
uint32_t FindParameter(const char *key)
{
if (key == NULL) {
return (uint32_t)(-1);
}
BEGET_CHECK(key != NULL, return (uint32_t)(-1));
uint32_t handle = 0;
int ret = SystemFindParameter(key, &handle);
if (ret != 0) {
@ -49,9 +46,7 @@ uint32_t GetParameterCommitId(uint32_t handle)
{
uint32_t commitId = 0;
int ret = SystemGetParameterCommitId(handle, &commitId);
if (ret != 0) {
return (uint32_t)(-1);
}
BEGET_CHECK(ret == 0, return (uint32_t)(-1));
return commitId;
}

View File

@ -50,6 +50,9 @@ static void GetVersions(void)
return;
}
const char *fullName = GetFullName_();
if (fullName == NULL) {
return;
}
const char *tmp = strstr(fullName, "-");
if (tmp == NULL) {
return;

View File

@ -28,6 +28,7 @@
#include "param_utils.h"
#include "shell_utils.h"
#include "init_param.h"
#include "beget_ext.h"
#ifdef PARAM_SUPPORT_SELINUX
#include <policycoreutils.h>
#include <selinux/selinux.h>
@ -47,9 +48,7 @@ void demoExit(void)
kill(g_shellPid, SIGKILL);
#endif
}
if (g_isSetTerminal != 0) {
tcsetattr(0, TCSAFLUSH, &g_terminalState);
}
BEGET_CHECK(g_isSetTerminal == 0, tcsetattr(0, TCSAFLUSH, &g_terminalState));
}
#define CMD_PATH "/system/bin/paramshell"
@ -60,9 +59,7 @@ void demoExit(void)
static char *GetLocalBuffer(uint32_t *buffSize)
{
static char buffer[PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX] = {0};
if (buffSize != NULL) {
*buffSize = sizeof(buffer);
}
BEGET_CHECK(buffSize == NULL, *buffSize = sizeof(buffer));
return buffer;
}
@ -138,9 +135,7 @@ int SetParamShellPrompt(BShellHandle shell, const char *param)
static char *GetPermissionString(uint32_t mode, int shift, char *str, int size)
{
if (size < MASK_LENGTH_MAX) {
return str;
}
BEGET_CHECK(!(size < MASK_LENGTH_MAX), return str);
str[0] = '-';
str[1] = '-';
str[2] = '-'; // 2 watcher
@ -161,10 +156,7 @@ static void ShowParam(BShellHandle shell, const char *name, const char *value)
{
ParamAuditData auditData = {};
int ret = GetParamSecurityAuditData(name, 0, &auditData);
if (ret != 0) {
BSH_LOGE("Failed to get param security for %s", name);
return;
}
BSH_CHECK(ret == 0, return, "Failed to get param security for %s", name);
BShellEnvOutput(shell, "Parameter infomation:\r\n");
#ifdef PARAM_SUPPORT_SELINUX
BShellEnvOutput(shell, "selinux : %s \r\n", auditData.label);
@ -258,9 +250,7 @@ static int32_t BShellParamCmdCat(BShellHandle shell, int32_t argc, char *argv[])
char *realParameter = GetRealParameter(shell, argv[1], buffer, buffSize);
BSH_CHECK(realParameter != NULL, return BSH_INVALID_PARAM, "Invalid shell env");
int ret = SystemGetParameter(realParameter, buffer, &buffSize);
if (ret == 0) {
BShellEnvOutput(shell, " %s\r\n", buffer);
}
BSH_CHECK(ret != 0, BShellEnvOutput(shell, " %s\r\n", buffer));
return 0;
}
@ -276,9 +266,7 @@ static void ShowParamForCmdGet(ParamHandle handle, void *cookie)
{
uint32_t buffSize = 0;
char *buffer = GetLocalBuffer(&buffSize);
if (buffSize < (PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX)) {
return;
}
BSH_CHECK(!(buffSize < (PARAM_NAME_LEN_MAX + PARAM_CONST_VALUE_LEN_MAX)), return);
char *value = buffer + PARAM_NAME_LEN_MAX;
(void)SystemGetParameterName(handle, buffer, PARAM_NAME_LEN_MAX);
uint32_t valueLen = buffSize - PARAM_NAME_LEN_MAX;

View File

@ -105,18 +105,14 @@ static int SetPerms(const Service *service)
capData[CAP_TO_INDEX(service->servPerm.caps[i])].inheritable |= CAP_TO_MASK(service->servPerm.caps[i]);
}
if (capset(&capHeader, capData) != 0) {
INIT_LOGE("capset faild for service: %s, error: %d", service->name, errno);
return SERVICE_FAILURE;
}
INIT_ERROR_CHECK(capset(&capHeader, capData) == 0, return SERVICE_FAILURE,
"capset faild for service: %s, error: %d", service->name, errno);
for (unsigned int i = 0; i < service->servPerm.capsCnt; ++i) {
if (service->servPerm.caps[i] == FULL_CAP) {
return SetAllAmbientCapability();
}
if (SetAmbientCapability(service->servPerm.caps[i]) != 0) {
INIT_LOGE("SetAmbientCapability faild for service: %s", service->name);
return SERVICE_FAILURE;
}
INIT_ERROR_CHECK(SetAmbientCapability(service->servPerm.caps[i]) == 0, return SERVICE_FAILURE,
"SetAmbientCapability faild for service: %s", service->name);
}
return SERVICE_SUCCESS;
}
@ -183,10 +179,8 @@ void SetSecon(Service *service)
INIT_LOGI("service %s secon set to %s.", service->name, service->secon);
}
} else {
if (setexeccon("u:r:limit_domain:s0") < 0) {
INIT_LOGE("failed to set service %s's secon (%s).", service->name, "u:r:limit_domain:s0");
_exit(PROCESS_EXIT_CODE);
}
INIT_ERROR_CHECK(!(setexeccon("u:r:limit_domain:s0") < 0), _exit(PROCESS_EXIT_CODE),
"failed to set service %s's secon (%s).", service->name, "u:r:limit_domain:s0");
INIT_LOGE("Please set secon field in service %s's cfg file, limit_domain will be blocked", service->name);
}
#endif // WITH_SELINUX
@ -228,22 +222,16 @@ static void PublishHoldFds(Service *service)
INIT_LOGE("Duplicate file descriptors of Service \' %s \' failed. err = %d", service->name, errno);
continue;
}
if (snprintf_s((char *)fdBuffer + pos, sizeof(fdBuffer) - pos, sizeof(fdBuffer) - 1, "%d ", fd) < 0) {
INIT_LOGE("snprintf_s failed err=%d", errno);
return;
}
INIT_ERROR_CHECK(!(snprintf_s((char *)fdBuffer + pos, sizeof(fdBuffer) - pos, sizeof(fdBuffer) - 1,
"%d ", fd) < 0), return, "snprintf_s failed err=%d", errno);
pos = strlen(fdBuffer);
}
fdBuffer[pos - 1] = '\0'; // Remove last ' '
INIT_LOGI("fd buffer: [%s]", fdBuffer);
char envName[MAX_BUFFER_LEN] = {};
if (snprintf_s(envName, MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1, ENV_FD_HOLD_PREFIX"%s", service->name) < 0) {
INIT_LOGE("snprintf_s failed err=%d", errno);
return;
}
if (setenv(envName, fdBuffer, 1) < 0) {
INIT_LOGE("Failed to set env %s", envName);
}
INIT_ERROR_CHECK(!(snprintf_s(envName, MAX_BUFFER_LEN, MAX_BUFFER_LEN - 1, ENV_FD_HOLD_PREFIX"%s",
service->name) < 0), return, "snprintf_s failed err=%d", errno);
INIT_CHECK_ONLY_ELOG(!(setenv(envName, fdBuffer, 1) < 0), "Failed to set env %s", envName);
INIT_LOGI("File descriptors of Service \' %s \' published", service->name);
}
}
@ -258,10 +246,8 @@ static int BindCpuCore(Service *service)
}
#ifndef __LITEOS_A__
int pid = getpid();
if (sched_setaffinity(pid, sizeof(service->cpuSet), &service->cpuSet) != 0) {
INIT_LOGE("%s set affinity between process(pid=%d) with CPU's core failed", service->name, pid);
return SERVICE_FAILURE;
}
INIT_ERROR_CHECK(sched_setaffinity(pid, sizeof(service->cpuSet), &service->cpuSet) == 0,
return SERVICE_FAILURE, "%s set affinity between process(pid=%d) with CPU's core failed", service->name, pid);
INIT_LOGI("%s set affinity between process(pid=%d) with CPU's core successfully", service->name, pid);
#endif
return SERVICE_SUCCESS;
@ -629,9 +615,7 @@ void ServiceStartTimer(Service *service, uint64_t timeout)
return;
}
status = LE_StartTimer(LE_GetDefaultLoop(), service->timer, timeout, 1);
if (status != LE_SUCCESS) {
INIT_LOGE("Start service timer for service \' %s \' failed, status = %d", service->name, status);
return;
}
INIT_ERROR_CHECK(status == LE_SUCCESS, return,
"Start service timer for service \' %s \' failed, status = %d", service->name, status);
EnableServiceTimer(service);
}

View File

@ -92,14 +92,9 @@ int GetParamValue(const char *symValue, unsigned int symLen, char *paramValue, u
static int SyncExecCommand(int argc, char * const *argv)
{
if (argc == 0 || argv == NULL || argv[0] == NULL) {
return -1;
}
INIT_CHECK(!(argc == 0 || argv == NULL || argv[0] == NULL), return -1);
pid_t pid = fork();
if (pid < 0) {
INIT_LOGE("Fork new process to format failed: %d", errno);
return -1;
}
INIT_ERROR_CHECK(!(pid < 0), return -1, "Fork new process to format failed: %d", errno);
if (pid == 0) {
INIT_CHECK_ONLY_ELOG(execv(argv[0], argv) == 0, "execv %s failed! err %d.", argv[0], errno);
exit(-1);
@ -110,9 +105,8 @@ static int SyncExecCommand(int argc, char * const *argv)
INIT_LOGE("Failed to wait pid %d, errno %d", pid, errno);
return ret;
}
if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
INIT_LOGE("Command %s failed with status %d", argv[0], WEXITSTATUS(status));
}
INIT_CHECK_ONLY_ELOG(!(!WIFEXITED(status) || WEXITSTATUS(status) != 0),
"Command %s failed with status %d", argv[0], WEXITSTATUS(status));
return 0;
}
@ -221,9 +215,7 @@ static void DoExec(const struct CmdArgs *ctx)
if (pid == 0) {
OpenHidebug(ctx->argv[0]);
int ret = execv(ctx->argv[0], ctx->argv);
if (ret == -1) {
INIT_LOGE("DoExec: execute \"%s\" failed: %d.", ctx->argv[0], errno);
}
INIT_CHECK_ONLY_ELOG(ret != -1, "DoExec: execute \"%s\" failed: %d.", ctx->argv[0], errno);
_exit(0x7f);
}
return;

View File

@ -38,26 +38,18 @@ static int ParseRequiredMountInfo(const char *item, Fstab *fstab)
char mountOptions[MAX_BUFFER_LEN] = {};
char partName[PARTITION_NAME_SIZE] = {};
// Sanity checks
if (item == NULL || *item == '\0' || fstab == NULL) {
return -1;
}
INIT_CHECK(!(item == NULL || *item == '\0' || fstab == NULL), return -1);
char *p = NULL;
const char *q = item;
if ((p = strstr(item, "=")) != NULL) {
q = item + strlen(OHOS_REQUIRED_MOUNT_PREFIX); // Get partition name
if (q == NULL || *q == '\0' || (p - q) <= 0) {
return -1;
}
if (strncpy_s(partName, PARTITION_NAME_SIZE -1, q, p - q) != EOK) {
INIT_LOGE("Failed to copy requried partition name");
return -1;
}
INIT_CHECK(!(q == NULL || *q == '\0' || (p - q) <= 0), return -1);
INIT_ERROR_CHECK(strncpy_s(partName, PARTITION_NAME_SIZE -1, q, p - q) == EOK,
return -1, "Failed to copy requried partition name");
p++; // skip '='
if (strncpy_s(mountOptions, MAX_BUFFER_LEN -1, p, strlen(p)) != EOK) {
INIT_LOGE("Failed to copy requried mount info: %s", item);
return -1;
}
INIT_ERROR_CHECK(strncpy_s(mountOptions, MAX_BUFFER_LEN -1, p, strlen(p)) == EOK,
return -1, "Failed to copy requried mount info: %s", item);
}
INIT_LOGV("Mount option of partition %s is [%s]", partName, mountOptions);
if (ParseFstabPerLine(mountOptions, fstab, false, "@") < 0) {
@ -70,18 +62,12 @@ static int ParseRequiredMountInfo(const char *item, Fstab *fstab)
static Fstab* LoadFstabFromCommandLine(void)
{
Fstab *fstab = NULL;
char *cmdline = ReadFileData("/proc/cmdline");
char *cmdline = ReadFileData(BOOT_CMD_LINE);
bool isDone = false;
if (cmdline == NULL) {
INIT_LOGE("Read from \'/proc/cmdline\' failed, err = %d", errno);
return NULL;
}
if ((fstab = (Fstab *)calloc(1, sizeof(Fstab))) == NULL) {
INIT_LOGE("Allocate memory for FS table failed, err = %d", errno);
return NULL;
}
INIT_ERROR_CHECK(cmdline != NULL, return NULL, "Read from \'/proc/cmdline\' failed, err = %d", errno);
INIT_ERROR_CHECK((fstab = (Fstab *)calloc(1, sizeof(Fstab))) != NULL, return NULL,
"Allocate memory for FS table failed, err = %d", errno);
char *start = cmdline;
char *end = start + strlen(cmdline);
while (start < end) {
@ -135,9 +121,7 @@ Fstab* LoadRequiredFstab(void)
if (fstab == NULL) {
INIT_LOGI("Cannot load fstab from command line, try read from fstab.required");
const char *fstabFile = "/etc/fstab.required";
if (access(fstabFile, F_OK) != 0) {
fstabFile = "/system/etc/fstab.required";
}
INIT_CHECK(access(fstabFile, F_OK) == 0, fstabFile = "/system/etc/fstab.required");
INIT_ERROR_CHECK(access(fstabFile, F_OK) == 0, abort(), "Failed get fstab.required");
fstab = ReadFstabFromFile(fstabFile, false);
}

View File

@ -43,9 +43,7 @@ void NotifyServiceChange(Service *service, int status)
INIT_LOGI("NotifyServiceChange %s %s to %s", service->name,
statusMap[service->status].name, statusMap[status].name);
service->status = status;
if (status == SERVICE_IDLE) {
return;
}
INIT_CHECK(status != SERVICE_IDLE, return);
char paramName[PARAM_NAME_LEN_MAX] = { 0 };
int ret = snprintf_s(paramName, sizeof(paramName), sizeof(paramName) - 1,
"%s.%s", STARTUP_SERVICE_CTL, service->name);
@ -80,11 +78,8 @@ int ServiceExec(const Service *service)
INIT_ERROR_CHECK(service != NULL && service->pathArgs.count > 0,
return SERVICE_FAILURE, "Exec service failed! null ptr.");
if (service->importance != 0) {
if (setpriority(PRIO_PROCESS, 0, service->importance) != 0) {
INIT_LOGE("setpriority failed for %s, importance = %d, err=%d",
service->name, service->importance, errno);
_exit(0x7f); // 0x7f: user specified
}
INIT_ERROR_CHECK(setpriority(PRIO_PROCESS, 0, service->importance) == 0, _exit(0x7f),
"setpriority failed for %s, importance = %d, err=%d", service->name, service->importance, errno);
}
INIT_CHECK_ONLY_ELOG(unsetenv("UV_THREADPOOL_SIZE") == 0, "set UV_THREADPOOL_SIZE error : %d.", errno);
OpenHidebug(service->name);
@ -130,9 +125,8 @@ void GetAccessToken(void)
service->apl,
};
uint64_t tokenId = GetAccessTokenId(&nativeTokenInfoParams);
if (tokenId == 0) {
INIT_LOGE("Get totken id %lld of service \' %s \' failed", tokenId, service->name);
}
INIT_CHECK_ONLY_ELOG(tokenId != 0,
"Get totken id %lld of service \' %s \' failed", tokenId, service->name);
service->tokenId = tokenId;
}
node = GetNextGroupNode(NODE_TYPE_SERVICES, node);

View File

@ -50,10 +50,8 @@ static void LogToFile(const char *logFile, const char *tag, const char *info)
if (clock_gettime(CLOCK_REALTIME, &curr) != 0) {
return;
}
FILE *outfile = fopen(logFile, "a+");
if (outfile == NULL) {
return;
}
FILE *outfile = NULL;
INIT_CHECK_ONLY_RETURN((outfile = fopen(logFile, "a+")) != NULL);
struct tm t;
char dateTime[80]; // 80 data time
strftime(dateTime, sizeof(dateTime), "%Y-%m-%d %H:%M:%S", localtime_r(&curr.tv_sec, &t));

View File

@ -108,9 +108,7 @@ static LE_STATUS HandleClientEvent_(const LoopHandle loopHandle, const TaskHandl
LE_STATUS status = LE_SUCCESS;
if (LE_TEST_FLAGS(oper, Event_Write)) {
if (client->connected == 0 && client->connectComplete) {
client->connectComplete(handle);
}
LE_ONLY_CHECK(!(client->connected == 0 && client->connectComplete), client->connectComplete(handle));
client->connected = 1;
status = HandleSendMsg_(loopHandle, handle, client->sendMessageComplete);
}
@ -143,9 +141,7 @@ static LE_STATUS HandleServerEvent_(const LoopHandle loopHandle, const TaskHandl
return LE_FAILURE;
}
StreamServerTask *server = (StreamServerTask *)serverTask;
if (server->incommingConntect == NULL) {
return LE_SUCCESS;
}
LE_ONLY_CHECK(server->incommingConntect != NULL, return LE_SUCCESS);
int ret = server->incommingConntect(loopHandle, serverTask);
if (ret != LE_SUCCESS) {

View File

@ -77,11 +77,10 @@ void CloseTask(const LoopHandle loopHandle, BaseTask *task)
LE_Buffer *CreateBuffer(uint32_t bufferSize)
{
if (bufferSize >= LOOP_MAX_BUFFER) {
return NULL;
}
LE_Buffer *buffer = (LE_Buffer *)malloc(sizeof(LE_Buffer) + bufferSize);
LE_CHECK(buffer != NULL, return NULL, "Failed to alloc memory for buffer");
LE_ONLY_CHECK(bufferSize < LOOP_MAX_BUFFER, return NULL);
LE_Buffer *buffer = NULL;
LE_CHECK((buffer = (LE_Buffer *)malloc(sizeof(LE_Buffer) + bufferSize)) != NULL,
return NULL, "Failed to alloc memory for buffer");
ListInit(&buffer->node);
buffer->buffSize = bufferSize;
buffer->dataSize = 0;

View File

@ -31,9 +31,7 @@ static LE_STATUS HandleWatcherEvent_(const LoopHandle loopHandle, const TaskHand
watcher->processEvent(taskHandle, fd, &events, (void *)userData);
}
watcher = (WatcherTask *)GetTaskByFd((EventLoop *)loopHandle, fd);
if (watcher == NULL) {
return 0;
}
LE_ONLY_CHECK(watcher != NULL, return 0);
if (watcher->base.flags & WATCHER_ONCE) {
loop->delEvent(loop, fd, watcher->events);
return 0;

View File

@ -22,7 +22,7 @@
static ParamWorkSpace g_paramWorkSpace = {};
static int WorkSpaceNodeCompare(const HashNode *node1, const HashNode *node2)
PARAM_STATIC int WorkSpaceNodeCompare(const HashNode *node1, const HashNode *node2)
{
WorkSpace *workSpace1 = HASHMAP_ENTRY(node1, WorkSpace, hashNode);
WorkSpace *workSpace2 = HASHMAP_ENTRY(node2, WorkSpace, hashNode);

View File

@ -87,16 +87,6 @@ static TestHashNode *TestCreateHashNode(const char *value)
return node;
}
static void CreateTestFile(const char *fileName, const char *data)
{
FILE *tmpFile = fopen(fileName, "wr");
if (tmpFile != nullptr) {
fprintf(tmpFile, "%s", data);
(void)fflush(tmpFile);
fclose(tmpFile);
}
}
namespace init_ut {
class InitGroupManagerUnitTest : public testing::Test {
public:
@ -169,35 +159,6 @@ HWTEST_F(InitGroupManagerUnitTest, TestHashMap, TestSize.Level1)
HWTEST_F(InitGroupManagerUnitTest, TestInitGroupMgrInit, TestSize.Level1)
{
const char *data = "{"
"\"jobs\": [\"param:job1\", \"param:job2\", \"param:job4\"],"
"\"services\": [\"service:service1\", \"service:service3\", \"service:service2\"],"
"\"groups\": [\"subsystem.xxx1.group\", \"subsystem.xxx2.group\", \"subsystem.xxx4.group\"]"
"}";
const char *xxx1 = "{"
"\"groups\": [\"subsystem.xxx11.group\""
"}";
const char *xxx11 = "{"
"\"groups\": [\"subsystem.xxx12.group\""
"}";
const char *xxx12 = "{"
"\"groups\": [\"subsystem.xxx13.group\""
"}";
const char *xxx13 = "{"
"\"groups\": [\"subsystem.xxx14.group\""
"}";
const char *xxx14 = "{"
"\"groups\": [\"subsystem.xxx11.group\""
"}";
const char *cmdLine = "BOOT_IMAGE=/kernel init=/init bootgroup=device.charge.group";
CreateTestFile(GROUP_DEFAULT_PATH "/device.boot.group.cfg", data);
CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx1.group.cfg", xxx1);
CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx11.group.cfg", xxx11);
CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx12.group.cfg", xxx12);
CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx13.group.cfg", xxx13);
CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx14.group.cfg", xxx14);
CreateTestFile(BOOT_CMD_LINE, cmdLine);
InitServiceSpace();
InitWorkspace *workspace = GetInitWorkspace();
EXPECT_EQ(workspace->groupMode, GROUP_CHARGE);

View File

@ -34,9 +34,24 @@
using namespace testing::ext;
using namespace std;
using HashTab = struct {
HashNodeCompare nodeCompare;
HashKeyCompare keyCompare;
HashNodeFunction nodeHash;
HashKeyFunction keyHash;
HashNodeOnFree nodeFree;
int maxBucket;
uint32_t tableId;
HashNode *buckets[0];
};
extern "C" {
void OnClose(ParamTaskPtr client);
}
static LE_STATUS TestHandleTaskEvent(const LoopHandle loop, const TaskHandle task, uint32_t oper)
{
return LE_SUCCESS;
}
static void OnReceiveRequest(const TaskHandle task, const uint8_t *buffer, uint32_t nread)
{
@ -187,6 +202,14 @@ public:
void ProcessEventTest()
{
ProcessEvent((EventLoop *)LE_GetDefaultLoop(), 1, Event_Read);
LE_BaseInfo info = {TASK_EVENT, NULL};
int testfd = 65535; // 65535 is not exist fd
BaseTask *task = CreateTask(LE_GetDefaultLoop(), testfd, &info, sizeof(StreamClientTask));
task->handleEvent = TestHandleTaskEvent;
if (task != nullptr) {
ProcessEvent((EventLoop *)LE_GetDefaultLoop(), testfd, Event_Read);
}
((HashTab *)(((EventLoop *)LE_GetDefaultLoop())->taskMap))->nodeFree(&task->hashNode);
}
void ProcessasynEvent()
{

View File

@ -47,6 +47,7 @@ HWTEST_F(MountUnitTest, TestMountRequriedPartitions, TestSize.Level0)
int ret = MountRequriedPartitions(&fstab1);
EXPECT_EQ(ret, -1);
}
LoadRequiredFstab();
}
HWTEST_F(MountUnitTest, TestGetBlockDevicePath, TestSize.Level1)
{

View File

@ -25,6 +25,7 @@
#include "init_utils.h"
#include "securec.h"
#include "init_group_manager.h"
#include "trigger_manager.h"
using namespace testing::ext;
using namespace std;
@ -45,7 +46,10 @@ public:
void SetUp() {};
void TearDown() {};
};
HWTEST_F(ServiceUnitTest, TestDestoryHashMap, TestSize.Level1)
{
HashMapDestory(GetInitWorkspace()->hashMap[0]);
}
HWTEST_F(ServiceUnitTest, case01, TestSize.Level1)
{
const char *jsonStr = "{\"services\":{\"name\":\"test_service\",\"path\":[\"/data/init_ut/test_service\"],"

View File

@ -80,6 +80,8 @@ if (defined(ohos_lite)) {
"//base/startup/init_lite/services/log",
"//base/startup/init_lite/services/loopevent/include",
"//base/startup/init_lite/services/loopevent/timer",
"//base/startup/init_lite/services/loopevent/task",
"//base/startup/init_lite/services/loopevent/utils",
"//base/startup/init_lite/services/param/adapter",
"//base/startup/init_lite/services/param/linux",
"//base/startup/init_lite/services/param/include",

View File

@ -651,14 +651,6 @@ static void CheckService(const cJSON* curItem)
cJSON *capJ = cJSON_GetArrayItem(filedJ, i);
EXPECT_TRUE(cJSON_IsNumber(capJ));
EXPECT_TRUE(cJSON_GetNumberValue(capJ) >= 0.0);
// only shell can have all capabilities
if ((unsigned int)cJSON_GetNumberValue(capJ) == MAX_CAPABILITY_VALUE) {
if (nameStr != nullptr) {
EXPECT_EQ(0, strcmp(nameStr, "shell"));
}
EXPECT_EQ(1, capsCnt);
}
}
}

View File

@ -20,6 +20,7 @@
#include "param_manager.h"
#include "param_security.h"
#include "param_utils.h"
#include "init_group_manager.h"
#ifdef PARAM_LOAD_CFG_FROM_CODE
#include "param_cfg.h"
#endif
@ -209,6 +210,35 @@ static void PrepareInnerKitsCfg()
CreateTestFile("/data/init_ut/mount_unitest/ReadFstabFromFile1.fstable", innerKitsCfg);
CreateTestFile("/etc/fstab.required", "test");
}
static void PrepareGroupTestCfg()
{
const char *data = "{"
"\"jobs\": [\"param:job1\", \"param:job2\", \"param:job4\"],"
"\"services\": [\"service:service1\", \"service:service3\", \"service:service2\"],"
"\"groups\": [\"subsystem.xxx1.group\", \"subsystem.xxx2.group\", \"subsystem.xxx4.group\"]"
"}";
const char *xxx1 = "{"
"\"groups\": [\"subsystem.xxx11.group\""
"}";
const char *xxx11 = "{"
"\"groups\": [\"subsystem.xxx12.group\""
"}";
const char *xxx12 = "{"
"\"groups\": [\"subsystem.xxx13.group\""
"}";
const char *xxx13 = "{"
"\"groups\": [\"subsystem.xxx14.group\""
"}";
const char *xxx14 = "{"
"\"groups\": [\"subsystem.xxx11.group\""
"}";
CreateTestFile(GROUP_DEFAULT_PATH "/device.boot.group.cfg", data);
CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx1.group.cfg", xxx1);
CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx11.group.cfg", xxx11);
CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx12.group.cfg", xxx12);
CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx13.group.cfg", xxx13);
CreateTestFile(GROUP_DEFAULT_PATH "/subsystem.xxx14.group.cfg", xxx14);
}
static bool IsDir(const std::string &path)
{
struct stat st {};
@ -319,12 +349,14 @@ void PrepareInitUnitTestEnv(void)
PrepareUeventdcfg();
PrepareInnerKitsCfg();
PrepareModCfg();
PrepareGroupTestCfg();
SetInitLogLevel(INIT_FATAL);
#if !(defined __LITEOS_A__ || defined __LITEOS_M__)
// for cmdline
const char *cmdLine = "bootgroup=device.charge.group earlycon=uart8250,mmio32,0xfe660000 \
root=PARTUUID=614e0000-0000 rw rootwait rootfstype=ext4 console=ttyFIQ0 hardware=rk3568";
const char *cmdLine = "bootgroup=device.charge.group earlycon=uart8250,mmio32,0xfe660000 "
"root=PARTUUID=614e0000-0000 rw rootwait rootfstype=ext4 console=ttyFIQ0 hardware=rk3568"
" BOOT_IMAGE=/kernel init=/init ohos.required_mount.test=test";
CreateTestFile(BOOT_CMD_LINE, cmdLine);
// for dac

View File

@ -18,10 +18,17 @@
#include "param_message.h"
#include "param_stub.h"
#include "trigger_manager.h"
#include "param_utils.h"
#include "param_osadp.h"
#include "param_manager.h"
using namespace testing::ext;
using namespace std;
extern "C" {
int WorkSpaceNodeCompare(const HashNode *node1, const HashNode *node2);
}
static void OnClose(ParamTaskPtr client)
{
UNUSED(client);
@ -389,4 +396,29 @@ HWTEST_F(ParamUnitTest, TestDumpParamMemory, TestSize.Level0)
ParamUnitTest test;
test.TestDumpParamMemory();
}
HWTEST_F(ParamUnitTest, TestLinuxRWLock, TestSize.Level0)
{
ParamRWMutexCreate(nullptr);
ParamRWMutexWRLock(nullptr);
ParamRWMutexRDLock(nullptr);
ParamRWMutexUnlock(nullptr);
ParamRWMutexDelete(nullptr);
ParamMutexDelete(nullptr);
WorkSpace *workspace1 = (WorkSpace *)malloc(sizeof(WorkSpace) + strlen("testfilename1"));
WorkSpace *workspace2 = (WorkSpace *)malloc(sizeof(WorkSpace) + strlen("testfilename1"));
if (strcpy_s(workspace1->fileName, strlen("testfilename1"), "testfilename") != EOK) {
EXPECT_EQ(0, 1);
}
if (strcpy_s(workspace2->fileName, strlen("testfilename1"), "testfilename") != EOK) {
EXPECT_EQ(0, 1);
}
EXPECT_EQ(WorkSpaceNodeCompare(&(workspace1->hashNode), &(workspace2->hashNode)), 0);
EXPECT_NE(GetPersistCommitId(), -1);
EXPECT_NE(GetPersistCommitId(), -1);
EXPECT_STREQ(GetServiceCtrlName("ohos.ctl.start", "test"), "ohos.servicectrl.test");
EXPECT_STREQ(GetServiceCtrlName("ohos.startup.powerctrl", "reboot"), "ohos.servicectrl.reboot");
EXPECT_STREQ(GetServiceCtrlName("ohos.servicectrl.", "test"), "ohos.servicectrl..test");
free(workspace1);
free(workspace2);
}
}

View File

@ -18,6 +18,7 @@
#include "param_message.h"
#include "param_stub.h"
#include "trigger_manager.h"
#include "le_timer.h"
using namespace testing::ext;
using namespace std;
@ -427,6 +428,9 @@ public:
AddWatch(MSG_ADD_WATCHER, name, value);
char buffer[] = "testbuff";
CheckTrigger(GetTriggerWorkSpace(), TRIGGER_PARAM_WATCH, buffer, strlen(buffer), TestTriggerExecute);
#ifdef PARAM_SUPPORT_TRIGGER
SystemDumpTriggers(1);
#endif
AddWatch(MSG_DEL_WATCHER, name, value);
return 0;
}
@ -507,6 +511,11 @@ HWTEST_F(ParamServiceUnitTest, TestAddParamWatch3, TestSize.Level0)
{
ParamServiceUnitTest test;
test.TestAddParamWatch3();
if (GetParamService()->timer != nullptr) {
((TimerTask *)GetParamService()->timer)->processTimer(nullptr, nullptr);
}
int hashCode = CheckWatchTriggerTimeout();
EXPECT_EQ(hashCode, 0);
}
HWTEST_F(ParamServiceUnitTest, TestCloseTriggerWatch, TestSize.Level0)

View File

@ -137,6 +137,16 @@ public:
EXPECT_EQ(ret, 0);
ret = clientParamSercurityOps.securityFreeLabel(&srclabel);
EXPECT_EQ(ret, 0);
int fd = open("/bin/updater", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC, S_IRWXU);
if (fd < 0) {
return 0;
}
RegisterSecuritySelinuxOps(&clientParamSercurityOps, 0);
if (clientParamSercurityOps.securityCheckParamPermission != nullptr) {
clientParamSercurityOps.securityCheckParamPermission(nullptr, nullptr, 0);
}
close(fd);
unlink("/bin/updater");
return 0;
}

View File

@ -17,6 +17,7 @@
#include "init_jobs_internal.h"
#include "init_param.h"
#include "init_utils.h"
#include "init_hashmap.h"
#include "loop_event.h"
#include "param_manager.h"
#include "param_stub.h"
@ -466,9 +467,9 @@ public:
int TestDumpTrigger()
{
SystemDumpTriggers(1);
RegisterBootStateChange(BootStateChange);
(void)AddCompleteJob("param:ohos.servicectrl.display", "ohos.servicectrl.display=*", "display system");
HashMapDestory(GetTriggerWorkSpace()->hashMap);
return 0;
}
};

View File

@ -18,9 +18,14 @@
#include "init_param.h"
#include "parameter.h"
#include "sysparam_errno.h"
#include "param_comm.h"
#include "param_wrapper.h"
#include "sysversion.h"
using namespace testing::ext;
extern "C" {
int GetIntParameter(const char *key, int def);
}
namespace OHOS {
class SysparaUnitTest : public testing::Test {
public:
@ -30,7 +35,6 @@ public:
void TearDown() {}
};
HWTEST_F(SysparaUnitTest, parameterTest001, TestSize.Level0)
{
printf("Device type =%s\n", GetDeviceType());
@ -261,4 +265,29 @@ HWTEST_F(SysparaUnitTest, parameterTest0012, TestSize.Level0)
ret = GetParameterName(handle, nameGet1, 32);
EXPECT_EQ(ret, -1);
}
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);
EXPECT_EQ(GetIntParameter(key1, 0), 0);
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};
ret = GetDevUdid(udid, UDID_LEN);
EXPECT_NE(ret, -1);
EXPECT_NE(GetMajorVersion(), 0);
GetSeniorVersion();
GetFeatureVersion();
GetBuildVersion();
}
} // namespace OHOS