Bug 621213 – GDBusProxy and well-known names
[platform/upstream/glib.git] / gio / tests / gdbus-proxy-well-known-name.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
34 static void
35 proxy_new_cb (GObject       *source_object,
36               GAsyncResult  *res,
37               gpointer       user_data)
38 {
39   GDBusProxy **ret = user_data;
40   GError *error;
41
42   error = NULL;
43   *ret = g_dbus_proxy_new_finish (res, &error);
44   g_assert_no_error (error);
45   g_assert (ret != NULL);
46
47   g_main_loop_quit (loop);
48 }
49
50 static void
51 test_proxy_well_known_name (void)
52 {
53   GDBusProxy *p;
54   GDBusProxy *p2;
55   GDBusProxy *ap;
56   GDBusProxy *ap2;
57   GDBusConnection *c;
58   GError *error;
59   gchar *name_owner;
60   gchar **property_names;
61   GVariant *variant;
62   GVariant *result;
63
64   session_bus_up ();
65
66   /* TODO: wait a bit for the bus to come up.. ideally session_bus_up() won't return
67    * until one can connect to the bus but that's not how things work right now
68    */
69   usleep (500 * 1000);
70
71   error = NULL;
72   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
73   g_assert_no_error (error);
74   g_assert (c != NULL);
75
76   error = NULL;
77   p = g_dbus_proxy_new_sync (c,
78                              G_DBUS_PROXY_FLAGS_NONE,
79                              NULL,                      /* GDBusInterfaceInfo* */
80                              "com.example.TestService", /* name */
81                              "/com/example/TestObject", /* object path */
82                              "com.example.Frob",        /* interface name */
83                              NULL,                      /* GCancellable */
84                              &error);
85   g_assert_no_error (error);
86
87   /* we shouldn't have a name owner nor any cached properties */
88   g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
89   g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
90
91   /* also for async: we shouldn't have a name owner nor any cached properties */
92   g_dbus_proxy_new (c,
93                     G_DBUS_PROXY_FLAGS_NONE,
94                     NULL,                      /* GDBusInterfaceInfo* */
95                     "com.example.TestService", /* name */
96                     "/com/example/TestObject", /* object path */
97                     "com.example.Frob",        /* interface name */
98                     NULL,                      /* GCancellable */
99                     (GAsyncReadyCallback) proxy_new_cb,
100                     &ap);
101   g_main_loop_run (loop);
102   g_assert_cmpstr (g_dbus_proxy_get_name_owner (ap), ==, NULL);
103   g_assert (g_dbus_proxy_get_cached_property_names (ap) == NULL);
104
105   /* this is safe; testserver will exit once the bus goes away */
106   g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
107
108   /* check that we get the notify::g-name-owner signal */
109   _g_assert_property_notify (p, "g-name-owner");
110
111   /* Now we should have a name owner as well as properties */
112   name_owner = g_dbus_proxy_get_name_owner (p);
113   property_names = g_dbus_proxy_get_cached_property_names (p);
114   g_assert (g_dbus_is_unique_name (name_owner));
115   g_assert (property_names != NULL && g_strv_length (property_names) > 0);
116   g_free (name_owner);
117   g_strfreev (property_names);
118
119   /* if we create another proxy with the service being available, check that
120    * it has a name owner and properties
121    */
122   error = NULL;
123   p2 = g_dbus_proxy_new_sync (c,
124                               G_DBUS_PROXY_FLAGS_NONE,
125                               NULL,                      /* GDBusInterfaceInfo* */
126                               "com.example.TestService", /* name */
127                               "/com/example/TestObject", /* object path */
128                               "com.example.Frob",        /* interface name */
129                               NULL,                      /* GCancellable */
130                               &error);
131   g_assert_no_error (error);
132   name_owner = g_dbus_proxy_get_name_owner (p2);
133   property_names = g_dbus_proxy_get_cached_property_names (p2);
134   g_assert (g_dbus_is_unique_name (name_owner));
135   g_assert (property_names != NULL && g_strv_length (property_names) > 0);
136   g_free (name_owner);
137   g_strfreev (property_names);
138
139   /* also for async: we should have a name owner and cached properties */
140   g_dbus_proxy_new (c,
141                     G_DBUS_PROXY_FLAGS_NONE,
142                     NULL,                      /* GDBusInterfaceInfo* */
143                     "com.example.TestService", /* name */
144                     "/com/example/TestObject", /* object path */
145                     "com.example.Frob",        /* interface name */
146                     NULL,                      /* GCancellable */
147                     (GAsyncReadyCallback) proxy_new_cb,
148                     &ap2);
149   g_main_loop_run (loop);
150   name_owner = g_dbus_proxy_get_name_owner (ap2);
151   property_names = g_dbus_proxy_get_cached_property_names (ap2);
152   g_assert (g_dbus_is_unique_name (name_owner));
153   g_assert (property_names != NULL && g_strv_length (property_names) > 0);
154   g_free (name_owner);
155   g_strfreev (property_names);
156
157   /* Check property value is the initial value */
158   variant = g_dbus_proxy_get_cached_property (p, "y");
159   g_assert (variant != NULL);
160   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
161   g_variant_unref (variant);
162   variant = g_dbus_proxy_get_cached_property (p2, "y");
163   g_assert (variant != NULL);
164   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
165   g_variant_unref (variant);
166   variant = g_dbus_proxy_get_cached_property (ap, "y");
167   g_assert (variant != NULL);
168   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
169   g_variant_unref (variant);
170   variant = g_dbus_proxy_get_cached_property (ap2, "y");
171   g_assert (variant != NULL);
172   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
173   g_variant_unref (variant);
174
175   /* Check that properties are updated on both p and p2 */
176   result = g_dbus_proxy_call_sync (p,
177                                    "FrobSetProperty",
178                                    g_variant_new ("(sv)",
179                                                   "y",
180                                                   g_variant_new_byte (42)),
181                                    G_DBUS_CALL_FLAGS_NONE,
182                                    -1,
183                                    NULL,
184                                    &error);
185   g_assert_no_error (error);
186   g_assert (result != NULL);
187   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
188   g_variant_unref (result);
189   _g_assert_signal_received (p, "g-properties-changed");
190   variant = g_dbus_proxy_get_cached_property (p, "y");
191   g_assert (variant != NULL);
192   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
193   g_variant_unref (variant);
194   variant = g_dbus_proxy_get_cached_property (p2, "y");
195   g_assert (variant != NULL);
196   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
197   g_variant_unref (variant);
198   variant = g_dbus_proxy_get_cached_property (ap, "y");
199   g_assert (variant != NULL);
200   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
201   g_variant_unref (variant);
202   variant = g_dbus_proxy_get_cached_property (ap2, "y");
203   g_assert (variant != NULL);
204   g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
205   g_variant_unref (variant);
206
207   /* Nuke the service and check that we get the signal and then don't
208    * have a name owner nor any cached properties
209    */
210   result = g_dbus_proxy_call_sync (p,
211                                    "Quit",
212                                    NULL,
213                                    G_DBUS_CALL_FLAGS_NONE,
214                                    -1,
215                                    NULL,
216                                    &error);
217   g_assert_no_error (error);
218   g_assert (result != NULL);
219   g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
220   g_variant_unref (result);
221   /* and wait... */
222   _g_assert_property_notify (p, "g-name-owner");
223   /* now we shouldn't have a name owner nor any cached properties */
224   g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
225   g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
226   g_assert (g_dbus_proxy_get_cached_property (p, "y") == NULL);
227
228   /* now bring back the server and wait for the proxy to be updated.. now
229    * the 'y' property should be back at 1...
230    */
231   /* this is safe; testserver will exit once the bus goes away */
232   g_assert (g_spawn_command_line_async (SRCDIR "/gdbus-testserver.py", NULL));
233   /* check that we get the notify::g-name-owner signal */
234   _g_assert_property_notify (p, "g-name-owner");
235   /* Now we should have a name owner as well as properties */
236   name_owner = g_dbus_proxy_get_name_owner (p);
237   property_names = g_dbus_proxy_get_cached_property_names (p);
238   g_assert (g_dbus_is_unique_name (name_owner));
239   g_assert (property_names != NULL && g_strv_length (property_names) > 0);
240   g_free (name_owner);
241   g_strfreev (property_names);
242   /* and finally check the 'y' property */
243   variant = g_dbus_proxy_get_cached_property (p, "y");
244   g_assert (variant != NULL);
245   g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
246   g_variant_unref (variant);
247
248   g_object_unref (p2);
249   g_object_unref (p);
250   g_object_unref (ap2);
251   g_object_unref (ap);
252
253   g_object_unref (c);
254
255   /* tear down bus */
256   session_bus_down ();
257 }
258
259 /* ---------------------------------------------------------------------------------------------------- */
260
261 int
262 main (int   argc,
263       char *argv[])
264 {
265   gint ret;
266
267   g_type_init ();
268   g_test_init (&argc, &argv, NULL);
269
270   /* all the tests rely on a shared main loop */
271   loop = g_main_loop_new (NULL, FALSE);
272
273   /* all the tests use a session bus with a well-known address that we can bring up and down
274    * using session_bus_up() and session_bus_down().
275    */
276   g_unsetenv ("DISPLAY");
277   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
278
279   g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);
280
281   ret = g_test_run();
282   return ret;
283 }