summaryrefslogtreecommitdiff
path: root/pam
diff options
context:
space:
mode:
authorStefan Walter <stefw@src.gnome.org>2007-11-29 21:43:31 +0000
committerStefan Walter <stefw@src.gnome.org>2007-11-29 21:43:31 +0000
commit4f3c57f483e5186613b0589e8f29eb5b389a01ee (patch)
treeaaad106150331b37d3d0c29ee80d37f6271760f2 /pam
parent5c31349516280c9489b0408ff8f21866dd615c4a (diff)
downloadgnome-keyring-4f3c57f483e5186613b0589e8f29eb5b389a01ee.tar.gz
Cleanup and organize tests to be with the components that they test.
* common/Makefile.am: * common/tests/*: * keyrings/tests/*: * library/Makefile.am: * library/tests/*: * pam/Makefile.am: * pam/tests/*: * pkix/tests/*: * tests/*: * ui/gkr-ask-daemon.c: * ui/gkr-ask-daemon.h: * configure.in: Cleanup and organize tests to be with the components that they test. svn path=/trunk/; revision=910
Diffstat (limited to 'pam')
-rw-r--r--pam/Makefile.am7
-rw-r--r--pam/tests/Makefile.am13
-rw-r--r--pam/tests/unit-test-pam-setup.c128
-rw-r--r--pam/tests/unit-test-pam.c85
4 files changed, 233 insertions, 0 deletions
diff --git a/pam/Makefile.am b/pam/Makefile.am
index 565415d4..560a77f9 100644
--- a/pam/Makefile.am
+++ b/pam/Makefile.am
@@ -23,3 +23,10 @@ pam_gnome_keyring_la_LDFLAGS = \
install-pam: install
@echo "WARNING: install-pam is no longer used, use the --with-pam-dir configure option instead"
+if WITH_TESTS
+TESTS_DIR = tests
+else
+TESTS_DIR =
+endif
+
+SUBDIRS = . $(TESTS_DIR)
diff --git a/pam/tests/Makefile.am b/pam/tests/Makefile.am
new file mode 100644
index 00000000..889036aa
--- /dev/null
+++ b/pam/tests/Makefile.am
@@ -0,0 +1,13 @@
+
+UNIT_AUTO = unit-test-pam-setup.c \
+ unit-test-pam.c
+
+UNIT_PROMPT =
+
+UNIT_LIBS = \
+ -lpam
+
+UNIT_FLAGS = \
+ -DEXTERNAL_TEST
+
+include $(top_srcdir)/tests/test.make
diff --git a/pam/tests/unit-test-pam-setup.c b/pam/tests/unit-test-pam-setup.c
new file mode 100644
index 00000000..0be4c30a
--- /dev/null
+++ b/pam/tests/unit-test-pam-setup.c
@@ -0,0 +1,128 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* unit-test-pam-setup.c: Setup for PAM tests
+
+ Copyright (C) 2007 Stefan Walter
+
+ The Gnome Keyring Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The Gnome Keyring 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
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the Gnome Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+
+ Author: Stef Walter <stef@memberwebs.com>
+*/
+
+#include <sys/types.h>
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <signal.h>
+#include <unistd.h>
+
+#include "run-auto-test.h"
+
+#include <security/pam_appl.h>
+
+/*
+ * Each test looks like (on one line):
+ * void unit_test_xxxxx (CuTest* cu)
+ *
+ * Each setup looks like (on one line):
+ * void unit_setup_xxxxx (void);
+ *
+ * Each teardown looks like (on one line):
+ * void unit_teardown_xxxxx (void);
+ *
+ * Tests be run in the order specified here.
+ */
+
+/* Used directly by the other tests */
+pam_handle_t *test_pamh = NULL;
+
+static int
+conv_func (int n, const struct pam_message **msg,
+ struct pam_response **resp, void *arg)
+{
+ struct pam_response *aresp;
+ int i;
+
+ g_assert (n > 0 && n < PAM_MAX_NUM_MSG);
+ aresp = g_new0(struct pam_response, n);
+
+ for (i = 0; i < n; ++i) {
+ aresp[i].resp_retcode = 0;
+ aresp[i].resp = NULL;
+ switch (msg[i]->msg_style) {
+ case PAM_PROMPT_ECHO_OFF:
+ aresp[i].resp = getpass (msg[i]->msg);
+ g_assert (aresp[i].resp != NULL);
+ break;
+ case PAM_PROMPT_ECHO_ON:
+ aresp[i].resp = getpass (msg[i]->msg);
+ g_assert (aresp[i].resp != NULL);
+ break;
+ case PAM_ERROR_MSG:
+ fputs(msg[i]->msg, stderr);
+ if (strlen(msg[i]->msg) > 0 &&
+ msg[i]->msg[strlen(msg[i]->msg) - 1] != '\n')
+ fputc('\n', stderr);
+ break;
+ case PAM_TEXT_INFO:
+ fputs(msg[i]->msg, stdout);
+ if (strlen(msg[i]->msg) > 0 &&
+ msg[i]->msg[strlen(msg[i]->msg) - 1] != '\n')
+ fputc('\n', stdout);
+ break;
+ default:
+ return PAM_CONV_ERR;
+ }
+ }
+ *resp = aresp;
+ return PAM_SUCCESS;
+}
+
+struct pam_conv conv = { conv_func, NULL };
+
+void unit_setup_pam (void)
+{
+ char user[1024];
+ int ret;
+
+ printf ("Make sure the PAM module is installed by doing:\n"
+ "# make install-pam\n"
+ "\n"
+ "Then make /etc/pam.d/testgkr contains:\n"
+ "\n"
+ "auth required pam_unix.so nullok_secure\n"
+ "auth optional pam_gnome_keyring.so try_first_pass\n"
+ "session required pam_unix.so\n"
+ "session optional pam_gnome_keyring.so\n"
+ "\n");
+ sleep (1);
+
+ printf ("User: ");
+ fgets (user, sizeof (user), stdin);
+
+ g_strstrip (user);
+
+ ret = pam_start ("testgkr", user[0] ? user : g_get_user_name (), &conv, &test_pamh);
+ if (ret != PAM_SUCCESS)
+ g_error ("couldn't initialize pam");
+
+ g_assert (test_pamh);
+}
+
+void unit_teardown_pam (void)
+{
+ g_assert (test_pamh);
+ pam_end (test_pamh, PAM_SUCCESS);
+}
diff --git a/pam/tests/unit-test-pam.c b/pam/tests/unit-test-pam.c
new file mode 100644
index 00000000..21e2075d
--- /dev/null
+++ b/pam/tests/unit-test-pam.c
@@ -0,0 +1,85 @@
+/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
+/* unit-test-pam.c: Test PAM module
+
+ Copyright (C) 2007 Stefan Walter
+
+ The Gnome Keyring Library is free software; you can redistribute it and/or
+ modify it under the terms of the GNU Library General Public License as
+ published by the Free Software Foundation; either version 2 of the
+ License, or (at your option) any later version.
+
+ The Gnome Keyring 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
+ Library General Public License for more details.
+
+ You should have received a copy of the GNU Library General Public
+ License along with the Gnome Library; see the file COPYING.LIB. If not,
+ write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
+ Boston, MA 02111-1307, USA.
+
+ Author: Stef Walter <stef@memberwebs.com>
+*/
+
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <unistd.h>
+
+#include "run-auto-test.h"
+
+#include <security/pam_appl.h>
+
+/*
+ * Each test looks like (on one line):
+ * void unit_test_xxxxx (CuTest* cu)
+ *
+ * Each setup looks like (on one line):
+ * void unit_setup_xxxxx (void);
+ *
+ * Each teardown looks like (on one line):
+ * void unit_teardown_xxxxx (void);
+ *
+ * Tests be run in the order specified here.
+ */
+
+extern pam_handle_t *test_pamh;
+
+void unit_test_pam_open (CuTest* cu)
+{
+ char** pam_env;
+
+ /* Clear out this environment variable so we force a new daemon */
+ putenv("GNOME_KEYRING_SOCKET=");
+
+ int ret = pam_authenticate (test_pamh, 0);
+ if (ret != PAM_SUCCESS)
+ g_printerr ("Bad user/password?\n\n");
+ CuAssertIntEquals (cu, PAM_SUCCESS, ret);
+
+ pam_env = pam_getenvlist (test_pamh);
+ while (*pam_env)
+ putenv ((char*)*(pam_env++));
+
+ ret = pam_open_session (test_pamh, 0);
+ CuAssertIntEquals (cu, PAM_SUCCESS, ret);
+}
+
+void unit_test_pam_env (CuTest* cu)
+{
+ const char *socket;
+
+
+ socket = g_getenv ("GNOME_KEYRING_SOCKET");
+ CuAssert (cu, "socket should have been setup", socket && socket[0]);
+ CuAssert (cu, "socket should have been created", g_file_test (socket, G_FILE_TEST_EXISTS));
+
+ g_printerr ("GNOME_KEYRING_SOCKET is: %s\n", g_getenv ("GNOME_KEYRING_SOCKET"));
+ sleep (3);
+}
+
+void unit_test_pam_close (CuTest* cu)
+{
+ int ret = pam_close_session (test_pamh, 0);
+ CuAssertIntEquals (cu, PAM_SUCCESS, ret);
+}