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