From dd9e2a077f614139894dadfb6c8269e071e95424 Mon Sep 17 00:00:00 2001 From: cheng_jinsong Date: Fri, 23 Sep 2022 02:07:24 -0700 Subject: [PATCH] add unittest cases Signed-off-by: cheng_jinsong Change-Id: Ifbd6923f273f167d78e514cc24c04a3e275e321a --- test/unittest/BUILD.gn | 4 ++ test/unittest/init/group_unittest.cpp | 6 +++ test/unittest/init/loopevent_unittest.cpp | 13 +++++ test/unittest/init/service_unittest.cpp | 6 ++- test/unittest/init/utils_unittest.cpp | 2 + test/unittest/innerkits/hookmgr_unittest.cpp | 8 +++ .../unittest/innerkits/innerkits_unittest.cpp | 43 +++++++++++++++ test/unittest/param/trigger_unittest.cpp | 3 +- .../unittest/param/watcher_agent_unittest.cpp | 15 ++++++ test/unittest/syspara/syspara_unittest.cpp | 1 + .../test_data/system/etc/param/ohos.para | 40 -------------- .../test_data/system/etc/param/ohos.para.dac | 34 ------------ .../system/etc/param/ohos.para.selinux | 29 ----------- test/unittest/test_data/trigger_test.cfg | 52 ------------------- 14 files changed, 99 insertions(+), 157 deletions(-) delete mode 100755 test/unittest/test_data/system/etc/param/ohos.para delete mode 100755 test/unittest/test_data/system/etc/param/ohos.para.dac delete mode 100755 test/unittest/test_data/system/etc/param/ohos.para.selinux delete mode 100755 test/unittest/test_data/trigger_test.cfg diff --git a/test/unittest/BUILD.gn b/test/unittest/BUILD.gn index ac97ad88..a6eaf29f 100755 --- a/test/unittest/BUILD.gn +++ b/test/unittest/BUILD.gn @@ -160,6 +160,10 @@ ohos_unittest("init_unittest") { "//base/startup/init/interfaces/innerkits/hookmgr/hookmgr.c", "//base/startup/init/interfaces/innerkits/init_module_engine/init_modulemgr.c", "//base/startup/init/interfaces/innerkits/modulemgr/modulemgr.c", + "//base/startup/init/interfaces/innerkits/service_control/service_control.c", + "//base/startup/init/interfaces/innerkits/service_watcher/service_watcher.c", + "//base/startup/init/interfaces/innerkits/syscap/init_syscap.c", + "//base/startup/init/interfaces/innerkits/syspara/param_wrapper.cpp", "innerkits/hookmgr_unittest.cpp", "innerkits/modulemgr_unittest.cpp", ] diff --git a/test/unittest/init/group_unittest.cpp b/test/unittest/init/group_unittest.cpp index 7110c7d3..6fed056c 100644 --- a/test/unittest/init/group_unittest.cpp +++ b/test/unittest/init/group_unittest.cpp @@ -300,7 +300,13 @@ HWTEST_F(InitGroupManagerUnitTest, TestAddService2, TestSize.Level1) ParseAllServices(fileRoot); cJSON_Delete(fileRoot); char cmdStr[] = "all#bootevent"; + char cmdStr1[] = "parameter_service"; + ProcessControlFd(ACTION_DUMP, "all", NULL); ProcessControlFd(ACTION_DUMP, cmdStr, NULL); + ProcessControlFd(ACTION_DUMP, cmdStr1, NULL); + ProcessControlFd(ACTION_SANDBOX, cmdStr, NULL); + ProcessControlFd(ACTION_MODULEMGR, cmdStr, NULL); + ProcessControlFd(ACTION_MAX, cmdStr, NULL); Service *service = GetServiceByName("test-service6"); ASSERT_NE(service, nullptr); workspace->groupMode = GROUP_BOOT; diff --git a/test/unittest/init/loopevent_unittest.cpp b/test/unittest/init/loopevent_unittest.cpp index b684c22f..dfeb208a 100644 --- a/test/unittest/init/loopevent_unittest.cpp +++ b/test/unittest/init/loopevent_unittest.cpp @@ -297,4 +297,17 @@ HWTEST_F(LoopEventUnittest, ProcessWatcherTask, TestSize.Level1) LoopEventUnittest loopevtest = LoopEventUnittest(); loopevtest.ProcessWatcherTask(); } + +HWTEST_F(LoopEventUnittest, CloseTackUnittest, TestSize.Level1) +{ + 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); +} + } // namespace init_ut diff --git a/test/unittest/init/service_unittest.cpp b/test/unittest/init/service_unittest.cpp index 9365422e..076db042 100644 --- a/test/unittest/init/service_unittest.cpp +++ b/test/unittest/init/service_unittest.cpp @@ -307,9 +307,12 @@ HWTEST_F(ServiceUnitTest, TestServiceBootEventHook, TestSize.Level1) SystemWriteParam("bootevent.bootevent1", "true"); SystemWriteParam("bootevent.bootevent1", "true"); SystemWriteParam("bootevent.bootevent2", "true"); - SystemSetParameter("ohos.servicectrl.save.bootevent", "save.bootevent"); + const char *initBootevent[] = {"init", "test"}; + PluginExecCmd("bootevent", ARRAY_LENGTH(initBootevent), initBootevent); + PluginExecCmd("save.bootevent", 0, nullptr); (void)HookMgrExecute(GetBootStageHookMgr(), INIT_SERVICE_DUMP, (void *)(&serviceInfoContext), NULL); (void)HookMgrExecute(GetBootStageHookMgr(), INIT_SERVICE_CLEAR, (void *)(&serviceInfoContext), NULL); + PluginExecCmd("clear", 0, nullptr); cJSON_Delete(fileRoot); } @@ -330,6 +333,7 @@ HWTEST_F(ServiceUnitTest, TestServiceExec, TestSize.Level1) caps[0] = FULL_CAP; service->servPerm.caps = caps; service->servPerm.capsCnt = 1; + IsEnableSandbox(); EnterServiceSandbox(service); int ret = ServiceExec(service); EXPECT_EQ(ret, 0); diff --git a/test/unittest/init/utils_unittest.cpp b/test/unittest/init/utils_unittest.cpp index 2b0d000d..99d46814 100644 --- a/test/unittest/init/utils_unittest.cpp +++ b/test/unittest/init/utils_unittest.cpp @@ -73,5 +73,7 @@ HWTEST_F(UtilsUnitTest, TestUtilsApi, TestSize.Level0) mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH; CheckAndCreatFile("/data/init_ut/testcreatfile", mode); CheckAndCreatFile("/data/init_ut/nodir/testcreatfile", mode); + char testStr[] = ".trim"; + EXPECT_STREQ(TrimHead(testStr, '.'), "trim"); } } // namespace init_ut diff --git a/test/unittest/innerkits/hookmgr_unittest.cpp b/test/unittest/innerkits/hookmgr_unittest.cpp index 7df0eea1..c0473b35 100755 --- a/test/unittest/innerkits/hookmgr_unittest.cpp +++ b/test/unittest/innerkits/hookmgr_unittest.cpp @@ -16,6 +16,7 @@ #include #include #include "hookmgr.h" +#include "bootstage.h" using namespace testing::ext; using namespace std; @@ -299,4 +300,11 @@ HWTEST_F(HookMgrUnitTest, HookMgrExecute_unitest, TestSize.Level1) EXPECT_EQ(ret, 0); EXPECT_EQ(ctx.result, 1); } + +HWTEST_F(HookMgrUnitTest, HookMgrExecuteInit_unitest, TestSize.Level1) +{ + HookMgrExecute(GetBootStageHookMgr(), INIT_GLOBAL_INIT, nullptr, nullptr); + HookMgrExecute(GetBootStageHookMgr(), INIT_PRE_CFG_LOAD, nullptr, nullptr); + InitAddClearServiceHook([](SERVICE_INFO_CTX *serviceCtx) {return;}); +} } // namespace init_ut diff --git a/test/unittest/innerkits/innerkits_unittest.cpp b/test/unittest/innerkits/innerkits_unittest.cpp index 5d9e4f13..31a24c46 100644 --- a/test/unittest/innerkits/innerkits_unittest.cpp +++ b/test/unittest/innerkits/innerkits_unittest.cpp @@ -14,11 +14,21 @@ */ #include +#include #include #include "fs_manager/fs_manager.h" #include "init_log.h" #include "param_stub.h" #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; @@ -210,4 +220,37 @@ HWTEST_F(InnerkitsUnitTest, MountAllWithFstabFile_unittest, TestSize.Level1) { EXPECT_NE(MountAllWithFstabFile("/etc/fstab.required", 0), 1); } + +HWTEST_F(InnerkitsUnitTest, others_unittest, 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"); +} } // namespace init_ut diff --git a/test/unittest/param/trigger_unittest.cpp b/test/unittest/param/trigger_unittest.cpp index 1537d4ae..ab91015d 100644 --- a/test/unittest/param/trigger_unittest.cpp +++ b/test/unittest/param/trigger_unittest.cpp @@ -266,7 +266,8 @@ public: JobNode *node = AddTrigger(TRIGGER_PARAM, triggerName, buffer, 0); JobNode *trigger = GetTriggerByName(GetTriggerWorkSpace(), triggerName); EXPECT_EQ(trigger, node); - + DoJobNow(triggerName); + ClearTrigger(nullptr, 0); g_matchTrigger = 0; SystemWriteParam(param1, "1"); ret = sprintf_s(buffer, sizeof(buffer), "%s=%s", param1, "1"); diff --git a/test/unittest/param/watcher_agent_unittest.cpp b/test/unittest/param/watcher_agent_unittest.cpp index 1d7b1b67..effd73a0 100644 --- a/test/unittest/param/watcher_agent_unittest.cpp +++ b/test/unittest/param/watcher_agent_unittest.cpp @@ -26,6 +26,7 @@ #include "system_ability_definition.h" #include "watcher.h" #include "watcher_manager_kits.h" +#include "service_watcher.h" using namespace testing::ext; using namespace std; @@ -36,6 +37,10 @@ void TestParameterChange(const char *key, const char *value, void *context) { printf("TestParameterChange key:%s %s", key, value); } +void TestWatcherCallBack(const char *key, ServiceStatus status) +{ + printf("TestWatcherCallBack key:%s %d", key, status); +} class WatcherAgentUnitTest : public ::testing::Test { public: @@ -167,4 +172,14 @@ HWTEST_F(WatcherAgentUnitTest, TestResetService, TestSize.Level0) { WatcherAgentUnitTest test; test.TestResetService(); +} + +HWTEST_F(WatcherAgentUnitTest, TestWatcherService, TestSize.Level0) +{ + const char *errstr = "111111111111111111111111111111111111111111111111111111111111111111111111111111111111"; + ServiceWatchForStatus("param_watcher", TestWatcherCallBack); + ServiceWaitForStatus("param_watcher", SERVICE_STARTED, 1); + EXPECT_EQ(ServiceWatchForStatus(errstr, TestWatcherCallBack), -1); + EXPECT_EQ(ServiceWatchForStatus(NULL, TestWatcherCallBack), -1); + WatchParameter("testParam", nullptr, nullptr); } \ No newline at end of file diff --git a/test/unittest/syspara/syspara_unittest.cpp b/test/unittest/syspara/syspara_unittest.cpp index 723d6bf4..c0483763 100644 --- a/test/unittest/syspara/syspara_unittest.cpp +++ b/test/unittest/syspara/syspara_unittest.cpp @@ -276,6 +276,7 @@ HWTEST_F(SysparaUnitTest, parameterTest0013, TestSize.Level0) EXPECT_EQ(ret, 0); GetParameter_(nullptr, nullptr, nullptr, 0); EXPECT_EQ(GetIntParameter(key1, 0), 0); + EXPECT_EQ(GetUintParameter(key1, 0), 0); EXPECT_EQ(IsValidParamValue(nullptr, 0), 0); EXPECT_EQ(IsValidParamValue("testvalue", strlen("testvalue") + 1), 1); EXPECT_EQ(StringToLL("0x11", &out), 0); diff --git a/test/unittest/test_data/system/etc/param/ohos.para b/test/unittest/test_data/system/etc/param/ohos.para deleted file mode 100755 index b27c935b..00000000 --- a/test/unittest/test_data/system/etc/param/ohos.para +++ /dev/null @@ -1,40 +0,0 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -build_version = 2.0 - -# whether ohos is enabled. -hw_sc.build.os.enable=true - -# ohos API version number. -hw_sc.build.os.apiversion=6 - -# ohos system version. -hw_sc.build.os.version=2.2.0 - -# ohos device type please use hw_sc.build.os.devicetype - -# ohos release type -hw_sc.build.os.releasetype=Canary1 - -const.hardware=Hi3516DV300 -const.actionable_compatible_property.enabled=false -const.postinstall.fstab.prefix=/system -const.secure=1 -security.perf_harden=1 -const.allow.mock.location=0 -const.debuggable=1 -const.build.characteristics="default" -const.product.model="ohos" -const.product.name="OpenHarmony 2.0 Canary" -persist.sys.usb.config=hdc diff --git a/test/unittest/test_data/system/etc/param/ohos.para.dac b/test/unittest/test_data/system/etc/param/ohos.para.dac deleted file mode 100755 index 94bca428..00000000 --- a/test/unittest/test_data/system/etc/param/ohos.para.dac +++ /dev/null @@ -1,34 +0,0 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -build_version root:root:0777 -hw_sc.build.os.enable root:root:0777 -hw_sc.build.os.apiversion root:root:0777 -hw_sc.build.os.version root:root:0777 -hw_sc.build.os.releasetype root:root:0777 - -const.actionable_compatible_property.enabled root:root:0777 -const.postinstall.fstab.prefix root:root:0777 -const.secure root:root:0777 -security.perf_harden root:root:0777 -const.allow.mock.location root:root:0777 -const.debuggable root:root:0777 -persist.sys.usb.config root:root:0777 - -# default forbid other user to start service -ohos.servicectrl. root:root:0777 - -test.permission. root:root:0770 -test.permission.read. root:root:0774 -test.permission.write. root:root:0772 -test.permission.watcher. root:root:0771 diff --git a/test/unittest/test_data/system/etc/param/ohos.para.selinux b/test/unittest/test_data/system/etc/param/ohos.para.selinux deleted file mode 100755 index f46d984d..00000000 --- a/test/unittest/test_data/system/etc/param/ohos.para.selinux +++ /dev/null @@ -1,29 +0,0 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. -# Licensed under the Apache License, Version 2.0 (the "License"); -# you may not use this file except in compliance with the License. -# You may obtain a copy of the License at -# -# http://www.apache.org/licenses/LICENSE-2.0 -# -# Unless required by applicable law or agreed to in writing, software -# distributed under the License is distributed on an "AS IS" BASIS, -# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -# See the License for the specific language governing permissions and -# limitations under the License. - -build_version root:root:0777 -hw_sc.build.os.enable root:root:0777 -hw_sc.build.os.apiversion root:root:0777 -hw_sc.build.os.version root:root:0777 -hw_sc.build.os.releasetype root:root:0777 - -const.actionable_compatible_property.enabled root:root:0777 -const.postinstall.fstab.prefix root:root:0777 -const.secure root:root:0777 -security.perf_harden root:root:0777 -const.allow.mock.location root:root:0777 -const.debuggable root:root:0777 -persist.sys.usb.config root:root:0777 - -# default forbid other user to start service -ohos.servicectrl. root:root:0777 diff --git a/test/unittest/test_data/trigger_test.cfg b/test/unittest/test_data/trigger_test.cfg deleted file mode 100755 index 7b754cb5..00000000 --- a/test/unittest/test_data/trigger_test.cfg +++ /dev/null @@ -1,52 +0,0 @@ -{ - - "jobs" : [{ - "name" : "early-init", - "cmds" : [ - " write '/proc/sys/kernel/sysrq 0'", - " load_persist_params ", - " load_persist_params ", - " # load_persist_params ", - " restorecon /postinstall", - "mkdir /acct/uid", - "chown root system /dev/memcg/memory.pressure_level", - "chmod 0040 /dev/memcg/memory.pressure_level", - "mkdir /dev/memcg/apps/ 0755 system system", - "mkdir /dev/memcg/system 0550 system system", - "start ueventd", - "exec_start apexd-bootstrap", - "setparam sys.usb.config ${persist.sys.usb.config}" - ] - }, - { - "name" : "param:trigger_test_1", - "condition" : "test.sys.boot_from_charger_mode=5", - "cmds" : [ - "class_stop charger", - "trigger late-init" - ] - }, - { - "name" : "param:trigger_test_2", - "condition" : "test.sys.boot_from_charger_mode=1 || test.sys.boot_from_charger_mode=2 || test.sys.boot_from_charger_mode=3", - "cmds" : [ - "class_stop charger", - "trigger late-init" - ] - }, - { - "name" : "load_persist_params_action", - "cmds" : [ - "load_persist_params", - "start logd", - "start logd-reinit" - ] - }, - { - "name" : "firmware_mounts_complete", - "cmds" : [ - "rm /dev/.booting" - ] - } - ] -} -- GitLab