2 * Copyright (c) 2004 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
30 #include <sys/event.h>
31 #include <sys/socket.h>
41 static int curtest = 1;
44 * This test uses UNIX domain socket pairs to perform some basic exercising
45 * of kqueue functionality on sockets. In particular, testing that for read
46 * and write filters, we see the correct detection of whether reads and
47 * writes should actually be able to occur.
50 * - Test read/write filters for listen/accept sockets.
51 * - Handle the XXXRW below regarding datagram sockets.
52 * - Test that watermark/buffer size "data" fields returned by kqueue are
54 * - Check that kqueue does something sensible when the remote endpoing is
58 #define OK(testname) printf("ok %d - %s\n", curtest, testname); \
62 fail(int error, const char *func, const char *socktype, const char *rest)
65 printf("not ok %d\n", curtest);
68 printf("# %s(): %s\n", func, strerror(error));
69 else if (rest == NULL)
70 printf("# %s(%s): %s\n", func, socktype,
73 printf("# %s(%s, %s): %s\n", func, socktype, rest,
79 fail_assertion(const char *func, const char *socktype, const char *rest,
80 const char *assertion)
83 printf("not ok %d - %s\n", curtest, assertion);
86 printf("# %s(): assertion %s failed\n", func,
88 else if (rest == NULL)
89 printf("# %s(%s): assertion %s failed\n", func,
92 printf("# %s(%s, %s): assertion %s failed\n", func,
93 socktype, rest, assertion);
98 * Test read kevent on a socket pair: check to make sure endpoint 0 isn't
99 * readable when we start, then write to endpoint 1 and confirm that endpoint
100 * 0 is now readable. Drain the write, then check that it's not readable
101 * again. Use non-blocking kqueue operations and socket operations.
104 test_evfilt_read(int kq, int fd[2], const char *socktype)
112 EV_SET(&ke, fd[0], EVFILT_READ, EV_ADD, 0, 0, NULL);
113 if (kevent(kq, &ke, 1, NULL, 0, NULL) == -1)
114 fail(errno, "kevent", socktype, "EVFILT_READ, EV_ADD");
115 OK("EVFILT_READ, EV_ADD");
118 * Confirm not readable to begin with, no I/O yet.
122 i = kevent(kq, NULL, 0, &ke, 1, &ts);
124 fail(errno, "kevent", socktype, "EVFILT_READ");
127 fail_assertion("kevent", socktype, "EVFILT_READ",
128 "empty socket unreadable");
129 OK("empty socket unreadable");
132 * Write a byte to one end.
135 len = write(fd[1], &ch, sizeof(ch));
137 fail(errno, "write", socktype, NULL);
138 OK("write one byte");
139 if (len != sizeof(ch))
140 fail_assertion("write", socktype, NULL, "write length");
141 OK("write one byte length");
144 * Other end should now be readable.
148 i = kevent(kq, NULL, 0, &ke, 1, &ts);
150 fail(errno, "kevent", socktype, "EVFILT_READ");
153 fail_assertion("kevent", socktype, "EVFILT_READ",
154 "non-empty socket unreadable");
155 OK("non-empty socket unreadable");
158 * Read a byte to clear the readable state.
160 len = read(fd[0], &ch, sizeof(ch));
162 fail(errno, "read", socktype, NULL);
164 if (len != sizeof(ch))
165 fail_assertion("read", socktype, NULL, "read length");
166 OK("read one byte length");
169 * Now re-check for readability.
173 i = kevent(kq, NULL, 0, &ke, 1, &ts);
175 fail(errno, "kevent", socktype, "EVFILT_READ");
178 fail_assertion("kevent", socktype, "EVFILT_READ",
179 "empty socket unreadable");
180 OK("empty socket unreadable");
182 EV_SET(&ke, fd[0], EVFILT_READ, EV_DELETE, 0, 0, NULL);
183 if (kevent(kq, &ke, 1, NULL, 0, NULL) == -1)
184 fail(errno, "kevent", socktype, "EVFILT_READ, EV_DELETE");
185 OK("EVFILT_READ, EV_DELETE");
189 test_evfilt_write(int kq, int fd[2], const char *socktype)
197 EV_SET(&ke, fd[0], EVFILT_WRITE, EV_ADD, 0, 0, NULL);
198 if (kevent(kq, &ke, 1, NULL, 0, NULL) == -1)
199 fail(errno, "kevent", socktype, "EVFILT_WRITE, EV_ADD");
200 OK("EVFILE_WRITE, EV_ADD");
203 * Confirm writable to begin with, no I/O yet.
207 i = kevent(kq, NULL, 0, &ke, 1, &ts);
209 fail(errno, "kevent", socktype, "EVFILT_WRITE");
212 fail_assertion("kevent", socktype, "EVFILT_WRITE",
213 "empty socket unwritable");
214 OK("empty socket unwritable");
217 * Write bytes into the socket until we can't write anymore.
220 while ((len = write(fd[0], &ch, sizeof(ch))) == sizeof(ch)) {};
221 if (len == -1 && errno != EAGAIN && errno != ENOBUFS)
222 fail(errno, "write", socktype, NULL);
224 if (len != -1 && len != sizeof(ch))
225 fail_assertion("write", socktype, NULL, "write length");
229 * Check to make sure the socket is no longer writable.
233 i = kevent(kq, NULL, 0, &ke, 1, &ts);
235 fail(errno, "kevent", socktype, "EVFILT_WRITE");
238 fail_assertion("kevent", socktype, "EVFILT_WRITE",
239 "full socket writable");
240 OK("full socket writable");
242 EV_SET(&ke, fd[0], EVFILT_WRITE, EV_DELETE, 0, 0, NULL);
243 if (kevent(kq, &ke, 1, NULL, 0, NULL) == -1)
244 fail(errno, "kevent", socktype, "EVFILT_WRITE, EV_DELETE");
245 OK("EVFILT_WRITE, EV_DELETE");
249 * Basic registration exercise for kqueue(2). Create several types/brands of
250 * sockets, and confirm that we can register for various events on them.
253 main(int argc, char *argv[])
261 fail(errno, "kqueue", NULL, NULL);
265 * Create a UNIX domain datagram socket, and attach/test/detach a
268 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) == -1)
269 fail(errno, "socketpair", "PF_UNIX, SOCK_DGRAM", NULL);
270 OK("socketpair() 1");
272 if (fcntl(sv[0], F_SETFL, O_NONBLOCK) != 0)
273 fail(errno, "fcntl", "PF_UNIX, SOCK_DGRAM", "O_NONBLOCK");
275 if (fcntl(sv[1], F_SETFL, O_NONBLOCK) != 0)
276 fail(errno, "fcntl", "PF_UNIX, SOCK_DGRAM", "O_NONBLOCK");
279 test_evfilt_read(kq, sv, "PF_UNIX, SOCK_DGRAM");
281 if (close(sv[0]) == -1)
282 fail(errno, "close", "PF_UNIX/SOCK_DGRAM", "sv[0]");
284 if (close(sv[1]) == -1)
285 fail(errno, "close", "PF_UNIX/SOCK_DGRAM", "sv[1]");
290 * XXXRW: We disable the write test in the case of datagram sockets,
291 * as kqueue can't tell when the remote socket receive buffer is
292 * full, whereas the UNIX domain socket implementation can tell and
296 * Create a UNIX domain datagram socket, and attach/test/detach a
297 * write filter on it.
299 if (socketpair(PF_UNIX, SOCK_DGRAM, 0, sv) == -1)
300 fail(errno, "socketpair", "PF_UNIX, SOCK_DGRAM", NULL);
302 if (fcntl(sv[0], F_SETFL, O_NONBLOCK) != 0)
303 fail(errno, "fcntl", "PF_UNIX, SOCK_DGRAM", "O_NONBLOCK");
304 if (fcntl(sv[1], F_SETFL, O_NONBLOCK) != 0)
305 fail(errno, "fcntl", "PF_UNIX, SOCK_DGRAM", "O_NONBLOCK");
307 test_evfilt_write(kq, sv, "PF_UNIX, SOCK_DGRAM");
309 if (close(sv[0]) == -1)
310 fail(errno, "close", "PF_UNIX/SOCK_DGRAM", "sv[0]");
311 if (close(sv[1]) == -1)
312 fail(errno, "close", "PF_UNIX/SOCK_DGRAM", "sv[1]");
316 * Create a UNIX domain stream socket, and attach/test/detach a
319 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1)
320 fail(errno, "socketpair", "PF_UNIX, SOCK_STREAM", NULL);
321 OK("socketpair() 2");
323 if (fcntl(sv[0], F_SETFL, O_NONBLOCK) != 0)
324 fail(errno, "fcntl", "PF_UNIX, SOCK_STREAM", "O_NONBLOCK");
326 if (fcntl(sv[1], F_SETFL, O_NONBLOCK) != 0)
327 fail(errno, "fcntl", "PF_UNIX, SOCK_STREAM", "O_NONBLOCK");
330 test_evfilt_read(kq, sv, "PF_UNIX, SOCK_STREAM");
332 if (close(sv[0]) == -1)
333 fail(errno, "close", "PF_UNIX/SOCK_STREAM", "sv[0]");
335 if (close(sv[1]) == -1)
336 fail(errno, "close", "PF_UNIX/SOCK_STREAM", "sv[1]");
340 * Create a UNIX domain stream socket, and attach/test/detach a
341 * write filter on it.
343 if (socketpair(PF_UNIX, SOCK_STREAM, 0, sv) == -1)
344 fail(errno, "socketpair", "PF_UNIX, SOCK_STREAM", NULL);
345 OK("socketpair() 3");
347 if (fcntl(sv[0], F_SETFL, O_NONBLOCK) != 0)
348 fail(errno, "fcntl", "PF_UNIX, SOCK_STREAM", "O_NONBLOCK");
350 if (fcntl(sv[1], F_SETFL, O_NONBLOCK) != 0)
351 fail(errno, "fcntl", "PF_UNIX, SOCK_STREAM", "O_NONBLOCK");
354 test_evfilt_write(kq, sv, "PF_UNIX, SOCK_STREAM");
356 if (close(sv[0]) == -1)
357 fail(errno, "close", "PF_UNIX/SOCK_STREAM", "sv[0]");
359 if (close(sv[1]) == -1)
360 fail(errno, "close", "PF_UNIX/SOCK_STREAM", "sv[1]");
364 fail(errno, "close", "kq", NULL);