1 /* GLib testing framework examples and tests
3 * Copyright (C) 2008-2012 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>
28 #include <dbus/dbus.h>
30 #include "gdbus-tests.h"
33 #include <gio/gunixconnection.h>
34 #include <gio/gnetworkingprivate.h>
35 #include <gio/gunixsocketaddress.h>
36 #include <gio/gunixfdlist.h>
39 /* ---------------------------------------------------------------------------------------------------- */
42 on_allow_mechanism (GDBusAuthObserver *observer,
43 const gchar *mechanism,
46 const gchar *mechanism_to_allow = user_data;
47 if (g_strcmp0 (mechanism, mechanism_to_allow) == 0)
54 auth_client_mechanism (const gchar *mechanism)
56 gchar *address = NULL;
57 GDBusConnection *c = NULL;
59 GDBusAuthObserver *auth_observer;
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);
65 auth_observer = g_dbus_auth_observer_new ();
67 g_signal_connect (auth_observer,
69 G_CALLBACK (on_allow_mechanism),
70 (gpointer) mechanism);
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,
78 g_assert_no_error (error);
83 g_object_unref (auth_observer);
87 auth_client_external (void)
89 auth_client_mechanism ("EXTERNAL");
93 auth_client_dbus_cookie_sha1 (void)
95 auth_client_mechanism ("DBUS_COOKIE_SHA1");
98 /* ---------------------------------------------------------------------------------------------------- */
101 on_new_connection (GDBusServer *server,
102 GDBusConnection *connection,
105 GMainLoop *loop = user_data;
106 g_main_loop_quit (loop);
111 on_timeout (gpointer user_data)
113 g_error ("Timeout waiting for client");
114 g_assert_not_reached ();
119 dbus_1_client_thread_func (gpointer user_data)
121 const gchar *address = user_data;
129 // For the dbus part, just use dbus-monitor to connect(1)
130 command_line = g_strdup_printf ("dbus-monitor --address %s", address);
133 rc = g_spawn_command_line_sync (command_line,
138 g_assert_no_error (error);
142 g_free (command_line);
147 auth_server_mechanism (const gchar *mechanism)
152 GDBusAuthObserver *auth_observer;
155 GThread *client_thread;
156 GDBusServerFlags flags;
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)
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) ||
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);
174 guid = g_dbus_generate_guid ();
177 if (g_unix_socket_address_abstract_names_supported ())
179 addr = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
184 tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
185 addr = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
189 addr = g_strdup ("nonce-tcp:");
192 loop = g_main_loop_new (NULL, FALSE);
194 auth_observer = g_dbus_auth_observer_new ();
196 flags = G_DBUS_SERVER_FLAGS_NONE;
197 if (g_strcmp0 (mechanism, "ANONYMOUS") == 0)
198 flags |= G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
201 server = g_dbus_server_new_sync (addr,
205 NULL, /* cancellable */
207 g_assert_no_error (error);
208 g_assert (server != NULL);
210 g_signal_connect (auth_observer,
212 G_CALLBACK (on_allow_mechanism),
213 (gpointer) mechanism);
215 g_signal_connect (server,
217 G_CALLBACK (on_new_connection),
220 g_dbus_server_start (server);
222 g_timeout_add_seconds (5, on_timeout, NULL);
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));
229 g_main_loop_run (loop);
231 g_dbus_server_stop (server);
233 g_thread_join (client_thread);
237 g_object_unref (auth_observer);
238 g_object_unref (server);
242 auth_server_anonymous (void)
244 auth_server_mechanism ("ANONYMOUS");
248 auth_server_external (void)
250 auth_server_mechanism ("EXTERNAL");
254 auth_server_dbus_cookie_sha1 (void)
256 auth_server_mechanism ("DBUS_COOKIE_SHA1");
259 /* ---------------------------------------------------------------------------------------------------- */
267 setlocale (LC_ALL, "C");
270 g_test_init (&argc, &argv, NULL);
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);