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