• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2022 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 "hiperf_libreport_test.h"
17 #include "debug_logger.h"
18 #include "hiperf_libreport.h"
19 #include "subcommand_dump.h"
20 #include "subcommand_report.h"
21 #include "utilities.h"
22 
23 using namespace testing::ext;
24 using namespace std;
25 using namespace OHOS::HiviewDFX;
26 
27 namespace OHOS {
28 namespace Developtools {
29 namespace HiPerf {
30 constexpr const char *PERF_DATA_INPUT_PATH =
31     "/data/test/resource/testdata/report/perf.data.libreport";
32 #ifdef __arm__
33 constexpr const char *PERF_DISABLE_UNWIND_DATA_INPUT_PATH =
34     "/data/test/resource/testdata/report/perf.disable.unwind.hiperf_example_cmd.data";
35 #else
36 constexpr const char *PERF_DISABLE_UNWIND_DATA_INPUT_PATH =
37     "/data/test/resource/testdata/report/perf.disable.unwind.hiperf_example_cmd_64.data";
38 #endif
39 constexpr const char *PERF_DISABLE_UNWIND_SYMBOL_INPUT_PATH = "/data/test/resource/testdata/report";
40 constexpr const char *PERF_DATA_ERROR_FORMAT_INPUT_PATH =
41     "/data/test/resource/testdata/report/perf.data.error.format.libreport";
42 constexpr const char *REPORT_OUTPUT_PATH = "./report.txt";
43 constexpr const char *REPORT_JSON_OUTPUT_PATH = "./perf.json";
44 
45 constexpr const char *ILLEGAL_PATH = "/proc/illegal";
46 
47 constexpr const char *TEST_ELF32 = "/data/test/resource/testdata/report/buildid_test_elf_32";
48 constexpr const char *BUILDID_TEST_ELF32 = "8e5a30338be326934ff93c998dcd0d22fe345870";
49 
50 constexpr const char *TEST_ELF64 = "/data/test/resource/testdata/report/buildid_test_elf_64";
51 constexpr const char *BUILDID_TEST_ELF64 = "24c55dccc5baaaa140da0083207abcb8d523e248";
52 
53 constexpr const char *TEST_ELF_BROKEN = "/data/test/resource/testdata/report/elf32_test_broken";
54 
55 class HiperfLibReportTest : public testing::Test {
56 public:
57     static void SetUpTestCase(void);
58     static void TearDownTestCase(void);
59     void SetUp();
60     void TearDown();
61     void DefaultReportContentCheck(const std::string &) const;
62     void DefaultDumpContentCheck(const std::string &) const;
63     void DefaultJsonContentCheck(const std::string &) const;
64     void UnwindJsonContentCheck(const std::string &, bool haveUnwind) const;
65     void DefaultSymbolsBuildIdContentCheck(const std::string &) const;
66 };
67 
DefaultSymbolsBuildIdContentCheck(const std::string & content) const68 void HiperfLibReportTest::DefaultSymbolsBuildIdContentCheck(const std::string &content) const
69 {
70     constexpr const char *buildIdList =
71         "[/system/lib/libhilogutil.so,c32713a57e92190404f8e46e19346a08],"
72         "[/system/lib/libhilog_os_adapter.z.so,6fb456cf373eb617e129c89b6f5982d9],"
73         "[/system/lib/libhilog.so,07daae0a6ed91e8ba56a0c76d6a3ac4a],[/system/lib/"
74         "libc++.so,38fdac430dfe95b261aa33d3d7cf553415686ba6],"
75         "[/system/lib/libutils.z.so,f24970cefc3e5fac09fe07168f9353f9],"
76         "[/system/bin/ld-musl-arm.so.1,],[/system/lib/libmpi.so,],"
77         "[/system/lib/libhi_osal.so,],[/system/lib/libhi3516cv500_vo.so,],[/system/"
78         "lib/libhi3516cv500_vdec.so,],[/system/lib/libhi3516cv500_sys.so,],[/system/lib/"
79         "libhi3516cv500_chnl.so,],[/system/lib/libffi.z.so,3b0380999b8469ed9b1ebd252d6cb08f],[/"
80         "system/lib/libdsoftbus_trans_direct_channel_sdk.z.so,f3c136104d821facf3573b7c34f36097],[/"
81         "system/lib/libipc_core.z.so,5a11ea1cacdad9d160eb8101f5a89e68],[/system/lib/"
82         "libtel_core_service.z.so,9dbeadf74de1d9cc83c25fbce88203ed],[/system/lib/"
83         "libace_napi_quickjs.z.so,99850793579d24a88d20931ab40147c0],[/system/lib/"
84         "libace_engine_qjs.z.so,34dfa8cbdeb49bb797ad306cc6a3e418],[/system/lib/"
85         "libvsync_client.z.so,4d6c9ae5004f39e16a4cd6194a2c1f80],[/system/lib/"
86         "libace.z.so,b8d9d3bb0184ba917e8d05d471e8387a],[/system/lib/"
87         "libeventhandler.z.so,b1e08a8a6eabe33363f721b14393b06b],[/system/bin/"
88         "hilogd,b11a9f3700884ded33879f81914e3aa2],[/system/bin/"
89         "wifi_hal_service,6beba486dcdf5debb5c9248bc5cdf5f0],[/data/local/tmp/"
90         "hiperf,32aa1e9c5f35d49e16fa1b6e86db130c],[/system/bin/"
91         "hiview,419493d9ccef1df5dedfa4a144249e49],[/system/lib/"
92         "libhiviewbase.z.so,349d82d4f1d2dc21c039156afbe470ad],[/system/lib/"
93         "libvsync_module.z.so,b8ad1b79c5816a6540a08d21ff88ac66],[[kernel.kallsyms],"
94         "ec918ed092bf1820cfdd9dbad43b19fbe0695472],[/vendor/modules/"
95         "hi_mipi_rx.ko,df9b94ace268f6ea6e43bb29a2e4d0e48cb5847d],[/vendor/modules/"
96         "hi3516cv500_hdmi.ko,0968b8fa34aa7f26dc5b953785933aeb62b3d5d5],[/vendor/modules/"
97         "hifb.ko,939f84e15d744af033284c1e6a6cd047ed3a5da9],[/vendor/modules/"
98         "hi3516cv500_vo_dev.ko,d391f542c407edb116fc16f45d25cc5fbee9f287],[/vendor/modules/"
99         "hi3516cv500_tde.ko,417a56e829c0773c5ed2303bf33ab51a50c414d7],[/vendor/modules/"
100         "hi3516cv500_sys.ko,221223701496c08e458bc7b98f17cb5f8ec54e7c],[/vendor/modules/"
101         "hi3516cv500_base.ko,20fc3ae721d798ba72fd62a78bc7c3c9a4007446],[/vendor/modules/"
102         "sys_config.ko,8f245b1e73775567c74574f912a96a26d1cb02c8],[/vendor/modules/"
103         "hi_proc.ko,789cbddace68515aea94aa2b7f0cbf2b81d48c9f],[/vendor/modules/"
104         "hi_irq.ko,3048ea5e3ed49489a876fe9ad4fcdbe73820f751],[/vendor/modules/"
105         "hi_osal.ko,3f7b2c2f0030a13dbd01c58f5b52eb0a1dd427af]";
106 
107     ASSERT_TRUE(content.size() >= 2u);
108     ASSERT_EQ(content.front(), '[');
109     ASSERT_EQ(content.back(), ']');
110     std::vector<std::string> fileBuildPairs = StringSplit(content, "[");
111     for (std::string &pair : fileBuildPairs) {
112         std::vector<std::string> buildIdPair = StringSplit(pair, ",");
113         ASSERT_EQ(buildIdPair.size(), 2u);
114     }
115     EXPECT_STREQ(content.c_str(), buildIdList);
116 }
117 
DefaultJsonContentCheck(const std::string & content) const118 void HiperfLibReportTest::DefaultJsonContentCheck(const std::string &content) const
119 {
120     /*
121         json must have { xxx  } format
122     */
123     ASSERT_TRUE(content.size() >= 2); // 2: args size
124     ASSERT_EQ(content.front(), '{');
125     ASSERT_EQ(content.back(), '}');
126 }
127 
UnwindJsonContentCheck(const std::string & content,bool haveUnwind) const128 void HiperfLibReportTest::UnwindJsonContentCheck(const std::string &content, bool haveUnwind) const
129 {
130     ASSERT_TRUE(content.size() >= 2); // 2: args size
131     ASSERT_EQ(content.front(), '{');
132     ASSERT_EQ(content.back(), '}');
133 #ifdef __arm__
134     ASSERT_EQ(content.find("{\"symbol\":6511,\"counts\":[3,548767,2923570]}") != std::string::npos,
135               haveUnwind);
136 #else
137     ASSERT_EQ(content.find("{\"symbol\":76,\"counts\":[0,0,147027]}") != std::string::npos,
138               haveUnwind);
139 #endif
140 }
141 
DefaultReportContentCheck(const std::string & content) const142 void HiperfLibReportTest::DefaultReportContentCheck(const std::string &content) const
143 {
144     /*
145 <<Hiperf Report>>
146 hostname: localhost
147 osrelease: 5.10.57
148 arch: armv7l
149 cpudesc: ARMv7 Processor rev 5 (v7l)
150 cmdline: ./hiperf --verbose record -d 1 -a
151 hiperf_record_time: Thu Jan  1 00:38:40 1970
152 Samples Count: 1000
153 Record Count: 3329
154 Event Count: 174434595
155     */
156 
157     // we found 198 times in report.txt
158     size_t kernelKallsymsCount = 198;
159 
160     EXPECT_EQ(content.find("hostname: localhost") != std::string::npos, true);
161     EXPECT_EQ(content.find("osrelease: 5.10.57") != std::string::npos, true);
162     EXPECT_EQ(content.find("arch: armv7l") != std::string::npos, true);
163     EXPECT_EQ(content.find("cpudesc: ARMv7 Processor rev 5 (v7l)") != std::string::npos, true);
164     EXPECT_EQ(content.find("hiperf_record_time: Thu Jan  1 00:38:40 1970") != std::string::npos,
165               true);
166     EXPECT_EQ(content.find("cmdline: ./hiperf --verbose record -d 1 -a") != std::string::npos,
167               true);
168 
169     EXPECT_EQ(content.find("Event: hw-cpu-cycles (type 0 id 0)") != std::string::npos, true);
170     EXPECT_EQ(content.find("Samples Count: 1000") != std::string::npos, true);
171     EXPECT_EQ(content.find("Event Count: 174434595") != std::string::npos, true);
172     EXPECT_EQ(SubStringCount(content, "[kernel.kallsyms]"), kernelKallsymsCount);
173 }
174 
DefaultDumpContentCheck(const std::string & content) const175 void HiperfLibReportTest::DefaultDumpContentCheck(const std::string &content) const
176 {
177     /*
178 magic: PERFILE2
179 header_size: 104
180 attr_size: 136
181 attrs[file section]: offset 112, size 136
182 data[file section]: offset 248, size 368316
183 event_types[file section]: offset 0, size 0
184 adds_features[]: 0x1DD8 0x0 0x0 0x5
185 feature: hostname
186 feature: osrelease
187 feature: arch
188 feature: nrcpus
189 feature: cpudesc
190 feature: total_mem
191 feature: cmdline
192 feature: event_desc
193 feature: hiperf_files_symbol
194 feature: hiperf_record_time
195 attr 1:
196   event_attr:
197     type 0, size 120, config 0
198     sample_freq 4000
199     sample_type (0x103e7)
200     sample_type names: ip,tid,time,callchain,id,cpu,period,stream_id,identifier
201     read_format (0x7)
202     disabled 1, inherit 1, pinned 0, exclusive 0
203     exclude_user 0, exclude_kernel 0, exclude_hv 0, exclude_idle 0
204     mmap 1, mmap2 1, comm 1, comm_exec 0, freq 1
205     inherit_stat 0, enable_on_exec 0, task 0, use_clockid 0
206     watermark 0, precise_ip 0, mmap_data 1, clockid 0
207     sample_id_all 0, exclude_host 0, exclude_guest 0
208     branch_sample_type 0x0
209     exclude_callchain_kernel 0, exclude_callchain_user 0
210     sample_regs_user 0x0
211     sample_stack_user 0x0
212   ids: 12
213     */
214 
215     size_t symbolsCount = 39;
216     size_t buildIdCount = 32;
217     size_t sampleCount = 1000;
218     size_t featureCount = 10;
219 
220     EXPECT_EQ(content.find("magic: PERFILE2") != std::string::npos, true);
221     EXPECT_EQ(SubStringCount(content, "fileid:"), symbolsCount);
222     EXPECT_EQ(SubStringCount(content, "buildId:"), buildIdCount);
223     EXPECT_EQ(SubStringCount(content, "record sample:"), sampleCount);
224     EXPECT_EQ(SubStringCount(content, "feature:"), featureCount);
225 }
SetUpTestCase()226 void HiperfLibReportTest::SetUpTestCase() {}
227 
TearDownTestCase()228 void HiperfLibReportTest::TearDownTestCase()
229 {
230     DebugLogger::GetInstance()->Reset();
231 }
232 
SetUp()233 void HiperfLibReportTest::SetUp() {}
234 
TearDown()235 void HiperfLibReportTest::TearDown() {}
236 
237 HWTEST_F(HiperfLibReportTest, Test_EchoLoopBackNumber, TestSize.Level1)
238 {
239     const char *result = EchoLoopBack("123");
240     EXPECT_EQ(result, "123");
241 }
242 
243 HWTEST_F(HiperfLibReportTest, Test_EchoLoopBackString, TestSize.Level1)
244 {
245     const char *result = EchoLoopBack("test");
246     EXPECT_EQ(result, "test");
247 }
248 
249 HWTEST_F(HiperfLibReportTest, Test_EchoLoopBackStringNumber, TestSize.Level1)
250 {
251     const char *result = EchoLoopBack("test123");
252     EXPECT_EQ(result, "test123");
253 }
254 HWTEST_F(HiperfLibReportTest, Test_EchoLoopBackEmpty, TestSize.Level1)
255 {
256     const char *result = EchoLoopBack("");
257     EXPECT_EQ(result, "");
258 }
259 
260 HWTEST_F(HiperfLibReportTest, Test_EchoLoopBackNull, TestSize.Level1)
261 {
262     const char *result = EchoLoopBack(nullptr);
263     EXPECT_EQ(result, nullptr);
264 }
265 
266 #ifdef HIPERF_DEBUG
267 HWTEST_F(HiperfLibReportTest, Test_SetDebug_Enable, TestSize.Level1)
268 {
269     EXPECT_EQ(SetDebug(true), 0);
270     EXPECT_EQ(DebugLogger::logDisabled_, false);
271 }
272 
273 HWTEST_F(HiperfLibReportTest, Test_SetDebug_Disable, TestSize.Level1)
274 {
275     EXPECT_EQ(SetDebug(false), 0);
276     EXPECT_EQ(DebugLogger::logDisabled_, true);
277 }
278 #endif
279 
280 HWTEST_F(HiperfLibReportTest, Test_Report_Success, TestSize.Level1)
281 {
282     unlink(REPORT_OUTPUT_PATH);
283     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
284 
285     StdoutRecord stdoutRecord;
286     stdoutRecord.Start();
287     EXPECT_EQ(::Report(PERF_DATA_INPUT_PATH, REPORT_OUTPUT_PATH, ""), 0);
288     std::string stringOut = stdoutRecord.Stop();
289     EXPECT_EQ(stringOut.find("report done") != std::string::npos, true);
290     std::string content;
291     EXPECT_EQ(ReadFileToString(REPORT_OUTPUT_PATH, content), true);
292     DefaultReportContentCheck(content);
293 }
294 
295 HWTEST_F(HiperfLibReportTest, Test_Report_Source_Null, TestSize.Level1)
296 {
297     unlink(REPORT_OUTPUT_PATH);
298     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
299     EXPECT_NE(::Report(nullptr, REPORT_OUTPUT_PATH, ""), 0);
300     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
301 }
302 
303 HWTEST_F(HiperfLibReportTest, Test_Report_Target_Null, TestSize.Level1)
304 {
305     unlink(REPORT_OUTPUT_PATH);
306     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
307     EXPECT_NE(::Report(PERF_DATA_INPUT_PATH, nullptr, ""), 0);
308     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
309 }
310 
311 HWTEST_F(HiperfLibReportTest, Test_Report_Both_Null, TestSize.Level1)
312 {
313     unlink(REPORT_OUTPUT_PATH);
314     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
315     EXPECT_NE(::Report(nullptr, nullptr, ""), 0);
316     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
317 }
318 
319 HWTEST_F(HiperfLibReportTest, Test_Report_Parameter_Illegal, TestSize.Level1)
320 {
321     unlink(REPORT_OUTPUT_PATH);
322     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
323     EXPECT_NE(::Report(ILLEGAL_PATH, ILLEGAL_PATH, ""), 0);
324     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
325     EXPECT_NE(access(ILLEGAL_PATH, F_OK), 0);
326 }
327 
328 HWTEST_F(HiperfLibReportTest, Test_Report_Parameter_Empty, TestSize.Level1)
329 {
330     unlink(REPORT_OUTPUT_PATH);
331     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
332     EXPECT_NE(::Report("", "", ""), 0);
333     EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
334     EXPECT_NE(access(ILLEGAL_PATH, F_OK), 0);
335 }
336 
337 HWTEST_F(HiperfLibReportTest, Test_Dump_IllegalPath, TestSize.Level1)
338 {
339     StdoutRecord stdoutRecord;
340     stdoutRecord.Start();
341     EXPECT_NE(::Dump(ILLEGAL_PATH), 0);
342 }
343 
344 HWTEST_F(HiperfLibReportTest, Test_ReportJson_Success, TestSize.Level1)
345 {
346     unlink(REPORT_JSON_OUTPUT_PATH);
347     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
348 
349     EXPECT_EQ(::ReportJson(PERF_DATA_INPUT_PATH, REPORT_JSON_OUTPUT_PATH), 0);
350     EXPECT_EQ(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
351     std::string content;
352     EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
353     DefaultJsonContentCheck(content);
354 }
355 
356 HWTEST_F(HiperfLibReportTest, Test_ReportJson_ErrorFormat, TestSize.Level1)
357 {
358     unlink(REPORT_JSON_OUTPUT_PATH);
359     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
360 
361     EXPECT_NE(::ReportJson(PERF_DATA_ERROR_FORMAT_INPUT_PATH, REPORT_JSON_OUTPUT_PATH), 0);
362     if (access(REPORT_JSON_OUTPUT_PATH, F_OK) == 0) {
363         std::string content;
364         EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
365 
366         // have file but no content
367         EXPECT_EQ(content.empty(), true);
368     }
369 }
370 
371 HWTEST_F(HiperfLibReportTest, Test_ReportJson_IllegalPath, TestSize.Level1)
372 {
373     unlink(REPORT_JSON_OUTPUT_PATH);
374     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
375 
376     EXPECT_NE(::ReportJson(ILLEGAL_PATH, ILLEGAL_PATH), 0);
377     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
378     EXPECT_NE(access(ILLEGAL_PATH, F_OK), 0);
379 }
380 
381 HWTEST_F(HiperfLibReportTest, Test_ReportJson_Nullptr, TestSize.Level1)
382 {
383     unlink(REPORT_JSON_OUTPUT_PATH);
384     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
385 
386     EXPECT_NE(::ReportJson(nullptr, nullptr), 0);
387     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
388 }
389 
390 HWTEST_F(HiperfLibReportTest, Test_ReportJson_Empty, TestSize.Level1)
391 {
392     unlink(REPORT_JSON_OUTPUT_PATH);
393     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
394 
395     EXPECT_NE(::ReportJson("", ""), 0);
396     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
397 }
398 
399 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_Success, TestSize.Level1)
400 {
401     const char *result = ReportGetSymbolFiles(PERF_DATA_INPUT_PATH);
402     ASSERT_NE(result, nullptr);
403     DefaultSymbolsBuildIdContentCheck(result);
404 }
405 
406 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_ErrorFormat, TestSize.Level1)
407 {
408     const char *result = ReportGetSymbolFiles(PERF_DATA_ERROR_FORMAT_INPUT_PATH);
409     ASSERT_NE(result, nullptr);
410     EXPECT_EQ(result[0], '\0');
411 }
412 
413 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_IllegalPath, TestSize.Level1)
414 {
415     const char *result = ReportGetSymbolFiles(ILLEGAL_PATH);
416     ASSERT_NE(result, nullptr);
417     EXPECT_EQ(result[0], '\0');
418 }
419 
420 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_Nullptr, TestSize.Level1)
421 {
422     const char *result = ReportGetSymbolFiles(nullptr);
423     ASSERT_NE(result, nullptr);
424     EXPECT_EQ(result[0], '\0');
425 }
426 
427 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_Empty, TestSize.Level1)
428 {
429     const char *result = ReportGetSymbolFiles("");
430     ASSERT_NE(result, nullptr);
431     EXPECT_EQ(result[0], '\0');
432 }
433 
434 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_32_Success, TestSize.Level1)
435 {
436     const char *result = ReportGetBuildId(TEST_ELF32);
437     ASSERT_NE(result, nullptr);
438     EXPECT_STREQ(result, BUILDID_TEST_ELF32);
439 }
440 
441 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_64_Success, TestSize.Level1)
442 {
443     const char *result = ReportGetBuildId(TEST_ELF64);
444     ASSERT_NE(result, nullptr);
445     EXPECT_STREQ(result, BUILDID_TEST_ELF64);
446 }
447 
448 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_Empty, TestSize.Level1)
449 {
450     const char *result = ReportGetBuildId("");
451     ASSERT_NE(result, nullptr);
452     EXPECT_STREQ(result, "");
453 }
454 
455 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_Nullptr, TestSize.Level1)
456 {
457     const char *result = ReportGetBuildId(TEST_ELF_BROKEN);
458     ASSERT_NE(result, nullptr);
459     EXPECT_STREQ(result, "");
460 }
461 
462 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_IllegalPath, TestSize.Level1)
463 {
464     const char *result = ReportGetBuildId(ILLEGAL_PATH);
465     ASSERT_NE(result, nullptr);
466     EXPECT_STREQ(result, "");
467 }
468 
469 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_Success, TestSize.Level1)
470 {
471     unlink(REPORT_JSON_OUTPUT_PATH);
472     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
473 
474     EXPECT_EQ(::ReportUnwindJson(PERF_DISABLE_UNWIND_DATA_INPUT_PATH, REPORT_JSON_OUTPUT_PATH,
475                                  PERF_DISABLE_UNWIND_SYMBOL_INPUT_PATH),
476               0);
477     EXPECT_EQ(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
478     std::string content;
479     EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
480     UnwindJsonContentCheck(content, true);
481 }
482 
483 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_NoSymbols_Failed, TestSize.Level1)
484 {
485     unlink(REPORT_JSON_OUTPUT_PATH);
486     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
487 
488     EXPECT_EQ(
489         ::ReportUnwindJson(PERF_DISABLE_UNWIND_DATA_INPUT_PATH, REPORT_JSON_OUTPUT_PATH, nullptr),
490         0);
491     EXPECT_EQ(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
492     std::string content;
493     EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
494     UnwindJsonContentCheck(content, false);
495 }
496 
497 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_ErrorFormat, TestSize.Level1)
498 {
499     unlink(REPORT_JSON_OUTPUT_PATH);
500     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
501 
502     EXPECT_NE(::ReportUnwindJson(PERF_DATA_ERROR_FORMAT_INPUT_PATH, REPORT_JSON_OUTPUT_PATH,
503                                  PERF_DISABLE_UNWIND_SYMBOL_INPUT_PATH),
504               0);
505     if (access(REPORT_JSON_OUTPUT_PATH, F_OK) == 0) {
506         std::string content;
507         EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
508 
509         // have file but no content
510         EXPECT_EQ(content.empty(), true);
511     }
512 }
513 
514 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_IllegalPath, TestSize.Level1)
515 {
516     unlink(REPORT_JSON_OUTPUT_PATH);
517     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
518 
519     EXPECT_NE(::ReportUnwindJson(ILLEGAL_PATH, ILLEGAL_PATH, ILLEGAL_PATH), 0);
520     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
521     EXPECT_NE(access(ILLEGAL_PATH, F_OK), 0);
522 }
523 
524 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_Nullptr, TestSize.Level1)
525 {
526     unlink(REPORT_JSON_OUTPUT_PATH);
527     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
528 
529     EXPECT_NE(::ReportUnwindJson(nullptr, nullptr, nullptr), 0);
530     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
531 }
532 
533 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_Empty, TestSize.Level1)
534 {
535     unlink(REPORT_JSON_OUTPUT_PATH);
536     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
537 
538     EXPECT_NE(::ReportUnwindJson("", "", ""), 0);
539     EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
540 }
541 } // namespace HiPerf
542 } // namespace Developtools
543 } // namespace OHOS
544