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 "hitrace_dump.h"
17 #include "common_utils.h"
18 
19 #include <iostream>
20 #include <memory>
21 #include <fstream>
22 #include <string>
23 #include <vector>
24 #include <map>
25 
26 #include <unistd.h>
27 #include <cstdio>
28 #include <fcntl.h>
29 #include "securec.h"
30 
31 #include "hilog/log.h"
32 #include "parameters.h"
33 #include <gtest/gtest.h>
34 
35 using namespace OHOS::HiviewDFX::Hitrace;
36 using namespace testing::ext;
37 using OHOS::HiviewDFX::HiLog;
38 
39 namespace {
40 
41 #undef LOG_DOMAIN
42 #define LOG_DOMAIN 0xD002D33
43 
44 #undef LOG_TAG
45 #define LOG_TAG "HitraceTest"
46 
47 const std::string TAG_PROP = "debug.hitrace.tags.enableflags";
48 const std::string DEFAULT_OUTPUT_DIR = "/data/log/hitrace/";
49 const std::string LOG_DIR = "/data/log/";
50 const int SLEEP_TIME = 10; // sleep 10ms
51 
52 std::string g_traceRootPath;
53 
AddPair2Table(std::string outputFileName,int nowSec)54 void AddPair2Table(std::string outputFileName, int nowSec)
55 {
56     std::vector<std::pair<std::string, int>> traceFilesTable = GetTraceFilesTable();
57     traceFilesTable.push_back({outputFileName, nowSec});
58     SetTraceFilesTable(traceFilesTable);
59 }
60 
CreateFile(std::string outputFileName)61 bool CreateFile(std::string outputFileName)
62 {
63     std::ofstream ofs;
64     ofs.open(outputFileName, std::ios::out | std::ios::trunc);
65     bool openRes = ofs.is_open();
66     ofs.close();
67     return openRes;
68 }
69 
EraseFile(std::string outputFileName)70 void EraseFile(std::string outputFileName)
71 {
72     std::vector<std::pair<std::string, int>> traceFilesTable = GetTraceFilesTable();
73     for (auto iter = traceFilesTable.begin(); iter != traceFilesTable.end();) {
74         if (strcmp(iter->first.c_str(), outputFileName.c_str())) {
75             iter = traceFilesTable.erase(iter);
76             continue;
77         }
78         iter++;
79     }
80     SetTraceFilesTable(traceFilesTable);
81 }
82 
TraverseFiles(std::vector<std::string> files,std::string outputFileName)83 bool TraverseFiles(std::vector<std::string> files, std::string outputFileName)
84 {
85     int i = 1;
86     bool isExists = false;
87     for (std::vector<std::string>::iterator iter = files.begin(); iter != files.end(); iter++) {
88         isExists |= (strcmp(iter->c_str(), outputFileName.c_str()) == 0);
89         HILOG_INFO(LOG_CORE, "ret.outputFile%{public}d: %{public}s", i++, iter->c_str());
90     }
91     return isExists;
92 }
93 
RunCmd(const string & cmdstr)94 bool RunCmd(const string& cmdstr)
95 {
96     FILE *fp = popen(cmdstr.c_str(), "r");
97     if (fp == nullptr) {
98         return false;
99     }
100     pclose(fp);
101     return true;
102 }
103 
104 class HitraceDumpTest : public testing::Test {
105 public:
106     static void SetUpTestCase(void);
107     static void TearDownTestCase(void);
108     void SetUp();
TearDown()109     void TearDown() {}
110 };
111 
SetUp()112 void HitraceDumpTest::SetUp()
113 {
114     CloseTrace();
115 }
116 
SetUpTestCase()117 void HitraceDumpTest::SetUpTestCase()
118 {
119     const std::string debugfsDir = "/sys/kernel/debug/tracing/";
120     const std::string tracefsDir = "/sys/kernel/tracing/";
121     if (access((debugfsDir + "trace_marker").c_str(), F_OK) != -1) {
122         g_traceRootPath = debugfsDir;
123     } else if (access((tracefsDir + "trace_marker").c_str(), F_OK) != -1) {
124         g_traceRootPath = tracefsDir;
125     } else {
126         HILOG_ERROR(LOG_CORE, "Error: Finding trace folder failed.");
127     }
128 
129     /* Open CMD_MODE */
130 }
131 
TearDownTestCase()132 void HitraceDumpTest::TearDownTestCase()
133 {
134     /* Close CMD_MODE */
135 }
136 
137 /**
138  * @tc.name: GetTraceModeTest_001
139  * @tc.desc: test GetTraceMode() service mode
140  * @tc.type: FUNC
141  */
142 HWTEST_F(HitraceDumpTest, GetTraceModeTest_001, TestSize.Level0)
143 {
144     // check service mode
145     const std::vector<std::string> tagGroups = {"scene_performance"};
146     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
147     TraceMode serviceMode = GetTraceMode();
148     ASSERT_TRUE(serviceMode == TraceMode::SERVICE_MODE);
149 
150     // check close mode
151     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
152     TraceMode closeMode = GetTraceMode();
153     ASSERT_TRUE(closeMode == TraceMode::CLOSE);
154 }
155 
156 /**
157  * @tc.name: GetTraceModeTest_002
158  * @tc.desc: test GetTraceMode() cmd mode
159  * @tc.type: FUNC
160  */
161 HWTEST_F(HitraceDumpTest, GetTraceModeTest_002, TestSize.Level0)
162 {
163     // check cmd mode
164     std::string args = "tags:sched clockType:boot bufferSize:1024 overwrite:1";
165     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
166     TraceMode cmdMode = GetTraceMode();
167     ASSERT_TRUE(cmdMode == TraceMode::CMD_MODE);
168 
169     // check close mode
170     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
171     TraceMode closeMode = GetTraceMode();
172     ASSERT_TRUE(closeMode == TraceMode::CLOSE);
173 }
174 
175 /**
176  * @tc.name: DumpForServiceMode_001
177  * @tc.desc: The correct usage of grasping trace in SERVICE_MODE.
178  * @tc.type: FUNC
179  */
180 HWTEST_F(HitraceDumpTest, DumpForServiceMode_001, TestSize.Level0)
181 {
182     const std::vector<std::string> tagGroups = {"scene_performance"};
183     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
184 
185     TraceRetInfo ret = DumpTrace();
186     ASSERT_TRUE(ret.errorCode == TraceErrorCode::SUCCESS);
187     ASSERT_TRUE(ret.outputFiles.size() > 0);
188     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
189 }
190 
191 /**
192  * @tc.name: DumpForCmdMode_001
193  * @tc.desc: The correct usage of grasping trace in CMD_MODE.
194  * @tc.type: FUNC
195  */
196 HWTEST_F(HitraceDumpTest, DumpForCmdMode_001, TestSize.Level0)
197 {
198     std::string args = "tags:sched clockType:boot bufferSize:1024 overwrite:1";
199     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
200 
201     ASSERT_TRUE(DumpTraceOn() == TraceErrorCode::SUCCESS);
202     sleep(1);
203 
204     TraceRetInfo ret = DumpTraceOff();
205     ASSERT_TRUE(ret.errorCode == TraceErrorCode::SUCCESS);
206     ASSERT_TRUE(ret.outputFiles.size() > 0);
207 
208     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
209 }
210 
211 /**
212  * @tc.name: DumpForCmdMode_002
213  * @tc.desc: Specifies the path of the command in CMD_MODE.
214  * @tc.type: FUNC
215  */
216 HWTEST_F(HitraceDumpTest, DumpForCmdMode_002, TestSize.Level0)
217 {
218     std::string filePathName = "/data/local/tmp/mytrace.sys";
219     std::string args = "tags:sched clockType:boot bufferSize:1024 overwrite:1 output:" + filePathName;
220     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
221     ASSERT_TRUE(DumpTraceOn() == TraceErrorCode::SUCCESS);
222     sleep(1);
223 
224     TraceRetInfo ret = DumpTraceOff();
225     ASSERT_TRUE(ret.errorCode == TraceErrorCode::SUCCESS);
226 
227     ASSERT_TRUE(TraverseFiles(ret.outputFiles, filePathName))
228         << "unspport set outputfile, default generate file in /data/log/hitrace.";
229 
230     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
231 }
232 
233 /**
234  * @tc.name: DumpForCmdMode_003
235  * @tc.desc: Invalid args verification in CMD_MODE.
236  * @tc.type: FUNC
237  */
238 HWTEST_F(HitraceDumpTest, DumpForCmdMode_003, TestSize.Level0)
239 {
240     std::string args = "clockType:boot bufferSize:1024 overwrite:1 ";
241     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
242     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
243 
244     args = "tags:hdc clockType:boot bufferSize:1024 overwrite:1 descriptions:123";
245     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::TAG_ERROR);
246 
247     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
248 }
249 
250 /**
251  * @tc.name: DumpForCmdMode_004
252  * @tc.desc: The CMD_MODE cannot be interrupted by the SERVICE_MODE.
253  * @tc.type: FUNC
254  */
255 HWTEST_F(HitraceDumpTest, DumpForCmdMode_004, TestSize.Level0)
256 {
257     std::string args = "tags:memory clockType:boot1 bufferSize:1024 overwrite:0";
258     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
259 
260     const std::vector<std::string> tagGroups = {"scene_performance"};
261     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::CALL_ERROR);
262 
263     ASSERT_TRUE(DumpTraceOn() == TraceErrorCode::SUCCESS);
264     sleep(1);
265 
266     TraceRetInfo ret = DumpTraceOff();
267     ASSERT_TRUE(ret.errorCode == TraceErrorCode::SUCCESS);
268     ASSERT_TRUE(ret.outputFiles.size() > 0);
269 
270     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
271 
272     args = "tags:memory clockType: bufferSize:1024 overwrite:1";
273     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
274     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
275 
276     args = "tags:memory clockType:perf bufferSize:1024 overwrite:1";
277     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
278     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
279 }
280 
281 /**
282  * @tc.name: DumpForCmdMode_005
283  * @tc.desc: Enable the cmd mode in non-close mode.
284  * @tc.type: FUNC
285  */
286 HWTEST_F(HitraceDumpTest, DumpForCmdMode_005, TestSize.Level0)
287 {
288     const std::vector<std::string> tagGroups = {"scene_performance"};
289     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
290     std::string args = "tags:sched clockType:boot bufferSize:1024 overwrite:1";
291     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::CALL_ERROR);
292     ASSERT_TRUE(DumpTraceOn() == TraceErrorCode::CALL_ERROR);
293 
294     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
295 }
296 
297 /**
298  * @tc.name: DumpForCmdMode_006
299  * @tc.desc: Test the CMD_MODE when there's extra space in args.
300  * @tc.type: FUNC
301  */
302 HWTEST_F(HitraceDumpTest, DumpForCmdMode_006, TestSize.Level0)
303 {
304     std::string args = "tags: sched clockType: boot bufferSize:1024 overwrite: 1";
305     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
306 
307     ASSERT_TRUE(DumpTraceOn() == TraceErrorCode::SUCCESS);
308     sleep(1);
309 
310     TraceRetInfo ret = DumpTraceOff();
311     ASSERT_TRUE(ret.errorCode == TraceErrorCode::SUCCESS);
312     ASSERT_TRUE(ret.outputFiles.size() > 0);
313 
314     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
315 }
316 
317 /**
318  * @tc.name: DumpForCmdMode_007
319  * @tc.desc: Test the CMD_MODE when set fileLimit in args.
320  * @tc.type: FUNC
321  */
322 HWTEST_F(HitraceDumpTest, DumpForCmdMode_007, TestSize.Level0)
323 {
324     std::string args = "tags:sched,ace,app,disk,distributeddatamgr,freq,graphic,idle,irq,load,mdfs,mmc,";
325     args += "notification,ohos,pagecache,regulators,sync,ufs,workq,zaudio,zcamera,zimage,zmedia ";
326     args += "clockType: boot bufferSize:1024 overwrite: 1 fileLimit: 2 fileSize: 51200";
327     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
328 
329     ASSERT_TRUE(DumpTraceOn() == TraceErrorCode::SUCCESS);
330     sleep(1);
331 
332     TraceRetInfo ret = DumpTraceOff();
333     ASSERT_TRUE(ret.errorCode == TraceErrorCode::SUCCESS);
334     ASSERT_TRUE(ret.outputFiles.size() > 0);
335 
336     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
337 }
338 
339 /**
340  * @tc.name: DumpForCmdMode_008
341  * @tc.desc: Test the CMD_MODE when there's extra space in args.
342  * @tc.type: FUNC
343  */
344 HWTEST_F(HitraceDumpTest, DumpForCmdMode_008, TestSize.Level0)
345 {
346     std::string filePathName = "/data/local/tmp/mylongtrace.sys";
347     std::string args = "tags:sched,ace,app,disk,distributeddatamgr,freq,graphic,idle,irq,load,mdfs,mmc,";
348     args += "notification,ohos,pagecache,regulators,sync,ufs,workq,zaudio,zcamera,zimage,zmedia ";
349     args += "clockType: boot bufferSize:1024 overwrite: 1 output:" + filePathName;
350     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
351 
352     ASSERT_TRUE(DumpTraceOn() == TraceErrorCode::SUCCESS);
353     if (remove(filePathName.c_str()) == 0) {
354         HILOG_INFO(LOG_CORE, "Delete mylongtrace.sys success.");
355     } else {
356         HILOG_ERROR(LOG_CORE, "Delete mylongtrace.sys failed.");
357     }
358     sleep(SLEEP_TIME);
359 
360     TraceRetInfo ret = DumpTraceOff();
361     ASSERT_TRUE(ret.errorCode == TraceErrorCode::SUCCESS);
362     ASSERT_TRUE(ret.outputFiles.size() > 0);
363 
364     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
365 }
366 
367 /**
368  * @tc.name: DumpForCmdMode_009
369  * @tc.desc: Test the CMD_MODE when set fileLimit in args.
370  * @tc.type: FUNC
371  */
372 HWTEST_F(HitraceDumpTest, DumpForCmdMode_009, TestSize.Level0)
373 {
374     std::string args = "tags:sched,ace,app,disk,distributeddatamgr,freq,graphic,idle,irq,load,mdfs,mmc,";
375     args += "notification,ohos,pagecache,regulators,sync,ufs,workq,zaudio,zcamera,zimage,zmedia ";
376     args += "clockType: boot bufferSize:1024 overwrite: 1 fileLimit: 2";
377     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
378 
379     ASSERT_TRUE(DumpTraceOn() == TraceErrorCode::SUCCESS);
380     sleep(1);
381 
382     TraceRetInfo ret = DumpTraceOff();
383     ASSERT_TRUE(ret.errorCode == TraceErrorCode::SUCCESS);
384     ASSERT_TRUE(ret.outputFiles.size() > 0);
385 
386     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
387 }
388 
389 /**
390  * @tc.name: ParammeterCheck_001
391  * @tc.desc: Check parameter after interface call.
392  * @tc.type: FUNC
393  */
394 HWTEST_F(HitraceDumpTest, ParammeterCheck_001, TestSize.Level0)
395 {
396     const std::vector<std::string> tagGroups = {"scene_performance"};
397     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
398 
399     // ckeck Property("debug.hitrace.tags.enableflags")
400     uint64_t openTags = OHOS::system::GetUintParameter<uint64_t>(TAG_PROP, 0);
401     ASSERT_TRUE(openTags > 0);
402 
403     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
404 
405     // ckeck Property("debug.hitrace.tags.enableflags")
406     uint64_t closeTags = OHOS::system::GetUintParameter<uint64_t>(TAG_PROP, 0);
407     ASSERT_TRUE(closeTags == 0);
408 }
409 
410 /**
411  * @tc.name: DumpForServiceMode_002
412  * @tc.desc: Verify if files can be returned as expected in Service_MODE.
413  * @tc.type: FUNC
414  */
415 HWTEST_F(HitraceDumpTest, DumpForServiceMode_002, TestSize.Level0)
416 {
417     const std::vector<std::string> tagGroups = {"scene_performance"};
418     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
419     ASSERT_TRUE(access(DEFAULT_OUTPUT_DIR.c_str(), F_OK) == 0) << "/data/log/hitrace not exists.";
420 
421     struct timeval now = {0, 0};
422     gettimeofday(&now, nullptr);
423     int nowSec = now.tv_sec;
424     int nowUsec = now.tv_usec;
425     nowSec--;
426     std::string outputFileName = DEFAULT_OUTPUT_DIR + "trace_" + std::to_string(nowSec)
427         + "_" + std::to_string(nowUsec) + ".sys";
428     ASSERT_TRUE(CreateFile(outputFileName)) << "create log file failed.";
429     HILOG_INFO(LOG_CORE, "outputFileName: %{public}s", outputFileName.c_str());
430     AddPair2Table(outputFileName, nowSec);
431 
432     TraceRetInfo ret = DumpTrace();
433     // Remove outputFileName in g_hitraceFilesTable
434     EraseFile(outputFileName);
435     ASSERT_TRUE(ret.errorCode == TraceErrorCode::SUCCESS);
436     ASSERT_TRUE(TraverseFiles(ret.outputFiles, outputFileName)) << "file created by user is not exists.";
437     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
438 }
439 
440 /**
441  * @tc.name: DumpForServiceMode_003
442  * @tc.desc: Verify if files can be deleted in Service_MODE.
443  * @tc.type: FUNC
444  */
445 HWTEST_F(HitraceDumpTest, DumpForServiceMode_003, TestSize.Level0)
446 {
447     const std::vector<std::string> tagGroups = {"scene_performance"};
448     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
449     ASSERT_TRUE(access(DEFAULT_OUTPUT_DIR.c_str(), F_OK) == 0) << "/data/log/hitrace not exists.";
450 
451     struct timeval now = {0, 0};
452     gettimeofday(&now, nullptr);
453     int nowSec = now.tv_sec;
454     int nowUsec = now.tv_usec;
455     nowSec = nowSec - 1900;
456     std::string outputFileName = DEFAULT_OUTPUT_DIR + "trace_" + std::to_string(nowSec)
457         + "_" + std::to_string(nowUsec) + ".sys";
458     ASSERT_TRUE(CreateFile(outputFileName)) << "create log file failed.";
459     HILOG_INFO(LOG_CORE, "outputFileName: %{public}s", outputFileName.c_str());
460     AddPair2Table(outputFileName, nowSec);
461 
462     TraceRetInfo ret = DumpTrace();
463     ASSERT_TRUE(ret.errorCode == TraceErrorCode::SUCCESS);
464     ASSERT_FALSE(TraverseFiles(ret.outputFiles, outputFileName))
465         << "Returned files that should have been deleted half an hour ago.";
466     ASSERT_TRUE(access(outputFileName.c_str(), F_OK) < 0) << "The file was not deleted half an hour ago";
467     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
468 }
469 
470 /**
471  * @tc.name: DumpForServiceMode_004
472  * @tc.desc: Invalid parameter verification in CMD_MODE.
473  * @tc.type: FUNC
474  */
475 HWTEST_F(HitraceDumpTest, DumpForServiceMode_004, TestSize.Level0)
476 {
477     const std::vector<std::string> tagGroups;
478     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::TAG_ERROR);
479 
480     const std::vector<std::string> tagGroups1 = {"scene_performance1"};
481     ASSERT_TRUE(OpenTrace(tagGroups1) == TraceErrorCode::TAG_ERROR);
482     ASSERT_TRUE(DumpTrace().errorCode == TraceErrorCode::CALL_ERROR);
483 
484     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
485 }
486 
487 /**
488  * @tc.name: DumpForServiceMode_005
489  * @tc.desc: Enable the service mode in CMD_MODE.
490  * @tc.type: FUNC
491  */
492 HWTEST_F(HitraceDumpTest, DumpForServiceMode_005, TestSize.Level0)
493 {
494     std::string args = "tags:sched clockType:boot bufferSize:1024 overwrite:1";
495     ASSERT_TRUE(OpenTrace(args) == TraceErrorCode::SUCCESS);
496 
497     const std::vector<std::string> tagGroups = {"scene_performance"};
498     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::CALL_ERROR);
499 
500     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
501 
502     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
503 
504     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::CALL_ERROR);
505 
506     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
507 }
508 
509 /**
510  * @tc.name: DumpForServiceMode_006
511  * @tc.desc: Invalid parameter verification in CMD_MODE.
512  * @tc.type: FUNC
513  */
514 HWTEST_F(HitraceDumpTest, DumpForServiceMode_006, TestSize.Level0)
515 {
516     const std::vector<std::string> tagGroups = {"scene_performance"};
517     ASSERT_TRUE(OpenTrace(tagGroups) == TraceErrorCode::SUCCESS);
518     ASSERT_TRUE(access(DEFAULT_OUTPUT_DIR.c_str(), F_OK) == 0) << "/data/log/hitrace not exists.";
519 
520     SetSysInitParamTags(123);
521     ASSERT_TRUE(SetCheckParam() == false);
522 
523     ASSERT_TRUE(CloseTrace() == TraceErrorCode::SUCCESS);
524 }
525 
526 /**
527  * @tc.name: CommonUtils_001
528  * @tc.desc: Test canonicalizeSpecPath(), enter an existing file path.
529  * @tc.type: FUNC
530 */
531 HWTEST_F(HitraceDumpTest, CommonUtils_001, TestSize.Level0)
532 {
533     // prepare a file
534     std::string filePath = "/data/local/tmp/tmp.txt";
535     if (access(filePath.c_str(), F_OK) != 0) {
536         int fd = open(filePath.c_str(), O_CREAT);
537         close(fd);
538     }
539     ASSERT_TRUE(CanonicalizeSpecPath(filePath.c_str()) == filePath);
540 }
541 
542 /**
543  * @tc.name: CommonUtils_002
544  * @tc.desc: Test canonicalizeSpecPath(), enter a non-existent file path.
545  * @tc.type: FUNC
546 */
547 HWTEST_F(HitraceDumpTest, CommonUtils_002, TestSize.Level0)
548 {
549     // prepare a file
550     std::string filePath = "/data/local/tmp/tmp1.txt";
551     if (access(filePath.c_str(), F_OK) != 0) {
552         ASSERT_TRUE(CanonicalizeSpecPath(filePath.c_str()) == filePath);
553     }
554 }
555 
556 /**
557  * @tc.name: CommonUtils_003
558  * @tc.desc: Test canonicalizeSpecPath(), enter a non-existent file path with "..".
559  * @tc.type: FUNC
560 */
561 HWTEST_F(HitraceDumpTest, CommonUtils_003, TestSize.Level0)
562 {
563     // prepare a file
564     std::string filePath = "../tmp2.txt";
565     if (access(filePath.c_str(), F_OK) != 0) {
566         ASSERT_TRUE(CanonicalizeSpecPath(filePath.c_str()) == "");
567     }
568 }
569 
570 /**
571  * @tc.name: CommonUtils_004
572  * @tc.desc: Test MarkClockSync().
573  * @tc.type: FUNC
574 */
575 HWTEST_F(HitraceDumpTest, CommonUtils_004, TestSize.Level0)
576 {
577     ASSERT_TRUE(MarkClockSync(g_traceRootPath) == true);
578 }
579 
580 /**
581  * @tc.name: CommonUtils_005
582  * @tc.desc: Test ParseTagInfo().
583  * @tc.type: FUNC
584 */
585 HWTEST_F(HitraceDumpTest, CommonUtils_005, TestSize.Level0)
586 {
587     std::map<std::string, OHOS::HiviewDFX::Hitrace::TagCategory> allTags;
588     std::map<std::string, std::vector<std::string>> tagGroupTable;
589     ASSERT_TRUE(ParseTagInfo(allTags, tagGroupTable) == true);
590     ASSERT_TRUE(allTags.size() > 0);
591     ASSERT_TRUE(tagGroupTable.size() > 0);
592 }
593 
594 /**
595  * @tc.name: CommonUtils_006
596  * @tc.desc: Test ParseTagInfo().
597  * @tc.type: FUNC
598 */
599 HWTEST_F(HitraceDumpTest, CommonUtils_006, TestSize.Level0)
600 {
601     std::map<std::string, OHOS::HiviewDFX::Hitrace::TagCategory> allTags;
602     std::map<std::string, std::vector<std::string>> tagGroupTable;
603 
604     ASSERT_TRUE(RunCmd("mount -o rw,remount /"));
605     ASSERT_TRUE(RunCmd("cp /system/etc/hiview/hitrace_utils.json /system/etc/hiview/hitrace_utils-bak.json"));
606     ASSERT_TRUE(RunCmd("sed -i 's/tag_groups/TestCommonUtils/g' /system/etc/hiview/hitrace_utils.json"));
607     ParseTagInfo(allTags, tagGroupTable);
608     ASSERT_TRUE(RunCmd("sed -i 's/tag_category/TestCommonUtils/g' /system/etc/hiview/hitrace_utils.json"));
609     ParseTagInfo(allTags, tagGroupTable);
610     ASSERT_TRUE(RunCmd("rm /system/etc/hiview/hitrace_utils.json"));
611     ParseTagInfo(allTags, tagGroupTable);
612     ASSERT_TRUE(RunCmd("mv /system/etc/hiview/hitrace_utils-bak.json /system/etc/hiview/hitrace_utils.json"));
613 
614     ASSERT_TRUE(IsNumber("scene_performance") == TraceErrorCode::SUCCESS);
615     ASSERT_TRUE(IsNumber("") == false);
616     ASSERT_TRUE(IsNumber("tags:sched clockType:boot bufferSize:1024 overwrite:1") == false);
617     CanonicalizeSpecPath(nullptr);
618     MarkClockSync("/sys/kernel/debug/tracing/test_trace_marker");
619 }
620 } // namespace
621