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 "init_unittest.h"
17
18 #include <cerrno>
19 #include <cstdio>
20 #include <ctime>
21
22 #include <sys/types.h>
23 #include <sys/mount.h>
24 #include <sys/stat.h>
25
26 #include "init.h"
27 #include "device.h"
28 #include "init_cmds.h"
29 #include "init_log.h"
30 #include "init_service.h"
31 #include "init_adapter.h"
32 #include "init_utils.h"
33 #include "loop_event.h"
34 #include "param_stub.h"
35 #include "fs_manager/fs_manager.h"
36 #include "fd_holder.h"
37 #include "fd_holder_service.h"
38 #include "bootstage.h"
39 #include "parameter.h"
40 #include "fs_dm_linear.h"
41
42 using namespace testing::ext;
43 using namespace std;
44
45 extern "C" {
46 INIT_STATIC void ProcessSignal(const struct signalfd_siginfo *siginfo);
47 }
48
49 namespace init_ut {
50 class InitUnitTest : public testing::Test {
51 public:
SetUpTestCase(void)52 static void SetUpTestCase(void) {};
TearDownTestCase(void)53 static void TearDownTestCase(void) {};
SetUp()54 void SetUp() {};
TearDown()55 void TearDown() {};
56 };
57
TestProcessSignal(struct signalfd_siginfo * siginfo)58 static int TestProcessSignal(struct signalfd_siginfo *siginfo)
59 {
60 siginfo->ssi_signo = SIGCHLD;
61 ProcessSignal(siginfo);
62 siginfo->ssi_signo = SIGTERM;
63 ProcessSignal(siginfo);
64 siginfo->ssi_signo = SIGUSR1;
65 ProcessSignal(siginfo);
66 return 0;
67 }
68
69 HWTEST_F(InitUnitTest, TestSignalHandle, TestSize.Level1)
70 {
71 struct signalfd_siginfo siginfo;
72 int ret = TestProcessSignal(&siginfo);
73 EXPECT_EQ(ret, 0);
74 }
75
76 HWTEST_F(InitUnitTest, TestSystemPrepare, TestSize.Level1)
77 {
78 SetStubResult(STUB_MOUNT, -1);
79 EXPECT_EQ(GetStubResult(STUB_MOUNT), -1);
80 SetStubResult(STUB_MKNODE, -1);
81 EXPECT_EQ(GetStubResult(STUB_MKNODE), -1);
82 CreateFsAndDeviceNode();
83
84 SetStubResult(STUB_MOUNT, 0);
85 EXPECT_EQ(GetStubResult(STUB_MOUNT), 0);
86 SetStubResult(STUB_MKNODE, 0);
87 EXPECT_EQ(GetStubResult(STUB_MKNODE), 0);
88 CreateFsAndDeviceNode();
89 }
90
91 HWTEST_F(InitUnitTest, TestSystemExecRcs, TestSize.Level1)
92 {
93 SystemExecuteRcs();
94 Service *service = GetServiceByName("param_watcher");
95 int ret = KeepCapability(service);
96 EXPECT_EQ(ret, 0);
97 ret = SetAmbientCapability(34); // CAP_SYSLOG
98 EXPECT_EQ(ret, 0);
99 }
100
TestProcessTimer(const TimerHandle taskHandle,void * context)101 static void TestProcessTimer(const TimerHandle taskHandle, void *context)
102 {
103 static int count = 0;
104 printf("ProcessTimer %d\n", count);
105 if (count == 0) { // 2 stop
106 // set service pid for test
107 Service *service = GetServiceByName("param_watcher");
108 if (service != nullptr) {
109 service->pid = getpid();
110 }
111 int fds1[] = {1, 0};
112 ServiceSaveFd("param_watcher", fds1, ARRAY_LENGTH(fds1));
113 ServiceSaveFdWithPoll("param_watcher", fds1, 0);
114 ServiceSaveFdWithPoll("param_watcher", fds1, ARRAY_LENGTH(fds1));
115 EXPECT_EQ(setenv("OHOS_FD_HOLD_param_watcher", "1 0", 0), 0);
116
117 size_t fdCount = 0;
118 int *fds = ServiceGetFd("param_watcher", &fdCount);
119 EXPECT_TRUE(fds != nullptr);
120 free(fds);
121
122 ServiceSaveFd("testservice", fds1, ARRAY_LENGTH(fds1));
123 ServiceSaveFd("deviceinfoservice", fds1, ARRAY_LENGTH(fds1));
124 }
125 if (count == 1) {
126 LE_StopTimer(LE_GetDefaultLoop(), taskHandle);
127 LE_StopLoop(LE_GetDefaultLoop());
128 }
129 count++;
130 }
131
132 HWTEST_F(InitUnitTest, TestFdHoldService, TestSize.Level1)
133 {
134 RegisterFdHoldWatcher(-1);
135 TimerHandle timer = nullptr;
136 int ret = LE_CreateTimer(LE_GetDefaultLoop(), &timer, TestProcessTimer, nullptr);
137 EXPECT_EQ(ret, 0);
138 ret = LE_StartTimer(LE_GetDefaultLoop(), timer, 500, 4);
139 EXPECT_EQ(ret, 0);
140 SystemRun();
141 }
142
143 HWTEST_F(InitUnitTest, TestInitLog, TestSize.Level1)
144 {
145 // test log
146 CheckAndCreateDir(INIT_LOG_PATH);
147 SetInitLogLevel(INIT_DEBUG);
148 int ret = GetInitLogLevel();
149 EXPECT_EQ(ret, INIT_DEBUG);
150 INIT_LOGI("TestInitLog");
151 INIT_LOGV("TestInitLog");
152 INIT_LOGE("TestInitLog");
153 INIT_LOGW("TestInitLog");
154 INIT_LOGF("TestInitLog");
155 // restore log level
156 SetInitLogLevel(INIT_INFO);
157 ret = GetInitLogLevel();
158 EXPECT_EQ(ret, INIT_INFO);
159 }
160
161 HWTEST_F(InitUnitTest, TestParseCfgByPriority, TestSize.Level1)
162 {
163 int ret = 0;
164 ret = ParseCfgByPriority(NULL);
165 EXPECT_EQ(ret, -1);
166 ret = ParseCfgByPriority("etc/init");
167 EXPECT_EQ(ret, 0);
168 }
169
170 HWTEST_F(InitUnitTest, FsDmCreateMultiTargetLinearDevice, TestSize.Level1)
171 {
172 const char *devName = "testDevice";
173 char dmDevPath[256] = "/dev/mapper/testDevice";
174 uint64_t dmDevPathLen = sizeof(dmDevPath);
175
176 DmLinearTarget targets[2];
177 uint32_t targetNum = 2;
178
179 targets[0].start = 0;
180 targets[0].length = 100;
181 targets[0].paras_len = strlen("linear /dev/sda 0") + 1;
182 targets[0].paras = const_cast<char*>("linear /dev/sda 0");
183
184 targets[1].start = 100;
185 targets[1].length = 200;
186 targets[1].paras_len = strlen("linear /dev/sdb 0") + 1;
187
188 int fd = open("/dev/null", O_RDWR);
189 int rc = FsDmCreateMultiTargetLinearDevice(devName, dmDevPath, dmDevPathLen, targets, targetNum);
190 EXPECT_EQ(rc, -1);
191 close(fd);
192 }
193 }
194