1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2022-2024 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 <thread> 20800b99b8Sopenharmony_ci#include <vector> 21800b99b8Sopenharmony_ci 22800b99b8Sopenharmony_ci#include <unistd.h> 23800b99b8Sopenharmony_ci 24800b99b8Sopenharmony_ci#include "dfx_define.h" 25800b99b8Sopenharmony_ci#include "dfx_dump_catcher.h" 26800b99b8Sopenharmony_ci#include "dfx_json_formatter.h" 27800b99b8Sopenharmony_ci#include "dfx_test_util.h" 28800b99b8Sopenharmony_ci#include "faultloggerd_client.h" 29800b99b8Sopenharmony_ci#include "procinfo.h" 30800b99b8Sopenharmony_ci 31800b99b8Sopenharmony_ciusing namespace testing; 32800b99b8Sopenharmony_ciusing namespace testing::ext; 33800b99b8Sopenharmony_ci 34800b99b8Sopenharmony_cinamespace OHOS { 35800b99b8Sopenharmony_cinamespace HiviewDFX { 36800b99b8Sopenharmony_ciclass DumpCatcherInterfacesTest : public testing::Test { 37800b99b8Sopenharmony_cipublic: 38800b99b8Sopenharmony_ci static void SetUpTestCase(); 39800b99b8Sopenharmony_ci static void TearDownTestCase(); 40800b99b8Sopenharmony_ci void SetUp(); 41800b99b8Sopenharmony_ci void TearDown(); 42800b99b8Sopenharmony_ci}; 43800b99b8Sopenharmony_ci 44800b99b8Sopenharmony_cistatic const int THREAD_ALIVE_TIME = 2; 45800b99b8Sopenharmony_ci 46800b99b8Sopenharmony_cistatic const int CREATE_THREAD_TIMEOUT = 300000; 47800b99b8Sopenharmony_ci 48800b99b8Sopenharmony_cistatic pid_t g_threadId = 0; 49800b99b8Sopenharmony_ci 50800b99b8Sopenharmony_cistatic pid_t g_processId = 0; 51800b99b8Sopenharmony_ci 52800b99b8Sopenharmony_ciint g_testPid = 0; 53800b99b8Sopenharmony_ci 54800b99b8Sopenharmony_civoid DumpCatcherInterfacesTest::SetUpTestCase() 55800b99b8Sopenharmony_ci{ 56800b99b8Sopenharmony_ci InstallTestHap("/data/FaultloggerdJsTest.hap"); 57800b99b8Sopenharmony_ci std::string testBundleName = TEST_BUNDLE_NAME; 58800b99b8Sopenharmony_ci std::string testAbiltyName = testBundleName + ".MainAbility"; 59800b99b8Sopenharmony_ci g_testPid = LaunchTestHap(testAbiltyName, testBundleName); 60800b99b8Sopenharmony_ci} 61800b99b8Sopenharmony_ci 62800b99b8Sopenharmony_civoid DumpCatcherInterfacesTest::TearDownTestCase() 63800b99b8Sopenharmony_ci{ 64800b99b8Sopenharmony_ci StopTestHap(TEST_BUNDLE_NAME); 65800b99b8Sopenharmony_ci UninstallTestHap(TEST_BUNDLE_NAME); 66800b99b8Sopenharmony_ci} 67800b99b8Sopenharmony_ci 68800b99b8Sopenharmony_civoid DumpCatcherInterfacesTest::SetUp() 69800b99b8Sopenharmony_ci{} 70800b99b8Sopenharmony_ci 71800b99b8Sopenharmony_civoid DumpCatcherInterfacesTest::TearDown() 72800b99b8Sopenharmony_ci{} 73800b99b8Sopenharmony_ci 74800b99b8Sopenharmony_cistatic void TestFunRecursive(int recursiveCount) 75800b99b8Sopenharmony_ci{ 76800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "Enter TestFunRecursive recursiveCount:" << recursiveCount; 77800b99b8Sopenharmony_ci if (recursiveCount <= 0) { 78800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "start enter sleep" << gettid(); 79800b99b8Sopenharmony_ci sleep(THREAD_ALIVE_TIME); 80800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "sleep end."; 81800b99b8Sopenharmony_ci } else { 82800b99b8Sopenharmony_ci TestFunRecursive(recursiveCount - 1); 83800b99b8Sopenharmony_ci } 84800b99b8Sopenharmony_ci} 85800b99b8Sopenharmony_ci 86800b99b8Sopenharmony_cistatic void* CreateRecursiveThread(void *argv) 87800b99b8Sopenharmony_ci{ 88800b99b8Sopenharmony_ci g_threadId = gettid(); 89800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "create Recursive MultiThread " << gettid(); 90800b99b8Sopenharmony_ci TestFunRecursive(266); // 266: set recursive count to 266, used for dumpcatcher get stack info 91800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "Recursive MultiThread thread sleep end."; 92800b99b8Sopenharmony_ci return nullptr; 93800b99b8Sopenharmony_ci} 94800b99b8Sopenharmony_ci 95800b99b8Sopenharmony_cistatic int RecursiveMultiThreadConstructor(void) 96800b99b8Sopenharmony_ci{ 97800b99b8Sopenharmony_ci pthread_t thread; 98800b99b8Sopenharmony_ci pthread_create(&thread, nullptr, CreateRecursiveThread, nullptr); 99800b99b8Sopenharmony_ci pthread_detach(thread); 100800b99b8Sopenharmony_ci usleep(CREATE_THREAD_TIMEOUT); 101800b99b8Sopenharmony_ci return 0; 102800b99b8Sopenharmony_ci} 103800b99b8Sopenharmony_ci 104800b99b8Sopenharmony_cistatic void* CreateThread(void *argv) 105800b99b8Sopenharmony_ci{ 106800b99b8Sopenharmony_ci g_threadId = gettid(); 107800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "create MultiThread " << gettid(); 108800b99b8Sopenharmony_ci sleep(THREAD_ALIVE_TIME); 109800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "create MultiThread thread sleep end."; 110800b99b8Sopenharmony_ci return nullptr; 111800b99b8Sopenharmony_ci} 112800b99b8Sopenharmony_ci 113800b99b8Sopenharmony_cistatic int MultiThreadConstructor(void) 114800b99b8Sopenharmony_ci{ 115800b99b8Sopenharmony_ci pthread_t thread; 116800b99b8Sopenharmony_ci pthread_create(&thread, nullptr, CreateThread, nullptr); 117800b99b8Sopenharmony_ci pthread_detach(thread); 118800b99b8Sopenharmony_ci usleep(CREATE_THREAD_TIMEOUT); 119800b99b8Sopenharmony_ci return 0; 120800b99b8Sopenharmony_ci} 121800b99b8Sopenharmony_ci 122800b99b8Sopenharmony_cistatic void ForkMultiThreadProcess(void) 123800b99b8Sopenharmony_ci{ 124800b99b8Sopenharmony_ci int pid = fork(); 125800b99b8Sopenharmony_ci if (pid == 0) { 126800b99b8Sopenharmony_ci MultiThreadConstructor(); 127800b99b8Sopenharmony_ci _exit(0); 128800b99b8Sopenharmony_ci } else if (pid < 0) { 129800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ForkMultiThreadProcess fail. "; 130800b99b8Sopenharmony_ci } else { 131800b99b8Sopenharmony_ci g_processId = pid; 132800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ForkMultiThreadProcess success, pid: " << pid; 133800b99b8Sopenharmony_ci } 134800b99b8Sopenharmony_ci} 135800b99b8Sopenharmony_ci 136800b99b8Sopenharmony_ci/** 137800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest001 138800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), PID(foundation)} 139800b99b8Sopenharmony_ci * @tc.type: FUNC 140800b99b8Sopenharmony_ci */ 141800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest001, TestSize.Level2) 142800b99b8Sopenharmony_ci{ 143800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest001: start."; 144800b99b8Sopenharmony_ci std::string testProcess1 = "accountmgr"; 145800b99b8Sopenharmony_ci int testPid1 = GetProcessPid(testProcess1); 146800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "testPid1:" << testPid1; 147800b99b8Sopenharmony_ci std::string testProcess2 = "foundation"; 148800b99b8Sopenharmony_ci int testPid2 = GetProcessPid(testProcess2); 149800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "testPid2:" << testPid2; 150800b99b8Sopenharmony_ci std::vector<int> multiPid {testPid1, testPid2}; 151800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 152800b99b8Sopenharmony_ci std::string msg = ""; 153800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchMultiPid(multiPid, msg); 154800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 155800b99b8Sopenharmony_ci string log[] = {"Tid:", "Name:", "Tid:", "Name:"}; 156800b99b8Sopenharmony_ci log[0] = log[0] + std::to_string(testPid1); 157800b99b8Sopenharmony_ci log[1] = log[1] + testProcess1; 158800b99b8Sopenharmony_ci log[2] = log[2] + std::to_string(testPid2); 159800b99b8Sopenharmony_ci log[3] = log[3] + testProcess2; 160800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 161800b99b8Sopenharmony_ci int count = GetKeywordsNum(msg, log, len); 162800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest001 Failed"; 163800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest001: end."; 164800b99b8Sopenharmony_ci} 165800b99b8Sopenharmony_ci 166800b99b8Sopenharmony_ci/** 167800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest002 168800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchMultiPid API: multiPid{0, 0} 169800b99b8Sopenharmony_ci * @tc.type: FUNC 170800b99b8Sopenharmony_ci */ 171800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest002, TestSize.Level2) 172800b99b8Sopenharmony_ci{ 173800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest002: start."; 174800b99b8Sopenharmony_ci int testPid1 = 0; 175800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "testPid1:" << testPid1; 176800b99b8Sopenharmony_ci int testPid2 = 0; 177800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "testPid2:" << testPid2; 178800b99b8Sopenharmony_ci std::vector<int> multiPid {testPid1, testPid2}; 179800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 180800b99b8Sopenharmony_ci std::string msg = ""; 181800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchMultiPid(multiPid, msg); 182800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 183800b99b8Sopenharmony_ci EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest002 Failed"; 184800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest002: end."; 185800b99b8Sopenharmony_ci} 186800b99b8Sopenharmony_ci 187800b99b8Sopenharmony_ci/** 188800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest003 189800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchMultiPid API: multiPid{-11, -11} 190800b99b8Sopenharmony_ci * @tc.type: FUNC 191800b99b8Sopenharmony_ci */ 192800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest003, TestSize.Level2) 193800b99b8Sopenharmony_ci{ 194800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest003: start."; 195800b99b8Sopenharmony_ci int testPid1 = -11; 196800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "testPid1:" << testPid1; 197800b99b8Sopenharmony_ci int testPid2 = -11; 198800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "testPid2:" << testPid2; 199800b99b8Sopenharmony_ci std::vector<int> multiPid {testPid1, testPid2}; 200800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 201800b99b8Sopenharmony_ci std::string msg = ""; 202800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchMultiPid(multiPid, msg); 203800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 204800b99b8Sopenharmony_ci EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest003 Failed"; 205800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest003: end."; 206800b99b8Sopenharmony_ci} 207800b99b8Sopenharmony_ci 208800b99b8Sopenharmony_ci/** 209800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest004 210800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), 0} 211800b99b8Sopenharmony_ci * @tc.type: FUNC 212800b99b8Sopenharmony_ci */ 213800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest004, TestSize.Level2) 214800b99b8Sopenharmony_ci{ 215800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest004: start."; 216800b99b8Sopenharmony_ci std::string testProcess = "accountmgr"; 217800b99b8Sopenharmony_ci int applyPid1 = GetProcessPid(testProcess); 218800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "applyPid1:" << applyPid1; 219800b99b8Sopenharmony_ci int applyPid2 = 0; 220800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "applyPid2:" << applyPid2; 221800b99b8Sopenharmony_ci std::vector<int> multiPid {applyPid1, applyPid2}; 222800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 223800b99b8Sopenharmony_ci std::string msg = ""; 224800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchMultiPid(multiPid, msg); 225800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 226800b99b8Sopenharmony_ci string log[] = { "Tid:", "Name:", "Failed" }; 227800b99b8Sopenharmony_ci log[0] = log[0] + std::to_string(applyPid1); 228800b99b8Sopenharmony_ci log[1] = log[1] + "accountmgr"; 229800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 230800b99b8Sopenharmony_ci int count = GetKeywordsNum(msg, log, len); 231800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest004 Failed"; 232800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest004: end."; 233800b99b8Sopenharmony_ci} 234800b99b8Sopenharmony_ci 235800b99b8Sopenharmony_ci/** 236800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest005 237800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr),PID(foundation),PID(systemui)} 238800b99b8Sopenharmony_ci * @tc.type: FUNC 239800b99b8Sopenharmony_ci */ 240800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest005, TestSize.Level2) 241800b99b8Sopenharmony_ci{ 242800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest005: start."; 243800b99b8Sopenharmony_ci std::vector<string> testProcessName = { "accountmgr", "foundation", "com.ohos.systemui" }; 244800b99b8Sopenharmony_ci string matchProcessName[] = { "accountmgr", "foundation", "m.ohos.systemui" }; 245800b99b8Sopenharmony_ci std::vector<int> multiPid; 246800b99b8Sopenharmony_ci std::vector<string> matchLog; 247800b99b8Sopenharmony_ci int index = 0; 248800b99b8Sopenharmony_ci for (string oneProcessName : testProcessName) { 249800b99b8Sopenharmony_ci int testPid = GetProcessPid(oneProcessName); 250800b99b8Sopenharmony_ci if (testPid == 0) { 251800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "process:" << oneProcessName << " pid is empty, skip"; 252800b99b8Sopenharmony_ci index++; 253800b99b8Sopenharmony_ci continue; 254800b99b8Sopenharmony_ci } 255800b99b8Sopenharmony_ci multiPid.emplace_back(testPid); 256800b99b8Sopenharmony_ci matchLog.emplace_back("Tid:" + std::to_string(testPid)); 257800b99b8Sopenharmony_ci matchLog.emplace_back("Name:" + matchProcessName[index]); 258800b99b8Sopenharmony_ci index++; 259800b99b8Sopenharmony_ci } 260800b99b8Sopenharmony_ci 261800b99b8Sopenharmony_ci // It is recommended that the number of effective pids be greater than 1, 262800b99b8Sopenharmony_ci // otherwise the testing purpose will not be achieved 263800b99b8Sopenharmony_ci EXPECT_GT(multiPid.size(), 1) << "DumpCatcherInterfacesTest005 Failed"; 264800b99b8Sopenharmony_ci 265800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 266800b99b8Sopenharmony_ci std::string msg = ""; 267800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchMultiPid(multiPid, msg); 268800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "ret:" << ret; 269800b99b8Sopenharmony_ci 270800b99b8Sopenharmony_ci int matchLogCount = matchLog.size(); 271800b99b8Sopenharmony_ci auto matchLogArray = std::make_unique<string[]>(matchLogCount); 272800b99b8Sopenharmony_ci index = 0; 273800b99b8Sopenharmony_ci for (string info : matchLog) { 274800b99b8Sopenharmony_ci matchLogArray[index] = info; 275800b99b8Sopenharmony_ci index++; 276800b99b8Sopenharmony_ci } 277800b99b8Sopenharmony_ci int count = GetKeywordsNum(msg, matchLogArray.get(), matchLogCount); 278800b99b8Sopenharmony_ci EXPECT_EQ(count, matchLogCount) << msg << "DumpCatcherInterfacesTest005 Failed"; 279800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest005: end."; 280800b99b8Sopenharmony_ci} 281800b99b8Sopenharmony_ci 282800b99b8Sopenharmony_ci/** 283800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest006 284800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), -11} 285800b99b8Sopenharmony_ci * @tc.type: FUNC 286800b99b8Sopenharmony_ci */ 287800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest006, TestSize.Level2) 288800b99b8Sopenharmony_ci{ 289800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest006: start."; 290800b99b8Sopenharmony_ci std::string testProcess = "accountmgr"; 291800b99b8Sopenharmony_ci int testPid1 = GetProcessPid(testProcess); 292800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "applyPid1:" << testPid1; 293800b99b8Sopenharmony_ci int testPid2 = -11; 294800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "applyPid2:" << testPid2; 295800b99b8Sopenharmony_ci std::vector<int> multiPid {testPid1, testPid2}; 296800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 297800b99b8Sopenharmony_ci std::string msg = ""; 298800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchMultiPid(multiPid, msg); 299800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 300800b99b8Sopenharmony_ci string log[] = { "Tid:", "Name:", "Failed"}; 301800b99b8Sopenharmony_ci log[0] = log[0] + std::to_string(testPid1); 302800b99b8Sopenharmony_ci log[1] = log[1] + "accountmgr"; 303800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 304800b99b8Sopenharmony_ci int count = GetKeywordsNum(msg, log, len); 305800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest006 Failed"; 306800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest006: end."; 307800b99b8Sopenharmony_ci} 308800b99b8Sopenharmony_ci 309800b99b8Sopenharmony_ci/** 310800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest007 311800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchMultiPid API: multiPid{9999, 9999} 312800b99b8Sopenharmony_ci * @tc.type: FUNC 313800b99b8Sopenharmony_ci */ 314800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest007, TestSize.Level2) 315800b99b8Sopenharmony_ci{ 316800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest007: start."; 317800b99b8Sopenharmony_ci int applyPid = 9999; 318800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "applyPid1:" << applyPid; 319800b99b8Sopenharmony_ci std::vector<int> multiPid {applyPid, applyPid}; 320800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 321800b99b8Sopenharmony_ci std::string msg = ""; 322800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchMultiPid(multiPid, msg); 323800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 324800b99b8Sopenharmony_ci EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest007 Failed"; 325800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest007: end."; 326800b99b8Sopenharmony_ci} 327800b99b8Sopenharmony_ci 328800b99b8Sopenharmony_ci/** 329800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest008 330800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchMultiPid API: multiPid{PID(accountmgr), 9999} 331800b99b8Sopenharmony_ci * @tc.type: FUNC 332800b99b8Sopenharmony_ci */ 333800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest008, TestSize.Level2) 334800b99b8Sopenharmony_ci{ 335800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest008: start."; 336800b99b8Sopenharmony_ci std::string apply = "accountmgr"; 337800b99b8Sopenharmony_ci int applyPid1 = GetProcessPid(apply); 338800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "applyPid1:" << applyPid1; 339800b99b8Sopenharmony_ci int applyPid2 = 9999; 340800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "applyPid2:" << applyPid2; 341800b99b8Sopenharmony_ci std::vector<int> multiPid {applyPid1, applyPid2}; 342800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 343800b99b8Sopenharmony_ci std::string msg = ""; 344800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchMultiPid(multiPid, msg); 345800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 346800b99b8Sopenharmony_ci string log[] = { "Tid:", "Name:", "Failed"}; 347800b99b8Sopenharmony_ci log[0] = log[0] + std::to_string(applyPid1); 348800b99b8Sopenharmony_ci log[1] = log[1] + apply; 349800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 350800b99b8Sopenharmony_ci int count = GetKeywordsNum(msg, log, len); 351800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest008 Failed"; 352800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest008: end."; 353800b99b8Sopenharmony_ci} 354800b99b8Sopenharmony_ci 355800b99b8Sopenharmony_ci/** 356800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest014 357800b99b8Sopenharmony_ci * @tc.desc: test DumpCatch API: PID(test hap), TID(0) 358800b99b8Sopenharmony_ci * @tc.type: FUNC 359800b99b8Sopenharmony_ci * @tc.require: issueI5PJ9O 360800b99b8Sopenharmony_ci */ 361800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest014, TestSize.Level2) 362800b99b8Sopenharmony_ci{ 363800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest014: start."; 364800b99b8Sopenharmony_ci bool isSuccess = g_testPid != 0; 365800b99b8Sopenharmony_ci if (!isSuccess) { 366800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 367800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Failed to launch target hap."; 368800b99b8Sopenharmony_ci return; 369800b99b8Sopenharmony_ci } 370800b99b8Sopenharmony_ci isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME); 371800b99b8Sopenharmony_ci if (!isSuccess) { 372800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 373800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Error process comm"; 374800b99b8Sopenharmony_ci return; 375800b99b8Sopenharmony_ci } 376800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 377800b99b8Sopenharmony_ci std::string msg = ""; 378800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatch(g_testPid, 0, msg); 379800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 380800b99b8Sopenharmony_ci string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn", "Name:OS_DfxWatchdog" }; 381800b99b8Sopenharmony_ci log[0] += std::to_string(g_testPid); 382800b99b8Sopenharmony_ci log[1] += TRUNCATE_TEST_BUNDLE_NAME; 383800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 384800b99b8Sopenharmony_ci int count = GetKeywordsNum(msg, log, len); 385800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest014 Failed"; 386800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest014: end."; 387800b99b8Sopenharmony_ci} 388800b99b8Sopenharmony_ci 389800b99b8Sopenharmony_ci/** 390800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest015 391800b99b8Sopenharmony_ci * @tc.desc: test DumpCatch API: PID(test hap), TID(test hap main thread) 392800b99b8Sopenharmony_ci * @tc.type: FUNC 393800b99b8Sopenharmony_ci * @tc.require: issueI5PJ9O 394800b99b8Sopenharmony_ci */ 395800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest015, TestSize.Level2) 396800b99b8Sopenharmony_ci{ 397800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest015: start."; 398800b99b8Sopenharmony_ci bool isSuccess = g_testPid != 0; 399800b99b8Sopenharmony_ci if (!isSuccess) { 400800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 401800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Failed to launch target hap."; 402800b99b8Sopenharmony_ci return; 403800b99b8Sopenharmony_ci } 404800b99b8Sopenharmony_ci isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME); 405800b99b8Sopenharmony_ci if (!isSuccess) { 406800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 407800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Error process comm"; 408800b99b8Sopenharmony_ci return; 409800b99b8Sopenharmony_ci } 410800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 411800b99b8Sopenharmony_ci std::string msg = ""; 412800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatch(g_testPid, g_testPid, msg); 413800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 414800b99b8Sopenharmony_ci string log[] = { "Tid:", "Name:", "#00", "/system/bin/appspawn"}; 415800b99b8Sopenharmony_ci log[0] += std::to_string(g_testPid); 416800b99b8Sopenharmony_ci log[1] += TRUNCATE_TEST_BUNDLE_NAME; 417800b99b8Sopenharmony_ci int len = sizeof(log) / sizeof(log[0]); 418800b99b8Sopenharmony_ci int count = GetKeywordsNum(msg, log, len); 419800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << msg; 420800b99b8Sopenharmony_ci EXPECT_EQ(count, len) << msg << "DumpCatcherInterfacesTest015 Failed"; 421800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest015: end."; 422800b99b8Sopenharmony_ci} 423800b99b8Sopenharmony_ci 424800b99b8Sopenharmony_ci/** 425800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest016 426800b99b8Sopenharmony_ci * @tc.desc: test DumpCatch API: PID(test hap), TID(-1) 427800b99b8Sopenharmony_ci * @tc.type: FUNC 428800b99b8Sopenharmony_ci * @tc.require: issueI5PJ9O 429800b99b8Sopenharmony_ci */ 430800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest016, TestSize.Level2) 431800b99b8Sopenharmony_ci{ 432800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest016: start."; 433800b99b8Sopenharmony_ci bool isSuccess = g_testPid != 0; 434800b99b8Sopenharmony_ci if (!isSuccess) { 435800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 436800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Failed to launch target hap."; 437800b99b8Sopenharmony_ci return; 438800b99b8Sopenharmony_ci } 439800b99b8Sopenharmony_ci isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME); 440800b99b8Sopenharmony_ci if (!isSuccess) { 441800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 442800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Error process comm"; 443800b99b8Sopenharmony_ci return; 444800b99b8Sopenharmony_ci } 445800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 446800b99b8Sopenharmony_ci std::string msg = ""; 447800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatch(g_testPid, -1, msg); 448800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 449800b99b8Sopenharmony_ci EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest016 Failed"; 450800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest016: end."; 451800b99b8Sopenharmony_ci} 452800b99b8Sopenharmony_ci 453800b99b8Sopenharmony_ci/** 454800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest017 455800b99b8Sopenharmony_ci * @tc.desc: test DumpCatch API: PID(-1), TID(-1) 456800b99b8Sopenharmony_ci * @tc.type: FUNC 457800b99b8Sopenharmony_ci * @tc.require: issueI5PJ9O 458800b99b8Sopenharmony_ci */ 459800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest017, TestSize.Level2) 460800b99b8Sopenharmony_ci{ 461800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest017: start."; 462800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 463800b99b8Sopenharmony_ci std::string msg = ""; 464800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatch(-1, -1, msg); 465800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 466800b99b8Sopenharmony_ci EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest017 Failed"; 467800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest017: end."; 468800b99b8Sopenharmony_ci} 469800b99b8Sopenharmony_ci 470800b99b8Sopenharmony_ci/** 471800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest018 472800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(gettid()) 473800b99b8Sopenharmony_ci * @tc.type: FUNC 474800b99b8Sopenharmony_ci */ 475800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest018, TestSize.Level2) 476800b99b8Sopenharmony_ci{ 477800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest018: start."; 478800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 479800b99b8Sopenharmony_ci std::string msg = ""; 480800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(getpid(), gettid(), msg, 1); 481800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 482800b99b8Sopenharmony_ci EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest018 Failed"; 483800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest018: end."; 484800b99b8Sopenharmony_ci} 485800b99b8Sopenharmony_ci 486800b99b8Sopenharmony_ci/** 487800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest019 488800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(0) 489800b99b8Sopenharmony_ci * @tc.type: FUNC 490800b99b8Sopenharmony_ci */ 491800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest019, TestSize.Level2) 492800b99b8Sopenharmony_ci{ 493800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest019: start."; 494800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 495800b99b8Sopenharmony_ci std::string msg = ""; 496800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(getpid(), 0, msg, 1); 497800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 498800b99b8Sopenharmony_ci EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest019 Failed"; 499800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest019: end."; 500800b99b8Sopenharmony_ci} 501800b99b8Sopenharmony_ci 502800b99b8Sopenharmony_ci/** 503800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest020 504800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(-1) 505800b99b8Sopenharmony_ci * @tc.type: FUNC 506800b99b8Sopenharmony_ci */ 507800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest020, TestSize.Level2) 508800b99b8Sopenharmony_ci{ 509800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest020: start."; 510800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 511800b99b8Sopenharmony_ci std::string msg = ""; 512800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(getpid(), -1, msg, 1); 513800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 514800b99b8Sopenharmony_ci EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest020 Failed"; 515800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest020: end."; 516800b99b8Sopenharmony_ci} 517800b99b8Sopenharmony_ci 518800b99b8Sopenharmony_ci 519800b99b8Sopenharmony_ci/** 520800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest021 521800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(0) 522800b99b8Sopenharmony_ci * @tc.type: FUNC 523800b99b8Sopenharmony_ci */ 524800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest021, TestSize.Level2) 525800b99b8Sopenharmony_ci{ 526800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest021: start."; 527800b99b8Sopenharmony_ci std::string apply = "accountmgr"; 528800b99b8Sopenharmony_ci int applyPid = GetProcessPid(apply); 529800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid; 530800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 531800b99b8Sopenharmony_ci std::string msg = ""; 532800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(applyPid, 0, msg, 1); 533800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 534800b99b8Sopenharmony_ci EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest021 Failed"; 535800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest021: end."; 536800b99b8Sopenharmony_ci} 537800b99b8Sopenharmony_ci 538800b99b8Sopenharmony_ci/** 539800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest022 540800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(accountmgr main thread) 541800b99b8Sopenharmony_ci * @tc.type: FUNC 542800b99b8Sopenharmony_ci */ 543800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest022, TestSize.Level2) 544800b99b8Sopenharmony_ci{ 545800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest022: start."; 546800b99b8Sopenharmony_ci std::string apply = "accountmgr"; 547800b99b8Sopenharmony_ci int applyPid = GetProcessPid(apply); 548800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid; 549800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 550800b99b8Sopenharmony_ci std::string msg = ""; 551800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(applyPid, applyPid, msg, 1); 552800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 553800b99b8Sopenharmony_ci EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest022 Failed"; 554800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest022: end."; 555800b99b8Sopenharmony_ci} 556800b99b8Sopenharmony_ci 557800b99b8Sopenharmony_ci/** 558800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest023 559800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(-1) 560800b99b8Sopenharmony_ci * @tc.type: FUNC 561800b99b8Sopenharmony_ci */ 562800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest023, TestSize.Level2) 563800b99b8Sopenharmony_ci{ 564800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest023: start."; 565800b99b8Sopenharmony_ci std::string apply = "accountmgr"; 566800b99b8Sopenharmony_ci int applyPid = GetProcessPid(apply); 567800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid; 568800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 569800b99b8Sopenharmony_ci std::string msg = ""; 570800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(applyPid, -1, msg, 1); 571800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 572800b99b8Sopenharmony_ci EXPECT_EQ(ret, false) << "DumpCatcherInterfacesTest023 Failed"; 573800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest023: end."; 574800b99b8Sopenharmony_ci} 575800b99b8Sopenharmony_ci 576800b99b8Sopenharmony_ci/** 577800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest024 578800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(accountmgr), TID(9999) 579800b99b8Sopenharmony_ci * @tc.type: FUNC 580800b99b8Sopenharmony_ci */ 581800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest024, TestSize.Level2) 582800b99b8Sopenharmony_ci{ 583800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest024: start."; 584800b99b8Sopenharmony_ci std::string apply = "accountmgr"; 585800b99b8Sopenharmony_ci int applyPid = GetProcessPid(apply); 586800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "apply:" << apply << ", pid:" << applyPid; 587800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 588800b99b8Sopenharmony_ci std::string msg = ""; 589800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(applyPid, 9999, msg, 1); 590800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 591800b99b8Sopenharmony_ci EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest024 Failed"; 592800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest024: end."; 593800b99b8Sopenharmony_ci} 594800b99b8Sopenharmony_ci 595800b99b8Sopenharmony_ci/** 596800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest025 597800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(9999) 598800b99b8Sopenharmony_ci * @tc.type: FUNC 599800b99b8Sopenharmony_ci */ 600800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest025, TestSize.Level2) 601800b99b8Sopenharmony_ci{ 602800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest025: start."; 603800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 604800b99b8Sopenharmony_ci std::string msg = ""; 605800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(getpid(), 9999, msg, 1); 606800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 607800b99b8Sopenharmony_ci EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest025 Failed"; 608800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest025: end."; 609800b99b8Sopenharmony_ci} 610800b99b8Sopenharmony_ci 611800b99b8Sopenharmony_ci/** 612800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest026 613800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread) 614800b99b8Sopenharmony_ci * @tc.type: FUNC 615800b99b8Sopenharmony_ci */ 616800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest026, TestSize.Level2) 617800b99b8Sopenharmony_ci{ 618800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest026: start."; 619800b99b8Sopenharmony_ci MultiThreadConstructor(); 620800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 621800b99b8Sopenharmony_ci std::string msg = ""; 622800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "dump local process, " << " tid:" << g_threadId; 623800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1); 624800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 625800b99b8Sopenharmony_ci EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest026 Failed"; 626800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest026: end."; 627800b99b8Sopenharmony_ci} 628800b99b8Sopenharmony_ci 629800b99b8Sopenharmony_ci/** 630800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest027 631800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(child process), TID(child thread of child process) 632800b99b8Sopenharmony_ci * @tc.type: FUNC 633800b99b8Sopenharmony_ci */ 634800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest027, TestSize.Level2) 635800b99b8Sopenharmony_ci{ 636800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest027: start."; 637800b99b8Sopenharmony_ci ForkMultiThreadProcess(); 638800b99b8Sopenharmony_ci std::vector<int> tids; 639800b99b8Sopenharmony_ci std::vector<int> nstids; 640800b99b8Sopenharmony_ci bool isSuccess = GetTidsByPid(g_processId, tids, nstids); 641800b99b8Sopenharmony_ci if (!isSuccess) { 642800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 643800b99b8Sopenharmony_ci return; 644800b99b8Sopenharmony_ci } 645800b99b8Sopenharmony_ci int childTid = tids[1]; // 1 : child thread 646800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "dump remote process, " << " pid:" << g_processId << ", tid:" << childTid; 647800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 648800b99b8Sopenharmony_ci std::string msg = ""; 649800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(g_processId, childTid, msg, 1); 650800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 651800b99b8Sopenharmony_ci EXPECT_TRUE(ret) << "DumpCatcherInterfacesTest027 Failed"; 652800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest027: end."; 653800b99b8Sopenharmony_ci} 654800b99b8Sopenharmony_ci 655800b99b8Sopenharmony_ci/** 656800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest028 657800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread) and config FrameNum 658800b99b8Sopenharmony_ci * @tc.type: FUNC 659800b99b8Sopenharmony_ci */ 660800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest028, TestSize.Level2) 661800b99b8Sopenharmony_ci{ 662800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest028: start."; 663800b99b8Sopenharmony_ci RecursiveMultiThreadConstructor(); 664800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 665800b99b8Sopenharmony_ci std::string msg = ""; 666800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "dump local process, " << " tid:" << g_threadId; 667800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1, 10); // 10 means backtrace frames is 10 668800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "message:" << msg; 669800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 670800b99b8Sopenharmony_ci EXPECT_TRUE(msg.find("#09") != std::string::npos); 671800b99b8Sopenharmony_ci EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest028 Failed"; 672800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest028: end."; 673800b99b8Sopenharmony_ci} 674800b99b8Sopenharmony_ci 675800b99b8Sopenharmony_ci/** 676800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest029 677800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchFd API: PID(getpid()), TID(child thread) and DEFAULT_MAX_FRAME_NUM 678800b99b8Sopenharmony_ci * @tc.type: FUNC 679800b99b8Sopenharmony_ci */ 680800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest029, TestSize.Level2) 681800b99b8Sopenharmony_ci{ 682800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest029: start."; 683800b99b8Sopenharmony_ci RecursiveMultiThreadConstructor(); 684800b99b8Sopenharmony_ci usleep(CREATE_THREAD_TIMEOUT); 685800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 686800b99b8Sopenharmony_ci std::string msg = ""; 687800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "dump local process, " << " tid:" << g_threadId; 688800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatchFd(getpid(), g_threadId, msg, 1); 689800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "message:" << msg; 690800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << ret; 691800b99b8Sopenharmony_ci#if defined(__aarch64__) 692800b99b8Sopenharmony_ci std::string stackKeyword = std::string("#") + std::to_string(DEFAULT_MAX_LOCAL_FRAME_NUM - 1); 693800b99b8Sopenharmony_ci#else 694800b99b8Sopenharmony_ci std::string stackKeyword = std::string("#") + std::to_string(DEFAULT_MAX_FRAME_NUM - 1); 695800b99b8Sopenharmony_ci#endif 696800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "stackKeyword:" << stackKeyword; 697800b99b8Sopenharmony_ci EXPECT_TRUE(msg.find(stackKeyword.c_str()) != std::string::npos); 698800b99b8Sopenharmony_ci EXPECT_EQ(ret, true) << "DumpCatcherInterfacesTest029 Failed"; 699800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest029: end."; 700800b99b8Sopenharmony_ci} 701800b99b8Sopenharmony_ci 702800b99b8Sopenharmony_ci#ifndef is_ohos_lite 703800b99b8Sopenharmony_ci/** 704800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest030 705800b99b8Sopenharmony_ci * @tc.desc: test DumpCatch remote API: PID(getpid()), TID(child thread) 706800b99b8Sopenharmony_ci * and maxFrameNums(DEFAULT_MAX_FRAME_NUM), isJson(true) 707800b99b8Sopenharmony_ci * @tc.type: FUNC 708800b99b8Sopenharmony_ci */ 709800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest030, TestSize.Level2) 710800b99b8Sopenharmony_ci{ 711800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest030: start."; 712800b99b8Sopenharmony_ci pid_t pid = fork(); 713800b99b8Sopenharmony_ci if (pid == 0) { 714800b99b8Sopenharmony_ci std::this_thread::sleep_for(std::chrono::seconds(10)); 715800b99b8Sopenharmony_ci _exit(0); 716800b99b8Sopenharmony_ci } 717800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "dump remote process, " << " pid:" << pid << ", tid:" << 0; 718800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 719800b99b8Sopenharmony_ci DfxJsonFormatter format; 720800b99b8Sopenharmony_ci string msg = ""; 721800b99b8Sopenharmony_ci bool ret = dumplog.DumpCatch(pid, 0, msg); 722800b99b8Sopenharmony_ci EXPECT_TRUE(ret) << "DumpCatch remote msg Failed."; 723800b99b8Sopenharmony_ci string jsonMsg = ""; 724800b99b8Sopenharmony_ci bool jsonRet = dumplog.DumpCatch(pid, 0, jsonMsg, DEFAULT_MAX_FRAME_NUM, true); 725800b99b8Sopenharmony_ci std::cout << jsonMsg << std::endl; 726800b99b8Sopenharmony_ci EXPECT_TRUE(jsonRet) << "DumpCatch remote json Failed."; 727800b99b8Sopenharmony_ci string stackMsg = ""; 728800b99b8Sopenharmony_ci bool formatRet = format.FormatJsonStack(jsonMsg, stackMsg); 729800b99b8Sopenharmony_ci EXPECT_TRUE(formatRet) << "FormatJsonStack Failed."; 730800b99b8Sopenharmony_ci size_t pos = msg.find("Process name:"); 731800b99b8Sopenharmony_ci if (pos != std::string::npos) { 732800b99b8Sopenharmony_ci msg = msg.erase(0, pos); 733800b99b8Sopenharmony_ci msg = msg.erase(0, msg.find("\n") + 1); 734800b99b8Sopenharmony_ci } else { 735800b99b8Sopenharmony_ci msg = msg.erase(0, msg.find("\n") + 1); 736800b99b8Sopenharmony_ci } 737800b99b8Sopenharmony_ci EXPECT_EQ(stackMsg == msg, true) << "stackMsg != msg"; 738800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest030: end."; 739800b99b8Sopenharmony_ci} 740800b99b8Sopenharmony_ci 741800b99b8Sopenharmony_ci/** 742800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest031 743800b99b8Sopenharmony_ci * @tc.desc: test DumpCatchProcess 744800b99b8Sopenharmony_ci * @tc.type: FUNC 745800b99b8Sopenharmony_ci */ 746800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest031, TestSize.Level2) 747800b99b8Sopenharmony_ci{ 748800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest031: start."; 749800b99b8Sopenharmony_ci std::string res = ExecuteCommands("uname"); 750800b99b8Sopenharmony_ci bool isSuccess = res.find("Linux") == std::string::npos; 751800b99b8Sopenharmony_ci if (!isSuccess) { 752800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 753800b99b8Sopenharmony_ci return; 754800b99b8Sopenharmony_ci } 755800b99b8Sopenharmony_ci isSuccess = g_testPid != 0; 756800b99b8Sopenharmony_ci if (!isSuccess) { 757800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 758800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Failed to launch target hap."; 759800b99b8Sopenharmony_ci return; 760800b99b8Sopenharmony_ci } 761800b99b8Sopenharmony_ci isSuccess = CheckProcessComm(g_testPid, TRUNCATE_TEST_BUNDLE_NAME); 762800b99b8Sopenharmony_ci if (!isSuccess) { 763800b99b8Sopenharmony_ci ASSERT_FALSE(isSuccess); 764800b99b8Sopenharmony_ci GTEST_LOG_(ERROR) << "Error process comm"; 765800b99b8Sopenharmony_ci return; 766800b99b8Sopenharmony_ci } 767800b99b8Sopenharmony_ci std::string stopProcessCmd = "kill -s SIGSTOP $(pidof com.example.myapplication)"; 768800b99b8Sopenharmony_ci ExecuteCommands(stopProcessCmd); 769800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 770800b99b8Sopenharmony_ci std::string msg = ""; 771800b99b8Sopenharmony_ci ASSERT_EQ(dumplog.DumpCatchProcess(g_testPid, msg), 1); //kernel stack 772800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << msg; 773800b99b8Sopenharmony_ci std::string formattedStack = ""; 774800b99b8Sopenharmony_ci ASSERT_TRUE(DfxJsonFormatter::FormatKernelStack(msg, formattedStack, false)); 775800b99b8Sopenharmony_ci ASSERT_GT(formattedStack.size(), 0); 776800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << formattedStack; 777800b99b8Sopenharmony_ci ASSERT_NE(formattedStack.find("#"), std::string::npos); 778800b99b8Sopenharmony_ci ASSERT_TRUE(DfxJsonFormatter::FormatKernelStack(msg, formattedStack, true)); 779800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << formattedStack; 780800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest031: end."; 781800b99b8Sopenharmony_ci} 782800b99b8Sopenharmony_ci#endif 783800b99b8Sopenharmony_ci 784800b99b8Sopenharmony_ci#ifndef is_ohos_lite 785800b99b8Sopenharmony_ci/** 786800b99b8Sopenharmony_ci * @tc.name: DumpCatcherInterfacesTest032 787800b99b8Sopenharmony_ci * @tc.desc: test DfxJsonFormatter 788800b99b8Sopenharmony_ci * @tc.type: FUNC 789800b99b8Sopenharmony_ci */ 790800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest032, TestSize.Level2) 791800b99b8Sopenharmony_ci{ 792800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest032: start."; 793800b99b8Sopenharmony_ci DfxJsonFormatter format; 794800b99b8Sopenharmony_ci string outStackStr = ""; 795800b99b8Sopenharmony_ci string errorJsonMsg = "{\"test\"}"; 796800b99b8Sopenharmony_ci bool formatRet = format.FormatJsonStack(errorJsonMsg, outStackStr); 797800b99b8Sopenharmony_ci EXPECT_FALSE(formatRet); 798800b99b8Sopenharmony_ci 799800b99b8Sopenharmony_ci outStackStr = ""; 800800b99b8Sopenharmony_ci string noThreadJsonMsg = "[{\"tid\" : \"1\"}]"; 801800b99b8Sopenharmony_ci formatRet = format.FormatJsonStack(noThreadJsonMsg, outStackStr); 802800b99b8Sopenharmony_ci EXPECT_TRUE(formatRet); 803800b99b8Sopenharmony_ci 804800b99b8Sopenharmony_ci outStackStr = ""; 805800b99b8Sopenharmony_ci string noTidJsonMsg = "[{\"thread_name\" : \"test\"}]"; 806800b99b8Sopenharmony_ci formatRet = format.FormatJsonStack(noTidJsonMsg, outStackStr); 807800b99b8Sopenharmony_ci EXPECT_TRUE(formatRet); 808800b99b8Sopenharmony_ci 809800b99b8Sopenharmony_ci outStackStr = ""; 810800b99b8Sopenharmony_ci string jsJsonMsg = R"~([{"frames":[{"buildId":"", "file":"/system/lib/ld-musl-arm.so.1", 811800b99b8Sopenharmony_ci "offset":0, "pc":"000fdf4c", "symbol":""}, {"line":"1", "file":"/system/lib/ld-musl-arm.so.1", 812800b99b8Sopenharmony_ci "offset":628, "pc":"000ff7f4", "symbol":"__pthread_cond_timedwait_time64"}], 813800b99b8Sopenharmony_ci "thread_name":"OS_SignalHandle", "tid":1608}])~"; 814800b99b8Sopenharmony_ci formatRet = format.FormatJsonStack(jsJsonMsg, outStackStr); 815800b99b8Sopenharmony_ci EXPECT_TRUE(formatRet); 816800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest032: end."; 817800b99b8Sopenharmony_ci} 818800b99b8Sopenharmony_ci#endif 819800b99b8Sopenharmony_ci 820800b99b8Sopenharmony_ci/** 821800b99b8Sopenharmony_ci@tc.name: DumpCatcherInterfacesTest033 822800b99b8Sopenharmony_ci@tc.desc: testDump after crashed 823800b99b8Sopenharmony_ci@tc.type: FUNC 824800b99b8Sopenharmony_ci*/ 825800b99b8Sopenharmony_ciHWTEST_F(DumpCatcherInterfacesTest, DumpCatcherInterfacesTest033, TestSize.Level2) 826800b99b8Sopenharmony_ci{ 827800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest033: start."; 828800b99b8Sopenharmony_ci pid_t pid = fork(); 829800b99b8Sopenharmony_ci if (pid == 0) { 830800b99b8Sopenharmony_ci int32_t fd = RequestFileDescriptor(FaultLoggerType::CPP_CRASH); 831800b99b8Sopenharmony_ci ASSERT_GT(fd, 0); 832800b99b8Sopenharmony_ci close(fd); 833800b99b8Sopenharmony_ci std::this_thread::sleep_for(std::chrono::seconds(10)); 834800b99b8Sopenharmony_ci _exit(0); 835800b99b8Sopenharmony_ci } else if (pid < 0) { 836800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "Fail in fork."; 837800b99b8Sopenharmony_ci } else { 838800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "dump remote process, " << "pid:" << pid << ", tid:" << 0; 839800b99b8Sopenharmony_ci DfxDumpCatcher dumplog; 840800b99b8Sopenharmony_ci string msg = ""; 841800b99b8Sopenharmony_ci EXPECT_FALSE(dumplog.DumpCatch(pid, 0, msg)); 842800b99b8Sopenharmony_ci constexpr int validTime = 8; 843800b99b8Sopenharmony_ci sleep(validTime); 844800b99b8Sopenharmony_ci msg = ""; 845800b99b8Sopenharmony_ci EXPECT_TRUE(dumplog.DumpCatch(pid, 0, msg)); 846800b99b8Sopenharmony_ci } 847800b99b8Sopenharmony_ci GTEST_LOG_(INFO) << "DumpCatcherInterfacesTest033: end."; 848800b99b8Sopenharmony_ci} 849800b99b8Sopenharmony_ci} // namespace HiviewDFX 850800b99b8Sopenharmony_ci} // namepsace OHOS