1 /* Test of select() substitute.
2 Copyright (C) 2008-2010 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 <sys/select.h>
23 #include "signature.h"
24 SIGNATURE_CHECK (select, int, (int, fd_set *, fd_set *, fd_set *,
26 /* The following may be macros without underlying functions, so only
27 check signature if they are not macros. */
29 SIGNATURE_CHECK (FD_CLR, void, (int, fd_set *));
32 SIGNATURE_CHECK (FD_ISSET, void, (int, fd_set *));
35 SIGNATURE_CHECK (FD_SET, int, (int, fd_set *));
38 SIGNATURE_CHECK (FD_ZERO, void, (fd_set *));
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
49 #include <sys/ioctl.h>
54 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
56 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
60 #ifdef HAVE_SYS_WAIT_H
61 # include <sys/wait.h>
65 # define SO_REUSEPORT SO_REUSEADDR
68 #define TEST_PORT 12345
71 /* Minimal testing infrastructure. */
76 failed (const char *reason)
80 printf ("failed (%s)\n", reason);
84 test (void (*fn) (void), const char *msg)
87 printf ("%s... ", msg);
98 /* Funny socket code. */
101 open_server_socket (void)
104 struct sockaddr_in ia;
106 s = socket (AF_INET, SOCK_STREAM, 0);
108 memset (&ia, 0, sizeof (ia));
109 ia.sin_family = AF_INET;
110 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
111 ia.sin_port = htons (TEST_PORT);
112 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
119 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
121 if (listen (s, 1) < 0)
131 connect_to_socket (int blocking)
134 struct sockaddr_in ia;
136 s = socket (AF_INET, SOCK_STREAM, 0);
138 memset (&ia, 0, sizeof (ia));
139 ia.sin_family = AF_INET;
140 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
141 ia.sin_port = htons (TEST_PORT);
146 unsigned long iMode = 1;
147 ioctl (s, FIONBIO, (char *) &iMode);
149 #elif defined F_GETFL
150 int oldflags = fcntl (s, F_GETFL, NULL);
152 if (!(oldflags & O_NONBLOCK))
153 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
157 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
158 && (blocking || errno != EINPROGRESS))
168 /* A slightly more convenient interface to select(2). */
171 do_select (int fd, int ev, struct timeval *tv)
173 fd_set rfds, wfds, xfds;
185 r = select (fd + 1, &rfds, &wfds, &xfds, tv);
190 if (FD_ISSET (fd, &rfds))
192 if (FD_ISSET (fd, &wfds))
194 if (FD_ISSET (fd, &xfds))
197 failed ("select returned 0");
199 failed ("select returned unrequested events");
205 do_select_nowait (int fd, int ev)
207 static struct timeval tv0;
208 return do_select (fd, ev, &tv0);
212 do_select_wait (int fd, int ev)
214 return do_select (fd, ev, NULL);
218 /* Test poll(2) for TTYs. */
224 if (do_select_nowait (0, SEL_IN) != 0)
226 if (do_select_nowait (0, SEL_OUT) == 0)
227 failed ("cannot write");
229 if (do_select_wait (0, SEL_IN) == 0)
230 failed ("return with infinite timeout");
233 if (do_select_nowait (0, SEL_IN) != 0)
234 failed ("can read after getc");
239 /* Test poll(2) for unconnected nonblocking sockets. */
242 test_connect_first (void)
244 int s = open_server_socket ();
245 struct sockaddr_in ia;
250 if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
251 failed ("can read, socket not connected");
253 c1 = connect_to_socket (false);
255 if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
256 failed ("expecting readability on passive socket");
257 if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
258 failed ("expecting readability on passive socket");
260 addrlen = sizeof (ia);
261 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
262 ASSERT (close (s) == 0);
263 ASSERT (close (c1) == 0);
264 ASSERT (close (c2) == 0);
268 /* Test poll(2) for unconnected blocking sockets. */
271 test_accept_first (void)
274 int s = open_server_socket ();
275 struct sockaddr_in ia;
286 addrlen = sizeof (ia);
287 c = accept (s, (struct sockaddr *) &ia, &addrlen);
288 ASSERT (close (s) == 0);
289 ASSERT (write (c, "foo", 3) == 3);
290 ASSERT (read (c, buf, 3) == 3);
291 shutdown (c, SHUT_RD);
292 ASSERT (close (c) == 0);
297 ASSERT (close (s) == 0);
298 c = connect_to_socket (true);
299 if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
300 failed ("cannot write after blocking connect");
301 ASSERT (write (c, "foo", 3) == 3);
303 if (do_select_wait (c, SEL_IN) != SEL_IN)
304 failed ("cannot read data left in the socket by closed process");
305 ASSERT (read (c, buf, 3) == 3);
306 ASSERT (write (c, "foo", 3) == 3);
307 (void) close (c); /* may fail with errno = ECONNRESET */
313 /* Common code for pipes and connected sockets. */
316 test_pair (int rd, int wd)
319 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
320 failed ("expecting writability before writing");
321 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
322 failed ("expecting writability before writing");
324 ASSERT (write (wd, "foo", 3) == 3);
325 if (do_select_wait (rd, SEL_IN) != SEL_IN)
326 failed ("expecting readability after writing");
327 if (do_select_nowait (rd, SEL_IN) != SEL_IN)
328 failed ("expecting readability after writing");
330 ASSERT (read (rd, buf, 3) == 3);
334 /* Test poll(2) on connected sockets. */
337 test_socket_pair (void)
339 struct sockaddr_in ia;
341 socklen_t addrlen = sizeof (ia);
342 int s = open_server_socket ();
343 int c1 = connect_to_socket (false);
344 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
346 ASSERT (close (s) == 0);
349 ASSERT (close (c1) == 0);
350 ASSERT (write (c2, "foo", 3) == 3);
351 (void) close (c2); /* may fail with errno = ECONNRESET */
355 /* Test poll(2) on pipes. */
362 ASSERT (pipe (fd) == 0);
363 test_pair (fd[0], fd[1]);
364 ASSERT (close (fd[0]) == 0);
365 ASSERT (close (fd[1]) == 0);
377 printf ("Please press Enter\n");
378 test (test_tty, "TTY");
381 result = test (test_connect_first, "Unconnected socket test");
382 result += test (test_socket_pair, "Connected sockets test");
383 result += test (test_accept_first, "General socket test with fork");
384 result += test (test_pipe, "Pipe test");