1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 
18 #include <string>
19 #include <unistd.h>
20 
21 #include "dfx_define.h"
22 #include "dfx_dump_catcher.h"
23 #include "dfx_test_util.h"
24 
25 using namespace testing::ext;
26 using namespace std;
27 
28 namespace OHOS {
29 namespace HiviewDFX {
30 namespace {
31 static int g_testPid = 0;
32 }
33 class DumpCatcherCommandTest : public testing::Test {
34 public:
35     static void SetUpTestCase();
36     static void TearDownTestCase();
37     void SetUp();
38     void TearDown();
39 };
40 
SetUpTestCase()41 void DumpCatcherCommandTest::SetUpTestCase()
42 {
43     InstallTestHap("/data/FaultloggerdJsTest.hap");
44     string testBundleName = TEST_BUNDLE_NAME;
45     string testAbiltyName = testBundleName + ".MainAbility";
46     g_testPid = LaunchTestHap(testAbiltyName, testBundleName);
47 }
48 
TearDownTestCase()49 void DumpCatcherCommandTest::TearDownTestCase()
50 {
51     StopTestHap(TEST_BUNDLE_NAME);
52     UninstallTestHap(TEST_BUNDLE_NAME);
53 }
54 
SetUp()55 void DumpCatcherCommandTest::SetUp()
56 {}
57 
TearDown()58 void DumpCatcherCommandTest::TearDown()
59 {}
60 
61 /**
62  * @tc.name: DumpCatcherCommandTest001
63  * @tc.desc: test dumpcatcher command: -p [accountmgr]
64  * @tc.type: FUNC
65  */
66 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest001, TestSize.Level2)
67 {
68     GTEST_LOG_(INFO) << "DumpCatcherCommandTest001: start.";
69     int testPid = GetProcessPid("accountmgr");
70     string testCommand = "dumpcatcher -p " + to_string(testPid);
71     string dumpRes = ExecuteCommands(testCommand);
72     GTEST_LOG_(INFO) << dumpRes;
73     string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
74     log[0] = log[0] + to_string(testPid);
75     log[1] = log[1] + "accountmgr";
76     int len = sizeof(log) / sizeof(log[0]);
77     int count = GetKeywordsNum(dumpRes, log, len);
78     EXPECT_EQ(count, len) << "DumpCatcherCommandTest001 Failed";
79     GTEST_LOG_(INFO) << "DumpCatcherCommandTest001: end.";
80 }
81 
82 /**
83  * @tc.name: DumpCatcherCommandTest002
84  * @tc.desc: test dumpcatcher command: -p [accountmgr] -t [main thread]
85  * @tc.type: FUNC
86  */
87 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest002, TestSize.Level2)
88 {
89     GTEST_LOG_(INFO) << "DumpCatcherCommandTest002: start.";
90     int testPid = GetProcessPid("accountmgr");
91     string testCommand = "dumpcatcher -p " + to_string(testPid) + " -t " + to_string(testPid);
92     string dumpRes = ExecuteCommands(testCommand);
93     GTEST_LOG_(INFO) << dumpRes;
94     string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
95     log[0] = log[0] + to_string(testPid);
96     log[1] = log[1] + "accountmgr";
97     int len = sizeof(log) / sizeof(log[0]);
98     int count = GetKeywordsNum(dumpRes, log, len);
99     EXPECT_EQ(count, len) << "DumpCatcherCommandTest002 Failed";
100     GTEST_LOG_(INFO) << "DumpCatcherCommandTest002: end.";
101 }
102 
103 /**
104  * @tc.name: DumpCatcherCommandTest003
105  * @tc.desc: test dumpcatcher command: -p [test hap]
106  * @tc.type: FUNC
107  */
108 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest003, TestSize.Level2)
109 {
110     GTEST_LOG_(INFO) << "DumpCatcherCommandTest003: start.";
111     if (g_testPid == 0) {
112         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
113         return;
114     }
115     if (!CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
116         GTEST_LOG_(ERROR) << "Error process comm";
117         return;
118     }
119     string testCommand = "dumpcatcher -p " + to_string(g_testPid);
120     string dumpRes = ExecuteCommands(testCommand);
121     GTEST_LOG_(INFO) << dumpRes;
122     string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
123     log[0] = log[0] + to_string(g_testPid);
124     log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME;
125     int len = sizeof(log) / sizeof(log[0]);
126     int count = GetKeywordsNum(dumpRes, log, len);
127     EXPECT_EQ(count, len) << "DumpCatcherCommandTest003 Failed";
128     GTEST_LOG_(INFO) << "DumpCatcherCommandTest003: end.";
129 }
130 
131 /**
132  * @tc.name: DumpCatcherCommandTest004
133  * @tc.desc: test dumpcatcher command: -p [test hap] -t [main thread]
134  * @tc.type: FUNC
135  */
136 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest004, TestSize.Level2)
137 {
138     GTEST_LOG_(INFO) << "DumpCatcherCommandTest004: start.";
139     if (g_testPid == 0) {
140         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
141         return;
142     }
143     if (!CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
144         GTEST_LOG_(ERROR) << "Error process comm";
145         return;
146     }
147     string testCommand = "dumpcatcher -p " + to_string(g_testPid) + " -t " + to_string(g_testPid);
148     string dumpRes = ExecuteCommands(testCommand);
149     GTEST_LOG_(INFO) << dumpRes;
150     string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
151     log[0] = log[0] + to_string(g_testPid);
152     log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME;
153     int len = sizeof(log) / sizeof(log[0]);
154     int count = GetKeywordsNum(dumpRes, log, len);
155     EXPECT_EQ(count, len) << "DumpCatcherCommandTest004 Failed";
156     GTEST_LOG_(INFO) << "DumpCatcherCommandTest004: end.";
157 }
158 
159 /**
160  * @tc.name: DumpCatcherCommandTest005
161  * @tc.desc: test dumpcatcher command: -p [test hap]
162  * @tc.type: FUNC
163  */
164 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest005, TestSize.Level2)
165 {
166     GTEST_LOG_(INFO) << "DumpCatcherCommandTest005: start.";
167     if (g_testPid == 0) {
168         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
169         return;
170     }
171     if (!CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
172         GTEST_LOG_(ERROR) << "Error process comm";
173         return;
174     }
175     string testCommand = "dumpcatcher -p " + to_string(g_testPid);
176     string dumpRes = ExecuteCommands(testCommand);
177     GTEST_LOG_(INFO) << dumpRes;
178     string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
179     log[0] = log[0] + to_string(g_testPid);
180     log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME;
181     int len = sizeof(log) / sizeof(log[0]);
182     int count = GetKeywordsNum(dumpRes, log, len);
183     EXPECT_EQ(count, len) << "DumpCatcherCommandTest005 Failed";
184     GTEST_LOG_(INFO) << "DumpCatcherCommandTest005: end.";
185 }
186 
187 /**
188  * @tc.name: DumpCatcherCommandTest006
189  * @tc.desc: test dumpcatcher command: -m -p [test hap]
190  * @tc.type: FUNC
191  * @tc.require: issueI5PJ9O
192  */
193 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest006, TestSize.Level2)
194 {
195     GTEST_LOG_(INFO) << "DumpCatcherCommandTest006: start.";
196     string testBundleName = TEST_BUNDLE_NAME;
197     string testAbiltyName = testBundleName + ".MainAbility";
198     int testPid = LaunchTestHap(testAbiltyName, testBundleName);
199     if (testPid == 0) {
200         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
201         return;
202     }
203     if (!CheckProcessComm(testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
204         GTEST_LOG_(ERROR) << "Error process comm";
205         return;
206     }
207     string procCMD = "dumpcatcher -m -p " + to_string(testPid);
208     string procDumpLog = ExecuteCommands(procCMD);
209     GTEST_LOG_(INFO) << "procDumpLog: " << procDumpLog;
210     string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn", "Name:OS_DfxWatchdog" };
211     log[0] += to_string(testPid);
212     log[1] += TRUNCATE_TEST_BUNDLE_NAME;
213     int expectNum = sizeof(log) / sizeof(log[0]);
214     int count = GetKeywordsNum(procDumpLog, log, expectNum);
215     EXPECT_EQ(count, expectNum) << "DumpCatcherCommandTest006 Failed";
216     GTEST_LOG_(INFO) << "DumpCatcherCommandTest006: end.";
217 }
218 
219 /**
220  * @tc.name: DumpCatcherCommandTest007
221  * @tc.desc: test dumpcatcher command: -m -p [test hap] -t [main thread]
222  * @tc.type: FUNC
223  * @tc.require: issueI5PJ9O
224  */
225 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest007, TestSize.Level2)
226 {
227     GTEST_LOG_(INFO) << "DumpCatcherCommandTest007: start.";
228     string testBundleName = TEST_BUNDLE_NAME;
229     string testAbiltyName = testBundleName + ".MainAbility";
230     int testPid = LaunchTestHap(testAbiltyName, testBundleName);
231     if (testPid == 0) {
232         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
233         return;
234     }
235     if (!CheckProcessComm(testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
236         GTEST_LOG_(ERROR) << "Error process comm";
237         return;
238     }
239     string procCMD = "dumpcatcher -m -p " + to_string(testPid) +
240         " -t " + to_string(testPid);
241     string procDumpLog = ExecuteCommands(procCMD);
242     GTEST_LOG_(INFO) << "procDumpLog: " << procDumpLog;
243     string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn" };
244     log[0] += to_string(testPid);
245     log[1] += TRUNCATE_TEST_BUNDLE_NAME;
246     int expectNum = sizeof(log) / sizeof(log[0]);
247     int count = GetKeywordsNum(procDumpLog, log, expectNum);
248     EXPECT_EQ(count, expectNum) << "DumpCatcherCommandTest007 Failed";
249     GTEST_LOG_(INFO) << "DumpCatcherCommandTest007: end.";
250 }
251 
252 /**
253  * @tc.name: DumpCatcherCommandTest008
254  * @tc.desc: test dumpcatcher command: -m -p [com.ohos.systemui] tid -1
255  * @tc.type: FUNC
256  * @tc.require: issueI5PJ9O
257  */
258 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest008, TestSize.Level2)
259 {
260     GTEST_LOG_(INFO) << "DumpCatcherCommandTest008: start.";
261     string systemui = "com.ohos.systemui";
262     int systemuiPid = GetProcessPid(systemui);
263     string procCMD = "dumpcatcher -m -p " + to_string(systemuiPid) + " -t -1";
264     string procDumpLog = ExecuteCommands(procCMD);
265     GTEST_LOG_(INFO) << "procDumpLog: " << procDumpLog;
266     string log[] = {"Failed"};
267     int expectNum = sizeof(log) / sizeof(log[0]);
268     int count = GetKeywordsNum(procDumpLog, log, expectNum);
269     EXPECT_EQ(count, expectNum) << "DumpCatcherCommandTest008 Failed";
270     GTEST_LOG_(INFO) << "DumpCatcherCommandTest008: end.";
271 }
272 
273 /**
274  * @tc.name: DumpCatcherCommandTest009
275  * @tc.desc: test dumpcatcher command: -m -p -1 tid -1
276  * @tc.type: FUNC
277  * @tc.require: issueI5PJ9O
278  */
279 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest009, TestSize.Level2)
280 {
281     GTEST_LOG_(INFO) << "DumpCatcherCommandTest009: start.";
282     string systemui = "com.ohos.systemui";
283     string procCMD = "dumpcatcher -m -p -1 -t -1";
284     string procDumpLog = ExecuteCommands(procCMD);
285     GTEST_LOG_(INFO) << "procDumpLog: " << procDumpLog;
286     string log[] = {"Failed"};
287     int len = sizeof(log) / sizeof(log[0]);
288     int count = GetKeywordsNum(procDumpLog, log, len);
289     EXPECT_EQ(count, len) << "DumpCatcherCommandTest009 Failed";
290     GTEST_LOG_(INFO) << "DumpCatcherCommandTest009: end.";
291 }
292 
293 /**
294  * @tc.name: DumpCatcherCommandTest010
295  * @tc.desc: test dumpcatcher command: -c -p pid
296  * @tc.type: FUNC
297  */
298 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest010, TestSize.Level2)
299 {
300     GTEST_LOG_(INFO) << "DumpCatcherCommandTest010: start.";
301     string testBundleName = TEST_BUNDLE_NAME;
302     string testAbiltyName = testBundleName + ".MainAbility";
303     int testPid = LaunchTestHap(testAbiltyName, testBundleName);
304     if (testPid == 0) {
305         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
306         return;
307     }
308     if (!CheckProcessComm(testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
309         GTEST_LOG_(ERROR) << "Error process comm";
310         return;
311     }
312     string procCMD = "dumpcatcher -c -p " + to_string(testPid);
313     string procDumpLog = ExecuteCommands(procCMD);
314     GTEST_LOG_(INFO) << "procDumpLog: " << procDumpLog;
315     string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
316     int expectNum = sizeof(log) / sizeof(log[0]);
317     int count = GetKeywordsNum(procDumpLog, log, expectNum);
318     EXPECT_EQ(count, expectNum) << "DumpCatcherCommandTest010 Failed";
319     GTEST_LOG_(INFO) << "DumpCatcherCommandTest010: end.";
320 }
321 
322 /**
323  * @tc.name: DumpCatcherCommandTest011
324  * @tc.desc: test dumpcatcher command: -k -p pid
325  * @tc.type: FUNC
326  */
327 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest011, TestSize.Level2)
328 {
329     GTEST_LOG_(INFO) << "DumpCatcherCommandTest011: start.";
330     string testBundleName = TEST_BUNDLE_NAME;
331     string testAbiltyName = testBundleName + ".MainAbility";
332     int testPid = LaunchTestHap(testAbiltyName, testBundleName);
333     if (testPid == 0) {
334         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
335         return;
336     }
337     if (!CheckProcessComm(testPid, TRUNCATE_TEST_BUNDLE_NAME)) {
338         GTEST_LOG_(ERROR) << "Error process comm";
339         return;
340     }
341     string procCMD = "dumpcatcher -k -p " + to_string(testPid);
342     string procDumpLog = ExecuteCommands(procCMD);
343     GTEST_LOG_(INFO) << "procDumpLog: " << procDumpLog;
344     string log[] = {"Failed"};
345     int expectNum = sizeof(log) / sizeof(log[0]);
346     int count = GetKeywordsNum(procDumpLog, log, expectNum);
347     EXPECT_EQ(count, expectNum) << "DumpCatcherCommandTest011 Failed";
348     GTEST_LOG_(INFO) << "DumpCatcherCommandTest011: end.";
349 }
350 
351 /**
352  * @tc.name: DumpCatcherCommandTest012
353  * @tc.desc: test dumpcatcher command:
354  * @tc.type: FUNC
355  */
356 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest012, TestSize.Level2)
357 {
358     GTEST_LOG_(INFO) << "DumpCatcherCommandTest012: start.";
359     string procCMD = "dumpcatcher";
360     string procDumpLog = ExecuteCommands(procCMD);
361     EXPECT_EQ(procDumpLog, "") << "DumpCatcherCommandTest012 Failed";
362     GTEST_LOG_(INFO) << "DumpCatcherCommandTest012: end.";
363 }
364 
365 /**
366  * @tc.name: DumpCatcherCommandTest013
367  * @tc.desc: test dumpcatcher command: -i
368  * @tc.type: FUNC
369  */
370 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest013, TestSize.Level2)
371 {
372     GTEST_LOG_(INFO) << "DumpCatcherCommandTest013: start.";
373     string procCMD = "dumpcatcher -i";
374     string procDumpLog = ExecuteCommands(procCMD);
375     string log[] = {"Usage:"};
376     int expectNum = sizeof(log) / sizeof(log[0]);
377     int count = GetKeywordsNum(procDumpLog, log, expectNum);
378     EXPECT_EQ(count, expectNum) << "DumpCatcherCommandTest013 Failed";
379     GTEST_LOG_(INFO) << "DumpCatcherCommandTest013: end.";
380 }
381 
382 /**
383  * @tc.name: DumpCatcherCommandTest014
384  * @tc.desc: test dumpcatcher command: -p 1 tid 1
385  * @tc.type: FUNC
386  * @tc.require: issueI5PJ9O
387  */
388 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest014, TestSize.Level2)
389 {
390     GTEST_LOG_(INFO) << "DumpCatcherCommandTest014: start.";
391     string systemui = "init";
392     string procCMD = "dumpcatcher -p 1 -t 1";
393     string procDumpLog = ExecuteCommands(procCMD);
394     GTEST_LOG_(INFO) << "procDumpLog: " << procDumpLog;
395     string log[] = {"Pid:1", "Name:init", "#00", "#01", "#02"};
396     int len = sizeof(log) / sizeof(log[0]);
397     int count = GetKeywordsNum(procDumpLog, log, len);
398     EXPECT_EQ(count, len) << "DumpCatcherCommandTest014 Failed";
399     GTEST_LOG_(INFO) << "DumpCatcherCommandTest014: end.";
400 }
401 
402 /**
403  * @tc.name: DumpCatcherCommandTest015
404  * @tc.desc: test dumpcatcher abnormal scenario
405  * @tc.type: FUNC
406  */
407 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest015, TestSize.Level2)
408 {
409     GTEST_LOG_(INFO) << "DumpCatcherCommandTest015: start.";
410     std::shared_ptr<DfxDumpCatcher> dump = make_shared<DfxDumpCatcher>();
411     std::string msg = "";
412     bool ret = dump->DoDumpCurrTid(0, msg, 0);
413     ASSERT_EQ(ret, false);
414     ret = dump->DoDumpLocalTid(-1, msg, 0);
415     ASSERT_EQ(ret, false);
416     ret = dump->DoDumpLocalPid(-1, msg, 0);
417     ASSERT_EQ(ret, false);
418     std::vector<int> pidV;
419     ret = dump->DumpCatchMultiPid(pidV, msg);
420     ASSERT_EQ(ret, false);
421     GTEST_LOG_(INFO) << "DumpCatcherCommandTest015: end.";
422 }
423 } // namespace HiviewDFX
424 } // namepsace OHOS