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 
105 class TraceUnitTest : public testing::Test {
106 public:
SetUpTestCase(void)107     static void SetUpTestCase(void) {};
TearDownTestCase(void)108     static void TearDownTestCase(void) {};
SetUp()109     void SetUp() {};
TearDown()110     void TearDown() {};
111 };
112 
113 HWTEST_F(TraceUnitTest, TraceTest_001, TestSize.Level1)
114 {
115     // open switch for trace
116     uint32_t dataIndex = 0;
117     WriteParam("persist.init.bootevent.enable", "true", &dataIndex, 0);
118     HookMgrExecute(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, nullptr, nullptr);
119     // close switch for trace
120     WriteParam("persist.init.bootevent.enable", "false", &dataIndex, 0);
121     HookMgrExecute(GetBootStageHookMgr(), INIT_POST_PERSIST_PARAM_LOAD, nullptr, nullptr);
122 }
123 
124 HWTEST_F(TraceUnitTest, TraceTest_002, TestSize.Level1)
125 {
126     CreateInitTraceConfig(1);
127     // start trace
128     PluginExecCmdByName("init_trace", "start");
129     // for run 1 s
130     sleep(1);
131     // stop trace
132     PluginExecCmdByName("init_trace", "stop");
133 }
134 
135 HWTEST_F(TraceUnitTest, TraceTest_003, TestSize.Level1)
136 {
137     CreateInitTraceConfig(0);
138     // start trace
139     PluginExecCmdByName("init_trace", "start");
140     // for run 1 s
141     sleep(1);
142     // stop trace
143     PluginExecCmdByName("init_trace", "stop");
144 }
145 
146 HWTEST_F(TraceUnitTest, TraceTest_004, TestSize.Level1)
147 {
148     std::string cmdArgs = "/system/etc/init_trace.cfg  ";
149     cmdArgs += STARTUP_INIT_UT_PATH"/system/etc/init_trace.cfg";
150     int cmdIndex = 0;
151     (void)GetMatchCmd("copy ", &cmdIndex);
152     DoCmdByIndex(cmdIndex, cmdArgs.c_str(), nullptr);
153 
154     // start trace
155     PluginExecCmdByName("init_trace", "start");
156     // for run 1 s
157     sleep(1);
158     // stop trace
159     PluginExecCmdByName("init_trace", "stop");
160 }
161 
162 HWTEST_F(TraceUnitTest, TraceTest_005, TestSize.Level1)
163 {
164     // start trace
165     PluginExecCmdByName("init_trace", "start");
166     // for run 1 s
167     sleep(1);
168     // interrupt trace
169     PluginExecCmdByName("init_trace", "1");
170 }
171 
172 HWTEST_F(TraceUnitTest, TraceTest_006, TestSize.Level1)
173 {
174     std::string cmdArgs = "/bin/test    ";
175     cmdArgs += STARTUP_INIT_UT_PATH"/bin/test";
176     int cmdIndex = 0;
177     (void)GetMatchCmd("copy ", &cmdIndex);
178     DoCmdByIndex(cmdIndex, cmdArgs.c_str(), nullptr);
179 
180     // start trace
181     PluginExecCmdByName("init_trace", "start");
182     // for run 1 s
183     sleep(1);
184     // stop trace
185     PluginExecCmdByName("init_trace", "stop");
186 }
187 
188 HWTEST_F(TraceUnitTest, TraceTest_007, TestSize.Level1)
189 {
190     CreateInitTraceConfig(0);
191     // other case
192     PluginExecCmdByName("init_trace", "other");
193 }
194 } // namespace init_ut
195