1#include <gio/gunixsocketaddress.h>
2
3static void
4test_unix_socket_address_construct (void)
5{
6 GUnixSocketAddress *a;
7
8 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS, NULL);
9 g_assert_cmpint (g_unix_socket_address_get_address_type (a), ==, G_UNIX_SOCKET_ADDRESS_PATH);
10 g_object_unref (object: a);
11
12 /* Try passing some default values for the arguments explicitly and
13 * make sure it makes no difference.
14 */
15 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS, first_property_name: "address-type", G_UNIX_SOCKET_ADDRESS_PATH, NULL);
16 g_assert_cmpint (g_unix_socket_address_get_address_type (a), ==, G_UNIX_SOCKET_ADDRESS_PATH);
17 g_object_unref (object: a);
18
19 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS, first_property_name: "abstract", FALSE, NULL);
20 g_assert_cmpint (g_unix_socket_address_get_address_type (a), ==, G_UNIX_SOCKET_ADDRESS_PATH);
21 g_object_unref (object: a);
22
23 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
24 first_property_name: "abstract", FALSE,
25 "address-type", G_UNIX_SOCKET_ADDRESS_PATH,
26 NULL);
27 g_assert_cmpint (g_unix_socket_address_get_address_type (a), ==, G_UNIX_SOCKET_ADDRESS_PATH);
28 g_object_unref (object: a);
29
30 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
31 first_property_name: "address-type", G_UNIX_SOCKET_ADDRESS_PATH,
32 "abstract", FALSE,
33 NULL);
34 g_assert_cmpint (g_unix_socket_address_get_address_type (a), ==, G_UNIX_SOCKET_ADDRESS_PATH);
35 g_object_unref (object: a);
36
37 /* Try explicitly setting abstract to TRUE */
38 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
39 first_property_name: "abstract", TRUE,
40 NULL);
41 g_assert_cmpint (g_unix_socket_address_get_address_type (a), ==, G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
42 g_object_unref (object: a);
43
44 /* Try explicitly setting a different kind of address */
45 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
46 first_property_name: "address-type", G_UNIX_SOCKET_ADDRESS_ANONYMOUS,
47 NULL);
48 g_assert_cmpint (g_unix_socket_address_get_address_type (a), ==, G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
49 g_object_unref (object: a);
50
51 /* Now try explicitly setting a different type of address after
52 * setting abstract to FALSE.
53 */
54 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
55 first_property_name: "abstract", FALSE,
56 "address-type", G_UNIX_SOCKET_ADDRESS_ANONYMOUS,
57 NULL);
58 g_assert_cmpint (g_unix_socket_address_get_address_type (a), ==, G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
59 g_object_unref (object: a);
60
61 /* And the other way around */
62 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
63 first_property_name: "address-type", G_UNIX_SOCKET_ADDRESS_ANONYMOUS,
64 "abstract", FALSE,
65 NULL);
66 g_assert_cmpint (g_unix_socket_address_get_address_type (a), ==, G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
67 g_object_unref (object: a);
68}
69
70static void
71test_unix_socket_address_to_string (void)
72{
73 GSocketAddress *addr = NULL;
74 gchar *str = NULL;
75
76 /* ADDRESS_PATH. */
77 addr = g_unix_socket_address_new_with_type (path: "/some/path", path_len: -1,
78 type: G_UNIX_SOCKET_ADDRESS_PATH);
79 str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (addr));
80 g_assert_cmpstr (str, ==, "/some/path");
81 g_free (mem: str);
82 g_object_unref (object: addr);
83
84 /* ADDRESS_ANONYMOUS. */
85 addr = g_unix_socket_address_new_with_type (path: "", path_len: 0,
86 type: G_UNIX_SOCKET_ADDRESS_ANONYMOUS);
87 str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (addr));
88 g_assert_cmpstr (str, ==, "anonymous");
89 g_free (mem: str);
90 g_object_unref (object: addr);
91
92 /* ADDRESS_ABSTRACT. */
93 addr = g_unix_socket_address_new_with_type (path: "abstract-path\0✋", path_len: 17,
94 type: G_UNIX_SOCKET_ADDRESS_ABSTRACT);
95 str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (addr));
96 g_assert_cmpstr (str, ==, "abstract-path\\x00\\xe2\\x9c\\x8b");
97 g_free (mem: str);
98 g_object_unref (object: addr);
99
100 /* ADDRESS_ABSTRACT_PADDED. */
101 addr = g_unix_socket_address_new_with_type (path: "abstract-path\0✋", path_len: 17,
102 type: G_UNIX_SOCKET_ADDRESS_ABSTRACT_PADDED);
103 str = g_socket_connectable_to_string (G_SOCKET_CONNECTABLE (addr));
104 g_assert_cmpstr (str, ==, "abstract-path\\x00\\xe2\\x9c\\x8b");
105 g_free (mem: str);
106 g_object_unref (object: addr);
107}
108
109int
110main (int argc,
111 char **argv)
112{
113 g_test_init (argc: &argc, argv: &argv, NULL);
114
115 g_test_add_func (testpath: "/socket/address/unix/construct", test_func: test_unix_socket_address_construct);
116 g_test_add_func (testpath: "/socket/address/unix/to-string", test_func: test_unix_socket_address_to_string);
117
118 return g_test_run ();
119}
120

source code of gtk/subprojects/glib/gio/tests/socket-address.c