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 <stdlib.h>
18 #include <unistd.h>
19 #include <vector>
20
21 #define private public
22 #include "iservice_registry.h"
23 #include "manager/dump_implement.h"
24 #include "raw_param.h"
25 #undef private
26
27 using namespace testing::ext;
28 namespace OHOS {
29 namespace HiviewDFX {
30
31 const std::string TOOL_NAME = "hidumper";
32 char FILE_NAME[] = "/tmp/test.XXXXXX";
33 int g_fd = -1;
34 class HiDumperManagerTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
38 void SetUp();
39 void TearDown();
40 int GetDumpResult(int argc, char *argv[]);
41 int fd_;
42 };
43
SetUpTestCase(void)44 void HiDumperManagerTest::SetUpTestCase(void)
45 {
46 g_fd = mkstemp(FILE_NAME);
47 if (g_fd == -1) {
48 printf("create tmp file fail!");
49 return;
50 }
51 }
TearDownTestCase(void)52 void HiDumperManagerTest::TearDownTestCase(void)
53 {
54 if (g_fd != -1) {
55 close(g_fd);
56 }
57 unlink(FILE_NAME);
58 }
SetUp(void)59 void HiDumperManagerTest::SetUp(void)
60 {
61 }
TearDown(void)62 void HiDumperManagerTest::TearDown(void)
63 {
64 }
65
GetDumpResult(int argc,char * argv[])66 int HiDumperManagerTest::GetDumpResult(int argc, char *argv[])
67 {
68 std::vector<std::u16string> args;
69 std::shared_ptr<RawParam> rawParam = std::make_shared<RawParam>(0, 1, 0, args, g_fd);
70 return DumpImplement::GetInstance().Main(argc, argv, rawParam);
71 }
72
73 /**
74 * @tc.name: MemoryDumperTest001
75 * @tc.desc: Test help msg has correct ret.
76 * @tc.type: FUNC
77 * @tc.require: issueI5NWZQ
78 */
79 HWTEST_F(HiDumperManagerTest, DumpTest001, TestSize.Level0)
80 {
81 char *argv[] = {
82 const_cast<char *>(TOOL_NAME.c_str()),
83 const_cast<char *>("-h"),
84 const_cast<char *>(""),
85 };
86 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
87 int ret = GetDumpResult(argc, argv);
88 ASSERT_EQ(ret, DumpStatus::DUMP_HELP);
89 }
90
91 /**
92 * @tc.name: MemoryDumperTest002
93 * @tc.desc: Test list system has correct ret.
94 * @tc.type: FUNC
95 * @tc.require: issueI5NWZQ
96 */
97 HWTEST_F(HiDumperManagerTest, DumpTest002, TestSize.Level0)
98 {
99 char *argv[] = {
100 const_cast<char *>(TOOL_NAME.c_str()),
101 const_cast<char *>("-lc"),
102 const_cast<char *>(""),
103 };
104 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
105 int ret = GetDumpResult(argc, argv);
106 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
107 }
108
109 /**
110 * @tc.name: MemoryDumperTest003
111 * @tc.desc: Test list aility has correct ret.
112 * @tc.type: FUNC
113 * @tc.require: issueI5NWZQ
114 */
115 HWTEST_F(HiDumperManagerTest, DumpTest003, TestSize.Level0)
116 {
117 char *argv[] = {
118 const_cast<char *>(TOOL_NAME.c_str()),
119 const_cast<char *>("-ls"),
120 const_cast<char *>(""),
121 };
122 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
123 int ret = GetDumpResult(argc, argv);
124 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
125 }
126
127 /**
128 * @tc.name: MemoryDumperTest004
129 * @tc.desc: Test dump base information has correct ret.
130 * @tc.type: FUNC
131 * @tc.require: issueI5NWZQ
132 */
133 HWTEST_F(HiDumperManagerTest, DumpTest004, TestSize.Level0)
134 {
135 char *argv[] = {
136 const_cast<char *>(TOOL_NAME.c_str()),
137 const_cast<char *>("-c"),
138 const_cast<char *>("base"),
139 const_cast<char *>(""),
140 };
141 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
142 int ret = GetDumpResult(argc, argv);
143 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
144 }
145
146 /**
147 * @tc.name: MemoryDumperTest005
148 * @tc.desc: Test dump system information has correct ret.
149 * @tc.type: FUNC
150 * @tc.require: issueI5NWZQ
151 */
152 HWTEST_F(HiDumperManagerTest, DumpTest005, TestSize.Level0)
153 {
154 char *argv[] = {
155 const_cast<char *>(TOOL_NAME.c_str()),
156 const_cast<char *>("-c"),
157 const_cast<char *>("system"),
158 const_cast<char *>(""),
159 };
160 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
161 int ret = GetDumpResult(argc, argv);
162 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
163 }
164
165 /**
166 * @tc.name: MemoryDumperTest006
167 * @tc.desc: Test dump all system information has correct ret.
168 * @tc.type: FUNC
169 * @tc.require: issueI5NWZQ
170 */
171 HWTEST_F(HiDumperManagerTest, DumpTest006, TestSize.Level0)
172 {
173 char *argv[] = {
174 const_cast<char *>(TOOL_NAME.c_str()),
175 const_cast<char *>("-c"),
176 const_cast<char *>(""),
177 };
178 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
179 int ret = GetDumpResult(argc, argv);
180 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
181 }
182
183 /**
184 * @tc.name: MemoryDumperTest007
185 * @tc.desc: Test dump all ability has correct ret.
186 * @tc.type: FUNC
187 * @tc.require: issueI5NWZQ
188 */
189 HWTEST_F(HiDumperManagerTest, DumpTest007, TestSize.Level0)
190 {
191 char *argv[] = {
192 const_cast<char *>(TOOL_NAME.c_str()),
193 const_cast<char *>("-s"),
194 const_cast<char *>(""),
195 };
196 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
197 int ret = GetDumpResult(argc, argv);
198 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
199 }
200
201 /**
202 * @tc.name: MemoryDumperTest007
203 * @tc.desc: Test dump one ability has correct ret.
204 * @tc.type: FUNC
205 * @tc.require: issueI5NWZQ
206 */
207 HWTEST_F(HiDumperManagerTest, DumpTest008, TestSize.Level0)
208 {
209 char *argv[] = {
210 const_cast<char *>(TOOL_NAME.c_str()),
211 const_cast<char *>("-s"),
212 const_cast<char *>("1212"),
213 const_cast<char *>(""),
214 };
215 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
216 int ret = GetDumpResult(argc, argv);
217 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
218 }
219
220 /**
221 * @tc.name: MemoryDumperTest009
222 * @tc.desc: Test dump one ability with arg has correct ret.
223 * @tc.type: FUNC
224 * @tc.require: issueI5NWZQ
225 */
226 HWTEST_F(HiDumperManagerTest, DumpTest009, TestSize.Level0)
227 {
228 char *argv[] = {
229 const_cast<char *>(TOOL_NAME.c_str()),
230 const_cast<char *>("-s"),
231 const_cast<char *>("SensorService"),
232 const_cast<char *>("-a"),
233 const_cast<char *>("-h"),
234 const_cast<char *>(""),
235 };
236 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
237 int ret = GetDumpResult(argc, argv);
238 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
239 }
240
241 /**
242 * @tc.name: MemoryDumperTest010
243 * @tc.desc: Test dump faultlogs of crash history has correct ret.
244 * @tc.type: FUNC
245 * @tc.require: issueI5NWZQ
246 */
247 HWTEST_F(HiDumperManagerTest, DumpTest010, TestSize.Level0)
248 {
249 char *argv[] = {
250 const_cast<char *>(TOOL_NAME.c_str()),
251 const_cast<char *>("-e"),
252 const_cast<char *>(""),
253 };
254 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
255 int ret = GetDumpResult(argc, argv);
256 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
257 }
258
259 /**
260 * @tc.name: MemoryDumperTest011
261 * @tc.desc: Test dump network information has correct ret.
262 * @tc.type: FUNC
263 * @tc.require: issueI5NWZQ
264 */
265 HWTEST_F(HiDumperManagerTest, DumpTest011, TestSize.Level0)
266 {
267 char *argv[] = {
268 const_cast<char *>(TOOL_NAME.c_str()),
269 const_cast<char *>("--net"),
270 const_cast<char *>(""),
271 };
272 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
273 int ret = GetDumpResult(argc, argv);
274 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
275 }
276
277 /**
278 * @tc.name: MemoryDumperTest012
279 * @tc.desc: Test dump storage information has correct ret.
280 * @tc.type: FUNC
281 * @tc.require: issueI5NWZQ
282 */
283 HWTEST_F(HiDumperManagerTest, DumpTest012, TestSize.Level0)
284 {
285 char *argv[] = {
286 const_cast<char *>(TOOL_NAME.c_str()),
287 const_cast<char *>("--storage"),
288 const_cast<char *>(""),
289 };
290 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
291 int ret = GetDumpResult(argc, argv);
292 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
293 }
294
295 /**
296 * @tc.name: MemoryDumperTest013
297 * @tc.desc: Test dump processes information has correct ret.
298 * @tc.type: FUNC
299 * @tc.require: issueI5NWZQ
300 */
301 HWTEST_F(HiDumperManagerTest, DumpTest013, TestSize.Level0)
302 {
303 char *argv[] = {
304 const_cast<char *>(TOOL_NAME.c_str()),
305 const_cast<char *>("-p"),
306 const_cast<char *>(""),
307 };
308 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
309 int ret = GetDumpResult(argc, argv);
310 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
311 }
312
313 /**
314 * @tc.name: MemoryDumperTest014
315 * @tc.desc: Test dump one process information has correct ret.
316 * @tc.type: FUNC
317 * @tc.require: issueI5NWZQ
318 */
319 HWTEST_F(HiDumperManagerTest, DumpTest014, TestSize.Level0)
320 {
321 char *argv[] = {
322 const_cast<char *>(TOOL_NAME.c_str()),
323 const_cast<char *>("-p"),
324 const_cast<char *>(std::to_string(getpid()).c_str()),
325 const_cast<char *>(""),
326 };
327 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
328 int ret = GetDumpResult(argc, argv);
329 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
330 }
331
332 /**
333 * @tc.name: MemoryDumperTest015
334 * @tc.desc: Test dump all process cpu usage has correct ret.
335 * @tc.type: FUNC
336 * @tc.require: issueI5NWZQ
337 */
338 HWTEST_F(HiDumperManagerTest, DumpTest015, TestSize.Level0)
339 {
340 char *argv[] = {
341 const_cast<char *>(TOOL_NAME.c_str()),
342 const_cast<char *>("--cpuusage"),
343 const_cast<char *>(""),
344 };
345 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
346 int ret = GetDumpResult(argc, argv);
347 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
348 }
349
350 /**
351 * @tc.name: MemoryDumperTest016
352 * @tc.desc: Test dump one process cpu usage has correct ret.
353 * @tc.type: FUNC
354 * @tc.require: issueI5NWZQ
355 */
356 HWTEST_F(HiDumperManagerTest, DumpTest016, TestSize.Level0)
357 {
358 char *argv[] = {
359 const_cast<char *>(TOOL_NAME.c_str()),
360 const_cast<char *>("--cpuusage"),
361 const_cast<char *>(std::to_string(getpid()).c_str()),
362 const_cast<char *>(""),
363 };
364 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
365 int ret = GetDumpResult(argc, argv);
366 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
367 }
368
369 /**
370 * @tc.name: MemoryDumperTest017
371 * @tc.desc: Test dumpreal CPU frequency has correct ret.
372 * @tc.type: FUNC
373 * @tc.require: issueI5NWZQ
374 */
375 HWTEST_F(HiDumperManagerTest, DumpTest017, TestSize.Level0)
376 {
377 char *argv[] = {
378 const_cast<char *>(TOOL_NAME.c_str()),
379 const_cast<char *>("--cpufreq"),
380 const_cast<char *>(""),
381 };
382 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
383 int ret = GetDumpResult(argc, argv);
384 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
385 }
386
387 /**
388 * @tc.name: MemoryDumperTest018
389 * @tc.desc: Test dump all processes memory info has correct ret.
390 * @tc.type: FUNC
391 * @tc.require: issueI5NWZQ
392 */
393 HWTEST_F(HiDumperManagerTest, DumpTest018, TestSize.Level0)
394 {
395 char *argv[] = {
396 const_cast<char *>(TOOL_NAME.c_str()),
397 const_cast<char *>("--mem"),
398 const_cast<char *>(""),
399 };
400 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
401 int ret = GetDumpResult(argc, argv);
402 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
403 }
404
405 /**
406 * @tc.name: MemoryDumperTest019
407 * @tc.desc: Test dump one process memory info has correct ret.
408 * @tc.type: FUNC
409 * @tc.require: issueI5NWZQ
410 */
411 HWTEST_F(HiDumperManagerTest, DumpTest019, TestSize.Level0)
412 {
413 char *argv[] = {
414 const_cast<char *>(TOOL_NAME.c_str()),
415 const_cast<char *>("--mem"),
416 const_cast<char *>(std::to_string(getpid()).c_str()),
417 const_cast<char *>(""),
418 };
419 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
420 int ret = GetDumpResult(argc, argv);
421 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
422 }
423
424 /**
425 * @tc.name: MemoryDumperTest019
426 * @tc.desc: Test compress has correct ret.
427 * @tc.type: FUNC
428 * @tc.require: issueI5NWZQ
429 */
430 HWTEST_F(HiDumperManagerTest, DumpTest020, TestSize.Level0)
431 {
432 char *argv[] = {
433 const_cast<char *>(TOOL_NAME.c_str()),
434 const_cast<char *>("-p"),
435 const_cast<char *>("--zip"),
436 const_cast<char *>(""),
437 };
438 int argc = sizeof(argv) / sizeof(argv[0]) - 1;
439 int ret = GetDumpResult(argc, argv);
440 ASSERT_EQ(ret, DumpStatus::DUMP_OK);
441 }
442 } // namespace HiviewDFX
443 } // namespace OHOS
444