[kdbus] KDBUS_ITEM_PAYLOAD_OFF items are (once again) relative to msg header
[platform/upstream/glib.git] / gio / tests / gdbus-tests.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, see <http://www.gnu.org/licenses/>.
17  *
18  * Author: David Zeuthen <davidz@redhat.com>
19  */
20
21 #include <gio/gio.h>
22 #include <unistd.h>
23
24 #include "gdbus-tests.h"
25
26 /* ---------------------------------------------------------------------------------------------------- */
27
28 typedef struct
29 {
30   GMainLoop *loop;
31   gboolean   timed_out;
32 } PropertyNotifyData;
33
34 static void
35 on_property_notify (GObject    *object,
36                     GParamSpec *pspec,
37                     gpointer    user_data)
38 {
39   PropertyNotifyData *data = user_data;
40   g_main_loop_quit (data->loop);
41 }
42
43 static gboolean
44 on_property_notify_timeout (gpointer user_data)
45 {
46   PropertyNotifyData *data = user_data;
47   data->timed_out = TRUE;
48   g_main_loop_quit (data->loop);
49   return TRUE;
50 }
51
52 gboolean
53 _g_assert_property_notify_run (gpointer     object,
54                                const gchar *property_name)
55 {
56   gchar *s;
57   gulong handler_id;
58   guint timeout_id;
59   PropertyNotifyData data;
60
61   data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
62   data.timed_out = FALSE;
63   s = g_strdup_printf ("notify::%s", property_name);
64   handler_id = g_signal_connect (object,
65                                  s,
66                                  G_CALLBACK (on_property_notify),
67                                  &data);
68   g_free (s);
69   timeout_id = g_timeout_add_seconds (30,
70                                       on_property_notify_timeout,
71                                       &data);
72   g_main_loop_run (data.loop);
73   g_signal_handler_disconnect (object, handler_id);
74   g_source_remove (timeout_id);
75   g_main_loop_unref (data.loop);
76
77   return data.timed_out;
78 }
79
80 /* ---------------------------------------------------------------------------------------------------- */
81
82 typedef struct
83 {
84   GMainLoop *loop;
85   gboolean   timed_out;
86 } SignalReceivedData;
87
88 static void
89 on_signal_received (gpointer user_data)
90 {
91   SignalReceivedData *data = user_data;
92   g_main_loop_quit (data->loop);
93 }
94
95 static gboolean
96 on_signal_received_timeout (gpointer user_data)
97 {
98   SignalReceivedData *data = user_data;
99   data->timed_out = TRUE;
100   g_main_loop_quit (data->loop);
101   return TRUE;
102 }
103
104 gboolean
105 _g_assert_signal_received_run (gpointer     object,
106                                const gchar *signal_name)
107 {
108   gulong handler_id;
109   guint timeout_id;
110   SignalReceivedData data;
111
112   data.loop = g_main_loop_new (g_main_context_get_thread_default (), FALSE);
113   data.timed_out = FALSE;
114   handler_id = g_signal_connect_swapped (object,
115                                          signal_name,
116                                          G_CALLBACK (on_signal_received),
117                                          &data);
118   timeout_id = g_timeout_add_seconds (30,
119                                       on_signal_received_timeout,
120                                       &data);
121   g_main_loop_run (data.loop);
122   g_signal_handler_disconnect (object, handler_id);
123   g_source_remove (timeout_id);
124   g_main_loop_unref (data.loop);
125
126   return data.timed_out;
127 }
128
129 /* ---------------------------------------------------------------------------------------------------- */
130
131 GDBusConnection *
132 _g_bus_get_priv (GBusType            bus_type,
133                  GCancellable       *cancellable,
134                  GError            **error)
135 {
136   gchar *address;
137   GDBusConnection *ret;
138
139   ret = NULL;
140
141   address = g_dbus_address_get_for_bus_sync (bus_type, cancellable, error);
142   if (address == NULL)
143     goto out;
144
145   ret = g_dbus_connection_new_for_address_sync (address,
146                                                 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
147                                                 G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
148                                                 NULL, /* GDBusAuthObserver */
149                                                 cancellable,
150                                                 error);
151   g_free (address);
152
153  out:
154   return ret;
155 }
156
157 /* ---------------------------------------------------------------------------------------------------- */