From f7ead041e8e773165c6c5fd8fb672179d9b8534d Mon Sep 17 00:00:00 2001 From: hugo303 Date: Thu, 11 Oct 2012 09:12:18 +0000 Subject: [PATCH] * Make XML output well-formed git-svn-id: svn+ssh://svn.code.sf.net/p/check/code/trunk@625 64e312b2-a51f-0410-8e61-82d0ca0eb02a --- NEWS | 3 +++ src/check_log.c | 4 +++- src/check_print.c | 39 +++++++++++++++++++++++++++++++++++++-- src/check_print.h | 2 ++ tests/ex_xml_output.c | 23 +++++++++++++++++++++++ tests/test_xml_output.sh | 10 ++++++++++ 6 files changed, 78 insertions(+), 3 deletions(-) diff --git a/NEWS b/NEWS index 80e141c..46c55e3 100644 --- a/NEWS +++ b/NEWS @@ -1,5 +1,8 @@ In development. +* Make XML output well-formed, solution from patch #3575642 on SF. + Solves bug #3485651 also. + * Fix buggy duration calculation, bug #3575451 on SF. * A more complete CMake / MSVC patch for those interested in pursuing diff --git a/src/check_log.c b/src/check_log.c index 7be1b42..c865d2c 100644 --- a/src/check_log.c +++ b/src/check_log.c @@ -258,7 +258,9 @@ void xml_lfun (SRunner *sr CK_ATTRIBUTE_UNUSED, FILE *file, enum print_output pr case CLSTART_S: s = obj; fprintf(file, " \n"); - fprintf(file, " %s\n", s->name); + fprintf(file, " ", s->name); + fprint_xml_esc(file, s->name); + fprintf(file,"\n", s->name); break; case CLEND_SR: break; diff --git a/src/check_print.c b/src/check_print.c index 09f0bee..7fd1e17 100644 --- a/src/check_print.c +++ b/src/check_print.c @@ -88,6 +88,37 @@ static void srunner_fprint_results (FILE *file, SRunner *sr, return; } +void fprint_xml_esc(FILE *file, const char *str) +{ + for (; *str != '\0'; str++) { + + switch (*str) { + + /* handle special characters that must be escaped */ + case '"': + fputs(""", file); + break; + case '\'': + fputs("'", file); + break; + case '<': + fputs("<", file); + break; + case '>': + fputs(">", file); + break; + case '&': + fputs("&", file); + break; + + /* regular characters, print as is */ + default: + fputc(*str, file); + break; + } + } +} + void tr_fprint (FILE *file, TestResult *tr, enum print_output print_mode) { if (print_mode == CK_ENV) { @@ -143,8 +174,12 @@ void tr_xmlprint (FILE *file, TestResult *tr, enum print_output print_mode CK_AT fprintf(file, " %s:%d\n", file_name, tr->line); fprintf(file, " %s\n", tr->tname); fprintf(file, " %d\n", tr->iter); - fprintf(file, " %s\n", tr->tcname); - fprintf(file, " %s\n", tr->msg); + fprintf(file, " "); + fprint_xml_esc(file, tr->tcname); + fprintf(file,"\n", tr->tcname); + fprintf(file, " ", tr->msg); + fprint_xml_esc(file, tr->msg); + fprintf(file,"\n", tr->msg); fprintf(file, " \n"); if (slash != NULL) { diff --git a/src/check_print.h b/src/check_print.h index f4b02da..f4ab08d 100644 --- a/src/check_print.h +++ b/src/check_print.h @@ -21,6 +21,8 @@ #ifndef CHECK_PRINT_H #define CHECK_PRINT_H +/* escape XML special characters (" ' < > &) in str and print to file */ +void fprint_xml_esc(FILE *file, const char *str); void tr_fprint (FILE *file, TestResult *tr, enum print_output print_mode); void tr_xmlprint (FILE *file, TestResult *tr, enum print_output print_mode); void srunner_fprint (FILE *file, SRunner *sr, enum print_output print_mode); diff --git a/tests/ex_xml_output.c b/tests/ex_xml_output.c index 07f52d2..7b640d1 100644 --- a/tests/ex_xml_output.c +++ b/tests/ex_xml_output.c @@ -35,6 +35,12 @@ START_TEST(test_loop) } END_TEST +START_TEST(test_xml_esc_fail_msg) +{ + fail("fail \" ' < > & message"); +} +END_TEST + static Suite *make_s1_suite (void) { Suite *s; @@ -64,12 +70,29 @@ static Suite *make_s2_suite (void) return s; } +/* check that XML special characters are properly escaped in XML log file */ +static Suite *make_xml_esc_suite (void) +{ + Suite *s; + TCase *tc; + + s = suite_create("XML escape \" ' < > & tests"); + tc = tcase_create ("description \" ' < > &"); + suite_add_tcase(s, tc); + + tcase_add_test (tc, test_xml_esc_fail_msg); + + return s; +} + static void run_tests (int printmode) { SRunner *sr; sr = srunner_create(make_s1_suite()); srunner_add_suite(sr, make_s2_suite()); + srunner_add_suite(sr, make_xml_esc_suite()); + srunner_set_xml(sr, "test.log.xml"); srunner_run_all(sr, printmode); srunner_free(sr); diff --git a/tests/test_xml_output.sh b/tests/test_xml_output.sh index 3cb4031..39fbc02 100755 --- a/tests/test_xml_output.sh +++ b/tests/test_xml_output.sh @@ -64,6 +64,16 @@ expected=" Iteration 2 failed + + XML escape " ' < > & tests + + ex_xml_output.c:40 + test_xml_esc_fail_msg + 0 + description " ' < > & + fail " ' < > & message + + " ./ex_xml_output > /dev/null -- 2.40.0