summaryrefslogtreecommitdiff
path: root/tests/tap
diff options
context:
space:
mode:
Diffstat (limited to 'tests/tap')
-rw-r--r--tests/tap/basic.c350
-rw-r--r--tests/tap/basic.h60
-rw-r--r--tests/tap/kerberos.c120
-rw-r--r--tests/tap/kerberos.h20
-rw-r--r--tests/tap/macros.h8
-rw-r--r--tests/tap/messages.c12
-rw-r--r--tests/tap/perl/Test/RRA.pm96
-rw-r--r--tests/tap/perl/Test/RRA/Automake.pm31
-rw-r--r--tests/tap/perl/Test/RRA/Config.pm16
-rw-r--r--tests/tap/process.c352
-rw-r--r--tests/tap/process.h31
-rw-r--r--tests/tap/string.h2
12 files changed, 899 insertions, 199 deletions
diff --git a/tests/tap/basic.c b/tests/tap/basic.c
index bb51606..0b8be8f 100644
--- a/tests/tap/basic.c
+++ b/tests/tap/basic.c
@@ -12,8 +12,9 @@
* This file is part of C TAP Harness. The current version plus supporting
* documentation is at <http://www.eyrie.org/~eagle/software/c-tap-harness/>.
*
- * Copyright 2009, 2010, 2011, 2012, 2013 Russ Allbery <eagle@eyrie.org>
- * Copyright 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2011, 2012, 2013
+ * Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015
+ * Russ Allbery <eagle@eyrie.org>
+ * Copyright 2001, 2002, 2004, 2005, 2006, 2007, 2008, 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
@@ -35,7 +36,9 @@
* DEALINGS IN THE SOFTWARE.
*/
+#include <assert.h>
#include <errno.h>
+#include <limits.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
@@ -102,6 +105,21 @@ struct cleanup_func {
static struct cleanup_func *cleanup_funcs = NULL;
/*
+ * Registered diag files. Any output found in these files will be printed out
+ * as if it were passed to diag() before any other output we do. This allows
+ * background processes to log to a file and have that output interleaved with
+ * the test output.
+ */
+struct diag_file {
+ char *name;
+ FILE *file;
+ char *buffer;
+ size_t bufsize;
+ struct diag_file *next;
+};
+static struct diag_file *diag_files = NULL;
+
+/*
* Print a specified prefix and then the test description. Handles turning
* the argument list into a va_args structure suitable for passing to
* print_desc, which has to be done in a macro. Assumes that format is the
@@ -121,6 +139,126 @@ static struct cleanup_func *cleanup_funcs = NULL;
/*
+ * Form a new string by concatenating multiple strings. The arguments must be
+ * terminated by (const char *) 0.
+ *
+ * This function only exists because we can't assume asprintf. We can't
+ * simulate asprintf with snprintf because we're only assuming SUSv3, which
+ * does not require that snprintf with a NULL buffer return the required
+ * length. When those constraints are relaxed, this should be ripped out and
+ * replaced with asprintf or a more trivial replacement with snprintf.
+ */
+static char *
+concat(const char *first, ...)
+{
+ va_list args;
+ char *result;
+ const char *string;
+ size_t offset;
+ size_t length = 0;
+
+ /*
+ * Find the total memory required. Ensure we don't overflow length. See
+ * the comment for breallocarray for why we're using UINT_MAX here.
+ */
+ va_start(args, first);
+ for (string = first; string != NULL; string = va_arg(args, const char *)) {
+ if (length >= UINT_MAX - strlen(string))
+ bail("strings too long in concat");
+ length += strlen(string);
+ }
+ va_end(args);
+ length++;
+
+ /* Create the string. */
+ result = bmalloc(length);
+ va_start(args, first);
+ offset = 0;
+ for (string = first; string != NULL; string = va_arg(args, const char *)) {
+ memcpy(result + offset, string, strlen(string));
+ offset += strlen(string);
+ }
+ va_end(args);
+ result[offset] = '\0';
+ return result;
+}
+
+
+/*
+ * Check all registered diag_files for any output. We only print out the
+ * output if we see a complete line; otherwise, we wait for the next newline.
+ */
+static void
+check_diag_files(void)
+{
+ struct diag_file *file;
+ fpos_t where;
+ size_t length;
+ int size, incomplete;
+
+ /*
+ * Walk through each file and read each line of output available. The
+ * general scheme here used is as follows: try to read a line of output at
+ * a time. If we get NULL, check for EOF; on EOF, advance to the next
+ * file.
+ *
+ * If we get some data, see if it ends in a newline. If it doesn't end in
+ * a newline, we have one of two cases: our buffer isn't large enough, in
+ * which case we resize it and try again, or we have incomplete data in
+ * the file, in which case we rewind the file and will try again next
+ * time.
+ */
+ for (file = diag_files; file != NULL; file = file->next) {
+ clearerr(file->file);
+
+ /* Store the current position in case we have to rewind. */
+ if (fgetpos(file->file, &where) < 0)
+ sysbail("cannot get position in %s", file->name);
+
+ /* Continue until we get EOF or an incomplete line of data. */
+ incomplete = 0;
+ while (!feof(file->file) && !incomplete) {
+ size = file->bufsize > INT_MAX ? INT_MAX : (int) file->bufsize;
+ if (fgets(file->buffer, size, file->file) == NULL) {
+ if (ferror(file->file))
+ sysbail("cannot read from %s", file->name);
+ continue;
+ }
+
+ /*
+ * See if the line ends in a newline. If not, see which error
+ * case we have. Use UINT_MAX as a substitute for SIZE_MAX (see
+ * the comment for breallocarray).
+ */
+ length = strlen(file->buffer);
+ if (file->buffer[length - 1] != '\n') {
+ if (length < file->bufsize - 1)
+ incomplete = 1;
+ else {
+ if (file->bufsize >= UINT_MAX - BUFSIZ)
+ sysbail("line too long in %s", file->name);
+ file->bufsize += BUFSIZ;
+ file->buffer = brealloc(file->buffer, file->bufsize);
+ }
+
+ /*
+ * On either incomplete lines or too small of a buffer, rewind
+ * and read the file again (on the next pass, if incomplete).
+ * It's simpler than trying to double-buffer the file.
+ */
+ if (fsetpos(file->file, &where) < 0)
+ sysbail("cannot set position in %s", file->name);
+ continue;
+ }
+
+ /* We saw a complete line. Print it out. */
+ printf("# %s", file->buffer);
+ }
+ }
+}
+
+
+/*
* Our exit handler. Called on completion of the test to report a summary of
* results provided we're still in the original process. This also handles
* printing out the plan if we used plan_lazy(), although that's suppressed if
@@ -130,22 +268,25 @@ static struct cleanup_func *cleanup_funcs = NULL;
static void
finish(void)
{
- int success;
+ int success, primary;
struct cleanup_func *current;
unsigned long highest = testnum - 1;
-
- /*
- * Don't do anything except free the cleanup functions if we aren't the
- * primary process (the process in which plan or plan_lazy was called).
- */
- if (_process != 0 && getpid() != _process) {
- while (cleanup_funcs != NULL) {
- current = cleanup_funcs;
- cleanup_funcs = cleanup_funcs->next;
- free(current);
- }
- return;
+ struct diag_file *file, *tmp;
+
+ /* Check for pending diag_file output. */
+ check_diag_files();
+
+ /* Free the diag_files. */
+ file = diag_files;
+ while (file != NULL) {
+ tmp = file;
+ file = file->next;
+ fclose(tmp->file);
+ free(tmp->name);
+ free(tmp->buffer);
+ free(tmp);
}
+ diag_files = NULL;
/*
* Determine whether all tests were successful, which is needed before
@@ -157,14 +298,20 @@ finish(void)
/*
* If there are any registered cleanup functions, we run those first. We
- * always run them, even if we didn't run a test.
+ * always run them, even if we didn't run a test. Don't do anything
+ * except free the diag_files and call cleanup functions if we aren't the
+ * primary process (the process in which plan or plan_lazy was called),
+ * and tell the cleanup functions that fact.
*/
+ primary = (_process == 0 || getpid() == _process);
while (cleanup_funcs != NULL) {
- cleanup_funcs->func(success);
+ cleanup_funcs->func(success, primary);
current = cleanup_funcs;
cleanup_funcs = cleanup_funcs->next;
free(current);
}
+ if (!primary)
+ return;
/* Don't do anything further if we never planned a test. */
if (_planned == 0)
@@ -198,7 +345,8 @@ finish(void)
/*
* Initialize things. Turns on line buffering on stdout and then prints out
- * the number of tests in the test suite.
+ * the number of tests in the test suite. We intentionally don't check for
+ * pending diag_file output here, since it should really come after the plan.
*/
void
plan(unsigned long count)
@@ -236,7 +384,8 @@ plan_lazy(void)
/*
* Skip the entire test suite and exits. Should be called instead of plan(),
- * not after it, since it prints out a special plan line.
+ * not after it, since it prints out a special plan line. Ignore diag_file
+ * output here, since it's not clear if it's allowed before the plan.
*/
void
skip_all(const char *format, ...)
@@ -253,25 +402,28 @@ skip_all(const char *format, ...)
* Takes a boolean success value and assumes the test passes if that value
* is true and fails if that value is false.
*/
-void
+int
ok(int success, const char *format, ...)
{
fflush(stderr);
+ check_diag_files();
printf("%sok %lu", success ? "" : "not ", testnum++);
if (!success)
_failed++;
PRINT_DESC(" - ", format);
putchar('\n');
+ return success;
}
/*
* Same as ok(), but takes the format arguments as a va_list.
*/
-void
+int
okv(int success, const char *format, va_list args)
{
fflush(stderr);
+ check_diag_files();
printf("%sok %lu", success ? "" : "not ", testnum++);
if (!success)
_failed++;
@@ -280,6 +432,7 @@ okv(int success, const char *format, va_list args)
vprintf(format, args);
}
putchar('\n');
+ return success;
}
@@ -290,6 +443,7 @@ void
skip(const char *reason, ...)
{
fflush(stderr);
+ check_diag_files();
printf("ok %lu # skip", testnum++);
PRINT_DESC(" ", reason);
putchar('\n');
@@ -299,19 +453,21 @@ skip(const char *reason, ...)
/*
* Report the same status on the next count tests.
*/
-void
-ok_block(unsigned long count, int status, const char *format, ...)
+int
+ok_block(unsigned long count, int success, const char *format, ...)
{
unsigned long i;
fflush(stderr);
+ check_diag_files();
for (i = 0; i < count; i++) {
- printf("%sok %lu", status ? "" : "not ", testnum++);
- if (!status)
+ printf("%sok %lu", success ? "" : "not ", testnum++);
+ if (!success)
_failed++;
PRINT_DESC(" - ", format);
putchar('\n');
}
+ return success;
}
@@ -324,6 +480,7 @@ skip_block(unsigned long count, const char *reason, ...)
unsigned long i;
fflush(stderr);
+ check_diag_files();
for (i = 0; i < count; i++) {
printf("ok %lu # skip", testnum++);
PRINT_DESC(" ", reason);
@@ -336,11 +493,15 @@ skip_block(unsigned long count, const char *reason, ...)
* Takes an expected integer and a seen integer and assumes the test passes
* if those two numbers match.
*/
-void
+int
is_int(long wanted, long seen, const char *format, ...)
{
+ int success;
+
fflush(stderr);
- if (wanted == seen)
+ check_diag_files();
+ success = (wanted == seen);
+ if (success)
printf("ok %lu", testnum++);
else {
diag("wanted: %ld", wanted);
@@ -350,6 +511,7 @@ is_int(long wanted, long seen, const char *format, ...)
}
PRINT_DESC(" - ", format);
putchar('\n');
+ return success;
}
@@ -357,15 +519,19 @@ is_int(long wanted, long seen, const char *format, ...)
* Takes a string and what the string should be, and assumes the test passes
* if those strings match (using strcmp).
*/
-void
+int
is_string(const char *wanted, const char *seen, const char *format, ...)
{
+ int success;
+
if (wanted == NULL)
wanted = "(null)";
if (seen == NULL)
seen = "(null)";
fflush(stderr);
- if (strcmp(wanted, seen) == 0)
+ check_diag_files();
+ success = (strcmp(wanted, seen) == 0);
+ if (success)
printf("ok %lu", testnum++);
else {
diag("wanted: %s", wanted);
@@ -375,6 +541,7 @@ is_string(const char *wanted, const char *seen, const char *format, ...)
}
PRINT_DESC(" - ", format);
putchar('\n');
+ return success;
}
@@ -382,11 +549,15 @@ is_string(const char *wanted, const char *seen, const char *format, ...)
* Takes an expected unsigned long and a seen unsigned long and assumes the
* test passes if the two numbers match. Otherwise, reports them in hex.
*/
-void
+int
is_hex(unsigned long wanted, unsigned long seen, const char *format, ...)
{
+ int success;
+
fflush(stderr);
- if (wanted == seen)
+ check_diag_files();
+ success = (wanted == seen);
+ if (success)
printf("ok %lu", testnum++);
else {
diag("wanted: %lx", (unsigned long) wanted);
@@ -396,6 +567,7 @@ is_hex(unsigned long wanted, unsigned long seen, const char *format, ...)
}
PRINT_DESC(" - ", format);
putchar('\n');
+ return success;
}
@@ -409,6 +581,7 @@ bail(const char *format, ...)
_aborted = 1;
fflush(stderr);
+ check_diag_files();
fflush(stdout);
printf("Bail out! ");
va_start(args, format);
@@ -430,6 +603,7 @@ sysbail(const char *format, ...)
_aborted = 1;
fflush(stderr);
+ check_diag_files();
fflush(stdout);
printf("Bail out! ");
va_start(args, format);
@@ -441,39 +615,96 @@ sysbail(const char *format, ...)
/*
- * Report a diagnostic to stderr.
+ * Report a diagnostic to stderr. Always returns 1 to allow embedding in
+ * compound statements.
*/
-void
+int
diag(const char *format, ...)
{
va_list args;
fflush(stderr);
+ check_diag_files();
fflush(stdout);
printf("# ");
va_start(args, format);
vprintf(format, args);
va_end(args);
printf("\n");
+ return 1;
}
/*
- * Report a diagnostic to stderr, appending strerror(errno).
+ * Report a diagnostic to stderr, appending strerror(errno). Always returns 1
+ * to allow embedding in compound statements.
*/
-void
+int
sysdiag(const char *format, ...)
{
va_list args;
int oerrno = errno;
fflush(stderr);
+ check_diag_files();
fflush(stdout);
printf("# ");
va_start(args, format);
vprintf(format, args);
va_end(args);
printf(": %s\n", strerror(oerrno));
+ return 1;
+}
+
+
+/*
+ * Register a new file for diag_file processing.
+ */
+void
+diag_file_add(const char *name)
+{
+ struct diag_file *file, *prev;
+
+ file = bcalloc(1, sizeof(struct diag_file));
+ file->name = bstrdup(name);
+ file->file = fopen(file->name, "r");
+ if (file->file == NULL)
+ sysbail("cannot open %s", name);
+ file->buffer = bmalloc(BUFSIZ);
+ file->bufsize = BUFSIZ;
+ if (diag_files == NULL)
+ diag_files = file;
+ else {
+ for (prev = diag_files; prev->next != NULL; prev = prev->next)
+ ;
+ prev->next = file;
+ }
+}
+
+
+/*
+ * Remove a file from diag_file processing. If the file is not found, do
+ * nothing, since there are some situations where it can be removed twice
+ * (such as if it's removed from a cleanup function, since cleanup functions
+ * are called after freeing all the diag_files).
+ */
+void
+diag_file_remove(const char *name)
+{
+ struct diag_file *file;
+ struct diag_file **prev = &diag_files;
+
+ for (file = diag_files; file != NULL; file = file->next) {
+ if (strcmp(file->name, name) == 0) {
+ *prev = file->next;
+ fclose(file->file);
+ free(file->name);
+ free(file->buffer);
+ free(file);
+ return;
+ }
+ prev = &file->next;
+ }
}
@@ -521,6 +752,32 @@ brealloc(void *p, size_t size)
/*
+ * The same as brealloc, but determine the size by multiplying an element
+ * count by a size, similar to calloc. The multiplication is checked for
+ * integer overflow.
+ *
+ * We should technically use SIZE_MAX here for the overflow check, but
+ * SIZE_MAX is C99 and we're only assuming C89 + SUSv3, which does not
+ * guarantee that it exists. They do guarantee that UINT_MAX exists, and we
+ * can assume that UINT_MAX <= SIZE_MAX.
+ *
+ * (In theory, C89 and C99 permit size_t to be smaller than unsigned int, but
+ * I disbelieve in the existence of such systems and they will have to cope
+ * without overflow checks.)
+ */
+void *
+breallocarray(void *p, size_t n, size_t size)
+{
+ if (n > 0 && UINT_MAX / n <= size)
+ bail("reallocarray too large");
+ p = realloc(p, n * size);
+ if (p == NULL)
+ sysbail("failed to realloc %lu bytes", (unsigned long) (n * size));
+ return p;
+}
+
+
+/*
* Copy a string, reporting a fatal error with bail on failure.
*/
char *
@@ -553,7 +810,7 @@ bstrndup(const char *s, size_t n)
/* Don't assume that the source string is nul-terminated. */
for (p = s; (size_t) (p - s) < n && *p != '\0'; p++)
;
- length = p - s;
+ length = (size_t) (p - s);
copy = malloc(length + 1);
if (p == NULL)
sysbail("failed to strndup %lu bytes", (unsigned long) length);
@@ -568,17 +825,12 @@ bstrndup(const char *s, size_t n)
* then SOURCE for the file and return the full path to the file. Returns
* NULL if the file doesn't exist. A non-NULL return should be freed with
* test_file_path_free().
- *
- * This function uses sprintf because it attempts to be independent of all
- * other portability layers. The use immediately after a memory allocation
- * should be safe without using snprintf or strlcpy/strlcat.
*/
char *
test_file_path(const char *file)
{
char *base;
char *path = NULL;
- size_t length;
const char *envs[] = { "BUILD", "SOURCE", NULL };
int i;
@@ -586,9 +838,7 @@ test_file_path(const char *file)
base = getenv(envs[i]);
if (base == NULL)
continue;
- length = strlen(base) + 1 + strlen(file) + 1;
- path = bmalloc(length);
- sprintf(path, "%s/%s", base, file);
+ path = concat(base, "/", file, (const char *) 0);
if (access(path, R_OK) == 0)
break;
free(path);
@@ -606,8 +856,7 @@ test_file_path(const char *file)
void
test_file_path_free(char *path)
{
- if (path != NULL)
- free(path);
+ free(path);
}
@@ -626,14 +875,11 @@ test_tmpdir(void)
{
const char *build;
char *path = NULL;
- size_t length;
build = getenv("BUILD");
if (build == NULL)
build = ".";
- length = strlen(build) + strlen("/tmp") + 1;
- path = bmalloc(length);
- sprintf(path, "%s/tmp", build);
+ path = concat(build, "/tmp", (const char *) 0);
if (access(path, X_OK) < 0)
if (mkdir(path, 0777) < 0)
sysbail("error creating temporary directory %s", path);
@@ -649,9 +895,9 @@ test_tmpdir(void)
void
test_tmpdir_free(char *path)
{
- rmdir(path);
if (path != NULL)
- free(path);
+ rmdir(path);
+ free(path);
}
diff --git a/tests/tap/basic.h b/tests/tap/basic.h
index 92d348a..4ecaaec 100644
--- a/tests/tap/basic.h
+++ b/tests/tap/basic.h
@@ -4,8 +4,9 @@
* This file is part of C TAP Harness. The current version plus supporting
* documentation is at <http://www.eyrie.org/~eagle/software/c-tap-harness/>.
*
- * Copyright 2009, 2010, 2011, 2012, 2013 Russ Allbery <eagle@eyrie.org>
- * Copyright 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2011, 2012
+ * Copyright 2009, 2010, 2011, 2012, 2013, 2014, 2015
+ * Russ Allbery <eagle@eyrie.org>
+ * Copyright 2001, 2002, 2004, 2005, 2006, 2007, 2008, 2011, 2012, 2014
* The Board of Trustees of the Leland Stanford Junior University
*
* Permission is hereby granted, free of charge, to any person obtaining a
@@ -67,26 +68,34 @@ void skip_all(const char *format, ...)
/*
* Basic reporting functions. The okv() function is the same as ok() but
* takes the test description as a va_list to make it easier to reuse the
- * reporting infrastructure when writing new tests.
+ * reporting infrastructure when writing new tests. ok() and okv() return the
+ * value of the success argument.
*/
-void ok(int success, const char *format, ...)
+int ok(int success, const char *format, ...)
__attribute__((__format__(printf, 2, 3)));
-void okv(int success, const char *format, va_list args);
+int okv(int success, const char *format, va_list args)
+ __attribute__((__format__(printf, 2, 0)));
void skip(const char *reason, ...)
__attribute__((__format__(printf, 1, 2)));
-/* Report the same status on, or skip, the next count tests. */
-void ok_block(unsigned long count, int success, const char *format, ...)
+/*
+ * Report the same status on, or skip, the next count tests. ok_block()
+ * returns the value of the success argument.
+ */
+int ok_block(unsigned long count, int success, const char *format, ...)
__attribute__((__format__(printf, 3, 4)));
void skip_block(unsigned long count, const char *reason, ...)
__attribute__((__format__(printf, 2, 3)));
-/* Check an expected value against a seen value. */
-void is_int(long wanted, long seen, const char *format, ...)
+/*
+ * Check an expected value against a seen value. Returns true if the test
+ * passes and false if it fails.
+ */
+int is_int(long wanted, long seen, const char *format, ...)
__attribute__((__format__(printf, 3, 4)));
-void is_string(const char *wanted, const char *seen, const char *format, ...)
+int is_string(const char *wanted, const char *seen, const char *format, ...)
__attribute__((__format__(printf, 3, 4)));
-void is_hex(unsigned long wanted, unsigned long seen, const char *format, ...)
+int is_hex(unsigned long wanted, unsigned long seen, const char *format, ...)
__attribute__((__format__(printf, 3, 4)));
/* Bail out with an error. sysbail appends strerror(errno). */
@@ -96,16 +105,30 @@ void sysbail(const char *format, ...)
__attribute__((__noreturn__, __nonnull__, __format__(printf, 1, 2)));
/* Report a diagnostic to stderr prefixed with #. */
-void diag(const char *format, ...)
+int diag(const char *format, ...)
__attribute__((__nonnull__, __format__(printf, 1, 2)));
-void sysdiag(const char *format, ...)
+int sysdiag(const char *format, ...)
__attribute__((__nonnull__, __format__(printf, 1, 2)));
+/*
+ * Register or unregister a file that contains supplementary diagnostics.
+ * Before any other output, all registered files will be read, line by line,
+ * and each line will be reported as a diagnostic as if it were passed to
+ * diag(). Nul characters are not supported in these files and will result in
+ * truncated output.
+ */
+void diag_file_add(const char *file)
+ __attribute__((__nonnull__));
+void diag_file_remove(const char *file)
+ __attribute__((__nonnull__));
+
/* Allocate memory, reporting a fatal error with bail on failure. */
void *bcalloc(size_t, size_t)
__attribute__((__alloc_size__(1, 2), __malloc__, __warn_unused_result__));
void *bmalloc(size_t)
__attribute__((__alloc_size__(1), __malloc__, __warn_unused_result__));
+void *breallocarray(void *, size_t, size_t)
+ __attribute__((__alloc_size__(2, 3), __malloc__, __warn_unused_result__));
void *brealloc(void *, size_t)
__attribute__((__alloc_size__(2), __malloc__, __warn_unused_result__));
char *bstrdup(const char *)
@@ -132,11 +155,14 @@ void test_tmpdir_free(char *path);
/*
* Register a cleanup function that is called when testing ends. All such
* registered functions will be run during atexit handling (and are therefore
- * subject to all the same constraints and caveats as atexit functions). The
- * function must return void and will be passed one argument, an int that will
- * be true if the test completed successfully and false otherwise.
+ * subject to all the same constraints and caveats as atexit functions).
+ *
+ * The function must return void and will be passed two arguments: an int that
+ * will be true if the test completed successfully and false otherwise, and an
+ * int that will be true if the cleanup function is run in the primary process
+ * (the one that called plan or plan_lazy) and false otherwise.
*/
-typedef void (*test_cleanup_func)(int);
+typedef void (*test_cleanup_func)(int, int);
void test_cleanup_register(test_cleanup_func)
__attribute__((__nonnull__));
diff --git a/tests/tap/kerberos.c b/tests/tap/kerberos.c
index 58315ee..6a5025a 100644
--- a/tests/tap/kerberos.c
+++ b/tests/tap/kerberos.c
@@ -15,7 +15,7 @@
* which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
*
* Written by Russ Allbery <eagle@eyrie.org>
- * Copyright 2006, 2007, 2009, 2010, 2011, 2012, 2013
+ * 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
@@ -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>
@@ -54,7 +55,9 @@
* Disable the requirement that format strings be literals, since it's easier
* to handle the possible patterns for kinit commands as an array.
*/
-#pragma GCC diagnostic ignored "-Wformat-nonliteral"
+#if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ > 2) || defined(__clang__)
+# pragma GCC diagnostic ignored "-Wformat-nonliteral"
+#endif
/*
@@ -201,33 +204,25 @@ kerberos_kinit(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.
+ * 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->pkinit_principal);
+ free(config->pkinit_cert);
free(config);
config = NULL;
}
@@ -245,6 +240,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 +352,38 @@ kerberos_setup(enum kerberos_needs needs)
*config->realm = '\0';
config->realm++;
}
+ test_file_path_free(path);
+
+ /*
+ * If we have PKINIT configuration, read it and fill out the relevant
+ * members of our config struct.
+ */
+ path = test_file_path("config/pkinit-principal");
if (path != NULL)
+ file = fopen(path, "r");
+ if (file != NULL) {
+ if (fgets(buffer, sizeof(buffer), file) == NULL)
+ bail("cannot read %s", path);
+ if (buffer[strlen(buffer) - 1] != '\n')
+ bail("no newline in %s", path);
+ buffer[strlen(buffer) - 1] = '\0';
+ fclose(file);
test_file_path_free(path);
+ path = test_file_path("config/pkinit-cert");
+ if (path != NULL) {
+ config->pkinit_principal = bstrdup(buffer);
+ config->pkinit_cert = bstrdup(path);
+ }
+ }
+ test_file_path_free(path);
+ if (config->pkinit_cert == NULL && (needs & TAP_KRB_NEEDS_PKINIT) != 0)
+ skip_all("PKINIT tests not configured");
/*
- * 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 +411,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;
}
diff --git a/tests/tap/kerberos.h b/tests/tap/kerberos.h
index c34f891..26f45f9 100644
--- a/tests/tap/kerberos.h
+++ b/tests/tap/kerberos.h
@@ -5,7 +5,7 @@
* which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
*
* Written by Russ Allbery <eagle@eyrie.org>
- * Copyright 2006, 2007, 2009, 2011, 2012, 2013
+ * Copyright 2006, 2007, 2009, 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
@@ -46,17 +46,21 @@ struct kerberos_config {
char *username; /* The local (non-realm) part of principal. */
char *realm; /* The realm part of the principal. */
char *password; /* The password. */
+ char *pkinit_principal; /* Principal for PKINIT authentication. */
+ char *pkinit_cert; /* Path to certificates for PKINIT. */
};
/*
* Whether to skip all tests (by calling skip_all) in kerberos_setup if
- * certain configuration information isn't available.
+ * certain configuration information isn't available. "_BOTH" means that the
+ * tests require both keytab and password, but PKINIT is not required.
*/
enum kerberos_needs {
TAP_KRB_NEEDS_NONE = 0x00,
TAP_KRB_NEEDS_KEYTAB = 0x01,
TAP_KRB_NEEDS_PASSWORD = 0x02,
- TAP_KRB_NEEDS_BOTH = 0x01 | 0x02
+ TAP_KRB_NEEDS_BOTH = 0x01 | 0x02,
+ TAP_KRB_NEEDS_PKINIT = 0x04
};
BEGIN_DECLS
@@ -73,11 +77,11 @@ BEGIN_DECLS
* the principal field will be NULL. If the files exist but loading them
* fails, or authentication fails, kerberos_setup calls bail.
*
- * kerberos_cleanup will be set up to run from an atexit handler. This means
- * that any child processes that should not remove the Kerberos ticket cache
- * should call _exit instead of exit. The principal will be automatically
- * freed when kerberos_cleanup is called or if kerberos_setup is called again.
- * The caller doesn't need to worry about it.
+ * kerberos_cleanup will be run as a cleanup function normally, freeing all
+ * resources and cleaning up temporary files on process exit. It can,
+ * however, be called directly if for some reason the caller needs to delete
+ * the Kerberos environment again. However, normally the caller can just call
+ * kerberos_setup again.
*/
struct kerberos_config *kerberos_setup(enum kerberos_needs)
__attribute__((__malloc__));
diff --git a/tests/tap/macros.h b/tests/tap/macros.h
index 04cc420..139cff0 100644
--- a/tests/tap/macros.h
+++ b/tests/tap/macros.h
@@ -8,7 +8,7 @@
* This file is part of C TAP Harness. The current version plus supporting
* documentation is at <http://www.eyrie.org/~eagle/software/c-tap-harness/>.
*
- * Copyright 2008, 2012, 2013 Russ Allbery <eagle@eyrie.org>
+ * Copyright 2008, 2012, 2013, 2015 Russ Allbery <eagle@eyrie.org>
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
@@ -53,8 +53,10 @@
* variadic macro support.
*/
#if !defined(__attribute__) && !defined(__alloc_size__)
-# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
-# define __alloc_size__(spec, args...) /* empty */
+# if defined(__GNUC__) && !defined(__clang__)
+# if __GNUC__ < 4 || (__GNUC__ == 4 && __GNUC_MINOR__ < 3)
+# define __alloc_size__(spec, args...) /* empty */
+# endif
# endif
#endif
diff --git a/tests/tap/messages.c b/tests/tap/messages.c
index 3754d18..9c28789 100644
--- a/tests/tap/messages.c
+++ b/tests/tap/messages.c
@@ -8,8 +8,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/>.
*
- * Copyright 2002, 2004, 2005 Russ Allbery <eagle@eyrie.org>
- * Copyright 2006, 2007, 2009, 2012
+ * Copyright 2002, 2004, 2005, 2015 Russ Allbery <eagle@eyrie.org>
+ * Copyright 2006, 2007, 2009, 2012, 2014
* The Board of Trustees of the Leland Stanford Junior University
*
* Permission is hereby granted, free of charge, to any person obtaining a
@@ -47,7 +47,7 @@ char *errors = NULL;
* An error handler that appends all errors to the errors global. Used by
* error_capture.
*/
-static void
+static void __attribute__((__format__(printf, 2, 0)))
message_log_buffer(int len UNUSED, const char *fmt, va_list args,
int error UNUSED)
{
@@ -75,10 +75,8 @@ message_log_buffer(int len UNUSED, const char *fmt, va_list args,
void
errors_capture(void)
{
- if (errors != NULL) {
- free(errors);
- errors = NULL;
- }
+ free(errors);
+ errors = NULL;
message_handlers_warn(1, message_log_buffer);
message_handlers_notice(1, message_log_buffer);
}
diff --git a/tests/tap/perl/Test/RRA.pm b/tests/tap/perl/Test/RRA.pm
index 1db6230..55e8c8d 100644
--- a/tests/tap/perl/Test/RRA.pm
+++ b/tests/tap/perl/Test/RRA.pm
@@ -5,31 +5,6 @@
# by both C packages with Automake and by stand-alone Perl modules. See
# Test::RRA::Automake for additional functions specifically for C Automake
# distributions.
-#
-# 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 <eagle@eyrie.org>
-# Copyright 2013
-# The Board of Trustees of the Leland Stanford Junior University
-#
-# Permission is hereby granted, free of charge, to any person obtaining a
-# copy of this software and associated documentation files (the "Software"),
-# to deal in the Software without restriction, including without limitation
-# the rights to use, copy, modify, merge, publish, distribute, sublicense,
-# and/or sell copies of the Software, and to permit persons to whom the
-# Software is furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
-# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-# DEALINGS IN THE SOFTWARE.
package Test::RRA;
@@ -51,29 +26,47 @@ our (@EXPORT_OK, @ISA, $VERSION);
# consistency is good).
BEGIN {
@ISA = qw(Exporter);
- @EXPORT_OK = qw(skip_unless_maintainer use_prereq);
+ @EXPORT_OK = qw(skip_unless_author skip_unless_automated use_prereq);
# This version should match the corresponding rra-c-util release, but with
# two digits for the minor version, including a leading zero if necessary,
# so that it will sort properly.
- $VERSION = '4.12';
+ $VERSION = '5.08';
}
-# Skip this test unless maintainer tests are requested. Takes a short
-# description of what tests this script would perform, which is used in the
-# skip message. Calls plan skip_all, which will terminate the program.
+# Skip this test unless author tests are requested. Takes a short description
+# of what tests this script would perform, which is used in the skip message.
+# Calls plan skip_all, which will terminate the program.
#
# $description - Short description of the tests
#
# Returns: undef
-sub skip_unless_maintainer {
+sub skip_unless_author {
my ($description) = @_;
- if (!$ENV{RRA_MAINTAINER_TESTS}) {
- plan skip_all => "$description only run for maintainer";
+ if (!$ENV{AUTHOR_TESTING}) {
+ plan skip_all => "$description only run for author";
}
return;
}
+# Skip this test unless doing automated testing or release testing. This is
+# used for tests that should be run by CPAN smoke testing or during releases,
+# but not for manual installs by end users. Takes a short description of what
+# tests this script would perform, which is used in the skip message. Calls
+# plan skip_all, which will terminate the program.
+#
+# $description - Short description of the tests
+#
+# Returns: undef
+sub skip_unless_automated {
+ my ($description) = @_;
+ for my $env (qw(AUTOMATED_TESTING RELEASE_TESTING AUTHOR_TESTING)) {
+ return if $ENV{$env};
+ }
+ plan skip_all => "$description normally skipped";
+ return;
+}
+
# Attempt to load a module and skip the test if the module could not be
# loaded. If the module could be loaded, call its import function manually.
# If the module could not be loaded, calls plan skip_all, which will terminate
@@ -143,13 +136,17 @@ Test::RRA - Support functions for Perl tests
=head1 SYNOPSIS
- use Test::RRA qw(skip_unless_maintainer use_prereq);
+ use Test::RRA
+ qw(skip_unless_author skip_unless_automated use_prereq);
+
+ # Skip this test unless author tests are requested.
+ skip_unless_author('Coding style tests');
- # Skip this test unless maintainer tests are requested.
- skip_unless_maintainer('Coding style tests');
+ # Skip this test unless doing automated or release testing.
+ skip_unless_automated('POD syntax tests');
# Load modules, skipping the test if they're not available.
- use_prereq('File::Slurp');
+ use_prereq('Perl6::Slurp', 'slurp');
use_prereq('Test::Script::Run', '0.04');
=head1 DESCRIPTION
@@ -166,12 +163,23 @@ script should be explicitly imported.
=over 4
-=item skip_unless_maintainer(DESC)
+=item skip_unless_author(DESC)
-Checks whether RRA_MAINTAINER_TESTS is set in the environment and skips
-the whole test (by calling C<plan skip_all> from Test::More) if it is not.
+Checks whether AUTHOR_TESTING is set in the environment and skips the
+whole test (by calling C<plan skip_all> from Test::More) if it is not.
DESC is a description of the tests being skipped. A space and C<only run
-for maintainer> will be appended to it and used as the skip reason.
+for author> will be appended to it and used as the skip reason.
+
+=item skip_unless_automated(DESC)
+
+Checks whether AUTHOR_TESTING, AUTOMATED_TESTING, or RELEASE_TESTING are
+set in the environment and skips the whole test (by calling C<plan
+skip_all> from Test::More) if they are not. This should be used by tests
+that should not run during end-user installs of the module, but which
+should run as part of CPAN smoke testing and release testing.
+
+DESC is a description of the tests being skipped. A space and C<normally
+skipped> will be appended to it and used as the skip reason.
=item use_prereq(MODULE[, VERSION][, IMPORT ...])
@@ -192,7 +200,7 @@ Russ Allbery <eagle@eyrie.org>
=head1 COPYRIGHT AND LICENSE
-Copyright 2013 The Board of Trustees of the Leland Stanford Junior
+Copyright 2013, 2014 The Board of Trustees of the Leland Stanford Junior
University
Permission is hereby granted, free of charge, to any person obtaining a
@@ -220,4 +228,8 @@ Test::More(3), Test::RRA::Automake(3), Test::RRA::Config(3)
This module is maintained in the rra-c-util package. The current version
is available from L<http://www.eyrie.org/~eagle/software/rra-c-util/>.
+The functions to control when tests are run use environment variables
+defined by the L<Lancaster
+Consensus|https://github.com/Perl-Toolchain-Gang/toolchain-site/blob/master/lancaster-consensus.md>.
+
=cut
diff --git a/tests/tap/perl/Test/RRA/Automake.pm b/tests/tap/perl/Test/RRA/Automake.pm
index b8ce095..1a7fa93 100644
--- a/tests/tap/perl/Test/RRA/Automake.pm
+++ b/tests/tap/perl/Test/RRA/Automake.pm
@@ -9,31 +9,6 @@
#
# All the functions here assume that BUILD and SOURCE are set in the
# environment. This is normally done via the C TAP Harness runtests wrapper.
-#
-# 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 <eagle@eyrie.org>
-# Copyright 2013
-# The Board of Trustees of the Leland Stanford Junior University
-#
-# Permission is hereby granted, free of charge, to any person obtaining a
-# copy of this software and associated documentation files (the "Software"),
-# to deal in the Software without restriction, including without limitation
-# the rights to use, copy, modify, merge, publish, distribute, sublicense,
-# and/or sell copies of the Software, and to permit persons to whom the
-# Software is furnished to do so, subject to the following conditions:
-#
-# The above copyright notice and this permission notice shall be included in
-# all copies or substantial portions of the Software.
-#
-# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
-# THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-# FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
-# DEALINGS IN THE SOFTWARE.
package Test::RRA::Automake;
@@ -87,7 +62,7 @@ BEGIN {
# This version should match the corresponding rra-c-util release, but with
# two digits for the minor version, including a leading zero if necessary,
# so that it will sort properly.
- $VERSION = '4.12';
+ $VERSION = '5.08';
}
# Perl directories to skip globally for perl_dirs. We ignore the perl
@@ -196,7 +171,7 @@ sub perl_dirs {
# Build the list of top-level directories to test.
opendir(my $rootdir, q{.}) or BAIL_OUT("cannot open .: $!");
- my @dirs = grep { -d $_ && !$skip{$_} } readdir($rootdir);
+ my @dirs = grep { -d && !$skip{$_} } readdir($rootdir);
closedir($rootdir);
@dirs = File::Spec->no_upwards(@dirs);
@@ -387,6 +362,8 @@ Russ Allbery <eagle@eyrie.org>
=head1 COPYRIGHT AND LICENSE
+Copyright 2014 Russ Allbery <eagle@eyrie.org>
+
Copyright 2013 The Board of Trustees of the Leland Stanford Junior
University
diff --git a/tests/tap/perl/Test/RRA/Config.pm b/tests/tap/perl/Test/RRA/Config.pm
index 106cc6e..3e04bdb 100644
--- a/tests/tap/perl/Test/RRA/Config.pm
+++ b/tests/tap/perl/Test/RRA/Config.pm
@@ -4,9 +4,6 @@
# configuration file to store some package-specific data. This module loads
# that configuration and provides the namespace for the configuration
# settings.
-#
-# 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/>.
package Test::RRA::Config;
@@ -31,12 +28,13 @@ BEGIN {
@EXPORT_OK = qw(
$COVERAGE_LEVEL @COVERAGE_SKIP_TESTS @CRITIC_IGNORE $LIBRARY_PATH
$MINIMUM_VERSION %MINIMUM_VERSION @POD_COVERAGE_EXCLUDE @STRICT_IGNORE
+ @STRICT_PREREQ
);
# This version should match the corresponding rra-c-util release, but with
# two digits for the minor version, including a leading zero if necessary,
# so that it will sort properly.
- $VERSION = '4.12';
+ $VERSION = '5.08';
}
# If BUILD or SOURCE are set in the environment, look for data/perl.conf under
@@ -65,6 +63,7 @@ our $MINIMUM_VERSION = '5.008';
our %MINIMUM_VERSION;
our @POD_COVERAGE_EXCLUDE;
our @STRICT_IGNORE;
+our @STRICT_PREREQ;
# Load the configuration.
if (!do($PATH)) {
@@ -163,6 +162,13 @@ for C<use strict> and C<use warnings>. The contents of this directory
must be either top-level directory names or directory names starting with
F<tests/>.
+=item @STRICT_PREREQ
+
+A list of Perl modules that have to be available in order to do meaningful
+Test::Strict testing. If any of the modules cannot be loaded via C<use>,
+Test::Strict checking will be skipped. There is currently no way to
+require specific versions of the modules.
+
=back
No variables are exported by default, but the variables can be imported
@@ -174,7 +180,7 @@ Russ Allbery <eagle@eyrie.org>
=head1 COPYRIGHT AND LICENSE
-Copyright 2013 The Board of Trustees of the Leland Stanford Junior
+Copyright 2013, 2014 The Board of Trustees of the Leland Stanford Junior
University
Permission is hereby granted, free of charge, to any person obtaining a
diff --git a/tests/tap/process.c b/tests/tap/process.c
index b8d6ff9..8c22324 100644
--- a/tests/tap/process.c
+++ b/tests/tap/process.c
@@ -7,12 +7,15 @@
* runs a function in a subprocess and checks its output and exit status
* against expected values.
*
+ * Requires an Autoconf probe for sys/select.h and a replacement for a missing
+ * mkstemp.
+ *
* 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 <eagle@eyrie.org>
- * Copyright 2002, 2004, 2005 Russ Allbery <eagle@eyrie.org>
- * Copyright 2009, 2010, 2011, 2013
+ * Copyright 2002, 2004, 2005, 2013 Russ Allbery <eagle@eyrie.org>
+ * Copyright 2009, 2010, 2011, 2013, 2014
* The Board of Trustees of the Leland Stanford Junior University
*
* Permission is hereby granted, free of charge, to any person obtaining a
@@ -37,12 +40,51 @@
#include <config.h>
#include <portable/system.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <signal.h>
+#ifdef HAVE_SYS_SELECT_H
+# include <sys/select.h>
+#endif
+#include <sys/stat.h>
+#ifdef HAVE_SYS_TIME_H
+# include <sys/time.h>
+#endif
#include <sys/wait.h>
+#include <time.h>
#include <tests/tap/basic.h>
#include <tests/tap/process.h>
#include <tests/tap/string.h>
+/* May be defined by the build system. */
+#ifndef PATH_FAKEROOT
+# define PATH_FAKEROOT ""
+#endif
+
+/* How long to wait for the process to start in seconds. */
+#define PROCESS_WAIT 10
+
+/*
+ * Used to store information about a background process. This contains
+ * everything required to stop the process and clean up after it.
+ */
+struct process {
+ pid_t pid; /* PID of child process */
+ char *pidfile; /* PID file to delete on process stop */
+ char *tmpdir; /* Temporary directory for log file */
+ char *logfile; /* Log file of process output */
+ bool is_child; /* Whether we can waitpid for process */
+ struct process *next; /* Next process in global list */
+};
+
+/*
+ * Global list of started processes, which will be cleaned up automatically on
+ * program exit if they haven't been explicitly stopped with process_stop
+ * prior to that point.
+ */
+static struct process *processes = NULL;
+
/*
* Given a function, an expected exit status, and expected output, runs that
@@ -178,3 +220,309 @@ run_setup(const char *const argv[])
}
free(output);
}
+
+
+/*
+ * Free the resources associated with tracking a process, without doing
+ * anything to the process. This is kept separate so that we can free
+ * resources during shutdown in a non-primary process.
+ */
+static void
+process_free(struct process *process)
+{
+ struct process **prev;
+
+ /* Do nothing if called with a NULL argument. */
+ if (process == NULL)
+ return;
+
+ /* Remove the process from the global list. */
+ prev = &processes;
+ while (*prev != NULL && *prev != process)
+ prev = &(*prev)->next;
+ if (*prev == process)
+ *prev = process->next;
+
+ /* Free resources. */
+ free(process->pidfile);
+ free(process->logfile);
+ test_tmpdir_free(process->tmpdir);
+ free(process);
+}
+
+
+/*
+ * Kill a process and wait for it to exit. Returns the status of the process.
+ * Calls bail on a system failure or a failure of the process to exit.
+ *
+ * We are quite aggressive with error reporting here because child processes
+ * that don't exit or that don't exist often indicate some form of test
+ * failure.
+ */
+static int
+process_kill(struct process *process)
+{
+ int result, i;
+ int status = -1;
+ struct timeval tv;
+ unsigned long pid = process->pid;
+
+ /* If the process is not a child, just kill it and hope. */
+ if (!process->is_child) {
+ if (kill(process->pid, SIGTERM) < 0 && errno != ESRCH)
+ sysbail("cannot send SIGTERM to process %lu", pid);
+ return 0;
+ }
+
+ /* Check if the process has already exited. */
+ result = waitpid(process->pid, &status, WNOHANG);
+ if (result < 0)
+ sysbail("cannot wait for child process %lu", pid);
+ else if (result > 0)
+ return status;
+
+ /*
+ * Kill the process and wait for it to exit. I don't want to go to the
+ * work of setting up a SIGCHLD handler or a full event loop here, so we
+ * effectively poll every tenth of a second for process exit (and
+ * hopefully faster when it does since the SIGCHLD may interrupt our
+ * select, although we're racing with it.
+ */
+ if (kill(process->pid, SIGTERM) < 0 && errno != ESRCH)
+ sysbail("cannot send SIGTERM to child process %lu", pid);
+ for (i = 0; i < PROCESS_WAIT * 10; i++) {
+ tv.tv_sec = 0;
+ tv.tv_usec = 100000;
+ select(0, NULL, NULL, NULL, &tv);
+ result = waitpid(process->pid, &status, WNOHANG);
+ if (result < 0)
+ sysbail("cannot wait for child process %lu", pid);
+ else if (result > 0)
+ return status;
+ }
+
+ /* The process still hasn't exited. Bail. */
+ bail("child process %lu did not exit on SIGTERM", pid);
+
+ /* Not reached, but some compilers may get confused. */
+ return status;
+}
+
+
+/*
+ * Stop a particular process given its process struct. This kills the
+ * process, waits for it to exit if possible (giving it at most five seconds),
+ * and then removes it from the global processes struct so that it isn't
+ * stopped again during global shutdown.
+ */
+void
+process_stop(struct process *process)
+{
+ int status;
+ unsigned long pid = process->pid;
+
+ /* Stop the process. */
+ status = process_kill(process);
+
+ /* Call diag to flush logs as well as provide exit status. */
+ if (process->is_child)
+ diag("stopped process %lu (exit status %d)", pid, status);
+ else
+ diag("stopped process %lu", pid);
+
+ /* Remove the log and PID file. */
+ diag_file_remove(process->logfile);
+ unlink(process->pidfile);
+ unlink(process->logfile);
+
+ /* Free resources. */
+ process_free(process);
+}
+
+
+/*
+ * Stop all running processes. This is called as a cleanup handler during
+ * process shutdown. The first argument, which says whether the test was
+ * successful, is ignored, since the same actions should be performed
+ * regardless. The second argument says whether this is the primary process,
+ * in which case we do the full shutdown. Otherwise, we only free resources
+ * but don't stop the process.
+ */
+static void
+process_stop_all(int success UNUSED, int primary)
+{
+ while (processes != NULL) {
+ if (primary)
+ process_stop(processes);
+ else
+ process_free(processes);
+ }
+}
+
+
+/*
+ * Read the PID of a process from a file. This is necessary when running
+ * under fakeroot to get the actual PID of the remctld process.
+ */
+static long
+read_pidfile(const char *path)
+{
+ FILE *file;
+ char buffer[BUFSIZ];
+ long pid;
+
+ file = fopen(path, "r");
+ if (file == NULL)
+ sysbail("cannot open %s", path);
+ if (fgets(buffer, sizeof(buffer), file) == NULL)
+ sysbail("cannot read from %s", path);
+ fclose(file);
+ pid = strtol(buffer, NULL, 10);
+ if (pid <= 0)
+ bail("cannot read PID from %s", path);
+ return pid;
+}
+
+
+/*
+ * Start a process and return its status information. The status information
+ * is also stored in the global processes linked list so that it can be
+ * stopped automatically on program exit.
+ *
+ * The boolean argument says whether to start the process under fakeroot. If
+ * true, PATH_FAKEROOT must be defined, generally by Autoconf. If it's not
+ * found, call skip_all.
+ *
+ * This is a helper function for process_start and process_start_fakeroot.
+ */
+static struct process *
+process_start_internal(const char *const argv[], const char *pidfile,
+ bool fakeroot)
+{
+ size_t i;
+ int log_fd;
+ const char *name;
+ struct timeval tv;
+ struct process *process;
+ const char **fakeroot_argv = NULL;
+ const char *path_fakeroot = PATH_FAKEROOT;
+
+ /* Check prerequisites. */
+ if (fakeroot && path_fakeroot[0] == '\0')
+ skip_all("fakeroot not found");
+
+ /* Create the process struct and log file. */
+ process = bcalloc(1, sizeof(struct process));
+ process->pidfile = bstrdup(pidfile);
+ process->tmpdir = test_tmpdir();
+ name = strrchr(argv[0], '/');
+ if (name != NULL)
+ name++;
+ else
+ name = argv[0];
+ basprintf(&process->logfile, "%s/%s.log.XXXXXX", process->tmpdir, name);
+ log_fd = mkstemp(process->logfile);
+ if (log_fd < 0)
+ sysbail("cannot create log file for %s", argv[0]);
+
+ /* If using fakeroot, rewrite argv accordingly. */
+ if (fakeroot) {
+ for (i = 0; argv[i] != NULL; i++)
+ ;
+ fakeroot_argv = bcalloc(2 + i + 1, sizeof(const char *));
+ fakeroot_argv[0] = path_fakeroot;
+ fakeroot_argv[1] = "--";
+ for (i = 0; argv[i] != NULL; i++)
+ fakeroot_argv[i + 2] = argv[i];
+ fakeroot_argv[i + 2] = NULL;
+ argv = fakeroot_argv;
+ }
+
+ /*
+ * Fork off the child process, redirect its standard output and standard
+ * error to the log file, and then exec the program.
+ */
+ process->pid = fork();
+ if (process->pid < 0)
+ sysbail("fork failed");
+ else if (process->pid == 0) {
+ if (dup2(log_fd, STDOUT_FILENO) < 0)
+ sysbail("cannot redirect standard output");
+ if (dup2(log_fd, STDERR_FILENO) < 0)
+ sysbail("cannot redirect standard error");
+ close(log_fd);
+ if (execv(argv[0], (char *const *) argv) < 0)
+ sysbail("exec of %s failed", argv[0]);
+ }
+ close(log_fd);
+ free(fakeroot_argv);
+
+ /*
+ * In the parent. Wait for the child to start by watching for the PID
+ * file to appear in 100ms intervals.
+ */
+ for (i = 0; i < PROCESS_WAIT * 10 && access(pidfile, F_OK) != 0; i++) {
+ tv.tv_sec = 0;
+ tv.tv_usec = 100000;
+ select(0, NULL, NULL, NULL, &tv);
+ }
+
+ /*
+ * If the PID file still hasn't appeared after ten seconds, attempt to
+ * kill the process and then bail.
+ */
+ if (access(pidfile, F_OK) != 0) {
+ kill(process->pid, SIGTERM);
+ alarm(5);
+ waitpid(process->pid, NULL, 0);
+ alarm(0);
+ bail("cannot start %s", argv[0]);
+ }
+
+ /*
+ * Read the PID back from the PID file. This usually isn't necessary for
+ * non-forking daemons, but always doing this makes this function general,
+ * and it's required when running under fakeroot.
+ */
+ if (fakeroot)
+ process->pid = read_pidfile(pidfile);
+ process->is_child = !fakeroot;
+
+ /* Register the log file as a source of diag messages. */
+ diag_file_add(process->logfile);
+
+ /*
+ * Add the process to our global list and set our cleanup handler if this
+ * is the first process we started.
+ */
+ if (processes == NULL)
+ test_cleanup_register(process_stop_all);
+ process->next = processes;
+ processes = process;
+
+ /* All done. */
+ return process;
+}
+
+
+/*
+ * Start a process and return the opaque process struct. The process must
+ * create pidfile with its PID when startup is complete.
+ */
+struct process *
+process_start(const char *const argv[], const char *pidfile)
+{
+ return process_start_internal(argv, pidfile, false);
+}
+
+
+/*
+ * Start a process under fakeroot and return the opaque process struct. If
+ * fakeroot is not available, calls skip_all. The process must create pidfile
+ * with its PID when startup is complete.
+ */
+struct process *
+process_start_fakeroot(const char *const argv[], const char *pidfile)
+{
+ return process_start_internal(argv, pidfile, true);
+}
diff --git a/tests/tap/process.h b/tests/tap/process.h
index ed90345..8137d5d 100644
--- a/tests/tap/process.h
+++ b/tests/tap/process.h
@@ -5,7 +5,7 @@
* which can be found at <http://www.eyrie.org/~eagle/software/rra-c-util/>.
*
* Written by Russ Allbery <eagle@eyrie.org>
- * Copyright 2009, 2010
+ * Copyright 2009, 2010, 2013
* The Board of Trustees of the Leland Stanford Junior University
*
* Permission is hereby granted, free of charge, to any person obtaining a
@@ -33,6 +33,9 @@
#include <config.h>
#include <tests/tap/macros.h>
+/* Opaque data type for process_start and friends. */
+struct process;
+
BEGIN_DECLS
/*
@@ -60,6 +63,32 @@ void is_function_output(test_function_type, void *data, int status,
void run_setup(const char *const argv[])
__attribute__((__nonnull__));
+/*
+ * process_start starts a process in the background, returning an opaque data
+ * struct that can be used to stop the process later. The standard output and
+ * standard error of the process will be sent to a log file registered with
+ * diag_file_add, so its output will be properly interleaved with the test
+ * case output.
+ *
+ * The process should create a PID file in the path given as the second
+ * argument when it's finished initialization.
+ *
+ * process_start_fakeroot is the same but starts the process under fakeroot.
+ * PATH_FAKEROOT must be defined (generally by Autoconf). If fakeroot is not
+ * found, process_start_fakeroot will call skip_all, so be sure to call this
+ * function before plan.
+ *
+ * process_stop can be called to explicitly stop the process. If it isn't
+ * called by the test program, it will be called automatically when the
+ * program exits.
+ */
+struct process *process_start(const char *const argv[], const char *pidfile)
+ __attribute__((__nonnull__));
+struct process *process_start_fakeroot(const char *const argv[],
+ const char *pidfile)
+ __attribute__((__nonnull__));
+void process_stop(struct process *);
+
END_DECLS
#endif /* TAP_PROCESS_H */
diff --git a/tests/tap/string.h b/tests/tap/string.h
index cc51945..d58f75d 100644
--- a/tests/tap/string.h
+++ b/tests/tap/string.h
@@ -42,7 +42,7 @@ BEGIN_DECLS
void basprintf(char **, const char *, ...)
__attribute__((__nonnull__, __format__(printf, 2, 3)));
void bvasprintf(char **, const char *, va_list)
- __attribute__((__nonnull__));
+ __attribute__((__nonnull__, __format__(printf, 2, 0)));
END_DECLS