Commit 15d2e90a authored by Nikki VonHollen's avatar Nikki VonHollen Committed by David Zeuthen

Bug 43608 – Add unit tests

https://bugs.freedesktop.org/show_bug.cgi?id=43608

Basic unittest support and a few tests. Adds basic unit tests for:

PolkitIdentity, PolkitUnixUser, PolkitUnixGroup,
PolkitBackendLocalAuthorizationStore, and PolkitBackendLocalAuthority.
Signed-off-by: 's avatarDavid Zeuthen <davidz@redhat.com>
parent 2675cc0d
## Process this file with automake to produce Makefile.in
SUBDIRS = actions data src docs po
SUBDIRS = actions data src docs po test
NULL =
......
......@@ -419,6 +419,9 @@ docs/Makefile
docs/polkit/Makefile
docs/man/Makefile
po/Makefile.in
test/Makefile
test/polkit/Makefile
test/polkitbackend/Makefile
])
dnl ==========================================================================
......
......@@ -58,17 +58,28 @@ static GList *get_groups_for_user (PolkitIdentity *user);
typedef struct
{
gchar *config_path;
PolkitBackendConfigSource *config_source;
gchar **authorization_store_paths;
GList *authorization_stores;
GFileMonitor *sysconf_dir_monitor;
GFileMonitor *localstate_dir_monitor;
GList *authorization_store_monitors;
} PolkitBackendLocalAuthorityPrivate;
/* ---------------------------------------------------------------------------------------------------- */
enum
{
PROP_0,
// Path overrides used for unit testing
PROP_CONFIG_PATH,
PROP_AUTH_STORE_PATHS,
};
/* ---------------------------------------------------------------------------------------------------- */
static GList *polkit_backend_local_authority_get_admin_auth_identities (PolkitBackendInteractiveAuthority *authority,
PolkitSubject *caller,
PolkitSubject *subject,
......@@ -169,13 +180,15 @@ authorization_store_path_compare_func (GFile *file_a,
static void
add_all_authorization_stores (PolkitBackendLocalAuthority *authority)
{
PolkitBackendLocalAuthorityPrivate *priv;
guint n;
GList *directories;
GList *l;
priv = POLKIT_BACKEND_LOCAL_AUTHORITY_GET_PRIVATE (authority);
directories = NULL;
for (n = 0; n < 2; n++)
for (n = 0; priv->authorization_store_paths && priv->authorization_store_paths[n]; n++)
{
const gchar *toplevel_path;
GFile *toplevel_directory;
......@@ -185,11 +198,7 @@ add_all_authorization_stores (PolkitBackendLocalAuthority *authority)
error = NULL;
if (n == 0)
toplevel_path = PACKAGE_LOCALSTATE_DIR "/lib/polkit-1/localauthority";
else
toplevel_path = PACKAGE_SYSCONF_DIR "/polkit-1/localauthority";
toplevel_path = priv->authorization_store_paths[n];
toplevel_directory = g_file_new_for_path (toplevel_path);
directory_enumerator = g_file_enumerate_children (toplevel_directory,
"standard::name,standard::type",
......@@ -276,30 +285,41 @@ static void
polkit_backend_local_authority_init (PolkitBackendLocalAuthority *authority)
{
PolkitBackendLocalAuthorityPrivate *priv;
priv = POLKIT_BACKEND_LOCAL_AUTHORITY_GET_PRIVATE (authority);
priv->config_path = NULL;
priv->authorization_store_paths = NULL;
}
static void
polkit_backend_local_authority_constructed (GObject *object)
{
PolkitBackendLocalAuthority *authority;
PolkitBackendLocalAuthorityPrivate *priv;
GFile *config_directory;
guint n;
authority = POLKIT_BACKEND_LOCAL_AUTHORITY (object);
priv = POLKIT_BACKEND_LOCAL_AUTHORITY_GET_PRIVATE (authority);
config_directory = g_file_new_for_path (PACKAGE_SYSCONF_DIR "/polkit-1/localauthority.conf.d");
g_debug ("Using config directory `%s'", priv->config_path);
config_directory = g_file_new_for_path (priv->config_path);
priv->config_source = polkit_backend_config_source_new (config_directory);
g_object_unref (config_directory);
add_all_authorization_stores (authority);
/* Monitor the toplevels */
for (n = 0; n < 2; n++)
priv->authorization_store_monitors = NULL;
for (n = 0; priv->authorization_store_paths && priv->authorization_store_paths[n]; n++)
{
const gchar *toplevel_path;
GFile *toplevel_directory;
GFileMonitor *monitor;
GError *error;
if (n == 0)
toplevel_path = PACKAGE_LOCALSTATE_DIR "/lib/polkit-1/localauthority";
else
toplevel_path = PACKAGE_SYSCONF_DIR "/polkit-1/localauthority";
toplevel_path = priv->authorization_store_paths[n];
toplevel_directory = g_file_new_for_path (toplevel_path);
error = NULL;
......@@ -322,13 +342,12 @@ polkit_backend_local_authority_init (PolkitBackendLocalAuthority *authority)
G_CALLBACK (on_toplevel_authority_store_monitor_changed),
authority);
if (n == 0)
priv->sysconf_dir_monitor = monitor;
else
priv->localstate_dir_monitor = monitor;
priv->authorization_store_monitors = g_list_append (priv->authorization_store_monitors, monitor);
g_object_unref (toplevel_directory);
}
G_OBJECT_CLASS (polkit_backend_local_authority_parent_class)->constructed (object);
}
static void
......@@ -342,14 +361,14 @@ polkit_backend_local_authority_finalize (GObject *object)
purge_all_authorization_stores (local_authority);
if (priv->sysconf_dir_monitor != NULL)
g_object_unref (priv->sysconf_dir_monitor);
if (priv->localstate_dir_monitor != NULL)
g_object_unref (priv->localstate_dir_monitor);
g_list_free_full (priv->authorization_store_monitors, g_object_unref);
if (priv->config_source != NULL)
g_object_unref (priv->config_source);
g_free (priv->config_path);
g_strfreev (priv->authorization_store_paths);
G_OBJECT_CLASS (polkit_backend_local_authority_parent_class)->finalize (object);
}
......@@ -371,24 +390,67 @@ polkit_backend_local_authority_get_features (PolkitBackendAuthority *authority)
return POLKIT_AUTHORITY_FEATURES_TEMPORARY_AUTHORIZATION;
}
static void
polkit_backend_local_authority_set_property (GObject *object, guint property_id, const GValue *value, GParamSpec *pspec)
{
PolkitBackendLocalAuthority *local_authority;
PolkitBackendLocalAuthorityPrivate *priv;
local_authority = POLKIT_BACKEND_LOCAL_AUTHORITY (object);
priv = POLKIT_BACKEND_LOCAL_AUTHORITY_GET_PRIVATE (local_authority);
switch (property_id)
{
case PROP_CONFIG_PATH:
g_free (priv->config_path);
priv->config_path = g_value_dup_string (value);
break;
case PROP_AUTH_STORE_PATHS:
g_strfreev (priv->authorization_store_paths);
priv->authorization_store_paths = g_strsplit (g_value_get_string (value), ";", 0);
break;
default:
G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
break;
}
}
static void
polkit_backend_local_authority_class_init (PolkitBackendLocalAuthorityClass *klass)
{
GObjectClass *gobject_class;
PolkitBackendAuthorityClass *authority_class;
PolkitBackendInteractiveAuthorityClass *interactive_authority_class;
GParamSpec *pspec;
gobject_class = G_OBJECT_CLASS (klass);
authority_class = POLKIT_BACKEND_AUTHORITY_CLASS (klass);
interactive_authority_class = POLKIT_BACKEND_INTERACTIVE_AUTHORITY_CLASS (klass);
gobject_class->set_property = polkit_backend_local_authority_set_property;
gobject_class->finalize = polkit_backend_local_authority_finalize;
gobject_class->constructed = polkit_backend_local_authority_constructed;
authority_class->get_name = polkit_backend_local_authority_get_name;
authority_class->get_version = polkit_backend_local_authority_get_version;
authority_class->get_features = polkit_backend_local_authority_get_features;
interactive_authority_class->get_admin_identities = polkit_backend_local_authority_get_admin_auth_identities;
interactive_authority_class->check_authorization_sync = polkit_backend_local_authority_check_authorization_sync;
pspec = g_param_spec_string ("config-path",
"Local Authority Configuration Path",
"Path to directory of LocalAuthority config files.",
PACKAGE_SYSCONF_DIR "/polkit-1/localauthority.conf.d",
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
g_object_class_install_property (gobject_class, PROP_CONFIG_PATH, pspec);
pspec = g_param_spec_string ("auth-store-paths",
"Local Authorization Store Paths",
"Semi-colon separated list of Authorization Store 'top' directories.",
PACKAGE_LOCALSTATE_DIR "/lib/polkit-1/localauthority;"
PACKAGE_SYSCONF_DIR "/polkit-1/localauthority",
G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE);
g_object_class_install_property (gobject_class, PROP_AUTH_STORE_PATHS, pspec);
g_type_class_add_private (klass, sizeof (PolkitBackendLocalAuthorityPrivate));
}
......
SUBDIRS = . polkit polkitbackend
AM_CFLAGS = $(GLIB_CFLAGS)
check_LTLIBRARIES = libpolkit-test-helper.la
libpolkit_test_helper_la_SOURCES = polkittesthelper.c polkittesthelper.h
libpolkit_test_helper_la_LIBADD = $(GLIB_LIBS)
clean-local :
rm -f *~
NULL =
INCLUDES = \
-I$(top_builddir)/src \
-I$(top_srcdir)/src \
-DPACKAGE_LIBEXEC_DIR=\""$(libexecdir)"\" \
-DPACKAGE_SYSCONF_DIR=\""$(sysconfdir)"\" \
-DPACKAGE_DATA_DIR=\""$(datadir)"\" \
-DPACKAGE_BIN_DIR=\""$(bindir)"\" \
-DPACKAGE_LOCALSTATE_DIR=\""$(localstatedir)"\" \
-DPACKAGE_LOCALE_DIR=\""$(localedir)"\" \
-DPACKAGE_LIB_DIR=\""$(libdir)"\" \
-D_POSIX_PTHREAD_SEMANTICS \
-D_REENTRANT \
$(NULL)
AM_CFLAGS = \
$(GLIB_CFLAGS) \
$(NULL)
LDADD = \
$(GLIB_LIBS) \
$(top_builddir)/src/polkit/libpolkit-gobject-1.la \
$(NULL)
TEST_PROGS =
# ----------------------------------------------------------------------------------------------------
TEST_PROGS += polkitunixusertest
polkitunixusertest_SOURCES = polkitunixusertest.c
TEST_PROGS += polkitunixgrouptest
polkitunixgrouptest_SOURCES = polkitunixgrouptest.c
TEST_PROGS += polkitidentitytest
polkitidentitytest_SOURCES = polkitidentitytest.c
# ----------------------------------------------------------------------------------------------------
check_PROGRAMS = $(TEST_PROGS)
TESTS = $(TEST_PROGS)
clean-local :
rm -f *~
/*
* Copyright (C) 2011 Google Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Nikki VonHollen <vonhollen@google.com>
*/
#include "glib.h"
#include <polkit/polkit.h>
static void
test_user_from_string (void)
{
PolkitIdentity *identity;
PolkitUnixUser *user;
GError *error = NULL;
identity = polkit_identity_from_string ("unix-user:root", &error);
g_assert (identity);
g_assert_no_error (error);
g_assert (POLKIT_IS_UNIX_USER (identity));
user = POLKIT_UNIX_USER (identity);
g_assert (user);
g_object_unref (user);
}
static void
test_group_from_string (void)
{
PolkitIdentity *identity;
PolkitUnixGroup *group;
GError *error = NULL;
identity = polkit_identity_from_string ("unix-group:root", &error);
g_assert (identity);
g_assert_no_error (error);
g_assert (POLKIT_IS_UNIX_GROUP (identity));
group = POLKIT_UNIX_GROUP (identity);
g_assert (group);
g_object_unref (group);
}
static void
test_user_to_string (void)
{
PolkitIdentity *identity;
GError *error = NULL;
gchar *value;
identity = polkit_identity_from_string ("unix-user:root", &error);
g_assert (identity);
g_assert_no_error (error);
value = polkit_identity_to_string (identity);
g_assert_cmpstr (value, ==, "unix-user:root");
g_free (value);
g_object_unref (identity);
}
static void
test_group_to_string (void)
{
PolkitIdentity *identity;
GError *error = NULL;
gchar *value;
identity = polkit_identity_from_string ("unix-group:root", &error);
g_assert (identity);
g_assert_no_error (error);
value = polkit_identity_to_string (identity);
g_assert_cmpstr (value, ==, "unix-group:root");
g_free (value);
g_object_unref (identity);
}
static void
test_equal (void)
{
PolkitIdentity *identity_a, *identity_b;
GError *error = NULL;
identity_a = polkit_identity_from_string ("unix-group:root", &error);
identity_b = polkit_identity_from_string ("unix-group:root", &error);
g_assert (polkit_identity_equal (identity_a, identity_b));
g_object_unref (identity_a);
g_object_unref (identity_b);
}
static void
test_hash (void)
{
PolkitIdentity *identity_a, *identity_b;
guint hash_a, hash_b;
GError *error = NULL;
identity_a = polkit_identity_from_string ("unix-group:root", &error);
identity_b = polkit_identity_from_string ("unix-group:root", &error);
hash_a = polkit_identity_hash (identity_a);
hash_b = polkit_identity_hash (identity_b);
g_assert_cmpint (hash_a, ==, hash_b);
g_object_unref (identity_a);
g_object_unref (identity_b);
}
int
main (int argc, char *argv[])
{
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/PolkitIdentity/user_from_string", test_user_from_string);
g_test_add_func ("/PolkitIdentity/user_to_string", test_user_to_string);
g_test_add_func ("/PolkitIdentity/group_from_string", test_group_from_string);
g_test_add_func ("/PolkitIdentity/group_to_string", test_group_to_string);
g_test_add_func ("/PolkitIdentity/equal", test_equal);
g_test_add_func ("/PolkitIdentity/hash", test_hash);
return g_test_run ();
}
/*
* Copyright (C) 2011 Google Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Nikki VonHollen <vonhollen@google.com>
*/
#include "glib.h"
#include <polkit/polkit.h>
static void
test_new (void)
{
PolkitUnixGroup *group;
group = POLKIT_UNIX_GROUP (polkit_unix_group_new (0));
g_assert (group);
gint group_gid = polkit_unix_group_get_gid (group);
g_assert_cmpint (group_gid, ==, 0);
g_object_unref (group);
}
static void
test_new_for_name (void)
{
GError *error = NULL;
PolkitUnixGroup *group;
group = POLKIT_UNIX_GROUP (polkit_unix_group_new_for_name ("root", &error));
g_assert (group);
g_assert_no_error (error);
gint group_gid = polkit_unix_group_get_gid (group);
g_assert_cmpint (group_gid, ==, 0);
g_object_unref (group);
}
static void
test_set_gid (void)
{
PolkitUnixGroup *group;
group = POLKIT_UNIX_GROUP (polkit_unix_group_new (0));
polkit_unix_group_set_gid (group, 5);
gint group_gid = polkit_unix_group_get_gid (group);
g_assert_cmpint (group_gid, ==, 5);
g_object_unref (group);
}
int
main (int argc, char *argv[])
{
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/PolkitUnixGroup/new", test_new);
g_test_add_func ("/PolkitUnixGroup/new_for_name", test_new_for_name);
g_test_add_func ("/PolkitUnixGroup/set_gid", test_set_gid);
return g_test_run ();
}
/*
* Copyright (C) 2011 Google Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Nikki VonHollen <vonhollen@google.com>
*/
#include "glib.h"
#include <polkit/polkit.h>
static void
test_new (void)
{
PolkitUnixUser *user;
user = POLKIT_UNIX_USER (polkit_unix_user_new (0));
g_assert (user);
gint user_uid = polkit_unix_user_get_uid (user);
g_assert_cmpint (user_uid, ==, 0);
g_object_unref (user);
}
static void
test_new_for_name (void)
{
GError *error = NULL;
PolkitUnixUser *user;
user = POLKIT_UNIX_USER (polkit_unix_user_new_for_name ("root", &error));
g_assert (user);
g_assert_no_error (error);
gint user_uid = polkit_unix_user_get_uid (user);
g_assert_cmpint (user_uid, ==, 0);
g_object_unref (user);
}
static void
test_set_uid (void)
{
PolkitUnixUser *user;
user = POLKIT_UNIX_USER (polkit_unix_user_new (0));
polkit_unix_user_set_uid (user, 5);
gint user_uid = polkit_unix_user_get_uid (user);
g_assert_cmpint (user_uid, ==, 5);
g_object_unref (user);
}
int
main (int argc, char *argv[])
{
g_type_init ();
g_test_init (&argc, &argv, NULL);
g_test_add_func ("/PolkitUnixUser/new", test_new);
g_test_add_func ("/PolkitUnixUser/new_for_name", test_new_for_name);
g_test_add_func ("/PolkitUnixUser/set_uid", test_set_uid);
return g_test_run ();
}
NULL =
INCLUDES = \
-I$(top_builddir)/src \
-I$(top_builddir)/test \
-I$(top_srcdir)/src \
-DPACKAGE_LIBEXEC_DIR=\""$(libexecdir)"\" \
-DPACKAGE_SYSCONF_DIR=\""$(sysconfdir)"\" \
-DPACKAGE_DATA_DIR=\""$(datadir)"\" \
-DPACKAGE_BIN_DIR=\""$(bindir)"\" \
-DPACKAGE_LOCALSTATE_DIR=\""$(localstatedir)"\" \
-DPACKAGE_LOCALE_DIR=\""$(localedir)"\" \
-DPACKAGE_LIB_DIR=\""$(libdir)"\" \
-D_POSIX_PTHREAD_SEMANTICS \
-D_REENTRANT \
$(NULL)
AM_CFLAGS = \
-D_POLKIT_COMPILATION \
-D_POLKIT_BACKEND_COMPILATION \
$(GLIB_CFLAGS) \
$(NULL)
LDADD = \
$(GLIB_LIBS) \
$(top_builddir)/src/polkit/libpolkit-gobject-1.la \
$(top_builddir)/src/polkitbackend/libpolkit-backend-1.la\
$(top_builddir)/test/libpolkit-test-helper.la \
$(NULL)
TEST_PROGS =
# ----------------------------------------------------------------------------------------------------
TEST_PROGS += polkitbackendlocalauthorizationstoretest
polkitbackendlocalauthorizationstoretest_SOURCES = polkitbackendlocalauthorizationstoretest.c
TEST_PROGS += polkitbackendlocalauthoritytest
polkitbackendlocalauthoritytest_SOURCES = polkitbackendlocalauthoritytest.c
# ----------------------------------------------------------------------------------------------------
check_PROGRAMS = $(TEST_PROGS)
TESTS = $(TEST_PROGS)
clean-local :
rm -f *~
[Normal Staff Permissions]
Identity=unix-group:users;unix-user:root
Action=com.example.awesomeproduct.*
ResultAny=no
ResultInactive=auth_self
ResultActive=yes
[Super Secret Project Permissions]
Identity=unix-user:root
Action=com.example.restrictedproduct.*
ResultAny=no
ResultInactive=no
ResultActive=auth_self
/*
* Copyright (C) 2011 Google Inc.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General
* Public License along with this library; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place, Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Nikki VonHollen <vonhollen@google.com>
*/
#include "glib.h"
#include <polkittesthelper.h>
#include <polkit/polkit.h>
#include <polkitbackend/polkitbackendlocalauthority.h>
#define TEST_CONFIG_PATH "./data/config"
#define TEST_AUTH_PATH1 "./data/authstore1"
#define TEST_AUTH_PATH2 "./data/authstore2"
/* Test helper types */
struct auth_context {
const gchar *identity;
gboolean subject_is_local;
gboolean subject_is_active;
const gchar *action_id;
PolkitImplicitAuthorization implicit;
PolkitImplicitAuthorization expect;
};
static PolkitBackendLocalAuthority *create_authority (void);
/* Test implementations */
static void
test_check_authorization_sync (const void *_ctx)
{
const struct auth_context *ctx = (const struct auth_context *) _ctx;
PolkitBackendLocalAuthority *authority = create_authority ();
PolkitSubject *caller = polkit_unix_session_new ("caller-session");
g_assert (caller);
PolkitSubject *subject = polkit_unix_session_new ("subject-session");;
g_assert (subject);
GError *error = NULL;
PolkitIdentity *user_for_subject = polkit_identity_from_string (ctx->identity, &error);
g_assert_no_error (error);
g_assert (user_for_subject);