• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-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 "faultloggerd_module_test.h"
17 
18 #include <securec.h>
19 #include <sstream>
20 #include <unistd.h>
21 
22 #include "dfx_define.h"
23 #include "dfx_test_util.h"
24 #include "faultloggerd_client.h"
25 
26 using namespace OHOS::HiviewDFX;
27 using namespace testing::ext;
28 
29 namespace {
WaitForServiceReady(const std::string & serviceName)30 void WaitForServiceReady(const std::string& serviceName)
31 {
32     int pid = GetProcessPid(serviceName);
33     if (pid <= 0) {
34         std::string cmd = "start " + serviceName;
35         ExecuteCommands(cmd);
36         const int sleepTime = 10; // 10 seconds
37         sleep(sleepTime);
38         pid = GetProcessPid(serviceName);
39     }
40     ASSERT_GT(pid, 0);
41 }
42 
CheckFdRequestFunction(int32_t type,bool isValidFd)43 void CheckFdRequestFunction(int32_t type, bool isValidFd)
44 {
45     int32_t fd = RequestFileDescriptor(type);
46     ASSERT_EQ((fd >= 0), isValidFd);
47     if (fd >= 0) {
48         close(fd);
49     }
50 }
51 
52 /**
53  * @tc.name: FaultloggerdClientPipeFdRquestTest001
54  * @tc.desc: check faultloggerd RequestPipeFd function
55  * @tc.type: FUNC
56  */
57 HWTEST_F(FaultloggerdModuleTest, FaultloggerdClientPipeFdRquestTest001, TestSize.Level0)
58 {
59     int pipeReadFd[] = { -1, -1 };
60     RequestSdkDump(getpid(), getpid(), pipeReadFd);
61     ASSERT_NE(pipeReadFd[PIPE_BUF_INDEX], -1);
62     ASSERT_NE(pipeReadFd[PIPE_RES_INDEX], -1);
63     int32_t ret = RequestDelPipeFd(getpid());
64     ASSERT_EQ(ret, 0);
65     int pipeFd[] = { -1, -1 };
66     RequestPipeFd(getpid(), FaultLoggerPipeType::PIPE_FD_READ, pipeFd);
67     ASSERT_EQ(pipeFd[PIPE_BUF_INDEX], -1);
68     ASSERT_EQ(pipeFd[PIPE_RES_INDEX], -1);
69 }
70 
71 /**
72  * @tc.name: FaultloggerdServiceTest001
73  * @tc.desc: check faultloggerd running status and ensure it has been started
74  * @tc.type: FUNC
75  */
76 HWTEST_F(FaultloggerdModuleTest, FaultloggerdServiceTest001, TestSize.Level0)
77 {
78     WaitForServiceReady("faultloggerd");
79 }
80 
81 #if defined(__BIONIC__)
82 /**
83  * @tc.name: FaultloggerdDfxHandlerPreloadTest001
84  * @tc.desc: check whether libdfx_signalhandler.z.so is preloaded.
85  * @tc.type: FUNC
86  */
87 HWTEST_F(FaultloggerdModuleTest, FaultloggerdDfxHandlerPreloadTest001, TestSize.Level0)
88 {
89     std::string cmd = "cat /proc/" + std::to_string(getpid()) + "/maps";
90     std::string result = ExecuteCommands(cmd);
91     ASSERT_EQ(result.find("libdfx_signalhandler.z.so") != std::string::npos, true);
92 }
93 #endif
94 
95 /**
96  * @tc.name: FaultloggerdClientFdRquestTest001
97  * @tc.desc: check faultloggerd logging function
98  * @tc.type: FUNC
99  */
100 HWTEST_F(FaultloggerdModuleTest, FaultloggerdClientFdRquestTest001, TestSize.Level0)
101 {
102     CheckFdRequestFunction(FaultLoggerType::CPP_CRASH, true);
103     CheckFdRequestFunction(FaultLoggerType::CPP_STACKTRACE, true);
104     CheckFdRequestFunction(FaultLoggerType::JS_STACKTRACE, true);
105     CheckFdRequestFunction(FaultLoggerType::JS_HEAP_SNAPSHOT, true);
106     CheckFdRequestFunction(FaultLoggerType::LEAK_STACKTRACE, true);
107 }
108 
109 /**
110  * @tc.name: FaultloggerdClientFdRquestTest004
111  * @tc.desc: check faultloggerd RequestFileDescriptorEx function
112  * @tc.type: FUNC
113  */
114 HWTEST_F(FaultloggerdModuleTest, FaultloggerdClientFdRquestTest004, TestSize.Level0)
115 {
116     struct FaultLoggerdRequest testRequest;
117     (void)memset_s(&testRequest, sizeof(testRequest), 0, sizeof(testRequest));
118     testRequest.type = FaultLoggerType::CPP_CRASH;
119     testRequest.pid = getpid();
120     testRequest.tid = gettid();
121     int32_t fd = RequestFileDescriptorEx(&testRequest);
122     ASSERT_TRUE(fd >= 0);
123     if (fd >= 0) {
124         close(fd);
125     }
126 }
127 
128 /**
129  * @tc.name: FaultloggerdClientPipeFdRquestTest002
130  * @tc.desc: check faultloggerd RequestPipeFd function by param error
131  * @tc.type: FUNC
132  */
133 HWTEST_F(FaultloggerdModuleTest, FaultloggerdClientPipeFdRquestTest002, TestSize.Level0)
134 {
135     int pipeReadFd[] = { -1, -1 };
136     RequestSdkDump(getpid(), getpid(), pipeReadFd);
137     int pipeWriteFd[] = { -1, -1 };
138     ASSERT_EQ(RequestPipeFd(getpid(), -1, pipeWriteFd), -1);
139     ASSERT_EQ(pipeWriteFd[PIPE_BUF_INDEX], -1);
140     ASSERT_EQ(pipeWriteFd[PIPE_RES_INDEX], -1);
141 }
142 }
143