From 5b9e8cdb5fe2097c0f274dd510aaada00979100f Mon Sep 17 00:00:00 2001 From: zdenekc Date: Wed, 1 Jun 2011 09:41:28 +0000 Subject: [PATCH] * fix failed test exit in no-fork mode git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@603 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- NEWS | 3 +++ src/check.c | 2 ++ src/check_error.c | 6 ++++++ src/check_error.h | 4 ++++ src/check_run.c | 25 +++++++++++++++++++------ tests/Makefile.am | 5 +++++ tests/check_nofork.c | 37 +++++++++++++++++++++++++++++++++++++ tests/test_check_nofork.sh | 14 ++++++++++++++ 8 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 tests/check_nofork.c create mode 100755 tests/test_check_nofork.sh diff --git a/NEWS b/NEWS index dcdaa50..0b4a56b 100644 --- a/NEWS +++ b/NEWS @@ -10,6 +10,9 @@ In development. * Added xslt link to xml output, added display of iteration field into xslt stylesheet and moved it to directory accessible from web page root +* Added longjmp to fail function to ensure that no code will be executed in test + function after failed assertion + Tue, Sep 22, 2009: Released Check 0.9.8 based on r559 (2009-09-23 21:00). diff --git a/src/check.c b/src/check.c index 1126dd4..f6c2f0e 100644 --- a/src/check.c +++ b/src/check.c @@ -250,6 +250,8 @@ void _fail_unless (int result, const char *file, #ifdef _POSIX_VERSION exit(1); #endif /* _POSIX_VERSION */ + } else { + longjmp(error_jmp_buffer, 1); } } } diff --git a/src/check_error.c b/src/check_error.c index 5ed0c1d..588f616 100644 --- a/src/check_error.c +++ b/src/check_error.c @@ -25,9 +25,15 @@ #include #include #include +#include #include "check_error.h" +/** + * Storage for setjmp/longjmp context information used in NOFORK mode + */ +jmp_buf error_jmp_buffer; + /* FIXME: including a colon at the end is a bad way to indicate an error */ void eprintf (const char *fmt, const char *file, int line, ...) diff --git a/src/check_error.h b/src/check_error.h index 2e1e211..75be45d 100644 --- a/src/check_error.h +++ b/src/check_error.h @@ -21,6 +21,10 @@ #ifndef ERROR_H #define ERROR_H +#include + +extern jmp_buf error_jmp_buffer; + /* Include stdlib.h beforehand */ /* Print error message and die diff --git a/src/check_run.c b/src/check_run.c index 92ac937..979a748 100644 --- a/src/check_run.c +++ b/src/check_run.c @@ -26,6 +26,7 @@ #include #include #include +#include #include "check.h" #include "check_error.h" @@ -48,6 +49,7 @@ enum tf_type { CK_NOFORK_FIXTURE }; + /* all functions are defined in the same order they are declared. functions that depend on forking are gathered all together. non-static functions are at the end of the file. */ @@ -216,7 +218,10 @@ static int srunner_run_unchecked_setup (SRunner *sr, TCase *tc) for (list_front(l); !list_at_end(l); list_advance(l)) { send_ctx_info(CK_CTX_SETUP); f = list_val(l); - f->fun(); + + if ( 0 == setjmp(error_jmp_buffer) ) { + f->fun(); + } tr = receive_result_info_nofork (tc->name, "unchecked_setup", 0); @@ -240,18 +245,24 @@ static TestResult * tcase_run_checked_setup (SRunner *sr, TCase *tc) List *l; Fixture *f; enum fork_status fstat = srunner_fork_status(sr); - + l = tc->ch_sflst; if (fstat == CK_FORK) { send_ctx_info(CK_CTX_SETUP); } - + for (list_front(l); !list_at_end(l); list_advance(l)) { + f = list_val(l); + if (fstat == CK_NOFORK) { send_ctx_info(CK_CTX_SETUP); + + if ( 0 == setjmp(error_jmp_buffer) ) { + f->fun(); + } + } else { + f->fun(); } - f = list_val(l); - f->fun(); /* Stop the setup and return the failure if nofork mode. */ if (fstat == CK_NOFORK) { @@ -305,7 +316,9 @@ static TestResult *tcase_run_tfun_nofork (SRunner *sr, TCase *tc, TF *tfun, int tr = tcase_run_checked_setup(sr, tc); if (tr == NULL) { - tfun->fn(i); + if ( 0 == setjmp(error_jmp_buffer) ) { + tfun->fn(i); + } tcase_run_checked_teardown(tc); return receive_result_info_nofork(tc->name, tfun->name, i); } diff --git a/tests/Makefile.am b/tests/Makefile.am index a68439a..f00c1f2 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -4,6 +4,7 @@ TESTS = \ check_check_export \ check_check \ test_output.sh \ + test_check_nofork.sh \ test_xml_output.sh \ test_log_output.sh @@ -20,6 +21,7 @@ noinst_PROGRAMS = \ check_check \ check_stress \ check_thread_stress \ + check_nofork \ ex_output \ ex_xml_output \ ex_log_output @@ -64,6 +66,9 @@ check_thread_stress_SOURCES = check_thread_stress.c check_thread_stress_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcompat.la @PTHREAD_LIBS@ check_thread_stress_CFLAGS = @PTHREAD_CFLAGS@ +check_nofork_SOURCES = check_nofork.c +check_nofork_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcompat.la + ex_output_SOURCES = ex_output.c ex_output_LDADD = $(top_builddir)/src/libcheck.la $(top_builddir)/lib/libcompat.la diff --git a/tests/check_nofork.c b/tests/check_nofork.c new file mode 100644 index 0000000..f7310d7 --- /dev/null +++ b/tests/check_nofork.c @@ -0,0 +1,37 @@ +#include "../lib/libcompat.h" + +#include +#include +#include + + +Suite *s; +TCase *tc; +SRunner *sr; + +START_TEST(test_nofork_exit) +{ + char* s = NULL; + + ck_assert(NULL != s); + + /* this test should not crash in nofork mode */ + ck_assert_str_eq("test", s); +} +END_TEST + +int main(void) +{ + s = suite_create("NoFork"); + tc = tcase_create("Exit"); + sr = srunner_create(s); + + suite_add_tcase(s, tc); + tcase_add_test(tc, test_nofork_exit); + + srunner_set_fork_status(sr, CK_NOFORK); + srunner_run_all(sr, CK_MINIMAL); + srunner_free(sr); + + return 0; +} diff --git a/tests/test_check_nofork.sh b/tests/test_check_nofork.sh new file mode 100755 index 0000000..62276e9 --- /dev/null +++ b/tests/test_check_nofork.sh @@ -0,0 +1,14 @@ +#!/bin/sh + +expected="Running suite(s): NoFork +0%: Checks: 1, Failures: 1, Errors: 0" + +actual=`./check_nofork 2>&1` +if [ x"${expected}" = x"${actual}" ]; then + exit 0 +else + echo "Problem with check_nofork" + echo "Expected: Failure" + echo "Got: Segmentation fault" + exit 1 +fi -- 2.49.0