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.
This commit is contained in:
Руслан Ижбулатов 2019-02-11 23:45:53 +00:00
parent f150de2139
commit c00724d5c9

View File

@ -1602,7 +1602,7 @@ test_get_available (gconstpointer user_data)
GError *err = NULL; GError *err = NULL;
GSocket *listener, *server, *client; GSocket *listener, *server, *client;
GInetAddress *addr; GInetAddress *addr;
GSocketAddress *saddr; GSocketAddress *saddr, *boundaddr;
gchar data[] = "0123456789abcdef"; gchar data[] = "0123456789abcdef";
gchar buf[34]; gchar buf[34];
gssize nread; gssize nread;
@ -1635,9 +1635,14 @@ test_get_available (gconstpointer user_data)
g_object_unref (saddr); g_object_unref (saddr);
g_object_unref (addr); 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_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) if (socket_type == G_SOCKET_TYPE_STREAM)
{ {
g_socket_listen (listener, &err); g_socket_listen (listener, &err);
@ -1799,7 +1804,7 @@ test_read_write (gconstpointer user_data)
GError *err = NULL; GError *err = NULL;
GSocket *listener, *server, *client; GSocket *listener, *server, *client;
GInetAddress *addr; GInetAddress *addr;
GSocketAddress *saddr; GSocketAddress *saddr, *boundaddr;
TestReadWriteData data; TestReadWriteData data;
guint8 data_write[1024], data_read[1024]; guint8 data_write[1024], data_read[1024];
GSocketConnection *server_stream, *client_stream; GSocketConnection *server_stream, *client_stream;
@ -1828,11 +1833,17 @@ test_read_write (gconstpointer user_data)
g_object_unref (saddr); g_object_unref (saddr);
g_object_unref (addr); 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_assert_no_error (err);
g_socket_listen (listener, &err); g_socket_listen (listener, &err);
g_assert_no_error (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_socket_connect (client, saddr, NULL, &err);
g_assert_no_error (err); g_assert_no_error (err);