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