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