• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
26 using namespace testing::ext;
27 using namespace std;
28 
29 namespace init_ut {
30 class ModuleMgrUnitTest : public testing::Test {
31 public:
SetUpTestCase(void)32     static void SetUpTestCase(void) {};
TearDownTestCase(void)33     static void TearDownTestCase(void) {};
SetUp(void)34     void SetUp(void) {};
TearDown(void)35     void TearDown(void) {};
36 };
37 
38 int g_cmdExecId = 0;
TestCmdExecutor(int id,const char * name,int argc,const char ** argv)39 int 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 
46 HWTEST_F(ModuleMgrUnitTest, PluginAddCmd, 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);
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 }
85 
86 HWTEST_F(ModuleMgrUnitTest, ModuleInstallTest, TestSize.Level1)
87 {
88     int ret;
89     int cnt;
90 
91     // Create module manager
92     ASSERT_EQ(ModuleMgrCreate(nullptr), nullptr);
93     ModuleMgrDestroy(nullptr);
94     MODULE_MGR *moduleMgr = ModuleMgrCreate("init");
95     ASSERT_NE(moduleMgr, nullptr);
96     cnt = ModuleMgrGetCnt(moduleMgr);
97     ASSERT_EQ(cnt, 0);
98 
99     // Install one module
100 #ifdef SUPPORT_64BIT
101     ret = ModuleMgrInstall(moduleMgr, "/system/lib64/init/libbootchart", 0, NULL);
102 #else
103     ret = ModuleMgrInstall(moduleMgr, "/system/lib/init/libbootchart", 0, NULL);
104 #endif
105     ASSERT_EQ(ret, 0);
106     cnt = ModuleMgrGetCnt(moduleMgr);
107     ASSERT_EQ(cnt, 1);
108 
109     // Uninstall the module
110     ModuleMgrUninstall(moduleMgr, "bootchart");
111     InitModuleMgrUnInstall("bootchart");
112     cnt = ModuleMgrGetCnt(moduleMgr);
113     ASSERT_EQ(cnt, 0);
114 
115     // Install two module
116     ret = ModuleMgrInstall(moduleMgr, "bootchart", 0, NULL);
117     ASSERT_EQ(ret, 0);
118     cnt = ModuleMgrGetCnt(moduleMgr);
119     ASSERT_EQ(cnt, 1);
120     ret = ModuleMgrInstall(moduleMgr, "notexist", 0, NULL);
121     ASSERT_NE(ret, 0);
122     cnt = ModuleMgrGetCnt(moduleMgr);
123     ASSERT_EQ(cnt, 1);
124 
125     // Uninstall the module
126     ModuleMgrUninstall(moduleMgr, "bootchart");
127     cnt = ModuleMgrGetCnt(moduleMgr);
128     ASSERT_EQ(cnt, 0);
129     ModuleMgrUninstall(moduleMgr, "notexist");
130     cnt = ModuleMgrGetCnt(moduleMgr);
131     ASSERT_EQ(cnt, 0);
132 
133     ModuleMgrDestroy(moduleMgr);
134 
135     // test updater mode
136     int fd = open("/bin/updater", O_WRONLY | O_CREAT | O_TRUNC | O_CLOEXEC,  S_IRWXU);
137     ASSERT_NE(fd, 0);
138     ModuleMgrScan("init/autorun");
139     unlink("/bin/updater");
140     close(fd);
141 }
142 
TestModuleDump(const MODULE_INFO * moduleInfo)143 static void TestModuleDump(const MODULE_INFO *moduleInfo)
144 {
145     printf("%s\n", moduleInfo->name);
146 }
147 
148 HWTEST_F(ModuleMgrUnitTest, ModuleTraversalTest, TestSize.Level1)
149 {
150     // Create module manager
151     MODULE_MGR *moduleMgr = ModuleMgrCreate("init");
152     ASSERT_NE(moduleMgr, nullptr);
153     int cnt = ModuleMgrGetCnt(moduleMgr);
154     ASSERT_EQ(cnt, 0);
155     // Install one module
156     int ret = ModuleMgrInstall(moduleMgr, "bootchart", 0, NULL);
157     ASSERT_EQ(ret, 0);
158     cnt = ModuleMgrGetCnt(moduleMgr);
159     ASSERT_EQ(cnt, 1);
160     ModuleMgrTraversal(nullptr, nullptr, nullptr);
161     ModuleMgrTraversal(moduleMgr, NULL, TestModuleDump);
162     InitModuleMgrDump();
163 
164     // Scan all modules
165     ModuleMgrScan(nullptr);
166 
167     moduleMgr = ModuleMgrScan("init/autorun");
168     ASSERT_NE(moduleMgr, nullptr);
169     cnt = ModuleMgrGetCnt(moduleMgr);
170     ASSERT_GE(cnt, 0);
171 
172     ModuleMgrUninstall(moduleMgr, NULL);
173     cnt = ModuleMgrGetCnt(moduleMgr);
174     ASSERT_EQ(cnt, 0);
175 
176     ModuleMgrGetArgs();
177     ModuleMgrDestroy(moduleMgr);
178 }
179 
180 HWTEST_F(ModuleMgrUnitTest, ModuleScanTest, TestSize.Level1)
181 {
182     // Scan all modules test init
183     MODULE_MGR *moduleMgr = ModuleMgrScan("init");
184     ASSERT_NE(moduleMgr, nullptr);
185     int cnt = ModuleMgrGetCnt(moduleMgr);
186     ASSERT_GE(cnt, 1);
187 
188     ModuleMgrUninstall(nullptr, nullptr);
189     ModuleMgrUninstall(moduleMgr, NULL);
190     cnt = ModuleMgrGetCnt(moduleMgr);
191     ASSERT_EQ(cnt, 0);
192     ModuleMgrDestroy(moduleMgr);
193 
194     // scan /lib/init/
195 #ifdef SUPPORT_64BIT
196     moduleMgr = ModuleMgrScan("/lib64/init");
197 #else
198     moduleMgr = ModuleMgrScan("/lib/init");
199 #endif
200     ASSERT_NE(moduleMgr, nullptr);
201     ModuleMgrGetCnt(nullptr);
202     cnt = ModuleMgrGetCnt(moduleMgr);
203     ModuleMgrDestroy(moduleMgr);
204     EXPECT_EQ(InitModuleMgrInstall(nullptr), -1);
205 }
206 }  // namespace init_ut
207