2 * Copyright (C) 2014 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
26 bool (*callback_fn)(int);
27 const char* sock_path;
28 ConnectData(bool (*callback_func)(int), const char* socket_path)
29 : callback_fn(callback_func), sock_path(socket_path) {}
32 static void* ConnectFn(void* data) {
33 ConnectData* pdata = reinterpret_cast<ConnectData*>(data);
34 bool (*callback_fn)(int) = pdata->callback_fn;
35 void* return_value = NULL;
37 int fd = socket(PF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
39 GTEST_LOG_(ERROR) << "socket call failed: " << strerror(errno);
40 return reinterpret_cast<void*>(-1);
43 struct sockaddr_un addr;
44 memset(&addr, 0, sizeof(addr));
45 addr.sun_family = AF_UNIX;
46 addr.sun_path[0] = '\0';
47 strcpy(addr.sun_path + 1, pdata->sock_path);
49 if (connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) {
50 GTEST_LOG_(ERROR) << "connect call failed: " << strerror(errno);
51 return_value = reinterpret_cast<void*>(-1);
53 else if (callback_fn != NULL && !callback_fn(fd)) {
54 return_value = reinterpret_cast<void*>(-1);
62 static void RunTest(void (*test_fn)(struct sockaddr_un*, int),
63 bool (*callback_fn)(int fd), const char* sock_path) {
64 int fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
65 ASSERT_NE(fd, -1) << strerror(errno);
67 struct sockaddr_un addr;
68 memset(&addr, 0, sizeof(addr));
69 addr.sun_family = AF_UNIX;
70 addr.sun_path[0] = '\0';
71 strcpy(addr.sun_path + 1, sock_path);
73 ASSERT_NE(-1, bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) << strerror(errno);
75 ASSERT_NE(-1, listen(fd, 1)) << strerror(errno);
77 ConnectData connect_data(callback_fn, sock_path);
80 ASSERT_EQ(0, pthread_create(&thread, NULL, ConnectFn, &connect_data));
84 FD_SET(fd, &read_set);
88 ASSERT_LT(0, select(fd+1, &read_set, NULL, NULL, &tv));
93 ASSERT_EQ(0, pthread_join(thread, &ret_val));
94 ASSERT_EQ(NULL, ret_val);
99 TEST(sys_socket, accept4_error) {
100 ASSERT_EQ(-1, accept4(-1, NULL, NULL, 0));
101 ASSERT_EQ(EBADF, errno);
104 static void TestAccept4(struct sockaddr_un* addr, int fd) {
105 socklen_t len = sizeof(*addr);
106 int fd_acc = accept4(fd, reinterpret_cast<struct sockaddr*>(addr), &len, SOCK_CLOEXEC);
107 ASSERT_NE(fd_acc, -1) << strerror(errno);
109 // Check that the flag was set properly.
110 ASSERT_EQ(FD_CLOEXEC, fcntl(fd_acc, F_GETFD) & FD_CLOEXEC);
115 TEST(sys_socket, accept4_smoke) {
116 RunTest(TestAccept4, NULL, "test_accept");
119 const char* g_RecvMsgs[] = {
124 #define NUM_RECV_MSGS (sizeof(g_RecvMsgs)/sizeof(const char*))
126 static bool SendMultiple(int fd) {
127 for (size_t i = 0; i < NUM_RECV_MSGS; i++) {
128 if (send(fd, g_RecvMsgs[i], strlen(g_RecvMsgs[i]) + 1, 0) < 0) {
129 GTEST_LOG_(ERROR) << "send call failed: " << strerror(errno);
137 static void TestRecvMMsg(struct sockaddr_un *addr, int fd) {
138 socklen_t len = sizeof(*addr);
139 int fd_acc = accept(fd, reinterpret_cast<struct sockaddr*>(addr), &len);
140 ASSERT_NE(fd_acc, -1) << strerror(errno);
142 struct mmsghdr msgs[NUM_RECV_MSGS];
143 memset(msgs, 0, sizeof(struct mmsghdr)*NUM_RECV_MSGS);
145 struct iovec io[NUM_RECV_MSGS];
146 char bufs[NUM_RECV_MSGS][100];
147 for (size_t i = 0; i < NUM_RECV_MSGS; i++) {
148 io[i].iov_base = reinterpret_cast<void*>(bufs[i]);
149 io[i].iov_len = strlen(g_RecvMsgs[i]) + 1;
151 msgs[i].msg_hdr.msg_iov = &io[i];
152 msgs[i].msg_hdr.msg_iovlen = 1;
153 msgs[i].msg_len = sizeof(struct msghdr);
157 memset(&ts, 0, sizeof(ts));
160 ASSERT_EQ(NUM_RECV_MSGS,
161 static_cast<size_t>(recvmmsg(fd_acc, msgs, NUM_RECV_MSGS, 0, &ts)))
163 for (size_t i = 0; i < NUM_RECV_MSGS; i++) {
164 ASSERT_STREQ(g_RecvMsgs[i], bufs[i]);
170 TEST(sys_socket, recvmmsg_smoke) {
171 RunTest(TestRecvMMsg, SendMultiple, "test_revmmsg");
174 TEST(sys_socket, recvmmsg_error) {
175 ASSERT_EQ(-1, recvmmsg(-1, NULL, 0, 0, NULL));
176 ASSERT_EQ(EBADF, errno);
179 const char* g_SendMsgs[] = {
184 #define NUM_SEND_MSGS (sizeof(g_SendMsgs)/sizeof(const char*))
186 static bool SendMMsg(int fd) {
187 struct mmsghdr msgs[NUM_SEND_MSGS];
188 memset(msgs, 0, sizeof(struct mmsghdr)*NUM_SEND_MSGS);
189 struct iovec io[NUM_SEND_MSGS];
190 for (size_t i = 0; i < NUM_SEND_MSGS; i++) {
191 io[i].iov_base = reinterpret_cast<void*>(const_cast<char*>(g_SendMsgs[i]));
192 io[i].iov_len = strlen(g_SendMsgs[i]) + 1;
193 msgs[i].msg_hdr.msg_iov = &io[i];
194 msgs[i].msg_hdr.msg_iovlen = 1;
195 msgs[i].msg_len = sizeof(struct msghdr);
198 if (sendmmsg(fd, msgs, NUM_SEND_MSGS, 0) < 0) {
199 GTEST_LOG_(ERROR) << "sendmmsg call failed: " << strerror(errno);
205 static void TestSendMMsg(struct sockaddr_un *addr, int fd) {
206 socklen_t len = sizeof(*addr);
207 int fd_acc = accept(fd, reinterpret_cast<struct sockaddr*>(addr), &len);
208 ASSERT_NE(fd_acc, -1) << strerror(errno);
212 FD_SET(fd_acc, &read_set);
214 for (size_t i = 0; i < NUM_SEND_MSGS; i++) {
218 ASSERT_LT(0, select(fd_acc+1, &read_set, NULL, NULL, &tv));
220 ASSERT_EQ(strlen(g_SendMsgs[i]) + 1,
221 static_cast<size_t>(recv(fd_acc, buffer, sizeof(buffer), 0)));
222 ASSERT_STREQ(g_SendMsgs[i], buffer);
228 TEST(sys_socket, sendmmsg_smoke) {
229 RunTest(TestSendMMsg, SendMMsg, "test_sendmmsg");
232 TEST(sys_socket, sendmmsg_error) {
233 ASSERT_EQ(-1, sendmmsg(-1, NULL, 0, 0));
234 ASSERT_EQ(EBADF, errno);