1/*
2 * Copyright (c) 2021 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15#include "bootstage.h"
16#include "init_cmds.h"
17#include "init_group_manager.h"
18#include "init_hashmap.h"
19#include "init_param.h"
20#include "init_module_engine.h"
21#include "init_cmdexecutor.h"
22#include "param_stub.h"
23#include "init_utils.h"
24#include "securec.h"
25
26using namespace testing::ext;
27using namespace std;
28
29namespace init_ut {
30class ModuleMgrUnitTest : public testing::Test {
31public:
32    static void SetUpTestCase(void) {};
33    static void TearDownTestCase(void) {};
34    void SetUp(void) {};
35    void TearDown(void) {};
36};
37
38int g_cmdExecId = 0;
39int TestCmdExecutor(int id, const char *name, int argc, const char **argv)
40{
41    printf("TestCmdExecutor id %d, name %s \n", id, name);
42    g_cmdExecId = id;
43    return 0;
44}
45
46HWTEST_F(ModuleMgrUnitTest, Init_ModuleMgrTest_PluginAddCmd001, TestSize.Level1)
47{
48    InitServiceSpace();
49    const char *testName = "testCmd1";
50    const char *cmdContent = "testCmd1 test1 test2 test3";
51    const char *cmdContentNotValid = "testCmd1 t e s t 1 t e s t 2 t";
52    int cmdExecId1 = AddCmdExecutor(testName, TestCmdExecutor);
53    ASSERT_NE(cmdExecId1 > 0, 0);
54    int cmdExecId2 = AddCmdExecutor("testCmd2", TestCmdExecutor);
55    ASSERT_NE(cmdExecId2 > 0, 0);
56    cmdExecId2 = AddCmdExecutor("testCmd3", TestCmdExecutor);
57    ASSERT_NE(cmdExecId2 > 0, 0);
58    int cmdExecId4 = AddCmdExecutor("testCmd4", TestCmdExecutor);
59    ASSERT_NE(cmdExecId4 > 0, 0);
60    PluginExecCmd("testCmd4", 0, nullptr);
61
62    int cmdIndex = 0;
63    const char *cmdName = PluginGetCmdIndex(cmdContent, &cmdIndex);
64    ASSERT_EQ(strcmp(cmdName, testName), 0);
65    printf("TestCmdExecutor cmdIndex 0x%04x, name %s \n", cmdIndex, cmdName);
66    ASSERT_NE(GetPluginCmdNameByIndex(cmdIndex), nullptr);
67
68    // exec
69    g_cmdExecId = -1;
70    PluginExecCmdByName(cmdName, cmdContent);
71    ASSERT_EQ(cmdExecId1, g_cmdExecId);
72    PluginExecCmdByName(cmdName, nullptr);
73    PluginExecCmdByName(cmdName, cmdContentNotValid);
74    g_cmdExecId = -1;
75    PluginExecCmdByCmdIndex(cmdIndex, cmdContent, nullptr);
76    ASSERT_EQ(cmdExecId1, g_cmdExecId);
77    const char *argv[] = {"test.value"};
78    PluginExecCmd("install", 1, argv);
79    PluginExecCmd("uninstall", 1, argv);
80    PluginExecCmd("setloglevel", 1, argv);
81
82    // del
83    RemoveCmdExecutor("testCmd4", cmdExecId4);
84    AddCareContextCmdExecutor("", nullptr);
85    RemoveCmdExecutor("testCmd4", -1);
86}
87
88static void TestModuleDump(const MODULE_INFO *moduleInfo)
89{
90    printf("%s\n", moduleInfo->name);
91}
92
93HWTEST_F(ModuleMgrUnitTest, Init_ModuleMgrTest_ModuleTraversal001, TestSize.Level1)
94{
95    // Create module manager
96    MODULE_MGR *moduleMgr = ModuleMgrCreate("init");
97    ASSERT_NE(moduleMgr, nullptr);
98    int cnt = ModuleMgrGetCnt(moduleMgr);
99    ASSERT_EQ(cnt, 0);
100    // Install one module
101    int ret = ModuleMgrInstall(moduleMgr, "libbootchart", 0, nullptr);
102    ASSERT_EQ(ret, 0);
103    cnt = ModuleMgrGetCnt(moduleMgr);
104    ASSERT_EQ(cnt, 1);
105    ModuleMgrTraversal(nullptr, nullptr, nullptr);
106    ModuleMgrTraversal(moduleMgr, nullptr, TestModuleDump);
107    InitModuleMgrDump();
108
109    InitModuleMgrInstall("/");
110
111    // Scan all modules
112    ModuleMgrScan(nullptr);
113    ModuleMgrScan("/");
114    moduleMgr = ModuleMgrScan("init");
115    moduleMgr = ModuleMgrScan(STARTUP_INIT_UT_PATH MODULE_LIB_NAME "/autorun");
116    ASSERT_NE(moduleMgr, nullptr);
117    cnt = ModuleMgrGetCnt(moduleMgr);
118    ASSERT_GE(cnt, 0);
119
120    ModuleMgrUninstall(moduleMgr, nullptr);
121    cnt = ModuleMgrGetCnt(moduleMgr);
122    ASSERT_EQ(cnt, 0);
123
124    ModuleMgrGetArgs();
125    ModuleMgrDestroy(moduleMgr);
126}
127
128HWTEST_F(ModuleMgrUnitTest, Init_ModuleMgrTest_ModuleAbnormal001, TestSize.Level1)
129{
130    int ret = InitModuleMgrInstall(nullptr);
131    ASSERT_EQ(ret, -1);
132}
133}  // namespace init_ut
134