1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 3800b99b8Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4800b99b8Sopenharmony_ci * you may not use this file except in compliance with the License. 5800b99b8Sopenharmony_ci * You may obtain a copy of the License at 6800b99b8Sopenharmony_ci * 7800b99b8Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8800b99b8Sopenharmony_ci * 9800b99b8Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10800b99b8Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11800b99b8Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12800b99b8Sopenharmony_ci * See the License for the specific language governing permissions and 13800b99b8Sopenharmony_ci * limitations under the License. 14800b99b8Sopenharmony_ci */ 15800b99b8Sopenharmony_ci 16800b99b8Sopenharmony_ci#include <gtest/gtest.h> 17800b99b8Sopenharmony_ci 18800b99b8Sopenharmony_ci#include <string> 19800b99b8Sopenharmony_ci#include <unistd.h> 20800b99b8Sopenharmony_ci 21800b99b8Sopenharmony_ci#include "dfx_define.h" 22800b99b8Sopenharmony_ci#include "dfx_dump_catcher.h" 23800b99b8Sopenharmony_ci#include "dfx_test_util.h" 24800b99b8Sopenharmony_ci 25800b99b8Sopenharmony_ciusing namespace testing::ext; 26800b99b8Sopenharmony_ciusing namespace std; 27800b99b8Sopenharmony_ci 28800b99b8Sopenharmony_cinamespace OHOS { 29800b99b8Sopenharmony_cinamespace HiviewDFX { 30800b99b8Sopenharmony_cinamespace { 31800b99b8Sopenharmony_cistatic int g_testPid = 0; 32800b99b8Sopenharmony_ci} 33800b99b8Sopenharmony_ciclass DumpCatcherCommandTest : public testing::Test { 34800b99b8Sopenharmony_cipublic: 35800b99b8Sopenharmony_ci static void SetUpTestCase(); 36800b99b8Sopenharmony_ci static void TearDownTestCase(); 37800b99b8Sopenharmony_ci void SetUp(); 38800b99b8Sopenharmony_ci void TearDown(); 39800b99b8Sopenharmony_ci}; 40800b99b8Sopenharmony_ci 41800b99b8Sopenharmony_civoid DumpCatcherCommandTest::SetUpTestCase() 42800b99b8Sopenharmony_ci{ 43800b99b8Sopenharmony_ci InstallTestHap("/data/FaultloggerdJsTest.hap"); 44800b99b8Sopenharmony_ci string testBundleName = TEST_BUNDLE_NAME; 45800b99b8Sopenharmony_ci string testAbiltyName = testBundleName + ".MainAbility"; 46800b99b8Sopenharmony_ci g_testPid = LaunchTestHap(testAbiltyName, testBundleName); 47800b99b8Sopenharmony_ci} 48800b99b8Sopenharmony_ci 49800b99b8Sopenharmony_civoid DumpCatcherCommandTest::TearDownTestCase() 50800b99b8Sopenharmony_ci{ 51800b99b8Sopenharmony_ci StopTestHap(TEST_BUNDLE_NAME); 52800b99b8Sopenharmony_ci UninstallTestHap(TEST_BUNDLE_NAME); 53800b99b8Sopenharmony_ci} 54800b99b8Sopenharmony_ci 55800b99b8Sopenharmony_civoid DumpCatcherCommandTest::SetUp() 56800b99b8Sopenharmony_ci{} 57800b99b8Sopenharmony_ci 58800b99b8Sopenharmony_civoid DumpCatcherCommandTest::TearDown() 59800b99b8Sopenharmony_ci{} 60800b99b8Sopenharmony_ci 61800b99b8Sopenharmony_ci/** 62800b99b8Sopenharmony_ci * @tc.name: DumpCatcherCommandTest001 63800b99b8Sopenharmony_ci * @tc.desc: test dumpcatcher command: -p [accountmgr] 64800b99b8Sopenharmony_ci * @tc.type: FUNC 65800b99b8Sopenharmony_ci */ 66800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest001, TestSize.Level2) 67800b99b8Sopenharmony_ci{ 68800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest001: start."; 69800b99b8Sopenharmony_ci int testPid = GetProcessPid("accountmgr"); 70800b99b8Sopenharmony_ci string testCommand = "dumpcatcher -p " + to_string(testPid); 71800b99b8Sopenharmony_ci string dumpRes = ExecuteCommands(testCommand); 72800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << dumpRes; 73800b99b8Sopenharmony_ci string log[] = {"Pid:", "Name:", "#00", "#01", "#02"}; 74800b99b8Sopenharmony_ci log[0] = log[0] + to_string(testPid); 75800b99b8Sopenharmony_ci log[1] = log[1] + "accountmgr"; 76800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 77800b99b8Sopenharmony_ci int count = GetKeywordsNum(dumpRes, log, len); 78800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << "DumpCatcherCommandTest001 Failed"; 79800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest001: end."; 80800b99b8Sopenharmony_ci} 81800b99b8Sopenharmony_ci 82800b99b8Sopenharmony_ci/** 83800b99b8Sopenharmony_ci * @tc.name: DumpCatcherCommandTest002 84800b99b8Sopenharmony_ci * @tc.desc: test dumpcatcher command: -p [accountmgr] -t [main thread] 85800b99b8Sopenharmony_ci * @tc.type: FUNC 86800b99b8Sopenharmony_ci */ 87800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest002, TestSize.Level2) 88800b99b8Sopenharmony_ci{ 89800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest002: start."; 90800b99b8Sopenharmony_ci int testPid = GetProcessPid("accountmgr"); 91800b99b8Sopenharmony_ci string testCommand = "dumpcatcher -p " + to_string(testPid) + " -t " + to_string(testPid); 92800b99b8Sopenharmony_ci string dumpRes = ExecuteCommands(testCommand); 93800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << dumpRes; 94800b99b8Sopenharmony_ci string log[] = {"Pid:", "Name:", "#00", "#01", "#02"}; 95800b99b8Sopenharmony_ci log[0] = log[0] + to_string(testPid); 96800b99b8Sopenharmony_ci log[1] = log[1] + "accountmgr"; 97800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 98800b99b8Sopenharmony_ci int count = GetKeywordsNum(dumpRes, log, len); 99800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << "DumpCatcherCommandTest002 Failed"; 100800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest002: end."; 101800b99b8Sopenharmony_ci} 102800b99b8Sopenharmony_ci 103800b99b8Sopenharmony_ci/** 104800b99b8Sopenharmony_ci * @tc.name: DumpCatcherCommandTest003 105800b99b8Sopenharmony_ci * @tc.desc: test dumpcatcher command: -p [test hap] 106800b99b8Sopenharmony_ci * @tc.type: FUNC 107800b99b8Sopenharmony_ci */ 108800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest003, TestSize.Level2) 109800b99b8Sopenharmony_ci{ 110800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest003: start."; 111800b99b8Sopenharmony_ci bool isSuccess = g_testPid != 0; 112800b99b8Sopenharmony_ci if (!isSuccess) { 113800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 114800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Failed to launch target hap."; 115800b99b8Sopenharmony_ci return; 116800b99b8Sopenharmony_ci } 117800b99b8Sopenharmony_ci isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME); 118800b99b8Sopenharmony_ci if (!isSuccess) { 119800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 120800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Error process comm"; 121800b99b8Sopenharmony_ci return; 122800b99b8Sopenharmony_ci } 123800b99b8Sopenharmony_ci string testCommand = "dumpcatcher -p " + to_string(g_testPid); 124800b99b8Sopenharmony_ci string dumpRes = ExecuteCommands(testCommand); 125800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << dumpRes; 126800b99b8Sopenharmony_ci string log[] = {"Pid:", "Name:", "#00", "#01", "#02"}; 127800b99b8Sopenharmony_ci log[0] = log[0] + to_string(g_testPid); 128800b99b8Sopenharmony_ci log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME; 129800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 130800b99b8Sopenharmony_ci int count = GetKeywordsNum(dumpRes, log, len); 131800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << "DumpCatcherCommandTest003 Failed"; 132800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest003: end."; 133800b99b8Sopenharmony_ci} 134800b99b8Sopenharmony_ci 135800b99b8Sopenharmony_ci/** 136800b99b8Sopenharmony_ci * @tc.name: DumpCatcherCommandTest004 137800b99b8Sopenharmony_ci * @tc.desc: test dumpcatcher command: -p [test hap] -t [main thread] 138800b99b8Sopenharmony_ci * @tc.type: FUNC 139800b99b8Sopenharmony_ci */ 140800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest004, TestSize.Level2) 141800b99b8Sopenharmony_ci{ 142800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest004: start."; 143800b99b8Sopenharmony_ci bool isSuccess = g_testPid != 0; 144800b99b8Sopenharmony_ci if (!isSuccess) { 145800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 146800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Failed to launch target hap."; 147800b99b8Sopenharmony_ci return; 148800b99b8Sopenharmony_ci } 149800b99b8Sopenharmony_ci isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME); 150800b99b8Sopenharmony_ci if (!isSuccess) { 151800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 152800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Error process comm"; 153800b99b8Sopenharmony_ci return; 154800b99b8Sopenharmony_ci } 155800b99b8Sopenharmony_ci string testCommand = "dumpcatcher -p " + to_string(g_testPid) + " -t " + to_string(g_testPid); 156800b99b8Sopenharmony_ci string dumpRes = ExecuteCommands(testCommand); 157800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << dumpRes; 158800b99b8Sopenharmony_ci string log[] = {"Pid:", "Name:", "#00", "#01", "#02"}; 159800b99b8Sopenharmony_ci log[0] = log[0] + to_string(g_testPid); 160800b99b8Sopenharmony_ci log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME; 161800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 162800b99b8Sopenharmony_ci int count = GetKeywordsNum(dumpRes, log, len); 163800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << "DumpCatcherCommandTest004 Failed"; 164800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest004: end."; 165800b99b8Sopenharmony_ci} 166800b99b8Sopenharmony_ci 167800b99b8Sopenharmony_ci/** 168800b99b8Sopenharmony_ci * @tc.name: DumpCatcherCommandTest005 169800b99b8Sopenharmony_ci * @tc.desc: test dumpcatcher command: -p [test hap] 170800b99b8Sopenharmony_ci * @tc.type: FUNC 171800b99b8Sopenharmony_ci */ 172800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest005, TestSize.Level2) 173800b99b8Sopenharmony_ci{ 174800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest005: start."; 175800b99b8Sopenharmony_ci bool isSuccess = g_testPid != 0; 176800b99b8Sopenharmony_ci if (!isSuccess) { 177800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 178800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Failed to launch target hap."; 179800b99b8Sopenharmony_ci return; 180800b99b8Sopenharmony_ci } 181800b99b8Sopenharmony_ci isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME); 182800b99b8Sopenharmony_ci if (!isSuccess) { 183800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 184800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Error process comm"; 185800b99b8Sopenharmony_ci return; 186800b99b8Sopenharmony_ci } 187800b99b8Sopenharmony_ci string testCommand = "dumpcatcher -p " + to_string(g_testPid); 188800b99b8Sopenharmony_ci string dumpRes = ExecuteCommands(testCommand); 189800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << dumpRes; 190800b99b8Sopenharmony_ci string log[] = {"Pid:", "Name:", "#00", "#01", "#02"}; 191800b99b8Sopenharmony_ci log[0] = log[0] + to_string(g_testPid); 192800b99b8Sopenharmony_ci log[1] = log[1] + TRUNCATE_TEST_BUNDLE_NAME; 193800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 194800b99b8Sopenharmony_ci int count = GetKeywordsNum(dumpRes, log, len); 195800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << "DumpCatcherCommandTest005 Failed"; 196800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest005: end."; 197800b99b8Sopenharmony_ci} 198800b99b8Sopenharmony_ci 199800b99b8Sopenharmony_ci/** 200800b99b8Sopenharmony_ci * @tc.name: DumpCatcherCommandTest012 201800b99b8Sopenharmony_ci * @tc.desc: test dumpcatcher command: 202800b99b8Sopenharmony_ci * @tc.type: FUNC 203800b99b8Sopenharmony_ci */ 204800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest012, TestSize.Level2) 205800b99b8Sopenharmony_ci{ 206800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest012: start."; 207800b99b8Sopenharmony_ci string procCMD = "dumpcatcher"; 208800b99b8Sopenharmony_ci string procDumpLog = ExecuteCommands(procCMD); 209800b99b8Sopenharmony_ci EXPECT_EQ(procDumpLog, "") << "DumpCatcherCommandTest012 Failed"; 210800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest012: end."; 211800b99b8Sopenharmony_ci} 212800b99b8Sopenharmony_ci 213800b99b8Sopenharmony_ci/** 214800b99b8Sopenharmony_ci * @tc.name: DumpCatcherCommandTest013 215800b99b8Sopenharmony_ci * @tc.desc: test dumpcatcher command: -i 216800b99b8Sopenharmony_ci * @tc.type: FUNC 217800b99b8Sopenharmony_ci */ 218800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest013, TestSize.Level2) 219800b99b8Sopenharmony_ci{ 220800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest013: start."; 221800b99b8Sopenharmony_ci string procCMD = "dumpcatcher -i"; 222800b99b8Sopenharmony_ci string procDumpLog = ExecuteCommands(procCMD); 223800b99b8Sopenharmony_ci string log[] = {"Usage:"}; 224800b99b8Sopenharmony_ci int expectNum = sizeof(log) / sizeof(log[0]); 225800b99b8Sopenharmony_ci int count = GetKeywordsNum(procDumpLog, log, expectNum); 226800b99b8Sopenharmony_ci EXPECT_EQ(count, expectNum) << "DumpCatcherCommandTest013 Failed"; 227800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest013: end."; 228800b99b8Sopenharmony_ci} 229800b99b8Sopenharmony_ci 230800b99b8Sopenharmony_ci/** 231800b99b8Sopenharmony_ci * @tc.name: DumpCatcherCommandTest014 232800b99b8Sopenharmony_ci * @tc.desc: test dumpcatcher command: -p 1 tid 1 233800b99b8Sopenharmony_ci * @tc.type: FUNC 234800b99b8Sopenharmony_ci * @tc.require: issueI5PJ9O 235800b99b8Sopenharmony_ci */ 236800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest014, TestSize.Level2) 237800b99b8Sopenharmony_ci{ 238800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest014: start."; 239800b99b8Sopenharmony_ci string systemui = "init"; 240800b99b8Sopenharmony_ci string procCMD = "dumpcatcher -p 1 -t 1"; 241800b99b8Sopenharmony_ci string procDumpLog = ExecuteCommands(procCMD); 242800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "procDumpLog: " << procDumpLog; 243800b99b8Sopenharmony_ci string log[] = {"Pid:1", "Name:init", "#00", "#01", "#02"}; 244800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 245800b99b8Sopenharmony_ci int count = GetKeywordsNum(procDumpLog, log, len); 246800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << "DumpCatcherCommandTest014 Failed"; 247800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest014: end."; 248800b99b8Sopenharmony_ci} 249800b99b8Sopenharmony_ci 250800b99b8Sopenharmony_ci/** 251800b99b8Sopenharmony_ci * @tc.name: DumpCatcherCommandTest015 252800b99b8Sopenharmony_ci * @tc.desc: test dumpcatcher abnormal scenario 253800b99b8Sopenharmony_ci * @tc.type: FUNC 254800b99b8Sopenharmony_ci */ 255800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherCommandTest, DumpCatcherCommandTest015, TestSize.Level2) 256800b99b8Sopenharmony_ci{ 257800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest015: start."; 258800b99b8Sopenharmony_ci std::shared_ptr<DfxDumpCatcher> dump = make_shared<DfxDumpCatcher>(); 259800b99b8Sopenharmony_ci std::string msg = ""; 260800b99b8Sopenharmony_ci bool ret = dump->DoDumpCurrTid(0, msg, 0); 261800b99b8Sopenharmony_ci ASSERT_EQ(ret, false); 262800b99b8Sopenharmony_ci ret = dump->DoDumpLocalTid(-1, msg, 0); 263800b99b8Sopenharmony_ci ASSERT_EQ(ret, false); 264800b99b8Sopenharmony_ci ret = dump->DoDumpLocalPid(-1, msg, 0); 265800b99b8Sopenharmony_ci ASSERT_EQ(ret, false); 266800b99b8Sopenharmony_ci std::vector<int> pidV; 267800b99b8Sopenharmony_ci ret = dump->DumpCatchMultiPid(pidV, msg); 268800b99b8Sopenharmony_ci ASSERT_EQ(ret, false); 269800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherCommandTest015: end."; 270800b99b8Sopenharmony_ci} 271800b99b8Sopenharmony_ci} // namespace HiviewDFX 272800b99b8Sopenharmony_ci} // namepsace OHOS