From 1f5d7eeaa7ed8b3016f9dc946b6e530e4fcef0fe Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD=20=D0=98=D0=B6=D0=B1?= =?UTF-8?q?=D1=83=D0=BB=D0=B0=D1=82=D0=BE=D0=B2?= Date: Mon, 11 Feb 2019 23:43:14 +0000 Subject: [PATCH 1/3] socket test: Adjust for Windows-specific sendto() error With winsock sending messages to NULL results in G_IO_ERROR_NOT_CONNECTED instead of G_IO_ERROR_FAILED. MSDN says: WSAENOTCONN 10057 Socket is not connected. A request to send or receive data was disallowed because the socket is not connected and (when sending on a datagram socket using sendto) no address was supplied. So this is a direct mapping of the implementation error. Covering it up in the wrapper (by converting it to G_IO_ERROR_FAILED) doesn't seem feasible or needed (no one, except for the testsuite, really cares which unrecoverable error is returned by sendto()). --- gio/tests/socket.c | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/gio/tests/socket.c b/gio/tests/socket.c index 8410a3e84..064202d11 100644 --- a/gio/tests/socket.c +++ b/gio/tests/socket.c @@ -757,7 +757,12 @@ test_ip_sync_dgram (GSocketFamily family) m[1].address = NULL; m[2].address = NULL; len = g_socket_send_messages (client, m, G_N_ELEMENTS (m), 0, NULL, &error); + /* This error code may vary between platforms and over time; it is not guaranteed API: */ +#ifndef G_OS_WIN32 g_assert_error (error, G_IO_ERROR, G_IO_ERROR_FAILED); +#else + g_assert_error (error, G_IO_ERROR, G_IO_ERROR_NOT_CONNECTED); +#endif g_clear_error (&error); g_assert_cmpint (len, ==, -1); From f150de213989639da3f8cfd7d5760ab4060df410 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD=20=D0=98=D0=B6=D0=B1?= =?UTF-8?q?=D1=83=D0=BB=D0=B0=D1=82=D0=BE=D0=B2?= Date: Mon, 11 Feb 2019 23:44:55 +0000 Subject: [PATCH 2/3] socket test: Bind Windows UDP sockets before calling recv() Winsock can't recv() on unbound UDP sockets. Bind the socket to loopback before trying to receive data on it. --- gio/tests/socket.c | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/gio/tests/socket.c b/gio/tests/socket.c index 064202d11..a3f3c43cb 100644 --- a/gio/tests/socket.c +++ b/gio/tests/socket.c @@ -829,6 +829,10 @@ test_ip_sync_dgram_timeouts (GSocketFamily family) GCancellable *cancellable = NULL; GThread *cancellable_thread = NULL; gssize len; +#ifdef G_OS_WIN32 + GInetAddress *iaddr; + GSocketAddress *addr; +#endif client = g_socket_new (family, G_SOCKET_TYPE_DATAGRAM, @@ -840,6 +844,16 @@ test_ip_sync_dgram_timeouts (GSocketFamily family) g_assert_cmpint (g_socket_get_socket_type (client), ==, G_SOCKET_TYPE_DATAGRAM); g_assert_cmpint (g_socket_get_protocol (client), ==, G_SOCKET_PROTOCOL_DEFAULT); +#ifdef G_OS_WIN32 + /* Winsock can't recv() on unbound udp socket */ + iaddr = g_inet_address_new_loopback (family); + addr = g_inet_socket_address_new (iaddr, 0); + g_object_unref (iaddr); + g_socket_bind (client, addr, TRUE, &error); + g_object_unref (addr); + g_assert_no_error (error); +#endif + /* No overall timeout: test the per-operation timeouts instead. */ g_socket_set_timeout (client, 0); From c00724d5c92b3fe8a628dcdab3770cf0330d2b9f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=D0=A0=D1=83=D1=81=D0=BB=D0=B0=D0=BD=20=D0=98=D0=B6=D0=B1?= =?UTF-8?q?=D1=83=D0=BB=D0=B0=D1=82=D0=BE=D0=B2?= Date: Mon, 11 Feb 2019 23:45:53 +0000 Subject: [PATCH 3/3] socket test: Use loopback for connecting, not 0.0.0.0 getsockname() returns the address that the socket was bound to. If it was bound to INADDR_ANY, getsockname() will stubbornly return INADDR_ANY (and someport - that one is valid). Subsequent connection attempts to INADDR_ANY:someport will fail with winsock. Actually, it doesn't make even sense to connect to INADDR_ANY at all (where is the socket connecting to? To a random interface of the host?), so this is just a straight-up change, without platform-specific ifdefing. Use loopback instead of INADDR_ANY. To ensure that binding and creation of INADDR_ANY is still tested, use two addresses: bind to INADDR_ANY, but connect to loopback, with the port number that we got from the bound address. --- gio/tests/socket.c | 19 +++++++++++++++---- 1 file changed, 15 insertions(+), 4 deletions(-) diff --git a/gio/tests/socket.c b/gio/tests/socket.c index a3f3c43cb..eeebddd62 100644 --- a/gio/tests/socket.c +++ b/gio/tests/socket.c @@ -1602,7 +1602,7 @@ test_get_available (gconstpointer user_data) GError *err = NULL; GSocket *listener, *server, *client; GInetAddress *addr; - GSocketAddress *saddr; + GSocketAddress *saddr, *boundaddr; gchar data[] = "0123456789abcdef"; gchar buf[34]; gssize nread; @@ -1635,9 +1635,14 @@ test_get_available (gconstpointer user_data) g_object_unref (saddr); g_object_unref (addr); - saddr = g_socket_get_local_address (listener, &err); + boundaddr = g_socket_get_local_address (listener, &err); g_assert_no_error (err); + addr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4); + saddr = g_inet_socket_address_new (addr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (boundaddr))); + g_object_unref (addr); + g_object_unref (boundaddr); + if (socket_type == G_SOCKET_TYPE_STREAM) { g_socket_listen (listener, &err); @@ -1799,7 +1804,7 @@ test_read_write (gconstpointer user_data) GError *err = NULL; GSocket *listener, *server, *client; GInetAddress *addr; - GSocketAddress *saddr; + GSocketAddress *saddr, *boundaddr; TestReadWriteData data; guint8 data_write[1024], data_read[1024]; GSocketConnection *server_stream, *client_stream; @@ -1828,11 +1833,17 @@ test_read_write (gconstpointer user_data) g_object_unref (saddr); g_object_unref (addr); - saddr = g_socket_get_local_address (listener, &err); + boundaddr = g_socket_get_local_address (listener, &err); g_assert_no_error (err); g_socket_listen (listener, &err); g_assert_no_error (err); + + addr = g_inet_address_new_loopback (G_SOCKET_FAMILY_IPV4); + saddr = g_inet_socket_address_new (addr, g_inet_socket_address_get_port (G_INET_SOCKET_ADDRESS (boundaddr))); + g_object_unref (addr); + g_object_unref (boundaddr); + g_socket_connect (client, saddr, NULL, &err); g_assert_no_error (err);