From 5dbc857f47ecd6c37748ce279c6535fbbf526590 Mon Sep 17 00:00:00 2001 From: Sebastian Pipping Date: Mon, 26 Apr 2021 15:12:53 +0200 Subject: [PATCH] tests: Cover helper unsignedCharToPrintable --- lib/internal.h | 1 + lib/xmlparse.c | 3 +-- tests/runtests.c | 20 ++++++++++++++++++++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/lib/internal.h b/lib/internal.h index 377c12b..444eba0 100644 --- a/lib/internal.h +++ b/lib/internal.h @@ -155,6 +155,7 @@ void _INTERNAL_trim_to_complete_utf8_characters(const char *from, #if defined(XML_DTD) unsigned long long testingAccountingGetCountBytesDirect(XML_Parser parser); unsigned long long testingAccountingGetCountBytesIndirect(XML_Parser parser); +const char *unsignedCharToPrintable(unsigned char c); #endif #ifdef __cplusplus diff --git a/lib/xmlparse.c b/lib/xmlparse.c index adaab23..a1aadd8 100644 --- a/lib/xmlparse.c +++ b/lib/xmlparse.c @@ -580,7 +580,6 @@ static void entityTrackingOnClose(XML_Parser parser, ENTITY *entity, static XML_Parser getRootParserOf(XML_Parser parser, unsigned int *outLevelDiff); -static const char *unsignedCharToPrintable(unsigned char c); #endif /* XML_DTD */ static unsigned long getDebugLevel(const char *variableName, @@ -7433,7 +7432,7 @@ getRootParserOf(XML_Parser parser, unsigned int *outLevelDiff) { return rootParser; } -static const char * +const char * unsignedCharToPrintable(unsigned char c) { switch (c) { case 0: diff --git a/tests/runtests.c b/tests/runtests.c index 8c5ad72..0e2b49f 100644 --- a/tests/runtests.c +++ b/tests/runtests.c @@ -11578,6 +11578,25 @@ START_TEST(test_billion_laughs_attack_protection_api) { XML_ParserFree(parserWithoutParent); } END_TEST + +START_TEST(test_helper_unsigned_char_to_printable) { + // Smoke test + unsigned char uc = 0; + for (; uc < (unsigned char)-1; uc++) { + const char *const printable = unsignedCharToPrintable(uc); + if (printable == NULL) + fail("unsignedCharToPrintable returned NULL"); + if (strlen(printable) < (size_t)1) + fail("unsignedCharToPrintable returned empty string"); + } + + // Two concrete samples + if (strcmp(unsignedCharToPrintable('A'), "A") != 0) + fail("unsignedCharToPrintable result mistaken"); + if (strcmp(unsignedCharToPrintable('\\'), "\\\\") != 0) + fail("unsignedCharToPrintable result mistaken"); +} +END_TEST #endif // defined(XML_DTD) static Suite * @@ -11955,6 +11974,7 @@ make_suite(void) { suite_add_tcase(s, tc_accounting); tcase_add_test(tc_accounting, test_accounting_precision); tcase_add_test(tc_accounting, test_billion_laughs_attack_protection_api); + tcase_add_test(tc_accounting, test_helper_unsigned_char_to_printable); #endif return s; -- 1.8.3.1