blob: 559ee7d68820858417bbdbd37ee291e7dba7e383 [file] [log] [blame]
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <gtest/gtest.h>
18
19#include <errno.h>
Christopher Ferris108f6dc2014-03-31 13:30:22 -070020#include <sys/types.h>
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010021#include <sys/socket.h>
Christopher Ferris108f6dc2014-03-31 13:30:22 -070022#include <sys/un.h>
23#include <fcntl.h>
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010024
Elliott Hughesa7f12942017-12-15 13:55:53 -080025#include "utils.h"
26
Yabin Cui294d1e22014-12-07 20:43:37 -080027struct ConnectData {
28 bool (*callback_fn)(int);
29 const char* sock_path;
30 ConnectData(bool (*callback_func)(int), const char* socket_path)
31 : callback_fn(callback_func), sock_path(socket_path) {}
32};
Christopher Ferris108f6dc2014-03-31 13:30:22 -070033
34static void* ConnectFn(void* data) {
Yabin Cui294d1e22014-12-07 20:43:37 -080035 ConnectData* pdata = reinterpret_cast<ConnectData*>(data);
36 bool (*callback_fn)(int) = pdata->callback_fn;
Yi Kong32bc0fc2018-08-02 17:31:13 -070037 void* return_value = nullptr;
Christopher Ferris108f6dc2014-03-31 13:30:22 -070038
39 int fd = socket(PF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
40 if (fd < 0) {
41 GTEST_LOG_(ERROR) << "socket call failed: " << strerror(errno);
42 return reinterpret_cast<void*>(-1);
43 }
44
Christopher Ferris2a391882024-12-19 13:44:35 -080045 struct sockaddr_un addr = {.sun_family = AF_UNIX, .sun_path[0] = '\0'};
Yabin Cui294d1e22014-12-07 20:43:37 -080046 strcpy(addr.sun_path + 1, pdata->sock_path);
Christopher Ferris108f6dc2014-03-31 13:30:22 -070047
48 if (connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) {
49 GTEST_LOG_(ERROR) << "connect call failed: " << strerror(errno);
50 return_value = reinterpret_cast<void*>(-1);
51 }
Yi Kong32bc0fc2018-08-02 17:31:13 -070052 else if (callback_fn != nullptr && !callback_fn(fd)) {
Christopher Ferris108f6dc2014-03-31 13:30:22 -070053 return_value = reinterpret_cast<void*>(-1);
54 }
55
56 close(fd);
57
58 return return_value;
59}
60
61static void RunTest(void (*test_fn)(struct sockaddr_un*, int),
Yabin Cui294d1e22014-12-07 20:43:37 -080062 bool (*callback_fn)(int fd), const char* sock_path) {
Christopher Ferris108f6dc2014-03-31 13:30:22 -070063 int fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
64 ASSERT_NE(fd, -1) << strerror(errno);
65
Christopher Ferris2a391882024-12-19 13:44:35 -080066 struct sockaddr_un addr = {.sun_family = AF_UNIX, .sun_path[0] = '\0'};
Yabin Cui294d1e22014-12-07 20:43:37 -080067 strcpy(addr.sun_path + 1, sock_path);
Christopher Ferris108f6dc2014-03-31 13:30:22 -070068
69 ASSERT_NE(-1, bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) << strerror(errno);
70
71 ASSERT_NE(-1, listen(fd, 1)) << strerror(errno);
72
Yabin Cui294d1e22014-12-07 20:43:37 -080073 ConnectData connect_data(callback_fn, sock_path);
74
Christopher Ferris108f6dc2014-03-31 13:30:22 -070075 pthread_t thread;
Yi Kong32bc0fc2018-08-02 17:31:13 -070076 ASSERT_EQ(0, pthread_create(&thread, nullptr, ConnectFn, &connect_data));
Christopher Ferris108f6dc2014-03-31 13:30:22 -070077
78 fd_set read_set;
79 FD_ZERO(&read_set);
80 FD_SET(fd, &read_set);
81 timeval tv;
82 tv.tv_sec = 5;
83 tv.tv_usec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -070084 ASSERT_LT(0, select(fd+1, &read_set, nullptr, nullptr, &tv));
Christopher Ferris108f6dc2014-03-31 13:30:22 -070085
86 test_fn(&addr, fd);
87
88 void* ret_val;
89 ASSERT_EQ(0, pthread_join(thread, &ret_val));
Yi Kong32bc0fc2018-08-02 17:31:13 -070090 ASSERT_EQ(nullptr, ret_val);
Christopher Ferris108f6dc2014-03-31 13:30:22 -070091
92 close(fd);
93}
Christopher Ferris108f6dc2014-03-31 13:30:22 -070094
95TEST(sys_socket, accept4_error) {
Yi Kong32bc0fc2018-08-02 17:31:13 -070096 ASSERT_EQ(-1, accept4(-1, nullptr, nullptr, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -070097 ASSERT_ERRNO(EBADF);
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +010098}
99
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700100static void TestAccept4(struct sockaddr_un* addr, int fd) {
101 socklen_t len = sizeof(*addr);
102 int fd_acc = accept4(fd, reinterpret_cast<struct sockaddr*>(addr), &len, SOCK_CLOEXEC);
103 ASSERT_NE(fd_acc, -1) << strerror(errno);
104
Elliott Hughesa7f12942017-12-15 13:55:53 -0800105 // Check that SOCK_CLOEXEC was set properly.
Elliott Hughesf9cfecf2021-02-04 16:58:13 -0800106 ASSERT_TRUE(CloseOnExec(fd_acc));
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700107
108 close(fd_acc);
109}
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700110
111TEST(sys_socket, accept4_smoke) {
Yi Kong32bc0fc2018-08-02 17:31:13 -0700112 RunTest(TestAccept4, nullptr, "test_accept");
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700113}
114
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700115const char* g_RecvMsgs[] = {
116 "RECVMMSG_ONE",
117 "RECVMMSG_TWO",
118 "RECVMMSG_THREE",
119};
120#define NUM_RECV_MSGS (sizeof(g_RecvMsgs)/sizeof(const char*))
121
122static bool SendMultiple(int fd) {
123 for (size_t i = 0; i < NUM_RECV_MSGS; i++) {
124 if (send(fd, g_RecvMsgs[i], strlen(g_RecvMsgs[i]) + 1, 0) < 0) {
125 GTEST_LOG_(ERROR) << "send call failed: " << strerror(errno);
126 return false;
127 }
128 }
129
130 return true;
131}
132
133static void TestRecvMMsg(struct sockaddr_un *addr, int fd) {
134 socklen_t len = sizeof(*addr);
135 int fd_acc = accept(fd, reinterpret_cast<struct sockaddr*>(addr), &len);
136 ASSERT_NE(fd_acc, -1) << strerror(errno);
137
Christopher Ferris2a391882024-12-19 13:44:35 -0800138 struct mmsghdr msgs[NUM_RECV_MSGS] = {};
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700139 struct iovec io[NUM_RECV_MSGS];
140 char bufs[NUM_RECV_MSGS][100];
141 for (size_t i = 0; i < NUM_RECV_MSGS; i++) {
142 io[i].iov_base = reinterpret_cast<void*>(bufs[i]);
143 io[i].iov_len = strlen(g_RecvMsgs[i]) + 1;
144
145 msgs[i].msg_hdr.msg_iov = &io[i];
146 msgs[i].msg_hdr.msg_iovlen = 1;
147 msgs[i].msg_len = sizeof(struct msghdr);
148 }
149
Christopher Ferris2a391882024-12-19 13:44:35 -0800150 struct timespec ts = {.tv_sec = 5, .tv_nsec = 0};
Ben Chengb7601642014-04-15 15:29:32 -0700151 ASSERT_EQ(NUM_RECV_MSGS,
152 static_cast<size_t>(recvmmsg(fd_acc, msgs, NUM_RECV_MSGS, 0, &ts)))
153 << strerror(errno);
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700154 for (size_t i = 0; i < NUM_RECV_MSGS; i++) {
155 ASSERT_STREQ(g_RecvMsgs[i], bufs[i]);
156 }
157
158 close(fd_acc);
159}
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700160
161TEST(sys_socket, recvmmsg_smoke) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800162 RunTest(TestRecvMMsg, SendMultiple, "test_revmmsg");
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700163}
164
165TEST(sys_socket, recvmmsg_error) {
zijunzhao4e274fa2023-05-25 18:48:38 +0000166#pragma clang diagnostic push
167#pragma clang diagnostic ignored "-Wnonnull"
Yi Kong32bc0fc2018-08-02 17:31:13 -0700168 ASSERT_EQ(-1, recvmmsg(-1, nullptr, 0, 0, nullptr));
Elliott Hughes95646e62023-09-21 14:11:19 -0700169 ASSERT_ERRNO(EBADF);
zijunzhao4e274fa2023-05-25 18:48:38 +0000170#pragma clang diagnostic pop
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700171}
172
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700173const char* g_SendMsgs[] = {
174 "MSG_ONE",
175 "MSG_TWO",
176 "MSG_THREE"
177};
178#define NUM_SEND_MSGS (sizeof(g_SendMsgs)/sizeof(const char*))
179
180static bool SendMMsg(int fd) {
Christopher Ferris2a391882024-12-19 13:44:35 -0800181 struct mmsghdr msgs[NUM_SEND_MSGS] = {};
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700182 struct iovec io[NUM_SEND_MSGS];
183 for (size_t i = 0; i < NUM_SEND_MSGS; i++) {
184 io[i].iov_base = reinterpret_cast<void*>(const_cast<char*>(g_SendMsgs[i]));
185 io[i].iov_len = strlen(g_SendMsgs[i]) + 1;
186 msgs[i].msg_hdr.msg_iov = &io[i];
187 msgs[i].msg_hdr.msg_iovlen = 1;
188 msgs[i].msg_len = sizeof(struct msghdr);
189 }
190
191 if (sendmmsg(fd, msgs, NUM_SEND_MSGS, 0) < 0) {
192 GTEST_LOG_(ERROR) << "sendmmsg call failed: " << strerror(errno);
193 return false;
194 }
195 return true;
196}
197
198static void TestSendMMsg(struct sockaddr_un *addr, int fd) {
199 socklen_t len = sizeof(*addr);
200 int fd_acc = accept(fd, reinterpret_cast<struct sockaddr*>(addr), &len);
201 ASSERT_NE(fd_acc, -1) << strerror(errno);
202
203 fd_set read_set;
204 FD_ZERO(&read_set);
205 FD_SET(fd_acc, &read_set);
206
207 for (size_t i = 0; i < NUM_SEND_MSGS; i++) {
208 timeval tv;
209 tv.tv_sec = 5;
210 tv.tv_usec = 0;
Yi Kong32bc0fc2018-08-02 17:31:13 -0700211 ASSERT_LT(0, select(fd_acc+1, &read_set, nullptr, nullptr, &tv));
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700212 char buffer[100];
Ben Chengb7601642014-04-15 15:29:32 -0700213 ASSERT_EQ(strlen(g_SendMsgs[i]) + 1,
214 static_cast<size_t>(recv(fd_acc, buffer, sizeof(buffer), 0)));
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700215 ASSERT_STREQ(g_SendMsgs[i], buffer);
216 }
217
218 close(fd_acc);
219}
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700220
221TEST(sys_socket, sendmmsg_smoke) {
Yabin Cui294d1e22014-12-07 20:43:37 -0800222 RunTest(TestSendMMsg, SendMMsg, "test_sendmmsg");
Christopher Ferris108f6dc2014-03-31 13:30:22 -0700223}
224
225TEST(sys_socket, sendmmsg_error) {
zijunzhao4e274fa2023-05-25 18:48:38 +0000226#pragma clang diagnostic push
227#pragma clang diagnostic ignored "-Wnonnull"
Yi Kong32bc0fc2018-08-02 17:31:13 -0700228 ASSERT_EQ(-1, sendmmsg(-1, nullptr, 0, 0));
Elliott Hughes95646e62023-09-21 14:11:19 -0700229 ASSERT_ERRNO(EBADF);
zijunzhao4e274fa2023-05-25 18:48:38 +0000230#pragma clang diagnostic pop
Guillaume Ranquet6ff0c752014-02-10 13:11:29 +0100231}