• 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 <cerrno>
17 #include <cstdio>
18 #include <ctime>
19 #include <sys/types.h>
20 #include <sys/mount.h>
21 #include <sys/stat.h>
22 
23 #include "init.h"
24 #include "device.h"
25 #include "init_cmds.h"
26 #include "init_log.h"
27 #include "init_service.h"
28 #include "init_unittest.h"
29 #include "init_adapter.h"
30 #include "init_utils.h"
31 #include "loop_event.h"
32 #include "param_stub.h"
33 #include "fs_manager/fs_manager.h"
34 #include "fd_holder.h"
35 #include "fd_holder_service.h"
36 #include "bootstage.h"
37 #include "parameter.h"
38 
39 using namespace testing::ext;
40 using namespace std;
41 
42 extern "C" {
43 INIT_STATIC void ProcessSignal(const struct signalfd_siginfo *siginfo);
SwitchRoot(const char * newRoot)44 int SwitchRoot(const char *newRoot)
45 {
46     return 0;
47 }
48 }
49 
50 namespace init_ut {
51 class InitUnitTest : public testing::Test {
52 public:
SetUpTestCase(void)53     static void SetUpTestCase(void) {};
TearDownTestCase(void)54     static void TearDownTestCase(void) {};
SetUp()55     void SetUp() {};
TearDown()56     void TearDown() {};
57 };
58 
59 HWTEST_F(InitUnitTest, TestSignalHandle, TestSize.Level1)
60 {
61     struct signalfd_siginfo siginfo;
62     siginfo.ssi_signo = SIGCHLD;
63     ProcessSignal(&siginfo);
64     siginfo.ssi_signo = SIGTERM;
65     ProcessSignal(&siginfo);
66     siginfo.ssi_signo = SIGUSR1;
67     ProcessSignal(&siginfo);
68     SUCCEED();
69 }
70 
71 HWTEST_F(InitUnitTest, TestSystemPrepare, TestSize.Level1)
72 {
73     SetStubResult(STUB_MOUNT, -1);
74     SetStubResult(STUB_MKNODE, -1);
75     CreateFsAndDeviceNode();
76 
77     SetStubResult(STUB_MOUNT, 0);
78     SetStubResult(STUB_MKNODE, 0);
79     CreateFsAndDeviceNode();
80 }
81 
82 HWTEST_F(InitUnitTest, TestSystemExecRcs, TestSize.Level1)
83 {
84     SystemExecuteRcs();
85     int ret = KeepCapability();
86     EXPECT_EQ(ret, 0);
87     ret = SetAmbientCapability(34); // CAP_SYSLOG
88     EXPECT_EQ(ret, 0);
89 }
90 
TestProcessTimer(const TimerHandle taskHandle,void * context)91 static void TestProcessTimer(const TimerHandle taskHandle, void *context)
92 {
93     static int count = 0;
94     printf("ProcessTimer %d\n", count);
95     if (count == 0) { // 2 stop
96         // set service pid for test
97         Service *service = GetServiceByName("param_watcher");
98         if (service != nullptr) {
99             service->pid = getpid();
100         }
101         int fds1[] = {1, 0};
102         ServiceSaveFd("param_watcher", fds1, ARRAY_LENGTH(fds1));
103         ServiceSaveFdWithPoll("param_watcher", fds1, 0);
104         ServiceSaveFdWithPoll("param_watcher", fds1, ARRAY_LENGTH(fds1));
105         EXPECT_EQ(setenv("OHOS_FD_HOLD_param_watcher", "1 0", 0), 0);
106 
107         size_t fdCount = 0;
108         int *fds = ServiceGetFd("param_watcher", &fdCount);
109         EXPECT_TRUE(fds != nullptr);
110         free(fds);
111 
112         ServiceSaveFd("testservice", fds1, ARRAY_LENGTH(fds1));
113         ServiceSaveFd("deviceinfoservice", fds1, ARRAY_LENGTH(fds1));
114     }
115     if (count == 1) {
116         LE_StopTimer(LE_GetDefaultLoop(), taskHandle);
117         LE_StopLoop(LE_GetDefaultLoop());
118     }
119     count++;
120 }
121 
122 HWTEST_F(InitUnitTest, TestFdHoldService, TestSize.Level1)
123 {
124     RegisterFdHoldWatcher(-1);
125     TimerHandle timer = nullptr;
126     int ret = LE_CreateTimer(LE_GetDefaultLoop(), &timer, TestProcessTimer, nullptr);
127     EXPECT_EQ(ret, 0);
128     ret = LE_StartTimer(LE_GetDefaultLoop(), timer, 500, 4);
129     EXPECT_EQ(ret, 0);
130     SystemRun();
131 }
132 
133 HWTEST_F(InitUnitTest, TestInitLog, TestSize.Level1)
134 {
135     // test log
136     CheckAndCreateDir(INIT_LOG_PATH);
137     SetInitLogLevel(INIT_DEBUG);
138     INIT_LOGI("TestInitLog");
139     INIT_LOGV("TestInitLog");
140     INIT_LOGE("TestInitLog");
141     INIT_LOGW("TestInitLog");
142     INIT_LOGF("TestInitLog");
143     // restore log level
144     int32_t loglevel = GetIntParameter("persist.init.debug.loglevel", INIT_ERROR);
145     SetInitLogLevel((InitLogLevel)loglevel);
146 }
147 }
148