1800b99b8Sopenharmony_ci/* 2800b99b8Sopenharmony_ci * Copyright (c) 2022-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 "faultloggerd_module_test.h" 17800b99b8Sopenharmony_ci 18800b99b8Sopenharmony_ci#include <securec.h> 19800b99b8Sopenharmony_ci#include <sstream> 20800b99b8Sopenharmony_ci#include <unistd.h> 21800b99b8Sopenharmony_ci 22800b99b8Sopenharmony_ci#include "dfx_test_util.h" 23800b99b8Sopenharmony_ci#include "faultloggerd_client.h" 24800b99b8Sopenharmony_ci 25800b99b8Sopenharmony_ciusing namespace OHOS::HiviewDFX; 26800b99b8Sopenharmony_ciusing namespace testing::ext; 27800b99b8Sopenharmony_ci 28800b99b8Sopenharmony_cinamespace { 29800b99b8Sopenharmony_civoid WaitForServiceReady(const std::string& serviceName) 30800b99b8Sopenharmony_ci{ 31800b99b8Sopenharmony_ci int pid = GetProcessPid(serviceName); 32800b99b8Sopenharmony_ci if (pid <= 0) { 33800b99b8Sopenharmony_ci std::string cmd = "start " + serviceName; 34800b99b8Sopenharmony_ci ExecuteCommands(cmd); 35800b99b8Sopenharmony_ci const int sleepTime = 10; // 10 seconds 36800b99b8Sopenharmony_ci sleep(sleepTime); 37800b99b8Sopenharmony_ci pid = GetProcessPid(serviceName); 38800b99b8Sopenharmony_ci } 39800b99b8Sopenharmony_ci ASSERT_GT(pid, 0); 40800b99b8Sopenharmony_ci ASSERT_TRUE(CheckConnectStatus()); 41800b99b8Sopenharmony_ci} 42800b99b8Sopenharmony_ci 43800b99b8Sopenharmony_civoid CheckFdRequestFunction(FaultLoggerType type, bool isValidFd) 44800b99b8Sopenharmony_ci{ 45800b99b8Sopenharmony_ci int32_t fd = RequestFileDescriptor(static_cast<int32_t>(type)); 46800b99b8Sopenharmony_ci ASSERT_EQ((fd >= 0), isValidFd); 47800b99b8Sopenharmony_ci if (fd >= 0) { 48800b99b8Sopenharmony_ci close(fd); 49800b99b8Sopenharmony_ci } 50800b99b8Sopenharmony_ci} 51800b99b8Sopenharmony_ci 52800b99b8Sopenharmony_ci/** 53800b99b8Sopenharmony_ci * @tc.name: FaultloggerdClientPipeFdRquestTest001 54800b99b8Sopenharmony_ci * @tc.desc: check faultloggerd RequestPipeFd function 55800b99b8Sopenharmony_ci * @tc.type: FUNC 56800b99b8Sopenharmony_ci */ 57800b99b8Sopenharmony_ciHWTEST_F(FaultloggerdModuleTest, FaultloggerdClientPipeFdRquestTest001, TestSize.Level0) 58800b99b8Sopenharmony_ci{ 59800b99b8Sopenharmony_ci RequestSdkDump(getpid(), getpid()); 60800b99b8Sopenharmony_ci int32_t pipeFd = RequestPipeFd(getpid(), FaultLoggerPipeType::PIPE_FD_READ_BUF); 61800b99b8Sopenharmony_ci ASSERT_NE(pipeFd, -1); 62800b99b8Sopenharmony_ci int32_t ret = RequestDelPipeFd(getpid()); 63800b99b8Sopenharmony_ci ASSERT_EQ(ret, 0); 64800b99b8Sopenharmony_ci pipeFd = RequestPipeFd(getpid(), FaultLoggerPipeType::PIPE_FD_READ_BUF); 65800b99b8Sopenharmony_ci ASSERT_EQ(pipeFd, -1); 66800b99b8Sopenharmony_ci} 67800b99b8Sopenharmony_ci 68800b99b8Sopenharmony_ci/** 69800b99b8Sopenharmony_ci * @tc.name: FaultloggerdServiceTest001 70800b99b8Sopenharmony_ci * @tc.desc: check faultloggerd running status and ensure it has been started 71800b99b8Sopenharmony_ci * @tc.type: FUNC 72800b99b8Sopenharmony_ci */ 73800b99b8Sopenharmony_ciHWTEST_F(FaultloggerdModuleTest, FaultloggerdServiceTest001, TestSize.Level0) 74800b99b8Sopenharmony_ci{ 75800b99b8Sopenharmony_ci WaitForServiceReady("faultloggerd"); 76800b99b8Sopenharmony_ci} 77800b99b8Sopenharmony_ci 78800b99b8Sopenharmony_ci#if defined(__BIONIC__) 79800b99b8Sopenharmony_ci/** 80800b99b8Sopenharmony_ci * @tc.name: FaultloggerdDfxHandlerPreloadTest001 81800b99b8Sopenharmony_ci * @tc.desc: check whether libdfx_signalhandler.z.so is preloaded. 82800b99b8Sopenharmony_ci * @tc.type: FUNC 83800b99b8Sopenharmony_ci */ 84800b99b8Sopenharmony_ciHWTEST_F(FaultloggerdModuleTest, FaultloggerdDfxHandlerPreloadTest001, TestSize.Level0) 85800b99b8Sopenharmony_ci{ 86800b99b8Sopenharmony_ci std::string cmd = "cat /proc/" + std::to_string(getpid()) + "/maps"; 87800b99b8Sopenharmony_ci std::string result = ExecuteCommands(cmd); 88800b99b8Sopenharmony_ci ASSERT_EQ(result.find("libdfx_signalhandler.z.so") != std::string::npos, true); 89800b99b8Sopenharmony_ci} 90800b99b8Sopenharmony_ci#endif 91800b99b8Sopenharmony_ci 92800b99b8Sopenharmony_ci/** 93800b99b8Sopenharmony_ci * @tc.name: FaultloggerdClientFdRquestTest001 94800b99b8Sopenharmony_ci * @tc.desc: check faultloggerd logging function 95800b99b8Sopenharmony_ci * @tc.type: FUNC 96800b99b8Sopenharmony_ci */ 97800b99b8Sopenharmony_ciHWTEST_F(FaultloggerdModuleTest, FaultloggerdClientFdRquestTest001, TestSize.Level0) 98800b99b8Sopenharmony_ci{ 99800b99b8Sopenharmony_ci CheckFdRequestFunction(FaultLoggerType::CPP_CRASH, true); 100800b99b8Sopenharmony_ci CheckFdRequestFunction(FaultLoggerType::CPP_STACKTRACE, true); 101800b99b8Sopenharmony_ci CheckFdRequestFunction(FaultLoggerType::JS_STACKTRACE, true); 102800b99b8Sopenharmony_ci CheckFdRequestFunction(FaultLoggerType::JS_HEAP_SNAPSHOT, true); 103800b99b8Sopenharmony_ci CheckFdRequestFunction(FaultLoggerType::LEAK_STACKTRACE, true); 104800b99b8Sopenharmony_ci} 105800b99b8Sopenharmony_ci 106800b99b8Sopenharmony_ci/** 107800b99b8Sopenharmony_ci * @tc.name: FaultloggerdClientFdRquestTest002 108800b99b8Sopenharmony_ci * @tc.desc: check faultloggerd RequestLogFileDescriptor function 109800b99b8Sopenharmony_ci * @tc.type: FUNC 110800b99b8Sopenharmony_ci */ 111800b99b8Sopenharmony_ciHWTEST_F(FaultloggerdModuleTest, FaultloggerdClientFdRquestTest002, TestSize.Level0) 112800b99b8Sopenharmony_ci{ 113800b99b8Sopenharmony_ci struct FaultLoggerdRequest testRequest; 114800b99b8Sopenharmony_ci (void)memset_s(&testRequest, sizeof(testRequest), 0, sizeof(testRequest)); 115800b99b8Sopenharmony_ci testRequest.type = (int)FaultLoggerType::CPP_CRASH; 116800b99b8Sopenharmony_ci testRequest.pid = getpid(); 117800b99b8Sopenharmony_ci testRequest.tid = gettid(); 118800b99b8Sopenharmony_ci testRequest.uid = getuid(); 119800b99b8Sopenharmony_ci int32_t fd = RequestLogFileDescriptor(&testRequest); 120800b99b8Sopenharmony_ci ASSERT_TRUE(fd >= 0); 121800b99b8Sopenharmony_ci if (fd >= 0) { 122800b99b8Sopenharmony_ci close(fd); 123800b99b8Sopenharmony_ci } 124800b99b8Sopenharmony_ci} 125800b99b8Sopenharmony_ci 126800b99b8Sopenharmony_ci/** 127800b99b8Sopenharmony_ci * @tc.name: FaultloggerdClientFdRquestTest003 128800b99b8Sopenharmony_ci * @tc.desc: check faultloggerd RequestPrintTHilog function 129800b99b8Sopenharmony_ci * @tc.type: FUNC 130800b99b8Sopenharmony_ci */ 131800b99b8Sopenharmony_ciHWTEST_F(FaultloggerdModuleTest, FaultloggerdClientFdRquestTest003, TestSize.Level0) 132800b99b8Sopenharmony_ci{ 133800b99b8Sopenharmony_ci char msg[] = "test log"; 134800b99b8Sopenharmony_ci size_t len = strlen(msg); 135800b99b8Sopenharmony_ci ASSERT_EQ(RequestPrintTHilog(msg, len), 0); 136800b99b8Sopenharmony_ci} 137800b99b8Sopenharmony_ci 138800b99b8Sopenharmony_ci/** 139800b99b8Sopenharmony_ci * @tc.name: FaultloggerdClientFdRquestTest004 140800b99b8Sopenharmony_ci * @tc.desc: check faultloggerd RequestFileDescriptorEx function 141800b99b8Sopenharmony_ci * @tc.type: FUNC 142800b99b8Sopenharmony_ci */ 143800b99b8Sopenharmony_ciHWTEST_F(FaultloggerdModuleTest, FaultloggerdClientFdRquestTest004, TestSize.Level0) 144800b99b8Sopenharmony_ci{ 145800b99b8Sopenharmony_ci struct FaultLoggerdRequest testRequest; 146800b99b8Sopenharmony_ci (void)memset_s(&testRequest, sizeof(testRequest), 0, sizeof(testRequest)); 147800b99b8Sopenharmony_ci testRequest.type = (int)FaultLoggerType::CPP_CRASH; 148800b99b8Sopenharmony_ci testRequest.pid = getpid(); 149800b99b8Sopenharmony_ci testRequest.tid = gettid(); 150800b99b8Sopenharmony_ci testRequest.uid = getuid(); 151800b99b8Sopenharmony_ci int32_t fd = RequestFileDescriptorEx(&testRequest); 152800b99b8Sopenharmony_ci ASSERT_TRUE(fd >= 0); 153800b99b8Sopenharmony_ci if (fd >= 0) { 154800b99b8Sopenharmony_ci close(fd); 155800b99b8Sopenharmony_ci } 156800b99b8Sopenharmony_ci} 157800b99b8Sopenharmony_ci 158800b99b8Sopenharmony_ci/** 159800b99b8Sopenharmony_ci * @tc.name: FaultloggerdClientPipeFdRquestTest002 160800b99b8Sopenharmony_ci * @tc.desc: check faultloggerd RequestPipeFd function by param error 161800b99b8Sopenharmony_ci * @tc.type: FUNC 162800b99b8Sopenharmony_ci */ 163800b99b8Sopenharmony_ciHWTEST_F(FaultloggerdModuleTest, FaultloggerdClientPipeFdRquestTest002, TestSize.Level0) 164800b99b8Sopenharmony_ci{ 165800b99b8Sopenharmony_ci RequestSdkDump(getpid(), getpid()); 166800b99b8Sopenharmony_ci int32_t pipeFd = RequestPipeFd(getpid(), -1); 167800b99b8Sopenharmony_ci ASSERT_EQ(pipeFd, -1); 168800b99b8Sopenharmony_ci pipeFd = RequestPipeFd(getpid(), FaultLoggerPipeType::PIPE_FD_DELETE); 169800b99b8Sopenharmony_ci ASSERT_EQ(pipeFd, -1); 170800b99b8Sopenharmony_ci} 171800b99b8Sopenharmony_ci 172800b99b8Sopenharmony_ci/** 173800b99b8Sopenharmony_ci * @tc.name: FaultloggerdPermissionCheckTest001 174800b99b8Sopenharmony_ci * @tc.desc: check faultloggerd RequestCheckPermission function 175800b99b8Sopenharmony_ci * @tc.type: FUNC 176800b99b8Sopenharmony_ci */ 177800b99b8Sopenharmony_ciHWTEST_F(FaultloggerdModuleTest, FaultloggerdPermissionCheckTest001, TestSize.Level0) 178800b99b8Sopenharmony_ci{ 179800b99b8Sopenharmony_ci ASSERT_TRUE(RequestCheckPermission(getpid())); 180800b99b8Sopenharmony_ci} 181800b99b8Sopenharmony_ci} 182