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>
38 #ifdef HAVE_SYS_WAIT_H
45 #define SO_REUSEPORT SO_REUSEADDR
48 #define TEST_PORT 12345
51 /* Minimal testing infrastructure. */
56 failed (const char *reason)
60 printf ("failed (%s)\n", reason);
64 test (void (*fn) (void), const char *msg)
67 printf ("%s... ", msg);
78 /* Funny socket code. */
84 struct sockaddr_in ia;
86 s = socket (AF_INET, SOCK_STREAM, 0);
88 memset (&ia, 0, sizeof (ia));
89 ia.sin_family = AF_INET;
90 inet_aton ("127.0.0.1", &ia.sin_addr);
91 ia.sin_port = htons (TEST_PORT);
92 if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
99 setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
101 if (listen (s, 1) < 0)
111 connect_to_socket (int blocking)
114 struct sockaddr_in ia;
116 s = socket (AF_INET, SOCK_STREAM, 0);
118 memset (&ia, 0, sizeof (ia));
119 ia.sin_family = AF_INET;
120 inet_aton ("127.0.0.1", &ia.sin_addr);
121 ia.sin_port = htons (TEST_PORT);
126 unsigned long iMode = 1;
127 ioctl (s, FIONBIO, &iMode);
129 #elif defined F_GETFL
130 int oldflags = fcntl (s, F_GETFL, NULL);
132 if (!(oldflags & O_NONBLOCK))
133 fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
137 if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
139 if (errno != EINPROGRESS)
146 failed ("huh, connect succeeded?");
152 /* A slightly more convenient interface to poll(2). */
155 poll1 (int fd, int ev, int time)
163 r = poll (&pfd, 1, time);
167 if (pfd.revents & ~(POLLHUP | POLLERR | POLLNVAL | ev))
168 failed ("invalid flag combination (unrequested events)");
174 poll1_nowait (int fd, int ev)
176 return poll1 (fd, ev, 0);
180 poll1_wait (int fd, int ev)
182 return poll1 (fd, ev, -1);
186 /* Test poll(2) for TTYs. */
192 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
194 if (poll1_nowait (0, POLLOUT) == 0)
195 failed ("cannot write");
197 if (poll1_wait (0, POLLIN | POLLRDNORM) == 0)
198 failed ("return with infinite timeout");
201 if (poll1_nowait (0, POLLIN | POLLRDNORM) != 0)
202 failed ("can read after getc");
207 /* Test poll(2) for unconnected nonblocking sockets. */
210 test_connect_first (void)
212 int s = open_server_socket ();
213 struct sockaddr_in ia;
218 if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
219 failed ("can read, socket not connected");
221 c1 = connect_to_socket (FALSE);
223 if (poll1_wait (s, POLLIN | POLLRDNORM | POLLRDBAND) != (POLLIN | POLLRDNORM))
224 failed ("expecting POLLIN | POLLRDNORM on passive socket");
225 if (poll1_nowait (s, POLLIN | POLLRDBAND) != POLLIN)
226 failed ("expecting POLLIN on passive socket");
227 if (poll1_nowait (s, POLLRDNORM | POLLRDBAND) != POLLRDNORM)
228 failed ("expecting POLLRDNORM on passive socket");
230 addrlen = sizeof (ia);
231 c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
238 /* Test poll(2) for unconnected blocking sockets. */
241 test_accept_first (void)
244 int s = open_server_socket ();
245 struct sockaddr_in ia;
256 addrlen = sizeof (ia);
257 c = accept (s, (struct sockaddr *) &ia, &addrlen);
261 shutdown (c, SHUT_RD);
268 c = connect_to_socket (TRUE);
269 if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
270 != (POLLOUT | POLLWRNORM))
271 failed ("cannot write after blocking connect");
274 if (poll1_wait (c, POLLIN) != POLLIN)
275 failed ("cannot read data left in the socket by closed process");
278 if ((poll1_wait (c, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
279 failed ("expecting POLLHUP after shutdown");
286 /* Common code for pipes and connected sockets. */
289 test_pair (int rd, int wd)
292 if (poll1_wait (wd, POLLIN | POLLRDNORM | POLLOUT | POLLWRNORM | POLLRDBAND)
293 != (POLLOUT | POLLWRNORM))
294 failed ("expecting POLLOUT | POLLWRNORM before writing");
295 if (poll1_nowait (wd, POLLIN | POLLRDNORM | POLLOUT | POLLRDBAND) != POLLOUT)
296 failed ("expecting POLLOUT before writing");
297 if (poll1_nowait (wd, POLLIN | POLLRDNORM | POLLWRNORM | POLLRDBAND)
299 failed ("expecting POLLWRNORM before writing");
301 write (wd, "foo", 3);
302 if (poll1_wait (rd, POLLIN | POLLRDNORM) != (POLLIN | POLLRDNORM))
303 failed ("expecting POLLIN | POLLRDNORM after writing");
304 if (poll1_nowait (rd, POLLIN) != POLLIN)
305 failed ("expecting POLLIN after writing");
306 if (poll1_nowait (rd, POLLRDNORM) != POLLRDNORM)
307 failed ("expecting POLLRDNORM after writing");
313 /* Test poll(2) on connected sockets. */
316 test_socket_pair (void)
318 struct sockaddr_in ia;
320 socklen_t addrlen = sizeof (ia);
321 int s = open_server_socket ();
322 int c1 = connect_to_socket (FALSE);
323 int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
329 write (c2, "foo", 3);
330 if ((poll1_nowait (c2, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
331 failed ("expecting POLLHUP after shutdown");
337 /* Test poll(2) on pipes. */
345 test_pair (fd[0], fd[1]);
347 if ((poll1_wait (fd[1], POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
348 failed ("expecting POLLHUP after shutdown");
362 printf ("Please press Enter\n");
363 test (test_tty, "TTY");
366 result = test (test_connect_first, "Unconnected socket test");
367 result += test (test_socket_pair, "Connected sockets test");
368 result += test (test_accept_first, "General socket test with fork");
369 result += test (test_pipe, "Pipe test");