From 67f238b79452c9399f19f514a6d685be603c00e2 Mon Sep 17 00:00:00 2001 From: Rhodri James Date: Fri, 28 Apr 2017 18:18:29 +0100 Subject: [PATCH] Refactor context URI reallocation tests --- expat/tests/runtests.c | 123 ++++++++++++----------------------------- 1 file changed, 35 insertions(+), 88 deletions(-) diff --git a/expat/tests/runtests.c b/expat/tests/runtests.c index 178f8d89..330bf875 100644 --- a/expat/tests/runtests.c +++ b/expat/tests/runtests.c @@ -9073,6 +9073,37 @@ external_entity_optioner(XML_Parser parser, return XML_STATUS_ERROR; } +/* This function is void; it will throw a fail() on error, so if it + * returns normally it must have succeeded. + */ +static void +context_realloc_test(XML_Parser parser, const char *text) +{ + ExtOption options[] = { + { "foo", ""}, + { "bar", "" }, + { NULL, NULL } + }; + int i; +#define MAX_REALLOC_COUNT 5 + + for (i = 0; i < MAX_REALLOC_COUNT; i++) { + reallocation_count = i; + XML_SetUserData(parser, options); + XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS); + XML_SetExternalEntityRefHandler(parser, external_entity_optioner); + if (_XML_Parse_SINGLE_BYTES(parser, text, strlen(text), + XML_TRUE) != XML_STATUS_ERROR) + break; + XML_ParserReset(parser, NULL); + } + if (i == 0) + fail("Parsing worked despite failing reallocations"); + else if (i == MAX_REALLOC_COUNT) + fail("Parsing failed even at max reallocation count"); +#undef MAX_REALLOC_COUNT +} + START_TEST(test_nsalloc_realloc_long_context) { const char *text = @@ -9100,30 +9131,9 @@ START_TEST(test_nsalloc_realloc_long_context) "'>\n" "&en;" ""; - ExtOption options[] = { - { "foo", ""}, - { "bar", "" }, - { NULL, NULL } - }; - int i; -#define MAX_REALLOC_COUNT 5 - for (i = 0; i < MAX_REALLOC_COUNT; i++) { - reallocation_count = i; - XML_SetUserData(parser, options); - XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS); - XML_SetExternalEntityRefHandler(parser, external_entity_optioner); - if (_XML_Parse_SINGLE_BYTES(parser, text, strlen(text), - XML_TRUE) != XML_STATUS_ERROR) - break; - XML_ParserReset(parser, NULL); - } - if (i == 0) - fail("Parsing worked despite failing reallocations"); - else if (i == MAX_REALLOC_COUNT) - fail("Parsing failed even at max reallocation count"); + context_realloc_test(parser, text); } -#undef MAX_REALLOC_COUNT END_TEST START_TEST(test_nsalloc_realloc_long_context_2) @@ -9153,30 +9163,9 @@ START_TEST(test_nsalloc_realloc_long_context_2) "'>\n" "&en;" ""; - ExtOption options[] = { - { "foo", ""}, - { "bar", "" }, - { NULL, NULL } - }; - int i; -#define MAX_REALLOC_COUNT 5 - for (i = 0; i < MAX_REALLOC_COUNT; i++) { - reallocation_count = i; - XML_SetUserData(parser, options); - XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS); - XML_SetExternalEntityRefHandler(parser, external_entity_optioner); - if (_XML_Parse_SINGLE_BYTES(parser, text, strlen(text), - XML_TRUE) != XML_STATUS_ERROR) - break; - XML_ParserReset(parser, NULL); - } - if (i == 0) - fail("Parsing worked despite failing reallocations"); - else if (i == MAX_REALLOC_COUNT) - fail("Parsing failed even at max reallocation count"); + context_realloc_test(parser, text); } -#undef MAX_REALLOC_COUNT END_TEST START_TEST(test_nsalloc_realloc_long_context_3) @@ -9206,30 +9195,9 @@ START_TEST(test_nsalloc_realloc_long_context_3) "'>\n" "&en;" ""; - ExtOption options[] = { - { "foo", ""}, - { "bar", "" }, - { NULL, NULL } - }; - int i; -#define MAX_REALLOC_COUNT 5 - for (i = 0; i < MAX_REALLOC_COUNT; i++) { - reallocation_count = i; - XML_SetUserData(parser, options); - XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS); - XML_SetExternalEntityRefHandler(parser, external_entity_optioner); - if (_XML_Parse_SINGLE_BYTES(parser, text, strlen(text), - XML_TRUE) != XML_STATUS_ERROR) - break; - XML_ParserReset(parser, NULL); - } - if (i == 0) - fail("Parsing worked despite failing reallocations"); - else if (i == MAX_REALLOC_COUNT) - fail("Parsing failed even at max reallocation count"); + context_realloc_test(parser, text); } -#undef MAX_REALLOC_COUNT END_TEST START_TEST(test_nsalloc_realloc_long_context_4) @@ -9259,30 +9227,9 @@ START_TEST(test_nsalloc_realloc_long_context_4) "'>\n" "&en;" ""; - ExtOption options[] = { - { "foo", ""}, - { "bar", "" }, - { NULL, NULL } - }; - int i; -#define MAX_REALLOC_COUNT 5 - for (i = 0; i < MAX_REALLOC_COUNT; i++) { - reallocation_count = i; - XML_SetUserData(parser, options); - XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS); - XML_SetExternalEntityRefHandler(parser, external_entity_optioner); - if (_XML_Parse_SINGLE_BYTES(parser, text, strlen(text), - XML_TRUE) != XML_STATUS_ERROR) - break; - XML_ParserReset(parser, NULL); - } - if (i == 0) - fail("Parsing worked despite failing reallocations"); - else if (i == MAX_REALLOC_COUNT) - fail("Parsing failed even at max reallocation count"); + context_realloc_test(parser, text); } -#undef MAX_REALLOC_COUNT END_TEST