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
41 using namespace testing::ext;
42 using namespace std;
43
44 extern "C" {
45 INIT_STATIC void ProcessSignal(const struct signalfd_siginfo *siginfo);
46 }
47
48 namespace init_ut {
49 class InitUnitTest : public testing::Test {
50 public:
SetUpTestCase(void)51 static void SetUpTestCase(void) {};
TearDownTestCase(void)52 static void TearDownTestCase(void) {};
SetUp()53 void SetUp() {};
TearDown()54 void TearDown() {};
55 };
56
57 HWTEST_F(InitUnitTest, TestSignalHandle, TestSize.Level1)
58 {
59 struct signalfd_siginfo siginfo;
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 SUCCEED();
67 }
68
69 HWTEST_F(InitUnitTest, TestSystemPrepare, TestSize.Level1)
70 {
71 SetStubResult(STUB_MOUNT, -1);
72 SetStubResult(STUB_MKNODE, -1);
73 CreateFsAndDeviceNode();
74
75 SetStubResult(STUB_MOUNT, 0);
76 SetStubResult(STUB_MKNODE, 0);
77 CreateFsAndDeviceNode();
78 }
79
80 HWTEST_F(InitUnitTest, TestSystemExecRcs, TestSize.Level1)
81 {
82 SystemExecuteRcs();
83 Service *service = GetServiceByName("param_watcher");
84 int ret = KeepCapability(service);
85 EXPECT_EQ(ret, 0);
86 ret = SetAmbientCapability(34); // CAP_SYSLOG
87 EXPECT_EQ(ret, 0);
88 }
89
TestProcessTimer(const TimerHandle taskHandle,void * context)90 static void TestProcessTimer(const TimerHandle taskHandle, void *context)
91 {
92 static int count = 0;
93 printf("ProcessTimer %d\n", count);
94 if (count == 0) { // 2 stop
95 // set service pid for test
96 Service *service = GetServiceByName("param_watcher");
97 if (service != nullptr) {
98 service->pid = getpid();
99 }
100 int fds1[] = {1, 0};
101 ServiceSaveFd("param_watcher", fds1, ARRAY_LENGTH(fds1));
102 ServiceSaveFdWithPoll("param_watcher", fds1, 0);
103 ServiceSaveFdWithPoll("param_watcher", fds1, ARRAY_LENGTH(fds1));
104 EXPECT_EQ(setenv("OHOS_FD_HOLD_param_watcher", "1 0", 0), 0);
105
106 size_t fdCount = 0;
107 int *fds = ServiceGetFd("param_watcher", &fdCount);
108 EXPECT_TRUE(fds != nullptr);
109 free(fds);
110
111 ServiceSaveFd("testservice", fds1, ARRAY_LENGTH(fds1));
112 ServiceSaveFd("deviceinfoservice", fds1, ARRAY_LENGTH(fds1));
113 }
114 if (count == 1) {
115 LE_StopTimer(LE_GetDefaultLoop(), taskHandle);
116 LE_StopLoop(LE_GetDefaultLoop());
117 }
118 count++;
119 }
120
121 HWTEST_F(InitUnitTest, TestFdHoldService, TestSize.Level1)
122 {
123 RegisterFdHoldWatcher(-1);
124 TimerHandle timer = nullptr;
125 int ret = LE_CreateTimer(LE_GetDefaultLoop(), &timer, TestProcessTimer, nullptr);
126 EXPECT_EQ(ret, 0);
127 ret = LE_StartTimer(LE_GetDefaultLoop(), timer, 500, 4);
128 EXPECT_EQ(ret, 0);
129 SystemRun();
130 }
131
132 HWTEST_F(InitUnitTest, TestInitLog, TestSize.Level1)
133 {
134 // test log
135 CheckAndCreateDir(INIT_LOG_PATH);
136 SetInitLogLevel(INIT_DEBUG);
137 INIT_LOGI("TestInitLog");
138 INIT_LOGV("TestInitLog");
139 INIT_LOGE("TestInitLog");
140 INIT_LOGW("TestInitLog");
141 INIT_LOGF("TestInitLog");
142 // restore log level
143 int32_t loglevel = GetIntParameter("persist.init.debug.loglevel", INIT_ERROR);
144 SetInitLogLevel((InitLogLevel)loglevel);
145 }
146 }
147