1 /*
2 * Copyright (c) 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 <fcntl.h>
17 #include <gtest/gtest.h>
18 #include <sys/socket.h>
19 #include <sys/un.h>
20 #include <unistd.h>
21
22 #include "dfx_define.h"
23 #include "dfx_test_util.h"
24 #include "faultloggerd_client.h"
25 #include "faultloggerd_socket.h"
26
27 #if defined(HAS_LIB_SELINUX)
28 #include <selinux/selinux.h>
29 #endif
30
31 using namespace testing;
32 using namespace testing::ext;
33
34 namespace OHOS {
35 namespace HiviewDFX {
36 class FaultloggerdClientTest : public testing::Test {
37 public:
38 static void SetUpTestCase();
39 static void TearDownTestCase();
40 void SetUp();
41 void TearDown();
42 };
43
SetUpTestCase()44 void FaultloggerdClientTest::SetUpTestCase()
45 {
46 }
47
TearDownTestCase()48 void FaultloggerdClientTest::TearDownTestCase()
49 {
50 }
51
SetUp()52 void FaultloggerdClientTest::SetUp()
53 {
54 }
55
TearDown()56 void FaultloggerdClientTest::TearDown()
57 {
58 }
59
IsSelinuxEnforced()60 bool IsSelinuxEnforced()
61 {
62 std::string cmd = "getenforce";
63 std::string selinuxStatus = ExecuteCommands(cmd);
64 GTEST_LOG_(INFO) << "getenforce return:" << selinuxStatus;
65 return (selinuxStatus.find("Enforcing") != std::string::npos) ? true : false;
66 }
67
68 /**
69 * @tc.name: FaultloggerdClientTest001
70 * @tc.desc: request a file descriptor for logging crash
71 * @tc.type: FUNC
72 */
73 HWTEST_F(FaultloggerdClientTest, FaultloggerdClientTest001, TestSize.Level2)
74 {
75 GTEST_LOG_(INFO) << "FaultloggerdClientTest001: start.";
76 int32_t fd = RequestFileDescriptor(FaultLoggerType::CPP_CRASH);
77 ASSERT_GT(fd, 0);
78 close(fd);
79
80 fd = RequestFileDescriptor(FaultLoggerType::JS_HEAP_SNAPSHOT);
81 ASSERT_GT(fd, 0);
82 close(fd);
83 GTEST_LOG_(INFO) << "FaultloggerdClientTest001: end.";
84 }
85
86 /**
87 * @tc.name: FaultloggerdClientTest002
88 * @tc.desc: request a file descriptor for logging with app uid
89 * @tc.type: FUNC
90 */
91 HWTEST_F(FaultloggerdClientTest, FaultloggerdClientTest002, TestSize.Level2)
92 {
93 GTEST_LOG_(INFO) << "FaultloggerdClientTest002: start.";
94 int32_t pid = fork();
95 if (pid == 0) {
96 int ret = -1;
97 constexpr int32_t appUid = 100068;
98 setuid(appUid);
99 int32_t fd = RequestFileDescriptor(FaultLoggerType::CPP_CRASH);
100 if (fd >= 0) {
101 close(fd);
102 ret = 0;
103 }
104
105 fd = RequestFileDescriptor(FaultLoggerType::JS_HEAP_SNAPSHOT);
106 if (fd >= 0) {
107 close(fd);
108 ret += 0;
109 }
110 exit(ret);
111 } else if (pid > 0) {
112 int status;
113 if (waitpid(pid, &status, 0) == -1) {
114 return;
115 }
116
117 int exitCode = -1;
118 if (WIFEXITED(status)) {
119 exitCode = WEXITSTATUS(status);
120 printf("Exit status was %d\n", exitCode);
121 }
122 ASSERT_EQ(exitCode, 0);
123 }
124 GTEST_LOG_(INFO) << "FaultloggerdClientTest002: end.";
125 }
126
127 /**
128 * @tc.name: FaultloggerdClientTest003
129 * @tc.desc: request a file descriptor for logging with inconsistent pid
130 * @tc.type: FUNC
131 */
132 HWTEST_F(FaultloggerdClientTest, FaultloggerdClientTest003, TestSize.Level2)
133 {
134 GTEST_LOG_(INFO) << "FaultloggerdClientTest003: start.";
135 int32_t pid = fork();
136 if (pid == 0) {
137 int ret = 1;
138 constexpr int32_t appUid = 100068;
139 setuid(appUid);
140 FaultLoggerdRequest request;
141 request.type = FaultLoggerType::JS_HEAP_SNAPSHOT;
142 request.pid = appUid;
143 int32_t fd = RequestFileDescriptorEx(&request);
144 if (fd >= 0) {
145 close(fd);
146 ret = 0;
147 }
148 exit(ret);
149 } else if (pid > 0) {
150 int status;
151 if (waitpid(pid, &status, 0) == -1) {
152 return;
153 }
154
155 int exitCode = -1;
156 if (WIFEXITED(status)) {
157 exitCode = WEXITSTATUS(status);
158 printf("Exit status was %d\n", exitCode);
159 }
160 ASSERT_EQ(exitCode, 1);
161 }
162 GTEST_LOG_(INFO) << "FaultloggerdClientTest003: end.";
163 }
164 #if defined(HAS_LIB_SELINUX)
165 /**
166 * @tc.name: FaultloggerdClientTest004
167 * @tc.desc: request a file descriptor for logging with inconsistent pid but in processdump scontext
168 * @tc.type: FUNC
169 */
170 HWTEST_F(FaultloggerdClientTest, FaultloggerdClientTest004, TestSize.Level2)
171 {
172 GTEST_LOG_(INFO) << "FaultloggerdClientTest004: start.";
173
174 // If selinux is not opened, skip this test item
175 if (!IsSelinuxEnforced()) {
176 GTEST_LOG_(INFO) << "Selinux is not opened, skip FaultloggerdClientTest004";
177 return;
178 }
179 int32_t pid = fork();
180 if (pid == 0) {
181 int ret = 1;
182 constexpr int32_t appUid = 100068;
183 setcon("u:r:processdump:s0");
184 setuid(appUid);
185 FaultLoggerdRequest request;
186 request.type = FaultLoggerType::JS_HEAP_SNAPSHOT;
187 request.pid = appUid;
188 int32_t fd = RequestFileDescriptorEx(&request);
189 if (fd >= 0) {
190 close(fd);
191 ret = 0;
192 }
193 exit(ret);
194 } else if (pid > 0) {
195 int status;
196 if (waitpid(pid, &status, 0) == -1) {
197 return;
198 }
199
200 int exitCode = -1;
201 if (WIFEXITED(status)) {
202 exitCode = WEXITSTATUS(status);
203 printf("Exit status was %d\n", exitCode);
204 }
205 ASSERT_EQ(exitCode, 0);
206 }
207 GTEST_LOG_(INFO) << "FaultloggerdClientTest004: end.";
208 }
209 #endif
210
DoClientProcess(const std::string & socketFileName)211 void DoClientProcess(const std::string& socketFileName)
212 {
213 // wait 2 seconds, waiting for the service to be ready
214 sleep(2);
215 int clientSocketFd = -1;
216
217 // socket connect time out 10 second
218 bool retBool = StartConnect(clientSocketFd, socketFileName.c_str(), 10);
219 ASSERT_TRUE(retBool);
220 ASSERT_NE(clientSocketFd, -1);
221 GTEST_LOG_(INFO) << "child connect finished, client fd:" << clientSocketFd;
222
223 int data = 12345; // 12345 is for server Cred test
224 retBool = SendMsgIovToSocket(clientSocketFd, reinterpret_cast<void *>(&data), sizeof(data));
225 ASSERT_TRUE(retBool);
226
227 GTEST_LOG_(INFO) << "Start read file desc";
228 int testFd = ReadFileDescriptorFromSocket(clientSocketFd);
229 GTEST_LOG_(INFO) << "recv testFd:" << testFd;
230 ASSERT_NE(testFd, -1);
231 close(clientSocketFd);
232 close(testFd);
233 }
234
DoServerProcess(const std::string & socketFileName)235 void DoServerProcess(const std::string& socketFileName)
236 {
237 GTEST_LOG_(INFO) << "server prepare listen";
238 int32_t serverSocketFd = -1;
239 std::string testFileName = "/data/test.txt";
240
241 // 5: means max connection count is 5
242 bool ret = StartListen(serverSocketFd, socketFileName.c_str(), 5);
243 ASSERT_TRUE(ret);
244 ASSERT_NE(serverSocketFd, -1);
245 GTEST_LOG_(INFO) << "server start listen fd:" << serverSocketFd;
246
247 struct timeval timev = {
248 20, // recv timeout 20 seconds
249 0
250 };
251 void* pTimev = &timev;
252 int retOpt = setsockopt(serverSocketFd, SOL_SOCKET, SO_RCVTIMEO, static_cast<const char*>(pTimev),
253 sizeof(struct timeval));
254 ASSERT_NE(retOpt, -1);
255
256 struct sockaddr_un clientAddr;
257 socklen_t clientAddrSize = static_cast<socklen_t>(sizeof(clientAddr));
258 int32_t connectionFd = accept(serverSocketFd, reinterpret_cast<struct sockaddr *>(&clientAddr),
259 &clientAddrSize);
260 ASSERT_GT(connectionFd, 0);
261 GTEST_LOG_(INFO) << "server accept fd:" << connectionFd;
262
263 int optval = 1;
264 retOpt = setsockopt(connectionFd, SOL_SOCKET, SO_PASSCRED, &optval, sizeof(optval));
265 ASSERT_NE(retOpt, -1);
266
267 struct ucred rcred;
268 bool retCred = RecvMsgCredFromSocket(connectionFd, &rcred);
269 ASSERT_TRUE(retCred);
270 GTEST_LOG_(INFO) << "uid:" << rcred.uid;
271
272 // 0666 for file read and write
273 int testFdServer = open(testFileName.c_str(), O_RDWR | O_CREAT, 0666);
274 GTEST_LOG_(INFO) << "Start SendFileDescriptorToSocket";
275 SendFileDescriptorToSocket(connectionFd, testFdServer);
276 GTEST_LOG_(INFO) << "Close server connect fd";
277 close(connectionFd);
278 close(testFdServer);
279 close(serverSocketFd);
280 unlink(testFileName.c_str());
281 }
282
283 /**
284 * @tc.name: FaultloggerdSocketTest001
285 * @tc.desc: test StartListen, RecvMsgCredFromSocket and SendMsgCtlToSocket
286 * @tc.type: FUNC
287 */
288 HWTEST_F(FaultloggerdClientTest, FaultloggerdSocketTest001, TestSize.Level2)
289 {
290 GTEST_LOG_(INFO) << "FaultloggerdSocketTest001: start.";
291 std::string testSocketName = "faultloggerd.server.test";
292
293 int32_t pid = fork();
294 if (pid == 0) {
295 DoClientProcess(testSocketName);
296 GTEST_LOG_(INFO) << "client exit";
297 exit(0);
298 } else if (pid > 0) {
299 DoServerProcess(testSocketName);
300
301 int status;
302 if (waitpid(pid, &status, 0) == -1) {
303 return;
304 }
305
306 int exitCode = -1;
307 if (WIFEXITED(status)) {
308 exitCode = WEXITSTATUS(status);
309 GTEST_LOG_(INFO) << "Exit status was " << exitCode;
310 }
311 ASSERT_EQ(exitCode, 0);
312 }
313 GTEST_LOG_(INFO) << "FaultloggerdSocketTest001: end.";
314 }
315 } // namespace HiviewDFX
316 } // namepsace OHOS
317