bus: Assign a serial number for messages from the driver
[platform/upstream/dbus.git] / test / test-shell-service.c
1 #include <config.h>
2
3 #include "test-utils.h"
4
5 static DBusLoop *loop;
6 static dbus_bool_t already_quit = FALSE;
7 static const char* echo_path = "/org/freedesktop/TestSuite";
8
9 typedef struct
10 {
11   int argc;
12   char **argv;
13 } EchoData;
14
15 static void
16 quit (void)
17 {
18   if (!already_quit)
19     {
20       _dbus_loop_quit (loop);
21       already_quit = TRUE;
22     }
23 }
24
25 static void die (const char *message) _DBUS_GNUC_NORETURN;
26
27 static void
28 die (const char *message)
29 {
30   fprintf (stderr, "*** test-service: %s", message);
31   exit (1);
32 }
33
34 static DBusHandlerResult
35 handle_echo (DBusConnection     *connection,
36              DBusMessage        *message)
37 {
38   DBusError error;
39   DBusMessage *reply;
40   DBusMessageIter iter;
41   int i;
42   EchoData *d;
43
44   _dbus_verbose ("sending reply to Echo method\n");
45
46   if (!dbus_connection_get_object_path_data (connection, echo_path, (void **)&d))
47       die ("No memory");
48
49
50   dbus_error_init (&error);
51
52   reply = dbus_message_new_method_return (message);
53   if (reply == NULL)
54     die ("No memory\n");
55   
56   dbus_message_iter_init_append (reply, &iter);
57   for (i = 0; i < d->argc; ++i)
58     if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &(d->argv[i])))
59       die ("No memory\n");
60
61   if (!dbus_connection_send (connection, reply, NULL))
62     die ("No memory\n");
63
64   fprintf (stderr, "Shell echo service echoed the command line\n");
65   
66   dbus_message_unref (reply);
67     
68   return DBUS_HANDLER_RESULT_HANDLED;
69 }
70
71 static void
72 path_unregistered_func (DBusConnection  *connection,
73                         void            *user_data)
74 {
75   /* connection was finalized */
76 }
77
78 static DBusHandlerResult
79 path_message_func (DBusConnection  *connection,
80                    DBusMessage     *message,
81                    void            *user_data)
82 {
83   if (dbus_message_is_method_call (message,
84                                    "org.freedesktop.TestSuite",
85                                    "Echo"))
86     return handle_echo (connection, message);
87   else if (dbus_message_is_method_call (message,
88                                         "org.freedesktop.TestSuite",
89                                         "Exit"))
90     {
91       quit ();
92       return DBUS_HANDLER_RESULT_HANDLED;
93     }
94   else
95     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
96 }
97
98 static DBusObjectPathVTable
99 echo_vtable = {
100   path_unregistered_func,
101   path_message_func,
102   NULL,
103 };
104
105 static DBusHandlerResult
106 filter_func (DBusConnection     *connection,
107              DBusMessage        *message,
108              void               *user_data)
109 {
110   if (dbus_message_is_signal (message,
111                               DBUS_INTERFACE_LOCAL,
112                               "Disconnected"))
113     {
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   if (result != DBUS_REQUEST_NAME_REPLY_PRIMARY_OWNER)
182     {
183       fprintf (stderr, "Unable to acquire service: code %d\n", result);
184       _dbus_verbose ("*** Failed to acquire service: %d\n", result);
185       exit (1);
186     }
187
188   _dbus_verbose ("*** Test service entering main loop\n");
189   _dbus_loop_run (loop);
190
191   test_connection_shutdown (loop, connection);
192
193   dbus_connection_remove_filter (connection, filter_func, NULL);
194   
195   dbus_connection_unref (connection);
196
197   _dbus_loop_unref (loop);
198   loop = NULL;
199   
200   dbus_shutdown ();
201
202   _dbus_verbose ("*** Test service exiting\n");
203   
204   return 0;
205 }