1dbecdeabb20e0ce11121819c63373f0afba57c58Marcus Brinkmann#include <config.h>
25e359834f28b0580e7b79a057614d4e3592709acColin Walters#include "../test-utils.h"
35e359834f28b0580e7b79a057614d4e3592709acColin Walters
45e359834f28b0580e7b79a057614d4e3592709acColin Waltersstatic void
55e359834f28b0580e7b79a057614d4e3592709acColin Waltersdie (const char *message, ...)
65e359834f28b0580e7b79a057614d4e3592709acColin Walters{
75e359834f28b0580e7b79a057614d4e3592709acColin Walters  va_list args;
85e359834f28b0580e7b79a057614d4e3592709acColin Walters  va_start (args, message);
95e359834f28b0580e7b79a057614d4e3592709acColin Walters  vfprintf (stderr, message, args);
105e359834f28b0580e7b79a057614d4e3592709acColin Walters  va_end (args);
115e359834f28b0580e7b79a057614d4e3592709acColin Walters  exit (1);
125e359834f28b0580e7b79a057614d4e3592709acColin Walters}
135e359834f28b0580e7b79a057614d4e3592709acColin Walters
145e359834f28b0580e7b79a057614d4e3592709acColin Waltersstatic DBusHandlerResult
155e359834f28b0580e7b79a057614d4e3592709acColin Waltersfilter_private_message (DBusConnection     *connection,
165e359834f28b0580e7b79a057614d4e3592709acColin Walters                        DBusMessage        *message,
175e359834f28b0580e7b79a057614d4e3592709acColin Walters                        void               *user_data)
185e359834f28b0580e7b79a057614d4e3592709acColin Walters{
195e359834f28b0580e7b79a057614d4e3592709acColin Walters  if (dbus_message_is_signal (message,
205e359834f28b0580e7b79a057614d4e3592709acColin Walters                              DBUS_INTERFACE_LOCAL,
215e359834f28b0580e7b79a057614d4e3592709acColin Walters                              "Disconnected"))
225e359834f28b0580e7b79a057614d4e3592709acColin Walters    {
235e359834f28b0580e7b79a057614d4e3592709acColin Walters       DBusLoop *loop = user_data;
245e359834f28b0580e7b79a057614d4e3592709acColin Walters       _dbus_loop_quit (loop);
255e359834f28b0580e7b79a057614d4e3592709acColin Walters       return DBUS_HANDLER_RESULT_HANDLED;
265e359834f28b0580e7b79a057614d4e3592709acColin Walters    }
275e359834f28b0580e7b79a057614d4e3592709acColin Walters  return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
285e359834f28b0580e7b79a057614d4e3592709acColin Walters}
295e359834f28b0580e7b79a057614d4e3592709acColin Walters
305e359834f28b0580e7b79a057614d4e3592709acColin Waltersstatic void
310e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Waltersopen_shutdown_private_connection (dbus_bool_t use_guid)
325e359834f28b0580e7b79a057614d4e3592709acColin Walters{
335e359834f28b0580e7b79a057614d4e3592709acColin Walters  DBusError error;
345e359834f28b0580e7b79a057614d4e3592709acColin Walters  DBusLoop *loop;
355e359834f28b0580e7b79a057614d4e3592709acColin Walters  DBusConnection *session;
365e359834f28b0580e7b79a057614d4e3592709acColin Walters  DBusMessage *msg;
375e359834f28b0580e7b79a057614d4e3592709acColin Walters  DBusMessage *reply;
385e359834f28b0580e7b79a057614d4e3592709acColin Walters  DBusConnection *privconn;
390e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  char *addr;
400e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  char *comma;
415e359834f28b0580e7b79a057614d4e3592709acColin Walters
425e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_error_init (&error);
435e359834f28b0580e7b79a057614d4e3592709acColin Walters
445e359834f28b0580e7b79a057614d4e3592709acColin Walters  loop = _dbus_loop_new ();
455e359834f28b0580e7b79a057614d4e3592709acColin Walters
465e359834f28b0580e7b79a057614d4e3592709acColin Walters  session = dbus_bus_get (DBUS_BUS_SESSION, &error);
475e359834f28b0580e7b79a057614d4e3592709acColin Walters  if (!session)
485e359834f28b0580e7b79a057614d4e3592709acColin Walters    die ("couldn't access session bus\n");
495e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_connection_set_exit_on_disconnect (session, FALSE);
504cbc30465e4fba8026240945cac7a651fda5dca3Scott James Remnant  msg = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuite.PrivServer",
515e359834f28b0580e7b79a057614d4e3592709acColin Walters                                      "/",
524cbc30465e4fba8026240945cac7a651fda5dca3Scott James Remnant                                      "org.freedesktop.DBus.TestSuite.PrivServer",
535e359834f28b0580e7b79a057614d4e3592709acColin Walters                                      "GetPrivateAddress");
545e359834f28b0580e7b79a057614d4e3592709acColin Walters  if (!(reply = dbus_connection_send_with_reply_and_block (session, msg, -1, &error)))
555e359834f28b0580e7b79a057614d4e3592709acColin Walters    die ("couldn't send message: %s\n", error.message);
565e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_message_unref (msg);
575e359834f28b0580e7b79a057614d4e3592709acColin Walters  if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &addr, DBUS_TYPE_INVALID))
585e359834f28b0580e7b79a057614d4e3592709acColin Walters    die ("couldn't parse message replym\n");
595e359834f28b0580e7b79a057614d4e3592709acColin Walters  printf ("got private temp address %s\n", addr);
600e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  addr = strdup (addr);
610e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  if (!use_guid)
620e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters    {
630e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters      char *comma = strrchr (addr, ',');
640e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters      if (comma)
650e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters        *comma = '\0';
660e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters    }
675e359834f28b0580e7b79a057614d4e3592709acColin Walters  privconn = dbus_connection_open (addr, &error);
680e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  free (addr);
695e359834f28b0580e7b79a057614d4e3592709acColin Walters  if (!privconn)
705e359834f28b0580e7b79a057614d4e3592709acColin Walters    die ("couldn't connect to server direct connection: %s\n", error.message);
715e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_message_unref (reply);
725e359834f28b0580e7b79a057614d4e3592709acColin Walters
735e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_connection_set_exit_on_disconnect (privconn, FALSE);
745e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_connection_add_filter (privconn, filter_private_message, loop, NULL);
755e359834f28b0580e7b79a057614d4e3592709acColin Walters  test_connection_setup (loop, privconn);
765e359834f28b0580e7b79a057614d4e3592709acColin Walters
774cbc30465e4fba8026240945cac7a651fda5dca3Scott James Remnant  msg = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuite.PrivServer",
785e359834f28b0580e7b79a057614d4e3592709acColin Walters                                      "/",
794cbc30465e4fba8026240945cac7a651fda5dca3Scott James Remnant                                      "org.freedesktop.DBus.TestSuite.PrivServer",
805e359834f28b0580e7b79a057614d4e3592709acColin Walters                                      "Quit");
815e359834f28b0580e7b79a057614d4e3592709acColin Walters  if (!dbus_connection_send (session, msg, NULL))
825e359834f28b0580e7b79a057614d4e3592709acColin Walters    die ("couldn't send Quit message\n");
835e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_message_unref (msg);
845e359834f28b0580e7b79a057614d4e3592709acColin Walters
855e359834f28b0580e7b79a057614d4e3592709acColin Walters  _dbus_loop_run (loop);
865e359834f28b0580e7b79a057614d4e3592709acColin Walters
875e359834f28b0580e7b79a057614d4e3592709acColin Walters  test_connection_shutdown (loop, session);
885e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_connection_unref (session);
895e359834f28b0580e7b79a057614d4e3592709acColin Walters
905e359834f28b0580e7b79a057614d4e3592709acColin Walters  test_connection_shutdown (loop, privconn);
915e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_connection_remove_filter (privconn, filter_private_message, loop);
925e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_connection_unref (privconn);
935e359834f28b0580e7b79a057614d4e3592709acColin Walters
945e359834f28b0580e7b79a057614d4e3592709acColin Walters  _dbus_loop_unref (loop);
955e359834f28b0580e7b79a057614d4e3592709acColin Walters}
965e359834f28b0580e7b79a057614d4e3592709acColin Walters
975e359834f28b0580e7b79a057614d4e3592709acColin Waltersint
985e359834f28b0580e7b79a057614d4e3592709acColin Waltersmain (int argc, char *argv[])
995e359834f28b0580e7b79a057614d4e3592709acColin Walters{
1000e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  open_shutdown_private_connection (TRUE);
1010e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters
1020e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  dbus_shutdown ();
1030e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters
1040e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  open_shutdown_private_connection (TRUE);
1050e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters
1060e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  dbus_shutdown ();
1070e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters
1080e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  open_shutdown_private_connection (FALSE);
1095e359834f28b0580e7b79a057614d4e3592709acColin Walters
1105e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_shutdown ();
1115e359834f28b0580e7b79a057614d4e3592709acColin Walters
1120e2a099b55a50b9b5a0f948e134b5a88e2924262Colin Walters  open_shutdown_private_connection (FALSE);
1135e359834f28b0580e7b79a057614d4e3592709acColin Walters
1145e359834f28b0580e7b79a057614d4e3592709acColin Walters  dbus_shutdown ();
1155e359834f28b0580e7b79a057614d4e3592709acColin Walters
1165e359834f28b0580e7b79a057614d4e3592709acColin Walters  return 0;
1175e359834f28b0580e7b79a057614d4e3592709acColin Walters}
118