1 /* Simple sanity-check for loopback through TCP and Unix sockets.
2 *
3 * Author: Simon McVittie <simon.mcvittie@collabora.co.uk>
4 * Copyright © 2010-2012 Nokia Corporation
5 * Copyright © 2015 Collabora Ltd.
6 *
7 * Permission is hereby granted, free of charge, to any person
8 * obtaining a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction,
10 * including without limitation the rights to use, copy, modify, merge,
11 * publish, distribute, sublicense, and/or sell copies of the Software,
12 * and to permit persons to whom the Software is furnished to do so,
13 * subject to the following conditions:
14 *
15 * The above copyright notice and this permission notice shall be
16 * included in all copies or substantial portions of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
19 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
21 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
22 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
23 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
24 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
25 * SOFTWARE.
26 */
27
28 #include <config.h>
29
30 #include <glib.h>
31 #include <glib/gstdio.h>
32
33 #include <dbus/dbus.h>
34
35 #include <errno.h>
36 #include <string.h>
37
38 #include "test-utils-glib.h"
39
40 typedef struct {
41 TestMainContext *ctx;
42 DBusError e;
43 gboolean skip;
44
45 DBusServer *server;
46 DBusConnection *server_conn;
47 /* queue of DBusMessage */
48 GQueue server_messages;
49
50 DBusConnection *client_conn;
51
52 gchar *tmp_runtime_dir;
53 gchar *saved_runtime_dir;
54 } Fixture;
55
56 static void
assert_no_error(const DBusError * e)57 assert_no_error (const DBusError *e)
58 {
59 if (G_UNLIKELY (dbus_error_is_set (e)))
60 g_error ("expected success but got error: %s: %s", e->name, e->message);
61 }
62
63 static DBusHandlerResult
server_message_cb(DBusConnection * server_conn,DBusMessage * message,void * data)64 server_message_cb (DBusConnection *server_conn,
65 DBusMessage *message,
66 void *data)
67 {
68 Fixture *f = data;
69
70 g_assert (server_conn == f->server_conn);
71 g_queue_push_tail (&f->server_messages, dbus_message_ref (message));
72
73 return DBUS_HANDLER_RESULT_HANDLED;
74 }
75
76 static void
new_conn_cb(DBusServer * server,DBusConnection * server_conn,void * data)77 new_conn_cb (DBusServer *server,
78 DBusConnection *server_conn,
79 void *data)
80 {
81 Fixture *f = data;
82 dbus_bool_t have_mem;
83
84 g_assert (f->server_conn == NULL);
85 f->server_conn = dbus_connection_ref (server_conn);
86 test_connection_setup (f->ctx, server_conn);
87
88 have_mem = dbus_connection_add_filter (server_conn,
89 server_message_cb, f, NULL);
90 g_assert (have_mem);
91 }
92
93 static void
setup(Fixture * f,gconstpointer addr)94 setup (Fixture *f,
95 gconstpointer addr)
96 {
97 f->ctx = test_main_context_get ();
98 dbus_error_init (&f->e);
99 g_queue_init (&f->server_messages);
100
101 if ((g_str_has_prefix (addr, "tcp:") ||
102 g_str_has_prefix (addr, "nonce-tcp:")) &&
103 !test_check_tcp_works ())
104 {
105 f->skip = TRUE;
106 return;
107 }
108
109 f->server = dbus_server_listen (addr, &f->e);
110 assert_no_error (&f->e);
111 g_assert (f->server != NULL);
112
113 dbus_server_set_new_connection_function (f->server,
114 new_conn_cb, f, NULL);
115 test_server_setup (f->ctx, f->server);
116 }
117
118 #ifdef DBUS_UNIX
119 static void
setup_runtime(Fixture * f,gconstpointer addr)120 setup_runtime (Fixture *f,
121 gconstpointer addr)
122 {
123 char *listening_at;
124 GError *error = NULL;
125
126 /* this is chosen to be something needing escaping */
127 f->tmp_runtime_dir = g_dir_make_tmp ("dbus=daemon=test.XXXXXX", &error);
128 g_assert_no_error (error);
129
130 /* we're relying on being single-threaded for this to be safe */
131 f->saved_runtime_dir = g_strdup (g_getenv ("XDG_RUNTIME_DIR"));
132 g_setenv ("XDG_RUNTIME_DIR", f->tmp_runtime_dir, TRUE);
133
134 setup (f, addr);
135
136 if (f->skip)
137 return;
138
139 listening_at = dbus_server_get_address (f->server);
140 g_test_message ("listening at %s", listening_at);
141 g_assert (g_str_has_prefix (listening_at, "unix:path="));
142 g_assert (strstr (listening_at, "dbus%3ddaemon%3dtest.") != NULL);
143 g_assert (strstr (listening_at, "/bus,") != NULL ||
144 g_str_has_suffix (listening_at, "/bus"));
145
146 dbus_free (listening_at);
147 }
148
149 static void
setup_no_runtime(Fixture * f,gconstpointer addr)150 setup_no_runtime (Fixture *f,
151 gconstpointer addr)
152 {
153 char *listening_at;
154
155 /* we're relying on being single-threaded for this to be safe */
156 f->saved_runtime_dir = g_strdup (g_getenv ("XDG_RUNTIME_DIR"));
157 g_unsetenv ("XDG_RUNTIME_DIR");
158
159 setup (f, addr);
160
161 if (f->skip)
162 return;
163
164 listening_at = dbus_server_get_address (f->server);
165 g_test_message ("listening at %s", listening_at);
166 /* we have fallen back to something in /tmp, either abstract or not */
167 g_assert (g_str_has_prefix (listening_at, "unix:"));
168 g_assert (strstr (listening_at, "=/tmp/") != NULL);
169
170 dbus_free (listening_at);
171 }
172 #endif
173
174 static void
test_connect(Fixture * f,gconstpointer addr)175 test_connect (Fixture *f,
176 gconstpointer addr)
177 {
178 const char *listening_address = addr;
179 char *address;
180 DBusAddressEntry **entries;
181 int n_entries;
182 dbus_bool_t ok;
183
184 if (f->skip)
185 return;
186
187 g_assert (f->server_conn == NULL);
188
189 address = dbus_server_get_address (f->server);
190 g_test_message ("listening at %s", address);
191
192 ok = dbus_parse_address (address, &entries, &n_entries, &f->e);
193 assert_no_error (&f->e);
194 g_assert_true (ok);
195 g_assert_cmpint (n_entries, ==, 1);
196
197 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "guid"), !=,
198 NULL);
199
200 if (g_strcmp0 (listening_address, "tcp:host=127.0.0.1") == 0)
201 {
202 g_assert_cmpstr (dbus_address_entry_get_method (entries[0]), ==, "tcp");
203 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "host"), ==,
204 "127.0.0.1");
205 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "port"), !=,
206 NULL);
207 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "noncefile"),
208 ==, NULL);
209 }
210 else if (g_strcmp0 (listening_address, "nonce-tcp:host=127.0.0.1") == 0)
211 {
212 g_assert_cmpstr (dbus_address_entry_get_method (entries[0]), ==,
213 "nonce-tcp");
214 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "host"), ==,
215 "127.0.0.1");
216 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "port"), !=,
217 NULL);
218 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "noncefile"),
219 !=, NULL);
220 }
221 #ifdef DBUS_UNIX
222 else if (g_strcmp0 (listening_address, "unix:tmpdir=/tmp") == 0)
223 {
224 g_assert_cmpstr (dbus_address_entry_get_method (entries[0]), ==, "unix");
225
226 if (dbus_address_entry_get_value (entries[0], "abstract") != NULL)
227 {
228 const char *abstract = dbus_address_entry_get_value (entries[0],
229 "abstract");
230
231 g_assert_true (g_str_has_prefix (abstract, "/tmp/dbus-"));
232 g_assert_cmpstr (dbus_address_entry_get_value (entries[0], "path"),
233 ==, NULL);
234 }
235 else
236 {
237 const char *path = dbus_address_entry_get_value (entries[0],
238 "path");
239
240 g_assert_nonnull (path);
241 g_assert_true (g_str_has_prefix (path, "/tmp/dbus-"));
242 }
243 }
244 else if (g_strcmp0 (listening_address, "unix:dir=/tmp") == 0)
245 {
246 const char *path = dbus_address_entry_get_value (entries[0],
247 "path");
248
249 g_assert_cmpstr (dbus_address_entry_get_method (entries[0]), ==, "unix");
250 g_assert_nonnull (path);
251 g_assert_true (g_str_has_prefix (path, "/tmp/dbus-"));
252 }
253 else if (g_strcmp0 (listening_address,
254 "unix:runtime=yes;unix:tmpdir=/tmp") == 0)
255 {
256 g_assert_cmpstr (dbus_address_entry_get_method (entries[0]), ==, "unix");
257 /* No particular statement about the path here: for that see
258 * setup_runtime() and setup_no_runtime() */
259 }
260 #endif
261 else
262 {
263 g_assert_not_reached ();
264 }
265
266 dbus_address_entries_free (entries);
267
268 f->client_conn = dbus_connection_open_private (address, &f->e);
269 assert_no_error (&f->e);
270 g_assert (f->client_conn != NULL);
271 test_connection_setup (f->ctx, f->client_conn);
272
273 while (f->server_conn == NULL)
274 {
275 test_progress ('.');
276 test_main_context_iterate (f->ctx, TRUE);
277 }
278
279 dbus_free (address);
280 }
281
282 static void
test_bad_guid(Fixture * f,gconstpointer addr G_GNUC_UNUSED)283 test_bad_guid (Fixture *f,
284 gconstpointer addr G_GNUC_UNUSED)
285 {
286 DBusMessage *incoming;
287 char *address;
288 gchar *guid;
289
290 if (f->skip)
291 return;
292
293 g_test_bug ("39720");
294
295 g_assert (f->server_conn == NULL);
296
297 address = dbus_server_get_address (f->server);
298 g_assert (strstr (address, "guid=") != NULL);
299 guid = strstr (address, "guid=");
300 g_assert_cmpuint (strlen (guid), >=, 5 + 32);
301
302 /* Change the first char of the guid to something different */
303 if (guid[5] == '0')
304 guid[5] = 'f';
305 else
306 guid[5] = '0';
307
308 f->client_conn = dbus_connection_open_private (address, &f->e);
309 assert_no_error (&f->e);
310 g_assert (f->client_conn != NULL);
311 test_connection_setup (f->ctx, f->client_conn);
312
313 while (f->server_conn == NULL)
314 {
315 test_progress ('.');
316 test_main_context_iterate (f->ctx, TRUE);
317 }
318
319 /* We get disconnected */
320
321 while (g_queue_is_empty (&f->server_messages))
322 {
323 test_progress ('.');
324 test_main_context_iterate (f->ctx, TRUE);
325 }
326
327 g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
328
329 incoming = g_queue_pop_head (&f->server_messages);
330
331 g_assert (!dbus_message_contains_unix_fds (incoming));
332 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
333 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
334 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
335 DBUS_INTERFACE_LOCAL);
336 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Disconnected");
337 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
338 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
339 g_assert_cmpstr (dbus_message_get_path (incoming), ==, DBUS_PATH_LOCAL);
340
341 dbus_clear_message (&incoming);
342 dbus_free (address);
343 }
344
345 static void
test_message(Fixture * f,gconstpointer addr)346 test_message (Fixture *f,
347 gconstpointer addr)
348 {
349 dbus_bool_t have_mem;
350 dbus_uint32_t serial;
351 DBusMessage *outgoing, *incoming;
352
353 if (f->skip)
354 return;
355
356 test_connect (f, addr);
357
358 outgoing = dbus_message_new_signal ("/com/example/Hello",
359 "com.example.Hello", "Greeting");
360 g_assert (outgoing != NULL);
361
362 have_mem = dbus_connection_send (f->client_conn, outgoing, &serial);
363 g_assert (have_mem);
364 g_assert (serial != 0);
365
366 while (g_queue_is_empty (&f->server_messages))
367 {
368 test_progress ('.');
369 test_main_context_iterate (f->ctx, TRUE);
370 }
371
372 g_assert_cmpuint (g_queue_get_length (&f->server_messages), ==, 1);
373
374 incoming = g_queue_pop_head (&f->server_messages);
375
376 g_assert (!dbus_message_contains_unix_fds (incoming));
377 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
378 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
379 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
380 "com.example.Hello");
381 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
382 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
383 g_assert_cmpstr (dbus_message_get_signature (incoming), ==, "");
384 g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
385 g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
386
387 dbus_clear_message (&incoming);
388 dbus_clear_message (&outgoing);
389 }
390
391 static void
teardown(Fixture * f,gconstpointer addr G_GNUC_UNUSED)392 teardown (Fixture *f,
393 gconstpointer addr G_GNUC_UNUSED)
394 {
395 if (f->client_conn != NULL)
396 dbus_connection_close (f->client_conn);
397
398 if (f->server_conn != NULL)
399 dbus_connection_close (f->server_conn);
400
401 dbus_clear_connection (&f->client_conn);
402 dbus_clear_connection (&f->server_conn);
403
404 if (f->server != NULL)
405 dbus_server_disconnect (f->server);
406
407 dbus_clear_server (&f->server);
408 test_main_context_unref (f->ctx);
409 }
410
411 #ifdef DBUS_UNIX
412 static void
teardown_no_runtime(Fixture * f,gconstpointer addr)413 teardown_no_runtime (Fixture *f,
414 gconstpointer addr)
415 {
416 teardown (f, addr);
417
418 /* we're relying on being single-threaded for this to be safe */
419 if (f->saved_runtime_dir != NULL)
420 g_setenv ("XDG_RUNTIME_DIR", f->saved_runtime_dir, TRUE);
421 else
422 g_unsetenv ("XDG_RUNTIME_DIR");
423 g_free (f->saved_runtime_dir);
424 }
425
426 static void
teardown_runtime(Fixture * f,gconstpointer addr)427 teardown_runtime (Fixture *f,
428 gconstpointer addr)
429 {
430 gchar *path;
431
432 teardown (f, addr);
433
434 /* the socket may exist */
435 path = g_strdup_printf ("%s/bus", f->tmp_runtime_dir);
436 test_remove_if_exists (path);
437 g_free (path);
438 /* there shouldn't be anything else in there */
439 test_rmdir_must_exist (f->tmp_runtime_dir);
440
441 /* we're relying on being single-threaded for this to be safe */
442 if (f->saved_runtime_dir != NULL)
443 g_setenv ("XDG_RUNTIME_DIR", f->saved_runtime_dir, TRUE);
444 else
445 g_unsetenv ("XDG_RUNTIME_DIR");
446 g_free (f->saved_runtime_dir);
447 g_free (f->tmp_runtime_dir);
448 }
449 #endif
450
451 int
main(int argc,char ** argv)452 main (int argc,
453 char **argv)
454 {
455 test_init (&argc, &argv);
456
457 g_test_add ("/connect/tcp", Fixture, "tcp:host=127.0.0.1", setup,
458 test_connect, teardown);
459 g_test_add ("/message/tcp", Fixture, "tcp:host=127.0.0.1", setup,
460 test_message, teardown);
461
462 g_test_add ("/connect/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
463 test_connect, teardown);
464 g_test_add ("/message/nonce-tcp", Fixture, "nonce-tcp:host=127.0.0.1", setup,
465 test_message, teardown);
466
467 g_test_add ("/message/bad-guid/tcp", Fixture, "tcp:host=127.0.0.1", setup,
468 test_bad_guid, teardown);
469
470 #ifdef DBUS_UNIX
471 g_test_add ("/connect/unix/tmpdir", Fixture, "unix:tmpdir=/tmp", setup,
472 test_connect, teardown);
473 g_test_add ("/message/unix/tmpdir", Fixture, "unix:tmpdir=/tmp", setup,
474 test_message, teardown);
475 g_test_add ("/connect/unix/dir", Fixture, "unix:dir=/tmp", setup,
476 test_connect, teardown);
477 g_test_add ("/message/unix/dir", Fixture, "unix:dir=/tmp", setup,
478 test_message, teardown);
479
480 g_test_add ("/connect/unix/runtime", Fixture,
481 "unix:runtime=yes;unix:tmpdir=/tmp", setup_runtime, test_connect,
482 teardown_runtime);
483 g_test_add ("/connect/unix/no-runtime", Fixture,
484 "unix:runtime=yes;unix:tmpdir=/tmp", setup_no_runtime, test_connect,
485 teardown_no_runtime);
486
487 g_test_add ("/message/bad-guid/unix", Fixture, "unix:tmpdir=/tmp", setup,
488 test_bad_guid, teardown);
489 #endif
490
491 return g_test_run ();
492 }
493