1 /*
2 * Copyright (c) 2023 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 "bootstage.h"
17 #include "init_utils.h"
18 #include "init_cmds.h"
19 #include "init_cmdexecutor.h"
20 #include "param_stub.h"
21 #include "securec.h"
22
23 using namespace std;
24 using namespace testing::ext;
25
26 namespace init_ut {
27 static const char *g_content =
28 "\"KERNEL\" : ["
29 "{"
30 "\"name\" : \"disk\","
31 "\"description\" : \"Disk I/O\","
32 "\"tag\" : 0,"
33 "\"type\" : \"KERNEL\","
34 "\"sys-files\" : ["
35 "\"events/f2fs/f2fs_sync_file_enter/enable\","
36 "\"events/f2fs/f2fs_sync_file_exit/enable\","
37 "\"events/f2fs/f2fs_write_begin/enable\","
38 "\"events/f2fs/f2fs_write_end/enable\","
39 "\"events/ext4/ext4_da_write_begin/enable\","
40 "\"events/ext4/ext4_da_write_end/enable\","
41 "\"events/ext4/ext4_sync_file_enter/enable\","
42 "\"events/ext4/ext4_sync_file_exit/enable\","
43 "\"events/block/block_rq_issue/enable\","
44 "\"events/block/block_rq_complete/enable\""
45 "]"
46 "},"
47 "{"
48 "\"name\" : \"mmc\","
49 "\"description\" : \"eMMC commands\","
50 "\"tag\" : 0,"
51 "\"type\" : \"KERNEL\","
52 "\"sys-files\" : ["
53 "\"events/mmc/enable\""
54 "]"
55 "},"
56 "{"
57 "\"name\" : \"test\","
58 "\"description\" : \"test\","
59 "\"tag\" : 0,"
60 "\"type\" : \"KERNEL\","
61 "\"sys-files\" : ["
62 "]"
63 "}"
64 "],"
65 "\"USER\" : ["
66 "{"
67 "\"name\" : \"ohos\","
68 "\"description\" : \"OpenHarmony\","
69 "\"tag\" : 30,"
70 "\"type\" : \"USER\","
71 "\"sys-files\" : ["
72 "]"
73 "},"
74 "{"
75 "\"name\" : \"ability\","
76 "\"description\" : \"Ability Manager\","
77 "\"tag\" : 31,"
78 "\"type\" : \"USER\","
79 "\"sys-files\" : ["
80 "]"
81 "},"
82 "{"
83 "\"name\" : \"usb\","
84 "\"description\" : \"usb subsystem\","
85 "\"tag\" : 19,"
86 "\"type\" : \"USER\","
87 "\"sys-files\" : ["
88 "]"
89 "}"
90 "]"
91 "}";
CreateInitTraceConfig(int compress)92 void CreateInitTraceConfig(int compress)
93 {
94 std::string config = "{ \"compress\" : ";
95 if (!compress) {
96 config += "false,";
97 } else {
98 config += "true, ";
99 }
100 config += g_content;
101 // create trace cfg
102 CreateTestFile(STARTUP_INIT_UT_PATH"/system/etc/init_trace.cfg", config.c_str());
103 }
104
TestPluginExecCmdByName(const char * name,const char * cmdContent)105 static int TestPluginExecCmdByName(const char *name, const char *cmdContent)
106 {
107 PluginExecCmdByName(name, cmdContent);
108 return 0;
109 }
110
111 class TraceUnitTest : public testing::Test {
112 public:
SetUpTestCase(void)113 static void SetUpTestCase(void) {};
TearDownTestCase(void)114 static void TearDownTestCase(void) {};
SetUp()115 void SetUp() {};
TearDown()116 void TearDown() {};
117 };
118
119 HWTEST_F(TraceUnitTest, TraceTest_001, TestSize.Level1)
120 {
121 // open switch for trace
122 uint32_t dataIndex = 0;
123 int ret = WriteParam("persist.init.bootevent.enable", "true", &dataIndex, 0);
124 EXPECT_EQ(ret, 0);
125 HookMgrExecute(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, nullptr, nullptr);
126 // close switch for trace
127 ret = WriteParam("persist.init.bootevent.enable", "false", &dataIndex, 0);
128 EXPECT_EQ(ret, 0);
129 HookMgrExecute(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, nullptr, nullptr);
130 }
131
132 HWTEST_F(TraceUnitTest, TraceTest_002, TestSize.Level1)
133 {
134 CreateInitTraceConfig(1);
135 // start trace
136 int ret = TestPluginExecCmdByName("init_trace", "start");
137 EXPECT_EQ(ret, 0);
138 // for run 1 s
139 sleep(1);
140 // stop trace
141 ret = TestPluginExecCmdByName("init_trace", "stop");
142 EXPECT_EQ(ret, 0);
143 }
144
145 HWTEST_F(TraceUnitTest, TraceTest_003, TestSize.Level1)
146 {
147 CreateInitTraceConfig(0);
148 // start trace
149 int ret = TestPluginExecCmdByName("init_trace", "start");
150 // for run 1 s
151 sleep(1);
152 // stop trace
153 ret = TestPluginExecCmdByName("init_trace", "stop");
154 EXPECT_EQ(ret, 0);
155 }
156
157 HWTEST_F(TraceUnitTest, TraceTest_004, TestSize.Level1)
158 {
159 std::string cmdArgs = "/system/etc/init_trace.cfg ";
160 cmdArgs += STARTUP_INIT_UT_PATH"/system/etc/init_trace.cfg";
161 int cmdIndex = 0;
162 (void)GetMatchCmd("copy ", &cmdIndex);
163 DoCmdByIndex(cmdIndex, cmdArgs.c_str(), nullptr);
164
165 // start trace
166 int ret = TestPluginExecCmdByName("init_trace", "start");
167 EXPECT_EQ(ret, 0);
168 // for run 1 s
169 sleep(1);
170 // stop trace
171 ret = TestPluginExecCmdByName("init_trace", "stop");
172 EXPECT_EQ(ret, 0);
173 }
174
175 HWTEST_F(TraceUnitTest, TraceTest_005, TestSize.Level1)
176 {
177 // start trace
178 int ret = TestPluginExecCmdByName("init_trace", "start");
179 EXPECT_EQ(ret, 0);
180 // for run 1 s
181 sleep(1);
182 // interrupt trace
183 ret = TestPluginExecCmdByName("init_trace", "1");
184 EXPECT_EQ(ret, 0);
185 }
186
187 HWTEST_F(TraceUnitTest, TraceTest_006, TestSize.Level1)
188 {
189 std::string cmdArgs = "/bin/test ";
190 cmdArgs += STARTUP_INIT_UT_PATH"/bin/test";
191 int cmdIndex = 0;
192 (void)GetMatchCmd("copy ", &cmdIndex);
193 DoCmdByIndex(cmdIndex, cmdArgs.c_str(), nullptr);
194
195 // start trace
196 int ret = TestPluginExecCmdByName("init_trace", "start");
197 EXPECT_EQ(ret, 0);
198 // for run 1 s
199 sleep(1);
200 // stop trace
201 ret = TestPluginExecCmdByName("init_trace", "stop");
202 EXPECT_EQ(ret, 0);
203 }
204
205 HWTEST_F(TraceUnitTest, TraceTest_007, TestSize.Level1)
206 {
207 CreateInitTraceConfig(0);
208 // other case
209 int ret = TestPluginExecCmdByName("init_trace", "other");
210 EXPECT_EQ(ret, 0);
211 }
212 } // namespace init_ut
213