GDBus: Avoid use of libdbus-1 in authentication tests
[platform/upstream/glib.git] / gio / tests / gdbus-auth.c
1 /* GLib testing framework examples and tests
2  *
3  * Copyright (C) 2008-2013 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 server_on_allow_mechanism (GDBusAuthObserver *observer,
43                            const gchar       *mechanism,
44                            gpointer           user_data)
45 {
46   const gchar *allowed_mechanism = user_data;
47   if (allowed_mechanism == NULL || g_strcmp0 (mechanism, allowed_mechanism) == 0)
48     return TRUE;
49   else
50     return FALSE;
51 }
52
53 /* pass NULL to allow any mechanism */
54 static GDBusServer *
55 server_new_for_mechanism (const gchar *allowed_mechanism)
56 {
57   gchar *addr;
58   gchar *guid;
59   GDBusServer *server;
60   GDBusAuthObserver *auth_observer;
61   GError *error;
62   GDBusServerFlags flags;
63
64   guid = g_dbus_generate_guid ();
65
66 #ifdef G_OS_UNIX
67   if (g_unix_socket_address_abstract_names_supported ())
68     {
69       addr = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
70     }
71   else
72     {
73       gchar *tmpdir;
74       tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
75       addr = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
76       g_free (tmpdir);
77     }
78 #else
79   addr = g_strdup ("nonce-tcp:");
80 #endif
81
82   auth_observer = g_dbus_auth_observer_new ();
83
84   flags = G_DBUS_SERVER_FLAGS_NONE;
85   if (g_strcmp0 (allowed_mechanism, "ANONYMOUS") == 0)
86     flags |= G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
87
88   error = NULL;
89   server = g_dbus_server_new_sync (addr,
90                                    flags,
91                                    guid,
92                                    auth_observer,
93                                    NULL, /* cancellable */
94                                    &error);
95   g_assert_no_error (error);
96   g_assert (server != NULL);
97
98   g_signal_connect (auth_observer,
99                     "allow-mechanism",
100                     G_CALLBACK (server_on_allow_mechanism),
101                     (gpointer) allowed_mechanism);
102
103   g_free (addr);
104   g_free (guid);
105   g_object_unref (auth_observer);
106
107   return server;
108 }
109
110 /* ---------------------------------------------------------------------------------------------------- */
111
112 static gboolean
113 test_auth_on_new_connection (GDBusServer     *server,
114                              GDBusConnection *connection,
115                              gpointer         user_data)
116 {
117   GMainLoop *loop = user_data;
118   g_main_loop_quit (loop);
119   return FALSE;
120 }
121
122 static gboolean
123 test_auth_on_timeout (gpointer user_data)
124 {
125   g_error ("Timeout waiting for client");
126   g_assert_not_reached ();
127   return FALSE;
128 }
129
130
131 typedef struct
132 {
133   const gchar *address;
134   const gchar *allowed_client_mechanism;
135   const gchar *allowed_server_mechanism;
136 } TestAuthData;
137
138 static gpointer
139 test_auth_client_thread_func (gpointer user_data)
140 {
141   TestAuthData *data = user_data;
142   GDBusConnection *c = NULL;
143   GError *error = NULL;
144   GDBusAuthObserver *auth_observer = NULL;
145
146   auth_observer = g_dbus_auth_observer_new ();
147
148   g_signal_connect (auth_observer,
149                     "allow-mechanism",
150                     G_CALLBACK (server_on_allow_mechanism),
151                     (gpointer) data->allowed_client_mechanism);
152
153   c = g_dbus_connection_new_for_address_sync (data->address,
154                                               G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
155                                               auth_observer,
156                                               NULL, /* GCancellable */
157                                               &error);
158   g_assert_no_error (error);
159   g_assert (c != NULL);
160   g_clear_object (&c);
161   g_clear_object (&auth_observer);
162   return NULL;
163 }
164
165 static void
166 test_auth_mechanism (const gchar *allowed_client_mechanism,
167                      const gchar *allowed_server_mechanism)
168 {
169   GDBusServer *server;
170   GMainLoop *loop;
171   GThread *client_thread;
172   TestAuthData data;
173
174   server = server_new_for_mechanism (allowed_server_mechanism);
175
176   loop = g_main_loop_new (NULL, FALSE);
177
178   g_signal_connect (server,
179                     "new-connection",
180                     G_CALLBACK (test_auth_on_new_connection),
181                     loop);
182
183   g_timeout_add_seconds (5, test_auth_on_timeout, NULL);
184
185   data.allowed_client_mechanism = allowed_client_mechanism;
186   data.allowed_server_mechanism = allowed_server_mechanism;
187   data.address = g_dbus_server_get_client_address (server);
188
189   /* run the D-Bus client in a thread */
190   client_thread = g_thread_new ("gdbus-client-thread",
191                                 test_auth_client_thread_func,
192                                 &data);
193
194   g_dbus_server_start (server);
195
196   g_main_loop_run (loop);
197
198   g_dbus_server_stop (server);
199
200   g_thread_join (client_thread);
201
202   g_object_unref (server);
203 }
204
205 /* ---------------------------------------------------------------------------------------------------- */
206
207 static void
208 auth_client_external (void)
209 {
210   test_auth_mechanism ("EXTERNAL", NULL);
211 }
212
213 static void
214 auth_client_dbus_cookie_sha1 (void)
215 {
216   test_auth_mechanism ("DBUS_COOKIE_SHA1", NULL);
217 }
218
219 static void
220 auth_server_anonymous (void)
221 {
222   test_auth_mechanism (NULL, "ANONYMOUS");
223 }
224
225 static void
226 auth_server_external (void)
227 {
228   test_auth_mechanism (NULL, "EXTERNAL");
229 }
230
231 static void
232 auth_server_dbus_cookie_sha1 (void)
233 {
234   test_auth_mechanism (NULL, "DBUS_COOKIE_SHA1");
235 }
236
237 /* ---------------------------------------------------------------------------------------------------- */
238
239 static gchar *temp_dbus_keyrings_dir = NULL;
240
241 static void
242 temp_dbus_keyrings_setup (void)
243 {
244   GError *error = NULL;
245
246   g_assert (temp_dbus_keyrings_dir == NULL);
247   temp_dbus_keyrings_dir = g_dir_make_tmp ("gdbus-test-dbus-keyrings-XXXXXX", &error);
248   g_assert_no_error (error);
249   g_assert (temp_dbus_keyrings_dir != NULL);
250   g_setenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR", temp_dbus_keyrings_dir, TRUE);
251   g_setenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION", "1", TRUE);
252 }
253
254 static void
255 temp_dbus_keyrings_teardown (void)
256 {
257   GDir *dir;
258   GError *error = NULL;
259   const gchar *name;
260
261   g_assert (temp_dbus_keyrings_dir != NULL);
262
263   dir = g_dir_open (temp_dbus_keyrings_dir, 0, &error);
264   g_assert_no_error (error);
265   g_assert (dir != NULL);
266   while ((name = g_dir_read_name (dir)) != NULL)
267     {
268       gchar *path = g_build_filename (temp_dbus_keyrings_dir, name, NULL);
269       g_assert (unlink (path) == 0);
270       g_free (path);
271     }
272   g_dir_close (dir);
273   g_assert (rmdir (temp_dbus_keyrings_dir) == 0);
274
275   temp_dbus_keyrings_dir = NULL;
276   g_setenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR", NULL, TRUE);
277   g_setenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION", NULL, TRUE);
278 }
279
280 /* ---------------------------------------------------------------------------------------------------- */
281
282 int
283 main (int   argc,
284       char *argv[])
285 {
286   gint ret;
287
288   setlocale (LC_ALL, "C");
289
290   temp_dbus_keyrings_setup ();
291
292   g_test_init (&argc, &argv, NULL);
293
294   g_test_add_func ("/gdbus/auth/client/EXTERNAL",         auth_client_external);
295   g_test_add_func ("/gdbus/auth/client/DBUS_COOKIE_SHA1", auth_client_dbus_cookie_sha1);
296   g_test_add_func ("/gdbus/auth/server/ANONYMOUS",        auth_server_anonymous);
297   g_test_add_func ("/gdbus/auth/server/EXTERNAL",         auth_server_external);
298   g_test_add_func ("/gdbus/auth/server/DBUS_COOKIE_SHA1", auth_server_dbus_cookie_sha1);
299
300   /* TODO: we currently don't have tests for
301    *
302    *  - DBUS_COOKIE_SHA1 timeouts (and clock changes etc)
303    *  - interoperability with libdbus-1 implementations of authentication methods (both client and server)
304    */
305
306   ret = g_test_run();
307
308   temp_dbus_keyrings_teardown ();
309
310   return ret;
311 }
312