usleep tests: Avoid failure due to known Cygwin 3.5.3 bug.
[gnulib.git] / tests / test-poll.c
blob58cd87cfb5ed208d5d1ba56c6b11780f45dd6f23
1 /* Test of poll() function.
2 Copyright (C) 2008-2024 Free Software Foundation, Inc.
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.
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.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, see <https://www.gnu.org/licenses/>. */
17 /* Written by Paolo Bonzini. */
19 #include <config.h>
21 /* Specification. */
22 #include <poll.h>
24 #include "signature.h"
25 SIGNATURE_CHECK (poll, int, (struct pollfd[], nfds_t, int));
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 <sys/ioctl.h>
35 #include <errno.h>
37 #include "macros.h"
39 #if defined _WIN32 && ! defined __CYGWIN__
40 # define WINDOWS_NATIVE
41 #endif
43 #ifdef WINDOWS_NATIVE
44 #include <io.h>
45 #define pipe(x) _pipe(x, 256, O_BINARY)
46 #endif
47 #ifdef HAVE_UNISTD_H
48 #include <unistd.h>
49 #endif
50 #ifdef HAVE_SYS_WAIT_H
51 #include <sys/wait.h>
52 #endif
54 #define TEST_PORT 12345
57 /* Minimal testing infrastructure. */
59 static int failures;
61 static void
62 failed (const char *reason)
64 if (++failures > 1)
65 printf (" ");
66 printf ("failed (%s)\n", reason);
69 static int
70 test (void (*fn) (void), const char *msg)
72 failures = 0;
73 printf ("%s... ", msg);
74 fflush (stdout);
75 fn ();
77 if (!failures)
78 printf ("passed\n");
80 return failures;
84 /* Funny socket code. */
86 static int
87 open_server_socket ()
89 int s, x;
90 struct sockaddr_in ia;
92 s = socket (AF_INET, SOCK_STREAM, 0);
94 x = 1;
95 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
97 memset (&ia, 0, sizeof (ia));
98 ia.sin_family = AF_INET;
99 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
100 ia.sin_port = htons (TEST_PORT);
101 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
103 perror ("bind");
104 exit (77);
107 if (listen (s, 1) < 0)
109 perror ("listen");
110 exit (77);
113 return s;
116 static int
117 connect_to_socket (int blocking)
119 int s;
120 struct sockaddr_in ia;
122 s = socket (AF_INET, SOCK_STREAM, 0);
124 memset (&ia, 0, sizeof (ia));
125 ia.sin_family = AF_INET;
126 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
127 ia.sin_port = htons (TEST_PORT);
129 if (!blocking)
131 #ifdef WINDOWS_NATIVE
132 unsigned long iMode = 1;
133 ioctl (s, FIONBIO, (char *) &iMode);
135 #elif defined F_GETFL
136 int oldflags = fcntl (s, F_GETFL, NULL);
138 if (!(oldflags & O_NONBLOCK))
139 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
140 #endif
143 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
144 && (blocking || errno != EINPROGRESS))
146 perror ("connect");
147 exit (77);
150 return s;
154 /* A slightly more convenient interface to poll(2). */
156 static int
157 poll1 (int fd, int ev, int time)
159 struct pollfd pfd;
160 int r;
162 pfd.fd = fd;
163 pfd.events = ev;
164 pfd.revents = -1;
165 r = poll (&pfd, 1, time);
166 if (r < 0)
167 return r;
169 if (pfd.revents & ~(POLLHUP | POLLERR | POLLNVAL | ev))
170 failed ("invalid flag combination (unrequested events)");
172 return pfd.revents;
175 static int
176 poll1_nowait (int fd, int ev)
178 return poll1 (fd, ev, 0);
181 static int
182 poll1_wait (int fd, int ev)
184 return poll1 (fd, ev, -1);
188 /* Test poll(2) for TTYs. */
190 #ifdef INTERACTIVE
191 static void
192 test_tty (void)
194 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
195 failed ("can read");
196 if (poll1_nowait (0, POLLOUT) == 0)
197 failed ("cannot write");
199 if (poll1_wait (0, POLLIN | POLLRDNORM) == 0)
200 failed ("return with infinite timeout");
202 getchar ();
203 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
204 failed ("can read after getc");
206 #endif
209 /* Test poll(2) for unconnected nonblocking sockets. */
211 static void
212 test_connect_first (void)
214 int s = open_server_socket ();
215 struct sockaddr_in ia;
216 socklen_t addrlen;
218 int c1, c2;
220 if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
221 failed ("can read, socket not connected");
223 c1 = connect_to_socket (false);
225 if (poll1_wait (s, POLLIN | POLLRDNORM | POLLRDBAND) != (POLLIN | POLLRDNORM))
226 failed ("expecting POLLIN | POLLRDNORM on passive socket");
227 if (poll1_nowait (s, POLLIN | POLLRDBAND) != POLLIN)
228 failed ("expecting POLLIN on passive socket");
229 if (poll1_nowait (s, POLLRDNORM | POLLRDBAND) != POLLRDNORM)
230 failed ("expecting POLLRDNORM on passive socket");
232 addrlen = sizeof (ia);
233 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
234 close (s);
235 close (c1);
236 close (c2);
240 /* Test poll(2) for unconnected blocking sockets. */
242 static void
243 test_accept_first (void)
245 #ifndef WINDOWS_NATIVE
246 int s = open_server_socket ();
247 struct sockaddr_in ia;
248 socklen_t addrlen;
249 char buf[3];
250 int c, pid;
252 pid = fork ();
253 if (pid < 0)
254 return;
256 if (pid == 0)
258 addrlen = sizeof (ia);
259 c = accept (s, (struct sockaddr *) &ia, &addrlen);
260 ASSERT (c >= 0);
261 close (s);
262 ASSERT (write (c, "foo", 3) == 3);
263 ASSERT (read (c, buf, 3) == 3);
264 shutdown (c, SHUT_RD);
265 close (c);
266 exit (0);
268 else
270 close (s);
271 c = connect_to_socket (true);
272 ASSERT (c >= 0);
273 if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
274 != (POLLOUT | POLLWRNORM))
275 failed ("cannot write after blocking connect");
276 ASSERT (write (c, "foo", 3) == 3);
277 wait (&pid);
278 if (poll1_wait (c, POLLIN) != POLLIN)
279 failed ("cannot read data left in the socket by closed process");
280 ASSERT (read (c, buf, 3) == 3);
281 ASSERT (write (c, "foo", 3) == 3);
282 int revents = poll1_wait (c, POLLIN | POLLOUT);
283 # ifdef __linux__
284 if ((revents & (POLLHUP | POLLERR)) == 0)
285 failed ("expecting POLLHUP after shutdown");
286 # else
287 (void) revents;
288 # endif
289 close (c);
291 #endif
295 /* Common code for pipes and connected sockets. */
297 static void
298 test_pair (int rd, int wd)
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");
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");
318 ASSERT (read (rd, buf, 3) == 3);
322 /* Test poll(2) on connected sockets. */
324 static void
325 test_socket_pair (void)
327 struct sockaddr_in ia;
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);
337 close (s);
339 test_pair (c1, c2);
340 close (c1);
341 ASSERT (write (c2, "foo", 3) == 3);
342 int revents = poll1_nowait (c2, POLLIN | POLLOUT);
343 #ifdef __linux__
344 if ((revents & (POLLHUP | POLLERR)) == 0)
345 failed ("expecting POLLHUP after shutdown");
346 #else
347 (void) revents;
348 #endif
350 close (c2);
354 /* Test poll(2) on pipes. */
356 static void
357 test_pipe (void)
359 int fd[2];
361 ASSERT (pipe (fd) >= 0);
362 test_pair (fd[0], fd[1]);
363 close (fd[0]);
364 int revents = poll1_wait (fd[1], POLLIN | POLLOUT);
365 #if !(defined _AIX || defined __CYGWIN__ || (defined _WIN32 && !defined __CYGWIN__))
366 if ((revents & (POLLHUP | POLLERR)) == 0)
367 failed ("expecting POLLHUP after shutdown");
368 #else
369 (void) revents;
370 #endif
372 close (fd[1]);
376 /* Do them all. */
379 main ()
381 int result;
383 #ifdef INTERACTIVE
384 printf ("Please press Enter\n");
385 test (test_tty, "TTY");
386 #endif
388 result = test (test_connect_first, "Unconnected socket test");
389 result += test (test_socket_pair, "Connected sockets test");
390 result += test (test_accept_first, "General socket test with fork");
391 result += test (test_pipe, "Pipe test");
393 return (result ? result : test_exit_status);