summaryrefslogtreecommitdiff
path: root/tests/tap/kerberos.c
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tap/kerberos.c')
-rw-r--r--tests/tap/kerberos.c107
1 files changed, 65 insertions, 42 deletions
diff --git a/tests/tap/kerberos.c b/tests/tap/kerberos.c
index 474cf4f..578a858 100644
--- a/tests/tap/kerberos.c
+++ b/tests/tap/kerberos.c
@@ -14,8 +14,8 @@
* The canonical version of this file is maintained in the rra-c-util package,
* which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
*
- * Written by Russ Allbery <rra@stanford.edu>
- * Copyright 2006, 2007, 2009, 2010, 2011, 2012
+ * Written by Russ Allbery <eagle@eyrie.org>
+ * Copyright 2006, 2007, 2009, 2010, 2011, 2012, 2013, 2014
* The Board of Trustees of the Leland Stanford Junior University
*
* Permission is hereby granted, free of charge, to any person obtaining a
@@ -38,7 +38,7 @@
*/
#include <config.h>
-#ifdef HAVE_KERBEROS
+#ifdef HAVE_KRB5
# include <portable/krb5.h>
#endif
#include <portable/system.h>
@@ -47,6 +47,7 @@
#include <tests/tap/basic.h>
#include <tests/tap/kerberos.h>
+#include <tests/tap/macros.h>
#include <tests/tap/process.h>
#include <tests/tap/string.h>
@@ -79,7 +80,7 @@ static char *tmpdir_conf = NULL;
* Kerberos libraries available and one if we don't. Uses keytab to obtain
* credentials, and fills in the cache member of the provided config struct.
*/
-#ifdef HAVE_KERBEROS
+#ifdef HAVE_KRB5
static void
kerberos_kinit(void)
@@ -147,7 +148,7 @@ kerberos_kinit(void)
free(krbtgt);
}
-#else /* !HAVE_KERBEROS */
+#else /* !HAVE_KRB5 */
static void
kerberos_kinit(void)
@@ -197,37 +198,27 @@ kerberos_kinit(void)
bail("cannot get Kerberos tickets");
}
-#endif /* !HAVE_KERBEROS */
+#endif /* !HAVE_KRB5 */
/*
- * Clean up at the end of a test. This removes the ticket cache and resets
- * and frees the memory allocated for the environment variables so that
- * valgrind output on test suites is cleaner.
+ * Free all the memory associated with our Kerberos setup, but don't remove
+ * the ticket cache. This is used when cleaning up on exit from a non-primary
+ * process so that test programs that fork don't remove the ticket cache still
+ * used by the main program.
*/
-void
-kerberos_cleanup(void)
+static void
+kerberos_free(void)
{
- char *path;
-
- if (tmpdir_ticket != NULL) {
- basprintf(&path, "%s/krb5cc_test", tmpdir_ticket);
- unlink(path);
- free(path);
- test_tmpdir_free(tmpdir_ticket);
- tmpdir_ticket = NULL;
- }
+ test_tmpdir_free(tmpdir_ticket);
+ tmpdir_ticket = NULL;
if (config != NULL) {
- if (config->keytab != NULL) {
- test_file_path_free(config->keytab);
- free(config->principal);
- free(config->cache);
- }
- if (config->userprinc != NULL) {
- free(config->userprinc);
- free(config->username);
- free(config->password);
- }
+ test_file_path_free(config->keytab);
+ free(config->principal);
+ free(config->cache);
+ free(config->userprinc);
+ free(config->username);
+ free(config->password);
free(config);
config = NULL;
}
@@ -245,6 +236,42 @@ kerberos_cleanup(void)
/*
+ * Clean up at the end of a test. This removes the ticket cache and resets
+ * and frees the memory allocated for the environment variables so that
+ * valgrind output on test suites is cleaner. Most of the work is done by
+ * kerberos_free, but this function also deletes the ticket cache.
+ */
+void
+kerberos_cleanup(void)
+{
+ char *path;
+
+ if (tmpdir_ticket != NULL) {
+ basprintf(&path, "%s/krb5cc_test", tmpdir_ticket);
+ unlink(path);
+ free(path);
+ }
+ kerberos_free();
+}
+
+
+/*
+ * The cleanup handler for the TAP framework. Call kerberos_cleanup if we're
+ * in the primary process and kerberos_free if not. The first argument, which
+ * indicates whether the test succeeded or not, is ignored, since we need to
+ * do the same thing either way.
+ */
+static void
+kerberos_cleanup_handler(int success UNUSED, int primary)
+{
+ if (primary)
+ kerberos_cleanup();
+ else
+ kerberos_free();
+}
+
+
+/*
* Obtain Kerberos tickets for the principal specified in config/principal
* using the keytab specified in config/keytab, both of which are presumed to
* be in tests in either the build or the source tree. Also sets KRB5_KTNAME
@@ -321,15 +348,13 @@ kerberos_setup(enum kerberos_needs needs)
*config->realm = '\0';
config->realm++;
}
- if (path != NULL)
- test_file_path_free(path);
+ test_file_path_free(path);
/*
- * Register the cleanup function as an atexit handler so that the caller
- * doesn't have to worry about cleanup.
+ * Register the cleanup function so that the caller doesn't have to do
+ * explicit cleanup.
*/
- if (atexit(kerberos_cleanup) != 0)
- sysdiag("cannot register cleanup function");
+ test_cleanup_register(kerberos_cleanup_handler);
/* Return the configuration. */
return config;
@@ -357,10 +382,8 @@ kerberos_cleanup_conf(void)
tmpdir_conf = NULL;
}
putenv((char *) "KRB5_CONFIG=");
- if (krb5_config != NULL) {
- free(krb5_config);
- krb5_config = NULL;
- }
+ free(krb5_config);
+ krb5_config = NULL;
}
@@ -401,7 +424,7 @@ kerberos_generate_conf(const char *realm)
* The remaining functions in this file are only available if Kerberos
* libraries are available.
*/
-#ifdef HAVE_KERBEROS
+#ifdef HAVE_KRB5
/*
@@ -485,4 +508,4 @@ kerberos_keytab_principal(krb5_context ctx, const char *path)
return princ;
}
-#endif /* HAVE_KERBEROS */
+#endif /* HAVE_KRB5 */