Branch data Line data Source code
1 : : /* GLib testing framework examples and tests
2 : : *
3 : : * Copyright (C) 2008-2010 Red Hat, Inc.
4 : : *
5 : : * SPDX-License-Identifier: LGPL-2.1-or-later
6 : : *
7 : : * This library is free software; you can redistribute it and/or
8 : : * modify it under the terms of the GNU Lesser General Public
9 : : * License as published by the Free Software Foundation; either
10 : : * version 2.1 of the License, or (at your option) any later version.
11 : : *
12 : : * This library is distributed in the hope that it will be useful,
13 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 : : * Lesser General Public License for more details.
16 : : *
17 : : * You should have received a copy of the GNU Lesser General
18 : : * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
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 : 2 : proxy_new_cb (GObject *source_object,
36 : : GAsyncResult *res,
37 : : gpointer user_data)
38 : : {
39 : 2 : GDBusProxy **ret = user_data;
40 : : GError *error;
41 : :
42 : 2 : error = NULL;
43 : 2 : *ret = g_dbus_proxy_new_finish (res, &error);
44 : 2 : g_assert_no_error (error);
45 : 2 : g_assert (ret != NULL);
46 : :
47 : 2 : g_main_loop_quit (loop);
48 : 2 : }
49 : :
50 : : static void
51 : 1 : 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 : 1 : session_bus_up ();
65 : :
66 : 1 : error = NULL;
67 : 1 : c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, &error);
68 : 1 : g_assert_no_error (error);
69 : 1 : g_assert (c != NULL);
70 : :
71 : 1 : error = NULL;
72 : 1 : p = g_dbus_proxy_new_sync (c,
73 : : G_DBUS_PROXY_FLAGS_NONE,
74 : : NULL, /* GDBusInterfaceInfo* */
75 : : "com.example.TestService", /* name */
76 : : "/com/example/TestObject", /* object path */
77 : : "com.example.Frob", /* interface name */
78 : : NULL, /* GCancellable */
79 : : &error);
80 : 1 : g_assert_no_error (error);
81 : :
82 : : /* we shouldn't have a name owner nor any cached properties */
83 : 1 : g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
84 : 1 : g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
85 : :
86 : : /* also for async: we shouldn't have a name owner nor any cached properties */
87 : 1 : g_dbus_proxy_new (c,
88 : : G_DBUS_PROXY_FLAGS_NONE,
89 : : NULL, /* GDBusInterfaceInfo* */
90 : : "com.example.TestService", /* name */
91 : : "/com/example/TestObject", /* object path */
92 : : "com.example.Frob", /* interface name */
93 : : NULL, /* GCancellable */
94 : : (GAsyncReadyCallback) proxy_new_cb,
95 : : &ap);
96 : 1 : g_main_loop_run (loop);
97 : 1 : g_assert_cmpstr (g_dbus_proxy_get_name_owner (ap), ==, NULL);
98 : 1 : g_assert (g_dbus_proxy_get_cached_property_names (ap) == NULL);
99 : :
100 : : /* this is safe; testserver will exit once the bus goes away */
101 : 1 : g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
102 : :
103 : : /* check that we get the notify::g-name-owner signal */
104 : 1 : _g_assert_property_notify (p, "g-name-owner");
105 : :
106 : : /* Now we should have a name owner as well as properties */
107 : 1 : name_owner = g_dbus_proxy_get_name_owner (p);
108 : 1 : property_names = g_dbus_proxy_get_cached_property_names (p);
109 : 1 : g_assert (g_dbus_is_unique_name (name_owner));
110 : 1 : g_assert (property_names != NULL && g_strv_length (property_names) > 0);
111 : 1 : g_free (name_owner);
112 : 1 : g_strfreev (property_names);
113 : :
114 : : /* if we create another proxy with the service being available, check that
115 : : * it has a name owner and properties
116 : : */
117 : 1 : error = NULL;
118 : 1 : p2 = g_dbus_proxy_new_sync (c,
119 : : G_DBUS_PROXY_FLAGS_NONE,
120 : : NULL, /* GDBusInterfaceInfo* */
121 : : "com.example.TestService", /* name */
122 : : "/com/example/TestObject", /* object path */
123 : : "com.example.Frob", /* interface name */
124 : : NULL, /* GCancellable */
125 : : &error);
126 : 1 : g_assert_no_error (error);
127 : 1 : name_owner = g_dbus_proxy_get_name_owner (p2);
128 : 1 : property_names = g_dbus_proxy_get_cached_property_names (p2);
129 : 1 : g_assert (g_dbus_is_unique_name (name_owner));
130 : 1 : g_assert (property_names != NULL && g_strv_length (property_names) > 0);
131 : 1 : g_free (name_owner);
132 : 1 : g_strfreev (property_names);
133 : :
134 : : /* also for async: we should have a name owner and cached properties */
135 : 1 : g_dbus_proxy_new (c,
136 : : G_DBUS_PROXY_FLAGS_NONE,
137 : : NULL, /* GDBusInterfaceInfo* */
138 : : "com.example.TestService", /* name */
139 : : "/com/example/TestObject", /* object path */
140 : : "com.example.Frob", /* interface name */
141 : : NULL, /* GCancellable */
142 : : (GAsyncReadyCallback) proxy_new_cb,
143 : : &ap2);
144 : 1 : g_main_loop_run (loop);
145 : 1 : name_owner = g_dbus_proxy_get_name_owner (ap2);
146 : 1 : property_names = g_dbus_proxy_get_cached_property_names (ap2);
147 : 1 : g_assert (g_dbus_is_unique_name (name_owner));
148 : 1 : g_assert (property_names != NULL && g_strv_length (property_names) > 0);
149 : 1 : g_free (name_owner);
150 : 1 : g_strfreev (property_names);
151 : :
152 : : /* Check property value is the initial value */
153 : 1 : variant = g_dbus_proxy_get_cached_property (p, "y");
154 : 1 : g_assert (variant != NULL);
155 : 1 : g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
156 : 1 : g_variant_unref (variant);
157 : 1 : variant = g_dbus_proxy_get_cached_property (p2, "y");
158 : 1 : g_assert (variant != NULL);
159 : 1 : g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
160 : 1 : g_variant_unref (variant);
161 : 1 : variant = g_dbus_proxy_get_cached_property (ap, "y");
162 : 1 : g_assert (variant != NULL);
163 : 1 : g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
164 : 1 : g_variant_unref (variant);
165 : 1 : variant = g_dbus_proxy_get_cached_property (ap2, "y");
166 : 1 : g_assert (variant != NULL);
167 : 1 : g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
168 : 1 : g_variant_unref (variant);
169 : :
170 : : /* Check that properties are updated on both p and p2 */
171 : 1 : result = g_dbus_proxy_call_sync (p,
172 : : "FrobSetProperty",
173 : : g_variant_new ("(sv)",
174 : : "y",
175 : : g_variant_new_byte (42)),
176 : : G_DBUS_CALL_FLAGS_NONE,
177 : : -1,
178 : : NULL,
179 : : &error);
180 : 1 : g_assert_no_error (error);
181 : 1 : g_assert (result != NULL);
182 : 1 : g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
183 : 1 : g_variant_unref (result);
184 : 1 : _g_assert_signal_received (p, "g-properties-changed");
185 : 1 : variant = g_dbus_proxy_get_cached_property (p, "y");
186 : 1 : g_assert (variant != NULL);
187 : 1 : g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
188 : 1 : g_variant_unref (variant);
189 : 1 : variant = g_dbus_proxy_get_cached_property (p2, "y");
190 : 1 : g_assert (variant != NULL);
191 : 1 : g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
192 : 1 : g_variant_unref (variant);
193 : 1 : variant = g_dbus_proxy_get_cached_property (ap, "y");
194 : 1 : g_assert (variant != NULL);
195 : 1 : g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
196 : 1 : g_variant_unref (variant);
197 : 1 : variant = g_dbus_proxy_get_cached_property (ap2, "y");
198 : 1 : g_assert (variant != NULL);
199 : 1 : g_assert_cmpint (g_variant_get_byte (variant), ==, 42);
200 : 1 : g_variant_unref (variant);
201 : :
202 : : /* Nuke the service and check that we get the signal and then don't
203 : : * have a name owner nor any cached properties
204 : : */
205 : 1 : result = g_dbus_proxy_call_sync (p,
206 : : "Quit",
207 : : NULL,
208 : : G_DBUS_CALL_FLAGS_NONE,
209 : : -1,
210 : : NULL,
211 : : &error);
212 : 1 : g_assert_no_error (error);
213 : 1 : g_assert (result != NULL);
214 : 1 : g_assert_cmpstr (g_variant_get_type_string (result), ==, "()");
215 : 1 : g_variant_unref (result);
216 : : /* and wait... */
217 : 1 : _g_assert_property_notify (p, "g-name-owner");
218 : : /* now we shouldn't have a name owner nor any cached properties */
219 : 1 : g_assert_cmpstr (g_dbus_proxy_get_name_owner (p), ==, NULL);
220 : 1 : g_assert (g_dbus_proxy_get_cached_property_names (p) == NULL);
221 : 1 : g_assert (g_dbus_proxy_get_cached_property (p, "y") == NULL);
222 : :
223 : : /* now bring back the server and wait for the proxy to be updated.. now
224 : : * the 'y' property should be back at 1...
225 : : */
226 : : /* this is safe; testserver will exit once the bus goes away */
227 : 1 : g_assert (g_spawn_command_line_async (g_test_get_filename (G_TEST_BUILT, "gdbus-testserver", NULL), NULL));
228 : :
229 : : /* check that we get the notify::g-name-owner signal */
230 : 1 : _g_assert_property_notify (p, "g-name-owner");
231 : : /* Now we should have a name owner as well as properties */
232 : 1 : name_owner = g_dbus_proxy_get_name_owner (p);
233 : 1 : property_names = g_dbus_proxy_get_cached_property_names (p);
234 : 1 : g_assert (g_dbus_is_unique_name (name_owner));
235 : 1 : g_assert (property_names != NULL && g_strv_length (property_names) > 0);
236 : 1 : g_free (name_owner);
237 : 1 : g_strfreev (property_names);
238 : : /* and finally check the 'y' property */
239 : 1 : variant = g_dbus_proxy_get_cached_property (p, "y");
240 : 1 : g_assert (variant != NULL);
241 : 1 : g_assert_cmpint (g_variant_get_byte (variant), ==, 1);
242 : 1 : g_variant_unref (variant);
243 : :
244 : 1 : g_object_unref (p2);
245 : 1 : g_object_unref (p);
246 : 1 : g_object_unref (ap2);
247 : 1 : g_object_unref (ap);
248 : :
249 : 1 : g_object_unref (c);
250 : :
251 : : /* tear down bus */
252 : 1 : session_bus_down ();
253 : 1 : }
254 : :
255 : : /* ---------------------------------------------------------------------------------------------------- */
256 : :
257 : : int
258 : 1 : main (int argc,
259 : : char *argv[])
260 : : {
261 : : gint ret;
262 : :
263 : 1 : g_test_init (&argc, &argv, G_TEST_OPTION_ISOLATE_DIRS, NULL);
264 : :
265 : : /* all the tests rely on a shared main loop */
266 : 1 : loop = g_main_loop_new (NULL, FALSE);
267 : :
268 : 1 : g_test_dbus_unset ();
269 : :
270 : 1 : g_test_add_func ("/gdbus/proxy-well-known-name", test_proxy_well_known_name);
271 : :
272 : 1 : ret = g_test_run();
273 : 1 : return ret;
274 : : }
|