From b3d14b0d3c96700a81be06e7482d3735245611ca Mon Sep 17 00:00:00 2001 From: Snild Dolkow Date: Fri, 29 Sep 2023 12:25:19 +0200 Subject: [PATCH] tests: Remove choice between XML_Parse and SINGLE_BYTES Since commit 091ba48d ("tests: Run SINGLE_BYTES with no chunking"), all tests are run an extra time with SINGLE_BYTES set to perform just a single XML_Parse() call. There is no longer a need for individual tests to switch between them. --- expat/tests/acc_tests.c | 170 +++++++++++++++++----------------------- expat/tests/handlers.c | 5 +- expat/tests/handlers.h | 1 - 3 files changed, 75 insertions(+), 101 deletions(-) diff --git a/expat/tests/acc_tests.c b/expat/tests/acc_tests.c index 30801bee..f2dee34d 100644 --- a/expat/tests/acc_tests.c +++ b/expat/tests/acc_tests.c @@ -56,50 +56,48 @@ #if defined(XML_DTD) START_TEST(test_accounting_precision) { - const XML_Bool filled_later = XML_TRUE; /* value is arbitrary */ struct AccountingTestCase cases[] = { - {"", NULL, NULL, 0, 0}, - {"", NULL, NULL, 0, 0}, + {"", NULL, NULL, 0}, + {"", NULL, NULL, 0}, /* Attributes */ - {"", NULL, NULL, 0, filled_later}, - {"", NULL, NULL, 0, 0}, - {"", NULL, NULL, 0, - filled_later}, + {"", NULL, NULL, 0}, + {"", NULL, NULL, 0}, + {"", NULL, NULL, 0}, {"", NULL, NULL, - sizeof(XML_Char) * 5 /* number of predefined entities */, filled_later}, + sizeof(XML_Char) * 5 /* number of predefined entities */}, {"\n" " \n" "", - NULL, NULL, 0, filled_later}, + NULL, NULL, 0}, /* Text */ - {"text", NULL, NULL, 0, filled_later}, - {"text1text2", NULL, NULL, 0, filled_later}, + {"text", NULL, NULL, 0}, + {"text1text2", NULL, NULL, 0}, {"&'><"", NULL, NULL, - sizeof(XML_Char) * 5 /* number of predefined entities */, filled_later}, - {"A)", NULL, NULL, 0, filled_later}, + sizeof(XML_Char) * 5 /* number of predefined entities */}, + {"A)", NULL, NULL, 0}, /* Prolog */ - {"", NULL, NULL, 0, filled_later}, + {"", NULL, NULL, 0}, /* Whitespace */ - {" ", NULL, NULL, 0, filled_later}, - {"", NULL, NULL, 0, filled_later}, - {"", NULL, NULL, 0, filled_later}, + {" ", NULL, NULL, 0}, + {"", NULL, NULL, 0}, + {"", NULL, NULL, 0}, /* Comments */ - {"", NULL, NULL, 0, filled_later}, + {"", NULL, NULL, 0}, /* Processing instructions */ {"", - NULL, NULL, 0, filled_later}, + NULL, NULL, 0}, {"", "%e1;", "", - 0, filled_later}, + 0}, /* CDATA */ - {"", NULL, NULL, 0, filled_later}, + {"", NULL, NULL, 0}, /* The following is the essence of this OSS-Fuzz finding: https://bugs.chromium.org/p/oss-fuzz/issues/detail?id=34302 https://oss-fuzz.com/testcase-detail/4860575394955264 @@ -108,8 +106,7 @@ START_TEST(test_accounting_precision) { "333\">\n" "]>\n" "&e;\n", - NULL, NULL, sizeof(XML_Char) * strlen("111333"), - filled_later}, + NULL, NULL, sizeof(XML_Char) * strlen("111333")}, /* Conditional sections */ {"\n", "]]>\n" "]]>", - NULL, sizeof(XML_Char) * (strlen("INCLUDE") + strlen("IGNORE")), - filled_later}, + NULL, sizeof(XML_Char) * (strlen("INCLUDE") + strlen("IGNORE"))}, /* General entities */ {"\n" "]>\n" "&nine;", - NULL, NULL, sizeof(XML_Char) * strlen("123456789"), filled_later}, + NULL, NULL, sizeof(XML_Char) * strlen("123456789")}, {"\n" "]>\n" "", - NULL, NULL, sizeof(XML_Char) * strlen("123456789"), filled_later}, + NULL, NULL, sizeof(XML_Char) * strlen("123456789")}, {"\n" "\n" @@ -142,18 +138,17 @@ START_TEST(test_accounting_precision) { "&nine2;&nine2;&nine2;", NULL, NULL, sizeof(XML_Char) * 3 /* calls to &nine2; */ * 2 /* calls to &nine; */ - * (strlen("&nine;") + strlen("123456789")), - filled_later}, + * (strlen("&nine;") + strlen("123456789"))}, {"\n" "]>\n" "&five;", - "12345", NULL, 0, filled_later}, + "12345", NULL, 0}, {"\n" "]>\n" "&five;", - "\xEF\xBB\xBF" /* UTF-8 BOM */, NULL, 0, filled_later}, + "\xEF\xBB\xBF" /* UTF-8 BOM */, NULL, 0}, /* Parameter entities */ {"\n" "", - NULL, NULL, sizeof(XML_Char) * strlen(""), filled_later}, + NULL, NULL, sizeof(XML_Char) * strlen("")}, {"\n" "%ninedef;\n" @@ -169,8 +164,7 @@ START_TEST(test_accounting_precision) { "&nine;", NULL, NULL, sizeof(XML_Char) - * (strlen("") + strlen("123456789")), - filled_later}, + * (strlen("") + strlen("123456789"))}, {"\">\n" "%comment;\">\n" @@ -179,8 +173,7 @@ START_TEST(test_accounting_precision) { "\n", NULL, NULL, sizeof(XML_Char) - * (strlen("%comment;%comment;") + 2 * strlen("")), - filled_later}, + * (strlen("%comment;%comment;") + 2 * strlen(""))}, {"\n" " \n" @@ -191,8 +184,7 @@ START_TEST(test_accounting_precision) { sizeof(XML_Char) * (strlen("") + 2 /* calls to "%five;" */ * strlen("12345") - + /* from "&five2;": */ strlen("[12345][12345]]]]")), - filled_later}, + + /* from "&five2;": */ strlen("[12345][12345]]]]"))}, {"\n" "", "'>\n" @@ -201,15 +193,13 @@ START_TEST(test_accounting_precision) { NULL, sizeof(XML_Char) * (strlen("%comment;%comment;") - + 2 /* calls to "%comment;" */ * strlen("")), - filled_later}, + + 2 /* calls to "%comment;" */ * strlen(""))}, {"\n" "", "\n" "%e1;'>\n" "%e2;\n", - "", sizeof(XML_Char) * strlen(""), - filled_later}, + "", sizeof(XML_Char) * strlen("")}, { "\n" "", @@ -219,7 +209,6 @@ START_TEST(test_accounting_precision) { "hello\n" "xml" /* without trailing newline! */, 0, - filled_later, }, { "\n" @@ -230,7 +219,6 @@ START_TEST(test_accounting_precision) { "hello\n" "xml\n" /* with trailing newline! */, 0, - filled_later, }, {"\n" "\n", @@ -239,69 +227,59 @@ START_TEST(test_accounting_precision) { "\n" "%e1;\n", "\xEF\xBB\xBF" /* UTF-8 BOM */, - strlen("\xEF\xBB\xBF"), filled_later}, + strlen("\xEF\xBB\xBF")}, }; const size_t countCases = sizeof(cases) / sizeof(cases[0]); size_t u = 0; for (; u < countCases; u++) { - size_t v = 0; - for (; v < 2; v++) { - const XML_Bool singleBytesWanted = (v == 0) ? XML_FALSE : XML_TRUE; - const unsigned long long expectedCountBytesDirect - = strlen(cases[u].primaryText); - const unsigned long long expectedCountBytesIndirect - = (cases[u].firstExternalText ? strlen(cases[u].firstExternalText) - : 0) - + (cases[u].secondExternalText ? strlen(cases[u].secondExternalText) - : 0) - + cases[u].expectedCountBytesIndirectExtra; + const unsigned long long expectedCountBytesDirect + = strlen(cases[u].primaryText); + const unsigned long long expectedCountBytesIndirect + = (cases[u].firstExternalText ? strlen(cases[u].firstExternalText) : 0) + + (cases[u].secondExternalText ? strlen(cases[u].secondExternalText) + : 0) + + cases[u].expectedCountBytesIndirectExtra; - XML_Parser parser = XML_ParserCreate(NULL); - XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS); - if (cases[u].firstExternalText) { - XML_SetExternalEntityRefHandler(parser, - accounting_external_entity_ref_handler); - XML_SetUserData(parser, (void *)&cases[u]); - cases[u].singleBytesWanted = singleBytesWanted; - } + XML_Parser parser = XML_ParserCreate(NULL); + XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS); + if (cases[u].firstExternalText) { + XML_SetExternalEntityRefHandler(parser, + accounting_external_entity_ref_handler); + XML_SetUserData(parser, (void *)&cases[u]); + } - const XmlParseFunction xmlParseFunction - = singleBytesWanted ? _XML_Parse_SINGLE_BYTES : XML_Parse; + enum XML_Status status + = _XML_Parse_SINGLE_BYTES(parser, cases[u].primaryText, + (int)strlen(cases[u].primaryText), XML_TRUE); + if (status != XML_STATUS_OK) { + _xml_failure(parser, __FILE__, __LINE__); + } - enum XML_Status status - = xmlParseFunction(parser, cases[u].primaryText, - (int)strlen(cases[u].primaryText), XML_TRUE); - if (status != XML_STATUS_OK) { - _xml_failure(parser, __FILE__, __LINE__); - } + const unsigned long long actualCountBytesDirect + = testingAccountingGetCountBytesDirect(parser); + const unsigned long long actualCountBytesIndirect + = testingAccountingGetCountBytesIndirect(parser); - const unsigned long long actualCountBytesDirect - = testingAccountingGetCountBytesDirect(parser); - const unsigned long long actualCountBytesIndirect - = testingAccountingGetCountBytesIndirect(parser); + XML_ParserFree(parser); - XML_ParserFree(parser); + if (actualCountBytesDirect != expectedCountBytesDirect) { + fprintf( + stderr, + "Document " EXPAT_FMT_SIZE_T("") " of " EXPAT_FMT_SIZE_T("") ": Expected " EXPAT_FMT_ULL( + "") " count direct bytes, got " EXPAT_FMT_ULL("") " instead.\n", + u + 1, countCases, expectedCountBytesDirect, actualCountBytesDirect); + fail("Count of direct bytes is off"); + } - if (actualCountBytesDirect != expectedCountBytesDirect) { - fprintf( - stderr, - "Document " EXPAT_FMT_SIZE_T("") " of " EXPAT_FMT_SIZE_T("") ", %s: Expected " EXPAT_FMT_ULL( - "") " count direct bytes, got " EXPAT_FMT_ULL("") " instead.\n", - u + 1, countCases, singleBytesWanted ? "single bytes" : "chunks", - expectedCountBytesDirect, actualCountBytesDirect); - fail("Count of direct bytes is off"); - } - - if (actualCountBytesIndirect != expectedCountBytesIndirect) { - fprintf( - stderr, - "Document " EXPAT_FMT_SIZE_T("") " of " EXPAT_FMT_SIZE_T("") ", %s: Expected " EXPAT_FMT_ULL( - "") " count indirect bytes, got " EXPAT_FMT_ULL("") " instead.\n", - u + 1, countCases, singleBytesWanted ? "single bytes" : "chunks", - expectedCountBytesIndirect, actualCountBytesIndirect); - fail("Count of indirect bytes is off"); - } + if (actualCountBytesIndirect != expectedCountBytesIndirect) { + fprintf( + stderr, + "Document " EXPAT_FMT_SIZE_T("") " of " EXPAT_FMT_SIZE_T("") ": Expected " EXPAT_FMT_ULL( + "") " count indirect bytes, got " EXPAT_FMT_ULL("") " instead.\n", + u + 1, countCases, expectedCountBytesIndirect, + actualCountBytesIndirect); + fail("Count of indirect bytes is off"); } } } diff --git a/expat/tests/handlers.c b/expat/tests/handlers.c index 92f9e6ee..e2119fb9 100644 --- a/expat/tests/handlers.c +++ b/expat/tests/handlers.c @@ -1508,10 +1508,7 @@ accounting_external_entity_ref_handler(XML_Parser parser, XML_Parser entParser = XML_ExternalEntityParserCreate(parser, context, 0); assert(entParser); - const XmlParseFunction xmlParseFunction - = testCase->singleBytesWanted ? _XML_Parse_SINGLE_BYTES : XML_Parse; - - const enum XML_Status status = xmlParseFunction( + const enum XML_Status status = _XML_Parse_SINGLE_BYTES( entParser, externalText, (int)strlen(externalText), XML_TRUE); XML_ParserFree(entParser); diff --git a/expat/tests/handlers.h b/expat/tests/handlers.h index 373d24b0..bd0a3e16 100644 --- a/expat/tests/handlers.h +++ b/expat/tests/handlers.h @@ -415,7 +415,6 @@ struct AccountingTestCase { const char *firstExternalText; /* often NULL */ const char *secondExternalText; /* often NULL */ const unsigned long long expectedCountBytesIndirectExtra; - XML_Bool singleBytesWanted; }; extern int accounting_external_entity_ref_handler(XML_Parser parser,