diff options
author | Jakub Czapiga <mordijc@gmail.com> | 2024-01-30 11:12:03 +0100 |
---|---|---|
committer | Andreas Schneider <asn@cryptomilk.org> | 2024-02-03 10:28:06 +0100 |
commit | a74d9cd2ede76a3e815785dad1f0406f275da83c (patch) | |
tree | f6eeba2c4b470014ca6741c0cc7289dee35bb499 | |
parent | 4e9219815f3c4514b3440a06383952c0672ad7ad (diff) | |
download | cmocka-a74d9cd2ede76a3e815785dad1f0406f275da83c.zip cmocka-a74d9cd2ede76a3e815785dad1f0406f275da83c.tar.gz cmocka-a74d9cd2ede76a3e815785dad1f0406f275da83c.tar.bz2 |
Sanitize XML strings.
Replace characters with their XML string equivalents to allow for them
in the tests and groups names.
Reviewed-by: Andreas Schneider <asn@cryptomilk.org>
(cherry picked from commit 90315882d6de183ab31b66b5008d4ca3d1fea140)
-rw-r--r-- | src/cmocka.c | 25 | ||||
-rw-r--r-- | tests/CMakeLists.txt | 4 | ||||
-rw-r--r-- | tests/test_groups.c | 2 |
3 files changed, 26 insertions, 5 deletions
diff --git a/src/cmocka.c b/src/cmocka.c index 723b8bf..6e87d39 100644 --- a/src/cmocka.c +++ b/src/cmocka.c @@ -2436,6 +2436,16 @@ enum cm_printf_type { static int xml_printed; static int file_append; +static void cmprepare_xml_attribute_string(char *buf, size_t buf_len, const char *src) +{ + snprintf(buf, buf_len, "%s", src); + c_strreplace(buf, buf_len, "&", "&", NULL); + c_strreplace(buf, buf_len, "\"", """, NULL); + c_strreplace(buf, buf_len, "\'", "'", NULL); + c_strreplace(buf, buf_len, "<", "<", NULL); + c_strreplace(buf, buf_len, ">", ">", NULL); +} + static void cmprintf_group_finish_xml(const char *group_name, size_t total_executed, size_t total_failed, @@ -2494,10 +2504,15 @@ static void cmprintf_group_finish_xml(const char *group_name, } } + char group_name_escaped[1024]; + cmprepare_xml_attribute_string(group_name_escaped, + sizeof(group_name_escaped), + group_name); + fprintf(fp, "<testsuites>\n"); fprintf(fp, " <testsuite name=\"%s\" time=\"%.3f\" " "tests=\"%u\" failures=\"%u\" errors=\"%u\" skipped=\"%u\" >\n", - group_name, + group_name_escaped, total_runtime, /* seconds */ (unsigned)total_executed, (unsigned)total_failed, @@ -2507,8 +2522,14 @@ static void cmprintf_group_finish_xml(const char *group_name, for (i = 0; i < total_executed; i++) { struct CMUnitTestState *cmtest = &cm_tests[i]; + /* Escape double quotes and remove spaces in test name */ + char test_name_escaped[1024]; + cmprepare_xml_attribute_string(test_name_escaped, + sizeof(test_name_escaped), + cmtest->test->name); + fprintf(fp, " <testcase name=\"%s\" time=\"%.3f\" >\n", - cmtest->test->name, cmtest->runtime); + test_name_escaped, cmtest->runtime); switch (cmtest->status) { case CM_TEST_ERROR: diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9c56437..9d7b59d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -190,7 +190,7 @@ set(test_groups_tap_out "# ok - test_group1" "1\\.\\.1" "ok 1 - int_test_success" - "# ok - test_group2") + "# ok - test_group2 num=<'\"2&2'\">") set(test_skip_tap_out "ok 1 # SKIP") set(test_setup_fail_tap_out @@ -223,7 +223,7 @@ set(test_groups_xml_out "<testcase name=\"null_test_success\" time=\"[0-9.]+\" >" "</testcase>" "</testsuite>" - ".*<testsuite name=\"test_group2\" time=\"[0-9.]+\" tests=\"1\" failures=\"0\" errors=\"0\" skipped=\"0\" >" + ".*<testsuite name=\"test_group2 num=<[;]&apos[;]"[;]2&[;]2&apos[;]"[;]>[;]\" time=\"[0-9.]+\" tests=\"1\" failures=\"0\" errors=\"0\" skipped=\"0\" >" "<testcase name=\"int_test_success\" time=\"[0-9.]+\" >" "</testcase>" "</testsuite>" diff --git a/tests/test_groups.c b/tests/test_groups.c index af9e2b8..f56db03 100644 --- a/tests/test_groups.c +++ b/tests/test_groups.c @@ -63,7 +63,7 @@ int main(void) { int result = 0; result += cmocka_run_group_tests(test_group1, NULL, NULL); - result += cmocka_run_group_tests(test_group2, NULL, NULL); + result += cmocka_run_group_tests_name("test_group2 num=<'\"2&2'\">", test_group2, NULL, NULL); return result; } |