From a9fa72df89525dd0083e0e02f80ec7e6e9a89e20 Mon Sep 17 00:00:00 2001 From: Alberto Ruiz Date: Fri, 9 Oct 2015 13:11:53 +0100 Subject: pam: Add test suite for the pam module --- pam/Makefile.am | 5 ++ tests/Makefile.am | 14 ++++ tests/pam-mock.c | 56 +++++++++++++++ tests/pam-mock.h | 47 ++++++++++++ tests/pam.c | 83 ++++++++++++++++++++++ .../pamprofile/dconf/profile/pamdconftest.profile | 0 6 files changed, 205 insertions(+) create mode 100644 tests/pam-mock.c create mode 100644 tests/pam-mock.h create mode 100644 tests/pam.c create mode 100644 tests/pamprofile/dconf/profile/pamdconftest.profile diff --git a/pam/Makefile.am b/pam/Makefile.am index 01d2097..6a64b05 100644 --- a/pam/Makefile.am +++ b/pam/Makefile.am @@ -1,7 +1,12 @@ pamlibdir = $(PAM_DEST_DIR) pamlib_PROGRAMS = pam_dconf.so +noinst_LIBRARIES = pam_dconf.a + pam_dconf_so_SOURCES = \ pam_dconf.c \ pam_dconf.h pam_dconf_so_CFLAGS = -fPIC -DPIC -shared -Wall + +pam_dconf_a_CFLAGS = -Wall +pam_dconf_a_SOURCES = $(pam_dconf_so_SOURCES) diff --git a/tests/Makefile.am b/tests/Makefile.am index adc8519..ec09941 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -102,3 +102,17 @@ client_LDADD = \ libdconf-mock.a \ $(gio_LIBS) client_SOURCES = client.c + +PAM_TEST_USERNAME = pamdconftest +TEST_PROGS += pam +pam_CFLAGS = $(glib_CFLAGS) $(gio_CFLAGS) -DSRCDIR=\"$(abs_srcdir)\" -DUSERNAME=\"$(PAM_TEST_USERNAME)\" +pam_SOURCES = \ + pam.c \ + pam-mock.c \ + pam-mock.h +pam_LDADD = \ + ../pam/pam_dconf.a \ + $(glib_LIBS) \ + $(gio_LIBS) +EXTRA_DIST += \ + pamprofile/dconf/profile/$(PAM_TEST_USERNAME).profile diff --git a/tests/pam-mock.c b/tests/pam-mock.c new file mode 100644 index 0000000..343f75d --- /dev/null +++ b/tests/pam-mock.c @@ -0,0 +1,56 @@ +#include +#include +#include +#include "pam-mock.h" + +static char *xdg_data_dirs = 0; +static char *xdg_runtime_dir = 0; + +void +pam_syslog (pam_handle_t *pamh, + int priority, + const char *fmt, + ...) +{ + /* We keep this around in case we do want to output the logging info */ + va_list vl; + va_start (vl, fmt); + vprintf (fmt, vl); + va_end (vl); + printf("\n"); + return; +} + +int +pam_get_user (pam_handle_t *pamh, + const char **user, + const char *prompt) +{ + *user = USERNAME; + return PAM_SUCCESS; +} + +const char* +pam_getenv (pam_handle_t *pamh, + const char *name) +{ + if (strcmp (name, "XDG_DATA_DIRS") == 0) + return xdg_data_dirs; + + if (strcmp (name, "XDG_RUNTIME_DIR") == 0) + return xdg_runtime_dir; + + return NULL; +} + +void +pam_mock_set_xdg_data_dirs (char * data_dirs) +{ + xdg_data_dirs = data_dirs; +} + +void +pam_mock_set_xdg_runtime_dir (char * runtime_dir) +{ + xdg_runtime_dir = runtime_dir; +} diff --git a/tests/pam-mock.h b/tests/pam-mock.h new file mode 100644 index 0000000..67dfe3c --- /dev/null +++ b/tests/pam-mock.h @@ -0,0 +1,47 @@ +#include +#include + +typedef int pam_handle_t; + +#define LOG_ERR 0 +#define LOG_NOTICE 0 +#define LOG_DEBUG 0 +#define PAM_SUCCESS 0 + +#define PAM_EXTERN + +PAM_EXTERN int +pam_sm_open_session (pam_handle_t *pamh, + int flags, + int argc, + const char **argv); + + +PAM_EXTERN int +pam_sm_close_session (pam_handle_t *pamh, + int flags, + int argc, + const char **argv); + + +void +pam_syslog (pam_handle_t *pamh, + int priority, + const char *fmt, + ...); + +int +pam_get_user (pam_handle_t *pamh, + const char **user, + const char *prompt); +const char* +pam_getenv (pam_handle_t *pamh, + const char *name); +void +pam_mock_set_xdg_data_dirs (char * data_dirs); + +void +pam_mock_set_xdg_runtime_dir (char * runtime_dir); + +void +pam_mock_set_user (char * user); diff --git a/tests/pam.c b/tests/pam.c new file mode 100644 index 0000000..0148477 --- /dev/null +++ b/tests/pam.c @@ -0,0 +1,83 @@ +#include +#include +#include +#include + +static gchar * +setup_profile (void) +{ + gchar *tmpdir; + + tmpdir = g_dir_make_tmp ("pam_dconf_test.XXXXXX", NULL); + + if (tmpdir == NULL) + return NULL; + + pam_mock_set_xdg_data_dirs (SRCDIR "/pamprofile"); + pam_mock_set_xdg_runtime_dir (tmpdir); + + return tmpdir; +} + +static void +teardown_profile (gchar *tmpdir) +{ + gchar *profile_link; + + profile_link = g_strconcat (tmpdir, "/dconf.profile", NULL); + + g_remove (profile_link); + g_remove (tmpdir); + + pam_mock_set_xdg_runtime_dir (NULL); + pam_mock_set_xdg_data_dirs (NULL); + + g_free (profile_link); +} + +static void +test_open_session (void) +{ + gchar *tmpdir; + gchar *profile_link; + GFile *link; + GFileInfo *info; + + tmpdir = setup_profile (); + g_assert (tmpdir != NULL); + + pam_sm_open_session (NULL, 0, 0, NULL); + + profile_link = g_strconcat (tmpdir, "/dconf.profile", NULL); + link = g_file_new_for_path (profile_link); + + /* Check if file object was created and exists */ + g_assert (link != NULL); + g_assert (g_file_query_exists (link, NULL)); + + info = g_file_query_info (link, + "standard::*", + G_FILE_QUERY_INFO_NOFOLLOW_SYMLINKS, + NULL, NULL); + /* Check if symlink exists and points to the right place */ + g_assert (info != NULL); + g_assert (g_file_info_get_is_symlink (info)); + g_assert (g_strcmp0 (g_file_info_get_symlink_target (info), + SRCDIR "/pamprofile/dconf/profile/" USERNAME ".profile") == 0); + + teardown_profile (tmpdir); + g_object_unref (info); + g_object_unref (link); + g_free (tmpdir); + g_free (profile_link); +} + +int +main (int argc, char** argv) +{ + g_test_init (&argc, &argv, NULL); + + g_test_add_func ("/pam/open_session", test_open_session); + + return g_test_run (); +} diff --git a/tests/pamprofile/dconf/profile/pamdconftest.profile b/tests/pamprofile/dconf/profile/pamdconftest.profile new file mode 100644 index 0000000..e69de29 -- cgit v1.2.1