• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022 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 "faultloggerd_client.h"
23 
24 using namespace testing::ext;
25 
26 namespace {
GetCmdResultFromPopen(const std::string & cmd)27 std::string GetCmdResultFromPopen(const std::string& cmd)
28 {
29     if (cmd.empty()) {
30         return "";
31     }
32 
33     FILE* fp = popen(cmd.c_str(), "r");
34     if (fp == nullptr) {
35         return "";
36     }
37     const int bufSize = 128;
38     char buffer[bufSize];
39     std::string result = "";
40     while (!feof(fp)) {
41         if (fgets(buffer, bufSize - 1, fp) != nullptr) {
42             result += buffer;
43         }
44     }
45     pclose(fp);
46     return result;
47 }
48 
GetServicePid(const std::string & serviceName)49 int GetServicePid(const std::string& serviceName)
50 {
51     std::string cmd = "pidof " + serviceName;
52     std::string pidStr = GetCmdResultFromPopen(cmd);
53     int32_t pid = 0;
54     std::stringstream pidStream(pidStr);
55     pidStream >> pid;
56     printf("the pid of service(%s) is %s \n", serviceName.c_str(), pidStr.c_str());
57     return pid;
58 }
59 
WaitForServiceReady(const std::string & serviceName)60 void WaitForServiceReady(const std::string& serviceName)
61 {
62     int pid = GetServicePid(serviceName);
63     if (pid <= 0) {
64         std::string cmd = "start " + serviceName;
65         GetCmdResultFromPopen(cmd);
66         const int sleepTime = 10; // 10 seconds
67         sleep(sleepTime);
68         pid = GetServicePid(serviceName);
69     }
70     ASSERT_GT(pid, 0);
71     ASSERT_TRUE(CheckConnectStatus());
72 }
73 
CheckFdRequestFunction(FaultLoggerType type,bool isValidFd)74 void CheckFdRequestFunction(FaultLoggerType type, bool isValidFd)
75 {
76     int32_t fd = RequestFileDescriptor(static_cast<int32_t>(type));
77     ASSERT_EQ((fd >= 0), isValidFd);
78     if (fd >= 0) {
79         close(fd);
80     }
81 }
82 
83 /**
84  * @tc.name: FaultloggerdServiceTest001
85  * @tc.desc: check faultloggerd running status and ensure it has been started
86  * @tc.type: FUNC
87  */
88 HWTEST_F(FaultloggerdModuleTest, FaultloggerdServiceTest001, TestSize.Level0)
89 {
90     WaitForServiceReady("faultloggerd");
91 }
92 
93 #if defined(__BIONIC__)
94 /**
95  * @tc.name: FaultloggerdDfxHandlerPreloadTest001
96  * @tc.desc: check whether libdfx_signalhandler.z.so is preloaded.
97  * @tc.type: FUNC
98  */
99 HWTEST_F(FaultloggerdModuleTest, FaultloggerdDfxHandlerPreloadTest001, TestSize.Level0)
100 {
101     std::string cmd = "cat /proc/" + std::to_string(getpid()) + "/maps";
102     std::string result = GetCmdResultFromPopen(cmd);
103     ASSERT_EQ(result.find("libdfx_signalhandler.z.so") != std::string::npos, true);
104 }
105 #endif
106 
107 /**
108  * @tc.name: FaultloggerdClientFdRquestTest001
109  * @tc.desc: check faultloggerd logging function
110  * @tc.type: FUNC
111  */
112 HWTEST_F(FaultloggerdModuleTest, FaultloggerdClientFdRquestTest001, TestSize.Level0)
113 {
114     CheckFdRequestFunction(FaultLoggerType::CPP_CRASH, true);
115     CheckFdRequestFunction(FaultLoggerType::CPP_STACKTRACE, true);
116     CheckFdRequestFunction(FaultLoggerType::JS_STACKTRACE, true);
117     CheckFdRequestFunction(FaultLoggerType::JS_HEAP_SNAPSHOT, true);
118     CheckFdRequestFunction(FaultLoggerType::JAVA_STACKTRACE, false);
119 }
120 
121 /**
122  * @tc.name: FaultloggerdClientFdRquestTest002
123  * @tc.desc: check faultloggerd RequestLogFileDescriptor function
124  * @tc.type: FUNC
125  */
126 HWTEST_F(FaultloggerdModuleTest, FaultloggerdClientFdRquestTest002, TestSize.Level0)
127 {
128     struct FaultLoggerdRequest testRequest;
129     if (memset_s(&testRequest, sizeof(testRequest), 0, sizeof(struct FaultLoggerdRequest)) != 0) {
130         return;
131     }
132     testRequest.type = (int)FaultLoggerType::CPP_CRASH;
133     testRequest.pid = getpid();
134     testRequest.tid = gettid();
135     testRequest.uid = getuid();
136     int32_t fd = RequestLogFileDescriptor(&testRequest);
137     ASSERT_TRUE(fd >= 0);
138     if (fd >= 0) {
139         close(fd);
140     }
141 }
142 
143 /**
144  * @tc.name: FaultloggerdClientFdRquestTest003
145  * @tc.desc: check faultloggerd RequestPrintTHilog function
146  * @tc.type: FUNC
147  */
148 HWTEST_F(FaultloggerdModuleTest, FaultloggerdClientFdRquestTest003, TestSize.Level0)
149 {
150     char msg[] = "test log";
151     size_t len = strlen(msg);
152     RequestPrintTHilog(msg, len);
153 }
154 
155 /**
156  * @tc.name: FaultloggerdClientPipeFdRquestTest001
157  * @tc.desc: check faultloggerd RequestPipeFd function
158  * @tc.type: FUNC
159  */
160 HWTEST_F(FaultloggerdModuleTest, FaultloggerdClientPipeFdRquestTest001, TestSize.Level0)
161 {
162     int32_t pipeFd = RequestPipeFd(getpid(), FaultLoggerPipeType::PIPE_FD_READ_BUF);
163     ASSERT_EQ(pipeFd, -1);
164     int32_t ret = RequestDelPipeFd(getpid());
165     ASSERT_EQ(ret, 0);
166 }
167 
168 /**
169  * @tc.name: FaultloggerdPermissionCheckTest001
170  * @tc.desc: check faultloggerd RequestCheckPermission function
171  * @tc.type: FUNC
172  */
173 HWTEST_F(FaultloggerdModuleTest, FaultloggerdPermissionCheckTest001, TestSize.Level0)
174 {
175     ASSERT_TRUE(RequestCheckPermission(getpid()));
176 }
177 }
178