• 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 "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 
TestProcessSignal(struct signalfd_siginfo * siginfo)57 static int TestProcessSignal(struct signalfd_siginfo *siginfo)
58 {
59     siginfo->ssi_signo = SIGCHLD;
60     ProcessSignal(siginfo);
61     siginfo->ssi_signo = SIGTERM;
62     ProcessSignal(siginfo);
63     siginfo->ssi_signo = SIGUSR1;
64     ProcessSignal(siginfo);
65     return 0;
66 }
67 
68 HWTEST_F(InitUnitTest, TestSignalHandle, TestSize.Level1)
69 {
70     struct signalfd_siginfo siginfo;
71     int ret = TestProcessSignal(&siginfo);
72     EXPECT_EQ(ret, 0);
73 }
74 
75 HWTEST_F(InitUnitTest, TestSystemPrepare, TestSize.Level1)
76 {
77     SetStubResult(STUB_MOUNT, -1);
78     EXPECT_EQ(GetStubResult(STUB_MOUNT), -1);
79     SetStubResult(STUB_MKNODE, -1);
80     EXPECT_EQ(GetStubResult(STUB_MKNODE), -1);
81     CreateFsAndDeviceNode();
82 
83     SetStubResult(STUB_MOUNT, 0);
84     EXPECT_EQ(GetStubResult(STUB_MOUNT), 0);
85     SetStubResult(STUB_MKNODE, 0);
86     EXPECT_EQ(GetStubResult(STUB_MKNODE), 0);
87     CreateFsAndDeviceNode();
88 }
89 
90 HWTEST_F(InitUnitTest, TestSystemExecRcs, TestSize.Level1)
91 {
92     SystemExecuteRcs();
93     Service *service = GetServiceByName("param_watcher");
94     int ret = KeepCapability(service);
95     EXPECT_EQ(ret, 0);
96     ret = SetAmbientCapability(34); // CAP_SYSLOG
97     EXPECT_EQ(ret, 0);
98 }
99 
TestProcessTimer(const TimerHandle taskHandle,void * context)100 static void TestProcessTimer(const TimerHandle taskHandle, void *context)
101 {
102     static int count = 0;
103     printf("ProcessTimer %d\n", count);
104     if (count == 0) { // 2 stop
105         // set service pid for test
106         Service *service = GetServiceByName("param_watcher");
107         if (service != nullptr) {
108             service->pid = getpid();
109         }
110         int fds1[] = {1, 0};
111         ServiceSaveFd("param_watcher", fds1, ARRAY_LENGTH(fds1));
112         ServiceSaveFdWithPoll("param_watcher", fds1, 0);
113         ServiceSaveFdWithPoll("param_watcher", fds1, ARRAY_LENGTH(fds1));
114         EXPECT_EQ(setenv("OHOS_FD_HOLD_param_watcher", "1 0", 0), 0);
115 
116         size_t fdCount = 0;
117         int *fds = ServiceGetFd("param_watcher", &fdCount);
118         EXPECT_TRUE(fds != nullptr);
119         free(fds);
120 
121         ServiceSaveFd("testservice", fds1, ARRAY_LENGTH(fds1));
122         ServiceSaveFd("deviceinfoservice", fds1, ARRAY_LENGTH(fds1));
123     }
124     if (count == 1) {
125         LE_StopTimer(LE_GetDefaultLoop(), taskHandle);
126         LE_StopLoop(LE_GetDefaultLoop());
127     }
128     count++;
129 }
130 
131 HWTEST_F(InitUnitTest, TestFdHoldService, TestSize.Level1)
132 {
133     RegisterFdHoldWatcher(-1);
134     TimerHandle timer = nullptr;
135     int ret = LE_CreateTimer(LE_GetDefaultLoop(), &timer, TestProcessTimer, nullptr);
136     EXPECT_EQ(ret, 0);
137     ret = LE_StartTimer(LE_GetDefaultLoop(), timer, 500, 4);
138     EXPECT_EQ(ret, 0);
139     SystemRun();
140 }
141 
142 HWTEST_F(InitUnitTest, TestInitLog, TestSize.Level1)
143 {
144     // test log
145     CheckAndCreateDir(INIT_LOG_PATH);
146     SetInitLogLevel(INIT_DEBUG);
147     int ret = GetInitLogLevel();
148     EXPECT_EQ(ret, INIT_DEBUG);
149     INIT_LOGI("TestInitLog");
150     INIT_LOGV("TestInitLog");
151     INIT_LOGE("TestInitLog");
152     INIT_LOGW("TestInitLog");
153     INIT_LOGF("TestInitLog");
154     // restore log level
155     SetInitLogLevel(INIT_INFO);
156     ret = GetInitLogLevel();
157     EXPECT_EQ(ret, INIT_INFO);
158 }
159 }
160