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
16 #include <sys/statvfs.h>
17 #include "init_cmds.h"
18 #include "init_param.h"
19 #include "init_group_manager.h"
20 #include "param_stub.h"
21 #include "init_utils.h"
22 #include "trigger_manager.h"
23
24 using namespace testing::ext;
25 using namespace std;
26
DoCmdByName(const char * name,const char * cmdContent)27 static int DoCmdByName(const char *name, const char *cmdContent)
28 {
29 int cmdIndex = 0;
30 (void)GetMatchCmd(name, &cmdIndex);
31 DoCmdByIndex(cmdIndex, cmdContent, nullptr);
32 return 0;
33 }
34
35 namespace init_ut {
36 class CmdsUnitTest : public testing::Test {
37 public:
SetUpTestCase(void)38 static void SetUpTestCase(void) {};
TearDownTestCase(void)39 static void TearDownTestCase(void) {};
SetUp()40 void SetUp() {};
TearDown()41 void TearDown() {};
42 };
43
44 HWTEST_F(CmdsUnitTest, TestCmdExecByName1, TestSize.Level1)
45 {
46 int ret = DoCmdByName("timer_start ", "media_service|5000");
47 ret = DoCmdByName("timer_start ", "|5000");
48 EXPECT_EQ(ret, 0);
49 ret = DoCmdByName("timer_stop ", "media_service");
50 EXPECT_EQ(ret, 0);
51 ret = DoCmdByName("exec ", "media_service");
52 EXPECT_EQ(ret, 0);
53 ret = DoCmdByName("syncexec ", "/system/bin/toybox");
54 EXPECT_EQ(ret, 0);
55 ret = DoCmdByName("load_access_token_id ", "media_service");
56 EXPECT_EQ(ret, 0);
57 ret = DoCmdByName("load_access_token_id ", "");
58 EXPECT_EQ(ret, 0);
59 ret = DoCmdByName("stopAllServices ", "false");
60 EXPECT_EQ(ret, 0);
61 ret = DoCmdByName("stopAllServices ", "true");
62 EXPECT_EQ(ret, 0);
63 ret = DoCmdByName("umount ", "/2222222");
64 EXPECT_EQ(ret, 0);
65 ret = DoCmdByName("mount ", "/2222222");
66 EXPECT_EQ(ret, 0);
67 ret = DoCmdByName("mount ", "ext4 /2222222 /data wait filecrypt=555");
68 EXPECT_EQ(ret, 0);
69 ret = DoCmdByName("umount ", "/2222222");
70 EXPECT_EQ(ret, 0);
71 ret = DoCmdByName("init_global_key ", "/data");
72 EXPECT_EQ(ret, 0);
73 ret = DoCmdByName("init_global_key ", "arg0 arg1");
74 EXPECT_EQ(ret, 0);
75 ret = DoCmdByName("init_main_user ", "testUser");
76 EXPECT_EQ(ret, 0);
77 }
78 HWTEST_F(CmdsUnitTest, TestCmdExecByName2, TestSize.Level1)
79 {
80 int ret = DoCmdByName("init_main_user ", nullptr);
81 EXPECT_EQ(ret, 0);
82 ret = DoCmdByName("mkswap ", "/data/init_ut");
83 EXPECT_EQ(ret, 0);
84 ret = DoCmdByName("swapon ", "/data/init_ut");
85 EXPECT_EQ(ret, 0);
86 ret = DoCmdByName("sync ", "");
87 EXPECT_EQ(ret, 0);
88 ret = DoCmdByName("restorecon ", "");
89 EXPECT_EQ(ret, 0);
90 ret = DoCmdByName("restorecon ", "/data /data");
91 EXPECT_EQ(ret, 0);
92 ret = DoCmdByName("suspend ", "");
93 EXPECT_EQ(ret, 0);
94 ret = DoCmdByName("wait ", "1");
95 EXPECT_EQ(ret, 0);
96 ret = DoCmdByName("wait ", "aaa 1");
97 EXPECT_EQ(ret, 0);
98 ret = DoCmdByName("mksandbox", "/sandbox");
99 EXPECT_EQ(ret, 0);
100 ret = DoCmdByName("mount_fstab ", "/2222222");
101 EXPECT_EQ(ret, 0);
102 ret = DoCmdByName("umount_fstab ", "/2222222");
103 EXPECT_EQ(ret, 0);
104 ret = DoCmdByName("mknode ", "node1 node1 node1 node1 node1");
105 EXPECT_EQ(ret, 0);
106 ret = DoCmdByName("makedev ", "/device1 device2");
107 EXPECT_EQ(ret, 0);
108 ret = DoCmdByName("symlink ", "/xxx/xxx/xxx1 /xxx/xxx/xxx2");
109 EXPECT_EQ(ret, 0);
110 ret = DoCmdByName("load_param ", "aaa onlyadd");
111 EXPECT_EQ(ret, 0);
112 ret = DoCmdByName("load_persist_params ", "");
113 EXPECT_EQ(ret, 0);
114 ret = DoCmdByName("load_param ", "");
115 EXPECT_EQ(ret, 0);
116 ret = DoCmdByName("setparam ", "bbb 0");
117 EXPECT_EQ(ret, 0);
118 ret = DoCmdByName("ifup ", "aaa, bbb");
119 EXPECT_EQ(ret, 0);
120 ret = DoCmdByName("insmod ", "a b");
121 EXPECT_EQ(ret, 0);
122 ret = DoCmdByName("insmod ", "/data /data");
123 }
124
125 HWTEST_F(CmdsUnitTest, TestCommonMkdir, TestSize.Level1)
126 {
__anon0d6a90920102(const char *mkdirFile, const char *cmdLine) 127 auto checkMkdirCmd = [=](const char *mkdirFile, const char *cmdLine) {
128 DoCmdByName("mkdir ", cmdLine);
129 return access(mkdirFile, F_OK);
130 };
131 EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir0", "/data/init_ut/test_dir0"), 0);
132 EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir1", "/data/init_ut/test_dir1 0755"), 0);
133 EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir2", "/data/init_ut/test_dir2 0755 system system"), 0);
134
135 // abnormal
136 EXPECT_NE(checkMkdirCmd("/data/init_ut/test_dir3", ""), 0);
137 EXPECT_NE(checkMkdirCmd("/data/init_ut/test_dir4", "/data/init_ut/test_dir4 0755 system"), 0);
138 EXPECT_EQ(checkMkdirCmd("/data/init_ut/test_dir5", "/data/init_ut/test_dir5 0755 error error"), 0);
139 }
140
141 HWTEST_F(CmdsUnitTest, TestCommonChown, TestSize.Level1)
142 {
143 const char *testFile = "/data/init_ut/test_dir0";
144 DoCmdByName("chown ", "system system /data/init_ut/test_dir0");
145 struct stat info = {};
146 stat(testFile, &info);
147 const unsigned int systemUidGid = 1000;
148 EXPECT_EQ(info.st_uid, systemUidGid);
149 EXPECT_EQ(info.st_gid, systemUidGid);
150
151 // abnormal
152 DoCmdByName("chown ", "error error /data/init_ut/test_dir0");
153 stat(testFile, &info);
154 EXPECT_EQ(info.st_uid, systemUidGid);
155 EXPECT_EQ(info.st_gid, systemUidGid);
156 }
157
158 HWTEST_F(CmdsUnitTest, TestCommonChmod, TestSize.Level1)
159 {
160 const char *testFile = "/data/init_ut/test_dir0/test_file0";
161 const mode_t testMode = S_IRWXU | S_IRWXG | S_IRWXO;
162 int fd = open(testFile, O_CREAT | O_WRONLY, testMode);
163 ASSERT_GE(fd, 0);
164 DoCmdByName("chmod ", "777 /data/init_ut/test_dir0/test_file0");
165 struct stat info;
166 stat(testFile, &info);
167 EXPECT_EQ(testMode, testMode & info.st_mode);
168
169 // abnormal
170 DoCmdByName("chmod ", "999 /data/init_ut/test_dir0/test_file0");
171 stat(testFile, &info);
172 EXPECT_EQ(testMode, testMode & info.st_mode);
173 DoCmdByName("chmod ", "777 /data/init_ut/test_dir0/test_file001");
174
175 close(fd);
176 }
177
178 HWTEST_F(CmdsUnitTest, TestCommonCopy, TestSize.Level1)
179 {
180 const char *testFile1 = "/data/init_ut/test_dir0/test_file_copy1";
181 DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file0 /data/init_ut/test_dir0/test_file_copy1");
182 int fd = open(testFile1, O_RDWR);
183 ASSERT_GE(fd, 0);
184 write(fd, "aaa", strlen("aaa"));
185
186 const char *testFile2 = "/data/init_ut/test_dir0/test_file_copy2";
187 DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy1 /data/init_ut/test_dir0/test_file_copy2");
188 int ret = access(testFile2, F_OK);
189 EXPECT_EQ(ret, 0);
190 close(fd);
191 // abnormal
192 DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy1 /data/init_ut/test_dir0/test_file_copy1");
193 DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy11 /data/init_ut/test_dir0/test_file_copy1");
194 DoCmdByName("copy ", "a");
195
196 DoCmdByName("chmod ", "111 /data/init_ut/test_dir0/test_file_copy1");
197 DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy1 /data/init_ut/test_dir0/test_file_copy2");
198
199 DoCmdByName("chmod ", "777 /data/init_ut/test_dir0/test_file_copy1");
200 DoCmdByName("chmod ", "111 /data/init_ut/test_dir0/test_file_copy2");
201 DoCmdByName("copy ", "/data/init_ut/test_dir0/test_file_copy1 /data/init_ut/test_dir0/test_file_copy2");
202 DoCmdByName("chmod ", "777 /data/init_ut/test_dir0/test_file_copy2");
203 }
204
205 HWTEST_F(CmdsUnitTest, TestCommonWrite, TestSize.Level1)
206 {
207 const char *testFile1 = "/data/init_ut/test_dir0/test_file_write1";
208 int fd = open(testFile1, O_RDWR | O_CREAT, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP | S_IROTH | S_IWOTH);
209 ASSERT_GE(fd, 0);
210
211 DoCmdByName("write ", "/data/init_ut/test_dir0/test_file_write1 aaa");
212 const int bufLen = 50;
213 char buffer[bufLen];
214 int length = read(fd, buffer, bufLen - 1);
215 EXPECT_EQ(length, strlen("aaa"));
216 close(fd);
217 // abnormal
218 DoCmdByName("write ", "/data/init_ut/test_dir0/test_file_write2 aaa 2");
219 }
220
221 HWTEST_F(CmdsUnitTest, TestCommonRm, TestSize.Level1)
222 {
223 const char *testFile1 = "/data/init_ut/test_dir0/test_file_write1";
224 DoCmdByName("rm ", testFile1);
225 int ret = access(testFile1, F_OK);
226 EXPECT_NE(ret, 0);
227
228 testFile1 = "/data/init_ut/test_dir1";
229 DoCmdByName("rmdir ", testFile1);
230 ret = access(testFile1, F_OK);
231 EXPECT_NE(ret, 0);
232
233 // abnormal
234 DoCmdByName("rmdir ", testFile1);
235 }
236
237 HWTEST_F(CmdsUnitTest, TestCommonExport, TestSize.Level1)
238 {
239 DoCmdByName("export ", "TEST_INIT 1");
240 EXPECT_STREQ("1", getenv("TEST_INIT"));
241 unsetenv("TEST_INIT");
242 EXPECT_STRNE("1", getenv("TEST_INIT"));
243 }
244
245 HWTEST_F(CmdsUnitTest, TestCommonMount, TestSize.Level1)
246 {
247 DoCmdByName("mount ", "ext4 /dev/block/platform/soc/10100000.himci.eMMC/by-name/vendor "
248 "/vendor wait rdonly barrier=1");
249 struct statvfs64 vfs {};
250 int ret = statvfs64("/vendor", &vfs);
251 EXPECT_GE(ret, 0);
252 EXPECT_GT(vfs.f_bsize, 0);
253 }
254
255 HWTEST_F(CmdsUnitTest, TestGetCmdKey, TestSize.Level1)
256 {
257 const char *cmd1 = GetCmdKey(0);
258 EXPECT_STREQ(cmd1, "start ");
259 }
260
261 HWTEST_F(CmdsUnitTest, TestDoCmdByIndex, TestSize.Level1)
262 {
263 DoCmdByIndex(1, "/data/init_ut/test_cmd_dir0", nullptr);
264 int ret = access("/data/init_ut/test_cmd_dir0", F_OK);
265 EXPECT_EQ(ret, 0);
266
267 const int execPos = 17;
268 DoCmdByIndex(execPos, "sleep 1", nullptr);
269 DoCmdByIndex(23, "test", nullptr); // 23 is cmd index
270 }
271
272 HWTEST_F(CmdsUnitTest, TestGetCmdLinesFromJson, TestSize.Level1)
273 {
274 const char *jsonStr = "{\"jobs\":[{\"name\":\"init\",\"cmds\":[\"sleep 1\",100,\"test321 123\"]}]}";
275 cJSON* jobItem = cJSON_Parse(jsonStr);
276 ASSERT_NE(nullptr, jobItem);
277 cJSON *cmdsItem = cJSON_GetObjectItem(jobItem, "jobs");
278 ASSERT_NE(nullptr, cmdsItem);
279 ASSERT_TRUE(cJSON_IsArray(cmdsItem));
280
281 cJSON *cmdsItem1 = cJSON_GetArrayItem(cmdsItem, 0);
282 ASSERT_NE(nullptr, cmdsItem1);
283 CmdLines **cmdLines = (CmdLines **)calloc(1, sizeof(CmdLines *));
284 ASSERT_NE(nullptr, cmdLines);
285 int ret = GetCmdLinesFromJson(cmdsItem1, cmdLines);
286 EXPECT_EQ(ret, -1);
287 cJSON *cmdsItem2 = cJSON_GetObjectItem(cmdsItem1, "cmds");
288 ASSERT_NE(nullptr, cmdsItem2);
289 ret = GetCmdLinesFromJson(cmdsItem2, cmdLines);
290 EXPECT_EQ(ret, 0);
291
292 cJSON_Delete(jobItem);
293 if (cmdLines[0] != nullptr) {
294 free(cmdLines[0]);
295 cmdLines[0] = nullptr;
296 }
297 free(cmdLines);
298 cmdLines = nullptr;
299 }
300
301 HWTEST_F(CmdsUnitTest, TestInitCmdFunc, TestSize.Level1)
302 {
303 int ret = GetBootModeFromMisc();
304 EXPECT_EQ(ret, 0);
305 ret = SetFileCryptPolicy(nullptr);
306 EXPECT_NE(ret, 0);
307 }
308
309 HWTEST_F(CmdsUnitTest, TestBuildStringFromCmdArg, TestSize.Level1)
310 {
311 int strNum = 3;
312 struct CmdArgs *ctx = (struct CmdArgs *)calloc(1, sizeof(struct CmdArgs) + sizeof(char *) * (strNum));
313 ctx->argc = strNum;
314 ctx->argv[0] = strdup("123456789012345678901234567890123456789012345678901234567890 \
315 1234567890123456789012345678901234567890123456789012345678901234567");
316 ctx->argv[1] = strdup("test");
317 ctx->argv[2] = nullptr;
318 char *options = BuildStringFromCmdArg(ctx, 0);
319 EXPECT_EQ(options[0], '\0');
320 free(options);
321
322 options = BuildStringFromCmdArg(ctx, 1);
323 EXPECT_STREQ(options, "test");
324 free(options);
325 FreeCmdArg(ctx);
326 }
327
328 HWTEST_F(CmdsUnitTest, TestInitDiffTime, TestSize.Level1)
329 {
330 INIT_TIMING_STAT stat;
331 stat.startTime.tv_sec = 2; // 2 is test sec
332 stat.startTime.tv_nsec = 1000; // 1000 is test nsec
333
334 stat.endTime.tv_sec = 3; // 3 is test sec
335 stat.endTime.tv_nsec = 0;
336
337 long long diff = InitDiffTime(&stat);
338 EXPECT_TRUE(diff > 0);
339 }
340 } // namespace init_ut
341