Checking in Rodrigo's patch along with my fixes to the patch
[platform/upstream/dbus.git] / test / test-shell-service.c
1
2 #include "test-utils.h"
3
4 static DBusLoop *loop;
5 static dbus_bool_t already_quit = FALSE;
6 static const char* echo_path = "/org/freedesktop/TestSuite";
7
8 typedef struct
9 {
10   int argc;
11   char **argv;
12 } EchoData;
13
14 static void
15 quit (void)
16 {
17   if (!already_quit)
18     {
19       _dbus_loop_quit (loop);
20       already_quit = TRUE;
21     }
22 }
23
24 static void
25 die (const char *message)
26 {
27   fprintf (stderr, "*** test-service: %s", message);
28   exit (1);
29 }
30
31 static DBusHandlerResult
32 handle_echo (DBusConnection     *connection,
33              DBusMessage        *message)
34 {
35   DBusError error;
36   DBusMessage *reply;
37   DBusMessageIter iter;
38   int i;
39   char *s;
40   EchoData *d;
41
42   _dbus_verbose ("sending reply to Echo method\n");
43
44   if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
45       die ("No memory");
46
47
48   dbus_error_init (&error);
49
50   reply = dbus_message_new_method_return (message);
51   if (reply == NULL)
52     die ("No memory\n");
53   
54   dbus_message_iter_init_append (reply, &iter);
55   for (i = 0; i < d->argc; ++i)
56     if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &(d->argv[i])))
57       die ("No memory\n");
58
59   if (!dbus_connection_send (connection, reply, NULL))
60     die ("No memory\n");
61
62   fprintf (stderr, "Shell echo service echoed the command line\n");
63   
64   dbus_message_unref (reply);
65     
66   return DBUS_HANDLER_RESULT_HANDLED;
67 }
68
69 static void
70 path_unregistered_func (DBusConnection  *connection,
71                         void            *user_data)
72 {
73   /* connection was finalized */
74 }
75
76 static DBusHandlerResult
77 path_message_func (DBusConnection  *connection,
78                    DBusMessage     *message,
79                    void            *user_data)
80 {
81   if (dbus_message_is_method_call (message,
82                                    "org.freedesktop.TestSuite",
83                                    "Echo"))
84     return handle_echo (connection, message);
85   else if (dbus_message_is_method_call (message,
86                                         "org.freedesktop.TestSuite",
87                                         "Exit"))
88     {
89       dbus_connection_close (connection);
90       quit ();
91       return DBUS_HANDLER_RESULT_HANDLED;
92     }
93   else
94     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
95 }
96
97 static DBusObjectPathVTable
98 echo_vtable = {
99   path_unregistered_func,
100   path_message_func,
101   NULL,
102 };
103
104 static DBusHandlerResult
105 filter_func (DBusConnection     *connection,
106              DBusMessage        *message,
107              void               *user_data)
108 {
109   if (dbus_message_is_signal (message,
110                               DBUS_INTERFACE_LOCAL,
111                               "Disconnected"))
112     {
113       dbus_connection_close (connection);
114       quit ();
115       return DBUS_HANDLER_RESULT_HANDLED;
116     }
117   else
118     {
119       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
120     }
121 }
122
123 int
124 main (int    argc,
125       char **argv)
126 {
127   DBusConnection *connection;
128   DBusError error;
129   EchoData echo_data;
130   int result;
131   
132   echo_data.argc = argc;
133   echo_data.argv = argv;
134   
135   dbus_error_init (&error);
136   connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
137   if (connection == NULL)
138     {
139       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
140                error.message);
141       dbus_error_free (&error);
142       return 1;
143     }
144
145   loop = _dbus_loop_new ();
146   if (loop == NULL)
147     die ("No memory\n");
148   
149   if (!test_connection_setup (loop, connection))
150     die ("No memory\n");
151
152   if (!dbus_connection_add_filter (connection,
153                                    filter_func, NULL, NULL))
154     die ("No memory");
155
156   if (!dbus_connection_register_object_path (connection,
157                                              echo_path,
158                                              &echo_vtable,
159                                              (void*) &echo_data))
160     die ("No memory");
161
162   {
163     void *d;
164     if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
165       die ("No memory");
166     if (d != (void*) &echo_data)
167       die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
168   }
169   
170   result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess",
171                                   0, &error);
172   if (dbus_error_is_set (&error))
173     {
174       fprintf (stderr, "Error %s\n", error.message);
175       _dbus_verbose ("*** Failed to acquire service: %s\n",
176                      error.message);
177       dbus_error_free (&error);
178       exit (1);
179     }
180
181   _dbus_verbose ("*** Test service entering main loop\n");
182   _dbus_loop_run (loop);
183
184   test_connection_shutdown (loop, connection);
185
186   dbus_connection_remove_filter (connection, filter_func, NULL);
187   
188   dbus_connection_unref (connection);
189
190   _dbus_loop_unref (loop);
191   loop = NULL;
192   
193   dbus_shutdown ();
194
195   _dbus_verbose ("*** Test service exiting\n");
196   
197   return 0;
198 }