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>
48 #include <sys/ioctl.h>
53 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
55 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
61 #define pipe(x) _pipe(x, 256, O_BINARY)
66 #ifdef HAVE_SYS_WAIT_H
71 #define SO_REUSEPORT SO_REUSEADDR
74 #define TEST_PORT 12345
77 /* Minimal testing infrastructure. */
82 failed (const char *reason)
86 printf ("failed (%s)\n", reason);
90 test (void (*fn) (void), const char *msg)
93 printf ("%s... ", msg);
104 /* Funny socket code. */
107 open_server_socket (void)
110 struct sockaddr_in ia;
112 s = socket (AF_INET, SOCK_STREAM, 0);
114 memset (&ia, 0, sizeof (ia));
115 ia.sin_family = AF_INET;
116 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
117 ia.sin_port = htons (TEST_PORT);
118 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
125 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
127 if (listen (s, 1) < 0)
137 connect_to_socket (int blocking)
140 struct sockaddr_in ia;
142 s = socket (AF_INET, SOCK_STREAM, 0);
144 memset (&ia, 0, sizeof (ia));
145 ia.sin_family = AF_INET;
146 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
147 ia.sin_port = htons (TEST_PORT);
152 unsigned long iMode = 1;
153 ioctl (s, FIONBIO, (char *) &iMode);
155 #elif defined F_GETFL
156 int oldflags = fcntl (s, F_GETFL, NULL);
158 if (!(oldflags & O_NONBLOCK))
159 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
163 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
164 && (blocking || errno != EINPROGRESS))
174 /* A slightly more convenient interface to select(2). */
177 do_select (int fd, int ev, struct timeval *tv)
179 fd_set rfds, wfds, xfds;
191 r = select (fd + 1, &rfds, &wfds, &xfds, tv);
196 if (FD_ISSET (fd, &rfds))
198 if (FD_ISSET (fd, &wfds))
200 if (FD_ISSET (fd, &xfds))
203 failed ("select returned 0");
205 failed ("select returned unrequested events");
211 do_select_nowait (int fd, int ev)
213 static struct timeval tv0;
214 return do_select (fd, ev, &tv0);
218 do_select_wait (int fd, int ev)
220 return do_select (fd, ev, NULL);
224 /* Test poll(2) for TTYs. */
230 if (do_select_nowait (0, SEL_IN) != 0)
232 if (do_select_nowait (0, SEL_OUT) == 0)
233 failed ("cannot write");
235 if (do_select_wait (0, SEL_IN) == 0)
236 failed ("return with infinite timeout");
239 if (do_select_nowait (0, SEL_IN) != 0)
240 failed ("can read after getc");
245 /* Test poll(2) for unconnected nonblocking sockets. */
248 test_connect_first (void)
250 int s = open_server_socket ();
251 struct sockaddr_in ia;
256 if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
257 failed ("can read, socket not connected");
259 c1 = connect_to_socket (false);
261 if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
262 failed ("expecting readability on passive socket");
263 if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
264 failed ("expecting readability on passive socket");
266 addrlen = sizeof (ia);
267 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
268 ASSERT (close (s) == 0);
269 ASSERT (close (c1) == 0);
270 ASSERT (close (c2) == 0);
274 /* Test poll(2) for unconnected blocking sockets. */
277 test_accept_first (void)
280 int s = open_server_socket ();
281 struct sockaddr_in ia;
292 addrlen = sizeof (ia);
293 c = accept (s, (struct sockaddr *) &ia, &addrlen);
294 ASSERT (close (s) == 0);
295 ASSERT (write (c, "foo", 3) == 3);
296 ASSERT (read (c, buf, 3) == 3);
297 shutdown (c, SHUT_RD);
298 ASSERT (close (c) == 0);
303 ASSERT (close (s) == 0);
304 c = connect_to_socket (true);
305 if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
306 failed ("cannot write after blocking connect");
307 ASSERT (write (c, "foo", 3) == 3);
309 if (do_select_wait (c, SEL_IN) != SEL_IN)
310 failed ("cannot read data left in the socket by closed process");
311 ASSERT (read (c, buf, 3) == 3);
312 ASSERT (write (c, "foo", 3) == 3);
313 ASSERT (close (c) == 0);
319 /* Common code for pipes and connected sockets. */
322 test_pair (int rd, int wd)
325 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
326 failed ("expecting writability before writing");
327 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
328 failed ("expecting writability before writing");
330 ASSERT (write (wd, "foo", 3) == 3);
331 if (do_select_wait (rd, SEL_IN) != SEL_IN)
332 failed ("expecting readability after writing");
333 if (do_select_nowait (rd, SEL_IN) != SEL_IN)
334 failed ("expecting readability after writing");
336 ASSERT (read (rd, buf, 3) == 3);
340 /* Test poll(2) on connected sockets. */
343 test_socket_pair (void)
345 struct sockaddr_in ia;
347 socklen_t addrlen = sizeof (ia);
348 int s = open_server_socket ();
349 int c1 = connect_to_socket (false);
350 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
352 ASSERT (close (s) == 0);
355 ASSERT (close (c1) == 0);
356 ASSERT (write (c2, "foo", 3) == 3);
357 ASSERT (close (c2) == 0);
361 /* Test poll(2) on pipes. */
368 ASSERT (pipe (fd) == 0);
369 test_pair (fd[0], fd[1]);
370 ASSERT (close (fd[0]) == 0);
371 ASSERT (close (fd[1]) == 0);
383 printf ("Please press Enter\n");
384 test (test_tty, "TTY");
387 result = test (test_connect_first, "Unconnected socket test");
388 result += test (test_socket_pair, "Connected sockets test");
389 result += test (test_accept_first, "General socket test with fork");
390 result += test (test_pipe, "Pipe test");