Blame gio/tests/gdbus-auth.c

Packit ae235b
/* GLib testing framework examples and tests
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2008-2013 Red Hat, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: David Zeuthen <davidz@redhat.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include <locale.h>
Packit ae235b
#include <gio/gio.h>
Packit ae235b
Packit ae235b
#include <string.h>
Packit ae235b
#include <unistd.h>
Packit ae235b
Packit ae235b
#include "gdbus-tests.h"
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
#include <gio/gunixconnection.h>
Packit ae235b
#include <gio/gnetworkingprivate.h>
Packit ae235b
#include <gio/gunixsocketaddress.h>
Packit ae235b
#include <gio/gunixfdlist.h>
Packit ae235b
#endif
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
server_on_allow_mechanism (GDBusAuthObserver *observer,
Packit ae235b
                           const gchar       *mechanism,
Packit ae235b
                           gpointer           user_data)
Packit ae235b
{
Packit ae235b
  const gchar *allowed_mechanism = user_data;
Packit ae235b
  if (allowed_mechanism == NULL || g_strcmp0 (mechanism, allowed_mechanism) == 0)
Packit ae235b
    return TRUE;
Packit ae235b
  else
Packit ae235b
    return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* pass NULL to allow any mechanism */
Packit ae235b
static GDBusServer *
Packit ae235b
server_new_for_mechanism (const gchar *allowed_mechanism)
Packit ae235b
{
Packit ae235b
  gchar *addr;
Packit ae235b
  gchar *guid;
Packit ae235b
  GDBusServer *server;
Packit ae235b
  GDBusAuthObserver *auth_observer;
Packit ae235b
  GError *error;
Packit ae235b
  GDBusServerFlags flags;
Packit ae235b
Packit ae235b
  guid = g_dbus_generate_guid ();
Packit ae235b
Packit ae235b
#ifdef G_OS_UNIX
Packit ae235b
  if (g_unix_socket_address_abstract_names_supported ())
Packit ae235b
    {
Packit ae235b
      addr = g_strdup ("unix:tmpdir=/tmp/gdbus-test-");
Packit ae235b
    }
Packit ae235b
  else
Packit ae235b
    {
Packit ae235b
      gchar *tmpdir;
Packit ae235b
      tmpdir = g_dir_make_tmp ("gdbus-test-XXXXXX", NULL);
Packit ae235b
      addr = g_strdup_printf ("unix:tmpdir=%s", tmpdir);
Packit ae235b
      g_free (tmpdir);
Packit ae235b
    }
Packit ae235b
#else
Packit ae235b
  addr = g_strdup ("nonce-tcp:");
Packit ae235b
#endif
Packit ae235b
Packit ae235b
  auth_observer = g_dbus_auth_observer_new ();
Packit ae235b
Packit ae235b
  flags = G_DBUS_SERVER_FLAGS_NONE;
Packit ae235b
  if (g_strcmp0 (allowed_mechanism, "ANONYMOUS") == 0)
Packit ae235b
    flags |= G_DBUS_SERVER_FLAGS_AUTHENTICATION_ALLOW_ANONYMOUS;
Packit ae235b
Packit ae235b
  error = NULL;
Packit ae235b
  server = g_dbus_server_new_sync (addr,
Packit ae235b
                                   flags,
Packit ae235b
                                   guid,
Packit ae235b
                                   auth_observer,
Packit ae235b
                                   NULL, /* cancellable */
Packit ae235b
                                   &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (server != NULL);
Packit ae235b
Packit ae235b
  g_signal_connect (auth_observer,
Packit ae235b
                    "allow-mechanism",
Packit ae235b
                    G_CALLBACK (server_on_allow_mechanism),
Packit ae235b
                    (gpointer) allowed_mechanism);
Packit ae235b
Packit ae235b
  g_free (addr);
Packit ae235b
  g_free (guid);
Packit ae235b
  g_object_unref (auth_observer);
Packit ae235b
Packit ae235b
  return server;
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
test_auth_on_new_connection (GDBusServer     *server,
Packit ae235b
                             GDBusConnection *connection,
Packit ae235b
                             gpointer         user_data)
Packit ae235b
{
Packit ae235b
  GMainLoop *loop = user_data;
Packit ae235b
  g_main_loop_quit (loop);
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
static gboolean
Packit ae235b
test_auth_on_timeout (gpointer user_data)
Packit ae235b
{
Packit ae235b
  g_error ("Timeout waiting for client");
Packit ae235b
  g_assert_not_reached ();
Packit ae235b
  return FALSE;
Packit ae235b
}
Packit ae235b
Packit ae235b
Packit ae235b
typedef struct
Packit ae235b
{
Packit ae235b
  const gchar *address;
Packit ae235b
  const gchar *allowed_client_mechanism;
Packit ae235b
  const gchar *allowed_server_mechanism;
Packit ae235b
} TestAuthData;
Packit ae235b
Packit ae235b
static gpointer
Packit ae235b
test_auth_client_thread_func (gpointer user_data)
Packit ae235b
{
Packit ae235b
  TestAuthData *data = user_data;
Packit ae235b
  GDBusConnection *c = NULL;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  GDBusAuthObserver *auth_observer = NULL;
Packit ae235b
Packit ae235b
  auth_observer = g_dbus_auth_observer_new ();
Packit ae235b
Packit ae235b
  g_signal_connect (auth_observer,
Packit ae235b
                    "allow-mechanism",
Packit ae235b
                    G_CALLBACK (server_on_allow_mechanism),
Packit ae235b
                    (gpointer) data->allowed_client_mechanism);
Packit ae235b
Packit ae235b
  c = g_dbus_connection_new_for_address_sync (data->address,
Packit ae235b
                                              G_DBUS_CONNECTION_FLAGS_AUTHENTICATION_CLIENT,
Packit ae235b
                                              auth_observer,
Packit ae235b
                                              NULL, /* GCancellable */
Packit ae235b
                                              &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (c != NULL);
Packit ae235b
  g_clear_object (&c);
Packit ae235b
  g_clear_object (&auth_observer);
Packit ae235b
  return NULL;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
test_auth_mechanism (const gchar *allowed_client_mechanism,
Packit ae235b
                     const gchar *allowed_server_mechanism)
Packit ae235b
{
Packit ae235b
  GDBusServer *server;
Packit ae235b
  GMainLoop *loop;
Packit ae235b
  GThread *client_thread;
Packit ae235b
  TestAuthData data;
Packit ae235b
Packit ae235b
  server = server_new_for_mechanism (allowed_server_mechanism);
Packit ae235b
Packit ae235b
  loop = g_main_loop_new (NULL, FALSE);
Packit ae235b
Packit ae235b
  g_signal_connect (server,
Packit ae235b
                    "new-connection",
Packit ae235b
                    G_CALLBACK (test_auth_on_new_connection),
Packit ae235b
                    loop);
Packit ae235b
Packit ae235b
  g_timeout_add_seconds (5, test_auth_on_timeout, NULL);
Packit ae235b
Packit ae235b
  data.allowed_client_mechanism = allowed_client_mechanism;
Packit ae235b
  data.allowed_server_mechanism = allowed_server_mechanism;
Packit ae235b
  data.address = g_dbus_server_get_client_address (server);
Packit ae235b
Packit ae235b
  /* run the D-Bus client in a thread */
Packit ae235b
  client_thread = g_thread_new ("gdbus-client-thread",
Packit ae235b
                                test_auth_client_thread_func,
Packit ae235b
                                &data);
Packit ae235b
Packit ae235b
  g_dbus_server_start (server);
Packit ae235b
Packit ae235b
  g_main_loop_run (loop);
Packit ae235b
Packit ae235b
  g_dbus_server_stop (server);
Packit ae235b
Packit ae235b
  g_thread_join (client_thread);
Packit ae235b
Packit ae235b
  while (g_main_context_iteration (NULL, FALSE));
Packit ae235b
  g_main_loop_unref (loop);
Packit ae235b
Packit ae235b
  g_object_unref (server);
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static void
Packit ae235b
auth_client_external (void)
Packit ae235b
{
Packit ae235b
  test_auth_mechanism ("EXTERNAL", NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
auth_client_dbus_cookie_sha1 (void)
Packit ae235b
{
Packit ae235b
  test_auth_mechanism ("DBUS_COOKIE_SHA1", NULL);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
auth_server_anonymous (void)
Packit ae235b
{
Packit ae235b
  test_auth_mechanism (NULL, "ANONYMOUS");
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
auth_server_external (void)
Packit ae235b
{
Packit ae235b
  test_auth_mechanism (NULL, "EXTERNAL");
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
auth_server_dbus_cookie_sha1 (void)
Packit ae235b
{
Packit ae235b
  test_auth_mechanism (NULL, "DBUS_COOKIE_SHA1");
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
static gchar *temp_dbus_keyrings_dir = NULL;
Packit ae235b
Packit ae235b
static void
Packit ae235b
temp_dbus_keyrings_setup (void)
Packit ae235b
{
Packit ae235b
  GError *error = NULL;
Packit ae235b
Packit ae235b
  g_assert (temp_dbus_keyrings_dir == NULL);
Packit ae235b
  temp_dbus_keyrings_dir = g_dir_make_tmp ("gdbus-test-dbus-keyrings-XXXXXX", &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (temp_dbus_keyrings_dir != NULL);
Packit ae235b
  g_setenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR", temp_dbus_keyrings_dir, TRUE);
Packit ae235b
  g_setenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION", "1", TRUE);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
temp_dbus_keyrings_teardown (void)
Packit ae235b
{
Packit ae235b
  GDir *dir;
Packit ae235b
  GError *error = NULL;
Packit ae235b
  const gchar *name;
Packit ae235b
Packit ae235b
  g_assert (temp_dbus_keyrings_dir != NULL);
Packit ae235b
Packit ae235b
  dir = g_dir_open (temp_dbus_keyrings_dir, 0, &error);
Packit ae235b
  g_assert_no_error (error);
Packit ae235b
  g_assert (dir != NULL);
Packit ae235b
  while ((name = g_dir_read_name (dir)) != NULL)
Packit ae235b
    {
Packit ae235b
      gchar *path = g_build_filename (temp_dbus_keyrings_dir, name, NULL);
Packit ae235b
      g_assert (unlink (path) == 0);
Packit ae235b
      g_free (path);
Packit ae235b
    }
Packit ae235b
  g_dir_close (dir);
Packit ae235b
  g_assert (rmdir (temp_dbus_keyrings_dir) == 0);
Packit ae235b
Packit ae235b
  g_free (temp_dbus_keyrings_dir);
Packit ae235b
  temp_dbus_keyrings_dir = NULL;
Packit ae235b
  g_unsetenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR");
Packit ae235b
  g_unsetenv ("G_DBUS_COOKIE_SHA1_KEYRING_DIR_IGNORE_PERMISSION");
Packit ae235b
}
Packit ae235b
Packit ae235b
/* ---------------------------------------------------------------------------------------------------- */
Packit ae235b
Packit ae235b
int
Packit ae235b
main (int   argc,
Packit ae235b
      char *argv[])
Packit ae235b
{
Packit ae235b
  gint ret;
Packit ae235b
Packit ae235b
  setlocale (LC_ALL, "C");
Packit ae235b
Packit ae235b
  temp_dbus_keyrings_setup ();
Packit ae235b
Packit ae235b
  g_test_init (&argc, &argv, NULL);
Packit ae235b
Packit ae235b
  g_test_add_func ("/gdbus/auth/client/EXTERNAL",         auth_client_external);
Packit ae235b
  g_test_add_func ("/gdbus/auth/client/DBUS_COOKIE_SHA1", auth_client_dbus_cookie_sha1);
Packit ae235b
  g_test_add_func ("/gdbus/auth/server/ANONYMOUS",        auth_server_anonymous);
Packit ae235b
  g_test_add_func ("/gdbus/auth/server/EXTERNAL",         auth_server_external);
Packit ae235b
  g_test_add_func ("/gdbus/auth/server/DBUS_COOKIE_SHA1", auth_server_dbus_cookie_sha1);
Packit ae235b
Packit ae235b
  /* TODO: we currently don't have tests for
Packit ae235b
   *
Packit ae235b
   *  - DBUS_COOKIE_SHA1 timeouts (and clock changes etc)
Packit ae235b
   *  - interoperability with libdbus-1 implementations of authentication methods (both client and server)
Packit ae235b
   */
Packit ae235b
Packit ae235b
  ret = g_test_run();
Packit ae235b
Packit ae235b
  temp_dbus_keyrings_teardown ();
Packit ae235b
Packit ae235b
  return ret;
Packit ae235b
}
Packit ae235b