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.1 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, see <http://www.gnu.org/licenses/>.
17 *
18 * Author: Cosimo Cecchi <cosimoc@gnome.org>
19 */
20
21#include <gio/gio.h>
22#include <unistd.h>
23#include <string.h>
24
25#include "gdbus-tests.h"
26
27/* all tests rely on a shared mainloop */
28static GMainLoop *loop = NULL;
29
30/* ---------------------------------------------------------------------------------------------------- */
31
32static void
33proxy_new_cb (GObject *source_object,
34 GAsyncResult *res,
35 gpointer user_data)
36{
37 GDBusProxy **ret = user_data;
38 GError *error;
39
40 error = NULL;
41 *ret = g_dbus_proxy_new_finish (res, error: &error);
42 g_assert_no_error (error);
43 g_assert_nonnull (ret);
44
45 g_main_loop_quit (loop);
46}
47
48static void
49test_proxy_unique_name (void)
50{
51 GDBusProxy *wp;
52 GDBusProxy *p;
53 GDBusProxy *ap;
54 GDBusConnection *c;
55 GError *error;
56 gchar *name_owner;
57 gchar **property_names;
58 GVariant *variant;
59 GVariant *result;
60 char *unique_name;
61
62 session_bus_up ();
63
64 error = NULL;
65 c = g_bus_get_sync (bus_type: G_BUS_TYPE_SESSION, NULL, error: &error);
66 g_assert_no_error (error);
67 g_assert_nonnull (c);
68
69 /* use a proxy to the well-known name to set things up */
70 wp = g_dbus_proxy_new_sync (connection: c,
71 flags: G_DBUS_PROXY_FLAGS_NONE,
72 NULL, /* GDBusInterfaceInfo* */
73 name: "com.example.TestService", /* name */
74 object_path: "/com/example/TestObject", /* object path */
75 interface_name: "com.example.Frob", /* interface name */
76 NULL, /* GCancellable */
77 error: &error);
78 g_assert_no_error (error);
79
80 /* this is safe; testserver will exit once the bus goes away */
81 g_assert_true (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
82
83 /* check that we get the notify::g-name-owner signal */
84 _g_assert_property_notify (wp, "g-name-owner");
85
86 /* now get the unique name of testserver's connection */
87 unique_name = g_dbus_proxy_get_name_owner (proxy: wp);
88
89 /* if we create another a proxy with the service being available, check that
90 * it has a name owner and properties
91 */
92 error = NULL;
93 p = g_dbus_proxy_new_sync (connection: c,
94 flags: G_DBUS_PROXY_FLAGS_NONE,
95 NULL, /* GDBusInterfaceInfo* */
96 name: unique_name, /* name */
97 object_path: "/com/example/TestObject", /* object path */
98 interface_name: "com.example.Frob", /* interface name */
99 NULL, /* GCancellable */
100 error: &error);
101 g_assert_no_error (error);
102 name_owner = g_dbus_proxy_get_name_owner (proxy: p);
103 property_names = g_dbus_proxy_get_cached_property_names (proxy: p);
104 g_assert_true (g_dbus_is_unique_name (name_owner));
105 g_assert_nonnull (property_names);
106 g_assert_cmpint (g_strv_length (property_names), >, 0);
107 g_free (mem: name_owner);
108 g_strfreev (str_array: property_names);
109
110 /* also for async: we should have a name owner and cached properties */
111 g_dbus_proxy_new (connection: c,
112 flags: G_DBUS_PROXY_FLAGS_NONE,
113 NULL, /* GDBusInterfaceInfo* */
114 name: unique_name, /* name */
115 object_path: "/com/example/TestObject", /* object path */
116 interface_name: "com.example.Frob", /* interface name */
117 NULL, /* GCancellable */
118 callback: (GAsyncReadyCallback) proxy_new_cb,
119 user_data: &ap);
120 g_main_loop_run (loop);
121 name_owner = g_dbus_proxy_get_name_owner (proxy: ap);
122 property_names = g_dbus_proxy_get_cached_property_names (proxy: ap);
123 g_assert_true (g_dbus_is_unique_name (name_owner));
124 g_assert_nonnull (property_names);
125 g_assert_cmpint (g_strv_length (property_names), >, 0);
126 g_free (mem: name_owner);
127 g_strfreev (str_array: property_names);
128
129 /* Check property value is the initial value */
130 variant = g_dbus_proxy_get_cached_property (proxy: p, property_name: "y");
131 g_assert_nonnull (variant);
132 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
133 g_variant_unref (value: variant);
134 variant = g_dbus_proxy_get_cached_property (proxy: ap, property_name: "y");
135 g_assert_nonnull (variant);
136 g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
137 g_variant_unref (value: variant);
138
139 /* Check that properties are updated on p */
140 result = g_dbus_proxy_call_sync (proxy: p,
141 method_name: "FrobSetProperty",
142 parameters: g_variant_new (format_string: "(sv)",
143 "y",
144 g_variant_new_byte (value: 42)),
145 flags: G_DBUS_CALL_FLAGS_NONE,
146 timeout_msec: -1,
147 NULL,
148 error: &error);
149 g_assert_no_error (error);
150 g_assert_nonnull (result);
151 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
152 g_variant_unref (value: result);
153 _g_assert_signal_received (p, "g-properties-changed");
154 variant = g_dbus_proxy_get_cached_property (proxy: p, property_name: "y");
155 g_assert_nonnull (variant);
156 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
157 g_variant_unref (value: variant);
158 variant = g_dbus_proxy_get_cached_property (proxy: ap, property_name: "y");
159 g_assert_nonnull (variant);
160 g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
161 g_variant_unref (value: variant);
162
163 /* Nuke the service and check that we get the signal and then don't
164 * have a name owner nor any cached properties
165 */
166 result = g_dbus_proxy_call_sync (proxy: p,
167 method_name: "Quit",
168 NULL,
169 flags: G_DBUS_CALL_FLAGS_NONE,
170 timeout_msec: -1,
171 NULL,
172 error: &error);
173 g_assert_no_error (error);
174 g_assert_nonnull (result);
175 g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
176 g_variant_unref (value: result);
177 /* and wait... */
178 _g_assert_property_notify (p, "g-name-owner");
179 /* now we shouldn't have a name owner nor any cached properties */
180 g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
181 g_assert_null (g_dbus_proxy_get_cached_property_names (p));
182 g_assert_null (g_dbus_proxy_get_cached_property (p, "y"));
183
184 g_object_unref (object: p);
185 g_object_unref (object: ap);
186
187 g_object_unref (object: wp);
188 g_free (mem: unique_name);
189
190 g_object_unref (object: c);
191
192 /* tear down bus */
193 session_bus_down ();
194}
195
196/* ---------------------------------------------------------------------------------------------------- */
197
198int
199main (int argc,
200 char *argv[])
201{
202 gint ret;
203
204 g_test_init (argc: &argc, argv: &argv, NULL);
205
206 /* all the tests rely on a shared main loop */
207 loop = g_main_loop_new (NULL, FALSE);
208
209 g_test_dbus_unset ();
210
211 g_test_add_func (testpath: "/gdbus/proxy-unique-name", test_func: test_proxy_unique_name);
212
213 ret = g_test_run();
214 return ret;
215}
216

source code of gtk/subprojects/glib/gio/tests/gdbus-proxy-unique-name.c