Use GTestDBus in all GDBus unit tests
[platform/upstream/glib.git] / gio / tests / gdbus-auth.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2012 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 <locale.h>
24 #include <gio/gio.h>
25
26 #include <string.h>
27 #include <unistd.h>
28 #include <dbus/dbus.h>
29
30 #include "gdbus-tests.h"
31
32 #ifdef G_OS_UNIX
33 #include <gio/gunixconnection.h>
34 #include <gio/gnetworkingprivate.h>
35 #include <gio/gunixsocketaddress.h>
36 #include <gio/gunixfdlist.h>
37 #endif
38
39 /* ---------------------------------------------------------------------------------------------------- */
40
41 static gboolean
42 on_allow_mechanism (GDBusAuthObserver *observer,
43                     const gchar       *mechanism,
44                     gpointer           user_data)
45 {
46   const gchar *mechanism_to_allow = user_data;
47   if (g_strcmp0 (mechanism, mechanism_to_allow) == 0)
48     return TRUE;
49   else
50     return FALSE;
51 }
52
53 static void
54 auth_client_mechanism (const gchar *mechanism)
55 {
56   gchar *address = NULL;
57   GDBusConnection *c = NULL;
58   GError *error = NULL;
59   GDBusAuthObserver *auth_observer;
60
61   address = g_dbus_address_get_for_bus_sync (G_BUS_TYPE_SESSION, NULL, &error);
62   g_assert_no_error (error);
63   g_assert (address != NULL);
64
65   auth_observer = g_dbus_auth_observer_new ();
66
67   g_signal_connect (auth_observer,
68                     "allow-mechanism",
69                     G_CALLBACK (on_allow_mechanism),
70                     (gpointer) mechanism);
71
72   c = g_dbus_connection_new_for_address_sync (address,
73                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT |
74                                               G_DBUS_CONNECTION_FLAGS_MESSAGE_BUS_CONNECTION,
75                                               auth_observer,
76                                               NULL,
77                                               &error);
78   g_assert_no_error (error);
79   g_assert (c != NULL);
80
81   g_free (address);
82   g_object_unref (c);
83   g_object_unref (auth_observer);
84 }
85
86 static void
87 auth_client_external (void)
88 {
89   auth_client_mechanism ("EXTERNAL");
90 }
91
92 static void
93 auth_client_dbus_cookie_sha1 (void)
94 {
95   auth_client_mechanism ("DBUS_COOKIE_SHA1");
96 }
97
98 /* ---------------------------------------------------------------------------------------------------- */
99
100 static gboolean
101 on_new_connection (GDBusServer     *server,
102                    GDBusConnection *connection,
103                    gpointer         user_data)
104 {
105   GMainLoop *loop = user_data;
106   g_main_loop_quit (loop);
107   return FALSE;
108 }
109
110 static gboolean
111 on_timeout (gpointer user_data)
112 {
113   g_error ("Timeout waiting for client");
114   g_assert_not_reached ();
115   return FALSE;
116 }
117
118 static gpointer
119 dbus_1_client_thread_func (gpointer user_data)
120 {
121   const gchar *address = user_data;
122   gchar *str_stdout;
123   gchar *str_stderr;
124   gint exit_status;
125   gboolean rc;
126   gchar *command_line;
127   GError *error;
128
129   // For the dbus part, just use dbus-monitor to connect(1)
130   command_line = g_strdup_printf ("dbus-monitor --address %s", address);
131
132   error = NULL;
133   rc = g_spawn_command_line_sync (command_line,
134                                   &str_stdout,
135                                   &str_stderr,
136                                   &exit_status,
137                                   &error);
138   g_assert_no_error (error);
139   g_assert (rc);
140   g_free (str_stdout);
141   g_free (str_stderr);
142   g_free (command_line);
143   return NULL;
144 }
145
146 static void
147 auth_server_mechanism (const gchar *mechanism)
148 {
149   gchar *addr;
150   gchar *guid;
151   GDBusServer *server;
152   GDBusAuthObserver *auth_observer;
153   GMainLoop *loop;
154   GError *error;
155   GThread *client_thread;
156   GDBusServerFlags flags;
157
158   /* Skip DBUS_COOKIE_SHA1 test unless we have a sufficiently new version of dbus-1 */
159   if (g_strcmp0 (mechanism, "DBUS_COOKIE_SHA1") == 0)
160     {
161       int dbus_major, dbus_minor, dbus_micro;
162       dbus_get_version (&dbus_major, &dbus_minor, &dbus_micro);
163       if (!((dbus_major == 1 && dbus_minor == 4 && dbus_micro >= 21) ||
164             (dbus_major == 1 && dbus_minor == 5 && dbus_micro >= 13) ||
165             (dbus_major == 1 && dbus_minor > 5) ||
166             (dbus_major > 1)))
167         {
168           g_print ("Your libdbus-1 library is too old (version %d.%d.%d) so skipping DBUS_COOKIE_SHA1 test. See https://bugs.freedesktop.org/show_bug.cgi?id=48580 for more details.\n",
169                    dbus_major, dbus_minor, dbus_micro);
170           return;
171         }
172     }
173
174   guid = g_dbus_generate_guid ();
175
176 #ifdef G_OS_UNIX
177   if (g_unix_socket_address_abstract_names_supported ())
178     {
179       addr = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
180     }
181   else
182     {
183       gchar *tmpdir;
184       tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
185       addr = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
186       g_free (tmpdir);
187     }
188 #else
189   addr = g_strdup ("nonce-tcp:");
190 #endif
191
192   loop = g_main_loop_new (NULL, FALSE);
193
194   auth_observer = g_dbus_auth_observer_new ();
195
196   flags = G_DBUS_SERVER_FLAGS_NONE;
197   if (g_strcmp0 (mechanism, "ANONYMOUS") == 0)
198     flags |= G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
199
200   error = NULL;
201   server = g_dbus_server_new_sync (addr,
202                                    flags,
203                                    guid,
204                                    auth_observer,
205                                    NULL, /* cancellable */
206                                    &error);
207   g_assert_no_error (error);
208   g_assert (server != NULL);
209
210   g_signal_connect (auth_observer,
211                     "allow-mechanism",
212                     G_CALLBACK (on_allow_mechanism),
213                     (gpointer) mechanism);
214
215   g_signal_connect (server,
216                     "new-connection",
217                     G_CALLBACK (on_new_connection),
218                     loop);
219
220   g_dbus_server_start (server);
221
222   g_timeout_add_seconds (5, on_timeout, NULL);
223
224   /* run the libdbus-1 client in a thread */
225   client_thread = g_thread_new ("dbus-1-client-thread",
226                                 dbus_1_client_thread_func,
227                                 (gpointer) g_dbus_server_get_client_address (server));
228
229   g_main_loop_run (loop);
230
231   g_dbus_server_stop (server);
232
233   g_thread_join (client_thread);
234
235   g_free (addr);
236   g_free (guid);
237   g_object_unref (auth_observer);
238   g_object_unref (server);
239 }
240
241 static void
242 auth_server_anonymous (void)
243 {
244   auth_server_mechanism ("ANONYMOUS");
245 }
246
247 static void
248 auth_server_external (void)
249 {
250   auth_server_mechanism ("EXTERNAL");
251 }
252
253 static void
254 auth_server_dbus_cookie_sha1 (void)
255 {
256   auth_server_mechanism ("DBUS_COOKIE_SHA1");
257 }
258
259 /* ---------------------------------------------------------------------------------------------------- */
260
261 int
262 main (int   argc,
263       char *argv[])
264 {
265   gint ret;
266
267   setlocale (LC_ALL, "C");
268
269   g_type_init ();
270   g_test_init (&argc, &argv, NULL);
271
272   session_bus_up ();
273
274   g_test_add_func ("/gdbus/auth/client/EXTERNAL",         auth_client_external);
275   g_test_add_func ("/gdbus/auth/client/DBUS_COOKIE_SHA1", auth_client_dbus_cookie_sha1);
276   g_test_add_func ("/gdbus/auth/server/ANONYMOUS",        auth_server_anonymous);
277   g_test_add_func ("/gdbus/auth/server/EXTERNAL",         auth_server_external);
278   g_test_add_func ("/gdbus/auth/server/DBUS_COOKIE_SHA1", auth_server_dbus_cookie_sha1);
279
280   ret = g_test_run();
281
282   /* tear down bus */
283   session_bus_down ();
284
285   return ret;
286 }
287