2 * Copyright © 2010-2012 Nokia Corporation
3 * Copyright © 2014 Collabora Ltd.
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation files
7 * (the "Software"), to deal in the Software without restriction,
8 * including without limitation the rights to use, copy, modify, merge,
9 * publish, distribute, sublicense, and/or sell copies of the Software,
10 * and to permit persons to whom the Software is furnished to do so,
11 * subject to the following conditions:
13 * The above copyright notice and this permission notice shall be
14 * included in all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28 #include <dbus/dbus.h>
29 #include <dbus/dbus-internals.h>
30 #include <dbus/dbus-sysdeps.h>
37 # include <dbus/dbus-sysdeps-unix.h>
41 # ifdef HAVE_SYS_RESOURCE_H
42 # include <sys/resource.h>
44 # include <sys/stat.h>
45 # include <sys/time.h>
46 # include <sys/types.h>
50 #include "test-utils.h"
52 /* Arbitrary; included here to avoid relying on the default */
53 #define MAX_MESSAGE_UNIX_FDS 20
54 /* This test won't work on Linux unless this is true. */
55 _DBUS_STATIC_ASSERT (MAX_MESSAGE_UNIX_FDS <= 253);
57 /* Arbitrary; included here to avoid relying on the default. */
58 #define MAX_INCOMING_UNIX_FDS (MAX_MESSAGE_UNIX_FDS * 4)
60 /* Arbitrary, except that MAX_MESSAGE_UNIX_FDS * SOME_MESSAGES must be
61 * less than the process's file descriptor limit. */
62 #define SOME_MESSAGES 50
64 /* Linux won't allow more than 253 fds per sendmsg(). */
65 #define TOO_MANY_FDS 255
66 _DBUS_STATIC_ASSERT (MAX_MESSAGE_UNIX_FDS < TOO_MANY_FDS);
68 /* As in test/relay.c, this is a miniature dbus-daemon: we relay messages
69 * from the client on the left to the client on the right.
71 * left socket left dispatch right socket right
72 * client ===========> server --------------> server ===========> client
82 DBusConnection *left_client_conn;
83 DBusConnection *left_server_conn;
85 DBusConnection *right_server_conn;
86 DBusConnection *right_client_conn;
87 /* queue of DBusMessage received by right_client_conn */
93 #if !GLIB_CHECK_VERSION (2, 38, 0)
94 #define g_test_skip(s) my_test_skip (s)
95 static inline void my_test_skip (const gchar *s)
97 g_message ("SKIP: %s", s);
101 #ifdef HAVE_UNIX_FD_PASSING
103 static void oom (const gchar *doing) G_GNUC_NORETURN;
106 oom (const gchar *doing)
108 g_error ("out of memory (%s)", doing);
112 assert_no_error (const DBusError *e)
114 if (G_UNLIKELY (dbus_error_is_set (e)))
115 g_error ("expected success but got error: %s: %s", e->name, e->message);
118 static DBusHandlerResult
119 left_server_message_cb (DBusConnection *server_conn,
120 DBusMessage *message,
125 g_assert (server_conn == f->left_server_conn);
126 g_assert (f->right_server_conn != NULL);
128 dbus_connection_send (f->right_server_conn, message, NULL);
130 return DBUS_HANDLER_RESULT_HANDLED;
133 static DBusHandlerResult
134 right_client_message_cb (DBusConnection *client_conn,
135 DBusMessage *message,
140 g_assert (client_conn == f->right_client_conn);
141 g_queue_push_tail (&f->messages, dbus_message_ref (message));
143 return DBUS_HANDLER_RESULT_HANDLED;
147 new_conn_cb (DBusServer *server,
148 DBusConnection *server_conn,
153 dbus_connection_set_max_message_unix_fds (server_conn,
154 MAX_MESSAGE_UNIX_FDS);
155 dbus_connection_set_max_received_unix_fds (server_conn,
156 MAX_INCOMING_UNIX_FDS);
158 if (f->left_server_conn == NULL)
160 f->left_server_conn = dbus_connection_ref (server_conn);
162 if (!dbus_connection_add_filter (server_conn,
163 left_server_message_cb, f, NULL))
164 oom ("adding filter");
168 g_assert (f->right_server_conn == NULL);
169 f->right_server_conn = dbus_connection_ref (server_conn);
172 test_connection_setup (f->ctx, server_conn);
176 test_connect (Fixture *f,
177 gconstpointer data G_GNUC_UNUSED)
181 g_assert (f->left_server_conn == NULL);
182 g_assert (f->right_server_conn == NULL);
184 address = dbus_server_get_address (f->server);
185 g_assert (address != NULL);
187 f->left_client_conn = dbus_connection_open_private (address, &f->e);
188 assert_no_error (&f->e);
189 g_assert (f->left_client_conn != NULL);
190 test_connection_setup (f->ctx, f->left_client_conn);
192 /* The left client connection is allowed to behave abusively. */
193 dbus_connection_set_max_message_unix_fds (f->left_client_conn, 1000);
194 dbus_connection_set_max_received_unix_fds (f->left_client_conn, 1000000);
196 while (f->left_server_conn == NULL)
199 test_main_context_iterate (f->ctx, TRUE);
202 f->right_client_conn = dbus_connection_open_private (address, &f->e);
203 assert_no_error (&f->e);
204 g_assert (f->right_client_conn != NULL);
205 test_connection_setup (f->ctx, f->right_client_conn);
209 while (f->right_server_conn == NULL)
212 test_main_context_iterate (f->ctx, TRUE);
215 if (!dbus_connection_add_filter (f->right_client_conn,
216 right_client_message_cb, f, NULL))
217 oom ("adding filter");
219 /* The right client connection is allowed to queue all the messages. */
220 dbus_connection_set_max_message_unix_fds (f->right_client_conn, 1000);
221 dbus_connection_set_max_received_unix_fds (f->right_client_conn, 1000000);
223 while (!dbus_connection_get_is_authenticated (f->left_client_conn) ||
224 !dbus_connection_get_is_authenticated (f->right_client_conn) ||
225 !dbus_connection_get_is_authenticated (f->left_server_conn) ||
226 !dbus_connection_get_is_authenticated (f->right_server_conn))
229 test_main_context_iterate (f->ctx, TRUE);
232 if (!dbus_connection_can_send_type (f->left_client_conn,
234 g_error ("left client connection cannot send Unix fds");
236 if (!dbus_connection_can_send_type (f->left_server_conn,
238 g_error ("left server connection cannot send Unix fds");
240 if (!dbus_connection_can_send_type (f->right_client_conn,
242 g_error ("right client connection cannot send Unix fds");
244 if (!dbus_connection_can_send_type (f->right_server_conn,
246 g_error ("right server connection cannot send Unix fds");
252 gconstpointer data G_GNUC_UNUSED)
254 #ifdef HAVE_UNIX_FD_PASSING
255 /* We assume that anything with fd-passing supports the unix: transport */
257 f->ctx = test_main_context_get ();
258 dbus_error_init (&f->e);
259 g_queue_init (&f->messages);
261 f->server = dbus_server_listen ("unix:tmpdir=/tmp", &f->e);
262 assert_no_error (&f->e);
263 g_assert (f->server != NULL);
265 dbus_server_set_new_connection_function (f->server,
266 new_conn_cb, f, NULL);
267 test_server_setup (f->ctx, f->server);
269 f->fd_before = open ("/dev/null", O_RDONLY);
271 /* this should succeed on any reasonable Unix */
272 if (f->fd_before < 0)
273 g_error ("cannot open /dev/null for reading: %s", g_strerror (errno));
275 _dbus_fd_set_close_on_exec (f->fd_before);
280 test_relay (Fixture *f,
283 #ifdef HAVE_UNIX_FD_PASSING
284 /* We assume that any platform with working fd-passing is POSIX,
285 * and therefore has open() and fstat() */
286 dbus_uint32_t serial;
287 DBusMessage *outgoing, *incoming;
289 struct stat stat_before;
290 struct stat stat_after;
292 test_connect (f, data);
294 outgoing = dbus_message_new_signal ("/com/example/Hello",
295 "com.example.Hello", "Greeting");
296 g_assert (outgoing != NULL);
298 if (!dbus_message_append_args (outgoing,
299 DBUS_TYPE_UNIX_FD, &f->fd_before,
301 oom ("appending fd");
303 if (!dbus_connection_send (f->left_client_conn, outgoing, &serial))
304 oom ("sending message");
306 dbus_message_unref (outgoing);
308 while (g_queue_get_length (&f->messages) < 1)
311 test_main_context_iterate (f->ctx, TRUE);
314 g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 1);
316 incoming = g_queue_pop_head (&f->messages);
318 g_assert (dbus_message_contains_unix_fds (incoming));
319 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
320 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
321 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
322 "com.example.Hello");
323 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
324 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
325 g_assert_cmpstr (dbus_message_get_signature (incoming), ==,
326 DBUS_TYPE_UNIX_FD_AS_STRING);
327 g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
328 g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
330 if (!dbus_message_get_args (incoming,
332 DBUS_TYPE_UNIX_FD, &fd_after,
334 g_error ("%s: %s", f->e.name, f->e.message);
336 assert_no_error (&f->e);
338 if (fstat (f->fd_before, &stat_before) < 0)
339 g_error ("%s", g_strerror (errno));
341 if (fstat (fd_after, &stat_after) < 0)
342 g_error ("%s", g_strerror (errno));
344 /* this seems like enough to say "it's the same file" */
345 g_assert_cmpint (stat_before.st_dev, ==, stat_after.st_dev);
346 g_assert_cmpint (stat_before.st_ino, ==, stat_after.st_ino);
347 g_assert_cmpint (stat_before.st_rdev, ==, stat_after.st_rdev);
349 dbus_message_unref (incoming);
351 if (close (fd_after) < 0)
352 g_error ("%s", g_strerror (errno));
354 g_assert (dbus_connection_get_is_connected (f->right_client_conn));
355 g_assert (dbus_connection_get_is_connected (f->right_server_conn));
356 g_assert (dbus_connection_get_is_connected (f->left_client_conn));
357 g_assert (dbus_connection_get_is_connected (f->left_server_conn));
359 g_test_skip ("fd-passing not supported on this platform");
364 test_limit (Fixture *f,
367 #ifdef HAVE_UNIX_FD_PASSING
368 dbus_uint32_t serial;
369 DBusMessage *outgoing, *incoming;
372 test_connect (f, data);
374 outgoing = dbus_message_new_signal ("/com/example/Hello",
375 "com.example.Hello", "Greeting");
376 g_assert (outgoing != NULL);
378 for (i = 0; i < MAX_MESSAGE_UNIX_FDS; i++)
380 if (!dbus_message_append_args (outgoing,
381 DBUS_TYPE_UNIX_FD, &f->fd_before,
383 oom ("appending fd");
386 if (!dbus_connection_send (f->left_client_conn, outgoing, &serial))
387 oom ("sending message");
389 dbus_message_unref (outgoing);
391 while (g_queue_get_length (&f->messages) < 1)
394 test_main_context_iterate (f->ctx, TRUE);
397 g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 1);
399 incoming = g_queue_pop_head (&f->messages);
401 g_assert (dbus_message_contains_unix_fds (incoming));
402 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
403 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
404 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
405 "com.example.Hello");
406 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
407 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
408 g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
409 g_assert_cmpuint (dbus_message_get_serial (incoming), ==, serial);
411 dbus_message_unref (incoming);
413 g_assert (dbus_connection_get_is_connected (f->right_client_conn));
414 g_assert (dbus_connection_get_is_connected (f->right_server_conn));
415 g_assert (dbus_connection_get_is_connected (f->left_client_conn));
416 g_assert (dbus_connection_get_is_connected (f->left_server_conn));
418 g_test_skip ("fd-passing not supported on this platform");
423 test_too_many (Fixture *f,
426 #ifdef HAVE_UNIX_FD_PASSING
427 DBusMessage *outgoing;
430 test_connect (f, data);
432 outgoing = dbus_message_new_signal ("/com/example/Hello",
433 "com.example.Hello", "Greeting");
434 g_assert (outgoing != NULL);
436 for (i = 0; i < MAX_MESSAGE_UNIX_FDS + GPOINTER_TO_UINT (data); i++)
438 if (!dbus_message_append_args (outgoing,
439 DBUS_TYPE_UNIX_FD, &f->fd_before,
441 oom ("appending fd");
444 if (!dbus_connection_send (f->left_client_conn, outgoing, NULL))
445 oom ("sending message");
447 dbus_message_unref (outgoing);
449 /* The sender is unceremoniously disconnected. */
450 while (dbus_connection_get_is_connected (f->left_client_conn) ||
451 dbus_connection_get_is_connected (f->left_server_conn))
454 test_main_context_iterate (f->ctx, TRUE);
457 /* The message didn't get through without its fds. */
458 g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 0);
460 /* The intended victim is unaffected by the left connection's
462 g_assert (dbus_connection_get_is_connected (f->right_client_conn));
463 g_assert (dbus_connection_get_is_connected (f->right_server_conn));
465 g_test_skip ("fd-passing not supported on this platform");
470 test_too_many_split (Fixture *f,
473 #ifdef HAVE_UNIX_FD_PASSING
474 DBusMessage *outgoing;
476 int left_client_socket;
480 int fds[TOO_MANY_FDS];
483 /* This test deliberately pushes up against OS limits, so skip it
484 * if we don't have enough fds. 4 times the maximum per message
485 * ought to be enough: that will cover the message, the dup'd fds
486 * we actually send, the copy that we potentially receive, and some
487 * spare capacity for everything else. */
488 #ifdef HAVE_GETRLIMIT
491 if (getrlimit (RLIMIT_NOFILE, &lim) == 0)
493 if (lim.rlim_cur != RLIM_INFINITY &&
494 lim.rlim_cur < 4 * TOO_MANY_FDS)
496 g_test_skip ("not enough RLIMIT_NOFILE");
502 test_connect (f, data);
504 outgoing = dbus_message_new_signal ("/com/example/Hello",
505 "com.example.Hello", "Greeting");
506 g_assert (outgoing != NULL);
508 /* TOO_MANY_FDS fds are far too many: in particular, Linux doesn't allow
509 * sending this many in a single sendmsg(). libdbus never splits
510 * a message between two sendmsg() calls if it can help it, and
511 * in particular it always sends all the fds with the first sendmsg(),
512 * but malicious senders might not be so considerate. */
513 for (i = 0; i < TOO_MANY_FDS; i++)
515 if (!dbus_message_append_args (outgoing,
516 DBUS_TYPE_UNIX_FD, &f->fd_before,
518 oom ("appending fd");
521 /* This probably shouldn't work for messages with fds, but it does,
522 * which is convenient for this sort of trickery. */
523 if (!dbus_message_marshal (outgoing, &payload, &payload_len))
524 oom ("marshalling message");
526 _dbus_string_init_const_len (&buffer, payload, payload_len);
528 for (i = 0; i < TOO_MANY_FDS; i++)
530 fds[i] = dup (f->fd_before);
533 g_error ("could not dup fd: %s", g_strerror (errno));
536 /* This is blatant cheating, and the API documentation specifically
537 * tells you not use this function in this way. Never do this
538 * in application code. */
539 if (!dbus_connection_get_socket (f->left_client_conn, &left_client_socket))
540 g_error ("'unix:' DBusConnection should have had a socket");
542 /* Just to be sure that we're at a message boundary. */
543 dbus_connection_flush (f->left_client_conn);
545 /* We have too many fds for one sendmsg(), so send the first half
546 * (rounding down if odd) with the first byte... */
547 done = _dbus_write_socket_with_unix_fds (left_client_socket, &buffer, 0, 1,
548 &fds[0], TOO_MANY_FDS / 2);
551 g_error ("could not send first byte and first batch of fds: %s",
554 /* ... and the second half (rounding up if odd) with the rest of
556 done = _dbus_write_socket_with_unix_fds (left_client_socket, &buffer, 1,
557 payload_len - 1, &fds[TOO_MANY_FDS / 2],
558 TOO_MANY_FDS - (TOO_MANY_FDS / 2));
562 g_error ("could not send rest of message and rest of fds: %s",
565 else if (done < payload_len - 1)
567 /* For simplicity, assume the socket buffer is big enough for the
568 * whole message, which should be < 2 KiB. If this fails on some
569 * OS, redo this test code to use a proper loop like the real
571 g_error ("short write in sendmsg(), fix this test: %d/%d",
572 done, payload_len - 1);
577 for (i = 0; i < TOO_MANY_FDS; i++)
580 dbus_message_unref (outgoing);
582 /* The sender is unceremoniously disconnected. */
583 while (dbus_connection_get_is_connected (f->left_client_conn) ||
584 dbus_connection_get_is_connected (f->left_server_conn))
587 test_main_context_iterate (f->ctx, TRUE);
590 /* The message didn't get through without its fds. */
591 g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 0);
593 /* The intended victim is unaffected by the left connection's
595 g_assert (dbus_connection_get_is_connected (f->right_client_conn));
596 g_assert (dbus_connection_get_is_connected (f->right_server_conn));
598 g_test_skip ("fd-passing not supported on this platform");
603 test_flood (Fixture *f,
606 #ifdef HAVE_UNIX_FD_PASSING
608 DBusMessage *outgoing[SOME_MESSAGES];
609 dbus_uint32_t serial;
611 test_connect (f, data);
613 for (j = 0; j < SOME_MESSAGES; j++)
615 outgoing[j] = dbus_message_new_signal ("/com/example/Hello",
616 "com.example.Hello", "Greeting");
617 g_assert (outgoing[j] != NULL);
619 for (i = 0; i < GPOINTER_TO_UINT (data); i++)
621 if (!dbus_message_append_args (outgoing[j],
622 DBUS_TYPE_UNIX_FD, &f->fd_before,
624 oom ("appending fd");
628 /* This is in its own loop so we do it as fast as possible */
629 for (j = 0; j < SOME_MESSAGES; j++)
631 if (!dbus_connection_send (f->left_client_conn, outgoing[j], &serial))
632 oom ("sending message");
635 for (j = 0; j < SOME_MESSAGES; j++)
637 dbus_message_unref (outgoing[j]);
640 while (g_queue_get_length (&f->messages) < SOME_MESSAGES)
643 test_main_context_iterate (f->ctx, TRUE);
646 g_assert_cmpuint (g_queue_get_length (&f->messages), ==, SOME_MESSAGES);
648 for (j = 0; j < SOME_MESSAGES; j++)
650 DBusMessage *incoming;
652 incoming = g_queue_pop_head (&f->messages);
654 g_assert (dbus_message_contains_unix_fds (incoming));
655 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
656 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
657 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
658 "com.example.Hello");
659 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
660 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
661 g_assert_cmpstr (dbus_message_get_path (incoming), ==, "/com/example/Hello");
663 dbus_message_unref (incoming);
666 g_assert (dbus_connection_get_is_connected (f->right_client_conn));
667 g_assert (dbus_connection_get_is_connected (f->right_server_conn));
668 g_assert (dbus_connection_get_is_connected (f->left_client_conn));
669 g_assert (dbus_connection_get_is_connected (f->left_server_conn));
671 g_test_skip ("fd-passing not supported on this platform");
676 test_odd_limit (Fixture *f,
679 #ifdef HAVE_UNIX_FD_PASSING
680 DBusMessage *outgoing;
683 test_connect (f, data);
684 dbus_connection_set_max_message_unix_fds (f->left_server_conn, 7);
685 dbus_connection_set_max_message_unix_fds (f->right_server_conn, 7);
687 outgoing = dbus_message_new_signal ("/com/example/Hello",
688 "com.example.Hello", "Greeting");
689 g_assert (outgoing != NULL);
691 for (i = 0; i < 7 + GPOINTER_TO_INT (data); i++)
693 if (!dbus_message_append_args (outgoing,
694 DBUS_TYPE_UNIX_FD, &f->fd_before,
696 oom ("appending fd");
699 if (!dbus_connection_send (f->left_client_conn, outgoing, NULL))
700 oom ("sending message");
702 dbus_message_unref (outgoing);
704 if (GPOINTER_TO_INT (data) > 0)
706 /* The sender is unceremoniously disconnected. */
707 while (dbus_connection_get_is_connected (f->left_client_conn) ||
708 dbus_connection_get_is_connected (f->left_server_conn))
711 test_main_context_iterate (f->ctx, TRUE);
714 /* The message didn't get through without its fds. */
715 g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 0);
717 /* The intended victim is unaffected by the left connection's
719 g_assert (dbus_connection_get_is_connected (f->right_client_conn));
720 g_assert (dbus_connection_get_is_connected (f->right_server_conn));
724 DBusMessage *incoming;
726 /* We're at or under the limit. The message gets through intact. */
727 while (g_queue_get_length (&f->messages) < 1)
730 test_main_context_iterate (f->ctx, TRUE);
733 g_assert_cmpuint (g_queue_get_length (&f->messages), ==, 1);
735 incoming = g_queue_pop_head (&f->messages);
737 g_assert (dbus_message_contains_unix_fds (incoming));
738 g_assert_cmpstr (dbus_message_get_destination (incoming), ==, NULL);
739 g_assert_cmpstr (dbus_message_get_error_name (incoming), ==, NULL);
740 g_assert_cmpstr (dbus_message_get_interface (incoming), ==,
741 "com.example.Hello");
742 g_assert_cmpstr (dbus_message_get_member (incoming), ==, "Greeting");
743 g_assert_cmpstr (dbus_message_get_sender (incoming), ==, NULL);
744 g_assert_cmpstr (dbus_message_get_path (incoming), ==,
745 "/com/example/Hello");
747 dbus_message_unref (incoming);
749 g_assert (dbus_connection_get_is_connected (f->right_client_conn));
750 g_assert (dbus_connection_get_is_connected (f->right_server_conn));
751 g_assert (dbus_connection_get_is_connected (f->left_client_conn));
752 g_assert (dbus_connection_get_is_connected (f->left_server_conn));
755 g_test_skip ("fd-passing not supported on this platform");
760 teardown (Fixture *f,
761 gconstpointer data G_GNUC_UNUSED)
763 #ifdef HAVE_UNIX_FD_PASSING
764 if (f->left_client_conn != NULL)
766 dbus_connection_close (f->left_client_conn);
767 dbus_connection_unref (f->left_client_conn);
768 f->left_client_conn = NULL;
771 if (f->right_client_conn != NULL)
773 dbus_connection_close (f->right_client_conn);
774 dbus_connection_unref (f->right_client_conn);
775 f->right_client_conn = NULL;
778 if (f->left_server_conn != NULL)
780 dbus_connection_close (f->left_server_conn);
781 dbus_connection_unref (f->left_server_conn);
782 f->left_server_conn = NULL;
785 if (f->right_server_conn != NULL)
787 dbus_connection_close (f->right_server_conn);
788 dbus_connection_unref (f->right_server_conn);
789 f->right_server_conn = NULL;
792 if (f->server != NULL)
794 dbus_server_disconnect (f->server);
795 dbus_server_unref (f->server);
799 test_main_context_unref (f->ctx);
801 if (f->fd_before >= 0 && close (f->fd_before) < 0)
802 g_error ("%s", g_strerror (errno));
810 g_test_init (&argc, &argv, NULL);
811 g_test_bug_base ("https://bugs.freedesktop.org/show_bug.cgi?id=");
813 g_test_add ("/relay", Fixture, NULL, setup,
814 test_relay, teardown);
815 g_test_add ("/limit", Fixture, NULL, setup,
816 test_limit, teardown);
818 g_test_add ("/too-many/plus1", Fixture, GUINT_TO_POINTER (1), setup,
819 test_too_many, teardown);
820 g_test_add ("/too-many/plus2", Fixture, GUINT_TO_POINTER (2), setup,
821 test_too_many, teardown);
822 g_test_add ("/too-many/plus17", Fixture, GUINT_TO_POINTER (17), setup,
823 test_too_many, teardown);
825 g_test_add ("/too-many/split", Fixture, NULL, setup,
826 test_too_many_split, teardown);
828 g_test_add ("/flood/1", Fixture, GUINT_TO_POINTER (1),
829 setup, test_flood, teardown);
830 #if MAX_MESSAGE_UNIX_FDS >= 2
831 g_test_add ("/flood/half-limit", Fixture,
832 GUINT_TO_POINTER (MAX_MESSAGE_UNIX_FDS / 2),
833 setup, test_flood, teardown);
835 #if MAX_MESSAGE_UNIX_FDS >= 4
836 g_test_add ("/flood/over-half-limit", Fixture,
837 GUINT_TO_POINTER (3 * MAX_MESSAGE_UNIX_FDS / 4),
838 setup, test_flood, teardown);
840 g_test_add ("/flood/limit", Fixture,
841 GUINT_TO_POINTER (MAX_MESSAGE_UNIX_FDS), setup, test_flood, teardown);
843 g_test_add ("/odd-limit/minus1", Fixture, GINT_TO_POINTER (-1), setup,
844 test_odd_limit, teardown);
845 g_test_add ("/odd-limit/at", Fixture, GINT_TO_POINTER (0), setup,
846 test_odd_limit, teardown);
847 g_test_add ("/odd-limit/plus1", Fixture, GINT_TO_POINTER (+1), setup,
848 test_odd_limit, teardown);
849 g_test_add ("/odd-limit/plus2", Fixture, GINT_TO_POINTER (+2), setup,
850 test_odd_limit, teardown);
852 return g_test_run ();