1 /* Test of <sys/select.h> substitute.
2 Copyright (C) 2007, 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 Bruno Haible <bruno@clisp.org>, 2007. */
18 /* Enhanced by Paolo Bonzini, 2008. */
22 #include <sys/select.h>
24 /* Check that the 'struct timeval' type is defined. */
29 #include <netinet/in.h>
30 #include <arpa/inet.h>
37 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
39 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
45 #define pipe(x) _pipe(x, 256, O_BINARY)
50 #ifdef HAVE_SYS_WAIT_H
55 #define SO_REUSEPORT SO_REUSEADDR
58 #define TEST_PORT 12345
61 /* Minimal testing infrastructure. */
66 failed (const char *reason)
70 printf ("failed (%s)\n", reason);
74 test (void (*fn) (void), const char *msg)
77 printf ("%s... ", msg);
88 /* Funny socket code. */
94 struct sockaddr_in ia;
96 s = socket (AF_INET, SOCK_STREAM, 0);
98 memset (&ia, 0, sizeof (ia));
99 ia.sin_family = AF_INET;
100 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
101 ia.sin_port = htons (TEST_PORT);
102 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
109 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
111 if (listen (s, 1) < 0)
121 connect_to_socket (int blocking)
124 struct sockaddr_in ia;
126 s = socket (AF_INET, SOCK_STREAM, 0);
128 memset (&ia, 0, sizeof (ia));
129 ia.sin_family = AF_INET;
130 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
131 ia.sin_port = htons (TEST_PORT);
136 unsigned long iMode = 1;
137 ioctl (s, FIONBIO, (char *) &iMode);
139 #elif defined F_GETFL
140 int oldflags = fcntl (s, F_GETFL, NULL);
142 if (!(oldflags & O_NONBLOCK))
143 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
147 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
148 && (blocking || errno != EINPROGRESS))
158 /* A slightly more convenient interface to select(2). */
161 do_select (int fd, int ev, struct timeval *tv)
163 fd_set rfds, wfds, xfds;
175 r = select (fd + 1, &rfds, &wfds, &xfds, tv);
180 if (FD_ISSET (fd, &rfds))
182 if (FD_ISSET (fd, &wfds))
184 if (FD_ISSET (fd, &xfds))
187 failed ("select returned 0");
189 failed ("select returned unrequested events");
195 do_select_nowait (int fd, int ev)
197 static struct timeval tv0;
198 return do_select (fd, ev, &tv0);
202 do_select_wait (int fd, int ev)
204 return do_select (fd, ev, NULL);
208 /* Test poll(2) for TTYs. */
214 if (do_select_nowait (0, SEL_IN) != 0)
216 if (do_select_nowait (0, SEL_OUT) == 0)
217 failed ("cannot write");
219 if (do_select_wait (0, SEL_IN) == 0)
220 failed ("return with infinite timeout");
223 if (do_select_nowait (0, SEL_IN) != 0)
224 failed ("can read after getc");
229 /* Test poll(2) for unconnected nonblocking sockets. */
232 test_connect_first (void)
234 int s = open_server_socket ();
235 struct sockaddr_in ia;
240 if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
241 failed ("can read, socket not connected");
243 c1 = connect_to_socket (false);
245 if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
246 failed ("expecting readability on passive socket");
247 if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
248 failed ("expecting readability on passive socket");
250 addrlen = sizeof (ia);
251 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
258 /* Test poll(2) for unconnected blocking sockets. */
261 test_accept_first (void)
264 int s = open_server_socket ();
265 struct sockaddr_in ia;
276 addrlen = sizeof (ia);
277 c = accept (s, (struct sockaddr *) &ia, &addrlen);
281 shutdown (c, SHUT_RD);
288 c = connect_to_socket (true);
289 if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
290 failed ("cannot write after blocking connect");
293 if (do_select_wait (c, SEL_IN) != SEL_IN)
294 failed ("cannot read data left in the socket by closed process");
303 /* Common code for pipes and connected sockets. */
306 test_pair (int rd, int wd)
309 if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
310 failed ("expecting writability before writing");
311 if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
312 failed ("expecting writability before writing");
314 write (wd, "foo", 3);
315 if (do_select_wait (rd, SEL_IN) != SEL_IN)
316 failed ("expecting readability after writing");
317 if (do_select_nowait (rd, SEL_IN) != SEL_IN)
318 failed ("expecting readability after writing");
324 /* Test poll(2) on connected sockets. */
327 test_socket_pair (void)
329 struct sockaddr_in ia;
331 socklen_t addrlen = sizeof (ia);
332 int s = open_server_socket ();
333 int c1 = connect_to_socket (false);
334 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
340 write (c2, "foo", 3);
345 /* Test poll(2) on pipes. */
353 test_pair (fd[0], fd[1]);
366 gl_sockets_startup (SOCKETS_1_1);
369 printf ("Please press Enter\n");
370 test (test_tty, "TTY");
373 result = test (test_connect_first, "Unconnected socket test");
374 result += test (test_socket_pair, "Connected sockets test");
375 result += test (test_accept_first, "General socket test with fork");
376 result += test (test_pipe, "Pipe test");