diff --git a/Makefile.am b/Makefile.am index 13f641f..70d072c 100644 --- a/Makefile.am +++ b/Makefile.am @@ -1,6 +1,6 @@ ## Process this file with automake to produce Makefile.in -SUBDIRS = actions data src docs po +SUBDIRS = actions data src docs po test NULL = diff --git a/configure.ac b/configure.ac index a1650ef..89f48ca 100644 --- a/configure.ac +++ b/configure.ac @@ -419,6 +419,9 @@ docs/Makefile docs/polkit/Makefile docs/man/Makefile po/Makefile.in +test/Makefile +test/polkit/Makefile +test/polkitbackend/Makefile ]) dnl ========================================================================== diff --git a/src/polkitbackend/polkitbackendlocalauthority.c b/src/polkitbackend/polkitbackendlocalauthority.c index dc6b36c..0f3cd65 100644 --- a/src/polkitbackend/polkitbackendlocalauthority.c +++ b/src/polkitbackend/polkitbackendlocalauthority.c @@ -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); } @@ -372,23 +391,66 @@ polkit_backend_local_authority_get_features (PolkitBackendAuthority *authority) } 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)); } diff --git a/test/Makefile.am b/test/Makefile.am new file mode 100644 index 0000000..9927eab --- /dev/null +++ b/test/Makefile.am @@ -0,0 +1,11 @@ + +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 *~ diff --git a/test/polkit/Makefile.am b/test/polkit/Makefile.am new file mode 100644 index 0000000..70fbf67 --- /dev/null +++ b/test/polkit/Makefile.am @@ -0,0 +1,46 @@ + +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 *~ diff --git a/test/polkit/polkitidentitytest.c b/test/polkit/polkitidentitytest.c new file mode 100644 index 0000000..edbc765 --- /dev/null +++ b/test/polkit/polkitidentitytest.c @@ -0,0 +1,148 @@ +/* + * 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 + */ + +#include "glib.h" +#include + + +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 (); +} diff --git a/test/polkit/polkitunixgrouptest.c b/test/polkit/polkitunixgrouptest.c new file mode 100644 index 0000000..f1417b3 --- /dev/null +++ b/test/polkit/polkitunixgrouptest.c @@ -0,0 +1,82 @@ +/* + * 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 + */ + +#include "glib.h" +#include + + +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 (); +} diff --git a/test/polkit/polkitunixusertest.c b/test/polkit/polkitunixusertest.c new file mode 100644 index 0000000..1ad0a65 --- /dev/null +++ b/test/polkit/polkitunixusertest.c @@ -0,0 +1,82 @@ +/* + * 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 + */ + +#include "glib.h" +#include + + +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 (); +} diff --git a/test/polkitbackend/Makefile.am b/test/polkitbackend/Makefile.am new file mode 100644 index 0000000..8067232 --- /dev/null +++ b/test/polkitbackend/Makefile.am @@ -0,0 +1,48 @@ + +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 *~ diff --git a/test/polkitbackend/data/authstore1/10-test/com.example.pkla b/test/polkitbackend/data/authstore1/10-test/com.example.pkla new file mode 100644 index 0000000..e716465 --- /dev/null +++ b/test/polkitbackend/data/authstore1/10-test/com.example.pkla @@ -0,0 +1,6 @@ +[Normal Staff Permissions] +Identity=unix-group:users;unix-user:root +Action=com.example.awesomeproduct.* +ResultAny=no +ResultInactive=auth_self +ResultActive=yes diff --git a/test/polkitbackend/data/authstore2/10-test/com.example.pkla b/test/polkitbackend/data/authstore2/10-test/com.example.pkla new file mode 100644 index 0000000..f013c5b --- /dev/null +++ b/test/polkitbackend/data/authstore2/10-test/com.example.pkla @@ -0,0 +1,6 @@ +[Super Secret Project Permissions] +Identity=unix-user:root +Action=com.example.restrictedproduct.* +ResultAny=no +ResultInactive=no +ResultActive=auth_self diff --git a/test/polkitbackend/polkitbackendlocalauthoritytest.c b/test/polkitbackend/polkitbackendlocalauthoritytest.c new file mode 100644 index 0000000..f76ea41 --- /dev/null +++ b/test/polkitbackend/polkitbackendlocalauthoritytest.c @@ -0,0 +1,158 @@ +/* + * 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 + */ + +#include "glib.h" + +#include +#include +#include + +#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); + + PolkitDetails *details = polkit_details_new (); + g_assert (details); + + PolkitDetails *out_details = polkit_details_new (); + g_assert (out_details); + + PolkitImplicitAuthorization auth; + + auth = polkit_backend_interactive_authority_check_authorization_sync ( + POLKIT_BACKEND_INTERACTIVE_AUTHORITY (authority), + caller, + subject, + user_for_subject, + ctx->subject_is_local, + ctx->subject_is_active, + ctx->action_id, + details, + ctx->implicit, + out_details); + + g_assert_cmpint (auth, ==, ctx->expect); + + g_object_unref (authority); + g_object_unref (caller); + g_object_unref (subject); + g_object_unref (user_for_subject); + g_object_unref (details); + g_object_unref (out_details); +} + + +/* Factory for mock local authority. */ +static PolkitBackendLocalAuthority * +create_authority (void) +{ + return g_object_new ( + POLKIT_BACKEND_TYPE_LOCAL_AUTHORITY, + "config-path", TEST_CONFIG_PATH, + "auth-store-paths", TEST_AUTH_PATH1 ";" TEST_AUTH_PATH2, + NULL); +} + + +/* Variations of the check_authorization_sync */ +struct auth_context check_authorization_test_data [] = { + {"unix-user:root", TRUE, TRUE, "com.example.awesomeproduct.foo", + POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN, + POLKIT_IMPLICIT_AUTHORIZATION_AUTHORIZED}, + {"unix-user:root", TRUE, FALSE, "com.example.awesomeproduct.foo", + POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN, + POLKIT_IMPLICIT_AUTHORIZATION_AUTHENTICATION_REQUIRED}, + {"unix-user:root", FALSE, FALSE, "com.example.awesomeproduct.foo", + POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN, + POLKIT_IMPLICIT_AUTHORIZATION_NOT_AUTHORIZED}, + {"unix-user:root", TRUE, TRUE, "com.example.restrictedproduct.foo", + POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN, + POLKIT_IMPLICIT_AUTHORIZATION_AUTHENTICATION_REQUIRED}, + {"unix-user:root", TRUE, TRUE, "com.example.missingproduct.foo", + POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN, + POLKIT_IMPLICIT_AUTHORIZATION_UNKNOWN}, + {NULL}, +}; + + +/* Automatically create many variations of the check_authorization_sync test */ +static void +add_check_authorization_tests (void) { + unsigned int i; + for (i = 0; check_authorization_test_data[i].identity; i++) { + struct auth_context *ctx = &check_authorization_test_data[i]; + gchar *test_name = g_strdup_printf ( + "/PolkitBackendLocalAuthority/check_authorization_sync_%d", i); + g_test_add_data_func(test_name, ctx, test_check_authorization_sync); + } +}; + + +int +main (int argc, char *argv[]) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + polkit_test_redirect_logs (); + + // Register extension point only once. Required to create authority. + GIOExtensionPoint *ep = g_io_extension_point_register ( + POLKIT_BACKEND_AUTHORITY_EXTENSION_POINT_NAME); + g_io_extension_point_set_required_type (ep, + POLKIT_BACKEND_TYPE_AUTHORITY); + + add_check_authorization_tests (); + return g_test_run (); +}; diff --git a/test/polkitbackend/polkitbackendlocalauthorizationstoretest.c b/test/polkitbackend/polkitbackendlocalauthorizationstoretest.c new file mode 100644 index 0000000..617acf9 --- /dev/null +++ b/test/polkitbackend/polkitbackendlocalauthorizationstoretest.c @@ -0,0 +1,128 @@ +/* + * 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 + */ + +#include "glib.h" + +#include +#include +#include + +#define DATA_DIR "./data/authstore1/10-test" +#define DATA_EXT ".pkla" + +static void +test_new (void) +{ + PolkitBackendLocalAuthorizationStore *store; + GFile *data_dir; + + data_dir = g_file_new_for_path (DATA_DIR); + + store = polkit_backend_local_authorization_store_new (data_dir, DATA_EXT); + g_assert (store); +} + + +static void +test_lookup (void) +{ + GFile *data_dir; + PolkitBackendLocalAuthorizationStore *store; + GError *error = NULL; + PolkitIdentity *identity; + gboolean ok; + PolkitImplicitAuthorization ret_any; + PolkitImplicitAuthorization ret_inactive; + PolkitImplicitAuthorization ret_active; + PolkitDetails *details; + + // Create the auth store + data_dir = g_file_new_for_path (DATA_DIR); + store = polkit_backend_local_authorization_store_new (data_dir, DATA_EXT); + g_assert (store); + + // We don't care about details + details = polkit_details_new (); + + // Create an identity to query with + identity = polkit_identity_from_string("unix-group:users", &error); + g_assert (identity); + g_assert_no_error (error); + + // Lookup an exisiting record + ok = polkit_backend_local_authorization_store_lookup ( + store, + identity, + "com.example.awesomeproduct.dofoo", + details, + &ret_any, + &ret_inactive, + &ret_active, + NULL); + g_assert (ok); + g_assert_cmpstr ("no", ==, polkit_implicit_authorization_to_string (ret_any)); + g_assert_cmpstr ("auth_self", ==, polkit_implicit_authorization_to_string (ret_inactive)); + g_assert_cmpstr ("yes", ==, polkit_implicit_authorization_to_string (ret_active)); + + // Create another identity to query with + identity = polkit_identity_from_string("unix-user:root", &error); + g_assert (identity); + g_assert_no_error (error); + + // Lookup another exisiting record + ok = polkit_backend_local_authorization_store_lookup ( + store, + identity, + "com.example.awesomeproduct.dofoo", + details, + &ret_any, + &ret_inactive, + &ret_active, + NULL); + g_assert (ok); + g_assert_cmpstr ("no", ==, polkit_implicit_authorization_to_string (ret_any)); + g_assert_cmpstr ("auth_self", ==, polkit_implicit_authorization_to_string (ret_inactive)); + g_assert_cmpstr ("yes", ==, polkit_implicit_authorization_to_string (ret_active)); + + // Lookup a missing record + ok = polkit_backend_local_authorization_store_lookup ( + store, + identity, + "com.example.restrictedproduct.dobar", + details, + &ret_any, + &ret_inactive, + &ret_active, + NULL); + g_assert (!ok); +} + + +int +main (int argc, char *argv[]) +{ + g_type_init (); + g_test_init (&argc, &argv, NULL); + polkit_test_redirect_logs (); + g_test_add_func ("/PolkitBackendLocalAuthorizationStore/new", test_new); + g_test_add_func ("/PolkitBackendLocalAuthorizationStore/lookup", test_lookup); + return g_test_run (); +} diff --git a/test/polkittesthelper.c b/test/polkittesthelper.c new file mode 100644 index 0000000..5373bdc --- /dev/null +++ b/test/polkittesthelper.c @@ -0,0 +1,48 @@ +/* + * 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 + */ + +#include "polkittesthelper.h" + +/* TODO: Log handling with unit tests is horrible. Figure out a way to always + * show logs, without munging up test output. For now, we hide them + * unless --verbose is used with g_test_message(...). + */ + +void +polkit_test_log_handler (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data) +{ + g_test_message("%s", message); +} + +/** + * Send all future log messages to g_test_message(...). + * + * Logs will only be shown when test programs are run with --verbose. + */ +void +polkit_test_redirect_logs (void) +{ + g_log_set_default_handler (polkit_test_log_handler, NULL); +} + diff --git a/test/polkittesthelper.h b/test/polkittesthelper.h new file mode 100644 index 0000000..c8ce161 --- /dev/null +++ b/test/polkittesthelper.h @@ -0,0 +1,34 @@ +/* + * 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 + */ + +#ifndef POLKIT_TEST_HELPER_H_ +#define POLKIT_TEST_HELPER_H_ + +#include "glib.h" + +void polkit_test_log_handler (const gchar *log_domain, + GLogLevelFlags log_level, + const gchar *message, + gpointer user_data); + +void polkit_test_redirect_logs (void); + +#endif