Patches by Kjartan Maraas <kmaraas at gnome dot org>
[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   EchoData *d;
40
41   _dbus_verbose ("sending reply to Echo method\n");
42
43   if (!dbus_connection_get_object_path_data (connection, echo_path, (void **)&d))
44       die ("No memory");
45
46
47   dbus_error_init (&error);
48
49   reply = dbus_message_new_method_return (message);
50   if (reply == NULL)
51     die ("No memory\n");
52   
53   dbus_message_iter_init_append (reply, &iter);
54   for (i = 0; i < d->argc; ++i)
55     if (!dbus_message_iter_append_basic (&iter, DBUS_TYPE_STRING, &(d->argv[i])))
56       die ("No memory\n");
57
58   if (!dbus_connection_send (connection, reply, NULL))
59     die ("No memory\n");
60
61   fprintf (stderr, "Shell echo service echoed the command line\n");
62   
63   dbus_message_unref (reply);
64     
65   return DBUS_HANDLER_RESULT_HANDLED;
66 }
67
68 static void
69 path_unregistered_func (DBusConnection  *connection,
70                         void            *user_data)
71 {
72   /* connection was finalized */
73 }
74
75 static DBusHandlerResult
76 path_message_func (DBusConnection  *connection,
77                    DBusMessage     *message,
78                    void            *user_data)
79 {
80   if (dbus_message_is_method_call (message,
81                                    "org.freedesktop.TestSuite",
82                                    "Echo"))
83     return handle_echo (connection, message);
84   else if (dbus_message_is_method_call (message,
85                                         "org.freedesktop.TestSuite",
86                                         "Exit"))
87     {
88       dbus_connection_close (connection);
89       quit ();
90       return DBUS_HANDLER_RESULT_HANDLED;
91     }
92   else
93     return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
94 }
95
96 static DBusObjectPathVTable
97 echo_vtable = {
98   path_unregistered_func,
99   path_message_func,
100   NULL,
101 };
102
103 static DBusHandlerResult
104 filter_func (DBusConnection     *connection,
105              DBusMessage        *message,
106              void               *user_data)
107 {
108   if (dbus_message_is_signal (message,
109                               DBUS_INTERFACE_LOCAL,
110                               "Disconnected"))
111     {
112       dbus_connection_close (connection);
113       quit ();
114       return DBUS_HANDLER_RESULT_HANDLED;
115     }
116   else
117     {
118       return DBUS_HANDLER_RESULT_NOT_YET_HANDLED;
119     }
120 }
121
122 int
123 main (int    argc,
124       char **argv)
125 {
126   DBusConnection *connection;
127   DBusError error;
128   EchoData echo_data;
129   int result;
130   
131   echo_data.argc = argc;
132   echo_data.argv = argv;
133   
134   dbus_error_init (&error);
135   connection = dbus_bus_get (DBUS_BUS_STARTER, &error);
136   if (connection == NULL)
137     {
138       fprintf (stderr, "*** Failed to open connection to activating message bus: %s\n",
139                error.message);
140       dbus_error_free (&error);
141       return 1;
142     }
143
144   loop = _dbus_loop_new ();
145   if (loop == NULL)
146     die ("No memory\n");
147   
148   if (!test_connection_setup (loop, connection))
149     die ("No memory\n");
150
151   if (!dbus_connection_add_filter (connection,
152                                    filter_func, NULL, NULL))
153     die ("No memory");
154
155   if (!dbus_connection_register_object_path (connection,
156                                              echo_path,
157                                              &echo_vtable,
158                                              (void*) &echo_data))
159     die ("No memory");
160
161   {
162     void *d;
163     if (!dbus_connection_get_object_path_data (connection, echo_path, &d))
164       die ("No memory");
165     if (d != (void*) &echo_data)
166       die ("dbus_connection_get_object_path_data() doesn't seem to work right\n");
167   }
168   
169   result = dbus_bus_request_name (connection, "org.freedesktop.DBus.TestSuiteShellEchoServiceSuccess",
170                                   0, &error);
171   if (dbus_error_is_set (&error))
172     {
173       fprintf (stderr, "Error %s\n", error.message);
174       _dbus_verbose ("*** Failed to acquire service: %s\n",
175                      error.message);
176       dbus_error_free (&error);
177       exit (1);
178     }
179
180   _dbus_verbose ("*** Test service entering main loop\n");
181   _dbus_loop_run (loop);
182
183   test_connection_shutdown (loop, connection);
184
185   dbus_connection_remove_filter (connection, filter_func, NULL);
186   
187   dbus_connection_unref (connection);
188
189   _dbus_loop_unref (loop);
190   loop = NULL;
191   
192   dbus_shutdown ();
193
194   _dbus_verbose ("*** Test service exiting\n");
195   
196   return 0;
197 }