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