1 /*
2 * Copyright (c) 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 #include <gtest/gtest.h>
16 #include <map>
17 #include <cstdlib>
18 #include <unistd.h>
19 #include <vector>
20 #include <cerrno>
21
22 #include "iservice_registry.h"
23 #include "manager/dump_implement.h"
24 #include "raw_param.h"
25 #include "dump_utils.h"
26
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace HiviewDFX {
30 const int SCORE_ADJ = 1000;
31 const std::string SCORE_ADJ_STR = "1000";
32 const std::string TOOL_NAME = "hidumper";
33 char g_fileName[] = "/tmp/test.XXXXXX";
34 int g_fd = -1;
35
36 const uint64_t BASELINE_SIZE = 1353 * 1024;
37 #if defined(__LP64__)
38 const std::string LIB_PATH = "lib64/";
39 #else
40 const std::string LIB_PATH = "lib/";
41 #endif
42 vector<string> OUTPUT_PATH = {
43 "/system/etc/init/hidumper_service.cfg",
44 "/system/etc/hidumper/infos_config.json",
45 "/system/bin/hidumper",
46 "/system/" + LIB_PATH + "libhidumperclient.z.so",
47 "/system/" + LIB_PATH + "libhidumper_client.z.so",
48 "/system/" + LIB_PATH + "libhidumpermemory.z.so",
49 "/system/" + LIB_PATH + "libhidumperservice.z.so",
50 "/system/" + LIB_PATH + "libhidumperservice_cpu_source.z.so",
51 "/system/" + LIB_PATH + "platformsdk/lib_dump_usage.z.so",
52 };
53 class HiDumperManagerTest : public testing::Test {
54 public:
55 static void SetUpTestCase(void);
56 static void TearDownTestCase(void);
57 void SetUp();
58 void TearDown();
59 int GetDumpResult(int argc, char *argv[]);
60 int fd_;
61 };
62
SetUpTestCase(void)63 void HiDumperManagerTest::SetUpTestCase(void)
64 {
65 g_fd = mkstemp(g_fileName);
66 if (g_fd == -1) {
67 printf("create tmp file fail! erro = %s", DumpUtils::ErrnoToMsg(errno).c_str());
68 return;
69 }
70 }
TearDownTestCase(void)71 void HiDumperManagerTest::TearDownTestCase(void)
72 {
73 if (g_fd != -1) {
74 close(g_fd);
75 g_fd = -1;
76 }
77 unlink(g_fileName);
78 }
SetUp(void)79 void HiDumperManagerTest::SetUp(void)
80 {
81 }
TearDown(void)82 void HiDumperManagerTest::TearDown(void)
83 {
84 }
85
GetDumpResult(int argc, char *argv[])86 int HiDumperManagerTest::GetDumpResult(int argc, char *argv[])
87 {
88 std::vector<std::u16string> args;
89 std::shared_ptr<RawParam> rawParam = std::make_shared<RawParam>(0, 1, 0, args, g_fd);
90 return DumpImplement::GetInstance().Main(argc, argv, rawParam);
91 }
92
93 /**
94 * @tc.name: MemoryDumperTest001
95 * @tc.desc: Test help msg has correct ret.
96 * @tc.type: FUNC
97 * @tc.require: issueI5NWZQ
98 */
HWTEST_F(HiDumperManagerTest, DumpTest001, TestSize.Level0)99 HWTEST_F(HiDumperManagerTest, DumpTest001, TestSize.Level0)
100 {
101 char *argv[] = {
102 const_cast<char *>(TOOL_NAME.c_str()),
103 const_cast<char *>("-h"),
104 const_cast<char *>(""),
105 };
106 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
107 int ret = GetDumpResult(argc, argv);
108 ASSERT_EQ(ret, DumpStatus::DUMP_HELP);
109 }
110
111 /**
112 * @tc.name: MemoryDumperTest002
113 * @tc.desc: Test list system has correct ret.
114 * @tc.type: FUNC
115 * @tc.require: issueI5NWZQ
116 */
HWTEST_F(HiDumperManagerTest, DumpTest002, TestSize.Level0)117 HWTEST_F(HiDumperManagerTest, DumpTest002, TestSize.Level0)
118 {
119 char *argv[] = {
120 const_cast<char *>(TOOL_NAME.c_str()),
121 const_cast<char *>("-lc"),
122 const_cast<char *>(""),
123 };
124 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
125 int ret = GetDumpResult(argc, argv);
126 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
127 }
128
129 /**
130 * @tc.name: MemoryDumperTest003
131 * @tc.desc: Test list aility has correct ret.
132 * @tc.type: FUNC
133 * @tc.require: issueI5NWZQ
134 */
HWTEST_F(HiDumperManagerTest, DumpTest003, TestSize.Level0)135 HWTEST_F(HiDumperManagerTest, DumpTest003, TestSize.Level0)
136 {
137 char *argv[] = {
138 const_cast<char *>(TOOL_NAME.c_str()),
139 const_cast<char *>("-ls"),
140 const_cast<char *>(""),
141 };
142 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
143 int ret = GetDumpResult(argc, argv);
144 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
145 }
146
147 /**
148 * @tc.name: MemoryDumperTest004
149 * @tc.desc: Test dump base information has correct ret.
150 * @tc.type: FUNC
151 * @tc.require: issueI5NWZQ
152 */
HWTEST_F(HiDumperManagerTest, DumpTest004, TestSize.Level0)153 HWTEST_F(HiDumperManagerTest, DumpTest004, TestSize.Level0)
154 {
155 char *argv[] = {
156 const_cast<char *>(TOOL_NAME.c_str()),
157 const_cast<char *>("-c"),
158 const_cast<char *>("base"),
159 const_cast<char *>(""),
160 };
161 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
162 int ret = GetDumpResult(argc, argv);
163 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
164 }
165
166 /**
167 * @tc.name: MemoryDumperTest005
168 * @tc.desc: Test dump system information has correct ret.
169 * @tc.type: FUNC
170 * @tc.require: issueI5NWZQ
171 */
HWTEST_F(HiDumperManagerTest, DumpTest005, TestSize.Level0)172 HWTEST_F(HiDumperManagerTest, DumpTest005, TestSize.Level0)
173 {
174 char *argv[] = {
175 const_cast<char *>(TOOL_NAME.c_str()),
176 const_cast<char *>("-c"),
177 const_cast<char *>("system"),
178 const_cast<char *>(""),
179 };
180 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
181 int ret = GetDumpResult(argc, argv);
182 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
183 }
184
185 /**
186 * @tc.name: MemoryDumperTest006
187 * @tc.desc: Test dump all system information has correct ret.
188 * @tc.type: FUNC
189 * @tc.require: issueI5NWZQ
190 */
HWTEST_F(HiDumperManagerTest, DumpTest006, TestSize.Level0)191 HWTEST_F(HiDumperManagerTest, DumpTest006, TestSize.Level0)
192 {
193 char *argv[] = {
194 const_cast<char *>(TOOL_NAME.c_str()),
195 const_cast<char *>("-c"),
196 const_cast<char *>(""),
197 };
198 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
199 int ret = GetDumpResult(argc, argv);
200 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
201 }
202
203 /**
204 * @tc.name: MemoryDumperTest007
205 * @tc.desc: Test dump all ability has correct ret.
206 * @tc.type: FUNC
207 * @tc.require: issueI5NWZQ
208 */
HWTEST_F(HiDumperManagerTest, DumpTest007, TestSize.Level0)209 HWTEST_F(HiDumperManagerTest, DumpTest007, TestSize.Level0)
210 {
211 char *argv[] = {
212 const_cast<char *>(TOOL_NAME.c_str()),
213 const_cast<char *>("-s"),
214 const_cast<char *>(""),
215 };
216 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
217 int ret = GetDumpResult(argc, argv);
218 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
219 }
220
221 /**
222 * @tc.name: MemoryDumperTest007
223 * @tc.desc: Test dump one ability has correct ret.
224 * @tc.type: FUNC
225 * @tc.require: issueI5NWZQ
226 */
HWTEST_F(HiDumperManagerTest, DumpTest008, TestSize.Level0)227 HWTEST_F(HiDumperManagerTest, DumpTest008, TestSize.Level0)
228 {
229 char *argv[] = {
230 const_cast<char *>(TOOL_NAME.c_str()),
231 const_cast<char *>("-s"),
232 const_cast<char *>("1212"),
233 const_cast<char *>(""),
234 };
235 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
236 int ret = GetDumpResult(argc, argv);
237 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
238 }
239
240 /**
241 * @tc.name: MemoryDumperTest009
242 * @tc.desc: Test dump one ability with arg has correct ret.
243 * @tc.type: FUNC
244 * @tc.require: issueI5NWZQ
245 */
HWTEST_F(HiDumperManagerTest, DumpTest009, TestSize.Level0)246 HWTEST_F(HiDumperManagerTest, DumpTest009, TestSize.Level0)
247 {
248 char *argv[] = {
249 const_cast<char *>(TOOL_NAME.c_str()),
250 const_cast<char *>("-s"),
251 const_cast<char *>("SensorService"),
252 const_cast<char *>("-a"),
253 const_cast<char *>("-h"),
254 const_cast<char *>(""),
255 };
256 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
257 int ret = GetDumpResult(argc, argv);
258 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
259 }
260
261 /**
262 * @tc.name: MemoryDumperTest010
263 * @tc.desc: Test dump faultlogs of crash history has correct ret.
264 * @tc.type: FUNC
265 * @tc.require: issueI5NWZQ
266 */
HWTEST_F(HiDumperManagerTest, DumpTest010, TestSize.Level0)267 HWTEST_F(HiDumperManagerTest, DumpTest010, TestSize.Level0)
268 {
269 char *argv[] = {
270 const_cast<char *>(TOOL_NAME.c_str()),
271 const_cast<char *>("-e"),
272 const_cast<char *>(""),
273 };
274 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
275 int ret = GetDumpResult(argc, argv);
276 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
277 }
278
279 /**
280 * @tc.name: MemoryDumperTest011
281 * @tc.desc: Test dump network information has correct ret.
282 * @tc.type: FUNC
283 * @tc.require: issueI5NWZQ
284 */
HWTEST_F(HiDumperManagerTest, DumpTest011, TestSize.Level0)285 HWTEST_F(HiDumperManagerTest, DumpTest011, TestSize.Level0)
286 {
287 char *argv[] = {
288 const_cast<char *>(TOOL_NAME.c_str()),
289 const_cast<char *>("--net"),
290 const_cast<char *>(""),
291 };
292 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
293 int ret = GetDumpResult(argc, argv);
294 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
295 }
296
297 /**
298 * @tc.name: MemoryDumperTest012
299 * @tc.desc: Test dump storage information has correct ret.
300 * @tc.type: FUNC
301 * @tc.require: issueI5NWZQ
302 */
HWTEST_F(HiDumperManagerTest, DumpTest012, TestSize.Level0)303 HWTEST_F(HiDumperManagerTest, DumpTest012, TestSize.Level0)
304 {
305 char *argv[] = {
306 const_cast<char *>(TOOL_NAME.c_str()),
307 const_cast<char *>("--storage"),
308 const_cast<char *>(""),
309 };
310 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
311 int ret = GetDumpResult(argc, argv);
312 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
313 }
314
315 /**
316 * @tc.name: MemoryDumperTest013
317 * @tc.desc: Test dump processes information has correct ret.
318 * @tc.type: FUNC
319 * @tc.require: issueI5NWZQ
320 */
HWTEST_F(HiDumperManagerTest, DumpTest013, TestSize.Level0)321 HWTEST_F(HiDumperManagerTest, DumpTest013, TestSize.Level0)
322 {
323 char *argv[] = {
324 const_cast<char *>(TOOL_NAME.c_str()),
325 const_cast<char *>("-p"),
326 const_cast<char *>(""),
327 };
328 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
329 int ret = GetDumpResult(argc, argv);
330 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
331 }
332
333 /**
334 * @tc.name: MemoryDumperTest014
335 * @tc.desc: Test dump one process information has correct ret.
336 * @tc.type: FUNC
337 * @tc.require: issueI5NWZQ
338 */
HWTEST_F(HiDumperManagerTest, DumpTest014, TestSize.Level0)339 HWTEST_F(HiDumperManagerTest, DumpTest014, TestSize.Level0)
340 {
341 char *argv[] = {
342 const_cast<char *>(TOOL_NAME.c_str()),
343 const_cast<char *>("-p"),
344 const_cast<char *>(std::to_string(getpid()).c_str()),
345 const_cast<char *>(""),
346 };
347 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
348 int ret = GetDumpResult(argc, argv);
349 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
350 }
351
352 #ifdef HIDUMPER_HIVIEWDFX_HIVIEW_ENABLE
353 /**
354 * @tc.name: MemoryDumperTest015
355 * @tc.desc: Test dump all process cpu usage has correct ret.
356 * @tc.type: FUNC
357 * @tc.require: issueI5NWZQ
358 */
HWTEST_F(HiDumperManagerTest, DumpTest015, TestSize.Level0)359 HWTEST_F(HiDumperManagerTest, DumpTest015, TestSize.Level0)
360 {
361 char *argv[] = {
362 const_cast<char *>(TOOL_NAME.c_str()),
363 const_cast<char *>("--cpuusage"),
364 const_cast<char *>(""),
365 };
366 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
367 int ret = GetDumpResult(argc, argv);
368 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
369 }
370
371 /**
372 * @tc.name: MemoryDumperTest016
373 * @tc.desc: Test dump one process cpu usage has correct ret.
374 * @tc.type: FUNC
375 * @tc.require: issueI5NWZQ
376 */
HWTEST_F(HiDumperManagerTest, DumpTest016, TestSize.Level0)377 HWTEST_F(HiDumperManagerTest, DumpTest016, TestSize.Level0)
378 {
379 char *argv[] = {
380 const_cast<char *>(TOOL_NAME.c_str()),
381 const_cast<char *>("--cpuusage"),
382 const_cast<char *>(std::to_string(getpid()).c_str()),
383 const_cast<char *>(""),
384 };
385 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
386 int ret = GetDumpResult(argc, argv);
387 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
388 }
389 #endif
390
391 /**
392 * @tc.name: MemoryDumperTest017
393 * @tc.desc: Test dumpreal CPU frequency has correct ret.
394 * @tc.type: FUNC
395 * @tc.require: issueI5NWZQ
396 */
HWTEST_F(HiDumperManagerTest, DumpTest017, TestSize.Level0)397 HWTEST_F(HiDumperManagerTest, DumpTest017, TestSize.Level0)
398 {
399 char *argv[] = {
400 const_cast<char *>(TOOL_NAME.c_str()),
401 const_cast<char *>("--cpufreq"),
402 const_cast<char *>(""),
403 };
404 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
405 int ret = GetDumpResult(argc, argv);
406 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
407 }
408
409 /**
410 * @tc.name: MemoryDumperTest018
411 * @tc.desc: Test dump all processes memory info has correct ret.
412 * @tc.type: FUNC
413 * @tc.require: issueI5NWZQ
414 */
HWTEST_F(HiDumperManagerTest, DumpTest018, TestSize.Level0)415 HWTEST_F(HiDumperManagerTest, DumpTest018, TestSize.Level0)
416 {
417 char *argv[] = {
418 const_cast<char *>(TOOL_NAME.c_str()),
419 const_cast<char *>("--mem"),
420 const_cast<char *>(""),
421 };
422 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
423 int ret = GetDumpResult(argc, argv);
424 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
425 }
426
427 /**
428 * @tc.name: MemoryDumperTest019
429 * @tc.desc: Test dump one process memory info has correct ret.
430 * @tc.type: FUNC
431 * @tc.require: issueI5NWZQ
432 */
HWTEST_F(HiDumperManagerTest, DumpTest019, TestSize.Level0)433 HWTEST_F(HiDumperManagerTest, DumpTest019, TestSize.Level0)
434 {
435 char *argv[] = {
436 const_cast<char *>(TOOL_NAME.c_str()),
437 const_cast<char *>("--mem"),
438 const_cast<char *>(std::to_string(getpid()).c_str()),
439 const_cast<char *>(""),
440 };
441 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
442 int ret = GetDumpResult(argc, argv);
443 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
444 }
445
446 /**
447 * @tc.name: MemoryDumperTest020
448 * @tc.desc: Test compress has correct ret.
449 * @tc.type: FUNC
450 * @tc.require: issueI5NWZQ
451 */
HWTEST_F(HiDumperManagerTest, DumpTest020, TestSize.Level0)452 HWTEST_F(HiDumperManagerTest, DumpTest020, TestSize.Level0)
453 {
454 char *argv[] = {
455 const_cast<char *>(TOOL_NAME.c_str()),
456 const_cast<char *>("-p"),
457 const_cast<char *>("--zip"),
458 const_cast<char *>(""),
459 };
460 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
461 std::vector<std::u16string> args;
462 std::shared_ptr<RawParam> rawParam = std::make_shared<RawParam>(0, 1, 0, args, g_fd);
463 std::string srcpath = "/data/log/hilog/";
464 rawParam->SetFolder(srcpath);
465 int ret = DumpImplement::GetInstance().Main(argc, argv, rawParam);
466 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
467 }
468
469 /**
470 * @tc.name: MemoryDumperTest023
471 * @tc.desc: Test for all infomation.
472 * @tc.type: FUNC
473 */
HWTEST_F(HiDumperManagerTest, DumpTest023, TestSize.Level0)474 HWTEST_F(HiDumperManagerTest, DumpTest023, TestSize.Level0)
475 {
476 char *argv[] = {
477 const_cast<char *>(TOOL_NAME.c_str()),
478 const_cast<char *>(""),
479 };
480 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
481 int ret = GetDumpResult(argc, argv);
482 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
483 }
484
485 /**
486 * @tc.name: MemoryDumperTest021
487 * @tc.desc: Test dump statistic in /proc/pid/smaps has correct ret.
488 * @tc.type: FUNC
489 */
HWTEST_F(HiDumperManagerTest, DumpTest021, TestSize.Level0)490 HWTEST_F(HiDumperManagerTest, DumpTest021, TestSize.Level0)
491 {
492 char *argv[] = {
493 const_cast<char *>(TOOL_NAME.c_str()),
494 const_cast<char *>("--mem-smaps"),
495 const_cast<char *>(std::to_string(getpid()).c_str()),
496 const_cast<char *>(""),
497 };
498 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
499 int ret = GetDumpResult(argc, argv);
500 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
501 }
502
503 /**
504 * @tc.name: MemoryDumperTest022
505 * @tc.desc: Test dump details in /proc/pid/smaps has correct ret.
506 * @tc.type: FUNC
507 */
HWTEST_F(HiDumperManagerTest, DumpTest022, TestSize.Level0)508 HWTEST_F(HiDumperManagerTest, DumpTest022, TestSize.Level0)
509 {
510 char *argv[] = {
511 const_cast<char *>(TOOL_NAME.c_str()),
512 const_cast<char *>("--mem-smaps"),
513 const_cast<char *>(std::to_string(getpid()).c_str()),
514 const_cast<char *>("-v"),
515 };
516 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
517 int ret = GetDumpResult(argc, argv);
518 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
519 }
520
521 /**
522 * @tc.name: MemoryDumperTest024
523 * @tc.desc: Test ? msg has correct ret.
524 * @tc.type: FUNC
525 * @tc.require: issueI5NWZQ
526 */
HWTEST_F(HiDumperManagerTest, DumpTest024, TestSize.Level0)527 HWTEST_F(HiDumperManagerTest, DumpTest024, TestSize.Level0)
528 {
529 char *argv[] = {
530 const_cast<char *>(TOOL_NAME.c_str()),
531 const_cast<char *>("-?"),
532 const_cast<char *>(""),
533 };
534 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
535 int ret = GetDumpResult(argc, argv);
536 ASSERT_EQ(ret, DumpStatus::DUMP_INVALID_ARG);
537 }
538
539 /**
540 * @tc.name: MemoryDumperTest025
541 * @tc.desc: Test ? with -vv msg has correct ret.
542 * @tc.type: FUNC
543 * @tc.require: issueI5NWZQ
544 */
HWTEST_F(HiDumperManagerTest, DumpTest025, TestSize.Level0)545 HWTEST_F(HiDumperManagerTest, DumpTest025, TestSize.Level0)
546 {
547 char *argv[] = {
548 const_cast<char *>(TOOL_NAME.c_str()),
549 const_cast<char *>("-?"),
550 const_cast<char *>("-v"),
551 };
552 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
553 int ret = GetDumpResult(argc, argv);
554 ASSERT_EQ(ret, DumpStatus::DUMP_INVALID_ARG);
555 }
556
557 /**
558 * @tc.name: MemoryDumperTest026
559 * @tc.desc: Test cpufreq with ? msg has correct ret.
560 * @tc.type: FUNC
561 * @tc.require: issueI5NWZQ
562 */
HWTEST_F(HiDumperManagerTest, DumpTest026, TestSize.Level0)563 HWTEST_F(HiDumperManagerTest, DumpTest026, TestSize.Level0)
564 {
565 char *argv[] = {
566 const_cast<char *>(TOOL_NAME.c_str()),
567 const_cast<char *>("--cpufreq"),
568 const_cast<char *>("-?"),
569 };
570 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
571 int ret = GetDumpResult(argc, argv);
572 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
573 }
574
575 /**
576 * @tc.name: MemoryDumperTest027
577 * @tc.desc: Test error msg has correct ret.
578 * @tc.type: FUNC
579 * @tc.require: issueI5NWZQ
580 */
HWTEST_F(HiDumperManagerTest, DumpTest027, TestSize.Level0)581 HWTEST_F(HiDumperManagerTest, DumpTest027, TestSize.Level0)
582 {
583 char *argv[] = {
584 const_cast<char *>(TOOL_NAME.c_str()),
585 const_cast<char *>("--error"),
586 const_cast<char *>(""),
587 };
588 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
589 int ret = GetDumpResult(argc, argv);
590 ASSERT_EQ(ret, DumpStatus::DUMP_INVALID_ARG);
591 }
592
593 /**
594 * @tc.name: MemoryDumperTest028
595 * @tc.desc: Test dump error process memory info has correct ret.
596 * @tc.type: FUNC
597 * @tc.require: issueI5NWZQ
598 */
HWTEST_F(HiDumperManagerTest, DumpTest028, TestSize.Level0)599 HWTEST_F(HiDumperManagerTest, DumpTest028, TestSize.Level0)
600 {
601 char *argv[] = {
602 const_cast<char *>(TOOL_NAME.c_str()),
603 const_cast<char *>("--mem"),
604 const_cast<char *>("-1"),
605 const_cast<char *>(""),
606 };
607 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
608 int ret = GetDumpResult(argc, argv);
609 ASSERT_EQ(ret, DumpStatus::DUMP_INVALID_ARG);
610 }
611
612 /**
613 * @tc.name: MemoryDumperTest029
614 * @tc.desc: Test dump statistic in /proc/pid/smaps has correct ret.
615 * @tc.type: FUNC
616 */
HWTEST_F(HiDumperManagerTest, DumpTest029, TestSize.Level0)617 HWTEST_F(HiDumperManagerTest, DumpTest029, TestSize.Level0)
618 {
619 char *argv[] = {
620 const_cast<char *>(TOOL_NAME.c_str()),
621 const_cast<char *>("--mem-jsheap"),
622 const_cast<char *>(std::to_string(getpid()).c_str()),
623 const_cast<char *>(""),
624 };
625 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
626 int ret = GetDumpResult(argc, argv);
627 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
628 }
629
630 /**
631 * @tc.name: MemoryDumperTest030
632 * @tc.desc: Test dump statistic in /proc/pid/smaps has error ret.
633 * @tc.type: FUNC
634 */
HWTEST_F(HiDumperManagerTest, DumpTest030, TestSize.Level0)635 HWTEST_F(HiDumperManagerTest, DumpTest030, TestSize.Level0)
636 {
637 char *argv[] = {
638 const_cast<char *>(TOOL_NAME.c_str()),
639 const_cast<char *>("--mem-smaps"),
640 const_cast<char *>(""),
641 };
642 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
643 int ret = GetDumpResult(argc, argv);
644 ASSERT_EQ(ret, DumpStatus::DUMP_FAIL);
645 }
646
647 /**
648 * @tc.name: DumpTest031
649 * @tc.desc: Test too many arguments.
650 * @tc.type: FUNC
651 */
HWTEST_F(HiDumperManagerTest, DumpTest031, TestSize.Level0)652 HWTEST_F(HiDumperManagerTest, DumpTest031, TestSize.Level0)
653 {
654 char *argv[] = {
655 const_cast<char *>(TOOL_NAME.c_str()),
656 };
657 int argc = ARG_MAX_COUNT + 1;
658 int ret = GetDumpResult(argc, argv);
659 ASSERT_EQ(ret, DumpStatus::DUMP_FAIL);
660 }
661
662 /**
663 * @tc.name: DumpTest032
664 * @tc.desc: Test empty argument.
665 * @tc.type: FUNC
666 */
HWTEST_F(HiDumperManagerTest, DumpTest032, TestSize.Level0)667 HWTEST_F(HiDumperManagerTest, DumpTest032, TestSize.Level0)
668 {
669 char *argv[] = {
670 const_cast<char *>(TOOL_NAME.c_str()),
671 const_cast<char *>("--mem"),
672 const_cast<char *>("-1"),
673 const_cast<char *>(""),
674 const_cast<char *>("--mem"),
675 };
676 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
677 int ret = GetDumpResult(argc, argv);
678 ASSERT_EQ(ret, DumpStatus::DUMP_FAIL);
679 }
680
681 /**
682 * @tc.name: DumpTest033
683 * @tc.desc: Test too long argument .
684 * @tc.type: FUNC
685 */
HWTEST_F(HiDumperManagerTest, DumpTest033, TestSize.Level0)686 HWTEST_F(HiDumperManagerTest, DumpTest033, TestSize.Level0)
687 {
688 std::string longArg;
689 longArg.assign(SINGLE_ARG_MAXLEN + 1, 'c');
690 char *argv[] = {
691 const_cast<char *>(TOOL_NAME.c_str()),
692 const_cast<char *>("-h"),
693 const_cast<char *>(longArg.c_str()),
694 };
695 int argc = sizeof(argv) / sizeof(argv[0]);
696 int ret = GetDumpResult(argc, argv);
697 ASSERT_EQ(ret, DumpStatus::DUMP_FAIL);
698 }
699
700 /**
701 * @tc.name: DumpTest034
702 * @tc.desc: Test null argument.
703 * @tc.type: FUNC
704 */
HWTEST_F(HiDumperManagerTest, DumpTest034, TestSize.Level0)705 HWTEST_F(HiDumperManagerTest, DumpTest034, TestSize.Level0)
706 {
707 char *argv[] = {
708 const_cast<char *>(TOOL_NAME.c_str()),
709 nullptr,
710 };
711 int argc = sizeof(argv) / sizeof(argv[0]);
712 int ret = GetDumpResult(argc, argv);
713 ASSERT_EQ(ret, DumpStatus::DUMP_FAIL);
714 }
715
716 /**
717 * @tc.name: DumpTest035
718 * @tc.desc: Test DumpUtils
719 * @tc.type: FUNC
720 */
HWTEST_F(HiDumperManagerTest, DumpTest035, TestSize.Level0)721 HWTEST_F(HiDumperManagerTest, DumpTest035, TestSize.Level0)
722 {
723 DumpUtils::SetAdj(SCORE_ADJ);
724 std::string name = DumpUtils::ConvertSaIdToSaName(SCORE_ADJ_STR);
725 ASSERT_TRUE(name == SCORE_ADJ_STR);
726 }
727
728 /**
729 * @tc.name: IpcStatDumpTest001
730 * @tc.desc: hidumper --ipc -a --start-stat
731 * @tc.type: FUNC
732 */
HWTEST_F(HiDumperManagerTest, IpcStatDumpTest001, TestSize.Level0)733 HWTEST_F(HiDumperManagerTest, IpcStatDumpTest001, TestSize.Level0)
734 {
735 char *argv[] = {
736 const_cast<char *>(TOOL_NAME.c_str()),
737 const_cast<char *>("--ipc"),
738 const_cast<char *>("-a"),
739 const_cast<char *>("--start-stat"),
740 };
741 int argc = sizeof(argv) / sizeof(argv[0]);
742 int ret = GetDumpResult(argc, argv);
743 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
744 }
745
746 /**
747 * @tc.name: IpcStatDumpTest002
748 * @tc.desc: hidumper --ipc -a --stop-stat
749 * @tc.type: FUNC
750 */
HWTEST_F(HiDumperManagerTest, IpcStatDumpTest002, TestSize.Level0)751 HWTEST_F(HiDumperManagerTest, IpcStatDumpTest002, TestSize.Level0)
752 {
753 char *argv[] = {
754 const_cast<char *>(TOOL_NAME.c_str()),
755 const_cast<char *>("--ipc"),
756 const_cast<char *>("-a"),
757 const_cast<char *>("--stop-stat"),
758 };
759 int argc = sizeof(argv) / sizeof(argv[0]);
760 int ret = GetDumpResult(argc, argv);
761 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
762 }
763
764 /**
765 * @tc.name: IpcStatDumpTest003
766 * @tc.desc: hidumper --ipc -a --stat
767 * @tc.type: FUNC
768 */
HWTEST_F(HiDumperManagerTest, IpcStatDumpTest003, TestSize.Level0)769 HWTEST_F(HiDumperManagerTest, IpcStatDumpTest003, TestSize.Level0)
770 {
771 char *argv[] = {
772 const_cast<char *>(TOOL_NAME.c_str()),
773 const_cast<char *>("--ipc"),
774 const_cast<char *>("-a"),
775 const_cast<char *>("--stat"),
776 };
777 int argc = sizeof(argv) / sizeof(argv[0]);
778 int ret = GetDumpResult(argc, argv);
779 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
780 }
781
782 /**
783 * @tc.name: IpcStatDumpTest004
784 * @tc.desc: hidumper --ipc
785 * @tc.type: FUNC
786 */
HWTEST_F(HiDumperManagerTest, IpcStatDumpTest004, TestSize.Level0)787 HWTEST_F(HiDumperManagerTest, IpcStatDumpTest004, TestSize.Level0)
788 {
789 char *argv[] = {
790 const_cast<char *>(TOOL_NAME.c_str()),
791 const_cast<char *>("--ipc"),
792 };
793 int argc = sizeof(argv) / sizeof(argv[0]);
794 int ret = GetDumpResult(argc, argv);
795 ASSERT_TRUE(ret != DumpStatus::DUMP_OK);
796 }
797
798 /**
799 * @tc.name: IpcStatDumpTest005
800 * @tc.desc: hidumper --ipc -a
801 * @tc.type: FUNC
802 */
HWTEST_F(HiDumperManagerTest, IpcStatDumpTest005, TestSize.Level0)803 HWTEST_F(HiDumperManagerTest, IpcStatDumpTest005, TestSize.Level0)
804 {
805 char *argv[] = {
806 const_cast<char *>(TOOL_NAME.c_str()),
807 const_cast<char *>("--ipc"),
808 const_cast<char *>("-a"),
809 };
810 int argc = sizeof(argv) / sizeof(argv[0]);
811 int ret = GetDumpResult(argc, argv);
812 ASSERT_TRUE(ret != DumpStatus::DUMP_OK);
813 }
814
815 /**
816 * @tc.name: IpcStatDumpTest006
817 * @tc.desc: hidumper --ipc -a --start-stat --stop-stat
818 * @tc.type: FUNC
819 */
HWTEST_F(HiDumperManagerTest, IpcStatDumpTest006, TestSize.Level0)820 HWTEST_F(HiDumperManagerTest, IpcStatDumpTest006, TestSize.Level0)
821 {
822 char *argv[] = {
823 const_cast<char *>(TOOL_NAME.c_str()),
824 const_cast<char *>("--ipc"),
825 const_cast<char *>("-a"),
826 const_cast<char *>("--start-stat"),
827 const_cast<char *>("--stop-stat"),
828 };
829 int argc = sizeof(argv) / sizeof(argv[0]);
830 int ret = GetDumpResult(argc, argv);
831 ASSERT_TRUE(ret != DumpStatus::DUMP_OK);
832 }
833
834 /**
835 * @tc.name: IpcStatDumpTest007
836 * @tc.desc: hidumper --start-stat
837 * @tc.type: FUNC
838 */
HWTEST_F(HiDumperManagerTest, IpcStatDumpTest007, TestSize.Level0)839 HWTEST_F(HiDumperManagerTest, IpcStatDumpTest007, TestSize.Level0)
840 {
841 char *argv[] = {
842 const_cast<char *>(TOOL_NAME.c_str()),
843 const_cast<char *>("--start-stat"),
844 };
845 int argc = sizeof(argv) / sizeof(argv[0]);
846 int ret = GetDumpResult(argc, argv);
847 ASSERT_TRUE(ret != DumpStatus::DUMP_OK);
848 }
849
850 /**
851 * @tc.name: IpcStatDumpTest008
852 * @tc.desc: hidumper --stop-stat
853 * @tc.type: FUNC
854 */
HWTEST_F(HiDumperManagerTest, IpcStatDumpTest008, TestSize.Level0)855 HWTEST_F(HiDumperManagerTest, IpcStatDumpTest008, TestSize.Level0)
856 {
857 char *argv[] = {
858 const_cast<char *>(TOOL_NAME.c_str()),
859 const_cast<char *>("--stop-stat"),
860 };
861 int argc = sizeof(argv) / sizeof(argv[0]);
862 int ret = GetDumpResult(argc, argv);
863 ASSERT_TRUE(ret != DumpStatus::DUMP_OK);
864 }
865
866 /**
867 * @tc.name: IpcStatDumpTest009
868 * @tc.desc: hidumper --stat
869 * @tc.type: FUNC
870 */
HWTEST_F(HiDumperManagerTest, IpcStatDumpTest009, TestSize.Level0)871 HWTEST_F(HiDumperManagerTest, IpcStatDumpTest009, TestSize.Level0)
872 {
873 char *argv[] = {
874 const_cast<char *>(TOOL_NAME.c_str()),
875 const_cast<char *>("--stat"),
876 };
877 int argc = sizeof(argv) / sizeof(argv[0]);
878 int ret = GetDumpResult(argc, argv);
879 ASSERT_TRUE(ret != DumpStatus::DUMP_OK);
880 }
881
882 /**
883 * @tc.name: IpcStatDumpTest010
884 * @tc.desc: hidumper --ipc -1 --start-stat
885 * @tc.type: FUNC
886 */
HWTEST_F(HiDumperManagerTest, IpcStatDumpTest010, TestSize.Level0)887 HWTEST_F(HiDumperManagerTest, IpcStatDumpTest010, TestSize.Level0)
888 {
889 char *argv[] = {
890 const_cast<char *>(TOOL_NAME.c_str()),
891 const_cast<char *>("--ipc"),
892 const_cast<char *>("-1"),
893 const_cast<char *>("--start-stat"),
894 };
895 int argc = sizeof(argv) / sizeof(argv[0]);
896 int ret = GetDumpResult(argc, argv);
897 ASSERT_TRUE(ret != DumpStatus::DUMP_OK);
898 }
899
900 /**
901 * @tc.name: HidumperRomTest001
902 * @tc.desc: test hidumper rom
903 * @tc.type: FUNC
904 */
HWTEST_F(HiDumperManagerTest, HidumperRomTest001, TestSize.Level0)905 HWTEST_F(HiDumperManagerTest, HidumperRomTest001, TestSize.Level0)
906 {
907 uint64_t realSize = 0;
908 for (int i = 0; i < OUTPUT_PATH.size(); i++) {
909 struct stat info = { 0 };
910 stat(OUTPUT_PATH[i].c_str(), &info);
911 std::cout << "path:" << OUTPUT_PATH[i].c_str() << " realSize: " << info.st_size << std::endl;
912 realSize += static_cast<uint64_t>(info.st_size);
913 }
914 std::cout << "total realSize = " << realSize << std::endl;
915 EXPECT_LT(realSize, BASELINE_SIZE * 1.05);
916 }
917 } // namespace HiviewDFX
918 } // namespace OHOS
919