gnupload: Support option -h as alias of --help.
[gnulib.git] / tests / test-poll.c
blob5dce09e6436d12b8fbeed903e57749e63ad28e81
1 /* Test of poll() function.
2 Copyright (C) 2008-2018 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 <stdbool.h>
35 #include <sys/ioctl.h>
36 #include <errno.h>
38 #include "macros.h"
40 #if defined _WIN32 && ! defined __CYGWIN__
41 # define WINDOWS_NATIVE
42 #endif
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
55 #define TEST_PORT 12345
58 /* Minimal testing infrastructure. */
60 static int failures;
62 static void
63 failed (const char *reason)
65 if (++failures > 1)
66 printf (" ");
67 printf ("failed (%s)\n", reason);
70 static int
71 test (void (*fn) (void), const char *msg)
73 failures = 0;
74 printf ("%s... ", msg);
75 fflush (stdout);
76 fn ();
78 if (!failures)
79 printf ("passed\n");
81 return failures;
85 /* Funny socket code. */
87 static int
88 open_server_socket ()
90 int s, x;
91 struct sockaddr_in ia;
93 s = socket (AF_INET, SOCK_STREAM, 0);
95 x = 1;
96 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
98 memset (&ia, 0, sizeof (ia));
99 ia.sin_family = AF_INET;
100 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
101 ia.sin_port = htons (TEST_PORT);
102 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
104 perror ("bind");
105 exit (77);
108 if (listen (s, 1) < 0)
110 perror ("listen");
111 exit (77);
114 return s;
117 static int
118 connect_to_socket (int blocking)
120 int s;
121 struct sockaddr_in ia;
123 s = socket (AF_INET, SOCK_STREAM, 0);
125 memset (&ia, 0, sizeof (ia));
126 ia.sin_family = AF_INET;
127 inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
128 ia.sin_port = htons (TEST_PORT);
130 if (!blocking)
132 #ifdef WINDOWS_NATIVE
133 unsigned long iMode = 1;
134 ioctl (s, FIONBIO, (char *) &iMode);
136 #elif defined F_GETFL
137 int oldflags = fcntl (s, F_GETFL, NULL);
139 if (!(oldflags & O_NONBLOCK))
140 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
141 #endif
144 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
145 && (blocking || errno != EINPROGRESS))
147 perror ("connect");
148 exit (77);
151 return s;
155 /* A slightly more convenient interface to poll(2). */
157 static int
158 poll1 (int fd, int ev, int time)
160 struct pollfd pfd;
161 int r;
163 pfd.fd = fd;
164 pfd.events = ev;
165 pfd.revents = -1;
166 r = poll (&pfd, 1, time);
167 if (r < 0)
168 return r;
170 if (pfd.revents & ~(POLLHUP | POLLERR | POLLNVAL | ev))
171 failed ("invalid flag combination (unrequested events)");
173 return pfd.revents;
176 static int
177 poll1_nowait (int fd, int ev)
179 return poll1 (fd, ev, 0);
182 static int
183 poll1_wait (int fd, int ev)
185 return poll1 (fd, ev, -1);
189 /* Test poll(2) for TTYs. */
191 #ifdef INTERACTIVE
192 static void
193 test_tty (void)
195 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
196 failed ("can read");
197 if (poll1_nowait (0, POLLOUT) == 0)
198 failed ("cannot write");
200 if (poll1_wait (0, POLLIN | POLLRDNORM) == 0)
201 failed ("return with infinite timeout");
203 getchar ();
204 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
205 failed ("can read after getc");
207 #endif
210 /* Test poll(2) for unconnected nonblocking sockets. */
212 static void
213 test_connect_first (void)
215 int s = open_server_socket ();
216 struct sockaddr_in ia;
217 socklen_t addrlen;
219 int c1, c2;
221 if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
222 failed ("can read, socket not connected");
224 c1 = connect_to_socket (false);
226 if (poll1_wait (s, POLLIN | POLLRDNORM | POLLRDBAND) != (POLLIN | POLLRDNORM))
227 failed ("expecting POLLIN | POLLRDNORM on passive socket");
228 if (poll1_nowait (s, POLLIN | POLLRDBAND) != POLLIN)
229 failed ("expecting POLLIN on passive socket");
230 if (poll1_nowait (s, POLLRDNORM | POLLRDBAND) != POLLRDNORM)
231 failed ("expecting POLLRDNORM on passive socket");
233 addrlen = sizeof (ia);
234 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
235 close (s);
236 close (c1);
237 close (c2);
241 /* Test poll(2) for unconnected blocking sockets. */
243 static void
244 test_accept_first (void)
246 #ifndef WINDOWS_NATIVE
247 int s = open_server_socket ();
248 struct sockaddr_in ia;
249 socklen_t addrlen;
250 char buf[3];
251 int c, pid;
253 pid = fork ();
254 if (pid < 0)
255 return;
257 if (pid == 0)
259 addrlen = sizeof (ia);
260 c = accept (s, (struct sockaddr *) &ia, &addrlen);
261 ASSERT (c >= 0);
262 close (s);
263 ASSERT (write (c, "foo", 3) == 3);
264 ASSERT (read (c, buf, 3) == 3);
265 shutdown (c, SHUT_RD);
266 close (c);
267 exit (0);
269 else
271 close (s);
272 c = connect_to_socket (true);
273 ASSERT (c >= 0);
274 if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
275 != (POLLOUT | POLLWRNORM))
276 failed ("cannot write after blocking connect");
277 ASSERT (write (c, "foo", 3) == 3);
278 wait (&pid);
279 if (poll1_wait (c, POLLIN) != POLLIN)
280 failed ("cannot read data left in the socket by closed process");
281 ASSERT (read (c, buf, 3) == 3);
282 ASSERT (write (c, "foo", 3) == 3);
283 if ((poll1_wait (c, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
284 failed ("expecting POLLHUP after shutdown");
285 close (c);
287 #endif
291 /* Common code for pipes and connected sockets. */
293 static void
294 test_pair (int rd, int wd)
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");
306 ASSERT (write (wd, "foo", 3) == 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");
314 ASSERT (read (rd, buf, 3) == 3);
318 /* Test poll(2) on connected sockets. */
320 static void
321 test_socket_pair (void)
323 struct sockaddr_in ia;
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 ASSERT (s >= 0);
330 ASSERT (c1 >= 0);
331 ASSERT (c2 >= 0);
333 close (s);
335 test_pair (c1, c2);
336 close (c1);
337 ASSERT (write (c2, "foo", 3) == 3);
338 if ((poll1_nowait (c2, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
339 failed ("expecting POLLHUP after shutdown");
341 close (c2);
345 /* Test poll(2) on pipes. */
347 static void
348 test_pipe (void)
350 int fd[2];
352 ASSERT (pipe (fd) >= 0);
353 test_pair (fd[0], fd[1]);
354 close (fd[0]);
355 if ((poll1_wait (fd[1], POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
356 failed ("expecting POLLHUP after shutdown");
358 close (fd[1]);
362 /* Do them all. */
365 main ()
367 int result;
369 #ifdef INTERACTIVE
370 printf ("Please press Enter\n");
371 test (test_tty, "TTY");
372 #endif
374 result = test (test_connect_first, "Unconnected socket test");
375 result += test (test_socket_pair, "Connected sockets test");
376 result += test (test_accept_first, "General socket test with fork");
377 result += test (test_pipe, "Pipe test");
379 exit (result);