1 /* Test of select() substitute.
2 Copyright (C) 2008 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>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
30 #include <sys/ioctl.h>
34 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
36 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
42 #define pipe(x) _pipe(x, 256, O_BINARY)
47 #ifdef HAVE_SYS_WAIT_H
52 #define SO_REUSEPORT SO_REUSEADDR
55 #define TEST_PORT 12345
58 /* Minimal testing infrastructure. */
63 failed (const char *reason)
67 printf ("failed (%s)\n", reason);
71 test (void (*fn) (void), const char *msg)
74 printf ("%s... ", msg);
85 /* Funny socket code. */
91 struct sockaddr_in ia;
93 s = socket (AF_INET, SOCK_STREAM, 0);
95 memset (&ia, 0, sizeof (ia));
96 ia.sin_family = AF_INET;
97 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
98 ia.sin_port = htons (TEST_PORT);
99 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
106 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
108 if (listen (s, 1) < 0)
118 connect_to_socket (int blocking)
121 struct sockaddr_in ia;
123 s = socket (AF_INET, SOCK_STREAM, 0);
125 memset (&ia, 0, sizeof (ia));
126 ia.sin_family = AF_INET;
127 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
128 ia.sin_port = htons (TEST_PORT);
133 unsigned long iMode = 1;
134 ioctl (s, FIONBIO, (char *) &iMode);
136 #elif defined F_GETFL
137 int oldflags = fcntl (s, F_GETFL, NULL);
139 if (!(oldflags & O_NONBLOCK))
140 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
144 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
145 && (blocking || errno != EINPROGRESS))
155 /* A slightly more convenient interface to select(2). */
158 do_select (int fd, int ev, struct timeval *tv)
160 fd_set rfds, wfds, xfds;
172 r = select (fd + 1, &rfds, &wfds, &xfds, tv);
177 if (FD_ISSET (fd, &rfds))
179 if (FD_ISSET (fd, &wfds))
181 if (FD_ISSET (fd, &xfds))
184 failed ("select returned 0");
186 failed ("select returned unrequested events");
192 do_select_nowait (int fd, int ev)
194 static struct timeval tv0;
195 return do_select (fd, ev, &tv0);
199 do_select_wait (int fd, int ev)
201 return do_select (fd, ev, NULL);
205 /* Test poll(2) for TTYs. */
211 if (do_select_nowait (0, SEL_IN) != 0)
213 if (do_select_nowait (0, SEL_OUT) == 0)
214 failed ("cannot write");
216 if (do_select_wait (0, SEL_IN) == 0)
217 failed ("return with infinite timeout");
220 if (do_select_nowait (0, SEL_IN) != 0)
221 failed ("can read after getc");
226 /* Test poll(2) for unconnected nonblocking sockets. */
229 test_connect_first (void)
231 int s = open_server_socket ();
232 struct sockaddr_in ia;
237 if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
238 failed ("can read, socket not connected");
240 c1 = connect_to_socket (false);
242 if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
243 failed ("expecting readability on passive socket");
244 if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
245 failed ("expecting readability on passive socket");
247 addrlen = sizeof (ia);
248 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
255 /* Test poll(2) for unconnected blocking sockets. */
258 test_accept_first (void)
261 int s = open_server_socket ();
262 struct sockaddr_in ia;
273 addrlen = sizeof (ia);
274 c = accept (s, (struct sockaddr *) &ia, &addrlen);
278 shutdown (c, SHUT_RD);
285 c = connect_to_socket (true);
286 if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
287 failed ("cannot write after blocking connect");
290 if (do_select_wait (c, SEL_IN) != SEL_IN)
291 failed ("cannot read data left in the socket by closed process");
300 /* Common code for pipes and connected sockets. */
303 test_pair (int rd, int wd)
306 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
307 failed ("expecting writability before writing");
308 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
309 failed ("expecting writability before writing");
311 write (wd, "foo", 3);
312 if (do_select_wait (rd, SEL_IN) != SEL_IN)
313 failed ("expecting readability after writing");
314 if (do_select_nowait (rd, SEL_IN) != SEL_IN)
315 failed ("expecting readability after writing");
321 /* Test poll(2) on connected sockets. */
324 test_socket_pair (void)
326 struct sockaddr_in ia;
328 socklen_t addrlen = sizeof (ia);
329 int s = open_server_socket ();
330 int c1 = connect_to_socket (false);
331 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
337 write (c2, "foo", 3);
342 /* Test poll(2) on pipes. */
350 test_pair (fd[0], fd[1]);
363 gl_sockets_startup (SOCKETS_1_1);
366 printf ("Please press Enter\n");
367 test (test_tty, "TTY");
370 result = test (test_connect_first, "Unconnected socket test");
371 result += test (test_socket_pair, "Connected sockets test");
372 result += test (test_accept_first, "General socket test with fork");
373 result += test (test_pipe, "Pipe test");