strtoumax: fix typo in previous commit.
[gnulib.git] / tests / test-poll.c
index cacb3a2..35c38fe 100644 (file)
@@ -1,5 +1,5 @@
 /* Test of poll() function.
-   Copyright (C) 2008 Free Software Foundation, Inc.
+   Copyright (C) 2008-2013 Free Software Foundation, Inc.
 
    This program is free software; you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    GNU General Public License for more details.
 
    You should have received a copy of the GNU General Public License
-   along with this program; if not, write to the Free Software Foundation,
-   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
+   along with this program; if not, see <http://www.gnu.org/licenses/>.  */
 
 /* Written by Paolo Bonzini.  */
 
 #include <config.h>
 
+/* Specification.  */
+#include <poll.h>
+
+#include "signature.h"
+SIGNATURE_CHECK (poll, int, (struct pollfd[], nfds_t, int));
+
 #include <stdio.h>
 #include <string.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 #include <arpa/inet.h>
-#include <poll.h>
 #include <fcntl.h>
 #include <stdlib.h>
+#include <stdbool.h>
+#include <sys/ioctl.h>
 #include <errno.h>
 
-#ifdef HAVE_IO_H
+#include "macros.h"
+
+#if (defined _WIN32 || defined __WIN32__) && ! defined __CYGWIN__
+# define WINDOWS_NATIVE
+#endif
+
+#ifdef WINDOWS_NATIVE
 #include <io.h>
+#define pipe(x) _pipe(x, 256, O_BINARY)
 #endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
 #include <sys/wait.h>
 #endif
 
-enum { FALSE, TRUE };
-
 #ifndef SO_REUSEPORT
 #define SO_REUSEPORT    SO_REUSEADDR
 #endif
 
-#define TEST_PORT      12345
+#define TEST_PORT       12345
 
 
 /* Minimal testing infrastructure.  */
@@ -85,9 +96,12 @@ open_server_socket ()
 
   s = socket (AF_INET, SOCK_STREAM, 0);
 
+  x = 1;
+  setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
+
   memset (&ia, 0, sizeof (ia));
   ia.sin_family = AF_INET;
-  inet_aton ("127.0.0.1", &ia.sin_addr);
+  inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
   ia.sin_port = htons (TEST_PORT);
   if (bind (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
     {
@@ -95,9 +109,6 @@ open_server_socket ()
       exit (77);
     }
 
-  x = 1;
-  setsockopt (s, SOL_SOCKET, SO_REUSEPORT, &x, sizeof (x));
-
   if (listen (s, 1) < 0)
     {
       perror ("listen");
@@ -117,33 +128,29 @@ connect_to_socket (int blocking)
 
   memset (&ia, 0, sizeof (ia));
   ia.sin_family = AF_INET;
-  inet_aton ("127.0.0.1", &ia.sin_addr);
+  inet_pton (AF_INET, "127.0.0.1", &ia.sin_addr);
   ia.sin_port = htons (TEST_PORT);
 
   if (!blocking)
     {
-#ifdef __MSVCRT__
+#ifdef WINDOWS_NATIVE
       unsigned long iMode = 1;
-      ioctl (s, FIONBIO, &iMode);
+      ioctl (s, FIONBIO, (char *) &iMode);
+
 #elif defined F_GETFL
       int oldflags = fcntl (s, F_GETFL, NULL);
+
       if (!(oldflags & O_NONBLOCK))
         fcntl (s, F_SETFL, oldflags | O_NONBLOCK);
 #endif
     }
 
-  if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0)
+  if (connect (s, (struct sockaddr *) &ia, sizeof (ia)) < 0
+      && (blocking || errno != EINPROGRESS))
     {
-      if (errno != EINPROGRESS)
-       {
-         perror ("connect");
-         exit (77);
-       }
+      perror ("connect");
+      exit (77);
     }
-  else if (!blocking)
-    failed ("huh, connect succeeded?");
 
   return s;
 }
@@ -160,7 +167,7 @@ poll1 (int fd, int ev, int time)
   pfd.fd = fd;
   pfd.events = ev;
   pfd.revents = 0;
-  r = poll (&pfd, 1, time);  
+  r = poll (&pfd, 1, time);
   if (r < 0)
     return r;
 
@@ -218,7 +225,7 @@ test_connect_first (void)
   if (poll1_nowait (s, POLLIN | POLLRDNORM | POLLRDBAND) != 0)
     failed ("can read, socket not connected");
 
-  c1 = connect_to_socket (FALSE);
+  c1 = connect_to_socket (false);
 
   if (poll1_wait (s, POLLIN | POLLRDNORM | POLLRDBAND) != (POLLIN | POLLRDNORM))
     failed ("expecting POLLIN | POLLRDNORM on passive socket");
@@ -240,7 +247,7 @@ test_connect_first (void)
 static void
 test_accept_first (void)
 {
-#ifndef __MSVCRT__
+#ifndef WINDOWS_NATIVE
   int s = open_server_socket ();
   struct sockaddr_in ia;
   socklen_t addrlen;
@@ -255,9 +262,10 @@ test_accept_first (void)
     {
       addrlen = sizeof (ia);
       c = accept (s, (struct sockaddr *) &ia, &addrlen);
+      ASSERT (c >= 0);
       close (s);
-      write (c, "foo", 3);
-      read (c, buf, 3);
+      ASSERT (write (c, "foo", 3) == 3);
+      ASSERT (read (c, buf, 3) == 3);
       shutdown (c, SHUT_RD);
       close (c);
       exit (0);
@@ -265,16 +273,17 @@ test_accept_first (void)
   else
     {
       close (s);
-      c = connect_to_socket (TRUE);
+      c = connect_to_socket (true);
+      ASSERT (c >= 0);
       if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
-         != (POLLOUT | POLLWRNORM))
+          != (POLLOUT | POLLWRNORM))
         failed ("cannot write after blocking connect");
-      write (c, "foo", 3);
+      ASSERT (write (c, "foo", 3) == 3);
       wait (&pid);
       if (poll1_wait (c, POLLIN) != POLLIN)
         failed ("cannot read data left in the socket by closed process");
-      read (c, buf, 3);
-      write (c, "foo", 3);
+      ASSERT (read (c, buf, 3) == 3);
+      ASSERT (write (c, "foo", 3) == 3);
       if ((poll1_wait (c, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
         failed ("expecting POLLHUP after shutdown");
       close (c);
@@ -298,7 +307,7 @@ test_pair (int rd, int wd)
       != POLLWRNORM)
     failed ("expecting POLLWRNORM before writing");
 
-  write (wd, "foo", 3);
+  ASSERT (write (wd, "foo", 3) == 3);
   if (poll1_wait (rd, POLLIN | POLLRDNORM) != (POLLIN | POLLRDNORM))
     failed ("expecting POLLIN | POLLRDNORM after writing");
   if (poll1_nowait (rd, POLLIN) != POLLIN)
@@ -306,7 +315,7 @@ test_pair (int rd, int wd)
   if (poll1_nowait (rd, POLLRDNORM) != POLLRDNORM)
     failed ("expecting POLLRDNORM after writing");
 
-  read (rd, buf, 3);
+  ASSERT (read (rd, buf, 3) == 3);
 }
 
 
@@ -319,14 +328,17 @@ test_socket_pair (void)
 
   socklen_t addrlen = sizeof (ia);
   int s = open_server_socket ();
-  int c1 = connect_to_socket (FALSE);
+  int c1 = connect_to_socket (false);
   int c2 = accept (s, (struct sockaddr *) &ia, &addrlen);
+  ASSERT (s >= 0);
+  ASSERT (c1 >= 0);
+  ASSERT (c2 >= 0);
 
   close (s);
 
   test_pair (c1, c2);
   close (c1);
-  write (c2, "foo", 3);
+  ASSERT (write (c2, "foo", 3) == 3);
   if ((poll1_nowait (c2, POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)
     failed ("expecting POLLHUP after shutdown");
 
@@ -341,7 +353,7 @@ test_pipe (void)
 {
   int fd[2];
 
-  pipe (fd);
+  ASSERT (pipe (fd) >= 0);
   test_pair (fd[0], fd[1]);
   close (fd[0]);
   if ((poll1_wait (fd[1], POLLIN | POLLOUT) & (POLLHUP | POLLERR)) == 0)