2 #include <gio/gnetworking.h>
3 #include <gio/gunixfdmessage.h>
4 #include <gio/gunixsocketaddress.h>
8 /* ensures that no FDs are left open at the end */
10 check_fd_list (const gint
*fd_list
)
14 for (i
= 0; i
< 40; i
++)
19 g_assert_cmpint (fd_list
[i
], ==, my_fd
);
22 for (i
= 0; i
< 40; i
++)
27 create_fd_list (gint
*fd_list
)
31 for (i
= 0; i
< 40; i
++)
34 g_assert_cmpint (fd_list
[i
], >, 0);
37 for (i
= 0; i
< 40; i
++)
45 GUnixFDMessage
*message
;
47 GUnixFDList
*list
, *l2
;
63 GUnixSocketAddressType type
;
65 create_fd_list (fd_list
);
67 s
= socketpair (PF_UNIX
, SOCK_STREAM
, 0, sv
);
68 g_assert_cmpint (s
, ==, 0);
71 list
= g_unix_fd_list_new_from_array (sv
, -1);
72 message
= G_UNIX_FD_MESSAGE (g_unix_fd_message_new_with_fd_list (list
));
73 g_object_unref (list
);
75 g_assert (g_unix_fd_message_get_fd_list (message
) == list
);
76 g_object_get (message
, "fd-list", &l2
, NULL
);
77 g_assert (l2
== list
);
78 g_assert_cmpint (g_unix_fd_list_get_length (list
), ==, 2);
80 peek
= g_unix_fd_list_peek_fds (list
, &s
);
81 g_assert_cmpint (s
, ==, 2);
82 stolen
= g_unix_fd_message_steal_fds (message
, &s
);
83 g_assert_cmpint (s
, ==, 2);
84 g_assert (stolen
== peek
);
86 g_assert_cmpint (stolen
[0], ==, sv
[0]);
87 g_assert_cmpint (stolen
[1], ==, sv
[1]);
88 g_assert_cmpint (stolen
[2], ==, sv
[2]);
91 g_unix_fd_message_append_fd (message
, sv
[0], &err
);
92 g_assert_no_error (err
);
94 g_assert_cmpint (s
, ==, 0);
95 g_unix_fd_message_append_fd (message
, sv
[1], &err
);
96 g_assert_no_error (err
);
98 g_assert_cmpint (s
, ==, 0);
100 s
= close (g_unix_fd_list_get (list
, 0, &err
));
101 g_assert_no_error (err
);
102 g_assert_cmpint (s
, ==, 0);
103 s
= close (g_unix_fd_list_get (list
, 1, &err
));
104 g_assert_no_error (err
);
105 g_assert_cmpint (s
, ==, 0);
106 s
= close (g_unix_fd_list_get (list
, 0, &err
));
107 g_assert_no_error (err
);
108 g_assert_cmpint (s
, ==, 0);
109 s
= close (g_unix_fd_list_get (list
, 1, &err
));
110 g_assert_no_error (err
);
111 g_assert_cmpint (s
, ==, 0);
112 s
= close (g_unix_fd_list_get (list
, 0, &err
));
113 g_assert_no_error (err
);
114 g_assert_cmpint (s
, ==, 0);
115 s
= close (g_unix_fd_list_get (list
, 1, &err
));
116 g_assert_no_error (err
);
117 g_assert_cmpint (s
, ==, 0);
119 g_object_unref (message
);
120 g_object_unref (list
);
122 message
= G_UNIX_FD_MESSAGE (g_unix_fd_message_new ());
123 list
= g_unix_fd_message_get_fd_list (message
);
125 g_assert_cmpint (s
, ==, 0);
127 s
= g_unix_fd_list_append (list
, sv
[0], &err
);
128 g_assert_no_error (err
);
129 g_assert_cmpint (s
, >=, 0);
130 s
= g_unix_fd_list_append (list
, sv
[1], &err
);
131 g_assert_no_error (err
);
132 g_assert_cmpint (s
, >=, 0);
135 g_assert_cmpint (s
, ==, 0);
137 g_assert_cmpint (s
, ==, 0);
138 s
= close (g_unix_fd_list_get (list
, 0, &err
));
139 g_assert_no_error (err
);
140 g_assert_cmpint (s
, ==, 0);
141 s
= close (g_unix_fd_list_get (list
, 1, &err
));
142 g_assert_no_error (err
);
143 g_assert_cmpint (s
, ==, 0);
145 s
= socketpair (PF_UNIX
, SOCK_STREAM
, 0, sv
);
146 g_assert_cmpint (s
, ==, 0);
148 sockets
[0] = g_socket_new_from_fd (sv
[0], &err
);
149 g_assert_no_error (err
);
150 g_assert (G_IS_SOCKET (sockets
[0]));
151 sockets
[1] = g_socket_new_from_fd (sv
[1], &err
);
152 g_assert_no_error (err
);
153 g_assert (G_IS_SOCKET (sockets
[1]));
155 addr
= g_socket_get_local_address (sockets
[0], &err
);
156 g_assert_no_error (err
);
157 g_assert (G_IS_UNIX_SOCKET_ADDRESS (addr
));
158 g_assert_cmpint (g_unix_socket_address_get_address_type (G_UNIX_SOCKET_ADDRESS (addr
)), ==, G_UNIX_SOCKET_ADDRESS_ANONYMOUS
);
159 g_assert_cmpint (g_unix_socket_address_get_path_len (G_UNIX_SOCKET_ADDRESS (addr
)), ==, 0);
161 G_GNUC_BEGIN_IGNORE_DEPRECATIONS
162 g_assert (!g_unix_socket_address_get_is_abstract (G_UNIX_SOCKET_ADDRESS (addr
)));
163 G_GNUC_END_IGNORE_DEPRECATIONS
167 "path-as-array", &array
,
168 "abstract", &abstract
,
169 "address-type", &type
,
171 g_assert_cmpstr (path
, ==, "");
172 g_assert_cmpint (array
->len
, ==, 0);
173 g_assert (!abstract
);
174 g_assert (type
== G_UNIX_SOCKET_ADDRESS_ANONYMOUS
);
176 g_byte_array_free (array
, TRUE
);
178 g_object_unref (addr
);
183 s
= g_socket_send_message (sockets
[0], NULL
, &ov
, 1,
184 (GSocketControlMessage
**) &message
,
186 g_assert_no_error (err
);
187 g_assert_cmpint (s
, ==, 1);
188 g_object_unref (message
);
195 s
= g_socket_receive_message (sockets
[1], NULL
, &iv
, 1,
196 (GSocketControlMessage
***) &mv
,
197 &nm
, &flags
, NULL
, &err
);
198 g_assert_no_error (err
);
199 g_assert_cmpint (s
, ==, 1);
200 g_object_unref (sockets
[0]);
201 g_object_unref (sockets
[1]);
203 g_assert_cmpint (nm
, ==, 1);
207 g_assert (G_IS_UNIX_FD_MESSAGE (message
));
208 list
= g_object_ref (g_unix_fd_message_get_fd_list (message
));
209 g_object_unref (message
);
211 peek
= g_unix_fd_list_peek_fds (list
, &s
);
212 g_assert_cmpint (s
, ==, 2);
213 sv
[0] = g_unix_fd_list_get (list
, 1, &err
);
214 g_assert_no_error (err
);
216 strcpy (buffer
, "failure to say failure to say 'i love gnome-panel!'.");
217 s
= write (sv
[0], buffer
, strlen (buffer
) + 1);
218 g_assert_cmpint (s
, ==, strlen (buffer
) + 1);
221 memset (buffer
, 0xff, sizeof buffer
);
223 s
= read (peek
[0], buffer
, sizeof buffer
);
224 g_assert_cmpint (s
, ==, 53);
225 g_assert_cmpstr (buffer
, ==,
226 "failure to say failure to say 'i love gnome-panel!'.");
228 g_object_unref (list
);
230 check_fd_list (fd_list
);
234 main (int argc
, char **argv
)
236 g_test_init (&argc
, &argv
, NULL
);
238 g_test_add_func ("/unix-streams/file-descriptors", test_fds
);