1 /* Test of select() substitute.
2 Copyright (C) 2008-2011 Free Software Foundation, Inc.
4 This program is free software: you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>. */
17 /* Written by Paolo Bonzini, 2008. */
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
27 #include <sys/ioctl.h>
32 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
36 #ifdef HAVE_SYS_WAIT_H
37 # include <sys/wait.h>
41 # define SO_REUSEPORT SO_REUSEADDR
44 #define TEST_PORT 12345
47 typedef int (*select_fn) (int, fd_set *, fd_set *, fd_set *, struct timeval *);
50 /* Minimal testing infrastructure. */
55 failed (const char *reason)
59 printf ("failed (%s)\n", reason);
63 test (void (*fn) (select_fn), select_fn my_select, const char *msg)
66 printf ("%s... ", msg);
77 /* Funny socket code. */
80 open_server_socket (void)
83 struct sockaddr_in ia;
85 s = socket (AF_INET, SOCK_STREAM, 0);
87 memset (&ia, 0, sizeof (ia));
88 ia.sin_family = AF_INET;
89 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
90 ia.sin_port = htons (TEST_PORT);
91 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
98 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
100 if (listen (s, 1) < 0)
110 connect_to_socket (bool blocking)
113 struct sockaddr_in ia;
115 s = socket (AF_INET, SOCK_STREAM, 0);
117 memset (&ia, 0, sizeof (ia));
118 ia.sin_family = AF_INET;
119 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
120 ia.sin_port = htons (TEST_PORT);
125 unsigned long iMode = 1;
126 ioctl (s, FIONBIO, (char *) &iMode);
128 #elif defined F_GETFL
129 int oldflags = fcntl (s, F_GETFL, NULL);
131 if (!(oldflags & O_NONBLOCK))
132 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
136 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
137 && (blocking || errno != EINPROGRESS))
147 /* A slightly more convenient interface to select(2).
148 Waits until a specific event occurs on a file descriptor FD.
149 EV is a bit mask of events to look for:
150 SEL_IN - input can be polled without blocking,
151 SEL_OUT - output can be provided without blocking,
152 SEL_EXC - an exception occurred,
153 A maximum wait time is specified by TIMEOUT.
154 *TIMEOUT = { 0, 0 } means to return immediately,
155 TIMEOUT = NULL means to wait indefinitely. */
157 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
160 do_select (int fd, int ev, struct timeval *timeout, select_fn my_select)
162 fd_set rfds, wfds, xfds;
174 r = my_select (fd + 1, &rfds, &wfds, &xfds, timeout);
179 if (FD_ISSET (fd, &rfds))
181 if (FD_ISSET (fd, &wfds))
183 if (FD_ISSET (fd, &xfds))
186 failed ("select returned 0");
188 failed ("select returned unrequested events");
194 do_select_nowait (int fd, int ev, select_fn my_select)
199 return do_select (fd, ev, &tv0, my_select);
203 do_select_wait (int fd, int ev, select_fn my_select)
205 return do_select (fd, ev, NULL, my_select);
209 /* Test select(2) for TTYs. */
213 test_tty (select_fn my_select)
215 if (do_select_nowait (0, SEL_IN, my_select) != 0)
217 if (do_select_nowait (0, SEL_OUT, my_select) == 0)
218 failed ("cannot write");
220 if (do_select_wait (0, SEL_IN, my_select) == 0)
221 failed ("return with infinite timeout");
224 if (do_select_nowait (0, SEL_IN, my_select) != 0)
225 failed ("can read after getc");
230 /* Test select(2) for unconnected nonblocking sockets. */
233 test_connect_first (select_fn my_select)
235 int s = open_server_socket ();
236 struct sockaddr_in ia;
241 if (do_select_nowait (s, SEL_IN | SEL_EXC, my_select) != 0)
242 failed ("can read, socket not connected");
244 c1 = connect_to_socket (false);
246 if (do_select_wait (s, SEL_IN | SEL_EXC, my_select) != SEL_IN)
247 failed ("expecting readability on passive socket");
248 if (do_select_nowait (s, SEL_IN | SEL_EXC, my_select) != SEL_IN)
249 failed ("expecting readability on passive socket");
251 addrlen = sizeof (ia);
252 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
253 ASSERT (close (s) == 0);
254 ASSERT (close (c1) == 0);
255 ASSERT (close (c2) == 0);
259 /* Test select(2) for unconnected blocking sockets. */
262 test_accept_first (select_fn my_select)
265 int s = open_server_socket ();
266 struct sockaddr_in ia;
277 addrlen = sizeof (ia);
278 c = accept (s, (struct sockaddr *) &ia, &addrlen);
279 ASSERT (close (s) == 0);
280 ASSERT (write (c, "foo", 3) == 3);
281 ASSERT (read (c, buf, 3) == 3);
282 shutdown (c, SHUT_RD);
283 ASSERT (close (c) == 0);
288 ASSERT (close (s) == 0);
289 c = connect_to_socket (true);
290 if (do_select_nowait (c, SEL_OUT, my_select) != SEL_OUT)
291 failed ("cannot write after blocking connect");
292 ASSERT (write (c, "foo", 3) == 3);
294 if (do_select_wait (c, SEL_IN, my_select) != SEL_IN)
295 failed ("cannot read data left in the socket by closed process");
296 ASSERT (read (c, buf, 3) == 3);
297 ASSERT (write (c, "foo", 3) == 3);
298 (void) close (c); /* may fail with errno = ECONNRESET */
304 /* Common code for pipes and connected sockets. */
307 test_pair (int rd, int wd, select_fn my_select)
310 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC, my_select) != SEL_OUT)
311 failed ("expecting writability before writing");
312 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC, my_select) != SEL_OUT)
313 failed ("expecting writability before writing");
315 ASSERT (write (wd, "foo", 3) == 3);
316 if (do_select_wait (rd, SEL_IN, my_select) != SEL_IN)
317 failed ("expecting readability after writing");
318 if (do_select_nowait (rd, SEL_IN, my_select) != SEL_IN)
319 failed ("expecting readability after writing");
321 ASSERT (read (rd, buf, 3) == 3);
325 /* Test select(2) on connected sockets. */
328 test_socket_pair (select_fn my_select)
330 struct sockaddr_in ia;
332 socklen_t addrlen = sizeof (ia);
333 int s = open_server_socket ();
334 int c1 = connect_to_socket (false);
335 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
337 ASSERT (close (s) == 0);
339 test_pair (c1, c2, my_select);
340 ASSERT (close (c1) == 0);
341 ASSERT (write (c2, "foo", 3) == 3);
342 (void) close (c2); /* may fail with errno = ECONNRESET */
346 /* Test select(2) on pipes. */
349 test_pipe (select_fn my_select)
353 ASSERT (pipe (fd) == 0);
354 test_pair (fd[0], fd[1], my_select);
355 ASSERT (close (fd[0]) == 0);
356 ASSERT (close (fd[1]) == 0);
363 test_function (select_fn my_select)
368 printf ("Please press Enter\n");
369 test (test_tty, "TTY", my_select);
372 result = test (test_connect_first, my_select, "Unconnected socket test");
373 result += test (test_socket_pair, my_select, "Connected sockets test");
374 result += test (test_accept_first, my_select, "General socket test with fork");
375 result += test (test_pipe, my_select, "Pipe test");