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
SetUp()69 void SetUp() override {}
TearDown()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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
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 */
487 HWTEST_F(FtraceFsOpsTest, SetBufferSizeKbNormal, TestSize.Level1)
488 {
489 EXPECT_TRUE(FtraceFsOps::GetInstance().SetBufferSizeKb(1024));
490 std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/buffer_size_kb";
491 std::string content = FileUtils::ReadFile(path);
492 EXPECT_STREQ(content.c_str(), "1024\n");
493 }
494
495 /*
496 * @tc.name: SetBufferSizeKbFalse
497 * @tc.desc: test FtraceFsOps::SetBufferSizeKb with false case.
498 * @tc.type: FUNC
499 */
500 HWTEST_F(FtraceFsOpsTest, SetBufferSizeKbFalse, TestSize.Level1)
501 {
502 FtraceFsOps ftraceFsOps;
503 ftraceFsOps.SetFtraceRoot("");
504 EXPECT_FALSE(ftraceFsOps.SetBufferSizeKb(1024));
505
506 ftraceFsOps.SetFtraceRoot("/test_path");
507 EXPECT_FALSE(ftraceFsOps.SetBufferSizeKb(1024));
508 }
509
510 /*
511 * @tc.name: SetTraceClockNormal
512 * @tc.desc: test FtraceFsOps::SetTraceClock with normal case.
513 * @tc.type: FUNC
514 */
515 HWTEST_F(FtraceFsOpsTest, SetTraceClockNormal, TestSize.Level1)
516 {
517 EXPECT_TRUE(FtraceFsOps::GetInstance().SetTraceClock("boot"));
518 std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/trace_clock";
519 std::string content = FileUtils::ReadFile(path);
520 EXPECT_STRNE(content.c_str(), "");
521 }
522
523 /*
524 * @tc.name: SetTraceClockFalse
525 * @tc.desc: test FtraceFsOps::SetTraceClock with false case.
526 * @tc.type: FUNC
527 */
528 HWTEST_F(FtraceFsOpsTest, SetTraceClockFalse, TestSize.Level1)
529 {
530 FtraceFsOps ftraceFsOps;
531 ftraceFsOps.SetFtraceRoot("");
532 EXPECT_FALSE(ftraceFsOps.SetTraceClock("uptime"));
533
534 ftraceFsOps.SetFtraceRoot("/test_path");
535 EXPECT_FALSE(ftraceFsOps.SetTraceClock("uptime"));
536 }
537
538 /*
539 * @tc.name: GetTraceClock
540 * @tc.desc: test FtraceFsOps::GetTraceClock with normal case.
541 * @tc.type: FUNC
542 */
543 HWTEST_F(FtraceFsOpsTest, GetTraceClock, TestSize.Level1)
544 {
545 std::string content = FtraceFsOps::GetInstance().GetTraceClock();
546 EXPECT_STRNE(content.c_str(), "");
547 }
548
549 /*
550 * @tc.name: AppendSetEventNormal
551 * @tc.desc: test FtraceFsOps::AppendSetEvent with normal case.
552 * @tc.type: FUNC
553 */
554 HWTEST_F(FtraceFsOpsTest, AppendSetEventNormal, TestSize.Level1)
555 {
556 if (!FtraceFsOps::GetInstance().IsHmKernel()) {
557 EXPECT_TRUE(FtraceFsOps::GetInstance().ClearSetEvent());
558 EXPECT_TRUE(FtraceFsOps::GetInstance().AppendSetEvent("sched", "sched_switch"));
559 }
560 std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/set_event";
561 std::string content = FileUtils::ReadFile(path);
562 EXPECT_STRNE(content.c_str(), "");
563 }
564
565 /*
566 * @tc.name: AppendSetEventFalse
567 * @tc.desc: test FtraceFsOps::AppendSetEvent with false case.
568 * @tc.type: FUNC
569 */
570 HWTEST_F(FtraceFsOpsTest, AppendSetEventFalse, TestSize.Level1)
571 {
572 FtraceFsOps ftraceFsOps;
573 ftraceFsOps.SetFtraceRoot("");
574 EXPECT_FALSE(ftraceFsOps.AppendSetEvent("sched", "sched_switch"));
575
576 ftraceFsOps.SetFtraceRoot("/test_path");
577 EXPECT_FALSE(ftraceFsOps.AppendSetEvent("sched", "sched_switch"));
578 }
579
580 /*
581 * @tc.name: EnableEventNormal
582 * @tc.desc: test FtraceFsOps::EnableEvent with normal case.
583 * @tc.type: FUNC
584 */
585 HWTEST_F(FtraceFsOpsTest, EnableEventNormal, TestSize.Level1)
586 {
587 EXPECT_TRUE(FtraceFsOps::GetInstance().EnableEvent("sched", "sched_switch"));
588 std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/events/sched/sched_switch/enable";
589 std::string content = FileUtils::ReadFile(path);
590 EXPECT_STREQ(content.c_str(), "1\n");
591 }
592
593 /*
594 * @tc.name: EnableEventFalse
595 * @tc.desc: test FtraceFsOps::EnableEvent with false case.
596 * @tc.type: FUNC
597 */
598 HWTEST_F(FtraceFsOpsTest, EnableEventFalse, TestSize.Level1)
599 {
600 FtraceFsOps ftraceFsOps;
601 ftraceFsOps.SetFtraceRoot("");
602 EXPECT_FALSE(ftraceFsOps.EnableEvent("sched", "sched_switch"));
603
604 ftraceFsOps.SetFtraceRoot("/test_path");
605 EXPECT_FALSE(ftraceFsOps.EnableEvent("sched", "sched_switch"));
606 }
607
608 /*
609 * @tc.name: DisableEventNormal
610 * @tc.desc: test FtraceFsOps::DisableEvent with normal case.
611 * @tc.type: FUNC
612 */
613 HWTEST_F(FtraceFsOpsTest, DisableEventNormal, TestSize.Level1)
614 {
615 EXPECT_TRUE(FtraceFsOps::GetInstance().DisableEvent("sched", "sched_switch"));
616 std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/events/sched/sched_switch/enable";
617 std::string content = FileUtils::ReadFile(path);
618 EXPECT_STREQ(content.c_str(), "0\n");
619 }
620
621 /*
622 * @tc.name: DisableEventFalse
623 * @tc.desc: test FtraceFsOps::DisableEvent with false case.
624 * @tc.type: FUNC
625 */
626 HWTEST_F(FtraceFsOpsTest, DisableEventFalse, TestSize.Level1)
627 {
628 FtraceFsOps ftraceFsOps;
629 ftraceFsOps.SetFtraceRoot("");
630 EXPECT_FALSE(ftraceFsOps.DisableEvent("sched", "sched_switch"));
631
632 ftraceFsOps.SetFtraceRoot("/test_path");
633 EXPECT_FALSE(ftraceFsOps.DisableEvent("sched", "sched_switch"));
634 }
635
636 /*
637 * @tc.name: EnableTracingNormal
638 * @tc.desc: test FtraceFsOps::EnableTracing with normal case.
639 * @tc.type: FUNC
640 */
641 HWTEST_F(FtraceFsOpsTest, EnableTracingNormal, TestSize.Level1)
642 {
643 EXPECT_TRUE(FtraceFsOps::GetInstance().EnableTracing());
644 std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/tracing_on";
645 std::string content = FileUtils::ReadFile(path);
646 EXPECT_STREQ(content.c_str(), "1\n");
647 }
648
649 /*
650 * @tc.name: EnableTracingFalse
651 * @tc.desc: test FtraceFsOps::EnableTracing with false case.
652 * @tc.type: FUNC
653 */
654 HWTEST_F(FtraceFsOpsTest, EnableTracingFalse, TestSize.Level1)
655 {
656 FtraceFsOps ftraceFsOps;
657 ftraceFsOps.SetFtraceRoot("");
658 EXPECT_FALSE(ftraceFsOps.EnableTracing());
659
660 ftraceFsOps.SetFtraceRoot("/test_path");
661 EXPECT_FALSE(ftraceFsOps.EnableTracing());
662 }
663
664 /*
665 * @tc.name: DisableTracingNormal
666 * @tc.desc: test FtraceFsOps::DisableTracing with normal case.
667 * @tc.type: FUNC
668 */
669 HWTEST_F(FtraceFsOpsTest, DisableTracingNormal, TestSize.Level1)
670 {
671 EXPECT_TRUE(FtraceFsOps::GetInstance().DisableTracing());
672 std::string path = FtraceFsOps::GetInstance().ftraceRoot_ + "/tracing_on";
673 std::string content = FileUtils::ReadFile(path);
674 EXPECT_STREQ(content.c_str(), "0\n");
675 }
676
677 /*
678 * @tc.name: DisableTracingFalse
679 * @tc.desc: test FtraceFsOps::DisableTracing with false case.
680 * @tc.type: FUNC
681 */
682 HWTEST_F(FtraceFsOpsTest, DisableTracingFalse, TestSize.Level1)
683 {
684 FtraceFsOps ftraceFsOps;
685 ftraceFsOps.SetFtraceRoot("");
686 EXPECT_FALSE(ftraceFsOps.DisableTracing());
687
688 ftraceFsOps.SetFtraceRoot("/test_path");
689 EXPECT_FALSE(ftraceFsOps.DisableTracing());
690 }
691 } // namespace