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