• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <config.h>
2 #include "../test-utils.h"
3 
4 static void
die(const char * message,...)5 die (const char *message, ...)
6 {
7   va_list args;
8   va_start (args, message);
9   vfprintf (stderr, message, args);
10   va_end (args);
11   exit (1);
12 }
13 
14 static DBusHandlerResult
filter_private_message(DBusConnection * connection,DBusMessage * message,void * user_data)15 filter_private_message (DBusConnection     *connection,
16                         DBusMessage        *message,
17                         void               *user_data)
18 {
19   if (dbus_message_is_signal (message,
20                               DBUS_INTERFACE_LOCAL,
21                               "Disconnected"))
22     {
23        DBusLoop *loop = user_data;
24        _dbus_loop_quit (loop);
25        return DBUS_HANDLER_RESULT_HANDLED;
26     }
27   return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
28 }
29 
30 static void
open_shutdown_private_connection(dbus_bool_t use_guid)31 open_shutdown_private_connection (dbus_bool_t use_guid)
32 {
33   DBusError error;
34   DBusLoop *loop;
35   DBusConnection *session;
36   DBusMessage *msg;
37   DBusMessage *reply;
38   DBusConnection *privconn;
39   char *addr;
40   char *comma;
41 
42   dbus_error_init (&error);
43 
44   loop = _dbus_loop_new ();
45 
46   session = dbus_bus_get (DBUS_BUS_SESSION, &error);
47   if (!session)
48     die ("couldn't access session bus\n");
49   dbus_connection_set_exit_on_disconnect (session, FALSE);
50   msg = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuite.PrivServer",
51                                       "/",
52                                       "org.freedesktop.DBus.TestSuite.PrivServer",
53                                       "GetPrivateAddress");
54   if (!(reply = dbus_connection_send_with_reply_and_block (session, msg, -1, &error)))
55     die ("couldn't send message: %s\n", error.message);
56   dbus_message_unref (msg);
57   if (!dbus_message_get_args (reply, &error, DBUS_TYPE_STRING, &addr, DBUS_TYPE_INVALID))
58     die ("couldn't parse message replym\n");
59   printf ("got private temp address %s\n", addr);
60   addr = strdup (addr);
61   if (!use_guid)
62     {
63       char *comma = strrchr (addr, ',');
64       if (comma)
65         *comma = '\0';
66     }
67   privconn = dbus_connection_open (addr, &error);
68   free (addr);
69   if (!privconn)
70     die ("couldn't connect to server direct connection: %s\n", error.message);
71   dbus_message_unref (reply);
72 
73   dbus_connection_set_exit_on_disconnect (privconn, FALSE);
74   dbus_connection_add_filter (privconn, filter_private_message, loop, NULL);
75   test_connection_setup (loop, privconn);
76 
77   msg = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuite.PrivServer",
78                                       "/",
79                                       "org.freedesktop.DBus.TestSuite.PrivServer",
80                                       "Quit");
81   if (!dbus_connection_send (session, msg, NULL))
82     die ("couldn't send Quit message\n");
83   dbus_message_unref (msg);
84 
85   _dbus_loop_run (loop);
86 
87   test_connection_shutdown (loop, session);
88   dbus_connection_unref (session);
89 
90   test_connection_shutdown (loop, privconn);
91   dbus_connection_remove_filter (privconn, filter_private_message, loop);
92   dbus_connection_unref (privconn);
93 
94   _dbus_loop_unref (loop);
95 }
96 
97 int
main(int argc,char * argv[])98 main (int argc, char *argv[])
99 {
100   open_shutdown_private_connection (TRUE);
101 
102   dbus_shutdown ();
103 
104   open_shutdown_private_connection (TRUE);
105 
106   dbus_shutdown ();
107 
108   open_shutdown_private_connection (FALSE);
109 
110   dbus_shutdown ();
111 
112   open_shutdown_private_connection (FALSE);
113 
114   dbus_shutdown ();
115 
116   return 0;
117 }
118