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
25using namespace testing::ext;
26using namespace std;
27
28namespace OHOS {
29namespace HiviewDFX {
30namespace {
31static int g_testPid = 0;
32}
33class DumpCatcherCommandTest : public testing::Test {
34public:
35    static void SetUpTestCase();
36    static void TearDownTestCase();
37    void SetUp();
38    void TearDown();
39};
40
41void 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
49void DumpCatcherCommandTest::TearDownTestCase()
50{
51    StopTestHap(TEST_BUNDLE_NAME);
52    UninstallTestHap(TEST_BUNDLE_NAME);
53}
54
55void DumpCatcherCommandTest::SetUp()
56{}
57
58void DumpCatcherCommandTest::TearDown()
59{}
60
61/**
62 * @tc.name: DumpCatcherCommandTest001
63 * @tc.desc: test dumpcatcher command: -p [accountmgr]
64 * @tc.type: FUNC
65 */
66HWTEST_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 */
87HWTEST_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 */
108HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest003, TestSize.Level2)
109{
110    GTEST_LOG_(INFO) << "DumpCatcherCommandTest003: start.";
111    bool isSuccess = g_testPid != 0;
112    if (!isSuccess) {
113        ASSERT_FALSE(isSuccess);
114        GTEST_LOG_(ERROR) << "Failed to launch target hap.";
115        return;
116    }
117    isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
118    if (!isSuccess) {
119        ASSERT_FALSE(isSuccess);
120        GTEST_LOG_(ERROR) << "Error process comm";
121        return;
122    }
123    string testCommand = "dumpcatcher -p " + to_string(g_testPid);
124    string dumpRes = ExecuteCommands(testCommand);
125    GTEST_LOG_(INFO) << dumpRes;
126    string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
127    log[0] = log[0] + to_string(g_testPid);
128    log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME;
129    int len = sizeof(log) / sizeof(log[0]);
130    int count = GetKeywordsNum(dumpRes, log, len);
131    EXPECT_EQ(count, len) << "DumpCatcherCommandTest003 Failed";
132    GTEST_LOG_(INFO) << "DumpCatcherCommandTest003: end.";
133}
134
135/**
136 * @tc.name: DumpCatcherCommandTest004
137 * @tc.desc: test dumpcatcher command: -p [test hap] -t [main thread]
138 * @tc.type: FUNC
139 */
140HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest004, TestSize.Level2)
141{
142    GTEST_LOG_(INFO) << "DumpCatcherCommandTest004: start.";
143    bool isSuccess = g_testPid != 0;
144    if (!isSuccess) {
145        ASSERT_FALSE(isSuccess);
146        GTEST_LOG_(ERROR) << "Failed to launch target hap.";
147        return;
148    }
149    isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
150    if (!isSuccess) {
151        ASSERT_FALSE(isSuccess);
152        GTEST_LOG_(ERROR) << "Error process comm";
153        return;
154    }
155    string testCommand = "dumpcatcher -p " + to_string(g_testPid) + " -t " + to_string(g_testPid);
156    string dumpRes = ExecuteCommands(testCommand);
157    GTEST_LOG_(INFO) << dumpRes;
158    string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
159    log[0] = log[0] + to_string(g_testPid);
160    log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME;
161    int len = sizeof(log) / sizeof(log[0]);
162    int count = GetKeywordsNum(dumpRes, log, len);
163    EXPECT_EQ(count, len) << "DumpCatcherCommandTest004 Failed";
164    GTEST_LOG_(INFO) << "DumpCatcherCommandTest004: end.";
165}
166
167/**
168 * @tc.name: DumpCatcherCommandTest005
169 * @tc.desc: test dumpcatcher command: -p [test hap]
170 * @tc.type: FUNC
171 */
172HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest005, TestSize.Level2)
173{
174    GTEST_LOG_(INFO) << "DumpCatcherCommandTest005: start.";
175    bool isSuccess = g_testPid != 0;
176    if (!isSuccess) {
177        ASSERT_FALSE(isSuccess);
178        GTEST_LOG_(ERROR) << "Failed to launch target hap.";
179        return;
180    }
181    isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME);
182    if (!isSuccess) {
183        ASSERT_FALSE(isSuccess);
184        GTEST_LOG_(ERROR) << "Error process comm";
185        return;
186    }
187    string testCommand = "dumpcatcher -p " + to_string(g_testPid);
188    string dumpRes = ExecuteCommands(testCommand);
189    GTEST_LOG_(INFO) << dumpRes;
190    string log[] = {"Pid:", "Name:", "#00", "#01", "#02"};
191    log[0] = log[0] + to_string(g_testPid);
192    log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME;
193    int len = sizeof(log) / sizeof(log[0]);
194    int count = GetKeywordsNum(dumpRes, log, len);
195    EXPECT_EQ(count, len) << "DumpCatcherCommandTest005 Failed";
196    GTEST_LOG_(INFO) << "DumpCatcherCommandTest005: end.";
197}
198
199/**
200 * @tc.name: DumpCatcherCommandTest012
201 * @tc.desc: test dumpcatcher command:
202 * @tc.type: FUNC
203 */
204HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest012, TestSize.Level2)
205{
206    GTEST_LOG_(INFO) << "DumpCatcherCommandTest012: start.";
207    string procCMD = "dumpcatcher";
208    string procDumpLog = ExecuteCommands(procCMD);
209    EXPECT_EQ(procDumpLog, "") << "DumpCatcherCommandTest012 Failed";
210    GTEST_LOG_(INFO) << "DumpCatcherCommandTest012: end.";
211}
212
213/**
214 * @tc.name: DumpCatcherCommandTest013
215 * @tc.desc: test dumpcatcher command: -i
216 * @tc.type: FUNC
217 */
218HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest013, TestSize.Level2)
219{
220    GTEST_LOG_(INFO) << "DumpCatcherCommandTest013: start.";
221    string procCMD = "dumpcatcher -i";
222    string procDumpLog = ExecuteCommands(procCMD);
223    string log[] = {"Usage:"};
224    int expectNum = sizeof(log) / sizeof(log[0]);
225    int count = GetKeywordsNum(procDumpLog, log, expectNum);
226    EXPECT_EQ(count, expectNum) << "DumpCatcherCommandTest013 Failed";
227    GTEST_LOG_(INFO) << "DumpCatcherCommandTest013: end.";
228}
229
230/**
231 * @tc.name: DumpCatcherCommandTest014
232 * @tc.desc: test dumpcatcher command: -p 1 tid 1
233 * @tc.type: FUNC
234 * @tc.require: issueI5PJ9O
235 */
236HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest014, TestSize.Level2)
237{
238    GTEST_LOG_(INFO) << "DumpCatcherCommandTest014: start.";
239    string systemui = "init";
240    string procCMD = "dumpcatcher -p 1 -t 1";
241    string procDumpLog = ExecuteCommands(procCMD);
242    GTEST_LOG_(INFO) << "procDumpLog: " << procDumpLog;
243    string log[] = {"Pid:1", "Name:init", "#00", "#01", "#02"};
244    int len = sizeof(log) / sizeof(log[0]);
245    int count = GetKeywordsNum(procDumpLog, log, len);
246    EXPECT_EQ(count, len) << "DumpCatcherCommandTest014 Failed";
247    GTEST_LOG_(INFO) << "DumpCatcherCommandTest014: end.";
248}
249
250/**
251 * @tc.name: DumpCatcherCommandTest015
252 * @tc.desc: test dumpcatcher abnormal scenario
253 * @tc.type: FUNC
254 */
255HWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest015, TestSize.Level2)
256{
257    GTEST_LOG_(INFO) << "DumpCatcherCommandTest015: start.";
258    std::shared_ptr<DfxDumpCatcher> dump = make_shared<DfxDumpCatcher>();
259    std::string msg = "";
260    bool ret = dump->DoDumpCurrTid(0, msg, 0);
261    ASSERT_EQ(ret, false);
262    ret = dump->DoDumpLocalTid(-1, msg, 0);
263    ASSERT_EQ(ret, false);
264    ret = dump->DoDumpLocalPid(-1, msg, 0);
265    ASSERT_EQ(ret, false);
266    std::vector<int> pidV;
267    ret = dump->DumpCatchMultiPid(pidV, msg);
268    ASSERT_EQ(ret, false);
269    GTEST_LOG_(INFO) << "DumpCatcherCommandTest015: end.";
270}
271} // namespace HiviewDFX
272} // namepsace OHOS