Bug 621213 – GDBusProxy and well-known names
[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 (GDBusProxy *proxy)
38 {
39   GVariant *result;
40   GError *error;
41   const gchar *str;
42   gchar *dbus_error_name;
43
44   /* check that we can invoke a method */
45   error = NULL;
46   result = g_dbus_proxy_call_sync (proxy,
47                                    "HelloWorld",
48                                    g_variant_new ("(s)", "Hey"),
49                                    G_DBUS_CALL_FLAGS_NONE,
50                                    -1,
51                                    NULL,
52                                    &error);
53   g_assert_no_error (error);
54   g_assert (result != NULL);
55   g_assert_cmpstr (g_variant_get_type_string (result), ==, "(s)");
56   g_variant_get (result, "(&s)", &str);
57   g_assert_cmpstr (str, ==, "You greeted me with 'Hey'. Thanks!");
58   g_variant_unref (result);
59
60   /* Check that we can completely recover the returned error */
61   result = g_dbus_proxy_call_sync (proxy,
62                                    "HelloWorld",
63                                    g_variant_new ("(s)", "Yo"),
64                                    G_DBUS_CALL_FLAGS_NONE,
65                                    -1,
66                                    NULL,
67                                    &error);
68   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_DBUS_ERROR);
69   g_assert (g_dbus_error_is_remote_error (error));
70   g_assert (g_dbus_error_is_remote_error (error));
71   g_assert (result == NULL);
72   dbus_error_name = g_dbus_error_get_remote_error (error);
73   g_assert_cmpstr (dbus_error_name, ==, "com.example.TestException");
74   g_free (dbus_error_name);
75   g_assert (g_dbus_error_strip_remote_error (error));
76   g_assert_cmpstr (error->message, ==, "Yo is not a proper greeting");
77   g_clear_error (&error);
78
79   /* Check that we get a timeout if the method handling is taking longer than timeout */
80   error = NULL;
81   result = g_dbus_proxy_call_sync (proxy,
82                                    "Sleep",
83                                    g_variant_new ("(i)", 500 /* msec */),
84                                    G_DBUS_CALL_FLAGS_NONE,
85                                    100 /* msec */,
86                                    NULL,
87                                    &error);
88   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
89   g_assert (!g_dbus_error_is_remote_error (error));
90   g_assert (result == NULL);
91   g_clear_error (&error);
92
93   /* Check that proxy-default timeouts work. */
94   g_assert_cmpint (g_dbus_proxy_get_default_timeout (proxy), ==, -1);
95
96   /* the default timeout is 25000 msec so this should work */
97   result = g_dbus_proxy_call_sync (proxy,
98                                    "Sleep",
99                                    g_variant_new ("(i)", 500 /* msec */),
100                                    G_DBUS_CALL_FLAGS_NONE,
101                                    -1, /* use proxy default (e.g. -1 -> e.g. 25000 msec) */
102                                    NULL,
103                                    &error);
104   g_assert_no_error (error);
105   g_assert (result != NULL);
106   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
107   g_variant_unref (result);
108
109   /* now set the proxy-default timeout to 250 msec and try the 500 msec call - this should FAIL */
110   g_dbus_proxy_set_default_timeout (proxy, 250);
111   g_assert_cmpint (g_dbus_proxy_get_default_timeout (proxy), ==, 250);
112   result = g_dbus_proxy_call_sync (proxy,
113                                    "Sleep",
114                                    g_variant_new ("(i)", 500 /* msec */),
115                                    G_DBUS_CALL_FLAGS_NONE,
116                                    -1, /* use proxy default (e.g. 250 msec) */
117                                    NULL,
118                                    &error);
119   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_TIMED_OUT);
120   g_assert (!g_dbus_error_is_remote_error (error));
121   g_assert (result == NULL);
122   g_clear_error (&error);
123
124   /* clean up after ourselves */
125   g_dbus_proxy_set_default_timeout (proxy, -1);
126 }
127
128 /* ---------------------------------------------------------------------------------------------------- */
129 /* Test that the property aspects of GDBusProxy works */
130 /* ---------------------------------------------------------------------------------------------------- */
131
132 static void
133 test_properties (GDBusProxy *proxy)
134 {
135   GError *error;
136   GVariant *variant;
137   GVariant *variant2;
138   GVariant *result;
139
140   error = NULL;
141
142   /*
143    * Check that we can read cached properties.
144    *
145    * No need to test all properties - GVariant has already been tested
146    */
147   variant = g_dbus_proxy_get_cached_property (proxy, "y");
148   g_assert (variant != NULL);
149   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
150   g_variant_unref (variant);
151   variant = g_dbus_proxy_get_cached_property (proxy, "o");
152   g_assert (variant != NULL);
153   g_assert_cmpstr (g_variant_get_string (variant, NULL), ==, "/some/path");
154   g_variant_unref (variant);
155
156   /*
157    * Now ask the service to change a property and check that #GDBusProxy::g-property-changed
158    * is received. Also check that the cache is updated.
159    */
160   variant2 = g_variant_new_byte (42);
161   result = g_dbus_proxy_call_sync (proxy,
162                                    "FrobSetProperty",
163                                    g_variant_new ("(sv)",
164                                                   "y",
165                                                   variant2),
166                                    G_DBUS_CALL_FLAGS_NONE,
167                                    -1,
168                                    NULL,
169                                    &error);
170   g_assert_no_error (error);
171   g_assert (result != NULL);
172   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
173   g_variant_unref (result);
174   _g_assert_signal_received (proxy, "g-properties-changed");
175   variant = g_dbus_proxy_get_cached_property (proxy, "y");
176   g_assert (variant != NULL);
177   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
178   g_variant_unref (variant);
179
180   g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (142));
181   variant = g_dbus_proxy_get_cached_property (proxy, "y");
182   g_assert (variant != NULL);
183   g_assert_cmpint (g_variant_get_byte (variant), ==, 142);
184   g_variant_unref (variant);
185
186   g_dbus_proxy_set_cached_property (proxy, "y", NULL);
187   variant = g_dbus_proxy_get_cached_property (proxy, "y");
188   g_assert (variant == NULL);
189
190   /* Check that the invalidation feature of the PropertiesChanged()
191    * signal works... First, check that we have a cached value of the
192    * property (from the initial GetAll() call)
193    */
194   variant = g_dbus_proxy_get_cached_property (proxy, "PropertyThatWillBeInvalidated");
195   g_assert (variant != NULL);
196   g_assert_cmpstr (g_variant_get_string (variant, NULL), ==, "InitialValue");
197   g_variant_unref (variant);
198   /* now ask to invalidate the property - this causes a
199    *
200    *   PropertiesChanaged("com.example.Frob",
201    *                      {},
202    *                      ["PropertyThatWillBeInvalidated")
203    *
204    * signal to be emitted. This is received before the method reply
205    * for FrobInvalidateProperty *but* since the proxy was created in
206    * the same thread as we're doing this synchronous call, we'll get
207    * the method reply before...
208    */
209   result = g_dbus_proxy_call_sync (proxy,
210                                    "FrobInvalidateProperty",
211                                    NULL,
212                                    G_DBUS_CALL_FLAGS_NONE,
213                                    -1,
214                                    NULL,
215                                    &error);
216   g_assert_no_error (error);
217   g_assert (result != NULL);
218   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
219   g_variant_unref (result);
220   /* ... hence we wait for the g-properties-changed signal to be delivered */
221   _g_assert_signal_received (proxy, "g-properties-changed");
222   /* ... and now we finally, check that the cached value has been invalidated */
223   variant = g_dbus_proxy_get_cached_property (proxy, "PropertyThatWillBeInvalidated");
224   g_assert (variant == NULL);
225 }
226
227 /* ---------------------------------------------------------------------------------------------------- */
228 /* Test that the signal aspects of GDBusProxy works */
229 /* ---------------------------------------------------------------------------------------------------- */
230
231 static void
232 test_proxy_signals_on_signal (GDBusProxy  *proxy,
233                               const gchar *sender_name,
234                               const gchar *signal_name,
235                               GVariant    *parameters,
236                               gpointer     user_data)
237 {
238   GString *s = user_data;
239
240   g_assert_cmpstr (signal_name, ==, "TestSignal");
241   g_assert_cmpstr (g_variant_get_type_string (parameters), ==, "(sov)");
242
243   g_variant_print_string (parameters, s, TRUE);
244 }
245
246 typedef struct
247 {
248   GMainLoop *internal_loop;
249   GString *s;
250 } TestSignalData;
251
252 static void
253 test_proxy_signals_on_emit_signal_cb (GDBusProxy   *proxy,
254                                       GAsyncResult *res,
255                                       gpointer      user_data)
256 {
257   TestSignalData *data = user_data;
258   GError *error;
259   GVariant *result;
260
261   error = NULL;
262   result = g_dbus_proxy_call_finish (proxy,
263                                      res,
264                                      &error);
265   g_assert_no_error (error);
266   g_assert (result != NULL);
267   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
268   g_variant_unref (result);
269
270   /* check that the signal was recieved before we got the method result */
271   g_assert (strlen (data->s->str) > 0);
272
273   /* break out of the loop */
274   g_main_loop_quit (data->internal_loop);
275 }
276
277 static void
278 test_signals (GDBusProxy *proxy)
279 {
280   GError *error;
281   GString *s;
282   gulong signal_handler_id;
283   TestSignalData data;
284   GVariant *result;
285
286   error = NULL;
287
288   /*
289    * Ask the service to emit a signal and check that we receive it.
290    *
291    * Note that blocking calls don't block in the mainloop so wait for the signal (which
292    * is dispatched before the method reply)
293    */
294   s = g_string_new (NULL);
295   signal_handler_id = g_signal_connect (proxy,
296                                         "g-signal",
297                                         G_CALLBACK (test_proxy_signals_on_signal),
298                                         s);
299
300   result = g_dbus_proxy_call_sync (proxy,
301                                    "EmitSignal",
302                                    g_variant_new ("(so)",
303                                                   "Accept the next proposition you hear",
304                                                   "/some/path"),
305                                    G_DBUS_CALL_FLAGS_NONE,
306                                    -1,
307                                    NULL,
308                                    &error);
309   g_assert_no_error (error);
310   g_assert (result != NULL);
311   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
312   g_variant_unref (result);
313   /* check that we haven't received the signal just yet */
314   g_assert (strlen (s->str) == 0);
315   /* and now wait for the signal */
316   _g_assert_signal_received (proxy, "g-signal");
317   g_assert_cmpstr (s->str,
318                    ==,
319                    "('Accept the next proposition you hear .. in bed!', objectpath '/some/path/in/bed', <'a variant'>)");
320   g_signal_handler_disconnect (proxy, signal_handler_id);
321   g_string_free (s, TRUE);
322
323   /*
324    * Now do this async to check the signal is received before the method returns.
325    */
326   s = g_string_new (NULL);
327   data.internal_loop = g_main_loop_new (NULL, FALSE);
328   data.s = s;
329   signal_handler_id = g_signal_connect (proxy,
330                                         "g-signal",
331                                         G_CALLBACK (test_proxy_signals_on_signal),
332                                         s);
333   g_dbus_proxy_call (proxy,
334                      "EmitSignal",
335                      g_variant_new ("(so)",
336                                     "You will make a great programmer",
337                                     "/some/other/path"),
338                      G_DBUS_CALL_FLAGS_NONE,
339                      -1,
340                      NULL,
341                      (GAsyncReadyCallback) test_proxy_signals_on_emit_signal_cb,
342                      &data);
343   g_main_loop_run (data.internal_loop);
344   g_main_loop_unref (data.internal_loop);
345   g_assert_cmpstr (s->str,
346                    ==,
347                    "('You will make a great programmer .. in bed!', objectpath '/some/other/path/in/bed', <'a variant'>)");
348   g_signal_handler_disconnect (proxy, signal_handler_id);
349   g_string_free (s, TRUE);
350 }
351
352 static void
353 test_bogus_method_return (GDBusProxy *proxy)
354 {
355   GError *error = NULL;
356   GVariant *result;
357
358   result = g_dbus_proxy_call_sync (proxy,
359                                    "PairReturn",
360                                    NULL,
361                                    G_DBUS_CALL_FLAGS_NONE,
362                                    -1,
363                                    NULL,
364                                    &error);
365   g_assert_error (error, G_IO_ERROR, G_IO_ERROR_INVALID_ARGUMENT);
366   g_assert (result == NULL);
367 }
368
369 /* ---------------------------------------------------------------------------------------------------- */
370
371 static const gchar *frob_dbus_interface_xml =
372   "<node>"
373   "  <interface name='com.example.Frob'>"
374   /* Deliberately different from gdbus-testserver.py's definition */
375   "    <method name='PairReturn'>"
376   "      <arg type='u' name='somenumber' direction='in'/>"
377   "      <arg type='s' name='somestring' direction='out'/>"
378   "    </method>"
379   "    <method name='HelloWorld'>"
380   "      <arg type='s' name='somestring' direction='in'/>"
381   "      <arg type='s' name='somestring' direction='out'/>"
382   "    </method>"
383   "    <method name='Sleep'>"
384   "      <arg type='i' name='timeout' direction='in'/>"
385   "    </method>"
386   "    <property name='y' type='y' access='readwrite'/>"
387   "  </interface>"
388   "</node>";
389 static GDBusInterfaceInfo *frob_dbus_interface_info;
390
391 static void
392 test_expected_interface (GDBusProxy *proxy)
393 {
394   /* This is obviously wrong but expected interface is not set so we don't fail... */
395   g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
396   g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
397   g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
398   g_dbus_proxy_set_cached_property (proxy, "does-not-exist", NULL);
399
400   /* Now repeat the method tests, with an expected interface set */
401   g_dbus_proxy_set_interface_info (proxy, frob_dbus_interface_info);
402   test_methods (proxy);
403
404   /* And now one more test where we deliberately set the expected
405    * interface definition incorrectly
406    */
407   test_bogus_method_return (proxy);
408
409   /* Also check that we complain if setting a cached property of the wrong type */
410   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
411     {
412       g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_string ("error_me_out!"));
413     }
414   g_test_trap_assert_stderr ("*Trying to set property y of type s but according to the expected interface the type is y*");
415   g_test_trap_assert_failed();
416
417   if (g_test_trap_fork (0, G_TEST_TRAP_SILENCE_STDOUT | G_TEST_TRAP_SILENCE_STDERR))
418     {
419       g_dbus_proxy_set_cached_property (proxy, "does-not-exist", g_variant_new_string ("something"));
420     }
421   g_test_trap_assert_stderr ("*Trying to lookup property does-not-exist which isn't in expected interface com.example.Frob*");
422   g_test_trap_assert_failed();
423
424   /* this should work, however (since the type is correct) */
425   g_dbus_proxy_set_cached_property (proxy, "y", g_variant_new_byte (42));
426 }
427
428 static void
429 test_proxy (void)
430 {
431   GDBusProxy *proxy;
432   GDBusConnection *connection;
433   GError *error;
434
435   session_bus_up ();
436
437   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
438    * until one can connect to the bus but that's not how things work right now
439    */
440   usleep (500 * 1000);
441
442   error = NULL;
443   connection = g_bus_get_sync (G_BUS_TYPE_SESSION,
444                                NULL,
445                                &error);
446   g_assert_no_error (error);
447   error = NULL;
448   proxy = g_dbus_proxy_new_sync (connection,
449                                  G_DBUS_PROXY_FLAGS_NONE,
450                                  NULL,                      /* GDBusInterfaceInfo */
451                                  "com.example.TestService", /* name */
452                                  "/com/example/TestObject", /* object path */
453                                  "com.example.Frob",        /* interface */
454                                  NULL, /* GCancellable */
455                                  &error);
456   g_assert_no_error (error);
457
458   /* this is safe; testserver will exit once the bus goes away */
459   g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
460
461   _g_assert_property_notify (proxy, "g-name-owner");
462
463   test_methods (proxy);
464   test_properties (proxy);
465   test_signals (proxy);
466   test_expected_interface (proxy);
467
468   g_object_unref (proxy);
469   g_object_unref (connection);
470 }
471
472 /* ---------------------------------------------------------------------------------------------------- */
473
474 int
475 main (int   argc,
476       char *argv[])
477 {
478   gint ret;
479   GDBusNodeInfo *introspection_data = NULL;
480
481   g_type_init ();
482   g_test_init (&argc, &argv, NULL);
483
484   introspection_data = g_dbus_node_info_new_for_xml (frob_dbus_interface_xml, NULL);
485   g_assert (introspection_data != NULL);
486   frob_dbus_interface_info = introspection_data->interfaces[0];
487
488   /* all the tests rely on a shared main loop */
489   loop = g_main_loop_new (NULL, FALSE);
490
491   /* all the tests use a session bus with a well-known address that we can bring up and down
492    * using session_bus_up() and session_bus_down().
493    */
494   g_unsetenv ("DISPLAY");
495   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
496
497   g_test_add_func ("/gdbus/proxy", test_proxy);
498
499   ret = g_test_run();
500
501   g_dbus_node_info_unref (introspection_data);
502   return ret;
503 }