1 /* Test of poll() function.
2 Copyright (C) 2008 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)
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, write to the Free Software Foundation,
16 Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
18 /* Written by Paolo Bonzini. */
24 #include <sys/socket.h>
25 #include <netinet/in.h>
26 #include <arpa/inet.h>
34 #if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
40 #define pipe(x) _pipe(x, 256, O_BINARY)
45 #ifdef HAVE_SYS_WAIT_H
50 #define SO_REUSEPORT SO_REUSEADDR
53 #define TEST_PORT 12345
56 /* Minimal testing infrastructure. */
61 failed (const char *reason)
65 printf ("failed (%s)\n", reason);
69 test (void (*fn) (void), const char *msg)
72 printf ("%s... ", msg);
83 /* Funny socket code. */
89 struct sockaddr_in ia;
91 s = socket (AF_INET, SOCK_STREAM, 0);
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)
104 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
106 if (listen (s, 1) < 0)
116 connect_to_socket (int blocking)
119 struct sockaddr_in ia;
121 s = socket (AF_INET, SOCK_STREAM, 0);
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);
131 unsigned long iMode = 1;
132 ioctlsocket (s, FIONBIO, (void *) &iMode);
134 #elif defined F_GETFL
135 int oldflags = fcntl (s, F_GETFL, NULL);
137 if (!(oldflags & O_NONBLOCK))
138 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
142 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
144 if (errno != EINPROGRESS)
151 failed ("huh, connect succeeded?");
157 /* A slightly more convenient interface to poll(2). */
160 poll1 (int fd, int ev, int time)
168 r = poll (&pfd, 1, time);
172 if (pfd.revents & ~(POLLHUP | POLLERR | POLLNVAL | ev))
173 failed ("invalid flag combination (unrequested events)");
179 poll1_nowait (int fd, int ev)
181 return poll1 (fd, ev, 0);
185 poll1_wait (int fd, int ev)
187 return poll1 (fd, ev, -1);
191 /* Test poll(2) for TTYs. */
197 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
199 if (poll1_nowait (0, POLLOUT) == 0)
200 failed ("cannot write");
202 if (poll1_wait (0, POLLIN | POLLRDNORM) == 0)
203 failed ("return with infinite timeout");
206 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
207 failed ("can read after getc");
212 /* Test poll(2) for unconnected nonblocking sockets. */
215 test_connect_first (void)
217 int s = open_server_socket ();
218 struct sockaddr_in ia;
223 if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
224 failed ("can read, socket not connected");
226 c1 = connect_to_socket (false);
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");
235 addrlen = sizeof (ia);
236 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
243 /* Test poll(2) for unconnected blocking sockets. */
246 test_accept_first (void)
249 int s = open_server_socket ();
250 struct sockaddr_in ia;
261 addrlen = sizeof (ia);
262 c = accept (s, (struct sockaddr *) &ia, &addrlen);
266 shutdown (c, SHUT_RD);
273 c = connect_to_socket (true);
274 if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
275 != (POLLOUT | POLLWRNORM))
276 failed ("cannot write after blocking connect");
279 if (poll1_wait (c, POLLIN) != POLLIN)
280 failed ("cannot read data left in the socket by closed process");
283 if ((poll1_wait (c, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
284 failed ("expecting POLLHUP after shutdown");
291 /* Common code for pipes and connected sockets. */
294 test_pair (int rd, int wd)
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)
304 failed ("expecting POLLWRNORM before writing");
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");
318 /* Test poll(2) on connected sockets. */
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);
334 write (c2, "foo", 3);
335 if ((poll1_nowait (c2, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
336 failed ("expecting POLLHUP after shutdown");
342 /* Test poll(2) on pipes. */
350 test_pair (fd[0], fd[1]);
352 if ((poll1_wait (fd[1], POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
353 failed ("expecting POLLHUP after shutdown");
366 gl_sockets_startup (SOCKETS_2_0);
369 printf ("Please press Enter\n");
370 test (test_tty, "TTY");
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");