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 : : #ifndef _MSC_VER
25 : : #include <unistd.h>
26 : : #endif
27 : :
28 : : #include "gdbus-tests.h"
29 : :
30 : : #include "gdbusprivate.h"
31 : :
32 : : /* ---------------------------------------------------------------------------------------------------- */
33 : :
34 : : typedef struct
35 : : {
36 : : GMainLoop *loop;
37 : : gboolean timed_out;
38 : : } PropertyNotifyData;
39 : :
40 : : static void
41 : 39 : on_property_notify (GObject *object,
42 : : GParamSpec *pspec,
43 : : gpointer user_data)
44 : : {
45 : 39 : PropertyNotifyData *data = user_data;
46 : 39 : g_main_loop_quit (data->loop);
47 : 39 : }
48 : :
49 : : static gboolean
50 : 0 : on_property_notify_timeout (gpointer user_data)
51 : : {
52 : 0 : PropertyNotifyData *data = user_data;
53 : 0 : data->timed_out = TRUE;
54 : 0 : g_main_loop_quit (data->loop);
55 : 0 : return G_SOURCE_CONTINUE;
56 : : }
57 : :
58 : : gboolean
59 : 39 : _g_assert_property_notify_run (gpointer object,
60 : : const gchar *property_name)
61 : : {
62 : : gchar *s;
63 : : gulong handler_id;
64 : : guint timeout_id;
65 : : PropertyNotifyData data;
66 : :
67 : 39 : data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
68 : 39 : data.timed_out = FALSE;
69 : 39 : s = g_strdup_printf ("notify::%s", property_name);
70 : 39 : handler_id = g_signal_connect (object,
71 : : s,
72 : : G_CALLBACK (on_property_notify),
73 : : &data);
74 : 39 : g_free (s);
75 : 39 : timeout_id = g_timeout_add_seconds (30,
76 : : on_property_notify_timeout,
77 : : &data);
78 : 39 : g_main_loop_run (data.loop);
79 : 39 : g_signal_handler_disconnect (object, handler_id);
80 : 39 : g_source_remove (timeout_id);
81 : 39 : g_main_loop_unref (data.loop);
82 : :
83 : 39 : return data.timed_out;
84 : : }
85 : :
86 : : static gboolean
87 : 0 : _give_up (gpointer data)
88 : : {
89 : 0 : g_error ("%s", (const gchar *) data);
90 : : g_return_val_if_reached (G_SOURCE_CONTINUE);
91 : : }
92 : :
93 : : typedef struct
94 : : {
95 : : GMainContext *context;
96 : : gboolean name_appeared;
97 : : gboolean unwatch_complete;
98 : : } WatchData;
99 : :
100 : : static void
101 : 2 : name_appeared_cb (GDBusConnection *connection,
102 : : const gchar *name,
103 : : const gchar *name_owner,
104 : : gpointer user_data)
105 : : {
106 : 2 : WatchData *data = user_data;
107 : :
108 : 2 : g_assert (name_owner != NULL);
109 : 2 : data->name_appeared = TRUE;
110 : 2 : g_main_context_wakeup (data->context);
111 : 2 : }
112 : :
113 : : static void
114 : 2 : watch_free_cb (gpointer user_data)
115 : : {
116 : 2 : WatchData *data = user_data;
117 : :
118 : 2 : data->unwatch_complete = TRUE;
119 : 2 : g_main_context_wakeup (data->context);
120 : 2 : }
121 : :
122 : : void
123 : 2 : ensure_gdbus_testserver_up (GDBusConnection *connection,
124 : : GMainContext *context)
125 : : {
126 : 2 : GSource *timeout_source = NULL;
127 : : guint watch_id;
128 : 2 : WatchData data = { context, FALSE, FALSE };
129 : :
130 : 2 : g_main_context_push_thread_default (context);
131 : :
132 : 2 : watch_id = g_bus_watch_name_on_connection (connection,
133 : : "com.example.TestService",
134 : : G_BUS_NAME_WATCHER_FLAGS_NONE,
135 : : name_appeared_cb,
136 : : NULL,
137 : : &data,
138 : : watch_free_cb);
139 : :
140 : 2 : timeout_source = g_timeout_source_new_seconds (60);
141 : 2 : g_source_set_callback (timeout_source, _give_up,
142 : : "waited more than ~ 60s for gdbus-testserver to take its bus name",
143 : : NULL);
144 : 2 : g_source_attach (timeout_source, context);
145 : :
146 : 4 : while (!data.name_appeared)
147 : 2 : g_main_context_iteration (context, TRUE);
148 : :
149 : 2 : g_bus_unwatch_name (watch_id);
150 : :
151 : 4 : while (!data.unwatch_complete)
152 : 2 : g_main_context_iteration (context, TRUE);
153 : :
154 : 2 : g_source_destroy (timeout_source);
155 : 2 : g_source_unref (timeout_source);
156 : :
157 : 2 : g_main_context_pop_thread_default (context);
158 : 2 : }
159 : :
160 : : /*
161 : : * connection_wait_for_bus:
162 : : * @conn: A connection
163 : : *
164 : : * Wait for asynchronous messages from @conn to have been processed
165 : : * by the message bus, as a sequence point so that we can make
166 : : * "happens before" and "happens after" assertions relative to this.
167 : : * The easiest way to achieve this is to call a message bus method that has
168 : : * no arguments and wait for it to return: because the message bus processes
169 : : * messages in-order, anything we sent before this must have been processed
170 : : * by the time this call arrives.
171 : : */
172 : : void
173 : 118 : connection_wait_for_bus (GDBusConnection *conn)
174 : : {
175 : 118 : GError *error = NULL;
176 : : GVariant *call_result;
177 : :
178 : 118 : call_result = g_dbus_connection_call_sync (conn,
179 : : DBUS_SERVICE_DBUS,
180 : : DBUS_PATH_DBUS,
181 : : DBUS_INTERFACE_DBUS,
182 : : "GetId",
183 : : NULL, /* arguments */
184 : : NULL, /* result type */
185 : : G_DBUS_CALL_FLAGS_NONE,
186 : : -1,
187 : : NULL,
188 : : &error);
189 : 118 : g_assert_no_error (error);
190 : 118 : g_assert_nonnull (call_result);
191 : 118 : g_variant_unref (call_result);
192 : 118 : }
193 : :
194 : : /* ---------------------------------------------------------------------------------------------------- */
195 : :
196 : : typedef struct
197 : : {
198 : : GMainLoop *loop;
199 : : gboolean timed_out;
200 : : } SignalReceivedData;
201 : :
202 : : static void
203 : 29 : on_signal_received (gpointer user_data)
204 : : {
205 : 29 : SignalReceivedData *data = user_data;
206 : 29 : g_main_loop_quit (data->loop);
207 : 29 : }
208 : :
209 : : static gboolean
210 : 0 : on_signal_received_timeout (gpointer user_data)
211 : : {
212 : 0 : SignalReceivedData *data = user_data;
213 : 0 : data->timed_out = TRUE;
214 : 0 : g_main_loop_quit (data->loop);
215 : 0 : return G_SOURCE_CONTINUE;
216 : : }
217 : :
218 : : gboolean
219 : 29 : _g_assert_signal_received_run (gpointer object,
220 : : const gchar *signal_name)
221 : : {
222 : : gulong handler_id;
223 : : guint timeout_id;
224 : : SignalReceivedData data;
225 : :
226 : 29 : data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
227 : 29 : data.timed_out = FALSE;
228 : 29 : handler_id = g_signal_connect_swapped (object,
229 : : signal_name,
230 : : G_CALLBACK (on_signal_received),
231 : : &data);
232 : 29 : timeout_id = g_timeout_add_seconds (30,
233 : : on_signal_received_timeout,
234 : : &data);
235 : 29 : g_main_loop_run (data.loop);
236 : 29 : g_signal_handler_disconnect (object, handler_id);
237 : 29 : g_source_remove (timeout_id);
238 : 29 : g_main_loop_unref (data.loop);
239 : :
240 : 29 : return data.timed_out;
241 : : }
242 : :
243 : : /* ---------------------------------------------------------------------------------------------------- */
244 : :
245 : : GDBusConnection *
246 : 103 : _g_bus_get_priv (GBusType bus_type,
247 : : GCancellable *cancellable,
248 : : GError **error)
249 : : {
250 : : gchar *address;
251 : : GDBusConnection *ret;
252 : :
253 : 103 : ret = NULL;
254 : :
255 : 103 : address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
256 : 103 : if (address == NULL)
257 : 0 : goto out;
258 : :
259 : 103 : ret = g_dbus_connection_new_for_address_sync (address,
260 : : G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
261 : : G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
262 : : NULL, /* GDBusAuthObserver */
263 : : cancellable,
264 : : error);
265 : 103 : g_free (address);
266 : :
267 : 103 : out:
268 : 103 : return ret;
269 : : }
270 : :
271 : : /* ---------------------------------------------------------------------------------------------------- */
|