Make the closure variants of name owning and watching actually work
[platform/upstream/glib.git] / gio / tests / gdbus-names.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
26 #include "gdbus-tests.h"
27
28 /* all tests rely on a shared mainloop */
29 static GMainLoop *loop;
30
31 /* ---------------------------------------------------------------------------------------------------- */
32 /* Test that g_bus_own_name() works correctly */
33 /* ---------------------------------------------------------------------------------------------------- */
34
35 typedef struct
36 {
37   GMainLoop *loop;
38   gboolean expect_null_connection;
39   guint num_bus_acquired;
40   guint num_acquired;
41   guint num_lost;
42   guint num_free_func;
43 } OwnNameData;
44
45 static void
46 own_name_data_free_func (OwnNameData *data)
47 {
48   data->num_free_func++;
49   g_main_loop_quit (loop);
50 }
51
52 static void
53 bus_acquired_handler (GDBusConnection *connection,
54                       const gchar     *name,
55                       gpointer         user_data)
56 {
57   OwnNameData *data = user_data;
58   g_dbus_connection_set_exit_on_close (connection, FALSE);
59   data->num_bus_acquired += 1;
60   g_main_loop_quit (loop);
61 }
62
63 static void
64 name_acquired_handler (GDBusConnection *connection,
65                        const gchar     *name,
66                        gpointer         user_data)
67 {
68   OwnNameData *data = user_data;
69   data->num_acquired += 1;
70   g_main_loop_quit (loop);
71 }
72
73 static void
74 name_lost_handler (GDBusConnection *connection,
75                    const gchar     *name,
76                    gpointer         user_data)
77 {
78   OwnNameData *data = user_data;
79   if (data->expect_null_connection)
80     {
81       g_assert (connection == NULL);
82     }
83   else
84     {
85       g_assert (connection != NULL);
86       g_dbus_connection_set_exit_on_close (connection, FALSE);
87     }
88   data->num_lost += 1;
89   g_main_loop_quit (loop);
90 }
91
92 static void
93 test_bus_own_name (void)
94 {
95   guint id;
96   guint id2;
97   OwnNameData data;
98   OwnNameData data2;
99   const gchar *name;
100   GDBusConnection *c;
101   GError *error;
102   gboolean name_has_owner_reply;
103   GDBusConnection *c2;
104   GVariant *result;
105
106   error = NULL;
107   name = "org.gtk.GDBus.Name1";
108
109   /*
110    * First check that name_lost_handler() is invoked if there is no bus.
111    *
112    * Also make sure name_lost_handler() isn't invoked when unowning the name.
113    */
114   data.num_bus_acquired = 0;
115   data.num_free_func = 0;
116   data.num_acquired = 0;
117   data.num_lost = 0;
118   data.expect_null_connection = TRUE;
119   id = g_bus_own_name (G_BUS_TYPE_SESSION,
120                        name,
121                        G_BUS_NAME_OWNER_FLAGS_NONE,
122                        bus_acquired_handler,
123                        name_acquired_handler,
124                        name_lost_handler,
125                        &data,
126                        (GDestroyNotify) own_name_data_free_func);
127   g_assert_cmpint (data.num_bus_acquired, ==, 0);
128   g_assert_cmpint (data.num_acquired, ==, 0);
129   g_assert_cmpint (data.num_lost,     ==, 0);
130   g_main_loop_run (loop);
131   g_assert_cmpint (data.num_bus_acquired, ==, 0);
132   g_assert_cmpint (data.num_acquired, ==, 0);
133   g_assert_cmpint (data.num_lost,     ==, 1);
134   g_bus_unown_name (id);
135   g_assert_cmpint (data.num_acquired, ==, 0);
136   g_assert_cmpint (data.num_lost,     ==, 1);
137   g_assert_cmpint (data.num_free_func, ==, 1);
138
139   /*
140    * Bring up a bus, then own a name and check bus_acquired_handler() then name_acquired_handler() is invoked.
141    */
142   session_bus_up ();
143   data.num_bus_acquired = 0;
144   data.num_acquired = 0;
145   data.num_lost = 0;
146   data.expect_null_connection = FALSE;
147   id = g_bus_own_name (G_BUS_TYPE_SESSION,
148                        name,
149                        G_BUS_NAME_OWNER_FLAGS_NONE,
150                        bus_acquired_handler,
151                        name_acquired_handler,
152                        name_lost_handler,
153                        &data,
154                        (GDestroyNotify) own_name_data_free_func);
155   g_assert_cmpint (data.num_bus_acquired, ==, 0);
156   g_assert_cmpint (data.num_acquired, ==, 0);
157   g_assert_cmpint (data.num_lost,     ==, 0);
158   g_main_loop_run (loop);
159   g_assert_cmpint (data.num_bus_acquired, ==, 1);
160   g_assert_cmpint (data.num_acquired, ==, 0);
161   g_assert_cmpint (data.num_lost,     ==, 0);
162   g_main_loop_run (loop);
163   g_assert_cmpint (data.num_bus_acquired, ==, 1);
164   g_assert_cmpint (data.num_acquired, ==, 1);
165   g_assert_cmpint (data.num_lost,     ==, 0);
166
167   /*
168    * Check that the name was actually acquired.
169    */
170   c = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
171   g_assert (c != NULL);
172   g_assert (!g_dbus_connection_is_closed (c));
173   result = g_dbus_connection_call_sync (c,
174                                         "org.freedesktop.DBus",  /* bus name */
175                                         "/org/freedesktop/DBus", /* object path */
176                                         "org.freedesktop.DBus",  /* interface name */
177                                         "NameHasOwner",          /* method name */
178                                         g_variant_new ("(s)", name),
179                                         G_VARIANT_TYPE ("(b)"),
180                                         G_DBUS_CALL_FLAGS_NONE,
181                                         -1,
182                                         NULL,
183                                         &error);
184   g_assert_no_error (error);
185   g_assert (result != NULL);
186   g_variant_get (result, "(b)", &name_has_owner_reply);
187   g_assert (name_has_owner_reply);
188   g_variant_unref (result);
189
190   /*
191    * Stop owning the name - this should invoke our free func
192    */
193   g_bus_unown_name (id);
194   g_assert_cmpint (data.num_free_func, ==, 2);
195
196   /*
197    * Check that the name was actually released.
198    */
199   result = g_dbus_connection_call_sync (c,
200                                         "org.freedesktop.DBus",  /* bus name */
201                                         "/org/freedesktop/DBus", /* object path */
202                                         "org.freedesktop.DBus",  /* interface name */
203                                         "NameHasOwner",          /* method name */
204                                         g_variant_new ("(s)", name),
205                                         G_VARIANT_TYPE ("(b)"),
206                                         G_DBUS_CALL_FLAGS_NONE,
207                                         -1,
208                                         NULL,
209                                         &error);
210   g_assert_no_error (error);
211   g_assert (result != NULL);
212   g_variant_get (result, "(b)", &name_has_owner_reply);
213   g_assert (!name_has_owner_reply);
214   g_variant_unref (result);
215
216   /*
217    * Own the name again.
218    */
219   data.num_bus_acquired = 0;
220   data.num_acquired = 0;
221   data.num_lost = 0;
222   data.expect_null_connection = FALSE;
223   id = g_bus_own_name_with_closures (G_BUS_TYPE_SESSION,
224                                      name,
225                                      G_BUS_NAME_OWNER_FLAGS_NONE,
226                                      g_cclosure_new (G_CALLBACK (bus_acquired_handler),
227                                                      &data,
228                                                      NULL),
229                                      g_cclosure_new (G_CALLBACK (name_acquired_handler),
230                                                      &data,
231                                                      NULL),
232                                      g_cclosure_new (G_CALLBACK (name_lost_handler),
233                                                      &data,
234                                                      (GClosureNotify) own_name_data_free_func));
235   g_assert_cmpint (data.num_bus_acquired, ==, 0);
236   g_assert_cmpint (data.num_acquired, ==, 0);
237   g_assert_cmpint (data.num_lost,     ==, 0);
238   g_main_loop_run (loop);
239   g_assert_cmpint (data.num_bus_acquired, ==, 1);
240   g_assert_cmpint (data.num_acquired, ==, 0);
241   g_assert_cmpint (data.num_lost,     ==, 0);
242   g_main_loop_run (loop);
243   g_assert_cmpint (data.num_bus_acquired, ==, 1);
244   g_assert_cmpint (data.num_acquired, ==, 1);
245   g_assert_cmpint (data.num_lost,     ==, 0);
246
247   /*
248    * Try owning the name with another object on the same connection  - this should
249    * fail because we already own the name.
250    */
251   data2.num_free_func = 0;
252   data2.num_bus_acquired = 0;
253   data2.num_acquired = 0;
254   data2.num_lost = 0;
255   data2.expect_null_connection = FALSE;
256   id2 = g_bus_own_name (G_BUS_TYPE_SESSION,
257                         name,
258                         G_BUS_NAME_OWNER_FLAGS_NONE,
259                         bus_acquired_handler,
260                         name_acquired_handler,
261                         name_lost_handler,
262                         &data2,
263                         (GDestroyNotify) own_name_data_free_func);
264   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
265   g_assert_cmpint (data2.num_acquired, ==, 0);
266   g_assert_cmpint (data2.num_lost,     ==, 0);
267   g_main_loop_run (loop);
268   g_assert_cmpint (data2.num_bus_acquired, ==, 1);
269   g_assert_cmpint (data2.num_acquired, ==, 0);
270   g_assert_cmpint (data2.num_lost,     ==, 0);
271   g_main_loop_run (loop);
272   g_assert_cmpint (data2.num_bus_acquired, ==, 1);
273   g_assert_cmpint (data2.num_acquired, ==, 0);
274   g_assert_cmpint (data2.num_lost,     ==, 1);
275   g_bus_unown_name (id2);
276   g_assert_cmpint (data2.num_bus_acquired, ==, 1);
277   g_assert_cmpint (data2.num_acquired, ==, 0);
278   g_assert_cmpint (data2.num_lost,     ==, 1);
279   g_assert_cmpint (data2.num_free_func, ==, 1);
280
281   /*
282    * Create a secondary (e.g. private) connection and try owning the name on that
283    * connection. This should fail both with and without _REPLACE because we
284    * didn't specify ALLOW_REPLACEMENT.
285    */
286   c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
287   g_assert (c2 != NULL);
288   g_assert (!g_dbus_connection_is_closed (c2));
289   /* first without _REPLACE */
290   data2.num_bus_acquired = 0;
291   data2.num_acquired = 0;
292   data2.num_lost = 0;
293   data2.expect_null_connection = FALSE;
294   data2.num_free_func = 0;
295   id2 = g_bus_own_name_on_connection (c2,
296                                       name,
297                                       G_BUS_NAME_OWNER_FLAGS_NONE,
298                                       name_acquired_handler,
299                                       name_lost_handler,
300                                       &data2,
301                                       (GDestroyNotify) own_name_data_free_func);
302   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
303   g_assert_cmpint (data2.num_acquired, ==, 0);
304   g_assert_cmpint (data2.num_lost,     ==, 0);
305   g_main_loop_run (loop);
306   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
307   g_assert_cmpint (data2.num_acquired, ==, 0);
308   g_assert_cmpint (data2.num_lost,     ==, 1);
309   g_bus_unown_name (id2);
310   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
311   g_assert_cmpint (data2.num_acquired, ==, 0);
312   g_assert_cmpint (data2.num_lost,     ==, 1);
313   g_assert_cmpint (data2.num_free_func, ==, 1);
314   /* then with _REPLACE */
315   data2.num_bus_acquired = 0;
316   data2.num_acquired = 0;
317   data2.num_lost = 0;
318   data2.expect_null_connection = FALSE;
319   data2.num_free_func = 0;
320   id2 = g_bus_own_name_on_connection (c2,
321                                       name,
322                                       G_BUS_NAME_OWNER_FLAGS_REPLACE,
323                                       name_acquired_handler,
324                                       name_lost_handler,
325                                       &data2,
326                                       (GDestroyNotify) own_name_data_free_func);
327   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
328   g_assert_cmpint (data2.num_acquired, ==, 0);
329   g_assert_cmpint (data2.num_lost,     ==, 0);
330   g_main_loop_run (loop);
331   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
332   g_assert_cmpint (data2.num_acquired, ==, 0);
333   g_assert_cmpint (data2.num_lost,     ==, 1);
334   g_bus_unown_name (id2);
335   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
336   g_assert_cmpint (data2.num_acquired, ==, 0);
337   g_assert_cmpint (data2.num_lost,     ==, 1);
338   g_assert_cmpint (data2.num_free_func, ==, 1);
339
340   /*
341    * Stop owning the name and grab it again with _ALLOW_REPLACEMENT.
342    */
343   data.expect_null_connection = FALSE;
344   g_bus_unown_name (id);
345   g_assert_cmpint (data.num_bus_acquired, ==, 1);
346   g_assert_cmpint (data.num_acquired, ==, 1);
347   g_assert_cmpint (data.num_free_func, ==, 3);
348   /* grab it again */
349   data.num_bus_acquired = 0;
350   data.num_acquired = 0;
351   data.num_lost = 0;
352   data.expect_null_connection = FALSE;
353   id = g_bus_own_name (G_BUS_TYPE_SESSION,
354                        name,
355                        G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
356                        bus_acquired_handler,
357                        name_acquired_handler,
358                        name_lost_handler,
359                        &data,
360                        (GDestroyNotify) own_name_data_free_func);
361   g_assert_cmpint (data.num_bus_acquired, ==, 0);
362   g_assert_cmpint (data.num_acquired, ==, 0);
363   g_assert_cmpint (data.num_lost,     ==, 0);
364   g_main_loop_run (loop);
365   g_assert_cmpint (data.num_bus_acquired, ==, 1);
366   g_assert_cmpint (data.num_acquired, ==, 0);
367   g_assert_cmpint (data.num_lost,     ==, 0);
368   g_main_loop_run (loop);
369   g_assert_cmpint (data.num_bus_acquired, ==, 1);
370   g_assert_cmpint (data.num_acquired, ==, 1);
371   g_assert_cmpint (data.num_lost,     ==, 0);
372
373   /*
374    * Now try to grab the name from the secondary connection.
375    *
376    */
377   /* first without _REPLACE - this won't make us acquire the name */
378   data2.num_bus_acquired = 0;
379   data2.num_acquired = 0;
380   data2.num_lost = 0;
381   data2.expect_null_connection = FALSE;
382   data2.num_free_func = 0;
383   id2 = g_bus_own_name_on_connection (c2,
384                                       name,
385                                       G_BUS_NAME_OWNER_FLAGS_NONE,
386                                       name_acquired_handler,
387                                       name_lost_handler,
388                                       &data2,
389                                       (GDestroyNotify) own_name_data_free_func);
390   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
391   g_assert_cmpint (data2.num_acquired, ==, 0);
392   g_assert_cmpint (data2.num_lost,     ==, 0);
393   g_main_loop_run (loop);
394   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
395   g_assert_cmpint (data2.num_acquired, ==, 0);
396   g_assert_cmpint (data2.num_lost,     ==, 1);
397   g_bus_unown_name (id2);
398   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
399   g_assert_cmpint (data2.num_acquired, ==, 0);
400   g_assert_cmpint (data2.num_lost,     ==, 1);
401   g_assert_cmpint (data2.num_free_func, ==, 1);
402   /* then with _REPLACE - here we should acquire the name - e.g. owner should lose it
403    * and owner2 should acquire it  */
404   data2.num_bus_acquired = 0;
405   data2.num_acquired = 0;
406   data2.num_lost = 0;
407   data2.expect_null_connection = FALSE;
408   data2.num_free_func = 0;
409   id2 = g_bus_own_name_on_connection (c2,
410                                       name,
411                                       G_BUS_NAME_OWNER_FLAGS_REPLACE,
412                                       name_acquired_handler,
413                                       name_lost_handler,
414                                       &data2,
415                                       (GDestroyNotify) own_name_data_free_func);
416   g_assert_cmpint (data.num_acquired, ==, 1);
417   g_assert_cmpint (data.num_lost,     ==, 0);
418   g_assert_cmpint (data2.num_acquired, ==, 0);
419   g_assert_cmpint (data2.num_lost,     ==, 0);
420   /* wait for handlers for both owner and owner2 to fire */
421   while (data.num_lost == 0 || data2.num_acquired == 0)
422     g_main_loop_run (loop);
423   g_assert_cmpint (data.num_acquired, ==, 1);
424   g_assert_cmpint (data.num_lost,     ==, 1);
425   g_assert_cmpint (data2.num_acquired, ==, 1);
426   g_assert_cmpint (data2.num_lost,     ==, 0);
427   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
428   /* ok, make owner2 release the name - then wait for owner to automagically reacquire it */
429   g_bus_unown_name (id2);
430   g_assert_cmpint (data2.num_free_func, ==, 1);
431   g_main_loop_run (loop);
432   g_assert_cmpint (data.num_acquired, ==, 2);
433   g_assert_cmpint (data.num_lost,     ==, 1);
434
435   /*
436    * Finally, nuke the bus and check name_lost_handler() is invoked.
437    *
438    */
439   data.expect_null_connection = TRUE;
440   session_bus_down ();
441   while (data.num_lost != 2)
442     g_main_loop_run (loop);
443   g_assert_cmpint (data.num_acquired, ==, 2);
444   g_assert_cmpint (data.num_lost,     ==, 2);
445   g_bus_unown_name (id);
446   g_assert_cmpint (data.num_free_func, ==, 4);
447
448   g_object_unref (c);
449   g_object_unref (c2);
450 }
451
452 /* ---------------------------------------------------------------------------------------------------- */
453 /* Test that g_bus_watch_name() works correctly */
454 /* ---------------------------------------------------------------------------------------------------- */
455
456 typedef struct
457 {
458   gboolean expect_null_connection;
459   guint num_acquired;
460   guint num_lost;
461   guint num_appeared;
462   guint num_vanished;
463   guint num_free_func;
464 } WatchNameData;
465
466 static void
467 watch_name_data_free_func (WatchNameData *data)
468 {
469   data->num_free_func++;
470   g_main_loop_quit (loop);
471 }
472
473 static void
474 w_bus_acquired_handler (GDBusConnection *connection,
475                         const gchar     *name,
476                         gpointer         user_data)
477 {
478 }
479
480 static void
481 w_name_acquired_handler (GDBusConnection *connection,
482                          const gchar     *name,
483                          gpointer         user_data)
484 {
485   WatchNameData *data = user_data;
486   data->num_acquired += 1;
487   g_main_loop_quit (loop);
488 }
489
490 static void
491 w_name_lost_handler (GDBusConnection *connection,
492                      const gchar     *name,
493                      gpointer         user_data)
494 {
495   WatchNameData *data = user_data;
496   data->num_lost += 1;
497   g_main_loop_quit (loop);
498 }
499
500 static void
501 name_appeared_handler (GDBusConnection *connection,
502                        const gchar     *name,
503                        const gchar     *name_owner,
504                        gpointer         user_data)
505 {
506   WatchNameData *data = user_data;
507   if (data->expect_null_connection)
508     {
509       g_assert (connection == NULL);
510     }
511   else
512     {
513       g_assert (connection != NULL);
514       g_dbus_connection_set_exit_on_close (connection, FALSE);
515     }
516   data->num_appeared += 1;
517   g_main_loop_quit (loop);
518 }
519
520 static void
521 name_vanished_handler (GDBusConnection *connection,
522                        const gchar     *name,
523                        gpointer         user_data)
524 {
525   WatchNameData *data = user_data;
526   if (data->expect_null_connection)
527     {
528       g_assert (connection == NULL);
529     }
530   else
531     {
532       g_assert (connection != NULL);
533       g_dbus_connection_set_exit_on_close (connection, FALSE);
534     }
535   data->num_vanished += 1;
536   g_main_loop_quit (loop);
537 }
538
539 static void
540 test_bus_watch_name (void)
541 {
542   WatchNameData data;
543   guint id;
544   guint owner_id;
545   GDBusConnection *connection;
546
547   /*
548    * First check that name_vanished_handler() is invoked if there is no bus.
549    *
550    * Also make sure name_vanished_handler() isn't invoked when unwatching the name.
551    */
552   data.num_free_func = 0;
553   data.num_appeared = 0;
554   data.num_vanished = 0;
555   data.expect_null_connection = TRUE;
556   id = g_bus_watch_name (G_BUS_TYPE_SESSION,
557                          "org.gtk.GDBus.Name1",
558                          G_BUS_NAME_WATCHER_FLAGS_NONE,
559                          name_appeared_handler,
560                          name_vanished_handler,
561                          &data,
562                          (GDestroyNotify) watch_name_data_free_func);
563   g_assert_cmpint (data.num_appeared, ==, 0);
564   g_assert_cmpint (data.num_vanished, ==, 0);
565   g_main_loop_run (loop);
566   g_assert_cmpint (data.num_appeared, ==, 0);
567   g_assert_cmpint (data.num_vanished, ==, 1);
568   g_bus_unwatch_name (id);
569   g_assert_cmpint (data.num_appeared, ==, 0);
570   g_assert_cmpint (data.num_vanished, ==, 1);
571   g_assert_cmpint (data.num_free_func, ==, 1);
572
573   /*
574    * Now bring up a bus, own a name, and then start watching it.
575    */
576   session_bus_up ();
577   /* own the name */
578   data.num_free_func = 0;
579   data.num_acquired = 0;
580   data.num_lost = 0;
581   data.expect_null_connection = FALSE;
582   owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
583                              "org.gtk.GDBus.Name1",
584                              G_BUS_NAME_OWNER_FLAGS_NONE,
585                              w_bus_acquired_handler,
586                              w_name_acquired_handler,
587                              w_name_lost_handler,
588                              &data,
589                              (GDestroyNotify) watch_name_data_free_func);
590   g_main_loop_run (loop);
591   g_assert_cmpint (data.num_acquired, ==, 1);
592   g_assert_cmpint (data.num_lost,     ==, 0);
593
594   connection = g_bus_get_sync (G_BUS_TYPE_SESSION, NULL, NULL);
595   g_assert (connection != NULL);
596
597   /* now watch the name */
598   data.num_appeared = 0;
599   data.num_vanished = 0;
600   id = g_bus_watch_name_on_connection (connection,
601                                        "org.gtk.GDBus.Name1",
602                                        G_BUS_NAME_WATCHER_FLAGS_NONE,
603                                        name_appeared_handler,
604                                        name_vanished_handler,
605                                        &data,
606                                        (GDestroyNotify) watch_name_data_free_func);
607   g_assert_cmpint (data.num_appeared, ==, 0);
608   g_assert_cmpint (data.num_vanished, ==, 0);
609   g_main_loop_run (loop);
610   g_assert_cmpint (data.num_appeared, ==, 1);
611   g_assert_cmpint (data.num_vanished, ==, 0);
612
613   /*
614    * Unwatch the name.
615    */
616   g_bus_unwatch_name (id);
617   g_assert_cmpint (data.num_free_func, ==, 1);
618
619   g_object_unref (connection);
620
621   /* unown the name */
622   g_bus_unown_name (owner_id);
623   g_assert_cmpint (data.num_acquired, ==, 1);
624   g_assert_cmpint (data.num_free_func, ==, 2);
625
626   /*
627    * Create a watcher and then make a name be owned.
628    *
629    * This should trigger name_appeared_handler() ...
630    */
631   /* watch the name */
632   data.num_appeared = 0;
633   data.num_vanished = 0;
634   data.num_free_func = 0;
635   id = g_bus_watch_name_with_closures (G_BUS_TYPE_SESSION,
636                                        "org.gtk.GDBus.Name1",
637                                        G_BUS_NAME_WATCHER_FLAGS_NONE,
638                                        g_cclosure_new (G_CALLBACK (name_appeared_handler),
639                                                        &data,
640                                                        NULL),
641                                        g_cclosure_new (G_CALLBACK (name_vanished_handler),
642                                                        &data,
643                                                        (GClosureNotify) watch_name_data_free_func));
644   g_assert_cmpint (data.num_appeared, ==, 0);
645   g_assert_cmpint (data.num_vanished, ==, 0);
646   g_main_loop_run (loop);
647   g_assert_cmpint (data.num_appeared, ==, 0);
648   g_assert_cmpint (data.num_vanished, ==, 1);
649
650   /* own the name */
651   data.num_acquired = 0;
652   data.num_lost = 0;
653   data.expect_null_connection = FALSE;
654   owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
655                              "org.gtk.GDBus.Name1",
656                              G_BUS_NAME_OWNER_FLAGS_NONE,
657                              w_bus_acquired_handler,
658                              w_name_acquired_handler,
659                              w_name_lost_handler,
660                              &data,
661                              (GDestroyNotify) watch_name_data_free_func);
662   while (data.num_acquired == 0 || data.num_appeared == 0)
663     g_main_loop_run (loop);
664   g_assert_cmpint (data.num_acquired, ==, 1);
665   g_assert_cmpint (data.num_lost,     ==, 0);
666   g_assert_cmpint (data.num_appeared, ==, 1);
667   g_assert_cmpint (data.num_vanished, ==, 1);
668
669   /*
670    * Nuke the bus and check that the name vanishes and is lost.
671    */
672   data.expect_null_connection = TRUE;
673   session_bus_down ();
674   g_main_loop_run (loop);
675   g_assert_cmpint (data.num_lost,     ==, 1);
676   g_assert_cmpint (data.num_vanished, ==, 2);
677
678   g_bus_unwatch_name (id);
679   g_assert_cmpint (data.num_free_func, ==, 1);
680
681   g_bus_unown_name (owner_id);
682   g_assert_cmpint (data.num_free_func, ==, 2);
683
684 }
685
686 /* ---------------------------------------------------------------------------------------------------- */
687
688 static void
689 test_validate_names (void)
690 {
691   guint n;
692   static const struct
693   {
694     gboolean name;
695     gboolean unique;
696     gboolean interface;
697     const gchar *string;
698   } names[] = {
699     { 1, 0, 1, "valid.well_known.name"},
700     { 1, 0, 0, "valid.well-known.name"},
701     { 1, 1, 0, ":valid.unique.name"},
702     { 0, 0, 0, "invalid.5well_known.name"},
703     { 0, 0, 0, "4invalid.5well_known.name"},
704     { 1, 1, 0, ":4valid.5unique.name"},
705     { 0, 0, 0, ""},
706     { 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 */
707     { 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! */
708     { 0, 0, 0, ".starts.with.a.dot"},
709     { 0, 0, 0, "contains.invalid;.characters"},
710     { 0, 0, 0, "contains.inva/lid.characters"},
711     { 0, 0, 0, "contains.inva[lid.characters"},
712     { 0, 0, 0, "contains.inva]lid.characters"},
713     { 0, 0, 0, "contains.inva_æøå_lid.characters"},
714     { 1, 1, 0, ":1.1"},
715   };
716
717   for (n = 0; n < G_N_ELEMENTS (names); n++)
718     {
719       if (names[n].name)
720         g_assert (g_dbus_is_name (names[n].string));
721       else
722         g_assert (!g_dbus_is_name (names[n].string));
723
724       if (names[n].unique)
725         g_assert (g_dbus_is_unique_name (names[n].string));
726       else
727         g_assert (!g_dbus_is_unique_name (names[n].string));
728
729       if (names[n].interface)
730         g_assert (g_dbus_is_interface_name (names[n].string));
731       else
732         g_assert (!g_dbus_is_interface_name (names[n].string));
733     }
734 }
735
736 /* ---------------------------------------------------------------------------------------------------- */
737
738 int
739 main (int   argc,
740       char *argv[])
741 {
742   gint ret;
743
744   g_type_init ();
745   g_test_init (&argc, &argv, NULL);
746
747   loop = g_main_loop_new (NULL, FALSE);
748
749   /* all the tests use a session bus with a well-known address that we can bring up and down
750    * using session_bus_up() and session_bus_down().
751    */
752   g_unsetenv ("DISPLAY");
753   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
754
755   g_test_add_func ("/gdbus/validate-names", test_validate_names);
756   g_test_add_func ("/gdbus/bus-own-name", test_bus_own_name);
757   g_test_add_func ("/gdbus/bus-watch-name", test_bus_watch_name);
758
759   ret = g_test_run();
760
761   g_main_loop_unref (loop);
762
763   return ret;
764 }