Fix !srcdir build
[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_call_sync (proxy,
50                                    "HelloWorld",
51                                    g_variant_new ("(s)", "Hey"),
52                                    G_DBUS_CALL_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_call_sync (proxy,
65                                    "HelloWorld",
66                                    g_variant_new ("(s)", "Yo"),
67                                    G_DBUS_CALL_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_call_sync (proxy,
85                                    "Sleep",
86                                    g_variant_new ("(i)", 500 /* msec */),
87                                    G_DBUS_CALL_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_call_sync (proxy,
101                                    "Sleep",
102                                    g_variant_new ("(i)", 500 /* msec */),
103                                    G_DBUS_CALL_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_call_sync (proxy,
116                                    "Sleep",
117                                    g_variant_new ("(i)", 500 /* msec */),
118                                    G_DBUS_CALL_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");
154   g_assert (variant != NULL);
155   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
156   g_variant_unref (variant);
157   variant = g_dbus_proxy_get_cached_property (proxy, "o");
158   g_assert (variant != NULL);
159   g_assert_cmpstr (g_variant_get_string (variant, NULL), ==, "/some/path");
160   g_variant_unref (variant);
161
162   /*
163    * Now ask the service to change a property and check that #GDBusProxy::g-property-changed
164    * is received. Also check that the cache is updated.
165    */
166   variant2 = g_variant_new_byte (42);
167   result = g_dbus_proxy_call_sync (proxy,
168                                    "FrobSetProperty",
169                                    g_variant_new ("(sv)",
170                                                   "y",
171                                                   variant2),
172                                    G_DBUS_CALL_FLAGS_NONE,
173                                    -1,
174                                    NULL,
175                                    &error);
176   g_assert_no_error (error);
177   g_assert (result != NULL);
178   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
179   g_variant_unref (result);
180   _g_assert_signal_received (proxy, "g-properties-changed");
181   variant = g_dbus_proxy_get_cached_property (proxy, "y");
182   g_assert (variant != NULL);
183   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
184   g_variant_unref (variant);
185
186   g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (142));
187   variant = g_dbus_proxy_get_cached_property (proxy, "y");
188   g_assert (variant != NULL);
189   g_assert_cmpint (g_variant_get_byte (variant), ==, 142);
190   g_variant_unref (variant);
191
192   g_dbus_proxy_set_cached_property (proxy, "y", NULL);
193   variant = g_dbus_proxy_get_cached_property (proxy, "y");
194   g_assert (variant == NULL);
195 }
196
197 /* ---------------------------------------------------------------------------------------------------- */
198 /* Test that the signal aspects of GDBusProxy works */
199 /* ---------------------------------------------------------------------------------------------------- */
200
201 static void
202 test_proxy_signals_on_signal (GDBusProxy  *proxy,
203                               const gchar *sender_name,
204                               const gchar *signal_name,
205                               GVariant    *parameters,
206                               gpointer     user_data)
207 {
208   GString *s = user_data;
209
210   g_assert_cmpstr (signal_name, ==, "TestSignal");
211   g_assert_cmpstr (g_variant_get_type_string (parameters), ==, "(sov)");
212
213   g_variant_print_string (parameters, s, TRUE);
214 }
215
216 typedef struct
217 {
218   GMainLoop *internal_loop;
219   GString *s;
220 } TestSignalData;
221
222 static void
223 test_proxy_signals_on_emit_signal_cb (GDBusProxy   *proxy,
224                                       GAsyncResult *res,
225                                       gpointer      user_data)
226 {
227   TestSignalData *data = user_data;
228   GError *error;
229   GVariant *result;
230
231   error = NULL;
232   result = g_dbus_proxy_call_finish (proxy,
233                                      res,
234                                      &error);
235   g_assert_no_error (error);
236   g_assert (result != NULL);
237   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
238   g_variant_unref (result);
239
240   /* check that the signal was recieved before we got the method result */
241   g_assert (strlen (data->s->str) > 0);
242
243   /* break out of the loop */
244   g_main_loop_quit (data->internal_loop);
245 }
246
247 static void
248 test_signals (GDBusConnection *connection,
249               const gchar     *name,
250               const gchar     *name_owner,
251               GDBusProxy      *proxy)
252 {
253   GError *error;
254   GString *s;
255   gulong signal_handler_id;
256   TestSignalData data;
257   GVariant *result;
258
259   error = NULL;
260
261   /*
262    * Ask the service to emit a signal and check that we receive it.
263    *
264    * Note that blocking calls don't block in the mainloop so wait for the signal (which
265    * is dispatched before the method reply)
266    */
267   s = g_string_new (NULL);
268   signal_handler_id = g_signal_connect (proxy,
269                                         "g-signal",
270                                         G_CALLBACK (test_proxy_signals_on_signal),
271                                         s);
272
273   result = g_dbus_proxy_call_sync (proxy,
274                                    "EmitSignal",
275                                    g_variant_new ("(so)",
276                                                   "Accept the next proposition you hear",
277                                                   "/some/path"),
278                                    G_DBUS_CALL_FLAGS_NONE,
279                                    -1,
280                                    NULL,
281                                    &error);
282   g_assert_no_error (error);
283   g_assert (result != NULL);
284   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
285   g_variant_unref (result);
286   /* check that we haven't received the signal just yet */
287   g_assert (strlen (s->str) == 0);
288   /* and now wait for the signal */
289   _g_assert_signal_received (proxy, "g-signal");
290   g_assert_cmpstr (s->str,
291                    ==,
292                    "('Accept the next proposition you hear .. in bed!', objectpath '/some/path/in/bed', <'a variant'>)");
293   g_signal_handler_disconnect (proxy, signal_handler_id);
294   g_string_free (s, TRUE);
295
296   /*
297    * Now do this async to check the signal is received before the method returns.
298    */
299   s = g_string_new (NULL);
300   data.internal_loop = g_main_loop_new (NULL, FALSE);
301   data.s = s;
302   signal_handler_id = g_signal_connect (proxy,
303                                         "g-signal",
304                                         G_CALLBACK (test_proxy_signals_on_signal),
305                                         s);
306   g_dbus_proxy_call (proxy,
307                      "EmitSignal",
308                      g_variant_new ("(so)",
309                                     "You will make a great programmer",
310                                     "/some/other/path"),
311                      G_DBUS_CALL_FLAGS_NONE,
312                      -1,
313                      NULL,
314                      (GAsyncReadyCallback) test_proxy_signals_on_emit_signal_cb,
315                      &data);
316   g_main_loop_run (data.internal_loop);
317   g_main_loop_unref (data.internal_loop);
318   g_assert_cmpstr (s->str,
319                    ==,
320                    "('You will make a great programmer .. in bed!', objectpath '/some/other/path/in/bed', <'a variant'>)");
321   g_signal_handler_disconnect (proxy, signal_handler_id);
322   g_string_free (s, TRUE);
323 }
324
325 static void
326 test_bogus_method_return (GDBusConnection *connection,
327                           const gchar     *name,
328                           const gchar     *name_owner,
329                           GDBusProxy      *proxy)
330 {
331   GError *error = NULL;
332   GVariant *result;
333
334   result = g_dbus_proxy_call_sync (proxy,
335                                    "PairReturn",
336                                    NULL,
337                                    G_DBUS_CALL_FLAGS_NONE,
338                                    -1,
339                                    NULL,
340                                    &error);
341   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
342   g_assert (result == NULL);
343 }
344
345 /* ---------------------------------------------------------------------------------------------------- */
346
347 static const gchar *frob_dbus_interface_xml =
348   "<node>"
349   "  <interface name='com.example.Frob'>"
350   /* Deliberately different from gdbus-testserver.py's definition */
351   "    <method name='PairReturn'>"
352   "      <arg type='u' name='somenumber' direction='in'/>"
353   "      <arg type='s' name='somestring' direction='out'/>"
354   "    </method>"
355   "    <method name='HelloWorld'>"
356   "      <arg type='s' name='somestring' direction='in'/>"
357   "      <arg type='s' name='somestring' direction='out'/>"
358   "    </method>"
359   "    <method name='Sleep'>"
360   "      <arg type='i' name='timeout' direction='in'/>"
361   "    </method>"
362   "    <property name='y' type='y' access='readwrite'/>"
363   "  </interface>"
364   "</node>";
365 static GDBusInterfaceInfo *frob_dbus_interface_info;
366
367 static void
368 on_proxy_appeared (GDBusConnection *connection,
369                    const gchar     *name,
370                    const gchar     *name_owner,
371                    GDBusProxy      *proxy,
372                    gpointer         user_data)
373 {
374   test_methods (connection, name, name_owner, proxy);
375   test_properties (connection, name, name_owner, proxy);
376   test_signals (connection, name, name_owner, proxy);
377
378   /* This is obviously wrong but expected interface is not set so we don't fail... */
379   g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
380   g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
381   g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
382   g_dbus_proxy_set_cached_property (proxy, "does-not-exist", NULL);
383
384   /* Now repeat the method tests, with an expected interface set */
385   g_dbus_proxy_set_interface_info (proxy, frob_dbus_interface_info);
386   test_methods (connection, name, name_owner, proxy);
387
388   /* And now one more test where we deliberately set the expected
389    * interface definition incorrectly
390    */
391   test_bogus_method_return (connection, name, name_owner, proxy);
392
393   /* Also check that we complain if setting a cached property of the wrong type */
394   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
395     {
396       g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
397     }
398   g_test_trap_assert_stderr ("*Trying to set property y of type s but according to the expected interface the type is y*");
399   g_test_trap_assert_failed();
400
401   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
402     {
403       g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
404     }
405   g_test_trap_assert_stderr ("*Trying to lookup property does-not-exist which isn't in expected interface com.example.Frob*");
406   g_test_trap_assert_failed();
407
408   /* this should work, however (since the type is correct) */
409   g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
410
411   g_main_loop_quit (loop);
412 }
413
414 static void
415 on_proxy_vanished (GDBusConnection *connection,
416                    const gchar     *name,
417                    gpointer         user_data)
418 {
419 }
420
421 static void
422 test_proxy (void)
423 {
424   guint watcher_id;
425
426   session_bus_up ();
427
428   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
429    * until one can connect to the bus but that's not how things work right now
430    */
431   usleep (500 * 1000);
432
433   watcher_id = g_bus_watch_proxy (G_BUS_TYPE_SESSION,
434                                   "com.example.TestService",
435                                   G_BUS_NAME_WATCHER_FLAGS_NONE,
436                                   "/com/example/TestObject",
437                                   "com.example.Frob",
438                                   G_TYPE_DBUS_PROXY,
439                                   G_DBUS_PROXY_FLAGS_NONE,
440                                   on_proxy_appeared,
441                                   on_proxy_vanished,
442                                   NULL,
443                                   NULL);
444
445   /* this is safe; testserver will exit once the bus goes away */
446   g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
447
448   g_main_loop_run (loop);
449
450   g_bus_unwatch_proxy (watcher_id);
451
452   /* tear down bus */
453   session_bus_down ();
454 }
455
456 /* ---------------------------------------------------------------------------------------------------- */
457
458 int
459 main (int   argc,
460       char *argv[])
461 {
462   gint ret;
463   GDBusNodeInfo *introspection_data = NULL;
464
465   g_type_init ();
466   g_test_init (&argc, &argv, NULL);
467
468   introspection_data = g_dbus_node_info_new_for_xml (frob_dbus_interface_xml, NULL);
469   g_assert (introspection_data != NULL);
470   frob_dbus_interface_info = introspection_data->interfaces[0];
471
472   /* all the tests rely on a shared main loop */
473   loop = g_main_loop_new (NULL, FALSE);
474
475   /* all the tests use a session bus with a well-known address that we can bring up and down
476    * using session_bus_up() and session_bus_down().
477    */
478   g_unsetenv ("DISPLAY");
479   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
480
481   g_test_add_func ("/gdbus/proxy", test_proxy);
482
483   ret = g_test_run();
484
485   g_dbus_node_info_unref (introspection_data);
486   return ret;
487 }