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