From d23b20e9573b260595f50e662e771d7a336ab07e Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=A9mie=20Galarneau?= Date: Thu, 21 Feb 2013 14:36:20 -0500 Subject: [PATCH] Fix snprintf test and output result to TAP format MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Replace the tap library with the one used by babeltrace's test suite which offers more functionality and outputs in a truly TAP-compliant format. Signed-off-by: Jérémie Galarneau --- .gitignore | 1 + configure.ac | 1 + tests/Makefile.am | 2 +- tests/runtests | 1 + tests/snprintf/Makefile.am | 10 +- tests/snprintf/README | 9 + tests/snprintf/prog.c | 17 +- tests/snprintf/run | 4 + tests/tap.c | 494 ++++++++++++++++++++++++++++--------- tests/tap.h | 102 ++++++-- 10 files changed, 488 insertions(+), 153 deletions(-) create mode 100644 tests/snprintf/README create mode 100755 tests/snprintf/run diff --git a/.gitignore b/.gitignore index c7c52008..1065aa32 100644 --- a/.gitignore +++ b/.gitignore @@ -40,3 +40,4 @@ tests/trace_event/trace_event_test tests/tracepoint/benchmark/tracepoint_benchmark tests/tracepoint/tracepoint_test tests/hello-static-lib/hello +tests/snprintf/prog diff --git a/configure.ac b/configure.ac index 06e5ea55..fbc534b0 100644 --- a/configure.ac +++ b/configure.ac @@ -286,6 +286,7 @@ AC_CONFIG_FILES([ tests/hello-static-lib/Makefile tests/hello.cxx/Makefile tests/same_line_tracepoint/Makefile + tests/snprintf/Makefile tests/ust-basic-tracing/Makefile tests/ust-multi-test/Makefile lttng-ust.pc diff --git a/tests/Makefile.am b/tests/Makefile.am index ed3db48c..4d066081 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -1,4 +1,4 @@ -SUBDIRS = . hello hello-static-lib same_line_tracepoint +SUBDIRS = . hello hello-static-lib same_line_tracepoint snprintf #ust-basic-tracing ust-multi-test #SUBDIRS = . hello2 basic basic_long simple_include snprintf test-nevents test-libustinstr-malloc dlopen same_line_marker trace_event register_test tracepoint libustctl_function_tests exit-fast diff --git a/tests/runtests b/tests/runtests index ca454059..1f047c53 100755 --- a/tests/runtests +++ b/tests/runtests @@ -28,6 +28,7 @@ function simple_harness_run() { fi } +./${TEST_DIR}/snprintf/run simple_harness_run ./manual_mode_tracing.sh diff --git a/tests/snprintf/Makefile.am b/tests/snprintf/Makefile.am index 734ab18a..9ca4f09a 100644 --- a/tests/snprintf/Makefile.am +++ b/tests/snprintf/Makefile.am @@ -1,7 +1,9 @@ -AM_CPPFLAGS = -I$(top_srcdir)/include +AM_CPPFLAGS = -I$(top_srcdir)/include -I$(top_srcdir)/tests/ -check_PROGRAMS = prog +noinst_PROGRAMS = prog prog_SOURCES = prog.c -prog_LDADD = $(top_builddir)/snprintf/libustsnprintf.la +prog_LDADD = $(top_builddir)/snprintf/libustsnprintf.la \ + $(top_builddir)/tests/libtap.a -TESTS = prog +noinst_SCRIPT = run +EXTRA_DIST = run diff --git a/tests/snprintf/README b/tests/snprintf/README new file mode 100644 index 00000000..1321fad2 --- /dev/null +++ b/tests/snprintf/README @@ -0,0 +1,9 @@ +ust_safe_snprintf test +------------------- + +Basic functionality test of the ust snprintf() implementation. + +DESCRIPTION +----------- + +The ust_safe_snprintf() is tested against a known output string. diff --git a/tests/snprintf/prog.c b/tests/snprintf/prog.c index dd0b04f8..8d3fd5c0 100644 --- a/tests/snprintf/prog.c +++ b/tests/snprintf/prog.c @@ -19,17 +19,24 @@ #include #include "ust_snprintf.h" +#include "tap.h" + int main() { char buf[100]; char *expected; + char test_fmt_str[] = "header %d, %s, %03d, '%3$*d'"; + char escaped_test_fmt_str[] = "header %%d, %%s, %%03d, '%%3$*d'"; + + plan_tests(1); expected = "header 9999, hello, 005, ' 9'"; - ust_safe_snprintf(buf, 99, "header %d, %s, %03d, '%3$*d'", 9999, "hello", 5, 9); - if(strcmp(buf, expected) != 0) { - printf("Error: expected \"%s\" and got \"%s\"\n", expected, buf); - return 1; - } + ust_safe_snprintf(buf, 99, test_fmt_str, 9999, "hello", 5, 9); + + char test_desc_fmt_str[] = "Got expected output string with format string \"%s\""; + char test_desc[sizeof(escaped_test_fmt_str) + sizeof(test_desc_fmt_str) - 1]; + sprintf(test_desc, test_desc_fmt_str, escaped_test_fmt_str); + ok(strcmp(buf, expected) == 0, test_desc); return 0; } diff --git a/tests/snprintf/run b/tests/snprintf/run new file mode 100755 index 00000000..cc77d408 --- /dev/null +++ b/tests/snprintf/run @@ -0,0 +1,4 @@ +#!/bin/bash + +TEST_DIR=$(dirname $0) +./${TEST_DIR}/prog diff --git a/tests/tap.c b/tests/tap.c index 4bb695ef..a4309511 100644 --- a/tests/tap.c +++ b/tests/tap.c @@ -1,177 +1,427 @@ -/* Copyright (C) 2010 Nils Carlson +/*- + * Copyright (c) 2004 Nik Clayton + * All rights reserved. * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * This 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. */ -#include + +#define _GNU_SOURCE +#include #include #include -#include -#include +#include + +#include "tap.h" + +static int no_plan = 0; +static int skip_all = 0; +static int have_plan = 0; +static unsigned int test_count = 0; /* Number of tests that have been run */ +static unsigned int e_tests = 0; /* Expected number of tests to run */ +static unsigned int failures = 0; /* Number of tests that failed */ +static char *todo_msg = NULL; +static char *todo_msg_fixed = "libtap malloc issue"; +static int todo = 0; +static int test_died = 0; + +/* Encapsulate the pthread code in a conditional. In the absence of + libpthread the code does nothing */ +#ifdef HAVE_LIBPTHREAD +#include +static pthread_mutex_t M = PTHREAD_MUTEX_INITIALIZER; +# define LOCK pthread_mutex_lock(&M); +# define UNLOCK pthread_mutex_unlock(&M); +#else +# define LOCK +# define UNLOCK +#endif + +static void _expected_tests(unsigned int); +static void _tap_init(void); +static void _cleanup(void); + +/* + * Generate a test result. + * + * ok -- boolean, indicates whether or not the test passed. + * test_name -- the name of the test, may be NULL + * test_comment -- a comment to print afterwards, may be NULL + */ +unsigned int +_gen_result(int ok, const char *func, char *file, unsigned int line, + char *test_name, ...) +{ + va_list ap; + char *local_test_name = NULL; + char *c; + int name_is_digits; + + LOCK; + + test_count++; + + /* Start by taking the test name and performing any printf() + expansions on it */ + if(test_name != NULL) { + va_start(ap, test_name); + vasprintf(&local_test_name, test_name, ap); + va_end(ap); + + /* Make sure the test name contains more than digits + and spaces. Emit an error message and exit if it + does */ + if(local_test_name) { + name_is_digits = 1; + for(c = local_test_name; *c != '\0'; c++) { + if(!isdigit(*c) && !isspace(*c)) { + name_is_digits = 0; + break; + } + } + + if(name_is_digits) { + diag(" You named your test '%s'. You shouldn't use numbers for your test names.", local_test_name); + diag(" Very confusing."); + } + } + } -static int tap_planned = -1; -static int tap_count = 1; -static int tap_passed = 0; + if(!ok) { + printf("not "); + failures++; + } -static pthread_t stdout_thread; -static int pipefd[2]; -static FILE *pipe_r_file; -static FILE *normal_stdout; + printf("ok %d", test_count); + + if(test_name != NULL) { + printf(" - "); + + /* Print the test name, escaping any '#' characters it + might contain */ + if(local_test_name != NULL) { + flockfile(stdout); + for(c = local_test_name; *c != '\0'; c++) { + if(*c == '#') + fputc('\\', stdout); + fputc((int)*c, stdout); + } + funlockfile(stdout); + } else { /* vasprintf() failed, use a fixed message */ + printf("%s", todo_msg_fixed); + } + } -static void *_tap_comment_stdout(void *_unused) -{ - char line[4096]; + /* If we're in a todo_start() block then flag the test as being + TODO. todo_msg should contain the message to print at this + point. If it's NULL then asprintf() failed, and we should + use the fixed message. + + This is not counted as a failure, so decrement the counter if + the test failed. */ + if(todo) { + printf(" # TODO %s", todo_msg ? todo_msg : todo_msg_fixed); + if(!ok) + failures--; + } - while (fgets(&line[0], 4096, pipe_r_file)) { - if (strncmp(line, "_TAP", 4)) { - fprintf(normal_stdout, "# %s", line); - } else { - fprintf(normal_stdout, "# %s", &line[4]); - } + printf("\n"); + + if(!ok) { + if(getenv("HARNESS_ACTIVE") != NULL) + fputs("\n", stderr); + + diag(" Failed %stest (%s:%s() at line %d)", + todo ? "(TODO) " : "", file, func, line); } - pthread_exit(0); + free(local_test_name); + + UNLOCK; + + /* We only care (when testing) that ok is positive, but here we + specifically only want to return 1 or 0 */ + return ok ? 1 : 0; } -static void tap_comment_stdout(void) +/* + * Initialise the TAP library. Will only do so once, however many times it's + * called. + */ +void +_tap_init(void) { - int stdout_fileno, new_stdout, result, fd; + static int run_once = 0; - if (pipe(pipefd) < 0) { - perror("# Failed to open pipe"); - return; - } + if(!run_once) { + atexit(_cleanup); - pipe_r_file = fdopen(pipefd[0], "r"); - if (!pipe_r_file) { - perror("# Couldn't create a FILE from the pipe"); - goto close_pipe; + /* stdout needs to be unbuffered so that the output appears + in the same place relative to stderr output as it does + with Test::Harness */ + setbuf(stdout, 0); + run_once = 1; } +} - /* Set it before we create the reading thread */ - setlinebuf(pipe_r_file); +/* + * Note that there's no plan. + */ +int +plan_no_plan(void) +{ - stdout_fileno = fileno(stdout); - if (stdout_fileno < 0) { - perror("# Couldn't get fileno for stdout!?"); - goto close_pipe_r_file; - } + LOCK; - new_stdout = dup(stdout_fileno); - if (new_stdout < 0) { - perror("# Couldn't dup stdout"); - goto close_pipe_r_file; - } + _tap_init(); - normal_stdout = fdopen(new_stdout, "w"); - if (!normal_stdout) { - perror("# Could create a FILE from new_stdout"); - goto close_dup_stdout; + if(have_plan != 0) { + fprintf(stderr, "You tried to plan twice!\n"); + test_died = 1; + UNLOCK; + exit(255); } - result = pthread_create(&stdout_thread, NULL, - _tap_comment_stdout, NULL); - if (result < 0) { - perror("# Couldn't start stdout_thread"); - goto close_normal_stdout; - } + have_plan = 1; + no_plan = 1; - fclose(stdout); - fclose(stderr); + UNLOCK; - fd = dup(pipefd[1]); - if (fd != STDOUT_FILENO) { - fprintf(stderr, "# Failed to open a new stdout!\n"); - goto close_normal_stdout; - } + return 1; +} - stdout = fdopen(fd, "w"); - if (!stdout) { - perror("Couldn't open a new stdout"); - goto close_fd; - } +/* + * Note that the plan is to skip all tests + */ +int +plan_skip_all(char *reason) +{ + + LOCK; + + _tap_init(); + + skip_all = 1; + + printf("1..0"); + + if(reason != NULL) + printf(" # Skip %s", reason); + + printf("\n"); + + UNLOCK; + + exit(0); +} + +/* + * Note the number of tests that will be run. + */ +int +plan_tests(unsigned int tests) +{ + + LOCK; - fd = dup(pipefd[1]); - if (fd != STDERR_FILENO) { - fprintf(stderr, "# Failed to open a new stderr!\n"); - goto close_fd; + _tap_init(); + + if(have_plan != 0) { + fprintf(stderr, "You tried to plan twice!\n"); + test_died = 1; + UNLOCK; + exit(255); } - stderr = fdopen(fd, "w"); - if (!stderr) { - perror("Couldn't open a new stderr"); - goto close_fd; + if(tests == 0) { + fprintf(stderr, "You said to run 0 tests! You've got to run something.\n"); + test_died = 1; + UNLOCK; + exit(255); } - setlinebuf(stdout); - setlinebuf(stderr); + have_plan = 1; + + _expected_tests(tests); + + UNLOCK; + + return e_tests; +} + +unsigned int +diag(char *fmt, ...) +{ + va_list ap; + + fputs("# ", stderr); + + va_start(ap, fmt); + vfprintf(stderr, fmt, ap); + va_end(ap); + fputs("\n", stderr); - return; + return 0; +} + +void +_expected_tests(unsigned int tests) +{ -close_fd: - close(fd); + printf("1..%d\n", tests); + e_tests = tests; +} + +int +skip(unsigned int n, char *fmt, ...) +{ + va_list ap; + char *skip_msg; + + LOCK; + + va_start(ap, fmt); + asprintf(&skip_msg, fmt, ap); + va_end(ap); + + while(n-- > 0) { + test_count++; + printf("ok %d # skip %s\n", test_count, + skip_msg != NULL ? + skip_msg : "libtap():malloc() failed"); + } + + free(skip_msg); + + UNLOCK; + + return 1; +} -close_normal_stdout: - fclose(normal_stdout); +void +todo_start(char *fmt, ...) +{ + va_list ap; -close_dup_stdout: - close(new_stdout); + LOCK; -close_pipe_r_file: - fclose(pipe_r_file); + va_start(ap, fmt); + vasprintf(&todo_msg, fmt, ap); + va_end(ap); -close_pipe: - close(pipefd[0]); - close(pipefd[1]); + todo = 1; - return; + UNLOCK; } -void tap_plan(int count) +void +todo_end(void) { - printf("1..%d\n", count); - tap_count = 1; - tap_planned = count; + LOCK; - tap_comment_stdout(); + todo = 0; + free(todo_msg); + UNLOCK; } -int tap_status(void) +int +exit_status(void) { - if (tap_passed == tap_planned) { - return 0; - } else { - return 1; + int r; + + LOCK; + + /* If there's no plan, just return the number of failures */ + if(no_plan || !have_plan) { + UNLOCK; + return failures; } + + /* Ran too many tests? Return the number of tests that were run + that shouldn't have been */ + if(e_tests < test_count) { + r = test_count - e_tests; + UNLOCK; + return r; + } + + /* Return the number of tests that failed + the number of tests + that weren't run */ + r = failures + e_tests - test_count; + UNLOCK; + + return r; } -void tap_ok(int bool, const char *format, ...) +/* + * Cleanup at the end of the run, produce any final output that might be + * required. + */ +void +_cleanup(void) { - va_list args; - char *ok_string = "_TAPok"; - char *not_ok_string = "_TAPnot ok"; - char string[4000]; - va_start(args, format); - vsprintf(string, format, args); - va_end(args); + LOCK; + + /* If plan_no_plan() wasn't called, and we don't have a plan, + and we're not skipping everything, then something happened + before we could produce any output */ + if(!no_plan && !have_plan && !skip_all) { + diag("Looks like your test died before it could output anything."); + UNLOCK; + return; + } + + if(test_died) { + diag("Looks like your test died just after %d.", test_count); + UNLOCK; + return; + } + + + /* No plan provided, but now we know how many tests were run, and can + print the header at the end */ + if(!skip_all && (no_plan || !have_plan)) { + printf("1..%d\n", test_count); + } + + if((have_plan && !no_plan) && e_tests < test_count) { + diag("Looks like you planned %d %s but ran %d extra.", + e_tests, e_tests == 1 ? "test" : "tests", test_count - e_tests); + UNLOCK; + return; + } + + if((have_plan || !no_plan) && e_tests > test_count) { + diag("Looks like you planned %d %s but only ran %d.", + e_tests, e_tests == 1 ? "test" : "tests", test_count); + UNLOCK; + return; + } - printf("%s %d - %s\n", bool ? ok_string : not_ok_string, - tap_count++, string); + if(failures) + diag("Looks like you failed %d %s of %d.", + failures, failures == 1 ? "test" : "tests", test_count); - if (bool) - tap_passed++; + UNLOCK; } diff --git a/tests/tap.h b/tests/tap.h index 7cfed700..0f059430 100644 --- a/tests/tap.h +++ b/tests/tap.h @@ -1,29 +1,89 @@ -/* Copyright (C) 2010 Nils Carlson +/*- + * Copyright (c) 2004 Nik Clayton + * All rights reserved. * - * This library is free software; you can redistribute it and/or - * modify it under the terms of the GNU Lesser General Public - * License as published by the Free Software Foundation; either - * version 2.1 of the License, or (at your option) any later version. + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. * - * This 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 - * Lesser General Public License for more details. - * - * You should have received a copy of the GNU Lesser General Public - * License along with this library; if not, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND + * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE + * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL + * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS + * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) + * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT + * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY + * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF + * SUCH DAMAGE. */ -#ifndef __TAP_H -#define __TAP_H -#include -#include +/* '## __VA_ARGS__' is a gcc'ism. C99 doesn't allow the token pasting + and requires the caller to add the final comma if they've ommitted + the optional arguments */ +#ifdef __GNUC__ +# define ok(e, test, ...) ((e) ? \ + _gen_result(1, __func__, __FILE__, __LINE__, \ + test, ## __VA_ARGS__) : \ + _gen_result(0, __func__, __FILE__, __LINE__, \ + test, ## __VA_ARGS__)) + +# define ok1(e) ((e) ? \ + _gen_result(1, __func__, __FILE__, __LINE__, "%s", #e) : \ + _gen_result(0, __func__, __FILE__, __LINE__, "%s", #e)) + +# define pass(test, ...) ok(1, test, ## __VA_ARGS__); +# define fail(test, ...) ok(0, test, ## __VA_ARGS__); + +# define skip_start(test, n, fmt, ...) \ + do { \ + if((test)) { \ + skip(n, fmt, ## __VA_ARGS__); \ + continue; \ + } +#elif __STDC_VERSION__ >= 199901L /* __GNUC__ */ +# define ok(e, ...) ((e) ? \ + _gen_result(1, __func__, __FILE__, __LINE__, \ + __VA_ARGS__) : \ + _gen_result(0, __func__, __FILE__, __LINE__, \ + __VA_ARGS__)) + +# define ok1(e) ((e) ? \ + _gen_result(1, __func__, __FILE__, __LINE__, "%s", #e) : \ + _gen_result(0, __func__, __FILE__, __LINE__, "%s", #e)) + +# define pass(...) ok(1, __VA_ARGS__); +# define fail(...) ok(0, __VA_ARGS__); + +# define skip_start(test, n, ...) \ + do { \ + if((test)) { \ + skip(n, __VA_ARGS__); \ + continue; \ + } +#else /* __STDC_VERSION__ */ +# error "Needs gcc or C99 compiler for variadic macros." +#endif /* __STDC_VERSION__ */ + +#define skip_end() } while(0); + +unsigned int _gen_result(int, const char *, char *, unsigned int, char *, ...); + +int plan_no_plan(void); +int plan_skip_all(char *); +int plan_tests(unsigned int); -void tap_plan(int count); +unsigned int diag(char *, ...); -void tap_ok(int bool, const char *format, ...); +int skip(unsigned int, char *, ...); -int tap_status(void); +void todo_start(char *, ...); +void todo_end(void); -#endif /* __TAP_H */ +int exit_status(void); -- 2.34.1