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 constexpr const char *REPORT_PROTO_OUTPUT_PATH = "./perf.proto";
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
124 ASSERT_TRUE(content.size() >= 2);
125 ASSERT_EQ(content.front(), '{');
126 ASSERT_EQ(content.back(), '}');
127 }
128
UnwindJsonContentCheck(const std::string & content,bool haveUnwind) const129 void HiperfLibReportTest::UnwindJsonContentCheck(const std::string &content, bool haveUnwind) const
130 {
131 ASSERT_TRUE(content.size() >= 2);
132 ASSERT_EQ(content.front(), '{');
133 ASSERT_EQ(content.back(), '}');
134 #ifdef __arm__
135 ASSERT_EQ(content.find("{\"symbol\":8,\"counts\":[0,0,1006548]}") != std::string::npos,
136 haveUnwind);
137 #else
138 ASSERT_EQ(content.find("{\"symbol\":16,\"counts\":[24,9065269,81271852]}") != std::string::npos,
139 haveUnwind);
140 #endif
141 }
142
DefaultReportContentCheck(const std::string & content) const143 void HiperfLibReportTest::DefaultReportContentCheck(const std::string &content) const
144 {
145 /*
146 <<Hiperf Report>>
147 hostname: localhost
148 osrelease: 5.10.57
149 arch: armv7l
150 cpudesc: ARMv7 Processor rev 5 (v7l)
151 cmdline: ./hiperf --verbose record -d 1 -a
152 hiperf_record_time: Thu Jan 1 00:38:40 1970
153 Samples Count: 1000
154 Record Count: 3329
155 Event Count: 174434595
156 */
157
158 // we found 198 times in report.txt
159 size_t kernelKallsymsCount = 198;
160
161 EXPECT_EQ(content.find("hostname: localhost") != std::string::npos, true);
162 EXPECT_EQ(content.find("osrelease: 5.10.57") != std::string::npos, true);
163 EXPECT_EQ(content.find("arch: armv7l") != std::string::npos, true);
164 EXPECT_EQ(content.find("cpudesc: ARMv7 Processor rev 5 (v7l)") != std::string::npos, true);
165 EXPECT_EQ(content.find("hiperf_record_time: Thu Jan 1 00:38:40 1970") != std::string::npos,
166 true);
167 EXPECT_EQ(content.find("cmdline: ./hiperf --verbose record -d 1 -a") != std::string::npos,
168 true);
169
170 EXPECT_EQ(content.find("Event: hw-cpu-cycles (type 0 id 0)") != std::string::npos, true);
171 EXPECT_EQ(content.find("Samples Count: 1000") != std::string::npos, true);
172 EXPECT_EQ(content.find("Event Count: 174434595") != std::string::npos, true);
173 EXPECT_EQ(SubStringCount(content, "[kernel.kallsyms]"), kernelKallsymsCount);
174 }
175
DefaultDumpContentCheck(const std::string & content) const176 void HiperfLibReportTest::DefaultDumpContentCheck(const std::string &content) const
177 {
178 /*
179 magic: PERFILE2
180 header_size: 104
181 attr_size: 136
182 attrs[file section]: offset 112, size 136
183 data[file section]: offset 248, size 368316
184 event_types[file section]: offset 0, size 0
185 adds_features[]: 0x1DD8 0x0 0x0 0x5
186 feature: hostname
187 feature: osrelease
188 feature: arch
189 feature: nrcpus
190 feature: cpudesc
191 feature: total_mem
192 feature: cmdline
193 feature: event_desc
194 feature: hiperf_files_symbol
195 feature: hiperf_record_time
196 attr 1:
197 event_attr:
198 type 0, size 120, config 0
199 sample_freq 4000
200 sample_type (0x103e7)
201 sample_type names: ip,tid,time,callchain,id,cpu,period,stream_id,identifier
202 read_format (0x7)
203 disabled 1, inherit 1, pinned 0, exclusive 0
204 exclude_user 0, exclude_kernel 0, exclude_hv 0, exclude_idle 0
205 mmap 1, mmap2 1, comm 1, comm_exec 0, freq 1
206 inherit_stat 0, enable_on_exec 0, task 0, use_clockid 0
207 watermark 0, precise_ip 0, mmap_data 1, clockid 0
208 sample_id_all 0, exclude_host 0, exclude_guest 0
209 branch_sample_type 0x0
210 exclude_callchain_kernel 0, exclude_callchain_user 0
211 sample_regs_user 0x0
212 sample_stack_user 0x0
213 ids: 12
214 */
215
216 size_t symbolsCount = 39;
217 size_t buildIdCount = 32;
218 size_t sampleCount = 1000;
219 size_t featureCount = 10;
220
221 EXPECT_EQ(content.find("magic: PERFILE2") != std::string::npos, true);
222 EXPECT_EQ(SubStringCount(content, "fileid:"), symbolsCount);
223 EXPECT_EQ(SubStringCount(content, "buildId:"), buildIdCount);
224 EXPECT_EQ(SubStringCount(content, "record sample:"), sampleCount);
225 EXPECT_EQ(SubStringCount(content, "feature:"), featureCount);
226 }
SetUpTestCase()227 void HiperfLibReportTest::SetUpTestCase() {}
228
TearDownTestCase()229 void HiperfLibReportTest::TearDownTestCase()
230 {
231 DebugLogger::GetInstance()->Reset();
232 }
233
SetUp()234 void HiperfLibReportTest::SetUp() {}
235
TearDown()236 void HiperfLibReportTest::TearDown() {}
237
238 HWTEST_F(HiperfLibReportTest, Test_EchoLoopBackNumber, TestSize.Level1)
239 {
240 const char *result = EchoLoopBack("123");
241 EXPECT_EQ(result, "123");
242 }
243
244 HWTEST_F(HiperfLibReportTest, Test_EchoLoopBackString, TestSize.Level1)
245 {
246 const char *result = EchoLoopBack("test");
247 EXPECT_EQ(result, "test");
248 }
249
250 HWTEST_F(HiperfLibReportTest, Test_EchoLoopBackStringNumber, TestSize.Level1)
251 {
252 const char *result = EchoLoopBack("test123");
253 EXPECT_EQ(result, "test123");
254 }
255 HWTEST_F(HiperfLibReportTest, Test_EchoLoopBackEmpty, TestSize.Level1)
256 {
257 const char *result = EchoLoopBack("");
258 EXPECT_EQ(result, "");
259 }
260
261 HWTEST_F(HiperfLibReportTest, Test_EchoLoopBackNull, TestSize.Level1)
262 {
263 const char *result = EchoLoopBack(nullptr);
264 EXPECT_EQ(result, nullptr);
265 }
266
267 #ifdef HIPERF_DEBUG
268 HWTEST_F(HiperfLibReportTest, Test_SetDebug_Enable, TestSize.Level1)
269 {
270 EXPECT_EQ(SetDebug(true), 0);
271 EXPECT_EQ(DebugLogger::logDisabled_, false);
272 }
273
274 HWTEST_F(HiperfLibReportTest, Test_SetDebug_Disable, TestSize.Level1)
275 {
276 EXPECT_EQ(SetDebug(false), 0);
277 EXPECT_EQ(DebugLogger::logDisabled_, true);
278 }
279 #endif
280
281 HWTEST_F(HiperfLibReportTest, Test_Report_Success, TestSize.Level1)
282 {
283 unlink(REPORT_OUTPUT_PATH);
284 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
285
286 StdoutRecord stdoutRecord;
287 stdoutRecord.Start();
288 EXPECT_EQ(::Report(PERF_DATA_INPUT_PATH, REPORT_OUTPUT_PATH, ""), 0);
289 std::string stringOut = stdoutRecord.Stop();
290 EXPECT_EQ(stringOut.find("report done") != std::string::npos, true);
291 std::string content;
292 EXPECT_EQ(ReadFileToString(REPORT_OUTPUT_PATH, content), true);
293 DefaultReportContentCheck(content);
294 }
295
296 HWTEST_F(HiperfLibReportTest, Test_Report_Source_Null, TestSize.Level1)
297 {
298 unlink(REPORT_OUTPUT_PATH);
299 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
300 EXPECT_NE(::Report(nullptr, REPORT_OUTPUT_PATH, ""), 0);
301 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
302 }
303
304 HWTEST_F(HiperfLibReportTest, Test_Report_Target_Null, TestSize.Level1)
305 {
306 unlink(REPORT_OUTPUT_PATH);
307 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
308 EXPECT_NE(::Report(PERF_DATA_INPUT_PATH, nullptr, ""), 0);
309 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
310 }
311
312 HWTEST_F(HiperfLibReportTest, Test_Report_Both_Null, TestSize.Level1)
313 {
314 unlink(REPORT_OUTPUT_PATH);
315 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
316 EXPECT_NE(::Report(nullptr, nullptr, ""), 0);
317 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
318 }
319
320 HWTEST_F(HiperfLibReportTest, Test_Report_Parameter_Illegal, TestSize.Level1)
321 {
322 unlink(REPORT_OUTPUT_PATH);
323 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
324 EXPECT_NE(::Report(ILLEGAL_PATH, ILLEGAL_PATH, ""), 0);
325 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
326 EXPECT_NE(access(ILLEGAL_PATH, F_OK), 0);
327 }
328
329 HWTEST_F(HiperfLibReportTest, Test_Report_Parameter_Empty, TestSize.Level1)
330 {
331 unlink(REPORT_OUTPUT_PATH);
332 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
333 EXPECT_NE(::Report("", "", ""), 0);
334 EXPECT_NE(access(REPORT_OUTPUT_PATH, F_OK), 0);
335 EXPECT_NE(access(ILLEGAL_PATH, F_OK), 0);
336 }
337
338 HWTEST_F(HiperfLibReportTest, Test_Dump_Success, TestSize.Level1)
339 {
340 StdoutRecord stdoutRecord;
341 stdoutRecord.Start();
342 EXPECT_EQ(::Dump(PERF_DATA_INPUT_PATH), 0);
343 std::string stringOut = stdoutRecord.Stop();
344 DefaultDumpContentCheck(stringOut);
345 }
346
347 HWTEST_F(HiperfLibReportTest, Test_Dump_IllegalPath, TestSize.Level1)
348 {
349 StdoutRecord stdoutRecord;
350 stdoutRecord.Start();
351 EXPECT_NE(::Dump(ILLEGAL_PATH), 0);
352 }
353
354 HWTEST_F(HiperfLibReportTest, Test_ReportJson_Success, TestSize.Level1)
355 {
356 unlink(REPORT_JSON_OUTPUT_PATH);
357 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
358
359 EXPECT_EQ(::ReportJson(PERF_DATA_INPUT_PATH, REPORT_JSON_OUTPUT_PATH), 0);
360 EXPECT_EQ(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
361 std::string content;
362 EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
363 DefaultJsonContentCheck(content);
364 }
365
366 HWTEST_F(HiperfLibReportTest, Test_ReportJson_ErrorFormat, TestSize.Level1)
367 {
368 unlink(REPORT_JSON_OUTPUT_PATH);
369 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
370
371 EXPECT_NE(::ReportJson(PERF_DATA_ERROR_FORMAT_INPUT_PATH, REPORT_JSON_OUTPUT_PATH), 0);
372 if (access(REPORT_JSON_OUTPUT_PATH, F_OK) == 0) {
373 std::string content;
374 EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
375
376 // have file but no content
377 EXPECT_EQ(content.empty(), true);
378 }
379 }
380
381 HWTEST_F(HiperfLibReportTest, Test_ReportJson_IllegalPath, 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(ILLEGAL_PATH, ILLEGAL_PATH), 0);
387 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
388 EXPECT_NE(access(ILLEGAL_PATH, F_OK), 0);
389 }
390
391 HWTEST_F(HiperfLibReportTest, Test_ReportJson_Nullptr, TestSize.Level1)
392 {
393 unlink(REPORT_JSON_OUTPUT_PATH);
394 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
395
396 EXPECT_NE(::ReportJson(nullptr, nullptr), 0);
397 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
398 }
399
400 HWTEST_F(HiperfLibReportTest, Test_ReportJson_Empty, TestSize.Level1)
401 {
402 unlink(REPORT_JSON_OUTPUT_PATH);
403 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
404
405 EXPECT_NE(::ReportJson("", ""), 0);
406 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
407 }
408
409 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_Success, TestSize.Level1)
410 {
411 const char *result = ReportGetSymbolFiles(PERF_DATA_INPUT_PATH);
412 ASSERT_NE(result, nullptr);
413 DefaultSymbolsBuildIdContentCheck(result);
414 }
415
416 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_ErrorFormat, TestSize.Level1)
417 {
418 const char *result = ReportGetSymbolFiles(PERF_DATA_ERROR_FORMAT_INPUT_PATH);
419 ASSERT_NE(result, nullptr);
420 EXPECT_EQ(result[0], '\0');
421 }
422
423 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_IllegalPath, TestSize.Level1)
424 {
425 const char *result = ReportGetSymbolFiles(ILLEGAL_PATH);
426 ASSERT_NE(result, nullptr);
427 EXPECT_EQ(result[0], '\0');
428 }
429
430 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_Nullptr, TestSize.Level1)
431 {
432 const char *result = ReportGetSymbolFiles(nullptr);
433 ASSERT_NE(result, nullptr);
434 EXPECT_EQ(result[0], '\0');
435 }
436
437 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_Empty, TestSize.Level1)
438 {
439 const char *result = ReportGetSymbolFiles("");
440 ASSERT_NE(result, nullptr);
441 EXPECT_EQ(result[0], '\0');
442 }
443
444 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_32_Success, TestSize.Level1)
445 {
446 const char *result = ReportGetBuildId(TEST_ELF32);
447 ASSERT_NE(result, nullptr);
448 EXPECT_STREQ(result, BUILDID_TEST_ELF32);
449 }
450
451 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_64_Success, TestSize.Level1)
452 {
453 const char *result = ReportGetBuildId(TEST_ELF64);
454 ASSERT_NE(result, nullptr);
455 EXPECT_STREQ(result, BUILDID_TEST_ELF64);
456 }
457
458 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_Empty, TestSize.Level1)
459 {
460 const char *result = ReportGetBuildId("");
461 ASSERT_NE(result, nullptr);
462 EXPECT_STREQ(result, "");
463 }
464
465 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_Nullptr, TestSize.Level1)
466 {
467 const char *result = ReportGetBuildId(TEST_ELF_BROKEN);
468 ASSERT_NE(result, nullptr);
469 EXPECT_STREQ(result, "");
470 }
471
472 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_IllegalPath, TestSize.Level1)
473 {
474 const char *result = ReportGetBuildId(ILLEGAL_PATH);
475 ASSERT_NE(result, nullptr);
476 EXPECT_STREQ(result, "");
477 }
478
479 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_Success, TestSize.Level1)
480 {
481 unlink(REPORT_JSON_OUTPUT_PATH);
482 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
483
484 EXPECT_EQ(::ReportUnwindJson(PERF_DISABLE_UNWIND_DATA_INPUT_PATH, REPORT_JSON_OUTPUT_PATH,
485 PERF_DISABLE_UNWIND_SYMBOL_INPUT_PATH),
486 0);
487 EXPECT_EQ(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
488 std::string content;
489 EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
490 UnwindJsonContentCheck(content, true);
491 }
492
493 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_NoSymbols_Failed, TestSize.Level1)
494 {
495 unlink(REPORT_JSON_OUTPUT_PATH);
496 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
497
498 EXPECT_EQ(
499 ::ReportUnwindJson(PERF_DISABLE_UNWIND_DATA_INPUT_PATH, REPORT_JSON_OUTPUT_PATH, nullptr),
500 0);
501 EXPECT_EQ(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
502 std::string content;
503 EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
504 UnwindJsonContentCheck(content, false);
505 }
506
507 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_ErrorFormat, TestSize.Level1)
508 {
509 unlink(REPORT_JSON_OUTPUT_PATH);
510 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
511
512 EXPECT_NE(::ReportUnwindJson(PERF_DATA_ERROR_FORMAT_INPUT_PATH, REPORT_JSON_OUTPUT_PATH,
513 PERF_DISABLE_UNWIND_SYMBOL_INPUT_PATH),
514 0);
515 if (access(REPORT_JSON_OUTPUT_PATH, F_OK) == 0) {
516 std::string content;
517 EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
518
519 // have file but no content
520 EXPECT_EQ(content.empty(), true);
521 }
522 }
523
524 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_IllegalPath, 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(ILLEGAL_PATH, ILLEGAL_PATH, ILLEGAL_PATH), 0);
530 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
531 EXPECT_NE(access(ILLEGAL_PATH, F_OK), 0);
532 }
533
534 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_Nullptr, TestSize.Level1)
535 {
536 unlink(REPORT_JSON_OUTPUT_PATH);
537 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
538
539 EXPECT_NE(::ReportUnwindJson(nullptr, nullptr, nullptr), 0);
540 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
541 }
542
543 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_Empty, TestSize.Level1)
544 {
545 unlink(REPORT_JSON_OUTPUT_PATH);
546 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
547
548 EXPECT_NE(::ReportUnwindJson("", "", ""), 0);
549 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
550 }
551 } // namespace HiPerf
552 } // namespace Developtools
553 } // namespace OHOS
554