test-shell-service.c revision dbecdeabb20e0ce11121819c63373f0afba57c58
1dbecdeabb20e0ce11121819c63373f0afba57c58Marcus Brinkmann#include <config.h>
2075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
3075945f611290f2b9db9a4ed6cf5433f2fd85785John (J#include "test-utils.h"
4075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
5075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jstatic DBusLoop *loop;
6075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jstatic dbus_bool_t already_quit = FALSE;
7075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jstatic const char* echo_path = "/org/freedesktop/TestSuite";
8075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
9075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jtypedef struct
10075945f611290f2b9db9a4ed6cf5433f2fd85785John (J{
11075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  int argc;
12075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  char **argv;
13075945f611290f2b9db9a4ed6cf5433f2fd85785John (J} EchoData;
14075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
15075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jstatic void
16075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jquit (void)
17075945f611290f2b9db9a4ed6cf5433f2fd85785John (J{
18075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (!already_quit)
19075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    {
20075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      _dbus_loop_quit (loop);
21075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      already_quit = TRUE;
22075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    }
23075945f611290f2b9db9a4ed6cf5433f2fd85785John (J}
24075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
25075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jstatic void
26075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jdie (const char *message)
27075945f611290f2b9db9a4ed6cf5433f2fd85785John (J{
28075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  fprintf (stderr, "*** test-service: %s", message);
29075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  exit (1);
30075945f611290f2b9db9a4ed6cf5433f2fd85785John (J}
31075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
32075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jstatic DBusHandlerResult
33075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jhandle_echo (DBusConnection     *connection,
34075945f611290f2b9db9a4ed6cf5433f2fd85785John (J             DBusMessage        *message)
35075945f611290f2b9db9a4ed6cf5433f2fd85785John (J{
36075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  DBusError error;
37075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  DBusMessage *reply;
38075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  DBusMessageIter iter;
39075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  int i;
40075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  EchoData *d;
41075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
42075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  _dbus_verbose ("sending reply to Echo method\n");
43075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
44c1091cbbd2477699dc16f8c8e3d15fea2f68d603John (J  if (!dbus_connection_get_object_path_data (connection, echo_path, (void **)&d))
45075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      die ("No memory");
46075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
47075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
48075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  dbus_error_init (&error);
49075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
50075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  reply = dbus_message_new_method_return (message);
51075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (reply == NULL)
52075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    die ("No memory\n");
53075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
54075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  dbus_message_iter_init_append (reply, &iter);
55075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  for (i = 0; i < d->argc; ++i)
56075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &(d->argv[i])))
57075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      die ("No memory\n");
58075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
59075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (!dbus_connection_send (connection, reply, NULL))
60075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    die ("No memory\n");
61075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
62075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  fprintf (stderr, "Shell echo service echoed the command line\n");
63075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
64075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  dbus_message_unref (reply);
65075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
66075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  return DBUS_HANDLER_RESULT_HANDLED;
67075945f611290f2b9db9a4ed6cf5433f2fd85785John (J}
68075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
69075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jstatic void
70075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jpath_unregistered_func (DBusConnection  *connection,
71075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                        void            *user_data)
72075945f611290f2b9db9a4ed6cf5433f2fd85785John (J{
73075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  /* connection was finalized */
74075945f611290f2b9db9a4ed6cf5433f2fd85785John (J}
75075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
76075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jstatic DBusHandlerResult
77075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jpath_message_func (DBusConnection  *connection,
78075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                   DBusMessage     *message,
79075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                   void            *user_data)
80075945f611290f2b9db9a4ed6cf5433f2fd85785John (J{
81075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (dbus_message_is_method_call (message,
82075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                                   "org.freedesktop.TestSuite",
83075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                                   "Echo"))
84075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    return handle_echo (connection, message);
85075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  else if (dbus_message_is_method_call (message,
86075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                                        "org.freedesktop.TestSuite",
87075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                                        "Exit"))
88075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    {
89075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      quit ();
90075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      return DBUS_HANDLER_RESULT_HANDLED;
91075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    }
92075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  else
93075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
94075945f611290f2b9db9a4ed6cf5433f2fd85785John (J}
95075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
96075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jstatic DBusObjectPathVTable
97075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jecho_vtable = {
98075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  path_unregistered_func,
99075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  path_message_func,
100075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  NULL,
101075945f611290f2b9db9a4ed6cf5433f2fd85785John (J};
102075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
103075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jstatic DBusHandlerResult
104075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jfilter_func (DBusConnection     *connection,
105075945f611290f2b9db9a4ed6cf5433f2fd85785John (J             DBusMessage        *message,
106075945f611290f2b9db9a4ed6cf5433f2fd85785John (J             void               *user_data)
107075945f611290f2b9db9a4ed6cf5433f2fd85785John (J{
108075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (dbus_message_is_signal (message,
109075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                              DBUS_INTERFACE_LOCAL,
110075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                              "Disconnected"))
111075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    {
112075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      quit ();
113075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      return DBUS_HANDLER_RESULT_HANDLED;
114075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    }
115075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  else
116075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    {
117075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
118075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    }
119075945f611290f2b9db9a4ed6cf5433f2fd85785John (J}
120075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
121075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jint
122075945f611290f2b9db9a4ed6cf5433f2fd85785John (Jmain (int    argc,
123075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      char **argv)
124075945f611290f2b9db9a4ed6cf5433f2fd85785John (J{
125075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  DBusConnection *connection;
126075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  DBusError error;
127075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  EchoData echo_data;
128075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  int result;
129075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
130075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  echo_data.argc = argc;
131075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  echo_data.argv = argv;
132075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
133075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  dbus_error_init (&error);
134075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
135075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (connection == NULL)
136075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    {
137075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
138075945f611290f2b9db9a4ed6cf5433f2fd85785John (J               error.message);
139075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      dbus_error_free (&error);
140075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      return 1;
141075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    }
142075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
143075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  loop = _dbus_loop_new ();
144075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (loop == NULL)
145075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    die ("No memory\n");
146075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
147075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (!test_connection_setup (loop, connection))
148075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    die ("No memory\n");
149075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
150075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (!dbus_connection_add_filter (connection,
151075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                                   filter_func, NULL, NULL))
152075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    die ("No memory");
153075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
154075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (!dbus_connection_register_object_path (connection,
155075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                                             echo_path,
156075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                                             &echo_vtable,
157075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                                             (void*) &echo_data))
158075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    die ("No memory");
159075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
160075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  {
161075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    void *d;
162075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
163075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      die ("No memory");
164075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    if (d != (void*) &echo_data)
165075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
166075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  }
167075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
168075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess",
169075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                                  0, &error);
170075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  if (dbus_error_is_set (&error))
171075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    {
172075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      fprintf (stderr, "Error %s\n", error.message);
173075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      _dbus_verbose ("*** Failed to acquire service: %s\n",
174075945f611290f2b9db9a4ed6cf5433f2fd85785John (J                     error.message);
175075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      dbus_error_free (&error);
176075945f611290f2b9db9a4ed6cf5433f2fd85785John (J      exit (1);
177075945f611290f2b9db9a4ed6cf5433f2fd85785John (J    }
178075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
179075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  _dbus_verbose ("*** Test service entering main loop\n");
180075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  _dbus_loop_run (loop);
181075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
182075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  test_connection_shutdown (loop, connection);
183075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
184075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  dbus_connection_remove_filter (connection, filter_func, NULL);
185075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
186075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  dbus_connection_unref (connection);
187075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
188075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  _dbus_loop_unref (loop);
189075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  loop = NULL;
190075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
191075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  dbus_shutdown ();
192075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
193075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  _dbus_verbose ("*** Test service exiting\n");
194075945f611290f2b9db9a4ed6cf5433f2fd85785John (J
195075945f611290f2b9db9a4ed6cf5433f2fd85785John (J  return 0;
196075945f611290f2b9db9a4ed6cf5433f2fd85785John (J}
197