maint: update copyright
[gnulib.git] / tests / test-select.h
1 /* Test of select() substitute.
2    Copyright (C) 2008-2014 Free Software Foundation, Inc.
3
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.
8
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.
13
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/>.  */
16
17 /* Written by Paolo Bonzini, 2008.  */
18
19 #include <stdio.h>
20 #include <string.h>
21 #include <netinet/in.h>
22 #include <arpa/inet.h>
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <stdlib.h>
26 #include <stdbool.h>
27 #include <sys/ioctl.h>
28 #include <errno.h>
29
30 #include "macros.h"
31
32 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
33 # define WINDOWS_NATIVE
34 #endif
35
36 #ifdef HAVE_SYS_WAIT_H
37 # include <sys/wait.h>
38 #endif
39
40 #ifndef SO_REUSEPORT
41 # define SO_REUSEPORT    SO_REUSEADDR
42 #endif
43
44 #define TEST_PORT       12345
45
46
47 typedef int (*select_fn) (int, fd_set *, fd_set *, fd_set *, struct timeval *);
48
49
50 /* Minimal testing infrastructure.  */
51
52 static int failures;
53
54 static void
55 failed (const char *reason)
56 {
57   if (++failures > 1)
58     printf ("  ");
59   printf ("failed (%s)\n", reason);
60 }
61
62 static int
63 test (void (*fn) (select_fn), select_fn my_select, const char *msg)
64 {
65   failures = 0;
66   printf ("%s... ", msg);
67   fflush (stdout);
68   fn (my_select);
69
70   if (!failures)
71     printf ("passed\n");
72
73   return failures;
74 }
75
76
77 /* Funny socket code.  */
78
79 static int
80 open_server_socket (void)
81 {
82   int s, x;
83   struct sockaddr_in ia;
84
85   s = socket (AF_INET, SOCK_STREAM, 0);
86
87   x = 1;
88   setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
89
90   memset (&ia, 0, sizeof (ia));
91   ia.sin_family = AF_INET;
92   inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
93   ia.sin_port = htons (TEST_PORT);
94   if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
95     {
96       perror ("bind");
97       exit (77);
98     }
99
100   if (listen (s, 1) < 0)
101     {
102       perror ("listen");
103       exit (77);
104     }
105
106   return s;
107 }
108
109 static int
110 connect_to_socket (bool blocking)
111 {
112   int s;
113   struct sockaddr_in ia;
114
115   s = socket (AF_INET, SOCK_STREAM, 0);
116
117   memset (&ia, 0, sizeof (ia));
118   ia.sin_family = AF_INET;
119   inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
120   ia.sin_port = htons (TEST_PORT);
121
122   if (!blocking)
123     {
124 #ifdef WINDOWS_NATIVE
125       unsigned long iMode = 1;
126       ioctl (s, FIONBIO, (char *) &iMode);
127
128 #elif defined F_GETFL
129       int oldflags = fcntl (s, F_GETFL, NULL);
130
131       if (!(oldflags & O_NONBLOCK))
132         fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
133 #endif
134     }
135
136   if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
137       && (blocking || errno != EINPROGRESS))
138     {
139       perror ("connect");
140       exit (77);
141     }
142
143   return s;
144 }
145
146
147 /* A slightly more convenient interface to select(2).
148    Waits until a specific event occurs on a file descriptor FD.
149    EV is a bit mask of events to look for:
150      SEL_IN - input can be polled without blocking,
151      SEL_OUT - output can be provided without blocking,
152      SEL_EXC - an exception occurred,
153    A maximum wait time is specified by TIMEOUT.
154    *TIMEOUT = { 0, 0 } means to return immediately,
155    TIMEOUT = NULL means to wait indefinitely.  */
156
157 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
158
159 static int
160 do_select (int fd, int ev, struct timeval *timeout, select_fn my_select)
161 {
162   fd_set rfds, wfds, xfds;
163   int r, rev;
164
165   FD_ZERO (&rfds);
166   FD_ZERO (&wfds);
167   FD_ZERO (&xfds);
168   if (ev & SEL_IN)
169     FD_SET (fd, &rfds);
170   if (ev & SEL_OUT)
171     FD_SET (fd, &wfds);
172   if (ev & SEL_EXC)
173     FD_SET (fd, &xfds);
174   r = my_select (fd + 1, &rfds, &wfds, &xfds, timeout);
175   if (r < 0)
176     return r;
177
178   rev = 0;
179   if (FD_ISSET (fd, &rfds))
180     rev |= SEL_IN;
181   if (FD_ISSET (fd, &wfds))
182     rev |= SEL_OUT;
183   if (FD_ISSET (fd, &xfds))
184     rev |= SEL_EXC;
185   if (rev && r == 0)
186     failed ("select returned 0");
187   if (rev & ~ev)
188     failed ("select returned unrequested events");
189
190   return rev;
191 }
192
193 static int
194 do_select_nowait (int fd, int ev, select_fn my_select)
195 {
196   struct timeval tv0;
197   tv0.tv_sec = 0;
198   tv0.tv_usec = 0;
199   return do_select (fd, ev, &tv0, my_select);
200 }
201
202 static int
203 do_select_wait (int fd, int ev, select_fn my_select)
204 {
205   return do_select (fd, ev, NULL, my_select);
206 }
207
208
209 /* Test select(2) for TTYs.  */
210
211 #ifdef INTERACTIVE
212 static void
213 test_tty (select_fn my_select)
214 {
215   if (do_select_nowait (0, SEL_IN, my_select) != 0)
216     failed ("can read");
217   if (do_select_nowait (0, SEL_OUT, my_select) == 0)
218     failed ("cannot write");
219
220   if (do_select_wait (0, SEL_IN, my_select) == 0)
221     failed ("return with infinite timeout");
222
223   getchar ();
224   if (do_select_nowait (0, SEL_IN, my_select) != 0)
225     failed ("can read after getc");
226 }
227 #endif
228
229
230 static int
231 do_select_bad_nfd_nowait (int nfd, select_fn my_select)
232 {
233   struct timeval tv0;
234   tv0.tv_sec = 0;
235   tv0.tv_usec = 0;
236   errno = 0;
237   return my_select (nfd, NULL, NULL, NULL, &tv0);
238 }
239
240 static void
241 test_bad_nfd (select_fn my_select)
242 {
243   if (do_select_bad_nfd_nowait (-1, my_select) != -1 || errno != EINVAL)
244     failed ("invalid errno after negative nfds");
245   /* Can't test FD_SETSIZE + 1 for EINVAL, since some systems allow
246      dynamically larger set size by redefining FD_SETSIZE anywhere up
247      to the actual maximum fd.  */
248   /* if (do_select_bad_nfd_nowait (FD_SETSIZE + 1, my_select) != -1 */
249   /*     || errno != EINVAL) */
250   /*   failed ("invalid errno after bogus nfds"); */
251 }
252
253 /* Test select(2) on invalid file descriptors.  */
254
255 static int
256 do_select_bad_fd (int fd, int ev, struct timeval *timeout, select_fn my_select)
257 {
258   fd_set rfds, wfds, xfds;
259
260   FD_ZERO (&rfds);
261   FD_ZERO (&wfds);
262   FD_ZERO (&xfds);
263   if (ev & SEL_IN)
264     FD_SET (fd, &rfds);
265   if (ev & SEL_OUT)
266     FD_SET (fd, &wfds);
267   if (ev & SEL_EXC)
268     FD_SET (fd, &xfds);
269   errno = 0;
270   return my_select (fd + 1, &rfds, &wfds, &xfds, timeout);
271   /* In this case, when fd is invalid, on some platforms, the bit for fd
272      is left alone in the fd_set, whereas on other platforms it is cleared.
273      So, don't check the bit for fd here.  */
274 }
275
276 static int
277 do_select_bad_fd_nowait (int fd, int ev, select_fn my_select)
278 {
279   struct timeval tv0;
280   tv0.tv_sec = 0;
281   tv0.tv_usec = 0;
282   return do_select_bad_fd (fd, ev, &tv0, my_select);
283 }
284
285 static void
286 test_bad_fd (select_fn my_select)
287 {
288   /* This tests fails on OSF/1 and native Windows, even with fd = 16.  */
289 #if !(defined __osf__ || defined WINDOWS_NATIVE)
290   int fd;
291
292   /* On Linux, Mac OS X, *BSD, values of fd like 99 or 399 are discarded
293      by the kernel early and therefore do *not* lead to EBADF, as required
294      by POSIX.  */
295 # if defined __linux__ || (defined __APPLE__ && defined __MACH__) || defined __FreeBSD__ || defined __OpenBSD__ || defined __NetBSD__
296   fd = 16;
297 # else
298   fd = 99;
299 # endif
300   close (fd);
301
302   if (do_select_bad_fd_nowait (fd, SEL_IN, my_select) == 0 || errno != EBADF)
303     failed ("invalid fd among rfds");
304   if (do_select_bad_fd_nowait (fd, SEL_OUT, my_select) == 0 || errno != EBADF)
305     failed ("invalid fd among wfds");
306   if (do_select_bad_fd_nowait (fd, SEL_EXC, my_select) == 0 || errno != EBADF)
307     failed ("invalid fd among xfds");
308 #endif
309 }
310
311
312 /* Test select(2) for unconnected nonblocking sockets.  */
313
314 static void
315 test_connect_first (select_fn my_select)
316 {
317   int s = open_server_socket ();
318   struct sockaddr_in ia;
319   socklen_t addrlen;
320
321   int c1, c2;
322
323   if (do_select_nowait (s, SEL_IN | SEL_EXC, my_select) != 0)
324     failed ("can read, socket not connected");
325
326   c1 = connect_to_socket (false);
327
328   if (do_select_wait (s, SEL_IN | SEL_EXC, my_select) != SEL_IN)
329     failed ("expecting readability on passive socket");
330   if (do_select_nowait (s, SEL_IN | SEL_EXC, my_select) != SEL_IN)
331     failed ("expecting readability on passive socket");
332
333   addrlen = sizeof (ia);
334   c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
335   ASSERT (close (s) == 0);
336   ASSERT (close (c1) == 0);
337   ASSERT (close (c2) == 0);
338 }
339
340
341 /* Test select(2) for unconnected blocking sockets.  */
342
343 static void
344 test_accept_first (select_fn my_select)
345 {
346 #ifndef WINDOWS_NATIVE
347   int s = open_server_socket ();
348   struct sockaddr_in ia;
349   socklen_t addrlen;
350   char buf[3];
351   int c, pid;
352
353   pid = fork ();
354   if (pid < 0)
355     return;
356
357   if (pid == 0)
358     {
359       addrlen = sizeof (ia);
360       c = accept (s, (struct sockaddr *) &ia, &addrlen);
361       ASSERT (close (s) == 0);
362       ASSERT (write (c, "foo", 3) == 3);
363       ASSERT (read (c, buf, 3) == 3);
364       shutdown (c, SHUT_RD);
365       ASSERT (close (c) == 0);
366       exit (0);
367     }
368   else
369     {
370       ASSERT (close (s) == 0);
371       c = connect_to_socket (true);
372       if (do_select_nowait (c, SEL_OUT, my_select) != SEL_OUT)
373         failed ("cannot write after blocking connect");
374       ASSERT (write (c, "foo", 3) == 3);
375       wait (&pid);
376       if (do_select_wait (c, SEL_IN, my_select) != SEL_IN)
377         failed ("cannot read data left in the socket by closed process");
378       ASSERT (read (c, buf, 3) == 3);
379       ASSERT (write (c, "foo", 3) == 3);
380       (void) close (c); /* may fail with errno = ECONNRESET */
381     }
382 #endif
383 }
384
385
386 /* Common code for pipes and connected sockets.  */
387
388 static void
389 test_pair (int rd, int wd, select_fn my_select)
390 {
391   char buf[3];
392   if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC, my_select) != SEL_OUT)
393     failed ("expecting writability before writing");
394   if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC, my_select) != SEL_OUT)
395     failed ("expecting writability before writing");
396
397   ASSERT (write (wd, "foo", 3) == 3);
398   if (do_select_wait (rd, SEL_IN, my_select) != SEL_IN)
399     failed ("expecting readability after writing");
400   if (do_select_nowait (rd, SEL_IN, my_select) != SEL_IN)
401     failed ("expecting readability after writing");
402
403   ASSERT (read (rd, buf, 3) == 3);
404 }
405
406
407 /* Test select(2) on connected sockets.  */
408
409 static void
410 test_socket_pair (select_fn my_select)
411 {
412   struct sockaddr_in ia;
413
414   socklen_t addrlen = sizeof (ia);
415   int s = open_server_socket ();
416   int c1 = connect_to_socket (false);
417   int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
418
419   ASSERT (close (s) == 0);
420
421   test_pair (c1, c2, my_select);
422   ASSERT (close (c1) == 0);
423   ASSERT (write (c2, "foo", 3) == 3);
424   (void) close (c2); /* may fail with errno = ECONNRESET */
425 }
426
427
428 /* Test select(2) on pipes.  */
429
430 static void
431 test_pipe (select_fn my_select)
432 {
433   int fd[2];
434
435   ASSERT (pipe (fd) == 0);
436   test_pair (fd[0], fd[1], my_select);
437   ASSERT (close (fd[0]) == 0);
438   ASSERT (close (fd[1]) == 0);
439 }
440
441
442 /* Do them all.  */
443
444 static int
445 test_function (select_fn my_select)
446 {
447   int result = 0;
448
449 #ifdef INTERACTIVE
450   printf ("Please press Enter\n");
451   test (test_tty, "TTY", my_select);
452 #endif
453
454   result += test (test_bad_nfd, my_select, "Invalid nfd test");
455   result += test (test_bad_fd, my_select, "Invalid fd test");
456   result += test (test_connect_first, my_select, "Unconnected socket test");
457   result += test (test_socket_pair, my_select, "Connected sockets test");
458   result += test (test_accept_first, my_select, "General socket test with fork");
459   result += test (test_pipe, my_select, "Pipe test");
460
461   return result;
462 }