1 /*
2 * Copyright (c) 2022 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 <cinttypes>
17 #include <sys/mount.h>
18 #include "fs_manager/fs_manager.h"
19 #include "init_log.h"
20 #include "init_param.h"
21 #include "param_stub.h"
22 #include "securec.h"
23 #include "systemcapability.h"
24 #include "service_control.h"
25 #include "control_fd.h"
26 #include "loop_event.h"
27 #include "fd_holder.h"
28 #include "fd_holder_internal.h"
29
30 using namespace testing::ext;
31 using namespace std;
32
33 namespace init_ut {
34
35 extern "C" {
36 void CmdDisConnectComplete(const TaskHandle client);
37 void CmdOnSendMessageComplete(const TaskHandle task, const BufferHandle handle);
38 void CmdOnClose(const TaskHandle task);
39 void CmdOnConnectComplete(const TaskHandle client);
40 void CmdOnRecvMessage(const TaskHandle task, const uint8_t *buffer, uint32_t buffLen);
41 int InitPtyInterface(CmdAgent *agent, uint16_t type, const char *cmd);
42 void ProcessPtyRead(const WatcherHandle taskHandle, int fd, uint32_t *events, const void *context);
43 void ProcessPtyWrite(const WatcherHandle taskHandle, int fd, uint32_t *events, const void *context);
44 int CmdOnIncommingConnect(const LoopHandle loop, const TaskHandle server);
45 CmdAgent *CmdAgentCreate(const char *server);
46 void CmdClientOnRecvMessage(const TaskHandle task, const uint8_t *buffer, uint32_t buffLen);
47 int SendCmdMessage(const CmdAgent *agent, uint16_t type, const char *cmd, const char *ptyName);
48 int SendMessage(LoopHandle loop, TaskHandle task, const char *message);
49 int *GetFdsFromMsg(size_t *outFdCount, pid_t *requestPid, struct msghdr msghdr);
50 int BuildSendData(char *buffer, size_t size, const char *serviceName, bool hold, bool poll);
51 }
52
53 class InnerkitsUnitTest : public testing::Test {
54 public:
SetUpTestCase(void)55 static void SetUpTestCase(void) {};
TearDownTestCase(void)56 static void TearDownTestCase(void) {};
SetUp()57 void SetUp() {};
TearDown()58 void TearDown() {};
59 };
60
61 /**
62 * @tc.name: ReadFstabFromFile_unitest
63 * @tc.desc: read fstab from test file.
64 * @tc.type: FUNC
65 * @tc.require:
66 * @tc.author:
67 */
68 HWTEST_F(InnerkitsUnitTest, ReadFstabFromFile_unitest, TestSize.Level1)
69 {
70 Fstab *fstab = nullptr;
71 const std::string fstabFile1 = "/data/fstab.updater1";
72 fstab = ReadFstabFromFile(fstabFile1.c_str(), false);
73 EXPECT_EQ(fstab, nullptr);
74 const std::string fstabFile2 = STARTUP_INIT_UT_PATH"/mount_unitest/ReadFstabFromFile1.fstable";
75 fstab = ReadFstabFromFile(fstabFile2.c_str(), false);
76 EXPECT_NE(fstab, nullptr);
77 ParseFstabPerLine(const_cast<char *>("test"), fstab, true, nullptr);
78 ReleaseFstab(fstab);
79 }
80
81 /**
82 * @tc.name: FindFstabItemForPath_unitest
83 * @tc.desc: read fstab from test file, then find item according to path.
84 * @tc.type: FUNC
85 * @tc.require:
86 * @tc.author:
87 */
88 HWTEST_F(InnerkitsUnitTest, FindFstabItemForPath_unitest, TestSize.Level1)
89 {
90 const std::string fstabFile1 = STARTUP_INIT_UT_PATH"/mount_unitest/ReadFstabFromFile1.fstable";
91 Fstab *fstab = nullptr;
92 fstab = ReadFstabFromFile(fstabFile1.c_str(), false);
93 ASSERT_NE(fstab, nullptr);
94 FstabItem* item = nullptr;
95 const std::string path1 = "";
96 item = FindFstabItemForPath(*fstab, path1.c_str());
97 if (item == nullptr) {
98 SUCCEED();
99 }
100 const std::string path2 = "/data";
101 item = FindFstabItemForPath(*fstab, path2.c_str());
102 if (item != nullptr) {
103 SUCCEED();
104 }
105 const std::string path3 = "/data2";
106 item = FindFstabItemForPath(*fstab, path3.c_str());
107 if (item == nullptr) {
108 SUCCEED();
109 }
110 const std::string path4 = "/data2/test";
111 item = FindFstabItemForPath(*fstab, path4.c_str());
112 if (item != nullptr) {
113 SUCCEED();
114 }
115 ReleaseFstab(fstab);
116 fstab = nullptr;
117 }
118
119 /**
120 * @tc.name: FindFstabItemForMountPoint_unitest
121 * @tc.desc: read fstab from test file, then find item that matches with the mount point.
122 * @tc.type: FUNC
123 * @tc.require:
124 * @tc.author:
125 */
126 HWTEST_F(InnerkitsUnitTest, FindFstabItemForMountPoint_unitest, TestSize.Level1)
127 {
128 const std::string fstabFile1 = STARTUP_INIT_UT_PATH"/mount_unitest/ReadFstabFromFile1.fstable";
129 Fstab *fstab = nullptr;
130 fstab = ReadFstabFromFile(fstabFile1.c_str(), false);
131 ASSERT_NE(fstab, nullptr);
132 FstabItem* item = nullptr;
133 const std::string mp1 = "/data";
134 const std::string mp2 = "/data2";
135 item = FindFstabItemForMountPoint(*fstab, mp2.c_str());
136 if (item == nullptr) {
137 SUCCEED();
138 }
139 const std::string mp3 = "/data";
140 item = FindFstabItemForMountPoint(*fstab, mp3.c_str());
141 if (item != nullptr) {
142 SUCCEED();
143 }
144 ReleaseFstab(fstab);
145 fstab = nullptr;
146 }
147
148 /**
149 * @tc.name: GetMountFlags_unitest
150 * @tc.desc: read fstab from test file, then find the item and get mount flags.
151 * @tc.type: FUNC
152 * @tc.require:
153 * @tc.author:
154 */
155 HWTEST_F(InnerkitsUnitTest, GetMountFlags_unitest, TestSize.Level1)
156 {
157 const std::string fstabFile1 = STARTUP_INIT_UT_PATH"/mount_unitest/ReadFstabFromFile1.fstable";
158 Fstab *fstab = nullptr;
159 fstab = ReadFstabFromFile(fstabFile1.c_str(), true);
160 ASSERT_NE(fstab, nullptr);
161 struct FstabItem* item = nullptr;
162 const std::string mp = "/hos";
163 item = FindFstabItemForMountPoint(*fstab, mp.c_str());
164 if (item == nullptr) {
165 SUCCEED();
166 }
167 const int bufferSize = 512;
168 char fsSpecificOptions[bufferSize] = {0};
169 unsigned long flags = GetMountFlags(item->mountOptions, fsSpecificOptions, bufferSize, item->mountPoint);
170 EXPECT_EQ(flags, static_cast<unsigned long>(MS_NOSUID | MS_NODEV | MS_NOATIME));
171 ReleaseFstab(fstab);
172 fstab = nullptr;
173 }
174
175 /**
176 * @tc.name: GetSlotInfo_unittest
177 * @tc.desc: get the number of slots and get current slot.
178 * @tc.type: FUNC
179 * @tc.require:issueI5NTX2
180 * @tc.author:
181 */
182 HWTEST_F(InnerkitsUnitTest, GetSlotInfo_unittest, TestSize.Level1)
183 {
184 EXPECT_NE(GetBootSlots(), -1);
185 EXPECT_NE(GetCurrentSlot(), -1);
186 }
187
188 /**
189 * @tc.name: LoadFstabFromCommandLine_unittest
190 * @tc.desc: get fstab from command line.
191 * @tc.type: FUNC
192 * @tc.require:issueI5NTX2
193 * @tc.author:
194 */
195 HWTEST_F(InnerkitsUnitTest, LoadFstabFromCommandLine_unittest, TestSize.Level1)
196 {
197 EXPECT_NE(LoadFstabFromCommandLine(), (Fstab *)NULL);
198 }
199
200 /**
201 * @tc.name: GetBlockDevicePath_unittest
202 * @tc.desc: get block device path according to valid or invalid partition.
203 * @tc.type: FUNC
204 * @tc.require:issueI5NTX2
205 * @tc.author:
206 */
207 HWTEST_F(InnerkitsUnitTest, GetBlockDevicePath_unittest, TestSize.Level1)
208 {
209 char devicePath[MAX_BUFFER_LEN] = {0};
210 EXPECT_EQ(GetBlockDevicePath("/vendor", devicePath, MAX_BUFFER_LEN), 0);
211 EXPECT_EQ(GetBlockDevicePath("/misc", devicePath, MAX_BUFFER_LEN), 0);
212 EXPECT_EQ(GetBlockDevicePath("/invalid", devicePath, MAX_BUFFER_LEN), -1);
213 unlink(BOOT_CMD_LINE);
214 EXPECT_EQ(GetBlockDevicePath("/invalid", devicePath, MAX_BUFFER_LEN), -1);
215 GetCurrentSlot();
216 }
217
218 /**
219 * @tc.name: DoFormat_unittest
220 * @tc.desc: format file system, includes ext4 and f2fs type.
221 * @tc.type: FUNC
222 * @tc.require:
223 * @tc.author:
224 */
225 HWTEST_F(InnerkitsUnitTest, DoFormat_unittest, TestSize.Level1)
226 {
227 EXPECT_NE(DoFormat("/testpath", "ext4"), -1);
228 EXPECT_NE(DoFormat("/testpath", "f2fs"), -1);
229 EXPECT_EQ(DoFormat("/testpath", "notFs"), -1);
230 EXPECT_EQ(DoFormat(nullptr, nullptr), -1);
231 }
232
233 /**
234 * @tc.name: MountAllWithFstabFile_unittest
235 * @tc.desc: mount partitions according to fstab that read from file.
236 * @tc.type: FUNC
237 * @tc.require:issueI5NTX2
238 * @tc.author:
239 */
240 HWTEST_F(InnerkitsUnitTest, MountAllWithFstabFile_unittest, TestSize.Level1)
241 {
242 EXPECT_NE(MountAllWithFstabFile(STARTUP_INIT_UT_PATH"/etc/fstab.required", 0), 1);
243 EXPECT_NE(UmountAllWithFstabFile(STARTUP_INIT_UT_PATH"/etc/fstab.required"), 1);
244 EXPECT_EQ(MountAllWithFstabFile("/testErrorFile", 0), -1);
245 EXPECT_EQ(MountAllWithFstabFile(nullptr, 0), -1);
246 EXPECT_EQ(GetMountStatusForMountPoint(nullptr), -1);
247 FstabItem fstabItem;
248 fstabItem.fsType = strdup("notSupport");
249 EXPECT_EQ(MountOneItem(nullptr), -1);
250 EXPECT_EQ(MountOneItem(&fstabItem), 0);
251 if (fstabItem.fsType != nullptr) {
252 free(fstabItem.fsType);
253 fstabItem.fsType = nullptr;
254 }
255 }
256
257 #define SYSCAP_MAX_SIZE 100
258
259 // TestSysCap
260 HWTEST_F(InnerkitsUnitTest, TestSysCap, TestSize.Level1)
261 {
262 bool ret = HasSystemCapability("test.cap");
263 EXPECT_EQ(ret, false);
264 ret = HasSystemCapability(nullptr);
265 EXPECT_EQ(ret, false);
266 ret = HasSystemCapability("ArkUI.ArkUI.Napi");
267 EXPECT_EQ(ret, true);
268 ret = HasSystemCapability("SystemCapability.ArkUI.ArkUI.Napi");
269 EXPECT_EQ(ret, true);
270 char *wrongName = reinterpret_cast<char *>(malloc(SYSCAP_MAX_SIZE));
271 ASSERT_NE(wrongName, nullptr);
272 EXPECT_EQ(memset_s(wrongName, SYSCAP_MAX_SIZE, 1, SYSCAP_MAX_SIZE), 0);
273 HasSystemCapability(wrongName);
274 free(wrongName);
275 }
276
277 // TestControlService
278 HWTEST_F(InnerkitsUnitTest, TestControlService, TestSize.Level1)
279 {
280 TestSetParamCheckResult("startup.service.ctl.", 0777, 0);
281 ServiceControl("deviceinfoservice", START);
282 SystemWriteParam("startup.service.ctl.deviceinfoservice", "2");
283 ServiceControl("deviceinfoservice", RESTART);
284 ServiceControl("deviceinfoservice", STOP);
285 SystemWriteParam("startup.service.ctl.deviceinfoservice", "0");
286 ServiceControl("param_watcher", RESTART);
287 EXPECT_EQ(ServiceControl(nullptr, RESTART), -1);
288 const char *argv[] = {"testArg"};
289 ServiceControlWithExtra("deviceinfoservice", RESTART, argv, 1);
290 ServiceControlWithExtra(nullptr, RESTART, argv, 1);
291 ServiceControlWithExtra(nullptr, 3, argv, 1); // 3 is action
292 ServiceControlWithExtra("notservie", RESTART, argv, 1);
293 ServiceControlWithExtra("deviceinfoservice", 3, argv, 1); // 3 is action
294 ServiceSetReady("deviceinfoservice");
295 ServiceSetReady(nullptr);
296 ServiceWaitForStatus("deviceinfoservice", SERVICE_READY, 1);
297 ServiceWaitForStatus("deviceinfoservice", SERVICE_READY, -1);
298 ServiceWaitForStatus(nullptr, SERVICE_READY, 1);
299 StartServiceByTimer("deviceinfoservice", 1);
300 StartServiceByTimer("deviceinfoservice", 0);
301 StartServiceByTimer(nullptr, 0);
302 StopServiceTimer("deviceinfoservice");
303 }
304
TestIncommingConnect(const LoopHandle loop,const TaskHandle server)305 static int TestIncommingConnect(const LoopHandle loop, const TaskHandle server)
306 {
307 UNUSED(loop);
308 UNUSED(server);
309 return 0;
310 }
311
312 // TestControlFd
313 HWTEST_F(InnerkitsUnitTest, TestControlFd, TestSize.Level1)
314 {
315 CmdClientInit("/data/testSock1", ACTION_DUMP, "cmd");
316 CmdClientInit("/data/testSock1", ACTION_DUMP, "cmd");
317 CmdClientInit(INIT_CONTROL_FD_SOCKET_PATH, ACTION_DUMP, nullptr);
318 CmdClientInit(nullptr, ACTION_DUMP, "cmd");
319 CmdDisConnectComplete(nullptr);
320 CmdOnSendMessageComplete(nullptr, nullptr);
321 CmdOnConnectComplete(nullptr);
322 CmdClientOnRecvMessage(nullptr, nullptr, 0);
323 CmdAgentCreate(nullptr);
324 CmdAgent *agent = CmdAgentCreate(INIT_CONTROL_FD_SOCKET_PATH);
325 EXPECT_NE(agent, nullptr);
326 SendCmdMessage(agent, ACTION_DUMP, "cmd", "test");
327 SendCmdMessage(agent, ACTION_DUMP, "cmd", nullptr);
328 SendMessage(nullptr, nullptr, nullptr);
329 uint32_t events = 0;
330 InitPtyInterface(agent, 0, "cmd");
331 InitPtyInterface(agent, 0, nullptr);
332 InitPtyInterface(nullptr, 0, nullptr);
333 mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
334 CheckAndCreatFile("/data/init_ut/testInput", mode);
335 int fd = open("/data/init_ut/testInput", O_RDWR);
336 perror("write failed");
337 EXPECT_GT(fd, 0);
338 EXPECT_GT(write(fd, "test", strlen("test")), 0);
339 perror("write failed");
340 lseek(fd, 0, SEEK_SET);
341 ProcessPtyRead(nullptr, fd, &events, (void *)agent);
342 ProcessPtyRead(nullptr, fd, &events, (void *)agent);
343 ProcessPtyRead(nullptr, STDERR_FILENO, &events, nullptr);
344 lseek(fd, 0, SEEK_SET);
345 ProcessPtyWrite(nullptr, fd, &events, (void *)agent);
346 ProcessPtyWrite(nullptr, fd, &events, (void *)agent);
347 ProcessPtyWrite(nullptr, STDERR_FILENO, &events, nullptr);
348 close(fd);
349
350 CmdOnClose(agent->task);
351 }
352
353 HWTEST_F(InnerkitsUnitTest, TestControlFdServer, TestSize.Level1)
354 {
355 CmdServiceInit(nullptr, nullptr);
__anon2210d2240102(uint16_t type, const char *serviceCmd, const void *context) 356 CmdServiceInit("/data/testSock1", [](uint16_t type, const char *serviceCmd, const void *context) {
357 UNUSED(type);
358 UNUSED(serviceCmd);
359 UNUSED(context);
360 });
361
362 TaskHandle testServer;
363 LE_StreamServerInfo info = {};
364 info.baseInfo.flags = TASK_STREAM | TASK_SERVER | TASK_PIPE | TASK_TEST;
365 info.server = (char *)"/data/testSock1";
366 info.socketId = -1;
367 info.baseInfo.close = NULL;
368 info.disConnectComplete = NULL;
369 info.incommingConnect = TestIncommingConnect;
370 info.sendMessageComplete = NULL;
371 info.recvMessage = NULL;
372 (void)LE_CreateStreamServer(LE_GetDefaultLoop(), &testServer, &info);
373 CmdOnIncommingConnect(LE_GetDefaultLoop(), testServer);
374
375 CmdOnRecvMessage(testServer, nullptr, 0);
376 CmdMessage *cmdMsg = (CmdMessage *)malloc(sizeof(CmdMessage) + strlen("test"));
377 cmdMsg->type = ACTION_DUMP;
378 cmdMsg->ptyName[0] = '\0';;
379 CmdOnRecvMessage(testServer, (uint8_t *)(&cmdMsg), 0);
380 cmdMsg->type = ACTION_DUMP;
381 cmdMsg->cmd[0] = 'a';
382 cmdMsg->ptyName[0] = 'a';
383 CmdOnRecvMessage(testServer, (uint8_t *)(&cmdMsg), 0);
384 CmdServiceProcessDelClient(0);
385 CmdServiceProcessDelClient(0);
386 free(cmdMsg);
387 }
388
389 HWTEST_F(InnerkitsUnitTest, TestHoldFd, TestSize.Level1)
390 {
391 int fds1[] = {1, 0};
392 ServiceSaveFd("testServiceName", fds1, ARRAY_LENGTH(fds1));
393 ServiceSaveFd(nullptr, fds1, ARRAY_LENGTH(fds1));
394 ServiceSaveFdWithPoll("testServiceName", fds1, 0);
395 ServiceSaveFdWithPoll(nullptr, fds1, 0);
396 ServiceSaveFdWithPoll("testServiceName", fds1, ARRAY_LENGTH(fds1));
397 EXPECT_EQ(setenv("OHOS_FD_HOLD_testServiceName", "1 0", 0), 0);
398
399 size_t fdCount = 0;
400 int *fds = nullptr;
401 ServiceGetFd("testService", nullptr);
402 ServiceGetFd("testService", &fdCount);
403 char *wrongName = (char *)malloc(MAX_FD_HOLDER_BUFFER + 1);
404 ASSERT_NE(wrongName, nullptr);
405 EXPECT_EQ(memset_s(wrongName, MAX_FD_HOLDER_BUFFER + 1, 1, MAX_FD_HOLDER_BUFFER + 1), 0);
406 ServiceGetFd(wrongName, &fdCount);
407 BuildSendData(wrongName, 1, "testService", 0, 1);
408 BuildSendData(wrongName, 1, "testService", 0, 0);
409 BuildSendData(nullptr, 1, "testService", 0, 0);
410 free(wrongName);
411
412 fds = ServiceGetFd("testServiceName", &fdCount);
413 EXPECT_NE(fds, nullptr);
414 struct msghdr msghdr;
415 BuildControlMessage(nullptr, nullptr, 1, 0);
416 BuildControlMessage(&msghdr, nullptr, 1, 0);
417 BuildControlMessage(&msghdr, fds, -1, 0);
418 BuildControlMessage(&msghdr, fds, -1, 1);
419
420 if (fds != nullptr)
421 {
422 free(fds);
423 fds = nullptr;
424 }
425 }
426
427 HWTEST_F(InnerkitsUnitTest, TestHoldFd2, TestSize.Level1)
428 {
429 size_t fdCount = 0;
430 int *fds = nullptr;
431 char buffer[MAX_FD_HOLDER_BUFFER + 1] = {};
432 pid_t requestPid = -1;
433 struct msghdr msghdr;
434 GetFdsFromMsg(&fdCount, &requestPid, msghdr);
435 msghdr.msg_flags = MSG_TRUNC;
436 GetFdsFromMsg(&fdCount, &requestPid, msghdr);
437 struct iovec iovec = {
438 .iov_base = buffer,
439 .iov_len = MAX_FD_HOLDER_BUFFER,
440 };
441 ReceiveFds(0, iovec, &fdCount, false, &requestPid);
442 fds = ReceiveFds(0, iovec, &fdCount, true, &requestPid);
443 if (fds != nullptr)
444 {
445 free(fds);
446 fds = nullptr;
447 }
448 if (msghdr.msg_control != nullptr) {
449 free(msghdr.msg_control);
450 }
451 }
452
453 } // namespace init_ut
454