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,190755171]}") != 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_IllegalPath, TestSize.Level1)
339 {
340 StdoutRecord stdoutRecord;
341 stdoutRecord.Start();
342 EXPECT_NE(::Dump(ILLEGAL_PATH), 0);
343 }
344
345 HWTEST_F(HiperfLibReportTest, Test_ReportJson_Success, TestSize.Level1)
346 {
347 unlink(REPORT_JSON_OUTPUT_PATH);
348 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
349
350 EXPECT_EQ(::ReportJson(PERF_DATA_INPUT_PATH, REPORT_JSON_OUTPUT_PATH), 0);
351 EXPECT_EQ(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
352 std::string content;
353 EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
354 DefaultJsonContentCheck(content);
355 }
356
357 HWTEST_F(HiperfLibReportTest, Test_ReportJson_ErrorFormat, TestSize.Level1)
358 {
359 unlink(REPORT_JSON_OUTPUT_PATH);
360 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
361
362 EXPECT_NE(::ReportJson(PERF_DATA_ERROR_FORMAT_INPUT_PATH, REPORT_JSON_OUTPUT_PATH), 0);
363 if (access(REPORT_JSON_OUTPUT_PATH, F_OK) == 0) {
364 std::string content;
365 EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
366
367 // have file but no content
368 EXPECT_EQ(content.empty(), true);
369 }
370 }
371
372 HWTEST_F(HiperfLibReportTest, Test_ReportJson_IllegalPath, TestSize.Level1)
373 {
374 unlink(REPORT_JSON_OUTPUT_PATH);
375 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
376
377 EXPECT_NE(::ReportJson(ILLEGAL_PATH, ILLEGAL_PATH), 0);
378 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
379 EXPECT_NE(access(ILLEGAL_PATH, F_OK), 0);
380 }
381
382 HWTEST_F(HiperfLibReportTest, Test_ReportJson_Nullptr, TestSize.Level1)
383 {
384 unlink(REPORT_JSON_OUTPUT_PATH);
385 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
386
387 EXPECT_NE(::ReportJson(nullptr, nullptr), 0);
388 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
389 }
390
391 HWTEST_F(HiperfLibReportTest, Test_ReportJson_Empty, 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("", ""), 0);
397 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
398 }
399
400 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_Success, TestSize.Level1)
401 {
402 const char *result = ReportGetSymbolFiles(PERF_DATA_INPUT_PATH);
403 ASSERT_NE(result, nullptr);
404 DefaultSymbolsBuildIdContentCheck(result);
405 }
406
407 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_ErrorFormat, TestSize.Level1)
408 {
409 const char *result = ReportGetSymbolFiles(PERF_DATA_ERROR_FORMAT_INPUT_PATH);
410 ASSERT_NE(result, nullptr);
411 EXPECT_EQ(result[0], '\0');
412 }
413
414 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_IllegalPath, TestSize.Level1)
415 {
416 const char *result = ReportGetSymbolFiles(ILLEGAL_PATH);
417 ASSERT_NE(result, nullptr);
418 EXPECT_EQ(result[0], '\0');
419 }
420
421 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_Nullptr, TestSize.Level1)
422 {
423 const char *result = ReportGetSymbolFiles(nullptr);
424 ASSERT_NE(result, nullptr);
425 EXPECT_EQ(result[0], '\0');
426 }
427
428 HWTEST_F(HiperfLibReportTest, Test_ReportGetSymbolFiles_Empty, TestSize.Level1)
429 {
430 const char *result = ReportGetSymbolFiles("");
431 ASSERT_NE(result, nullptr);
432 EXPECT_EQ(result[0], '\0');
433 }
434
435 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_32_Success, TestSize.Level1)
436 {
437 const char *result = ReportGetBuildId(TEST_ELF32);
438 ASSERT_NE(result, nullptr);
439 EXPECT_STREQ(result, BUILDID_TEST_ELF32);
440 }
441
442 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_64_Success, TestSize.Level1)
443 {
444 const char *result = ReportGetBuildId(TEST_ELF64);
445 ASSERT_NE(result, nullptr);
446 EXPECT_STREQ(result, BUILDID_TEST_ELF64);
447 }
448
449 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_Empty, TestSize.Level1)
450 {
451 const char *result = ReportGetBuildId("");
452 ASSERT_NE(result, nullptr);
453 EXPECT_STREQ(result, "");
454 }
455
456 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_Nullptr, TestSize.Level1)
457 {
458 const char *result = ReportGetBuildId(TEST_ELF_BROKEN);
459 ASSERT_NE(result, nullptr);
460 EXPECT_STREQ(result, "");
461 }
462
463 HWTEST_F(HiperfLibReportTest, Test_ReportGetBuildId_IllegalPath, TestSize.Level1)
464 {
465 const char *result = ReportGetBuildId(ILLEGAL_PATH);
466 ASSERT_NE(result, nullptr);
467 EXPECT_STREQ(result, "");
468 }
469
470 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_Success, TestSize.Level1)
471 {
472 unlink(REPORT_JSON_OUTPUT_PATH);
473 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
474
475 EXPECT_EQ(::ReportUnwindJson(PERF_DISABLE_UNWIND_DATA_INPUT_PATH, REPORT_JSON_OUTPUT_PATH,
476 PERF_DISABLE_UNWIND_SYMBOL_INPUT_PATH),
477 0);
478 EXPECT_EQ(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
479 std::string content;
480 EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
481 UnwindJsonContentCheck(content, true);
482 }
483
484 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_NoSymbols_Failed, TestSize.Level1)
485 {
486 unlink(REPORT_JSON_OUTPUT_PATH);
487 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
488
489 EXPECT_EQ(
490 ::ReportUnwindJson(PERF_DISABLE_UNWIND_DATA_INPUT_PATH, REPORT_JSON_OUTPUT_PATH, nullptr),
491 0);
492 EXPECT_EQ(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
493 std::string content;
494 EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
495 UnwindJsonContentCheck(content, false);
496 }
497
498 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_ErrorFormat, TestSize.Level1)
499 {
500 unlink(REPORT_JSON_OUTPUT_PATH);
501 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
502
503 EXPECT_NE(::ReportUnwindJson(PERF_DATA_ERROR_FORMAT_INPUT_PATH, REPORT_JSON_OUTPUT_PATH,
504 PERF_DISABLE_UNWIND_SYMBOL_INPUT_PATH),
505 0);
506 if (access(REPORT_JSON_OUTPUT_PATH, F_OK) == 0) {
507 std::string content;
508 EXPECT_EQ(ReadFileToString(REPORT_JSON_OUTPUT_PATH, content), true);
509
510 // have file but no content
511 EXPECT_EQ(content.empty(), true);
512 }
513 }
514
515 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_IllegalPath, TestSize.Level1)
516 {
517 unlink(REPORT_JSON_OUTPUT_PATH);
518 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
519
520 EXPECT_NE(::ReportUnwindJson(ILLEGAL_PATH, ILLEGAL_PATH, ILLEGAL_PATH), 0);
521 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
522 EXPECT_NE(access(ILLEGAL_PATH, F_OK), 0);
523 }
524
525 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_Nullptr, TestSize.Level1)
526 {
527 unlink(REPORT_JSON_OUTPUT_PATH);
528 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
529
530 EXPECT_NE(::ReportUnwindJson(nullptr, nullptr, nullptr), 0);
531 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
532 }
533
534 HWTEST_F(HiperfLibReportTest, Test_ReportUnwindJson_Empty, 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("", "", ""), 0);
540 EXPECT_NE(access(REPORT_JSON_OUTPUT_PATH, F_OK), 0);
541 }
542 } // namespace HiPerf
543 } // namespace Developtools
544 } // namespace OHOS
545