mirror of
https://gitlab.gnome.org/GNOME/glib.git
synced 2025-08-01 15:03:39 +02:00
gsocketconnectable: Add a to_string() virtual method
Add string serialisation functions for GNetworkAddress, GSocketAddress, GUnixSocketAddress, GInetSocketAddress, GNetworkService and GSocketConnectable. These are intended for use in debug output, not for serialisation in network or disc protocols. They are implemented as a new virtual method on GSocketConnectable: g_socket_connectable_to_string(). GInetSocketAddress and GUnixSocketAddress now implement GSocketConnectable directly to implement to_string(). Previously they implemented it via their abstract parent class, GSocketAddress. https://bugzilla.gnome.org/show_bug.cgi?id=737116
This commit is contained in:
@@ -235,6 +235,55 @@ test_socket_address (void)
|
||||
g_object_unref (saddr);
|
||||
}
|
||||
|
||||
static void
|
||||
test_socket_address_to_string (void)
|
||||
{
|
||||
GSocketAddress *sa = NULL;
|
||||
GInetAddress *ia = NULL;
|
||||
gchar *str = NULL;
|
||||
|
||||
/* IPv4. */
|
||||
ia = g_inet_address_new_from_string ("123.1.123.1");
|
||||
sa = g_inet_socket_address_new (ia, 80);
|
||||
str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
|
||||
g_assert_cmpstr (str, ==, "123.1.123.1:80");
|
||||
g_free (str);
|
||||
g_object_unref (sa);
|
||||
g_object_unref (ia);
|
||||
|
||||
/* IPv6. */
|
||||
ia = g_inet_address_new_from_string ("::80");
|
||||
sa = g_inet_socket_address_new (ia, 80);
|
||||
str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
|
||||
g_assert_cmpstr (str, ==, "[::80]:80");
|
||||
g_free (str);
|
||||
g_object_unref (sa);
|
||||
g_object_unref (ia);
|
||||
|
||||
/* IPv6 without port. */
|
||||
ia = g_inet_address_new_from_string ("::80");
|
||||
sa = g_inet_socket_address_new (ia, 0);
|
||||
str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
|
||||
g_assert_cmpstr (str, ==, "::80");
|
||||
g_free (str);
|
||||
g_object_unref (sa);
|
||||
g_object_unref (ia);
|
||||
|
||||
/* IPv6 with scope. */
|
||||
ia = g_inet_address_new_from_string ("::1");
|
||||
sa = G_SOCKET_ADDRESS (g_object_new (G_TYPE_INET_SOCKET_ADDRESS,
|
||||
"address", ia,
|
||||
"port", 123,
|
||||
"flowinfo", 10,
|
||||
"scope-id", 25,
|
||||
NULL));
|
||||
str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (sa));
|
||||
g_assert_cmpstr (str, ==, "[::1%25]:123");
|
||||
g_free (str);
|
||||
g_object_unref (sa);
|
||||
g_object_unref (ia);
|
||||
}
|
||||
|
||||
static void
|
||||
test_mask_parse (void)
|
||||
{
|
||||
@@ -368,6 +417,7 @@ main (int argc, char *argv[])
|
||||
g_test_add_func ("/inet-address/bytes", test_bytes);
|
||||
g_test_add_func ("/inet-address/property", test_property);
|
||||
g_test_add_func ("/socket-address/basic", test_socket_address);
|
||||
g_test_add_func ("/socket-address/to-string", test_socket_address_to_string);
|
||||
g_test_add_func ("/address-mask/parse", test_mask_parse);
|
||||
g_test_add_func ("/address-mask/property", test_mask_property);
|
||||
g_test_add_func ("/address-mask/equal", test_mask_equal);
|
||||
|
@@ -477,6 +477,43 @@ test_loopback_async (void)
|
||||
g_object_unref (addr);
|
||||
}
|
||||
|
||||
static void
|
||||
test_to_string (void)
|
||||
{
|
||||
GSocketConnectable *addr = NULL;
|
||||
gchar *str = NULL;
|
||||
GError *error = NULL;
|
||||
|
||||
/* Without port. */
|
||||
addr = g_network_address_new ("some-hostname", 0);
|
||||
str = g_socket_connectable_to_string (addr);
|
||||
g_assert_cmpstr (str, ==, "some-hostname");
|
||||
g_free (str);
|
||||
g_object_unref (addr);
|
||||
|
||||
/* With port. */
|
||||
addr = g_network_address_new ("some-hostname", 123);
|
||||
str = g_socket_connectable_to_string (addr);
|
||||
g_assert_cmpstr (str, ==, "some-hostname:123");
|
||||
g_free (str);
|
||||
g_object_unref (addr);
|
||||
|
||||
/* With scheme and port. */
|
||||
addr = g_network_address_parse_uri ("http://some-hostname:123", 80, &error);
|
||||
g_assert_no_error (error);
|
||||
str = g_socket_connectable_to_string (addr);
|
||||
g_assert_cmpstr (str, ==, "http:some-hostname:123");
|
||||
g_free (str);
|
||||
g_object_unref (addr);
|
||||
|
||||
/* Loopback. */
|
||||
addr = g_network_address_new ("localhost", 456);
|
||||
str = g_socket_connectable_to_string (addr);
|
||||
g_assert_cmpstr (str, ==, "localhost:456");
|
||||
g_free (str);
|
||||
g_object_unref (addr);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc, char *argv[])
|
||||
{
|
||||
@@ -520,6 +557,7 @@ main (int argc, char *argv[])
|
||||
g_test_add_func ("/network-address/loopback/basic", test_loopback_basic);
|
||||
g_test_add_func ("/network-address/loopback/sync", test_loopback_sync);
|
||||
g_test_add_func ("/network-address/loopback/async", test_loopback_async);
|
||||
g_test_add_func ("/network-address/to-string", test_to_string);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
@@ -67,6 +67,45 @@ test_unix_socket_address_construct (void)
|
||||
g_object_unref (a);
|
||||
}
|
||||
|
||||
static void
|
||||
test_unix_socket_address_to_string (void)
|
||||
{
|
||||
GSocketAddress *addr = NULL;
|
||||
gchar *str = NULL;
|
||||
|
||||
/* ADDRESS_PATH. */
|
||||
addr = g_unix_socket_address_new_with_type ("/some/path", -1,
|
||||
G_UNIX_SOCKET_ADDRESS_PATH);
|
||||
str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (addr));
|
||||
g_assert_cmpstr (str, ==, "/some/path");
|
||||
g_free (str);
|
||||
g_object_unref (addr);
|
||||
|
||||
/* ADDRESS_ANONYMOUS. */
|
||||
addr = g_unix_socket_address_new_with_type ("", 0,
|
||||
G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
|
||||
str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (addr));
|
||||
g_assert_cmpstr (str, ==, "anonymous");
|
||||
g_free (str);
|
||||
g_object_unref (addr);
|
||||
|
||||
/* ADDRESS_ABSTRACT. */
|
||||
addr = g_unix_socket_address_new_with_type ("abstract-path\0✋", 17,
|
||||
G_UNIX_SOCKET_ADDRESS_ABSTRACT);
|
||||
str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (addr));
|
||||
g_assert_cmpstr (str, ==, "abstract-path\\x00\\xe2\\x9c\\x8b");
|
||||
g_free (str);
|
||||
g_object_unref (addr);
|
||||
|
||||
/* ADDRESS_ABSTRACT_PADDED. */
|
||||
addr = g_unix_socket_address_new_with_type ("abstract-path\0✋", 17,
|
||||
G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
|
||||
str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (addr));
|
||||
g_assert_cmpstr (str, ==, "abstract-path\\x00\\xe2\\x9c\\x8b");
|
||||
g_free (str);
|
||||
g_object_unref (addr);
|
||||
}
|
||||
|
||||
int
|
||||
main (int argc,
|
||||
char **argv)
|
||||
@@ -74,6 +113,7 @@ main (int argc,
|
||||
g_test_init (&argc, &argv, NULL);
|
||||
|
||||
g_test_add_func ("/socket/address/unix/construct", test_unix_socket_address_construct);
|
||||
g_test_add_func ("/socket/address/unix/to-string", test_unix_socket_address_to_string);
|
||||
|
||||
return g_test_run ();
|
||||
}
|
||||
|
Reference in New Issue
Block a user