bc5c1a9365bda6b5aabd0f202bbcc7109b3b259b
[gnulib.git] / tests / test-select.c
1 /* Test of select() substitute.
2    Copyright (C) 2008-2010 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 <config.h>
20
21 #include <sys/select.h>
22
23 #include "signature.h"
24 SIGNATURE_CHECK (select, int, (int, fd_set *, fd_set *, fd_set *,
25                                struct timeval *));
26 /* The following may be macros without underlying functions, so only
27    check signature if they are not macros.  */
28 #ifndef FD_CLR
29 SIGNATURE_CHECK (FD_CLR, void, (int, fd_set *));
30 #endif
31 #ifndef FD_ISSET
32 SIGNATURE_CHECK (FD_ISSET, void, (int, fd_set *));
33 #endif
34 #ifndef FD_SET
35 SIGNATURE_CHECK (FD_SET, int, (int, fd_set *));
36 #endif
37 #ifndef FD_ZERO
38 SIGNATURE_CHECK (FD_ZERO, void, (fd_set *));
39 #endif
40
41 #include <stdio.h>
42 #include <string.h>
43 #include <netinet/in.h>
44 #include <arpa/inet.h>
45 #include <unistd.h>
46 #include <fcntl.h>
47 #include <stdlib.h>
48 #include <stdbool.h>
49 #include <sys/ioctl.h>
50 #include <errno.h>
51
52 #include "macros.h"
53
54 enum { SEL_IN = 1, SEL_OUT = 2, SEL_EXC = 4 };
55
56 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
57 # define WIN32_NATIVE
58 #endif
59
60 #ifdef HAVE_SYS_WAIT_H
61 # include <sys/wait.h>
62 #endif
63
64 #ifndef SO_REUSEPORT
65 # define SO_REUSEPORT    SO_REUSEADDR
66 #endif
67
68 #define TEST_PORT       12345
69
70
71 /* Minimal testing infrastructure.  */
72
73 static int failures;
74
75 static void
76 failed (const char *reason)
77 {
78   if (++failures > 1)
79     printf ("  ");
80   printf ("failed (%s)\n", reason);
81 }
82
83 static int
84 test (void (*fn) (void), const char *msg)
85 {
86   failures = 0;
87   printf ("%s... ", msg);
88   fflush (stdout);
89   fn ();
90
91   if (!failures)
92     printf ("passed\n");
93
94   return failures;
95 }
96
97
98 /* Funny socket code.  */
99
100 static int
101 open_server_socket (void)
102 {
103   int s, x;
104   struct sockaddr_in ia;
105
106   s = socket (AF_INET, SOCK_STREAM, 0);
107
108   memset (&ia, 0, sizeof (ia));
109   ia.sin_family = AF_INET;
110   inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
111   ia.sin_port = htons (TEST_PORT);
112   if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
113     {
114       perror ("bind");
115       exit (77);
116     }
117
118   x = 1;
119   setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
120
121   if (listen (s, 1) < 0)
122     {
123       perror ("listen");
124       exit (77);
125     }
126
127   return s;
128 }
129
130 static int
131 connect_to_socket (int blocking)
132 {
133   int s;
134   struct sockaddr_in ia;
135
136   s = socket (AF_INET, SOCK_STREAM, 0);
137
138   memset (&ia, 0, sizeof (ia));
139   ia.sin_family = AF_INET;
140   inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
141   ia.sin_port = htons (TEST_PORT);
142
143   if (!blocking)
144     {
145 #ifdef WIN32_NATIVE
146       unsigned long iMode = 1;
147       ioctl (s, FIONBIO, (char *) &iMode);
148
149 #elif defined F_GETFL
150       int oldflags = fcntl (s, F_GETFL, NULL);
151
152       if (!(oldflags & O_NONBLOCK))
153         fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
154 #endif
155     }
156
157   if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
158       && (blocking || errno != EINPROGRESS))
159     {
160       perror ("connect");
161       exit (77);
162     }
163
164   return s;
165 }
166
167
168 /* A slightly more convenient interface to select(2).  */
169
170 static int
171 do_select (int fd, int ev, struct timeval *tv)
172 {
173   fd_set rfds, wfds, xfds;
174   int r, rev;
175
176   FD_ZERO (&rfds);
177   FD_ZERO (&wfds);
178   FD_ZERO (&xfds);
179   if (ev & SEL_IN)
180     FD_SET (fd, &rfds);
181   if (ev & SEL_OUT)
182     FD_SET (fd, &wfds);
183   if (ev & SEL_EXC)
184     FD_SET (fd, &xfds);
185   r = select (fd + 1, &rfds, &wfds, &xfds, tv);
186   if (r < 0)
187     return r;
188
189   rev = 0;
190   if (FD_ISSET (fd, &rfds))
191     rev |= SEL_IN;
192   if (FD_ISSET (fd, &wfds))
193     rev |= SEL_OUT;
194   if (FD_ISSET (fd, &xfds))
195     rev |= SEL_EXC;
196   if (rev && r == 0)
197     failed ("select returned 0");
198   if (rev & ~ev)
199     failed ("select returned unrequested events");
200
201   return rev;
202 }
203
204 static int
205 do_select_nowait (int fd, int ev)
206 {
207   static struct timeval tv0;
208   return do_select (fd, ev, &tv0);
209 }
210
211 static int
212 do_select_wait (int fd, int ev)
213 {
214   return do_select (fd, ev, NULL);
215 }
216
217
218 /* Test poll(2) for TTYs.  */
219
220 #ifdef INTERACTIVE
221 static void
222 test_tty (void)
223 {
224   if (do_select_nowait (0, SEL_IN) != 0)
225     failed ("can read");
226   if (do_select_nowait (0, SEL_OUT) == 0)
227     failed ("cannot write");
228
229   if (do_select_wait (0, SEL_IN) == 0)
230     failed ("return with infinite timeout");
231
232   getchar ();
233   if (do_select_nowait (0, SEL_IN) != 0)
234     failed ("can read after getc");
235 }
236 #endif
237
238
239 /* Test poll(2) for unconnected nonblocking sockets.  */
240
241 static void
242 test_connect_first (void)
243 {
244   int s = open_server_socket ();
245   struct sockaddr_in ia;
246   socklen_t addrlen;
247
248   int c1, c2;
249
250   if (do_select_nowait (s, SEL_IN | SEL_EXC) != 0)
251     failed ("can read, socket not connected");
252
253   c1 = connect_to_socket (false);
254
255   if (do_select_wait (s, SEL_IN | SEL_EXC) != SEL_IN)
256     failed ("expecting readability on passive socket");
257   if (do_select_nowait (s, SEL_IN | SEL_EXC) != SEL_IN)
258     failed ("expecting readability on passive socket");
259
260   addrlen = sizeof (ia);
261   c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
262   ASSERT (close (s) == 0);
263   ASSERT (close (c1) == 0);
264   ASSERT (close (c2) == 0);
265 }
266
267
268 /* Test poll(2) for unconnected blocking sockets.  */
269
270 static void
271 test_accept_first (void)
272 {
273 #ifndef WIN32_NATIVE
274   int s = open_server_socket ();
275   struct sockaddr_in ia;
276   socklen_t addrlen;
277   char buf[3];
278   int c, pid;
279
280   pid = fork ();
281   if (pid < 0)
282     return;
283
284   if (pid == 0)
285     {
286       addrlen = sizeof (ia);
287       c = accept (s, (struct sockaddr *) &ia, &addrlen);
288       ASSERT (close (s) == 0);
289       ASSERT (write (c, "foo", 3) == 3);
290       ASSERT (read (c, buf, 3) == 3);
291       shutdown (c, SHUT_RD);
292       ASSERT (close (c) == 0);
293       exit (0);
294     }
295   else
296     {
297       ASSERT (close (s) == 0);
298       c = connect_to_socket (true);
299       if (do_select_nowait (c, SEL_OUT) != SEL_OUT)
300         failed ("cannot write after blocking connect");
301       ASSERT (write (c, "foo", 3) == 3);
302       wait (&pid);
303       if (do_select_wait (c, SEL_IN) != SEL_IN)
304         failed ("cannot read data left in the socket by closed process");
305       ASSERT (read (c, buf, 3) == 3);
306       ASSERT (write (c, "foo", 3) == 3);
307       (void) close (c); /* may fail with errno = ECONNRESET */
308     }
309 #endif
310 }
311
312
313 /* Common code for pipes and connected sockets.  */
314
315 static void
316 test_pair (int rd, int wd)
317 {
318   char buf[3];
319   if (do_select_wait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
320     failed ("expecting writability before writing");
321   if (do_select_nowait (wd, SEL_IN | SEL_OUT | SEL_EXC) != SEL_OUT)
322     failed ("expecting writability before writing");
323
324   ASSERT (write (wd, "foo", 3) == 3);
325   if (do_select_wait (rd, SEL_IN) != SEL_IN)
326     failed ("expecting readability after writing");
327   if (do_select_nowait (rd, SEL_IN) != SEL_IN)
328     failed ("expecting readability after writing");
329
330   ASSERT (read (rd, buf, 3) == 3);
331 }
332
333
334 /* Test poll(2) on connected sockets.  */
335
336 static void
337 test_socket_pair (void)
338 {
339   struct sockaddr_in ia;
340
341   socklen_t addrlen = sizeof (ia);
342   int s = open_server_socket ();
343   int c1 = connect_to_socket (false);
344   int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
345
346   ASSERT (close (s) == 0);
347
348   test_pair (c1, c2);
349   ASSERT (close (c1) == 0);
350   ASSERT (write (c2, "foo", 3) == 3);
351   (void) close (c2); /* may fail with errno = ECONNRESET */
352 }
353
354
355 /* Test poll(2) on pipes.  */
356
357 static void
358 test_pipe (void)
359 {
360   int fd[2];
361
362   ASSERT (pipe (fd) == 0);
363   test_pair (fd[0], fd[1]);
364   ASSERT (close (fd[0]) == 0);
365   ASSERT (close (fd[1]) == 0);
366 }
367
368
369 /* Do them all.  */
370
371 int
372 main (void)
373 {
374   int result;
375
376 #ifdef INTERACTIVE
377   printf ("Please press Enter\n");
378   test (test_tty, "TTY");
379 #endif
380
381   result = test (test_connect_first, "Unconnected socket test");
382   result += test (test_socket_pair, "Connected sockets test");
383   result += test (test_accept_first, "General socket test with fork");
384   result += test (test_pipe, "Pipe test");
385
386   exit (result);
387 }