1 #include <gio/gunixsocketaddress.h>
2
3 static void
test_unix_socket_address_construct(void)4 test_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 (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, "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 (a);
18
19 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS, "abstract", FALSE, NULL);
20 g_assert_cmpint (g_unix_socket_address_get_address_type (a), ==, G_UNIX_SOCKET_ADDRESS_PATH);
21 g_object_unref (a);
22
23 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
24 "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 (a);
29
30 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
31 "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 (a);
36
37 /* Try explicitly setting abstract to TRUE */
38 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
39 "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 (a);
43
44 /* Try explicitly setting a different kind of address */
45 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
46 "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 (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 "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 (a);
60
61 /* And the other way around */
62 a = g_object_new (G_TYPE_UNIX_SOCKET_ADDRESS,
63 "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 (a);
68 }
69
70 static void
test_unix_socket_address_to_string(void)71 test_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 ("/some/path", -1,
78 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 (str);
82 g_object_unref (addr);
83
84 /* ADDRESS_ANONYMOUS. */
85 addr = g_unix_socket_address_new_with_type ("", 0,
86 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 (str);
90 g_object_unref (addr);
91
92 /* ADDRESS_ABSTRACT. */
93 addr = g_unix_socket_address_new_with_type ("abstract-path\0✋", 17,
94 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 (str);
98 g_object_unref (addr);
99
100 /* ADDRESS_ABSTRACT_PADDED. */
101 addr = g_unix_socket_address_new_with_type ("abstract-path\0✋", 17,
102 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 (str);
106 g_object_unref (addr);
107 }
108
109 int
main(int argc,char ** argv)110 main (int argc,
111 char **argv)
112 {
113 g_test_init (&argc, &argv, NULL);
114
115 g_test_add_func ("/socket/address/unix/construct", test_unix_socket_address_construct);
116 g_test_add_func ("/socket/address/unix/to-string", test_unix_socket_address_to_string);
117
118 return g_test_run ();
119 }
120