Change the test for native Windows.
[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     {
144       if (errno != EINPROGRESS)
145         {
146           perror ("connect");
147           exit (77);
148         }
149     }
150   else if (!blocking)
151     failed ("huh, connect succeeded?");
152
153   return s;
154 }
155
156
157 /* A slightly more convenient interface to poll(2).  */
158
159 static int
160 poll1 (int fd, int ev, int time)
161 {
162   struct pollfd pfd;
163   int r;
164
165   pfd.fd = fd;
166   pfd.events = ev;
167   pfd.revents = 0;
168   r = poll (&pfd, 1, time);  
169   if (r < 0)
170     return r;
171
172   if (pfd.revents & ~(POLLHUP | POLLERR | POLLNVAL | ev))
173     failed ("invalid flag combination (unrequested events)");
174
175   return pfd.revents;
176 }
177
178 static int
179 poll1_nowait (int fd, int ev)
180 {
181   return poll1 (fd, ev, 0);
182 }
183
184 static int
185 poll1_wait (int fd, int ev)
186 {
187   return poll1 (fd, ev, -1);
188 }
189
190
191 /* Test poll(2) for TTYs.  */
192
193 #ifdef INTERACTIVE
194 static void
195 test_tty (void)
196 {
197   if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
198     failed ("can read");
199   if (poll1_nowait (0, POLLOUT) == 0)
200     failed ("cannot write");
201
202   if (poll1_wait (0, POLLIN | POLLRDNORM) == 0)
203     failed ("return with infinite timeout");
204
205   getchar ();
206   if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
207     failed ("can read after getc");
208 }
209 #endif
210
211
212 /* Test poll(2) for unconnected nonblocking sockets.  */
213
214 static void
215 test_connect_first (void)
216 {
217   int s = open_server_socket ();
218   struct sockaddr_in ia;
219   socklen_t addrlen;
220
221   int c1, c2;
222
223   if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
224     failed ("can read, socket not connected");
225
226   c1 = connect_to_socket (false);
227
228   if (poll1_wait (s, POLLIN | POLLRDNORM | POLLRDBAND) != (POLLIN | POLLRDNORM))
229     failed ("expecting POLLIN | POLLRDNORM on passive socket");
230   if (poll1_nowait (s, POLLIN | POLLRDBAND) != POLLIN)
231     failed ("expecting POLLIN on passive socket");
232   if (poll1_nowait (s, POLLRDNORM | POLLRDBAND) != POLLRDNORM)
233     failed ("expecting POLLRDNORM on passive socket");
234
235   addrlen = sizeof (ia);
236   c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
237   close (s);
238   close (c1);
239   close (c2);
240 }
241
242
243 /* Test poll(2) for unconnected blocking sockets.  */
244
245 static void
246 test_accept_first (void)
247 {
248 #ifndef WIN32_NATIVE
249   int s = open_server_socket ();
250   struct sockaddr_in ia;
251   socklen_t addrlen;
252   char buf[3];
253   int c, pid;
254
255   pid = fork ();
256   if (pid < 0)
257     return;
258
259   if (pid == 0)
260     {
261       addrlen = sizeof (ia);
262       c = accept (s, (struct sockaddr *) &ia, &addrlen);
263       close (s);
264       write (c, "foo", 3);
265       read (c, buf, 3);
266       shutdown (c, SHUT_RD);
267       close (c);
268       exit (0);
269     }
270   else
271     {
272       close (s);
273       c = connect_to_socket (true);
274       if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
275           != (POLLOUT | POLLWRNORM))
276         failed ("cannot write after blocking connect");
277       write (c, "foo", 3);
278       wait (&pid);
279       if (poll1_wait (c, POLLIN) != POLLIN)
280         failed ("cannot read data left in the socket by closed process");
281       read (c, buf, 3);
282       write (c, "foo", 3);
283       if ((poll1_wait (c, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
284         failed ("expecting POLLHUP after shutdown");
285       close (c);
286     }
287 #endif
288 }
289
290
291 /* Common code for pipes and connected sockets.  */
292
293 static void
294 test_pair (int rd, int wd)
295 {
296   char buf[3];
297   if (poll1_wait (wd, POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLRDBAND)
298       != (POLLOUT | POLLWRNORM))
299     failed ("expecting POLLOUT | POLLWRNORM before writing");
300   if (poll1_nowait (wd, POLLIN | POLLRDNORM | POLLOUT | POLLRDBAND) != POLLOUT)
301     failed ("expecting POLLOUT before writing");
302   if (poll1_nowait (wd, POLLIN | POLLRDNORM | POLLWRNORM | POLLRDBAND)
303       != POLLWRNORM)
304     failed ("expecting POLLWRNORM before writing");
305
306   write (wd, "foo", 3);
307   if (poll1_wait (rd, POLLIN | POLLRDNORM) != (POLLIN | POLLRDNORM))
308     failed ("expecting POLLIN | POLLRDNORM after writing");
309   if (poll1_nowait (rd, POLLIN) != POLLIN)
310     failed ("expecting POLLIN after writing");
311   if (poll1_nowait (rd, POLLRDNORM) != POLLRDNORM)
312     failed ("expecting POLLRDNORM after writing");
313
314   read (rd, buf, 3);
315 }
316
317
318 /* Test poll(2) on connected sockets.  */
319
320 static void
321 test_socket_pair (void)
322 {
323   struct sockaddr_in ia;
324
325   socklen_t addrlen = sizeof (ia);
326   int s = open_server_socket ();
327   int c1 = connect_to_socket (false);
328   int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
329
330   close (s);
331
332   test_pair (c1, c2);
333   close (c1);
334   write (c2, "foo", 3);
335   if ((poll1_nowait (c2, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
336     failed ("expecting POLLHUP after shutdown");
337
338   close (c2);
339 }
340
341
342 /* Test poll(2) on pipes.  */
343
344 static void
345 test_pipe (void)
346 {
347   int fd[2];
348
349   pipe (fd);
350   test_pair (fd[0], fd[1]);
351   close (fd[0]);
352   if ((poll1_wait (fd[1], POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
353     failed ("expecting POLLHUP after shutdown");
354
355   close (fd[1]);
356 }
357
358
359 /* Do them all.  */
360
361 int
362 main ()
363 {
364   int result;
365
366   gl_sockets_startup (SOCKETS_2_0);
367
368 #ifdef INTERACTIVE
369   printf ("Please press Enter\n");
370   test (test_tty, "TTY");
371 #endif
372
373   result = test (test_connect_first, "Unconnected socket test");
374   result += test (test_socket_pair, "Connected sockets test");
375   result += test (test_accept_first, "General socket test with fork");
376   result += test (test_pipe, "Pipe test");
377
378   exit (result);
379 }