77b8b3caae98c8c5961bc98634d467ca42018dce
[platform/upstream/dbus.git] / test / test-service.c
1
2 #include "test-utils.h"
3
4 static DBusLoop *loop;
5 static dbus_bool_t already_quit = FALSE;
6 static dbus_bool_t hello_from_self_reply_recived = FALSE;
7
8 static void
9 quit (void)
10 {
11   if (!already_quit)
12     {
13       _dbus_loop_quit (loop);
14       already_quit = TRUE;
15     }
16 }
17
18 static void
19 die (const char *message)
20 {
21   fprintf (stderr, "*** test-service: %s", message);
22   exit (1);
23 }
24
25 static void
26 check_hello_from_self_reply (DBusPendingCall *pcall, 
27                              void *user_data)
28 {
29   DBusMessage *reply;
30   DBusMessage *echo_message, *echo_reply;
31   DBusError error;
32   DBusConnection *connection;
33   
34   int type;
35   
36   dbus_error_init (&error);
37  
38   connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
39   if (connection == NULL)
40     {
41       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
42                error.message);
43       dbus_error_free (&error);
44       die("no memory");
45     }
46
47   
48   echo_message = (DBusMessage *)user_data;
49     
50   reply = dbus_pending_call_steal_reply (pcall);
51     
52   type = dbus_message_get_type (reply);
53     
54   if (type == DBUS_MESSAGE_TYPE_METHOD_RETURN)
55     {
56       const char *s;
57       printf ("Reply from HelloFromSelf recived\n");
58      
59       if (!dbus_message_get_args (echo_message,
60                               &error,
61                               DBUS_TYPE_STRING, &s,
62                               DBUS_TYPE_INVALID))
63         {
64             echo_reply = dbus_message_new_error (echo_message,
65                                       error.name,
66                                       error.message);
67
68             if (echo_reply == NULL)
69               die ("No memory\n");
70
71         } 
72       else
73         {  
74           echo_reply = dbus_message_new_method_return (echo_message);
75           if (echo_reply == NULL)
76             die ("No memory\n");
77   
78           if (!dbus_message_append_args (echo_reply,
79                                  DBUS_TYPE_STRING, &s,
80                                  DBUS_TYPE_INVALID))
81             die ("No memory");
82         }
83         
84       if (!dbus_connection_send (connection, echo_reply, NULL))
85         die ("No memory\n");
86       
87       dbus_message_unref (echo_reply);
88     }
89   else if (type == DBUS_MESSAGE_TYPE_ERROR)
90     {
91       dbus_set_error_from_message (&error, reply);
92       printf ("Error type in reply: %s\n", error.message);
93
94       if (strcmp (error.name, DBUS_ERROR_NO_MEMORY) != 0)
95         {
96             echo_reply = dbus_message_new_error (echo_reply,
97                                       error.name,
98                                       error.message);
99
100             if (echo_reply == NULL)
101               die ("No memory\n");
102
103             if (!dbus_connection_send (connection, echo_reply, NULL))
104               die ("No memory\n");
105
106             dbus_message_unref (echo_reply);
107         }
108       dbus_error_free (&error);
109     }
110   else
111      _dbus_assert_not_reached ("Unexpected message recived\n");
112
113   hello_from_self_reply_recived = TRUE;
114   
115   dbus_message_unref (reply);
116   dbus_message_unref (echo_message);
117   dbus_pending_call_unref (pcall);
118 }
119
120 static DBusHandlerResult
121 handle_run_hello_from_self (DBusConnection     *connection,
122                                                DBusMessage        *message)
123 {
124   DBusError error;
125   DBusMessage *reply, *self_message;
126   DBusPendingCall *pcall;
127   char *s;
128
129   _dbus_verbose ("sending reply to Echo method\n");
130   
131   dbus_error_init (&error);
132   
133   if (!dbus_message_get_args (message,
134                               &error,
135                               DBUS_TYPE_STRING, &s,
136                               DBUS_TYPE_INVALID))
137     {
138       reply = dbus_message_new_error (message,
139                                       error.name,
140                                       error.message);
141
142       if (reply == NULL)
143         die ("No memory\n");
144
145       if (!dbus_connection_send (connection, reply, NULL))
146         die ("No memory\n");
147
148       dbus_message_unref (reply);
149
150       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
151     }
152     printf ("Sending HelloFromSelf\n");
153
154  _dbus_verbose ("*** Sending message to self\n");
155  self_message = dbus_message_new_method_call ("org.freedesktop.DBus.TestSuiteEchoService",
156                                           "/org/freedesktop/TestSuite",
157                                           "org.freedesktop.TestSuite",
158                                           "HelloFromSelf");
159   
160   if (self_message == NULL)
161     die ("No memory");
162   
163   if (!dbus_connection_send_with_reply (connection, self_message, &pcall, -1))
164     die("No memory");
165   
166   dbus_message_ref (message);
167   if (!dbus_pending_call_set_notify (pcall, check_hello_from_self_reply, (void *)message, NULL))
168     die("No memory");
169     
170   printf ("Sent HelloFromSelf\n");
171   return DBUS_HANDLER_RESULT_HANDLED;
172 }
173
174 static DBusHandlerResult
175 handle_echo (DBusConnection     *connection,
176              DBusMessage        *message)
177 {
178   DBusError error;
179   DBusMessage *reply;
180   char *s;
181
182   _dbus_verbose ("sending reply to Echo method\n");
183   
184   dbus_error_init (&error);
185   
186   if (!dbus_message_get_args (message,
187                               &error,
188                               DBUS_TYPE_STRING, &s,
189                               DBUS_TYPE_INVALID))
190     {
191       reply = dbus_message_new_error (message,
192                                       error.name,
193                                       error.message);
194
195       if (reply == NULL)
196         die ("No memory\n");
197
198       if (!dbus_connection_send (connection, reply, NULL))
199         die ("No memory\n");
200
201       dbus_message_unref (reply);
202
203       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
204     }
205
206   reply = dbus_message_new_method_return (message);
207   if (reply == NULL)
208     die ("No memory\n");
209   
210   if (!dbus_message_append_args (reply,
211                                  DBUS_TYPE_STRING, &s,
212                                  DBUS_TYPE_INVALID))
213     die ("No memory");
214   
215   if (!dbus_connection_send (connection, reply, NULL))
216     die ("No memory\n");
217
218   fprintf (stderr, "Echo service echoed string: \"%s\"\n", s);
219   
220   dbus_message_unref (reply);
221     
222   return DBUS_HANDLER_RESULT_HANDLED;
223 }
224
225 static void
226 path_unregistered_func (DBusConnection  *connection,
227                         void            *user_data)
228 {
229   /* connection was finalized */
230 }
231
232 static DBusHandlerResult
233 path_message_func (DBusConnection  *connection,
234                    DBusMessage     *message,
235                    void            *user_data)
236 {
237   if (dbus_message_is_method_call (message,
238                                    "org.freedesktop.TestSuite",
239                                    "Echo"))
240     return handle_echo (connection, message);
241   else if (dbus_message_is_method_call (message,
242                                         "org.freedesktop.TestSuite",
243                                         "Exit"))
244     {
245       dbus_connection_close (connection);
246       quit ();
247       return DBUS_HANDLER_RESULT_HANDLED;
248     }
249   else if (dbus_message_is_method_call (message,
250                                         "org.freedesktop.TestSuite",
251                                         "EmitFoo"))
252     {
253       /* Emit the Foo signal */
254       DBusMessage *signal;
255       double v_DOUBLE;
256
257       _dbus_verbose ("emitting signal Foo\n");
258       
259       signal = dbus_message_new_signal ("/org/freedesktop/TestSuite",
260                                         "org.freedesktop.TestSuite",
261                                         "Foo");
262       if (signal == NULL)
263         die ("No memory\n");
264
265       v_DOUBLE = 42.6;
266       if (!dbus_message_append_args (signal,
267                                      DBUS_TYPE_DOUBLE, &v_DOUBLE,
268                                      DBUS_TYPE_INVALID))
269         die ("No memory");
270   
271       if (!dbus_connection_send (connection, signal, NULL))
272         die ("No memory\n");
273       
274       return DBUS_HANDLER_RESULT_HANDLED;
275     }
276     
277   else if (dbus_message_is_method_call (message,
278                                    "org.freedesktop.TestSuite",
279                                    "RunHelloFromSelf"))
280     {
281       return handle_run_hello_from_self (connection, message);
282     }
283   else if (dbus_message_is_method_call (message,
284                                         "org.freedesktop.TestSuite",
285                                         "HelloFromSelf"))
286     {
287         DBusMessage *reply;
288         printf ("Recived the HelloFromSelf message\n");
289         
290         reply = dbus_message_new_method_return (message);
291         if (reply == NULL)
292           die ("No memory");
293         
294         if (!dbus_connection_send (connection, reply, NULL))
295           die ("No memory");
296     }
297   else
298     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
299 }
300
301 static DBusObjectPathVTable
302 echo_vtable = {
303   path_unregistered_func,
304   path_message_func,
305   NULL,
306 };
307
308
309 static const char* echo_path = "/org/freedesktop/TestSuite" ;
310
311 static DBusHandlerResult
312 filter_func (DBusConnection     *connection,
313              DBusMessage        *message,
314              void               *user_data)
315 {
316   if (dbus_message_is_signal (message,
317                               DBUS_INTERFACE_LOCAL,
318                               "Disconnected"))
319     {
320       dbus_connection_close (connection);
321       quit ();
322       return DBUS_HANDLER_RESULT_HANDLED;
323     }
324   else
325     {
326       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
327     }
328 }
329
330 int
331 main (int    argc,
332       char **argv)
333 {
334   DBusError error;
335   int result;
336   DBusConnection *connection;
337   
338   dbus_error_init (&error);
339   connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
340   if (connection == NULL)
341     {
342       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
343                error.message);
344       dbus_error_free (&error);
345       return 1;
346     }
347
348   loop = _dbus_loop_new ();
349   if (loop == NULL)
350     die ("No memory\n");
351   
352   if (!test_connection_setup (loop, connection))
353     die ("No memory\n");
354
355   if (!dbus_connection_add_filter (connection,
356                                    filter_func, NULL, NULL))
357     die ("No memory");
358
359   if (!dbus_connection_register_object_path (connection,
360                                              echo_path,
361                                              &echo_vtable,
362                                              (void*) 0xdeadbeef))
363     die ("No memory");
364
365   {
366     void *d;
367     if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
368       die ("No memory");
369     if (d != (void*) 0xdeadbeef)
370       die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
371   }
372   
373   result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteEchoService",
374                                   0, &error);
375   if (dbus_error_is_set (&error))
376     {
377       fprintf (stderr, "Error %s\n", error.message);
378       _dbus_verbose ("*** Failed to acquire service: %s\n",
379                      error.message);
380       dbus_error_free (&error);
381       exit (1);
382     }
383   
384   _dbus_verbose ("*** Test service entering main loop\n");
385   _dbus_loop_run (loop);
386   
387   test_connection_shutdown (loop, connection);
388
389   dbus_connection_remove_filter (connection, filter_func, NULL);
390   
391   dbus_connection_unref (connection);
392
393   _dbus_loop_unref (loop);
394   loop = NULL;
395   
396   dbus_shutdown ();
397
398   _dbus_verbose ("*** Test service exiting\n");
399   
400   return 0;
401 }