1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2013 Red Hat, Inc.
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.
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.
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.
20 * Author: David Zeuthen <davidz@redhat.com>
29 #include "gdbus-tests.h"
32 #include <gio/gunixconnection.h>
33 #include <gio/gnetworkingprivate.h>
34 #include <gio/gunixsocketaddress.h>
35 #include <gio/gunixfdlist.h>
38 /* ---------------------------------------------------------------------------------------------------- */
41 server_on_allow_mechanism (GDBusAuthObserver *observer,
42 const gchar *mechanism,
45 const gchar *allowed_mechanism = user_data;
46 if (allowed_mechanism == NULL || g_strcmp0 (mechanism, allowed_mechanism) == 0)
52 /* pass NULL to allow any mechanism */
54 server_new_for_mechanism (const gchar *allowed_mechanism)
59 GDBusAuthObserver *auth_observer;
61 GDBusServerFlags flags;
63 guid = g_dbus_generate_guid ();
66 if (g_unix_socket_address_abstract_names_supported ())
68 addr = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
73 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
74 addr = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
78 addr = g_strdup ("nonce-tcp:");
81 auth_observer = g_dbus_auth_observer_new ();
83 flags = G_DBUS_SERVER_FLAGS_NONE;
84 if (g_strcmp0 (allowed_mechanism, "ANONYMOUS") == 0)
85 flags |= G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
88 server = g_dbus_server_new_sync (addr,
92 NULL, /* cancellable */
94 g_assert_no_error (error);
95 g_assert (server != NULL);
97 g_signal_connect (auth_observer,
99 G_CALLBACK (server_on_allow_mechanism),
100 (gpointer) allowed_mechanism);
104 g_object_unref (auth_observer);
109 /* ---------------------------------------------------------------------------------------------------- */
112 test_auth_on_new_connection (GDBusServer *server,
113 GDBusConnection *connection,
116 GMainLoop *loop = user_data;
117 g_main_loop_quit (loop);
122 test_auth_on_timeout (gpointer user_data)
124 g_error ("Timeout waiting for client");
125 g_assert_not_reached ();
132 const gchar *address;
133 const gchar *allowed_client_mechanism;
134 const gchar *allowed_server_mechanism;
138 test_auth_client_thread_func (gpointer user_data)
140 TestAuthData *data = user_data;
141 GDBusConnection *c = NULL;
142 GError *error = NULL;
143 GDBusAuthObserver *auth_observer = NULL;
145 auth_observer = g_dbus_auth_observer_new ();
147 g_signal_connect (auth_observer,
149 G_CALLBACK (server_on_allow_mechanism),
150 (gpointer) data->allowed_client_mechanism);
152 c = g_dbus_connection_new_for_address_sync (data->address,
153 G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
155 NULL, /* GCancellable */
157 g_assert_no_error (error);
158 g_assert (c != NULL);
160 g_clear_object (&auth_observer);
165 test_auth_mechanism (const gchar *allowed_client_mechanism,
166 const gchar *allowed_server_mechanism)
170 GThread *client_thread;
173 server = server_new_for_mechanism (allowed_server_mechanism);
175 loop = g_main_loop_new (NULL, FALSE);
177 g_signal_connect (server,
179 G_CALLBACK (test_auth_on_new_connection),
182 g_timeout_add_seconds (5, test_auth_on_timeout, NULL);
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);
188 /* run the D-Bus client in a thread */
189 client_thread = g_thread_new ("gdbus-client-thread",
190 test_auth_client_thread_func,
193 g_dbus_server_start (server);
195 g_main_loop_run (loop);
197 g_dbus_server_stop (server);
199 g_thread_join (client_thread);
201 g_object_unref (server);
204 /* ---------------------------------------------------------------------------------------------------- */
207 auth_client_external (void)
209 test_auth_mechanism ("EXTERNAL", NULL);
213 auth_client_dbus_cookie_sha1 (void)
215 test_auth_mechanism ("DBUS_COOKIE_SHA1", NULL);
219 auth_server_anonymous (void)
221 test_auth_mechanism (NULL, "ANONYMOUS");
225 auth_server_external (void)
227 test_auth_mechanism (NULL, "EXTERNAL");
231 auth_server_dbus_cookie_sha1 (void)
233 test_auth_mechanism (NULL, "DBUS_COOKIE_SHA1");
236 /* ---------------------------------------------------------------------------------------------------- */
238 static gchar *temp_dbus_keyrings_dir = NULL;
241 temp_dbus_keyrings_setup (void)
243 GError *error = NULL;
245 g_assert (temp_dbus_keyrings_dir == NULL);
246 temp_dbus_keyrings_dir = g_dir_make_tmp ("gdbus-test-dbus-keyrings-XXXXXX", &error);
247 g_assert_no_error (error);
248 g_assert (temp_dbus_keyrings_dir != NULL);
249 g_setenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR", temp_dbus_keyrings_dir, TRUE);
250 g_setenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION", "1", TRUE);
254 temp_dbus_keyrings_teardown (void)
257 GError *error = NULL;
260 g_assert (temp_dbus_keyrings_dir != NULL);
262 dir = g_dir_open (temp_dbus_keyrings_dir, 0, &error);
263 g_assert_no_error (error);
264 g_assert (dir != NULL);
265 while ((name = g_dir_read_name (dir)) != NULL)
267 gchar *path = g_build_filename (temp_dbus_keyrings_dir, name, NULL);
268 g_assert (unlink (path) == 0);
272 g_assert (rmdir (temp_dbus_keyrings_dir) == 0);
274 temp_dbus_keyrings_dir = NULL;
275 g_unsetenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR");
276 g_unsetenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION");
279 /* ---------------------------------------------------------------------------------------------------- */
287 setlocale (LC_ALL, "C");
289 temp_dbus_keyrings_setup ();
291 g_test_init (&argc, &argv, NULL);
293 g_test_add_func ("/gdbus/auth/client/EXTERNAL", auth_client_external);
294 g_test_add_func ("/gdbus/auth/client/DBUS_COOKIE_SHA1", auth_client_dbus_cookie_sha1);
295 g_test_add_func ("/gdbus/auth/server/ANONYMOUS", auth_server_anonymous);
296 g_test_add_func ("/gdbus/auth/server/EXTERNAL", auth_server_external);
297 g_test_add_func ("/gdbus/auth/server/DBUS_COOKIE_SHA1", auth_server_dbus_cookie_sha1);
299 /* TODO: we currently don't have tests for
301 * - DBUS_COOKIE_SHA1 timeouts (and clock changes etc)
302 * - interoperability with libdbus-1 implementations of authentication methods (both client and server)
307 temp_dbus_keyrings_teardown ();