From b03b6bfb8daa20dceff4593229f0df2c68c37e6e Mon Sep 17 00:00:00 2001
From: Paolo Bonzini <bonzini@gnu.org>
Date: Fri, 12 Sep 2008 16:12:44 +0200
Subject: [PATCH] port poll tests to Windows

2008-09-12  Paolo Bonzini  <bonzini@gnu.org>

	* lib/sys_socket.in.h (EINPROGRESS): Define for Winsock case.
	* modules/poll-tests: Add inet_pton, stdbool, sockets.
	* tests/test-poll.c: Use them.  Use _pipe on Windows.
---
 ChangeLog           |  6 ++++++
 lib/sys_socket.in.h |  1 +
 modules/poll-tests  |  3 +++
 tests/test-poll.c   | 19 +++++++++++--------
 4 files changed, 21 insertions(+), 8 deletions(-)

diff --git a/ChangeLog b/ChangeLog
index 4575ca9d42..ac15d94e55 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,3 +1,9 @@
+2008-09-12  Paolo Bonzini  <bonzini@gnu.org>
+
+	* lib/sys_socket.in.h (EINPROGRESS): Define for Winsock case.
+	* modules/poll-tests: Add inet_pton, stdbool, sockets.
+	* tests/test-poll.c: Use them.  Use _pipe on Windows.
+
 2008-09-12  Paolo Bonzini  <bonzini@gnu.org>
 
 	* modules/poll-tests: New.
diff --git a/lib/sys_socket.in.h b/lib/sys_socket.in.h
index d237e9c457..7c8ad30a08 100644
--- a/lib/sys_socket.in.h
+++ b/lib/sys_socket.in.h
@@ -95,6 +95,7 @@
 # endif
 
 # if defined _WIN32 || defined __WIN32__
+#  define EINPROGRESS		  WSAEINPROGRESS
 #  define ENOTSOCK                WSAENOTSOCK
 #  define EADDRINUSE              WSAEADDRINUSE
 #  define ENETRESET               WSAENETRESET
diff --git a/modules/poll-tests b/modules/poll-tests
index 1484aaa363..7c44a4883e 100644
--- a/modules/poll-tests
+++ b/modules/poll-tests
@@ -2,10 +2,13 @@ Files:
 tests/test-poll.c
 
 Depends-on:
+stdbool
 sys_socket
 netinet_in
 arpa_inet
 extensions
+inet_pton
+sockets
 
 configure.ac:
 AC_CHECK_HEADERS_ONCE([io.h unistd.h sys/wait.h])
diff --git a/tests/test-poll.c b/tests/test-poll.c
index cacb3a238f..0dbcb54fbe 100644
--- a/tests/test-poll.c
+++ b/tests/test-poll.c
@@ -27,10 +27,13 @@
 #include <poll.h>
 #include <fcntl.h>
 #include <stdlib.h>
+#include <stdbool.h>
 #include <errno.h>
+#include "sockets.h"
 
 #ifdef HAVE_IO_H
 #include <io.h>
+#define pipe(x) _pipe(x, 256, O_BINARY)
 #endif
 #ifdef HAVE_UNISTD_H
 #include <unistd.h>
@@ -39,8 +42,6 @@
 #include <sys/wait.h>
 #endif
 
-enum { FALSE, TRUE };
-
 #ifndef SO_REUSEPORT
 #define SO_REUSEPORT    SO_REUSEADDR
 #endif
@@ -87,7 +88,7 @@ open_server_socket ()
 
   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)
     {
@@ -117,14 +118,14 @@ 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__
       unsigned long iMode = 1;
-      ioctl (s, FIONBIO, &iMode);
+      ioctlsocket (s, FIONBIO, (void *) &iMode);
  
 #elif defined F_GETFL
       int oldflags = fcntl (s, F_GETFL, NULL);
@@ -218,7 +219,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");
@@ -265,7 +266,7 @@ test_accept_first (void)
   else
     {
       close (s);
-      c = connect_to_socket (TRUE);
+      c = connect_to_socket (true);
       if (poll1_nowait (c, POLLOUT | POLLWRNORM | POLLRDBAND)
 	  != (POLLOUT | POLLWRNORM))
         failed ("cannot write after blocking connect");
@@ -319,7 +320,7 @@ 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);
 
   close (s);
@@ -358,6 +359,8 @@ main ()
 {
   int result;
 
+  gl_sockets_startup (SOCKETS_2_0);
+
 #ifdef INTERACTIVE
   printf ("Please press Enter\n");
   test (test_tty, "TTY");
-- 
2.30.2