Merge branch 'gdbus-merge'
[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_DBUS_CALL_FLAGS_NONE,
180                                         -1,
181                                         NULL,
182                                         &error);
183   g_assert_no_error (error);
184   g_assert (result != NULL);
185   g_variant_get (result, "(b)", &name_has_owner_reply);
186   g_assert (name_has_owner_reply);
187   g_variant_unref (result);
188
189   /*
190    * Stop owning the name - this should invoke our free func
191    */
192   g_bus_unown_name (id);
193   g_assert_cmpint (data.num_free_func, ==, 2);
194
195   /*
196    * Check that the name was actually released.
197    */
198   result = g_dbus_connection_call_sync (c,
199                                         "org.freedesktop.DBus",  /* bus name */
200                                         "/org/freedesktop/DBus", /* object path */
201                                         "org.freedesktop.DBus",  /* interface name */
202                                         "NameHasOwner",          /* method name */
203                                         g_variant_new ("(s)", name),
204                                         G_DBUS_CALL_FLAGS_NONE,
205                                         -1,
206                                         NULL,
207                                         &error);
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);
213
214   /*
215    * Own the name again.
216    */
217   data.num_bus_acquired = 0;
218   data.num_acquired = 0;
219   data.num_lost = 0;
220   data.expect_null_connection = FALSE;
221   id = g_bus_own_name (G_BUS_TYPE_SESSION,
222                        name,
223                        G_BUS_NAME_OWNER_FLAGS_NONE,
224                        bus_acquired_handler,
225                        name_acquired_handler,
226                        name_lost_handler,
227                        &data,
228                        (GDestroyNotify) own_name_data_free_func);
229   g_assert_cmpint (data.num_bus_acquired, ==, 0);
230   g_assert_cmpint (data.num_acquired, ==, 0);
231   g_assert_cmpint (data.num_lost,     ==, 0);
232   g_main_loop_run (loop);
233   g_assert_cmpint (data.num_bus_acquired, ==, 1);
234   g_assert_cmpint (data.num_acquired, ==, 0);
235   g_assert_cmpint (data.num_lost,     ==, 0);
236   g_main_loop_run (loop);
237   g_assert_cmpint (data.num_bus_acquired, ==, 1);
238   g_assert_cmpint (data.num_acquired, ==, 1);
239   g_assert_cmpint (data.num_lost,     ==, 0);
240
241   /*
242    * Try owning the name with another object on the same connection  - this should
243    * fail because we already own the name.
244    */
245   data2.num_free_func = 0;
246   data2.num_bus_acquired = 0;
247   data2.num_acquired = 0;
248   data2.num_lost = 0;
249   data2.expect_null_connection = FALSE;
250   id2 = g_bus_own_name (G_BUS_TYPE_SESSION,
251                         name,
252                         G_BUS_NAME_OWNER_FLAGS_NONE,
253                         bus_acquired_handler,
254                         name_acquired_handler,
255                         name_lost_handler,
256                         &data2,
257                         (GDestroyNotify) own_name_data_free_func);
258   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
259   g_assert_cmpint (data2.num_acquired, ==, 0);
260   g_assert_cmpint (data2.num_lost,     ==, 0);
261   g_main_loop_run (loop);
262   g_assert_cmpint (data2.num_bus_acquired, ==, 1);
263   g_assert_cmpint (data2.num_acquired, ==, 0);
264   g_assert_cmpint (data2.num_lost,     ==, 0);
265   g_main_loop_run (loop);
266   g_assert_cmpint (data2.num_bus_acquired, ==, 1);
267   g_assert_cmpint (data2.num_acquired, ==, 0);
268   g_assert_cmpint (data2.num_lost,     ==, 1);
269   g_bus_unown_name (id2);
270   g_assert_cmpint (data2.num_bus_acquired, ==, 1);
271   g_assert_cmpint (data2.num_acquired, ==, 0);
272   g_assert_cmpint (data2.num_lost,     ==, 1);
273   g_assert_cmpint (data2.num_free_func, ==, 1);
274
275   /*
276    * Create a secondary (e.g. private) connection and try owning the name on that
277    * connection. This should fail both with and without _REPLACE because we
278    * didn't specify ALLOW_REPLACEMENT.
279    */
280   c2 = _g_bus_get_priv (G_BUS_TYPE_SESSION, NULL, NULL);
281   g_assert (c2 != NULL);
282   g_assert (!g_dbus_connection_is_closed (c2));
283   /* first without _REPLACE */
284   data2.num_bus_acquired = 0;
285   data2.num_acquired = 0;
286   data2.num_lost = 0;
287   data2.expect_null_connection = FALSE;
288   data2.num_free_func = 0;
289   id2 = g_bus_own_name_on_connection (c2,
290                                       name,
291                                       G_BUS_NAME_OWNER_FLAGS_NONE,
292                                       name_acquired_handler,
293                                       name_lost_handler,
294                                       &data2,
295                                       (GDestroyNotify) own_name_data_free_func);
296   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
297   g_assert_cmpint (data2.num_acquired, ==, 0);
298   g_assert_cmpint (data2.num_lost,     ==, 0);
299   g_main_loop_run (loop);
300   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
301   g_assert_cmpint (data2.num_acquired, ==, 0);
302   g_assert_cmpint (data2.num_lost,     ==, 1);
303   g_bus_unown_name (id2);
304   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
305   g_assert_cmpint (data2.num_acquired, ==, 0);
306   g_assert_cmpint (data2.num_lost,     ==, 1);
307   g_assert_cmpint (data2.num_free_func, ==, 1);
308   /* then with _REPLACE */
309   data2.num_bus_acquired = 0;
310   data2.num_acquired = 0;
311   data2.num_lost = 0;
312   data2.expect_null_connection = FALSE;
313   data2.num_free_func = 0;
314   id2 = g_bus_own_name_on_connection (c2,
315                                       name,
316                                       G_BUS_NAME_OWNER_FLAGS_REPLACE,
317                                       name_acquired_handler,
318                                       name_lost_handler,
319                                       &data2,
320                                       (GDestroyNotify) own_name_data_free_func);
321   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
322   g_assert_cmpint (data2.num_acquired, ==, 0);
323   g_assert_cmpint (data2.num_lost,     ==, 0);
324   g_main_loop_run (loop);
325   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
326   g_assert_cmpint (data2.num_acquired, ==, 0);
327   g_assert_cmpint (data2.num_lost,     ==, 1);
328   g_bus_unown_name (id2);
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_assert_cmpint (data2.num_free_func, ==, 1);
333
334   /*
335    * Stop owning the name and grab it again with _ALLOW_REPLACEMENT.
336    */
337   data.expect_null_connection = FALSE;
338   g_bus_unown_name (id);
339   g_assert_cmpint (data.num_bus_acquired, ==, 1);
340   g_assert_cmpint (data.num_acquired, ==, 1);
341   g_assert_cmpint (data.num_free_func, ==, 3);
342   /* grab it again */
343   data.num_bus_acquired = 0;
344   data.num_acquired = 0;
345   data.num_lost = 0;
346   data.expect_null_connection = FALSE;
347   id = g_bus_own_name (G_BUS_TYPE_SESSION,
348                        name,
349                        G_BUS_NAME_OWNER_FLAGS_ALLOW_REPLACEMENT,
350                        bus_acquired_handler,
351                        name_acquired_handler,
352                        name_lost_handler,
353                        &data,
354                        (GDestroyNotify) own_name_data_free_func);
355   g_assert_cmpint (data.num_bus_acquired, ==, 0);
356   g_assert_cmpint (data.num_acquired, ==, 0);
357   g_assert_cmpint (data.num_lost,     ==, 0);
358   g_main_loop_run (loop);
359   g_assert_cmpint (data.num_bus_acquired, ==, 1);
360   g_assert_cmpint (data.num_acquired, ==, 0);
361   g_assert_cmpint (data.num_lost,     ==, 0);
362   g_main_loop_run (loop);
363   g_assert_cmpint (data.num_bus_acquired, ==, 1);
364   g_assert_cmpint (data.num_acquired, ==, 1);
365   g_assert_cmpint (data.num_lost,     ==, 0);
366
367   /*
368    * Now try to grab the name from the secondary connection.
369    *
370    */
371   /* first without _REPLACE - this won't make us acquire the name */
372   data2.num_bus_acquired = 0;
373   data2.num_acquired = 0;
374   data2.num_lost = 0;
375   data2.expect_null_connection = FALSE;
376   data2.num_free_func = 0;
377   id2 = g_bus_own_name_on_connection (c2,
378                                       name,
379                                       G_BUS_NAME_OWNER_FLAGS_NONE,
380                                       name_acquired_handler,
381                                       name_lost_handler,
382                                       &data2,
383                                       (GDestroyNotify) own_name_data_free_func);
384   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
385   g_assert_cmpint (data2.num_acquired, ==, 0);
386   g_assert_cmpint (data2.num_lost,     ==, 0);
387   g_main_loop_run (loop);
388   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
389   g_assert_cmpint (data2.num_acquired, ==, 0);
390   g_assert_cmpint (data2.num_lost,     ==, 1);
391   g_bus_unown_name (id2);
392   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
393   g_assert_cmpint (data2.num_acquired, ==, 0);
394   g_assert_cmpint (data2.num_lost,     ==, 1);
395   g_assert_cmpint (data2.num_free_func, ==, 1);
396   /* then with _REPLACE - here we should acquire the name - e.g. owner should lose it
397    * and owner2 should acquire it  */
398   data2.num_bus_acquired = 0;
399   data2.num_acquired = 0;
400   data2.num_lost = 0;
401   data2.expect_null_connection = FALSE;
402   data2.num_free_func = 0;
403   id2 = g_bus_own_name_on_connection (c2,
404                                       name,
405                                       G_BUS_NAME_OWNER_FLAGS_REPLACE,
406                                       name_acquired_handler,
407                                       name_lost_handler,
408                                       &data2,
409                                       (GDestroyNotify) own_name_data_free_func);
410   g_assert_cmpint (data.num_acquired, ==, 1);
411   g_assert_cmpint (data.num_lost,     ==, 0);
412   g_assert_cmpint (data2.num_acquired, ==, 0);
413   g_assert_cmpint (data2.num_lost,     ==, 0);
414   /* wait for handlers for both owner and owner2 to fire */
415   while (data.num_lost == 0 || data2.num_acquired == 0)
416     g_main_loop_run (loop);
417   g_assert_cmpint (data.num_acquired, ==, 1);
418   g_assert_cmpint (data.num_lost,     ==, 1);
419   g_assert_cmpint (data2.num_acquired, ==, 1);
420   g_assert_cmpint (data2.num_lost,     ==, 0);
421   g_assert_cmpint (data2.num_bus_acquired, ==, 0);
422   /* ok, make owner2 release the name - then wait for owner to automagically reacquire it */
423   g_bus_unown_name (id2);
424   g_assert_cmpint (data2.num_free_func, ==, 1);
425   g_main_loop_run (loop);
426   g_assert_cmpint (data.num_acquired, ==, 2);
427   g_assert_cmpint (data.num_lost,     ==, 1);
428
429   /*
430    * Finally, nuke the bus and check name_lost_handler() is invoked.
431    *
432    */
433   data.expect_null_connection = TRUE;
434   session_bus_down ();
435   while (data.num_lost != 2)
436     g_main_loop_run (loop);
437   g_assert_cmpint (data.num_acquired, ==, 2);
438   g_assert_cmpint (data.num_lost,     ==, 2);
439   g_bus_unown_name (id);
440   g_assert_cmpint (data.num_free_func, ==, 4);
441
442   g_object_unref (c);
443   g_object_unref (c2);
444 }
445
446 /* ---------------------------------------------------------------------------------------------------- */
447 /* Test that g_bus_watch_name() works correctly */
448 /* ---------------------------------------------------------------------------------------------------- */
449
450 typedef struct
451 {
452   gboolean expect_null_connection;
453   guint num_acquired;
454   guint num_lost;
455   guint num_appeared;
456   guint num_vanished;
457   guint num_free_func;
458 } WatchNameData;
459
460 static void
461 watch_name_data_free_func (WatchNameData *data)
462 {
463   data->num_free_func++;
464   g_main_loop_quit (loop);
465 }
466
467 static void
468 w_bus_acquired_handler (GDBusConnection *connection,
469                         const gchar     *name,
470                         gpointer         user_data)
471 {
472 }
473
474 static void
475 w_name_acquired_handler (GDBusConnection *connection,
476                          const gchar     *name,
477                          gpointer         user_data)
478 {
479   WatchNameData *data = user_data;
480   data->num_acquired += 1;
481   g_main_loop_quit (loop);
482 }
483
484 static void
485 w_name_lost_handler (GDBusConnection *connection,
486                      const gchar     *name,
487                      gpointer         user_data)
488 {
489   WatchNameData *data = user_data;
490   data->num_lost += 1;
491   g_main_loop_quit (loop);
492 }
493
494 static void
495 name_appeared_handler (GDBusConnection *connection,
496                        const gchar     *name,
497                        const gchar     *name_owner,
498                        gpointer         user_data)
499 {
500   WatchNameData *data = user_data;
501   if (data->expect_null_connection)
502     {
503       g_assert (connection == NULL);
504     }
505   else
506     {
507       g_assert (connection != NULL);
508       g_dbus_connection_set_exit_on_close (connection, FALSE);
509     }
510   data->num_appeared += 1;
511   g_main_loop_quit (loop);
512 }
513
514 static void
515 name_vanished_handler (GDBusConnection *connection,
516                        const gchar     *name,
517                        gpointer         user_data)
518 {
519   WatchNameData *data = user_data;
520   if (data->expect_null_connection)
521     {
522       g_assert (connection == NULL);
523     }
524   else
525     {
526       g_assert (connection != NULL);
527       g_dbus_connection_set_exit_on_close (connection, FALSE);
528     }
529   data->num_vanished += 1;
530   g_main_loop_quit (loop);
531 }
532
533 static void
534 test_bus_watch_name (void)
535 {
536   WatchNameData data;
537   guint id;
538   guint owner_id;
539
540   /*
541    * First check that name_vanished_handler() is invoked if there is no bus.
542    *
543    * Also make sure name_vanished_handler() isn't invoked when unwatching the name.
544    */
545   data.num_free_func = 0;
546   data.num_appeared = 0;
547   data.num_vanished = 0;
548   data.expect_null_connection = TRUE;
549   id = g_bus_watch_name (G_BUS_TYPE_SESSION,
550                          "org.gtk.GDBus.Name1",
551                          G_BUS_NAME_WATCHER_FLAGS_NONE,
552                          name_appeared_handler,
553                          name_vanished_handler,
554                          &data,
555                          (GDestroyNotify) watch_name_data_free_func);
556   g_assert_cmpint (data.num_appeared, ==, 0);
557   g_assert_cmpint (data.num_vanished, ==, 0);
558   g_main_loop_run (loop);
559   g_assert_cmpint (data.num_appeared, ==, 0);
560   g_assert_cmpint (data.num_vanished, ==, 1);
561   g_bus_unwatch_name (id);
562   g_assert_cmpint (data.num_appeared, ==, 0);
563   g_assert_cmpint (data.num_vanished, ==, 1);
564   g_assert_cmpint (data.num_free_func, ==, 1);
565
566   /*
567    * Now bring up a bus, own a name, and then start watching it.
568    */
569   session_bus_up ();
570   /* own the name */
571   data.num_free_func = 0;
572   data.num_acquired = 0;
573   data.num_lost = 0;
574   data.expect_null_connection = FALSE;
575   owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
576                              "org.gtk.GDBus.Name1",
577                              G_BUS_NAME_OWNER_FLAGS_NONE,
578                              w_bus_acquired_handler,
579                              w_name_acquired_handler,
580                              w_name_lost_handler,
581                              &data,
582                              (GDestroyNotify) watch_name_data_free_func);
583   g_main_loop_run (loop);
584   g_assert_cmpint (data.num_acquired, ==, 1);
585   g_assert_cmpint (data.num_lost,     ==, 0);
586   /* now watch the name */
587   data.num_appeared = 0;
588   data.num_vanished = 0;
589   id = g_bus_watch_name (G_BUS_TYPE_SESSION,
590                          "org.gtk.GDBus.Name1",
591                          G_BUS_NAME_WATCHER_FLAGS_NONE,
592                          name_appeared_handler,
593                          name_vanished_handler,
594                          &data,
595                          (GDestroyNotify) watch_name_data_free_func);
596   g_assert_cmpint (data.num_appeared, ==, 0);
597   g_assert_cmpint (data.num_vanished, ==, 0);
598   g_main_loop_run (loop);
599   g_assert_cmpint (data.num_appeared, ==, 1);
600   g_assert_cmpint (data.num_vanished, ==, 0);
601
602   /*
603    * Unwatch the name.
604    */
605   g_bus_unwatch_name (id);
606   g_assert_cmpint (data.num_free_func, ==, 1);
607
608   /* unown the name */
609   g_bus_unown_name (owner_id);
610   g_assert_cmpint (data.num_acquired, ==, 1);
611   g_assert_cmpint (data.num_free_func, ==, 2);
612
613   /*
614    * Create a watcher and then make a name be owned.
615    *
616    * This should trigger name_appeared_handler() ...
617    */
618   /* watch the name */
619   data.num_appeared = 0;
620   data.num_vanished = 0;
621   data.num_free_func = 0;
622   id = g_bus_watch_name (G_BUS_TYPE_SESSION,
623                          "org.gtk.GDBus.Name1",
624                          G_BUS_NAME_WATCHER_FLAGS_NONE,
625                          name_appeared_handler,
626                          name_vanished_handler,
627                          &data,
628                          (GDestroyNotify) watch_name_data_free_func);
629   g_assert_cmpint (data.num_appeared, ==, 0);
630   g_assert_cmpint (data.num_vanished, ==, 0);
631   g_main_loop_run (loop);
632   g_assert_cmpint (data.num_appeared, ==, 0);
633   g_assert_cmpint (data.num_vanished, ==, 1);
634
635   /* own the name */
636   data.num_acquired = 0;
637   data.num_lost = 0;
638   data.expect_null_connection = FALSE;
639   owner_id = g_bus_own_name (G_BUS_TYPE_SESSION,
640                              "org.gtk.GDBus.Name1",
641                              G_BUS_NAME_OWNER_FLAGS_NONE,
642                              w_bus_acquired_handler,
643                              w_name_acquired_handler,
644                              w_name_lost_handler,
645                              &data,
646                              (GDestroyNotify) watch_name_data_free_func);
647   while (data.num_acquired == 0 || data.num_appeared == 0)
648     g_main_loop_run (loop);
649   g_assert_cmpint (data.num_acquired, ==, 1);
650   g_assert_cmpint (data.num_lost,     ==, 0);
651   g_assert_cmpint (data.num_appeared, ==, 1);
652   g_assert_cmpint (data.num_vanished, ==, 1);
653
654   /*
655    * Nuke the bus and check that the name vanishes and is lost.
656    */
657   data.expect_null_connection = TRUE;
658   session_bus_down ();
659   g_main_loop_run (loop);
660   g_assert_cmpint (data.num_lost,     ==, 1);
661   g_assert_cmpint (data.num_vanished, ==, 2);
662
663   g_bus_unwatch_name (id);
664   g_assert_cmpint (data.num_free_func, ==, 1);
665
666   g_bus_unown_name (owner_id);
667   g_assert_cmpint (data.num_free_func, ==, 2);
668
669 }
670
671 /* ---------------------------------------------------------------------------------------------------- */
672
673 static void
674 test_validate_names (void)
675 {
676   guint n;
677   static const struct
678   {
679     gboolean name;
680     gboolean unique;
681     gboolean interface;
682     const gchar *string;
683   } names[] = {
684     { 1, 0, 1, "valid.well_known.name"},
685     { 1, 0, 0, "valid.well-known.name"},
686     { 1, 1, 0, ":valid.unique.name"},
687     { 0, 0, 0, "invalid.5well_known.name"},
688     { 0, 0, 0, "4invalid.5well_known.name"},
689     { 1, 1, 0, ":4valid.5unique.name"},
690     { 0, 0, 0, ""},
691     { 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 */
692     { 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! */
693     { 0, 0, 0, ".starts.with.a.dot"},
694     { 0, 0, 0, "contains.invalid;.characters"},
695     { 0, 0, 0, "contains.inva/lid.characters"},
696     { 0, 0, 0, "contains.inva[lid.characters"},
697     { 0, 0, 0, "contains.inva]lid.characters"},
698     { 0, 0, 0, "contains.inva_æøå_lid.characters"},
699     { 1, 1, 0, ":1.1"},
700   };
701
702   for (n = 0; n < G_N_ELEMENTS (names); n++)
703     {
704       if (names[n].name)
705         g_assert (g_dbus_is_name (names[n].string));
706       else
707         g_assert (!g_dbus_is_name (names[n].string));
708
709       if (names[n].unique)
710         g_assert (g_dbus_is_unique_name (names[n].string));
711       else
712         g_assert (!g_dbus_is_unique_name (names[n].string));
713
714       if (names[n].interface)
715         g_assert (g_dbus_is_interface_name (names[n].string));
716       else
717         g_assert (!g_dbus_is_interface_name (names[n].string));
718     }
719 }
720
721 /* ---------------------------------------------------------------------------------------------------- */
722
723 int
724 main (int   argc,
725       char *argv[])
726 {
727   gint ret;
728
729   g_type_init ();
730   g_test_init (&argc, &argv, NULL);
731
732   loop = g_main_loop_new (NULL, FALSE);
733
734   /* all the tests use a session bus with a well-known address that we can bring up and down
735    * using session_bus_up() and session_bus_down().
736    */
737   g_unsetenv ("DISPLAY");
738   g_setenv ("DBUS_SESSION_BUS_ADDRESS", session_bus_get_temporary_address (), TRUE);
739
740   g_test_add_func ("/gdbus/validate-names", test_validate_names);
741   g_test_add_func ("/gdbus/bus-own-name", test_bus_own_name);
742   g_test_add_func ("/gdbus/bus-watch-name", test_bus_watch_name);
743
744   ret = g_test_run();
745
746   g_main_loop_unref (loop);
747
748   return ret;
749 }