1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2010 Red Hat, Inc.
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.
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.
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/>.
18 * Author: David Zeuthen <davidz@redhat.com>
24 #include "gdbus-tests.h"
26 /* all tests rely on a shared mainloop */
27 static GMainLoop
*loop
;
29 /* ---------------------------------------------------------------------------------------------------- */
30 /* Test that g_bus_own_name() works correctly */
31 /* ---------------------------------------------------------------------------------------------------- */
36 gboolean expect_null_connection
;
37 guint num_bus_acquired
;
44 own_name_data_free_func (OwnNameData
*data
)
46 data
->num_free_func
++;
47 g_main_loop_quit (loop
);
51 bus_acquired_handler (GDBusConnection
*connection
,
55 OwnNameData
*data
= user_data
;
56 g_dbus_connection_set_exit_on_close (connection
, FALSE
);
57 data
->num_bus_acquired
+= 1;
58 g_main_loop_quit (loop
);
62 name_acquired_handler (GDBusConnection
*connection
,
66 OwnNameData
*data
= user_data
;
67 data
->num_acquired
+= 1;
68 g_main_loop_quit (loop
);
72 name_lost_handler (GDBusConnection
*connection
,
76 OwnNameData
*data
= user_data
;
77 if (data
->expect_null_connection
)
79 g_assert (connection
== NULL
);
83 g_assert (connection
!= NULL
);
84 g_dbus_connection_set_exit_on_close (connection
, FALSE
);
87 g_main_loop_quit (loop
);
91 test_bus_own_name (void)
100 gboolean name_has_owner_reply
;
105 name
= "org.gtk.GDBus.Name1";
108 * First check that name_lost_handler() is invoked if there is no bus.
110 * Also make sure name_lost_handler() isn't invoked when unowning the name.
112 data
.num_bus_acquired
= 0;
113 data
.num_free_func
= 0;
114 data
.num_acquired
= 0;
116 data
.expect_null_connection
= TRUE
;
117 id
= g_bus_own_name (G_BUS_TYPE_SESSION
,
119 G_BUS_NAME_OWNER_FLAGS_NONE
,
120 bus_acquired_handler
,
121 name_acquired_handler
,
124 (GDestroyNotify
) own_name_data_free_func
);
125 g_assert_cmpint (data
.num_bus_acquired
, ==, 0);
126 g_assert_cmpint (data
.num_acquired
, ==, 0);
127 g_assert_cmpint (data
.num_lost
, ==, 0);
128 g_main_loop_run (loop
);
129 g_assert_cmpint (data
.num_bus_acquired
, ==, 0);
130 g_assert_cmpint (data
.num_acquired
, ==, 0);
131 g_assert_cmpint (data
.num_lost
, ==, 1);
132 g_bus_unown_name (id
);
133 g_assert_cmpint (data
.num_acquired
, ==, 0);
134 g_assert_cmpint (data
.num_lost
, ==, 1);
135 g_assert_cmpint (data
.num_free_func
, ==, 1);
138 * Bring up a bus, then own a name and check bus_acquired_handler() then name_acquired_handler() is invoked.
141 data
.num_bus_acquired
= 0;
142 data
.num_acquired
= 0;
144 data
.expect_null_connection
= FALSE
;
145 id
= g_bus_own_name (G_BUS_TYPE_SESSION
,
147 G_BUS_NAME_OWNER_FLAGS_NONE
,
148 bus_acquired_handler
,
149 name_acquired_handler
,
152 (GDestroyNotify
) own_name_data_free_func
);
153 g_assert_cmpint (data
.num_bus_acquired
, ==, 0);
154 g_assert_cmpint (data
.num_acquired
, ==, 0);
155 g_assert_cmpint (data
.num_lost
, ==, 0);
156 g_main_loop_run (loop
);
157 g_assert_cmpint (data
.num_bus_acquired
, ==, 1);
158 g_assert_cmpint (data
.num_acquired
, ==, 0);
159 g_assert_cmpint (data
.num_lost
, ==, 0);
160 g_main_loop_run (loop
);
161 g_assert_cmpint (data
.num_bus_acquired
, ==, 1);
162 g_assert_cmpint (data
.num_acquired
, ==, 1);
163 g_assert_cmpint (data
.num_lost
, ==, 0);
166 * Check that the name was actually acquired.
168 c
= g_bus_get_sync (G_BUS_TYPE_SESSION
, NULL
, NULL
);
169 g_assert (c
!= NULL
);
170 g_assert (!g_dbus_connection_is_closed (c
));
171 result
= g_dbus_connection_call_sync (c
,
172 "org.freedesktop.DBus", /* bus name */
173 "/org/freedesktop/DBus", /* object path */
174 "org.freedesktop.DBus", /* interface name */
175 "NameHasOwner", /* method name */
176 g_variant_new ("(s)", name
),
177 G_VARIANT_TYPE ("(b)"),
178 G_DBUS_CALL_FLAGS_NONE
,
182 g_assert_no_error (error
);
183 g_assert (result
!= NULL
);
184 g_variant_get (result
, "(b)", &name_has_owner_reply
);
185 g_assert (name_has_owner_reply
);
186 g_variant_unref (result
);
189 * Stop owning the name - this should invoke our free func
191 g_bus_unown_name (id
);
192 g_assert_cmpint (data
.num_free_func
, ==, 2);
195 * Check that the name was actually released.
197 result
= g_dbus_connection_call_sync (c
,
198 "org.freedesktop.DBus", /* bus name */
199 "/org/freedesktop/DBus", /* object path */
200 "org.freedesktop.DBus", /* interface name */
201 "NameHasOwner", /* method name */
202 g_variant_new ("(s)", name
),
203 G_VARIANT_TYPE ("(b)"),
204 G_DBUS_CALL_FLAGS_NONE
,
208 g_assert_no_error (error
);
209 g_assert (result
!= NULL
);
210 g_variant_get (result
, "(b)", &name_has_owner_reply
);
211 g_assert (!name_has_owner_reply
);
212 g_variant_unref (result
);
214 /* Now try owning the name and then immediately decide to unown the name */
215 g_assert_cmpint (data
.num_bus_acquired
, ==, 1);
216 g_assert_cmpint (data
.num_acquired
, ==, 1);
217 g_assert_cmpint (data
.num_lost
, ==, 0);
218 g_assert_cmpint (data
.num_free_func
, ==, 2);
219 id
= g_bus_own_name (G_BUS_TYPE_SESSION
,
221 G_BUS_NAME_OWNER_FLAGS_NONE
,
222 bus_acquired_handler
,
223 name_acquired_handler
,
226 (GDestroyNotify
) own_name_data_free_func
);
227 g_assert_cmpint (data
.num_bus_acquired
, ==, 1);
228 g_assert_cmpint (data
.num_acquired
, ==, 1);
229 g_assert_cmpint (data
.num_lost
, ==, 0);
230 g_assert_cmpint (data
.num_free_func
, ==, 2);
231 g_bus_unown_name (id
);
232 g_assert_cmpint (data
.num_bus_acquired
, ==, 1);
233 g_assert_cmpint (data
.num_acquired
, ==, 1);
234 g_assert_cmpint (data
.num_lost
, ==, 0);
235 g_assert_cmpint (data
.num_free_func
, ==, 2);
236 g_main_loop_run (loop
); /* the GDestroyNotify is called in idle because the bus is acquired in idle */
237 g_assert_cmpint (data
.num_free_func
, ==, 3);
240 * Own the name again.
242 data
.num_bus_acquired
= 0;
243 data
.num_acquired
= 0;
245 data
.expect_null_connection
= FALSE
;
246 id
= g_bus_own_name_with_closures (G_BUS_TYPE_SESSION
,
248 G_BUS_NAME_OWNER_FLAGS_NONE
,
249 g_cclosure_new (G_CALLBACK (bus_acquired_handler
),
252 g_cclosure_new (G_CALLBACK (name_acquired_handler
),
255 g_cclosure_new (G_CALLBACK (name_lost_handler
),
257 (GClosureNotify
) own_name_data_free_func
));
258 g_assert_cmpint (data
.num_bus_acquired
, ==, 0);
259 g_assert_cmpint (data
.num_acquired
, ==, 0);
260 g_assert_cmpint (data
.num_lost
, ==, 0);
261 g_main_loop_run (loop
);
262 g_assert_cmpint (data
.num_bus_acquired
, ==, 1);
263 g_assert_cmpint (data
.num_acquired
, ==, 0);
264 g_assert_cmpint (data
.num_lost
, ==, 0);
265 g_main_loop_run (loop
);
266 g_assert_cmpint (data
.num_bus_acquired
, ==, 1);
267 g_assert_cmpint (data
.num_acquired
, ==, 1);
268 g_assert_cmpint (data
.num_lost
, ==, 0);
271 * Try owning the name with another object on the same connection - this should
272 * fail because we already own the name.
274 data2
.num_free_func
= 0;
275 data2
.num_bus_acquired
= 0;
276 data2
.num_acquired
= 0;
278 data2
.expect_null_connection
= FALSE
;
279 id2
= g_bus_own_name (G_BUS_TYPE_SESSION
,
281 G_BUS_NAME_OWNER_FLAGS_NONE
,
282 bus_acquired_handler
,
283 name_acquired_handler
,
286 (GDestroyNotify
) own_name_data_free_func
);
287 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
288 g_assert_cmpint (data2
.num_acquired
, ==, 0);
289 g_assert_cmpint (data2
.num_lost
, ==, 0);
290 g_main_loop_run (loop
);
291 g_assert_cmpint (data2
.num_bus_acquired
, ==, 1);
292 g_assert_cmpint (data2
.num_acquired
, ==, 0);
293 g_assert_cmpint (data2
.num_lost
, ==, 0);
294 g_main_loop_run (loop
);
295 g_assert_cmpint (data2
.num_bus_acquired
, ==, 1);
296 g_assert_cmpint (data2
.num_acquired
, ==, 0);
297 g_assert_cmpint (data2
.num_lost
, ==, 1);
298 g_bus_unown_name (id2
);
299 g_assert_cmpint (data2
.num_bus_acquired
, ==, 1);
300 g_assert_cmpint (data2
.num_acquired
, ==, 0);
301 g_assert_cmpint (data2
.num_lost
, ==, 1);
302 g_assert_cmpint (data2
.num_free_func
, ==, 1);
305 * Create a secondary (e.g. private) connection and try owning the name on that
306 * connection. This should fail both with and without _REPLACE because we
307 * didn't specify ALLOW_REPLACEMENT.
309 c2
= _g_bus_get_priv (G_BUS_TYPE_SESSION
, NULL
, NULL
);
310 g_assert (c2
!= NULL
);
311 g_assert (!g_dbus_connection_is_closed (c2
));
312 /* first without _REPLACE */
313 data2
.num_bus_acquired
= 0;
314 data2
.num_acquired
= 0;
316 data2
.expect_null_connection
= FALSE
;
317 data2
.num_free_func
= 0;
318 id2
= g_bus_own_name_on_connection (c2
,
320 G_BUS_NAME_OWNER_FLAGS_NONE
,
321 name_acquired_handler
,
324 (GDestroyNotify
) own_name_data_free_func
);
325 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
326 g_assert_cmpint (data2
.num_acquired
, ==, 0);
327 g_assert_cmpint (data2
.num_lost
, ==, 0);
328 g_main_loop_run (loop
);
329 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
330 g_assert_cmpint (data2
.num_acquired
, ==, 0);
331 g_assert_cmpint (data2
.num_lost
, ==, 1);
332 g_bus_unown_name (id2
);
333 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
334 g_assert_cmpint (data2
.num_acquired
, ==, 0);
335 g_assert_cmpint (data2
.num_lost
, ==, 1);
336 g_assert_cmpint (data2
.num_free_func
, ==, 1);
337 /* then with _REPLACE */
338 data2
.num_bus_acquired
= 0;
339 data2
.num_acquired
= 0;
341 data2
.expect_null_connection
= FALSE
;
342 data2
.num_free_func
= 0;
343 id2
= g_bus_own_name_on_connection (c2
,
345 G_BUS_NAME_OWNER_FLAGS_REPLACE
,
346 name_acquired_handler
,
349 (GDestroyNotify
) own_name_data_free_func
);
350 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
351 g_assert_cmpint (data2
.num_acquired
, ==, 0);
352 g_assert_cmpint (data2
.num_lost
, ==, 0);
353 g_main_loop_run (loop
);
354 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
355 g_assert_cmpint (data2
.num_acquired
, ==, 0);
356 g_assert_cmpint (data2
.num_lost
, ==, 1);
357 g_bus_unown_name (id2
);
358 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
359 g_assert_cmpint (data2
.num_acquired
, ==, 0);
360 g_assert_cmpint (data2
.num_lost
, ==, 1);
361 g_assert_cmpint (data2
.num_free_func
, ==, 1);
364 * Stop owning the name and grab it again with _ALLOW_REPLACEMENT.
366 data
.expect_null_connection
= FALSE
;
367 g_bus_unown_name (id
);
368 g_assert_cmpint (data
.num_bus_acquired
, ==, 1);
369 g_assert_cmpint (data
.num_acquired
, ==, 1);
370 g_assert_cmpint (data
.num_free_func
, ==, 4);
372 data
.num_bus_acquired
= 0;
373 data
.num_acquired
= 0;
375 data
.expect_null_connection
= FALSE
;
376 id
= g_bus_own_name (G_BUS_TYPE_SESSION
,
378 G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT
,
379 bus_acquired_handler
,
380 name_acquired_handler
,
383 (GDestroyNotify
) own_name_data_free_func
);
384 g_assert_cmpint (data
.num_bus_acquired
, ==, 0);
385 g_assert_cmpint (data
.num_acquired
, ==, 0);
386 g_assert_cmpint (data
.num_lost
, ==, 0);
387 g_main_loop_run (loop
);
388 g_assert_cmpint (data
.num_bus_acquired
, ==, 1);
389 g_assert_cmpint (data
.num_acquired
, ==, 0);
390 g_assert_cmpint (data
.num_lost
, ==, 0);
391 g_main_loop_run (loop
);
392 g_assert_cmpint (data
.num_bus_acquired
, ==, 1);
393 g_assert_cmpint (data
.num_acquired
, ==, 1);
394 g_assert_cmpint (data
.num_lost
, ==, 0);
397 * Now try to grab the name from the secondary connection.
400 /* first without _REPLACE - this won't make us acquire the name */
401 data2
.num_bus_acquired
= 0;
402 data2
.num_acquired
= 0;
404 data2
.expect_null_connection
= FALSE
;
405 data2
.num_free_func
= 0;
406 id2
= g_bus_own_name_on_connection (c2
,
408 G_BUS_NAME_OWNER_FLAGS_NONE
,
409 name_acquired_handler
,
412 (GDestroyNotify
) own_name_data_free_func
);
413 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
414 g_assert_cmpint (data2
.num_acquired
, ==, 0);
415 g_assert_cmpint (data2
.num_lost
, ==, 0);
416 g_main_loop_run (loop
);
417 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
418 g_assert_cmpint (data2
.num_acquired
, ==, 0);
419 g_assert_cmpint (data2
.num_lost
, ==, 1);
420 g_bus_unown_name (id2
);
421 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
422 g_assert_cmpint (data2
.num_acquired
, ==, 0);
423 g_assert_cmpint (data2
.num_lost
, ==, 1);
424 g_assert_cmpint (data2
.num_free_func
, ==, 1);
425 /* then with _REPLACE - here we should acquire the name - e.g. owner should lose it
426 * and owner2 should acquire it */
427 data2
.num_bus_acquired
= 0;
428 data2
.num_acquired
= 0;
430 data2
.expect_null_connection
= FALSE
;
431 data2
.num_free_func
= 0;
432 id2
= g_bus_own_name_on_connection (c2
,
434 G_BUS_NAME_OWNER_FLAGS_REPLACE
,
435 name_acquired_handler
,
438 (GDestroyNotify
) own_name_data_free_func
);
439 g_assert_cmpint (data
.num_acquired
, ==, 1);
440 g_assert_cmpint (data
.num_lost
, ==, 0);
441 g_assert_cmpint (data2
.num_acquired
, ==, 0);
442 g_assert_cmpint (data2
.num_lost
, ==, 0);
443 /* wait for handlers for both owner and owner2 to fire */
444 while (data
.num_lost
== 0 || data2
.num_acquired
== 0)
445 g_main_loop_run (loop
);
446 g_assert_cmpint (data
.num_acquired
, ==, 1);
447 g_assert_cmpint (data
.num_lost
, ==, 1);
448 g_assert_cmpint (data2
.num_acquired
, ==, 1);
449 g_assert_cmpint (data2
.num_lost
, ==, 0);
450 g_assert_cmpint (data2
.num_bus_acquired
, ==, 0);
451 /* ok, make owner2 release the name - then wait for owner to automagically reacquire it */
452 g_bus_unown_name (id2
);
453 g_assert_cmpint (data2
.num_free_func
, ==, 1);
454 g_main_loop_run (loop
);
455 g_assert_cmpint (data
.num_acquired
, ==, 2);
456 g_assert_cmpint (data
.num_lost
, ==, 1);
459 * Finally, nuke the bus and check name_lost_handler() is invoked.
462 data
.expect_null_connection
= TRUE
;
464 while (data
.num_lost
!= 2)
465 g_main_loop_run (loop
);
466 g_assert_cmpint (data
.num_acquired
, ==, 2);
467 g_assert_cmpint (data
.num_lost
, ==, 2);
468 g_bus_unown_name (id
);
469 g_assert_cmpint (data
.num_free_func
, ==, 5);
476 /* See https://bugzilla.gnome.org/show_bug.cgi?id=711807 */
480 /* ---------------------------------------------------------------------------------------------------- */
481 /* Test that g_bus_watch_name() works correctly */
482 /* ---------------------------------------------------------------------------------------------------- */
486 gboolean expect_null_connection
;
495 watch_name_data_free_func (WatchNameData
*data
)
497 data
->num_free_func
++;
498 g_main_loop_quit (loop
);
502 w_bus_acquired_handler (GDBusConnection
*connection
,
509 w_name_acquired_handler (GDBusConnection
*connection
,
513 WatchNameData
*data
= user_data
;
514 data
->num_acquired
+= 1;
515 g_main_loop_quit (loop
);
519 w_name_lost_handler (GDBusConnection
*connection
,
523 WatchNameData
*data
= user_data
;
525 g_main_loop_quit (loop
);
529 name_appeared_handler (GDBusConnection
*connection
,
531 const gchar
*name_owner
,
534 WatchNameData
*data
= user_data
;
535 if (data
->expect_null_connection
)
537 g_assert (connection
== NULL
);
541 g_assert (connection
!= NULL
);
542 g_dbus_connection_set_exit_on_close (connection
, FALSE
);
544 data
->num_appeared
+= 1;
545 g_main_loop_quit (loop
);
549 name_vanished_handler (GDBusConnection
*connection
,
553 WatchNameData
*data
= user_data
;
554 if (data
->expect_null_connection
)
556 g_assert (connection
== NULL
);
560 g_assert (connection
!= NULL
);
561 g_dbus_connection_set_exit_on_close (connection
, FALSE
);
563 data
->num_vanished
+= 1;
564 g_main_loop_quit (loop
);
568 test_bus_watch_name (void)
573 GDBusConnection
*connection
;
576 * First check that name_vanished_handler() is invoked if there is no bus.
578 * Also make sure name_vanished_handler() isn't invoked when unwatching the name.
580 data
.num_free_func
= 0;
581 data
.num_appeared
= 0;
582 data
.num_vanished
= 0;
583 data
.expect_null_connection
= TRUE
;
584 id
= g_bus_watch_name (G_BUS_TYPE_SESSION
,
585 "org.gtk.GDBus.Name1",
586 G_BUS_NAME_WATCHER_FLAGS_NONE
,
587 name_appeared_handler
,
588 name_vanished_handler
,
590 (GDestroyNotify
) watch_name_data_free_func
);
591 g_assert_cmpint (data
.num_appeared
, ==, 0);
592 g_assert_cmpint (data
.num_vanished
, ==, 0);
593 g_main_loop_run (loop
);
594 g_assert_cmpint (data
.num_appeared
, ==, 0);
595 g_assert_cmpint (data
.num_vanished
, ==, 1);
596 g_bus_unwatch_name (id
);
597 g_assert_cmpint (data
.num_appeared
, ==, 0);
598 g_assert_cmpint (data
.num_vanished
, ==, 1);
599 g_assert_cmpint (data
.num_free_func
, ==, 1);
602 * Now bring up a bus, own a name, and then start watching it.
606 data
.num_free_func
= 0;
607 data
.num_acquired
= 0;
609 data
.expect_null_connection
= FALSE
;
610 owner_id
= g_bus_own_name (G_BUS_TYPE_SESSION
,
611 "org.gtk.GDBus.Name1",
612 G_BUS_NAME_OWNER_FLAGS_NONE
,
613 w_bus_acquired_handler
,
614 w_name_acquired_handler
,
617 (GDestroyNotify
) watch_name_data_free_func
);
618 g_main_loop_run (loop
);
619 g_assert_cmpint (data
.num_acquired
, ==, 1);
620 g_assert_cmpint (data
.num_lost
, ==, 0);
622 connection
= g_bus_get_sync (G_BUS_TYPE_SESSION
, NULL
, NULL
);
623 g_assert (connection
!= NULL
);
625 /* now watch the name */
626 data
.num_appeared
= 0;
627 data
.num_vanished
= 0;
628 id
= g_bus_watch_name_on_connection (connection
,
629 "org.gtk.GDBus.Name1",
630 G_BUS_NAME_WATCHER_FLAGS_NONE
,
631 name_appeared_handler
,
632 name_vanished_handler
,
634 (GDestroyNotify
) watch_name_data_free_func
);
635 g_assert_cmpint (data
.num_appeared
, ==, 0);
636 g_assert_cmpint (data
.num_vanished
, ==, 0);
637 g_main_loop_run (loop
);
638 g_assert_cmpint (data
.num_appeared
, ==, 1);
639 g_assert_cmpint (data
.num_vanished
, ==, 0);
644 g_bus_unwatch_name (id
);
645 g_assert_cmpint (data
.num_free_func
, ==, 1);
647 g_object_unref (connection
);
650 g_bus_unown_name (owner_id
);
651 g_assert_cmpint (data
.num_acquired
, ==, 1);
652 g_assert_cmpint (data
.num_free_func
, ==, 2);
655 * Create a watcher and then make a name be owned.
657 * This should trigger name_appeared_handler() ...
660 data
.num_appeared
= 0;
661 data
.num_vanished
= 0;
662 data
.num_free_func
= 0;
663 id
= g_bus_watch_name_with_closures (G_BUS_TYPE_SESSION
,
664 "org.gtk.GDBus.Name1",
665 G_BUS_NAME_WATCHER_FLAGS_NONE
,
666 g_cclosure_new (G_CALLBACK (name_appeared_handler
),
669 g_cclosure_new (G_CALLBACK (name_vanished_handler
),
671 (GClosureNotify
) watch_name_data_free_func
));
672 g_assert_cmpint (data
.num_appeared
, ==, 0);
673 g_assert_cmpint (data
.num_vanished
, ==, 0);
674 g_main_loop_run (loop
);
675 g_assert_cmpint (data
.num_appeared
, ==, 0);
676 g_assert_cmpint (data
.num_vanished
, ==, 1);
679 data
.num_acquired
= 0;
681 data
.expect_null_connection
= FALSE
;
682 owner_id
= g_bus_own_name (G_BUS_TYPE_SESSION
,
683 "org.gtk.GDBus.Name1",
684 G_BUS_NAME_OWNER_FLAGS_NONE
,
685 w_bus_acquired_handler
,
686 w_name_acquired_handler
,
689 (GDestroyNotify
) watch_name_data_free_func
);
690 while (data
.num_acquired
== 0 || data
.num_appeared
== 0)
691 g_main_loop_run (loop
);
692 g_assert_cmpint (data
.num_acquired
, ==, 1);
693 g_assert_cmpint (data
.num_lost
, ==, 0);
694 g_assert_cmpint (data
.num_appeared
, ==, 1);
695 g_assert_cmpint (data
.num_vanished
, ==, 1);
698 * Nuke the bus and check that the name vanishes and is lost.
700 data
.expect_null_connection
= TRUE
;
702 g_main_loop_run (loop
);
703 g_assert_cmpint (data
.num_lost
, ==, 1);
704 g_assert_cmpint (data
.num_vanished
, ==, 2);
706 g_bus_unwatch_name (id
);
707 g_assert_cmpint (data
.num_free_func
, ==, 1);
709 g_bus_unown_name (owner_id
);
710 g_assert_cmpint (data
.num_free_func
, ==, 2);
715 /* ---------------------------------------------------------------------------------------------------- */
718 test_validate_names (void)
728 { 1, 0, 1, "valid.well_known.name"},
729 { 1, 0, 0, "valid.well-known.name"},
730 { 1, 1, 0, ":valid.unique.name"},
731 { 0, 0, 0, "invalid.5well_known.name"},
732 { 0, 0, 0, "4invalid.5well_known.name"},
733 { 1, 1, 0, ":4valid.5unique.name"},
735 { 1, 0, 1, "very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.name1"}, /* 255 */
736 { 0, 0, 0, "very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.very.long.name12"}, /* 256 - too long! */
737 { 0, 0, 0, ".starts.with.a.dot"},
738 { 0, 0, 0, "contains.invalid;.characters"},
739 { 0, 0, 0, "contains.inva/lid.characters"},
740 { 0, 0, 0, "contains.inva[lid.characters"},
741 { 0, 0, 0, "contains.inva]lid.characters"},
742 { 0, 0, 0, "contains.inva_æøå_lid.characters"},
746 for (n
= 0; n
< G_N_ELEMENTS (names
); n
++)
749 g_assert (g_dbus_is_name (names
[n
].string
));
751 g_assert (!g_dbus_is_name (names
[n
].string
));
754 g_assert (g_dbus_is_unique_name (names
[n
].string
));
756 g_assert (!g_dbus_is_unique_name (names
[n
].string
));
758 if (names
[n
].interface
)
759 g_assert (g_dbus_is_interface_name (names
[n
].string
));
761 g_assert (!g_dbus_is_interface_name (names
[n
].string
));
765 /* ---------------------------------------------------------------------------------------------------- */
773 g_test_init (&argc
, &argv
, NULL
);
775 loop
= g_main_loop_new (NULL
, FALSE
);
777 g_test_dbus_unset ();
779 g_test_add_func ("/gdbus/validate-names", test_validate_names
);
780 g_test_add_func ("/gdbus/bus-own-name", test_bus_own_name
);
781 g_test_add_func ("/gdbus/bus-watch-name", test_bus_watch_name
);
785 g_main_loop_unref (loop
);