1 /* Test of select() substitute.
2 Copyright (C) 2008-2009 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>
33 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
35 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
41 #define pipe(x) _pipe(x, 256, O_BINARY)
46 #ifdef HAVE_SYS_WAIT_H
51 #define SO_REUSEPORT SO_REUSEADDR
54 #define TEST_PORT 12345
57 /* Minimal testing infrastructure. */
62 failed (const char *reason)
66 printf ("failed (%s)\n", reason);
70 test (void (*fn) (void), const char *msg)
73 printf ("%s... ", msg);
84 /* Funny socket code. */
87 open_server_socket (void)
90 struct sockaddr_in ia;
92 s = socket (AF_INET, SOCK_STREAM, 0);
94 memset (&ia, 0, sizeof (ia));
95 ia.sin_family = AF_INET;
96 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
97 ia.sin_port = htons (TEST_PORT);
98 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
105 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
107 if (listen (s, 1) < 0)
117 connect_to_socket (int blocking)
120 struct sockaddr_in ia;
122 s = socket (AF_INET, SOCK_STREAM, 0);
124 memset (&ia, 0, sizeof (ia));
125 ia.sin_family = AF_INET;
126 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
127 ia.sin_port = htons (TEST_PORT);
132 unsigned long iMode = 1;
133 ioctl (s, FIONBIO, (char *) &iMode);
135 #elif defined F_GETFL
136 int oldflags = fcntl (s, F_GETFL, NULL);
138 if (!(oldflags & O_NONBLOCK))
139 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
143 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
144 && (blocking || errno != EINPROGRESS))
154 /* A slightly more convenient interface to select(2). */
157 do_select (int fd, int ev, struct timeval *tv)
159 fd_set rfds, wfds, xfds;
171 r = select (fd + 1, &rfds, &wfds, &xfds, tv);
176 if (FD_ISSET (fd, &rfds))
178 if (FD_ISSET (fd, &wfds))
180 if (FD_ISSET (fd, &xfds))
183 failed ("select returned 0");
185 failed ("select returned unrequested events");
191 do_select_nowait (int fd, int ev)
193 static struct timeval tv0;
194 return do_select (fd, ev, &tv0);
198 do_select_wait (int fd, int ev)
200 return do_select (fd, ev, NULL);
204 /* Test poll(2) for TTYs. */
210 if (do_select_nowait (0, SEL_IN) != 0)
212 if (do_select_nowait (0, SEL_OUT) == 0)
213 failed ("cannot write");
215 if (do_select_wait (0, SEL_IN) == 0)
216 failed ("return with infinite timeout");
219 if (do_select_nowait (0, SEL_IN) != 0)
220 failed ("can read after getc");
225 /* Test poll(2) for unconnected nonblocking sockets. */
228 test_connect_first (void)
230 int s = open_server_socket ();
231 struct sockaddr_in ia;
236 if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
237 failed ("can read, socket not connected");
239 c1 = connect_to_socket (false);
241 if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
242 failed ("expecting readability on passive socket");
243 if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
244 failed ("expecting readability on passive socket");
246 addrlen = sizeof (ia);
247 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
254 /* Test poll(2) for unconnected blocking sockets. */
257 test_accept_first (void)
260 int s = open_server_socket ();
261 struct sockaddr_in ia;
272 addrlen = sizeof (ia);
273 c = accept (s, (struct sockaddr *) &ia, &addrlen);
277 shutdown (c, SHUT_RD);
284 c = connect_to_socket (true);
285 if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
286 failed ("cannot write after blocking connect");
289 if (do_select_wait (c, SEL_IN) != SEL_IN)
290 failed ("cannot read data left in the socket by closed process");
299 /* Common code for pipes and connected sockets. */
302 test_pair (int rd, int wd)
305 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
306 failed ("expecting writability before writing");
307 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
308 failed ("expecting writability before writing");
310 write (wd, "foo", 3);
311 if (do_select_wait (rd, SEL_IN) != SEL_IN)
312 failed ("expecting readability after writing");
313 if (do_select_nowait (rd, SEL_IN) != SEL_IN)
314 failed ("expecting readability after writing");
320 /* Test poll(2) on connected sockets. */
323 test_socket_pair (void)
325 struct sockaddr_in ia;
327 socklen_t addrlen = sizeof (ia);
328 int s = open_server_socket ();
329 int c1 = connect_to_socket (false);
330 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
336 write (c2, "foo", 3);
341 /* Test poll(2) on pipes. */
349 test_pair (fd[0], fd[1]);
363 printf ("Please press Enter\n");
364 test (test_tty, "TTY");
367 result = test (test_connect_first, "Unconnected socket test");
368 result += test (test_socket_pair, "Connected sockets test");
369 result += test (test_accept_first, "General socket test with fork");
370 result += test (test_pipe, "Pipe test");