More copyright year updates
[platform/upstream/glib.git] / gio / tests / gdbus-proxy.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2010 Red Hat, Inc.
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General
16  * Public License along with this library; if not, write to the
17  * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18  * Boston, MA 02111-1307, USA.
19  *
20  * Author: David Zeuthen <davidz@redhat.com>
21  */
22
23 #include <gio/gio.h>
24 #include <unistd.h>
25 #include <string.h>
26
27 #include "gdbus-tests.h"
28
29 /* all tests rely on a shared mainloop */
30 static GMainLoop *loop = NULL;
31
32 /* ---------------------------------------------------------------------------------------------------- */
33 /* Test that the method aspects of GDBusProxy works */
34 /* ---------------------------------------------------------------------------------------------------- */
35
36 static void
37 test_methods (GDBusConnection *connection,
38               const gchar     *name,
39               const gchar     *name_owner,
40               GDBusProxy      *proxy)
41 {
42   GVariant *result;
43   GError *error;
44   const gchar *str;
45   gchar *dbus_error_name;
46
47   /* check that we can invoke a method */
48   error = NULL;
49   result = g_dbus_proxy_invoke_method_sync (proxy,
50                                             "HelloWorld",
51                                             g_variant_new ("(s)", "Hey"),
52                                             G_DBUS_INVOKE_METHOD_FLAGS_NONE,
53                                             -1,
54                                             NULL,
55                                             &error);
56   g_assert_no_error (error);
57   g_assert (result != NULL);
58   g_assert_cmpstr (g_variant_get_type_string (result), ==, "(s)");
59   g_variant_get (result, "(s)", &str);
60   g_assert_cmpstr (str, ==, "You greeted me with 'Hey'. Thanks!");
61   g_variant_unref (result);
62
63   /* Check that we can completely recover the returned error */
64   result = g_dbus_proxy_invoke_method_sync (proxy,
65                                             "HelloWorld",
66                                             g_variant_new ("(s)", "Yo"),
67                                             G_DBUS_INVOKE_METHOD_FLAGS_NONE,
68                                             -1,
69                                             NULL,
70                                             &error);
71   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
72   g_assert (g_dbus_error_is_remote_error (error));
73   g_assert (g_dbus_error_is_remote_error (error));
74   g_assert (result == NULL);
75   dbus_error_name = g_dbus_error_get_remote_error (error);
76   g_assert_cmpstr (dbus_error_name, ==, "com.example.TestException");
77   g_free (dbus_error_name);
78   g_assert (g_dbus_error_strip_remote_error (error));
79   g_assert_cmpstr (error->message, ==, "Yo is not a proper greeting");
80   g_clear_error (&error);
81
82   /* Check that we get a timeout if the method handling is taking longer than timeout */
83   error = NULL;
84   result = g_dbus_proxy_invoke_method_sync (proxy,
85                                             "Sleep",
86                                             g_variant_new ("(i)", 500 /* msec */),
87                                             G_DBUS_INVOKE_METHOD_FLAGS_NONE,
88                                             100 /* msec */,
89                                             NULL,
90                                             &error);
91   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
92   g_assert (!g_dbus_error_is_remote_error (error));
93   g_assert (result == NULL);
94   g_clear_error (&error);
95
96   /* Check that proxy-default timeouts work. */
97   g_assert_cmpint (g_dbus_proxy_get_default_timeout (proxy), ==, -1);
98
99   /* the default timeout is 25000 msec so this should work */
100   result = g_dbus_proxy_invoke_method_sync (proxy,
101                                             "Sleep",
102                                             g_variant_new ("(i)", 500 /* msec */),
103                                             G_DBUS_INVOKE_METHOD_FLAGS_NONE,
104                                             -1, /* use proxy default (e.g. -1 -> e.g. 25000 msec) */
105                                             NULL,
106                                             &error);
107   g_assert_no_error (error);
108   g_assert (result != NULL);
109   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
110   g_variant_unref (result);
111
112   /* now set the proxy-default timeout to 250 msec and try the 500 msec call - this should FAIL */
113   g_dbus_proxy_set_default_timeout (proxy, 250);
114   g_assert_cmpint (g_dbus_proxy_get_default_timeout (proxy), ==, 250);
115   result = g_dbus_proxy_invoke_method_sync (proxy,
116                                             "Sleep",
117                                             g_variant_new ("(i)", 500 /* msec */),
118                                             G_DBUS_INVOKE_METHOD_FLAGS_NONE,
119                                             -1, /* use proxy default (e.g. 250 msec) */
120                                             NULL,
121                                             &error);
122   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
123   g_assert (!g_dbus_error_is_remote_error (error));
124   g_assert (result == NULL);
125   g_clear_error (&error);
126
127   /* clean up after ourselves */
128   g_dbus_proxy_set_default_timeout (proxy, -1);
129 }
130
131 /* ---------------------------------------------------------------------------------------------------- */
132 /* Test that the property aspects of GDBusProxy works */
133 /* ---------------------------------------------------------------------------------------------------- */
134
135 static void
136 test_properties (GDBusConnection *connection,
137                  const gchar     *name,
138                  const gchar     *name_owner,
139                  GDBusProxy      *proxy)
140 {
141   GError *error;
142   GVariant *variant;
143   GVariant *variant2;
144   GVariant *result;
145
146   error = NULL;
147
148   /*
149    * Check that we can read cached properties.
150    *
151    * No need to test all properties - GVariant has already been tested
152    */
153   variant = g_dbus_proxy_get_cached_property (proxy, "y", &error);
154   g_assert_no_error (error);
155   g_assert (variant != NULL);
156   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
157   g_variant_unref (variant);
158   variant = g_dbus_proxy_get_cached_property (proxy, "o", &error);
159   g_assert_no_error (error);
160   g_assert (variant != NULL);
161   g_assert_cmpstr (g_variant_get_string (variant, NULL), ==, "/some/path");
162   g_variant_unref (variant);
163
164   /*
165    * Now ask the service to change a property and check that #GDBusProxy::g-property-changed
166    * is received. Also check that the cache is updated.
167    */
168   variant2 = g_variant_new_byte (42);
169   result = g_dbus_proxy_invoke_method_sync (proxy,
170                                             "FrobSetProperty",
171                                             g_variant_new ("(sv)",
172                                                            "y",
173                                                            variant2),
174                                             G_DBUS_INVOKE_METHOD_FLAGS_NONE,
175                                             -1,
176                                             NULL,
177                                             &error);
178   g_assert_no_error (error);
179   g_assert (result != NULL);
180   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
181   g_variant_unref (result);
182   _g_assert_signal_received (proxy, "g-properties-changed");
183   variant = g_dbus_proxy_get_cached_property (proxy, "y", &error);
184   g_assert_no_error (error);
185   g_assert (variant != NULL);
186   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
187   g_variant_unref (variant);
188 }
189
190 /* ---------------------------------------------------------------------------------------------------- */
191 /* Test that the signal aspects of GDBusProxy works */
192 /* ---------------------------------------------------------------------------------------------------- */
193
194 static void
195 test_proxy_signals_on_signal (GDBusProxy  *proxy,
196                               const gchar *sender_name,
197                               const gchar *signal_name,
198                               GVariant    *parameters,
199                               gpointer     user_data)
200 {
201   GString *s = user_data;
202
203   g_assert_cmpstr (signal_name, ==, "TestSignal");
204   g_assert_cmpstr (g_variant_get_type_string (parameters), ==, "(sov)");
205
206   g_variant_print_string (parameters, s, TRUE);
207 }
208
209 typedef struct
210 {
211   GMainLoop *internal_loop;
212   GString *s;
213 } TestSignalData;
214
215 static void
216 test_proxy_signals_on_emit_signal_cb (GDBusProxy   *proxy,
217                                       GAsyncResult *res,
218                                       gpointer      user_data)
219 {
220   TestSignalData *data = user_data;
221   GError *error;
222   GVariant *result;
223
224   error = NULL;
225   result = g_dbus_proxy_invoke_method_finish (proxy,
226                                               res,
227                                               &error);
228   g_assert_no_error (error);
229   g_assert (result != NULL);
230   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
231   g_variant_unref (result);
232
233   /* check that the signal was recieved before we got the method result */
234   g_assert (strlen (data->s->str) > 0);
235
236   /* break out of the loop */
237   g_main_loop_quit (data->internal_loop);
238 }
239
240 static void
241 test_signals (GDBusConnection *connection,
242               const gchar     *name,
243               const gchar     *name_owner,
244               GDBusProxy      *proxy)
245 {
246   GError *error;
247   GString *s;
248   gulong signal_handler_id;
249   TestSignalData data;
250   GVariant *result;
251
252   error = NULL;
253
254   /*
255    * Ask the service to emit a signal and check that we receive it.
256    *
257    * Note that blocking calls don't block in the mainloop so wait for the signal (which
258    * is dispatched before the method reply)
259    */
260   s = g_string_new (NULL);
261   signal_handler_id = g_signal_connect (proxy,
262                                         "g-signal",
263                                         G_CALLBACK (test_proxy_signals_on_signal),
264                                         s);
265
266   result = g_dbus_proxy_invoke_method_sync (proxy,
267                                             "EmitSignal",
268                                             g_variant_new ("(so)",
269                                                            "Accept the next proposition you hear",
270                                                            "/some/path"),
271                                             G_DBUS_INVOKE_METHOD_FLAGS_NONE,
272                                             -1,
273                                             NULL,
274                                             &error);
275   g_assert_no_error (error);
276   g_assert (result != NULL);
277   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
278   g_variant_unref (result);
279   /* check that we haven't received the signal just yet */
280   g_assert (strlen (s->str) == 0);
281   /* and now wait for the signal */
282   _g_assert_signal_received (proxy, "g-signal");
283   g_assert_cmpstr (s->str,
284                    ==,
285                    "('Accept the next proposition you hear .. in bed!', objectpath '/some/path/in/bed', <'a variant'>)");
286   g_signal_handler_disconnect (proxy, signal_handler_id);
287   g_string_free (s, TRUE);
288
289   /*
290    * Now do this async to check the signal is received before the method returns.
291    */
292   s = g_string_new (NULL);
293   data.internal_loop = g_main_loop_new (NULL, FALSE);
294   data.s = s;
295   signal_handler_id = g_signal_connect (proxy,
296                                         "g-signal",
297                                         G_CALLBACK (test_proxy_signals_on_signal),
298                                         s);
299   g_dbus_proxy_invoke_method (proxy,
300                               "EmitSignal",
301                               g_variant_new ("(so)",
302                                              "You will make a great programmer",
303                                              "/some/other/path"),
304                               G_DBUS_INVOKE_METHOD_FLAGS_NONE,
305                               -1,
306                               NULL,
307                               (GAsyncReadyCallback) test_proxy_signals_on_emit_signal_cb,
308                               &data);
309   g_main_loop_run (data.internal_loop);
310   g_main_loop_unref (data.internal_loop);
311   g_assert_cmpstr (s->str,
312                    ==,
313                    "('You will make a great programmer .. in bed!', objectpath '/some/other/path/in/bed', <'a variant'>)");
314   g_signal_handler_disconnect (proxy, signal_handler_id);
315   g_string_free (s, TRUE);
316 }
317
318 static void
319 test_bogus_method_return (GDBusConnection *connection,
320                           const gchar     *name,
321                           const gchar     *name_owner,
322                           GDBusProxy      *proxy)
323 {
324   GError *error = NULL;
325   GVariant *result;
326
327   result = g_dbus_proxy_invoke_method_sync (proxy,
328                                             "PairReturn",
329                                             NULL,
330                                             G_DBUS_INVOKE_METHOD_FLAGS_NONE,
331                                             -1,
332                                             NULL,
333                                             &error);
334   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
335   g_assert (result == NULL);
336 }
337
338 /* ---------------------------------------------------------------------------------------------------- */
339
340 static const gchar *frob_dbus_interface_xml =
341   "<node>"
342   "  <interface name='com.example.Frob'>"
343   /* Deliberately different from gdbus-testserver.py's definition */
344   "    <method name='PairReturn'>"
345   "      <arg type='u' name='somenumber' direction='in'/>"
346   "      <arg type='s' name='somestring' direction='out'/>"
347   "    </method>"
348   "    <method name='HelloWorld'>"
349   "      <arg type='s' name='somestring' direction='in'/>"
350   "      <arg type='s' name='somestring' direction='out'/>"
351   "    </method>"
352   "    <method name='Sleep'>"
353   "      <arg type='i' name='timeout' direction='in'/>"
354   "    </method>"
355   "  </interface>"
356   "</node>";
357 static GDBusInterfaceInfo *frob_dbus_interface_info;
358
359 static void
360 on_proxy_appeared (GDBusConnection *connection,
361                    const gchar     *name,
362                    const gchar     *name_owner,
363                    GDBusProxy      *proxy,
364                    gpointer         user_data)
365 {
366   test_methods (connection, name, name_owner, proxy);
367   test_properties (connection, name, name_owner, proxy);
368   test_signals (connection, name, name_owner, proxy);
369
370   /* Now repeat the method tests, with an expected interface set */
371   g_dbus_proxy_set_interface_info (proxy, frob_dbus_interface_info);
372   test_methods (connection, name, name_owner, proxy);
373
374   /* And now one more test where we deliberately set the expected
375    * interface definition incorrectly
376    */
377   test_bogus_method_return (connection, name, name_owner, proxy);
378
379   g_main_loop_quit (loop);
380 }
381
382 static void
383 on_proxy_vanished (GDBusConnection *connection,
384                    const gchar     *name,
385                    gpointer         user_data)
386 {
387 }
388
389 static void
390 test_proxy (void)
391 {
392   guint watcher_id;
393
394   session_bus_up ();
395
396   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
397    * until one can connect to the bus but that's not how things work right now
398    */
399   usleep (500 * 1000);
400
401   watcher_id = g_bus_watch_proxy (G_BUS_TYPE_SESSION,
402                                   "com.example.TestService",
403                                   G_BUS_NAME_WATCHER_FLAGS_NONE,
404                                   "/com/example/TestObject",
405                                   "com.example.Frob",
406                                   G_TYPE_DBUS_PROXY,
407                                   G_DBUS_PROXY_FLAGS_NONE,
408                                   on_proxy_appeared,
409                                   on_proxy_vanished,
410                                   NULL,
411                                   NULL);
412
413   /* this is safe; testserver will exit once the bus goes away */
414   g_assert (g_spawn_command_line_async ("./gdbus-testserver.py", NULL));
415
416   g_main_loop_run (loop);
417
418   g_bus_unwatch_proxy (watcher_id);
419
420   /* tear down bus */
421   session_bus_down ();
422 }
423
424 /* ---------------------------------------------------------------------------------------------------- */
425
426 int
427 main (int   argc,
428       char *argv[])
429 {
430   gint ret;
431   GDBusNodeInfo *introspection_data = NULL;
432
433   g_type_init ();
434   g_test_init (&argc, &argv, NULL);
435
436   introspection_data = g_dbus_node_info_new_for_xml (frob_dbus_interface_xml, NULL);
437   g_assert (introspection_data != NULL);
438   frob_dbus_interface_info = introspection_data->interfaces[0];
439
440   /* all the tests rely on a shared main loop */
441   loop = g_main_loop_new (NULL, FALSE);
442
443   /* all the tests use a session bus with a well-known address that we can bring up and down
444    * using session_bus_up() and session_bus_down().
445    */
446   g_unsetenv ("DISPLAY");
447   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
448
449   g_test_add_func ("/gdbus/proxy", test_proxy);
450
451   ret = g_test_run();
452
453   g_dbus_node_info_unref (introspection_data);
454   return ret;
455 }