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