diff options
author | Nikki VonHollen <vonhollen@google.com> | 2011-12-20 11:10:17 -0500 |
---|---|---|
committer | David Zeuthen <davidz@redhat.com> | 2011-12-20 11:26:55 -0500 |
commit | 15d2e90a54009efb31300d8b59292d71ce98a5b2 (patch) | |
tree | 5c24ef72ded63d59de2b8986e79f96b44fde66c8 /test | |
parent | 2675cc0d82c3cf1541bbf6e2e4120d7a85a970e6 (diff) |
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: David Zeuthen <davidz@redhat.com>
Diffstat (limited to 'test')
-rw-r--r-- | test/Makefile.am | 11 | ||||
-rw-r--r-- | test/polkit/Makefile.am | 46 | ||||
-rw-r--r-- | test/polkit/polkitidentitytest.c | 148 | ||||
-rw-r--r-- | test/polkit/polkitunixgrouptest.c | 82 | ||||
-rw-r--r-- | test/polkit/polkitunixusertest.c | 82 | ||||
-rw-r--r-- | test/polkitbackend/Makefile.am | 48 | ||||
-rw-r--r-- | test/polkitbackend/data/authstore1/10-test/com.example.pkla | 6 | ||||
-rw-r--r-- | test/polkitbackend/data/authstore2/10-test/com.example.pkla | 6 | ||||
-rw-r--r-- | test/polkitbackend/polkitbackendlocalauthoritytest.c | 158 | ||||
-rw-r--r-- | test/polkitbackend/polkitbackendlocalauthorizationstoretest.c | 128 | ||||
-rw-r--r-- | test/polkittesthelper.c | 48 | ||||
-rw-r--r-- | test/polkittesthelper.h | 34 |
12 files changed, 797 insertions, 0 deletions
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 <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 (); +} 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 <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 (); +} 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 <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 (); +} 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 <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); + + 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 <vonhollen@google.com> + */ + +#include "glib.h" + +#include <polkittesthelper.h> +#include <polkit/polkit.h> +#include <polkitbackend/polkitbackendlocalauthorizationstore.h> + +#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 <vonhollen@google.com> + */ + +#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 <vonhollen@google.com> + */ + +#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 |