1 /*
2  * Copyright (c) Huawei Technologies Co., Ltd. 2021. All rights reserved.
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 <gtest/gtest.h>
17 
18 #include "file_utils.h"
19 #include "flow_controller.h"
20 #include "ftrace_fs_ops.h"
21 
22 namespace {
23 using FTRACE_NS::FlowController;
24 using FTRACE_NS::FtraceFsOps;
25 using testing::ext::TestSize;
26 
27 constexpr uint32_t BUFFER_SIZE_KB = 256;
28 constexpr uint32_t FLUSH_INTERVAL_MS = 100;
29 constexpr uint32_t FLUSH_THRESHOLD_KB = 1024;
30 constexpr uint32_t TRACE_PERIOD_MS = 500;
31 using WriterStructPtr = std::unique_ptr<WriterStruct>::pointer;
32 using ConstVoidPtr = std::unique_ptr<const void>::pointer;
33 
34 long WriteFunc(WriterStructPtr writer, ConstVoidPtr data, size_t size);
35 bool FlushFunc(WriterStructPtr writer);
36 
37 class FtraceFsOpsTest : public ::testing::Test {
38 protected:
SetUpTestCase()39     static void SetUpTestCase()
40     {
41         // start tracing sched_switch event
42         FlowController controller;
43         TracePluginConfig config;
44 
45         // set writer
46         WriterStruct writer = {WriteFunc, FlushFunc};
47         controller.SetWriter(static_cast<WriterStructPtr>(&writer));
48         // stop capture firstly
49         controller.StopCapture();
50         // set config
51         config.add_ftrace_events("sched/sched_switch");
52         config.add_hitrace_categories("ability");
53         config.add_hitrace_categories("ace");
54         config.set_buffer_size_kb(BUFFER_SIZE_KB);
55         config.set_flush_interval_ms(FLUSH_INTERVAL_MS);
56         config.set_flush_threshold_kb(FLUSH_THRESHOLD_KB);
57         config.set_parse_ksyms(true);
58         config.set_clock("global");
59         config.set_trace_period_ms(TRACE_PERIOD_MS);
60         config.set_raw_data_prefix("/data/local/tmp/raw_trace_");
61         std::vector<uint8_t> configData(config.ByteSizeLong());
62         config.SerializeToArray(configData.data(), configData.size());
63         controller.LoadConfig(configData.data(), configData.size());
64         controller.StartCapture();
65         sleep(1);
66         controller.StopCapture();
67     }
68 
69     void SetUp() override {}
70     void TearDown() override {}
71 };
72 
WriteFunc(WriterStructPtr writer, ConstVoidPtr data, size_t size)73 long WriteFunc(WriterStructPtr writer, ConstVoidPtr data, size_t size)
74 {
75     if (writer == nullptr || data == nullptr || size <= 0) {
76         return -1;
77     }
78 
79     return 0;
80 }
81 
FlushFunc(WriterStructPtr writer)82 bool FlushFunc(WriterStructPtr writer)
83 {
84     if (writer == nullptr) {
85         return false;
86     }
87     return true;
88 }
89 
90 /*
91  * @tc.name: GetFtraceRoot
92  * @tc.desc: test FtraceFsOps::GetFtraceRoot with normal case.
93  * @tc.type: FUNC
94  */
HWTEST_F(FtraceFsOpsTest, GetFtraceRoot, TestSize.Level1)95 HWTEST_F(FtraceFsOpsTest, GetFtraceRoot, TestSize.Level1)
96 {
97     std::string path = FtraceFsOps::GetInstance().GetFtraceRoot();
98     ASSERT_STRNE(path.c_str(), "");
99     EXPECT_TRUE((strcmp(path.c_str(), "/sys/kernel/tracing") || strcmp(path.c_str(), "/sys/kernel/debug/tracing")));
100 }
101 
102 /*
103  * @tc.name: GetKernelSymbols
104  * @tc.desc: test FtraceFsOps::GetKernelSymbols with normal case.
105  * @tc.type: FUNC
106  */
HWTEST_F(FtraceFsOpsTest, GetKernelSymbols, TestSize.Level1)107 HWTEST_F(FtraceFsOpsTest, GetKernelSymbols, TestSize.Level1)
108 {
109     std::string content = FtraceFsOps::GetInstance().GetKernelSymbols();
110     EXPECT_STRNE(content.c_str(), "");
111 }
112 
113 /*
114  * @tc.name: GetPrintkFormatsNormal
115  * @tc.desc: test FtraceFsOps::GetPrintkFormats with normal case.
116  * @tc.type: FUNC
117  */
HWTEST_F(FtraceFsOpsTest, GetPrintkFormatsNormal, TestSize.Level1)118 HWTEST_F(FtraceFsOpsTest, GetPrintkFormatsNormal, TestSize.Level1)
119 {
120     std::string content = FtraceFsOps::GetInstance().GetPrintkFormats();
121     EXPECT_STRNE(content.c_str(), "");
122 }
123 
124 /*
125  * @tc.name: GetPrintkFormatsFalse
126  * @tc.desc: test FtraceFsOps::GetPrintkFormats with false case.
127  * @tc.type: FUNC
128  */
HWTEST_F(FtraceFsOpsTest, GetPrintkFormatsFalse, TestSize.Level1)129 HWTEST_F(FtraceFsOpsTest, GetPrintkFormatsFalse, TestSize.Level1)
130 {
131     FtraceFsOps ftraceFsOps;
132     ftraceFsOps.SetFtraceRoot("");
133     std::string content = ftraceFsOps.GetPrintkFormats();
134     EXPECT_STREQ(content.c_str(), "");
135 
136     ftraceFsOps.SetFtraceRoot("/test_path");
137     content = ftraceFsOps.GetPrintkFormats();
138     EXPECT_STREQ(content.c_str(), "");
139 }
140 
141 /*
142  * @tc.name: GetProcessCommNormal
143  * @tc.desc: test FtraceFsOps::GetProcessComm with normal case.
144  * @tc.type: FUNC
145  */
HWTEST_F(FtraceFsOpsTest, GetProcessCommNormal, TestSize.Level1)146 HWTEST_F(FtraceFsOpsTest, GetProcessCommNormal, TestSize.Level1)
147 {
148     int32_t pid = getpid();
149     std::string content = FtraceFsOps::GetInstance().GetProcessComm(pid);
150     EXPECT_STRNE(content.c_str(), "");
151 }
152 
153 /*
154  * @tc.name: GetProcessCommFalse
155  * @tc.desc: test FtraceFsOps::GetProcessComm with false case.
156  * @tc.type: FUNC
157  */
HWTEST_F(FtraceFsOpsTest, GetProcessCommFalse, TestSize.Level1)158 HWTEST_F(FtraceFsOpsTest, GetProcessCommFalse, TestSize.Level1)
159 {
160     std::string content = FtraceFsOps::GetInstance().GetProcessComm(-1);
161     EXPECT_STREQ(content.c_str(), "");
162 }
163 
164 /*
165  * @tc.name: GetThreadCommNormal
166  * @tc.desc: test FtraceFsOps::GetThreadComm with normal case.
167  * @tc.type: FUNC
168  */
HWTEST_F(FtraceFsOpsTest, GetThreadCommNormal, TestSize.Level1)169 HWTEST_F(FtraceFsOpsTest, GetThreadCommNormal, TestSize.Level1)
170 {
171     int32_t pid = getpid();
172     std::string content = FtraceFsOps::GetInstance().GetThreadComm(pid, pid);
173     EXPECT_STRNE(content.c_str(), "");
174 }
175 
176 /*
177  * @tc.name: GetThreadCommFalse
178  * @tc.desc: test FtraceFsOps::GetThreadComm with false case.
179  * @tc.type: FUNC
180  */
HWTEST_F(FtraceFsOpsTest, GetThreadCommFalse, TestSize.Level1)181 HWTEST_F(FtraceFsOpsTest, GetThreadCommFalse, TestSize.Level1)
182 {
183     FtraceFsOps ftraceFsOps;
184     std::string content = ftraceFsOps.GetThreadComm(-1, -1);
185     EXPECT_STREQ(content.c_str(), "");
186 
187     int32_t pid = getpid();
188     content = ftraceFsOps.GetThreadComm(pid, -1);
189     EXPECT_STREQ(content.c_str(), "");
190 
191     content = ftraceFsOps.GetThreadComm(-1, pid);
192     EXPECT_STREQ(content.c_str(), "");
193 }
194 
195 /*
196  * @tc.name: GetSavedCmdLinesNormal
197  * @tc.desc: test FtraceFsOps::GetSavedCmdLines with normal case.
198  * @tc.type: FUNC
199  */
HWTEST_F(FtraceFsOpsTest, GetSavedCmdLinesNormal, TestSize.Level1)200 HWTEST_F(FtraceFsOpsTest, GetSavedCmdLinesNormal, TestSize.Level1)
201 {
202     std::string content = FtraceFsOps::GetInstance().GetSavedCmdLines();
203     EXPECT_STRNE(content.c_str(), "");
204 }
205 
206 /*
207  * @tc.name: GetSavedCmdLinesFalse
208  * @tc.desc: test FtraceFsOps::GetSavedCmdLines with false case.
209  * @tc.type: FUNC
210  */
HWTEST_F(FtraceFsOpsTest, GetSavedCmdLinesFalse, TestSize.Level1)211 HWTEST_F(FtraceFsOpsTest, GetSavedCmdLinesFalse, TestSize.Level1)
212 {
213     FtraceFsOps ftraceFsOps;
214     ftraceFsOps.SetFtraceRoot("");
215     std::string content = ftraceFsOps.GetSavedCmdLines();
216     EXPECT_STREQ(content.c_str(), "");
217 
218     ftraceFsOps.SetFtraceRoot("/test_path");
219     content = ftraceFsOps.GetSavedCmdLines();
220     EXPECT_STREQ(content.c_str(), "");
221 }
222 
223 /*
224  * @tc.name: GetSavedTgidsNormal
225  * @tc.desc: test FtraceFsOps::GetSavedTgids with normal case.
226  * @tc.type: FUNC
227  */
HWTEST_F(FtraceFsOpsTest, GetSavedTgidsNormal, TestSize.Level1)228 HWTEST_F(FtraceFsOpsTest, GetSavedTgidsNormal, TestSize.Level1)
229 {
230     std::string content = FtraceFsOps::GetInstance().GetSavedTgids();
231     EXPECT_STRNE(content.c_str(), "");
232 }
233 
234 /*
235  * @tc.name: GetSavedTgidsFalse
236  * @tc.desc: test FtraceFsOps::GetSavedTgids with false case.
237  * @tc.type: FUNC
238  */
HWTEST_F(FtraceFsOpsTest, GetSavedTgidsFalse, TestSize.Level1)239 HWTEST_F(FtraceFsOpsTest, GetSavedTgidsFalse, TestSize.Level1)
240 {
241     FtraceFsOps ftraceFsOps;
242     ftraceFsOps.SetFtraceRoot("");
243     std::string content = ftraceFsOps.GetSavedTgids();
244     EXPECT_STREQ(content.c_str(), "");
245 
246     ftraceFsOps.SetFtraceRoot("/test_path");
247     content = ftraceFsOps.GetSavedTgids();
248     EXPECT_STREQ(content.c_str(), "");
249 }
250 
251 /*
252  * @tc.name: GetPerCpuStatsNormal
253  * @tc.desc: test FtraceFsOps::GetPerCpuStats with normal case.
254  * @tc.type: FUNC
255  */
HWTEST_F(FtraceFsOpsTest, GetPerCpuStatsNormal, TestSize.Level1)256 HWTEST_F(FtraceFsOpsTest, GetPerCpuStatsNormal, TestSize.Level1)
257 {
258     if (!FtraceFsOps::GetInstance().IsHmKernel()) {
259         std::string content = FtraceFsOps::GetInstance().GetPerCpuStats(0);
260         EXPECT_STRNE(content.c_str(), "");
261     }
262 }
263 
264 /*
265  * @tc.name: GetPerCpuStatsFalse
266  * @tc.desc: test FtraceFsOps::GetPerCpuStats with false case.
267  * @tc.type: FUNC
268  */
HWTEST_F(FtraceFsOpsTest, GetPerCpuStatsFalse, TestSize.Level1)269 HWTEST_F(FtraceFsOpsTest, GetPerCpuStatsFalse, TestSize.Level1)
270 {
271     if (!FtraceFsOps::GetInstance().IsHmKernel()) {
272         FtraceFsOps ftraceFsOps;
273         std::string content = ftraceFsOps.GetPerCpuStats(-1);
274         EXPECT_STREQ(content.c_str(), "");
275 
276         ftraceFsOps.SetFtraceRoot("");
277         content = ftraceFsOps.GetPerCpuStats(0);
278         EXPECT_STREQ(content.c_str(), "");
279 
280         ftraceFsOps.SetFtraceRoot("/test_path");
281         content = ftraceFsOps.GetPerCpuStats(0);
282         EXPECT_STREQ(content.c_str(), "");
283     }
284 }
285 
286 /*
287  * @tc.name: GetRawTracePath
288  * @tc.desc: test FtraceFsOps::GetRawTracePath with normal case.
289  * @tc.type: FUNC
290  */
HWTEST_F(FtraceFsOpsTest, GetRawTracePath, TestSize.Level1)291 HWTEST_F(FtraceFsOpsTest, GetRawTracePath, TestSize.Level1)
292 {
293     std::string content = FtraceFsOps::GetInstance().GetRawTracePath(0);
294     EXPECT_STRNE(content.c_str(), "");
295 }
296 
297 /*
298  * @tc.name: GetPageHeaderFormatNormal
299  * @tc.desc: test FtraceFsOps::GetPageHeaderFormat with normal case.
300  * @tc.type: FUNC
301  */
HWTEST_F(FtraceFsOpsTest, GetPageHeaderFormatNormal, TestSize.Level1)302 HWTEST_F(FtraceFsOpsTest, GetPageHeaderFormatNormal, TestSize.Level1)
303 {
304     if (!FtraceFsOps::GetInstance().IsHmKernel()) {
305         std::string content = FtraceFsOps::GetInstance().GetPageHeaderFormat();
306         EXPECT_STRNE(content.c_str(), "");
307     }
308 }
309 
310 /*
311  * @tc.name: GetPageHeaderFormatFalse
312  * @tc.desc: test FtraceFsOps::GetPageHeaderFormat with false case.
313  * @tc.type: FUNC
314  */
HWTEST_F(FtraceFsOpsTest, GetPageHeaderFormatFalse, TestSize.Level1)315 HWTEST_F(FtraceFsOpsTest, GetPageHeaderFormatFalse, TestSize.Level1)
316 {
317     FtraceFsOps ftraceFsOps;
318     ftraceFsOps.SetFtraceRoot("");
319     std::string content = ftraceFsOps.GetPageHeaderFormat();
320     EXPECT_STREQ(content.c_str(), "");
321 
322     ftraceFsOps.SetFtraceRoot("/test_path");
323     content = ftraceFsOps.GetPageHeaderFormat();
324     EXPECT_STREQ(content.c_str(), "");
325 }
326 
327 /*
328  * @tc.name: GetEventDataFormatNormal
329  * @tc.desc: test FtraceFsOps::GetEventDataFormat with normal case.
330  * @tc.type: FUNC
331  */
HWTEST_F(FtraceFsOpsTest, GetEventDataFormatNormal, TestSize.Level1)332 HWTEST_F(FtraceFsOpsTest, GetEventDataFormatNormal, TestSize.Level1)
333 {
334     std::string content = FtraceFsOps::GetInstance().GetEventDataFormat("irq", "softirq_entry");
335     EXPECT_STRNE(content.c_str(), "");
336 }
337 
338 /*
339  * @tc.name: GetEventDataFormatFalse
340  * @tc.desc: test FtraceFsOps::GetEventDataFormat with false case.
341  * @tc.type: FUNC
342  */
HWTEST_F(FtraceFsOpsTest, GetEventDataFormatFalse, TestSize.Level1)343 HWTEST_F(FtraceFsOpsTest, GetEventDataFormatFalse, TestSize.Level1)
344 {
345     FtraceFsOps ftraceFsOps;
346     std::string content = ftraceFsOps.GetEventDataFormat("test_type", "test_name");
347     EXPECT_STREQ(content.c_str(), "");
348 
349     ftraceFsOps.SetFtraceRoot("");
350     content = ftraceFsOps.GetEventDataFormat("irq", "softirq_entry");
351     EXPECT_STREQ(content.c_str(), "");
352 
353     ftraceFsOps.SetFtraceRoot("/test_path");
354     content = ftraceFsOps.GetEventDataFormat("irq", "softirq_entry");
355     EXPECT_STREQ(content.c_str(), "");
356 }
357 
358 /*
359  * @tc.name: GetPlatformEventsNormal
360  * @tc.desc: test FtraceFsOps::GetPlatformEvents with normal case.
361  * @tc.type: FUNC
362  */
HWTEST_F(FtraceFsOpsTest, GetPlatformEventsNormal, TestSize.Level1)363 HWTEST_F(FtraceFsOpsTest, GetPlatformEventsNormal, TestSize.Level1)
364 {
365     std::vector<std::pair<std::string, std::string>> event = FtraceFsOps::GetInstance().GetPlatformEvents();
366     EXPECT_GT(event.size(), static_cast<size_t>(0));
367 }
368 
369 /*
370  * @tc.name: GetPlatformEventsFalse
371  * @tc.desc: test FtraceFsOps::GetPlatformEvents with false case.
372  * @tc.type: FUNC
373  */
HWTEST_F(FtraceFsOpsTest, GetPlatformEventsFalse, TestSize.Level1)374 HWTEST_F(FtraceFsOpsTest, GetPlatformEventsFalse, TestSize.Level1)
375 {
376     FtraceFsOps ftraceFsOps;
377     ftraceFsOps.SetFtraceRoot("");
378     std::vector<std::pair<std::string, std::string>> event = ftraceFsOps.GetPlatformEvents();
379     EXPECT_EQ(event.size(), static_cast<size_t>(0));
380 
381     ftraceFsOps.SetFtraceRoot("/test_path");
382     event = ftraceFsOps.GetPlatformEvents();
383     EXPECT_EQ(event.size(), static_cast<size_t>(0));
384 }
385 
386 /*
387  * @tc.name: ClearTraceBufferNormal
388  * @tc.desc: test FtraceFsOps::ClearTraceBuffer with normal case.
389  * @tc.type: FUNC
390  */
HWTEST_F(FtraceFsOpsTest, ClearTraceBufferNormal, TestSize.Level1)391 HWTEST_F(FtraceFsOpsTest, ClearTraceBufferNormal, TestSize.Level1)
392 {
393     EXPECT_TRUE(FtraceFsOps::GetInstance().ClearTraceBuffer());
394 }
395 
396 /*
397  * @tc.name: ClearTraceBufferFalse
398  * @tc.desc: test FtraceFsOps::ClearTraceBuffer with false case.
399  * @tc.type: FUNC
400  */
HWTEST_F(FtraceFsOpsTest, ClearTraceBufferFalse, TestSize.Level1)401 HWTEST_F(FtraceFsOpsTest, ClearTraceBufferFalse, TestSize.Level1)
402 {
403     FtraceFsOps ftraceFsOps;
404     ftraceFsOps.SetFtraceRoot("");
405     EXPECT_FALSE(ftraceFsOps.ClearTraceBuffer());
406 
407     ftraceFsOps.SetFtraceRoot("/test_path");
408     EXPECT_FALSE(ftraceFsOps.ClearTraceBuffer());
409 }
410 
411 /*
412  * @tc.name: SetRecordCmdOptionNormal
413  * @tc.desc: test FtraceFsOps::SetRecordCmdOption with normal case.
414  * @tc.type: FUNC
415  */
HWTEST_F(FtraceFsOpsTest, SetRecordCmdOptionNormal, TestSize.Level1)416 HWTEST_F(FtraceFsOpsTest, SetRecordCmdOptionNormal, TestSize.Level1)
417 {
418     if (!FtraceFsOps::GetInstance().IsHmKernel()) {
419         EXPECT_TRUE(FtraceFsOps::GetInstance().SetRecordCmdOption(true));
420         std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/options/record-cmd";
421         std::string content = FileUtils::ReadFile(path);
422         EXPECT_STREQ(content.c_str(), "1\n");
423         EXPECT_TRUE(FtraceFsOps::GetInstance().SetRecordCmdOption(false));
424         content = FileUtils::ReadFile(path);
425         EXPECT_STREQ(content.c_str(), "0\n");
426     }
427 }
428 
429 /*
430  * @tc.name: SetRecordCmdOptionFalse
431  * @tc.desc: test FtraceFsOps::SetRecordCmdOption with false case.
432  * @tc.type: FUNC
433  */
HWTEST_F(FtraceFsOpsTest, SetRecordCmdOptionFalse, TestSize.Level1)434 HWTEST_F(FtraceFsOpsTest, SetRecordCmdOptionFalse, TestSize.Level1)
435 {
436     FtraceFsOps ftraceFsOps;
437     ftraceFsOps.SetFtraceRoot("");
438     EXPECT_FALSE(ftraceFsOps.SetRecordCmdOption(true));
439     EXPECT_FALSE(ftraceFsOps.SetRecordCmdOption(false));
440 
441     ftraceFsOps.SetFtraceRoot("/test_path");
442     EXPECT_FALSE(ftraceFsOps.SetRecordCmdOption(true));
443     EXPECT_FALSE(ftraceFsOps.SetRecordCmdOption(false));
444 }
445 
446 /*
447  * @tc.name: SetRecordTgidOptionNormal
448  * @tc.desc: test FtraceFsOps::SetRecordTgidOption with normal case.
449  * @tc.type: FUNC
450  */
HWTEST_F(FtraceFsOpsTest, SetRecordTgidOptionNormal, TestSize.Level1)451 HWTEST_F(FtraceFsOpsTest, SetRecordTgidOptionNormal, TestSize.Level1)
452 {
453     if (!FtraceFsOps::GetInstance().IsHmKernel()) {
454         EXPECT_TRUE(FtraceFsOps::GetInstance().SetRecordTgidOption(true));
455         std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/options/record-tgid";
456         std::string content = FileUtils::ReadFile(path);
457         EXPECT_STREQ(content.c_str(), "1\n");
458 
459         EXPECT_TRUE(FtraceFsOps::GetInstance().SetRecordTgidOption(false));
460         content = FileUtils::ReadFile(path);
461         EXPECT_STREQ(content.c_str(), "0\n");
462     }
463 }
464 
465 /*
466  * @tc.name: SetRecordTgidOptionFalse
467  * @tc.desc: test FtraceFsOps::SetRecordTgidOption with false case.
468  * @tc.type: FUNC
469  */
HWTEST_F(FtraceFsOpsTest, SetRecordTgidOptionFalse, TestSize.Level1)470 HWTEST_F(FtraceFsOpsTest, SetRecordTgidOptionFalse, TestSize.Level1)
471 {
472     FtraceFsOps ftraceFsOps;
473     ftraceFsOps.SetFtraceRoot("");
474     EXPECT_FALSE(ftraceFsOps.SetRecordTgidOption(true));
475     EXPECT_FALSE(ftraceFsOps.SetRecordTgidOption(false));
476 
477     ftraceFsOps.SetFtraceRoot("/test_path");
478     EXPECT_FALSE(ftraceFsOps.SetRecordTgidOption(true));
479     EXPECT_FALSE(ftraceFsOps.SetRecordTgidOption(false));
480 }
481 
482 /*
483  * @tc.name: SetBufferSizeKbNormal
484  * @tc.desc: test FtraceFsOps::SetBufferSizeKb with normal case.
485  * @tc.type: FUNC
486  */
HWTEST_F(FtraceFsOpsTest, SetBufferSizeKbNormal, TestSize.Level1)487 HWTEST_F(FtraceFsOpsTest, SetBufferSizeKbNormal, TestSize.Level1)
488 {
489     if (!FtraceFsOps::GetInstance().IsHmKernel()) {
490         EXPECT_TRUE(FtraceFsOps::GetInstance().SetBufferSizeKb(4000));
491     } else {
492         EXPECT_TRUE(FtraceFsOps::GetInstance().SetBufferSizeKb(1024));
493     }
494     std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/buffer_size_kb";
495     std::string content = FileUtils::ReadFile(path);
496     if (!FtraceFsOps::GetInstance().IsHmKernel()) {
497         EXPECT_STREQ(content.c_str(), "4000\n");
498     } else {
499         EXPECT_STREQ(content.c_str(), "1024\n");
500     }
501 }
502 
503 /*
504  * @tc.name: SetBufferSizeKbFalse
505  * @tc.desc: test FtraceFsOps::SetBufferSizeKb with false case.
506  * @tc.type: FUNC
507  */
HWTEST_F(FtraceFsOpsTest, SetBufferSizeKbFalse, TestSize.Level1)508 HWTEST_F(FtraceFsOpsTest, SetBufferSizeKbFalse, TestSize.Level1)
509 {
510     FtraceFsOps ftraceFsOps;
511     ftraceFsOps.SetFtraceRoot("");
512     EXPECT_FALSE(ftraceFsOps.SetBufferSizeKb(1024));
513 
514     ftraceFsOps.SetFtraceRoot("/test_path");
515     EXPECT_FALSE(ftraceFsOps.SetBufferSizeKb(1024));
516 }
517 
518 /*
519  * @tc.name: SetTraceClockNormal
520  * @tc.desc: test FtraceFsOps::SetTraceClock with normal case.
521  * @tc.type: FUNC
522  */
HWTEST_F(FtraceFsOpsTest, SetTraceClockNormal, TestSize.Level1)523 HWTEST_F(FtraceFsOpsTest, SetTraceClockNormal, TestSize.Level1)
524 {
525     EXPECT_TRUE(FtraceFsOps::GetInstance().SetTraceClock("boot"));
526     std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/trace_clock";
527     std::string content = FileUtils::ReadFile(path);
528     EXPECT_STRNE(content.c_str(), "");
529 }
530 
531 /*
532  * @tc.name: SetTraceClockFalse
533  * @tc.desc: test FtraceFsOps::SetTraceClock with false case.
534  * @tc.type: FUNC
535  */
HWTEST_F(FtraceFsOpsTest, SetTraceClockFalse, TestSize.Level1)536 HWTEST_F(FtraceFsOpsTest, SetTraceClockFalse, TestSize.Level1)
537 {
538     FtraceFsOps ftraceFsOps;
539     ftraceFsOps.SetFtraceRoot("");
540     EXPECT_FALSE(ftraceFsOps.SetTraceClock("uptime"));
541 
542     ftraceFsOps.SetFtraceRoot("/test_path");
543     EXPECT_FALSE(ftraceFsOps.SetTraceClock("uptime"));
544 }
545 
546 /*
547  * @tc.name: GetTraceClock
548  * @tc.desc: test FtraceFsOps::GetTraceClock with normal case.
549  * @tc.type: FUNC
550  */
HWTEST_F(FtraceFsOpsTest, GetTraceClock, TestSize.Level1)551 HWTEST_F(FtraceFsOpsTest, GetTraceClock, TestSize.Level1)
552 {
553     std::string content = FtraceFsOps::GetInstance().GetTraceClock();
554     EXPECT_STRNE(content.c_str(), "");
555 }
556 
557 /*
558  * @tc.name: AppendSetEventNormal
559  * @tc.desc: test FtraceFsOps::AppendSetEvent with normal case.
560  * @tc.type: FUNC
561  */
HWTEST_F(FtraceFsOpsTest, AppendSetEventNormal, TestSize.Level1)562 HWTEST_F(FtraceFsOpsTest, AppendSetEventNormal, TestSize.Level1)
563 {
564     if (!FtraceFsOps::GetInstance().IsHmKernel()) {
565         EXPECT_TRUE(FtraceFsOps::GetInstance().ClearSetEvent());
566         EXPECT_TRUE(FtraceFsOps::GetInstance().AppendSetEvent("sched", "sched_switch"));
567     }
568     std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/set_event";
569     std::string content = FileUtils::ReadFile(path);
570     EXPECT_STRNE(content.c_str(), "");
571 }
572 
573 /*
574  * @tc.name: AppendSetEventFalse
575  * @tc.desc: test FtraceFsOps::AppendSetEvent with false case.
576  * @tc.type: FUNC
577  */
HWTEST_F(FtraceFsOpsTest, AppendSetEventFalse, TestSize.Level1)578 HWTEST_F(FtraceFsOpsTest, AppendSetEventFalse, TestSize.Level1)
579 {
580     FtraceFsOps ftraceFsOps;
581     ftraceFsOps.SetFtraceRoot("");
582     EXPECT_FALSE(ftraceFsOps.AppendSetEvent("sched", "sched_switch"));
583 
584     ftraceFsOps.SetFtraceRoot("/test_path");
585     EXPECT_FALSE(ftraceFsOps.AppendSetEvent("sched", "sched_switch"));
586 }
587 
588 /*
589  * @tc.name: EnableEventNormal
590  * @tc.desc: test FtraceFsOps::EnableEvent with normal case.
591  * @tc.type: FUNC
592  */
HWTEST_F(FtraceFsOpsTest, EnableEventNormal, TestSize.Level1)593 HWTEST_F(FtraceFsOpsTest, EnableEventNormal, TestSize.Level1)
594 {
595     EXPECT_TRUE(FtraceFsOps::GetInstance().EnableEvent("sched", "sched_switch"));
596     std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/events/sched/sched_switch/enable";
597     std::string content = FileUtils::ReadFile(path);
598     EXPECT_STREQ(content.c_str(), "1\n");
599 }
600 
601 /*
602  * @tc.name: EnableEventFalse
603  * @tc.desc: test FtraceFsOps::EnableEvent with false case.
604  * @tc.type: FUNC
605  */
HWTEST_F(FtraceFsOpsTest, EnableEventFalse, TestSize.Level1)606 HWTEST_F(FtraceFsOpsTest, EnableEventFalse, TestSize.Level1)
607 {
608     FtraceFsOps ftraceFsOps;
609     ftraceFsOps.SetFtraceRoot("");
610     EXPECT_FALSE(ftraceFsOps.EnableEvent("sched", "sched_switch"));
611 
612     ftraceFsOps.SetFtraceRoot("/test_path");
613     EXPECT_FALSE(ftraceFsOps.EnableEvent("sched", "sched_switch"));
614 }
615 
616 /*
617  * @tc.name: DisableEventNormal
618  * @tc.desc: test FtraceFsOps::DisableEvent with normal case.
619  * @tc.type: FUNC
620  */
HWTEST_F(FtraceFsOpsTest, DisableEventNormal, TestSize.Level1)621 HWTEST_F(FtraceFsOpsTest, DisableEventNormal, TestSize.Level1)
622 {
623     EXPECT_TRUE(FtraceFsOps::GetInstance().DisableEvent("sched", "sched_switch"));
624     std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/events/sched/sched_switch/enable";
625     std::string content = FileUtils::ReadFile(path);
626     EXPECT_STREQ(content.c_str(), "0\n");
627 }
628 
629 /*
630  * @tc.name: DisableEventFalse
631  * @tc.desc: test FtraceFsOps::DisableEvent with false case.
632  * @tc.type: FUNC
633  */
HWTEST_F(FtraceFsOpsTest, DisableEventFalse, TestSize.Level1)634 HWTEST_F(FtraceFsOpsTest, DisableEventFalse, TestSize.Level1)
635 {
636     FtraceFsOps ftraceFsOps;
637     ftraceFsOps.SetFtraceRoot("");
638     EXPECT_FALSE(ftraceFsOps.DisableEvent("sched", "sched_switch"));
639 
640     ftraceFsOps.SetFtraceRoot("/test_path");
641     EXPECT_FALSE(ftraceFsOps.DisableEvent("sched", "sched_switch"));
642 }
643 
644 /*
645  * @tc.name: EnableTracingNormal
646  * @tc.desc: test FtraceFsOps::EnableTracing with normal case.
647  * @tc.type: FUNC
648  */
HWTEST_F(FtraceFsOpsTest, EnableTracingNormal, TestSize.Level1)649 HWTEST_F(FtraceFsOpsTest, EnableTracingNormal, TestSize.Level1)
650 {
651     EXPECT_TRUE(FtraceFsOps::GetInstance().EnableTracing());
652     std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/tracing_on";
653     std::string content = FileUtils::ReadFile(path);
654     EXPECT_STREQ(content.c_str(), "1\n");
655 }
656 
657 /*
658  * @tc.name: EnableTracingFalse
659  * @tc.desc: test FtraceFsOps::EnableTracing with false case.
660  * @tc.type: FUNC
661  */
HWTEST_F(FtraceFsOpsTest, EnableTracingFalse, TestSize.Level1)662 HWTEST_F(FtraceFsOpsTest, EnableTracingFalse, TestSize.Level1)
663 {
664     FtraceFsOps ftraceFsOps;
665     ftraceFsOps.SetFtraceRoot("");
666     EXPECT_FALSE(ftraceFsOps.EnableTracing());
667 
668     ftraceFsOps.SetFtraceRoot("/test_path");
669     EXPECT_FALSE(ftraceFsOps.EnableTracing());
670 }
671 
672 /*
673  * @tc.name: DisableTracingNormal
674  * @tc.desc: test FtraceFsOps::DisableTracing with normal case.
675  * @tc.type: FUNC
676  */
HWTEST_F(FtraceFsOpsTest, DisableTracingNormal, TestSize.Level1)677 HWTEST_F(FtraceFsOpsTest, DisableTracingNormal, TestSize.Level1)
678 {
679     EXPECT_TRUE(FtraceFsOps::GetInstance().DisableTracing());
680     std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/tracing_on";
681     std::string content = FileUtils::ReadFile(path);
682     EXPECT_STREQ(content.c_str(), "0\n");
683 }
684 
685 /*
686  * @tc.name: DisableTracingFalse
687  * @tc.desc: test FtraceFsOps::DisableTracing with false case.
688  * @tc.type: FUNC
689  */
HWTEST_F(FtraceFsOpsTest, DisableTracingFalse, TestSize.Level1)690 HWTEST_F(FtraceFsOpsTest, DisableTracingFalse, TestSize.Level1)
691 {
692     FtraceFsOps ftraceFsOps;
693     ftraceFsOps.SetFtraceRoot("");
694     EXPECT_FALSE(ftraceFsOps.DisableTracing());
695 
696     ftraceFsOps.SetFtraceRoot("/test_path");
697     EXPECT_FALSE(ftraceFsOps.DisableTracing());
698 }
699 } // namespace