• 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 <thread>
20 #include <unistd.h>
21 
22 #include "dfx_define.h"
23 #include "dfx_dump_catcher.h"
24 #include "dfx_test_util.h"
25 
26 using namespace testing::ext;
27 using namespace std;
28 
29 namespace OHOS {
30 namespace HiviewDFX {
31 namespace {
32 static int g_testPid = 0;
33 }
34 class DumpCatcherCommandTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp();
39     void TearDown();
40 };
41 
SetUpTestCase()42 void DumpCatcherCommandTest::SetUpTestCase()
43 {
44     InstallTestHap("/data/FaultloggerdJsTest.hap");
45     string testBundleName = TEST_BUNDLE_NAME;
46     string testAbiltyName = testBundleName + ".MainAbility";
47     g_testPid = LaunchTestHap(testAbiltyName, testBundleName);
48 }
49 
TearDownTestCase()50 void DumpCatcherCommandTest::TearDownTestCase()
51 {
52     StopTestHap(TEST_BUNDLE_NAME);
53     UninstallTestHap(TEST_BUNDLE_NAME);
54 }
55 
SetUp()56 void DumpCatcherCommandTest::SetUp()
57 {}
58 
TearDown()59 void DumpCatcherCommandTest::TearDown()
60 {}
61 
62 /**
63  * @tc.name: DumpCatcherCommandTest001
64  * @tc.desc: test dumpcatcher command: -p [powermgr]
65  * @tc.type: FUNC
66  */
67 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest001, TestSize.Level0)
68 {
69     GTEST_LOG_(INFO) << "DumpCatcherCommandTest001: start.";
70     int testPid = GetProcessPid("powermgr");
71     string testCommand = "dumpcatcher -p " + to_string(testPid);
72     string dumpRes = ExecuteCommands(testCommand);
73     GTEST_LOG_(INFO) << dumpRes;
74     string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
75     log[0] = log[0] + to_string(testPid);
76     log[1] = log[1] + "powermgr";
77     int len = sizeof(log) / sizeof(log[0]);
78     int count = GetKeywordsNum(dumpRes, log, len);
79     EXPECT_EQ(count, len) << "DumpCatcherCommandTest001 Failed";
80     GTEST_LOG_(INFO) << "DumpCatcherCommandTest001: end.";
81 }
82 
83 /**
84  * @tc.name: DumpCatcherCommandTest002
85  * @tc.desc: test dumpcatcher command: -p [powermgr] -t [main thread]
86  * @tc.type: FUNC
87  */
88 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest002, TestSize.Level2)
89 {
90     GTEST_LOG_(INFO) << "DumpCatcherCommandTest002: start.";
91     int testPid = GetProcessPid("powermgr");
92     string testCommand = "dumpcatcher -p " + to_string(testPid) + " -t " + to_string(testPid);
93     string dumpRes = ExecuteCommands(testCommand);
94     GTEST_LOG_(INFO) << dumpRes;
95     string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
96     log[0] = log[0] + to_string(testPid);
97     log[1] = log[1] + "powermgr";
98     int len = sizeof(log) / sizeof(log[0]);
99     int count = GetKeywordsNum(dumpRes, log, len);
100     EXPECT_EQ(count, len) << "DumpCatcherCommandTest002 Failed";
101     GTEST_LOG_(INFO) << "DumpCatcherCommandTest002: end.";
102 }
103 
104 /**
105  * @tc.name: DumpCatcherCommandTest003
106  * @tc.desc: test dumpcatcher command: -p [test hap]
107  * @tc.type: FUNC
108  */
109 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest003, TestSize.Level2)
110 {
111     GTEST_LOG_(INFO) << "DumpCatcherCommandTest003: start.";
112     bool isSuccess = g_testPid != 0;
113     if (!isSuccess) {
114         ASSERT_FALSE(isSuccess);
115         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
116     } else {
117         isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
118         if (!isSuccess) {
119             ASSERT_FALSE(isSuccess);
120             GTEST_LOG_(ERROR) << "Error process comm";
121         } else {
122             string testCommand = "dumpcatcher -p " + to_string(g_testPid);
123             string dumpRes = ExecuteCommands(testCommand);
124             GTEST_LOG_(INFO) << dumpRes;
125             string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
126             log[0] = log[0] + to_string(g_testPid);
127             log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME;
128             int len = sizeof(log) / sizeof(log[0]);
129             int count = GetKeywordsNum(dumpRes, log, len);
130             EXPECT_EQ(count, len) << "DumpCatcherCommandTest003 Failed";
131             GTEST_LOG_(INFO) << "DumpCatcherCommandTest003: end.";
132         }
133     }
134 }
135 
136 /**
137  * @tc.name: DumpCatcherCommandTest004
138  * @tc.desc: test dumpcatcher command: -p [test hap] -t [main thread]
139  * @tc.type: FUNC
140  */
141 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest004, TestSize.Level2)
142 {
143     GTEST_LOG_(INFO) << "DumpCatcherCommandTest004: start.";
144     bool isSuccess = g_testPid != 0;
145     if (!isSuccess) {
146         ASSERT_FALSE(isSuccess);
147         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
148     } else {
149         isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
150         if (!isSuccess) {
151             ASSERT_FALSE(isSuccess);
152             GTEST_LOG_(ERROR) << "Error process comm";
153         } else {
154             string testCommand = "dumpcatcher -p " + to_string(g_testPid) + " -t " + to_string(g_testPid);
155             string dumpRes = ExecuteCommands(testCommand);
156             GTEST_LOG_(INFO) << dumpRes;
157             string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
158             log[0] = log[0] + to_string(g_testPid);
159             log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME;
160             int len = sizeof(log) / sizeof(log[0]);
161             int count = GetKeywordsNum(dumpRes, log, len);
162             EXPECT_EQ(count, len) << "DumpCatcherCommandTest004 Failed";
163             GTEST_LOG_(INFO) << "DumpCatcherCommandTest004: end.";
164         }
165     }
166 }
167 
168 /**
169  * @tc.name: DumpCatcherCommandTest005
170  * @tc.desc: test dumpcatcher command: -p [test hap]
171  * @tc.type: FUNC
172  */
173 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest005, TestSize.Level2)
174 {
175     GTEST_LOG_(INFO) << "DumpCatcherCommandTest005: start.";
176     bool isSuccess = g_testPid != 0;
177     if (!isSuccess) {
178         ASSERT_FALSE(isSuccess);
179         GTEST_LOG_(ERROR) << "Failed to launch target hap.";
180     } else {
181         isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
182         if (!isSuccess) {
183             ASSERT_FALSE(isSuccess);
184             GTEST_LOG_(ERROR) << "Error process comm";
185         } else {
186             string testCommand = "dumpcatcher -p " + to_string(g_testPid);
187             string dumpRes = ExecuteCommands(testCommand);
188             GTEST_LOG_(INFO) << dumpRes;
189             string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
190             log[0] = log[0] + to_string(g_testPid);
191             log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME;
192             int len = sizeof(log) / sizeof(log[0]);
193             int count = GetKeywordsNum(dumpRes, log, len);
194             EXPECT_EQ(count, len) << "DumpCatcherCommandTest005 Failed";
195             GTEST_LOG_(INFO) << "DumpCatcherCommandTest005: end.";
196         }
197     }
198 }
199 
200 /**
201  * @tc.name: DumpCatcherCommandTest012
202  * @tc.desc: test dumpcatcher command:
203  * @tc.type: FUNC
204  */
205 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest012, TestSize.Level2)
206 {
207     GTEST_LOG_(INFO) << "DumpCatcherCommandTest012: start.";
208     string procCMD = "dumpcatcher";
209     string procDumpLog = ExecuteCommands(procCMD);
210     EXPECT_EQ(procDumpLog, "") << "DumpCatcherCommandTest012 Failed";
211     GTEST_LOG_(INFO) << "DumpCatcherCommandTest012: end.";
212 }
213 
214 /**
215  * @tc.name: DumpCatcherCommandTest013
216  * @tc.desc: test dumpcatcher command: -i
217  * @tc.type: FUNC
218  */
219 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest013, TestSize.Level2)
220 {
221     GTEST_LOG_(INFO) << "DumpCatcherCommandTest013: start.";
222     string procCMD = "dumpcatcher -i";
223     string procDumpLog = ExecuteCommands(procCMD);
224     string log[] = {"Usage:"};
225     int expectNum = sizeof(log) / sizeof(log[0]);
226     int count = GetKeywordsNum(procDumpLog, log, expectNum);
227     EXPECT_EQ(count, expectNum) << "DumpCatcherCommandTest013 Failed";
228     GTEST_LOG_(INFO) << "DumpCatcherCommandTest013: end.";
229 }
230 
231 /**
232  * @tc.name: DumpCatcherCommandTest014
233  * @tc.desc: test dumpcatcher command: -p 1 tid 1
234  * @tc.type: FUNC
235  * @tc.require: issueI5PJ9O
236  */
237 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest014, TestSize.Level2)
238 {
239     GTEST_LOG_(INFO) << "DumpCatcherCommandTest014: start.";
240     string systemui = "init";
241     string procCMD = "dumpcatcher -p 1 -t 1";
242     string procDumpLog = ExecuteCommands(procCMD);
243     GTEST_LOG_(INFO) << "procDumpLog: " << procDumpLog;
244     string log[] = {"Pid:1", "Name:init", "#00", "#01", "#02"};
245     int len = sizeof(log) / sizeof(log[0]);
246     int count = GetKeywordsNum(procDumpLog, log, len);
247     EXPECT_EQ(count, len) << "DumpCatcherCommandTest014 Failed";
248     GTEST_LOG_(INFO) << "DumpCatcherCommandTest014: end.";
249 }
250 
251 /**
252  * @tc.name: DumpCatcherCommandTest015
253  * @tc.desc: test dumpcatcher command: -p [powermgr] -t [main thread] -T [1500]
254  * @tc.type: FUNC
255  */
256 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest015, TestSize.Level2)
257 {
258     GTEST_LOG_(INFO) << "DumpCatcherCommandTest015: start.";
259     int testPid = GetProcessPid("powermgr");
260     int timeout = 1500;
261     string testCommand = "dumpcatcher -p " + to_string(testPid) + " -t " + to_string(testPid);
262     testCommand += " -T " + to_string(timeout);
263     string dumpRes = ExecuteCommands(testCommand);
264     GTEST_LOG_(INFO) << dumpRes;
265     string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
266     log[0] = log[0] + to_string(testPid);
267     log[1] = log[1] + "powermgr";
268     int len = sizeof(log) / sizeof(log[0]);
269     int count = GetKeywordsNum(dumpRes, log, len);
270     EXPECT_EQ(count, len) << "DumpCatcherCommandTest015 Failed";
271     GTEST_LOG_(INFO) << "DumpCatcherCommandTest015: end.";
272 }
273 
274 /**
275  * @tc.name: DumpCatcherCommandTest016
276  * @tc.desc: test dumpcatcher command: -c save
277  * @tc.type: FUNC
278  */
279 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest016, TestSize.Level2)
280 {
281     GTEST_LOG_(INFO) << "DumpCatcherCommandTest016: start.";
282     string testCommand = "dumpcatcher -c save";
283     string dumpRes = ExecuteCommands(testCommand);
284     GTEST_LOG_(INFO) << dumpRes;
285     string log[] = {"input", "error"};
286     int len = sizeof(log) / sizeof(log[0]);
287     int count = GetKeywordsNum(dumpRes, log, len);
288     EXPECT_EQ(count, len) << "DumpCatcherCommandTest016 Failed";
289     GTEST_LOG_(INFO) << "DumpCatcherCommandTest016: end.";
290 }
291 
292 /**
293  * @tc.name: DumpCatcherCommandTest017
294  * @tc.desc: test dumpcatcher command: -c save 0
295  * @tc.type: FUNC
296  */
297 HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest017, TestSize.Level2)
298 {
299     GTEST_LOG_(INFO) << "DumpCatcherCommandTest017: start.";
300     string testCommand = "dumpcatcher -c save 0";
301     string dumpRes = ExecuteCommands(testCommand);
302     GTEST_LOG_(INFO) << dumpRes;
303     string log[] = {"pid", "error"};
304     int len = sizeof(log) / sizeof(log[0]);
305     int count = GetKeywordsNum(dumpRes, log, len);
306     EXPECT_EQ(count, len) << "DumpCatcherCommandTest017 Failed";
307     GTEST_LOG_(INFO) << "DumpCatcherCommandTest017: end.";
308 }
309 } // namespace HiviewDFX
310 } // namepsace OHOS