提交 40257160 编写于 作者: C cheng_jinsong

add hookmgr and modelmgr moduletest case

Signed-off-by: Ncheng_jinsong <chengjinsong2@huawei.com>
Change-Id: Ifddf0441df80a92a6b0e6418274d4c55a0a234a7
上级 5982450c
......@@ -18,6 +18,7 @@ group("testgroup") {
deps = [
"exec_test:exectest",
"fuzztest:fuzztest",
"moduletest:moduletest",
"moduletest:paramtestmodule",
"unittest:init_unittest",
]
......
......@@ -13,6 +13,7 @@
import("//base/startup/init/begetd.gni")
import("//build/ohos.gni")
import("//build/test.gni")
ohos_shared_library("libparamtestmodule") {
sources = [ "param_test_module.c" ]
......@@ -41,6 +42,28 @@ ohos_shared_library("libparamtestmodule") {
module_install_dir = "lib/init"
}
ohos_moduletest("InitModuleTest") {
module_out_path = "startup/init"
sources = [
"hookmgr_moduletest.cpp",
"modulemgr_moduletest.cpp",
]
include_dirs = [ "//base/startup/init/interfaces/innerkits/include" ]
deps = [
"//base/startup/init/interfaces/innerkits:libbegetutil",
"//third_party/googletest:gtest_main",
]
}
group("moduletest") {
testonly = true
deps = [ ":InitModuleTest" ]
}
group("paramtestmodule") {
if (param_test) {
deps = [ ":libparamtestmodule" ]
......
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cinttypes>
#include <thread>
#include <mutex>
#include <vector>
#include <gtest/gtest.h>
#include "hookmgr.h"
using namespace testing::ext;
using namespace std;
namespace initModuleTest {
#define STAGE_TEST_ONE 0
int g_publicData = 0;
mutex g_mutex;
HOOK_MGR *g_hookMgr = nullptr;
class HookMgrModuleTest : public testing::Test {
public:
static void SetUpTestCase(void) {};
static void TearDownTestCase(void) {};
void SetUp() {};
void TearDown() {};
};
struct HookExecCtx {
int result;
int retErr;
};
void HookExecFunc(int n)
{
for (int i = 0; i < n; ++i) {
HookMgrExecute(g_hookMgr, STAGE_TEST_ONE, nullptr, nullptr);
}
}
static int OhosTestHookMultiThread(const HOOK_INFO *hookInfo, void *executionContext)
{
lock_guard<std::mutex> lg(g_mutex);
g_publicData++;
return 0;
}
static int OhosTestHookMultiThreadAnother(const HOOK_INFO *hookInfo, void *executionContext)
{
lock_guard<std::mutex> lg(g_mutex);
g_publicData++;
return -1;
}
static void OhosHookPrint(const HOOK_INFO *hookInfo, void *traversalCookie)
{
printf("\tstage[%02d] prio[%02d] hook[%p].\n", hookInfo->stage, hookInfo->prio, hookInfo->hook);
}
static void DumpAllHooks(HOOK_MGR *hookMgr)
{
printf("----------All Hooks---------------\n");
HookMgrTraversal(hookMgr, NULL, OhosHookPrint);
printf("----------------------------------\n\n");
}
HWTEST_F(HookMgrModuleTest, HookMgrModuleTest, TestSize.Level1)
{
int ret;
int cnt;
g_hookMgr = HookMgrCreate("moduleTestHook");
ASSERT_NE(g_hookMgr, nullptr);
ret = HookMgrAdd(g_hookMgr, STAGE_TEST_ONE, 0, OhosTestHookMultiThread);
EXPECT_EQ(ret, 0);
cnt = HookMgrGetHooksCnt(g_hookMgr, STAGE_TEST_ONE);
EXPECT_EQ(cnt, 1);
HOOK_INFO info;
info.stage = STAGE_TEST_ONE;
info.prio = 1;
info.hook = OhosTestHookMultiThreadAnother;
info.hookCookie = nullptr;
HookMgrAddEx(g_hookMgr, &info);
cnt = HookMgrGetHooksCnt(g_hookMgr, STAGE_TEST_ONE);
EXPECT_EQ(cnt, 2);
cnt = HookMgrGetStagesCnt(g_hookMgr);
EXPECT_EQ(cnt, 1);
DumpAllHooks(g_hookMgr);
// test multiThread HookMgrExecute
std::vector<std::thread> threads;
for (int i = 1; i <= 10; ++i) {
threads.push_back(std::thread(HookExecFunc, 10));
}
for (auto & th : threads) {
th.join();
}
EXPECT_EQ(g_publicData, 200);
HookMgrAdd(g_hookMgr, STAGE_TEST_ONE, 1, OhosTestHookMultiThreadAnother);
cnt = HookMgrGetHooksCnt(g_hookMgr, STAGE_TEST_ONE);
EXPECT_EQ(cnt, 2);
HookMgrDel(g_hookMgr, STAGE_TEST_ONE, OhosTestHookMultiThreadAnother);
cnt = HookMgrGetHooksCnt(g_hookMgr, STAGE_TEST_ONE);
EXPECT_EQ(cnt, 1);
HookMgrDestroy(g_hookMgr);
}
} // namespace init_ut
/*
* Copyright (c) 2021 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include <cstdio>
#include <gtest/gtest.h>
#include "modulemgr.h"
using namespace testing::ext;
using namespace std;
namespace initModuleTest {
class ModuleMgrModuleTest : public testing::Test {
public:
static void SetUpTestCase(void) {};
static void TearDownTestCase(void) {};
void SetUp(void) {};
void TearDown(void) {};
};
static void TestModuleDump(const MODULE_INFO *moduleInfo)
{
printf("%s\n", moduleInfo->name);
}
HWTEST_F(ModuleMgrModuleTest, ModuleMrgTest, TestSize.Level1)
{
int cnt;
MODULE_MGR *moduleMgr = nullptr;
moduleMgr = ModuleMgrCreate("init");
// Install one module
ModuleMgrInstall(moduleMgr, "bootchart", 0, NULL);
ModuleMgrDestroy(moduleMgr);
// Scan all modules
moduleMgr = ModuleMgrScan("init");
ASSERT_NE(moduleMgr, nullptr);
ModuleMgrTraversal(moduleMgr, NULL, TestModuleDump);
ModuleMgrUninstall(moduleMgr, NULL);
cnt = ModuleMgrGetCnt(moduleMgr);
EXPECT_EQ(cnt, 0);
ModuleMgrGetArgs();
ModuleMgrDestroy(moduleMgr);
}
} // namespace init_ut
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册