1 /*
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 */
17 #include <gtest/gtest.h>
19 #include <errno.h>
20 #include <sys/types.h>
21 #include <sys/socket.h>
22 #include <sys/un.h>
23 #include <fcntl.h>
25 #define SOCK_PATH "test"
27 static void* ConnectFn(void* data) {
28 bool (*callback_fn)(int) = reinterpret_cast<bool (*)(int)>(data);
29 void* return_value = NULL;
31 int fd = socket(PF_UNIX, SOCK_SEQPACKET | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
32 if (fd < 0) {
33 GTEST_LOG_(ERROR) << "socket call failed: " << strerror(errno);
34 return reinterpret_cast<void*>(-1);
35 }
37 struct sockaddr_un addr;
38 memset(&addr, 0, sizeof(addr));
39 addr.sun_family = AF_UNIX;
40 addr.sun_path[0] = '\0';
41 strcpy(addr.sun_path + 1, SOCK_PATH);
43 if (connect(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr)) < 0) {
44 GTEST_LOG_(ERROR) << "connect call failed: " << strerror(errno);
45 return_value = reinterpret_cast<void*>(-1);
46 }
47 else if (callback_fn != NULL && !callback_fn(fd)) {
48 return_value = reinterpret_cast<void*>(-1);
49 }
51 close(fd);
53 return return_value;
54 }
56 static void RunTest(void (*test_fn)(struct sockaddr_un*, int),
57 bool (*callback_fn)(int fd)) {
58 int fd = socket(PF_UNIX, SOCK_SEQPACKET, 0);
59 ASSERT_NE(fd, -1) << strerror(errno);
61 struct sockaddr_un addr;
62 memset(&addr, 0, sizeof(addr));
63 addr.sun_family = AF_UNIX;
64 addr.sun_path[0] = '\0';
65 strcpy(addr.sun_path + 1, SOCK_PATH);
67 ASSERT_NE(-1, bind(fd, reinterpret_cast<struct sockaddr*>(&addr), sizeof(addr))) << strerror(errno);
69 ASSERT_NE(-1, listen(fd, 1)) << strerror(errno);
71 pthread_t thread;
72 ASSERT_EQ(0, pthread_create(&thread, NULL, ConnectFn, reinterpret_cast<void*>(callback_fn)));
74 fd_set read_set;
75 FD_ZERO(&read_set);
76 FD_SET(fd, &read_set);
77 timeval tv;
78 tv.tv_sec = 5;
79 tv.tv_usec = 0;
80 ASSERT_LT(0, select(fd+1, &read_set, NULL, NULL, &tv));
82 test_fn(&addr, fd);
84 void* ret_val;
85 ASSERT_EQ(0, pthread_join(thread, &ret_val));
86 ASSERT_EQ(NULL, ret_val);
88 close(fd);
89 }
91 TEST(sys_socket, accept4_error) {
92 ASSERT_EQ(-1, accept4(-1, NULL, NULL, 0));
93 ASSERT_EQ(EBADF, errno);
94 }
96 static void TestAccept4(struct sockaddr_un* addr, int fd) {
97 socklen_t len = sizeof(*addr);
98 int fd_acc = accept4(fd, reinterpret_cast<struct sockaddr*>(addr), &len, SOCK_CLOEXEC);
99 ASSERT_NE(fd_acc, -1) << strerror(errno);
101 // Check that the flag was set properly.
102 ASSERT_EQ(FD_CLOEXEC, fcntl(fd_acc, F_GETFD) & FD_CLOEXEC);
104 close(fd_acc);
105 }
107 TEST(sys_socket, accept4_smoke) {
108 RunTest(TestAccept4, NULL);
109 }
111 const char* g_RecvMsgs[] = {
112 "RECVMMSG_ONE",
113 "RECVMMSG_TWO",
114 "RECVMMSG_THREE",
115 };
116 #define NUM_RECV_MSGS (sizeof(g_RecvMsgs)/sizeof(const char*))
118 static bool SendMultiple(int fd) {
119 for (size_t i = 0; i < NUM_RECV_MSGS; i++) {
120 if (send(fd, g_RecvMsgs[i], strlen(g_RecvMsgs[i]) + 1, 0) < 0) {
121 GTEST_LOG_(ERROR) << "send call failed: " << strerror(errno);
122 return false;
123 }
124 }
126 return true;
127 }
129 static void TestRecvMMsg(struct sockaddr_un *addr, int fd) {
130 socklen_t len = sizeof(*addr);
131 int fd_acc = accept(fd, reinterpret_cast<struct sockaddr*>(addr), &len);
132 ASSERT_NE(fd_acc, -1) << strerror(errno);
134 struct mmsghdr msgs[NUM_RECV_MSGS];
135 memset(msgs, 0, sizeof(struct mmsghdr)*NUM_RECV_MSGS);
137 struct iovec io[NUM_RECV_MSGS];
138 char bufs[NUM_RECV_MSGS][100];
139 for (size_t i = 0; i < NUM_RECV_MSGS; i++) {
140 io[i].iov_base = reinterpret_cast<void*>(bufs[i]);
141 io[i].iov_len = strlen(g_RecvMsgs[i]) + 1;
143 msgs[i].msg_hdr.msg_iov = &io[i];
144 msgs[i].msg_hdr.msg_iovlen = 1;
145 msgs[i].msg_len = sizeof(struct msghdr);
146 }
148 struct timespec ts;
149 memset(&ts, 0, sizeof(ts));
150 ts.tv_sec = 5;
151 ts.tv_nsec = 0;
152 ASSERT_EQ(NUM_RECV_MSGS,
153 static_cast<size_t>(recvmmsg(fd_acc, msgs, NUM_RECV_MSGS, 0, &ts)))
154 << strerror(errno);
155 for (size_t i = 0; i < NUM_RECV_MSGS; i++) {
156 ASSERT_STREQ(g_RecvMsgs[i], bufs[i]);
157 }
159 close(fd_acc);
160 }
162 TEST(sys_socket, recvmmsg_smoke) {
163 RunTest(TestRecvMMsg, SendMultiple);
164 }
166 TEST(sys_socket, recvmmsg_error) {
167 ASSERT_EQ(-1, recvmmsg(-1, NULL, 0, 0, NULL));
168 ASSERT_EQ(EBADF, errno);
169 }
171 const char* g_SendMsgs[] = {
172 "MSG_ONE",
173 "MSG_TWO",
174 "MSG_THREE"
175 };
176 #define NUM_SEND_MSGS (sizeof(g_SendMsgs)/sizeof(const char*))
178 static bool SendMMsg(int fd) {
179 struct mmsghdr msgs[NUM_SEND_MSGS];
180 memset(msgs, 0, sizeof(struct mmsghdr)*NUM_SEND_MSGS);
181 struct iovec io[NUM_SEND_MSGS];
182 for (size_t i = 0; i < NUM_SEND_MSGS; i++) {
183 io[i].iov_base = reinterpret_cast<void*>(const_cast<char*>(g_SendMsgs[i]));
184 io[i].iov_len = strlen(g_SendMsgs[i]) + 1;
185 msgs[i].msg_hdr.msg_iov = &io[i];
186 msgs[i].msg_hdr.msg_iovlen = 1;
187 msgs[i].msg_len = sizeof(struct msghdr);
188 }
190 if (sendmmsg(fd, msgs, NUM_SEND_MSGS, 0) < 0) {
191 GTEST_LOG_(ERROR) << "sendmmsg call failed: " << strerror(errno);
192 return false;
193 }
194 return true;
195 }
197 static void TestSendMMsg(struct sockaddr_un *addr, int fd) {
198 socklen_t len = sizeof(*addr);
199 int fd_acc = accept(fd, reinterpret_cast<struct sockaddr*>(addr), &len);
200 ASSERT_NE(fd_acc, -1) << strerror(errno);
202 fd_set read_set;
203 FD_ZERO(&read_set);
204 FD_SET(fd_acc, &read_set);
206 for (size_t i = 0; i < NUM_SEND_MSGS; i++) {
207 timeval tv;
208 tv.tv_sec = 5;
209 tv.tv_usec = 0;
210 ASSERT_LT(0, select(fd_acc+1, &read_set, NULL, NULL, &tv));
211 char buffer[100];
212 ASSERT_EQ(strlen(g_SendMsgs[i]) + 1,
213 static_cast<size_t>(recv(fd_acc, buffer, sizeof(buffer), 0)));
214 ASSERT_STREQ(g_SendMsgs[i], buffer);
215 }
217 close(fd_acc);
218 }
220 TEST(sys_socket, sendmmsg_smoke) {
221 RunTest(TestSendMMsg, SendMMsg);
222 }
224 TEST(sys_socket, sendmmsg_error) {
225 ASSERT_EQ(-1, sendmmsg(-1, NULL, 0, 0));
226 ASSERT_EQ(EBADF, errno);
227 }