From da0aba21779529d98436e42323fc12f702390969 Mon Sep 17 00:00:00 2001 From: Russ Allbery Date: Fri, 11 Jul 2014 20:18:41 -0700 Subject: Update to rra-c-util 5.5 and C TAP Harness 3.1 Update to rra-c-util 5.5: * Use Lancaster Consensus environment variables to control tests. * Use calloc or reallocarray for protection against integer overflows. * Suppress warnings from Kerberos headers in non-system paths. * Assume calloc initializes pointers to NULL. * Assume free(NULL) is properly ignored. * Improve error handling in xasprintf and xvasprintf. * Check the return status of snprintf and vsnprintf properly. * Preserve errno if snprintf fails in vasprintf replacement. Update to C TAP Harness 3.1: * Reopen standard input to /dev/null when running a test list. * Don't leak extraneous file descriptors to tests. * Suppress lazy plans and test summaries if the test failed with bail. * runtests now treats the command line as a list of tests by default. * The full test executable path can now be passed to runtests -o. * Improved harness output for tests with lazy plans. * Improved harness output to a terminal for some abort cases. * Flush harness output after each test even when not on a terminal. Change-Id: I05161eb3d3be49a98f7762e876cb114da0c84e9a Reviewed-on: https://gerrit.stanford.edu/1529 Reviewed-by: Russ Allbery Tested-by: Russ Allbery --- tests/tap/kerberos.c | 105 +++++++++++++++++++++++++++++++-------------------- 1 file changed, 64 insertions(+), 41 deletions(-) (limited to 'tests/tap/kerberos.c') diff --git a/tests/tap/kerberos.c b/tests/tap/kerberos.c index 6f593f8..578a858 100644 --- a/tests/tap/kerberos.c +++ b/tests/tap/kerberos.c @@ -15,7 +15,7 @@ * which can be found at . * * Written by Russ Allbery - * Copyright 2006, 2007, 2009, 2010, 2011, 2012 + * 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 -#ifdef HAVE_KERBEROS +#ifdef HAVE_KRB5 # include #endif #include @@ -47,6 +47,7 @@ #include #include +#include #include #include @@ -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; } @@ -244,6 +235,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 @@ -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 */ -- cgit v1.2.3