diff --git a/expat/tests/alloc_tests.c b/expat/tests/alloc_tests.c index 0914ebcc..ed87dad2 100644 --- a/expat/tests/alloc_tests.c +++ b/expat/tests/alloc_tests.c @@ -46,6 +46,7 @@ #include "common.h" #include "minicheck.h" #include "dummy.h" +#include "handlers.h" #include "alloc_tests.h" void @@ -93,6 +94,196 @@ START_TEST(test_alloc_parse_xdecl) { } END_TEST +/* As above, but with an encoding big enough to cause storing the + * version information to expand the string pool being used. + */ +START_TEST(test_alloc_parse_xdecl_2) { + const char *text + = "" + "Hello, world"; + int i; + const int max_alloc_count = 20; + + for (i = 0; i < max_alloc_count; i++) { + g_allocation_count = i; + XML_SetXmlDeclHandler(g_parser, dummy_xdecl_handler); + XML_SetUnknownEncodingHandler(g_parser, long_encoding_handler, NULL); + if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) + != XML_STATUS_ERROR) + break; + /* See comment in test_alloc_parse_xdecl() */ + alloc_teardown(); + alloc_setup(); + } + if (i == 0) + fail("Parse succeeded despite failing allocator"); + if (i == max_alloc_count) + fail("Parse failed with max allocations"); +} +END_TEST + +/* Test the effects of allocation failures on a straightforward parse */ +START_TEST(test_alloc_parse_pi) { + const char *text = "\n" + "\n" + "" + "Hello, world" + ""; + int i; + const int max_alloc_count = 15; + + for (i = 0; i < max_alloc_count; i++) { + g_allocation_count = i; + XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler); + if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) + != XML_STATUS_ERROR) + break; + /* See comment in test_alloc_parse_xdecl() */ + alloc_teardown(); + alloc_setup(); + } + if (i == 0) + fail("Parse succeeded despite failing allocator"); + if (i == max_alloc_count) + fail("Parse failed with max allocations"); +} +END_TEST + +START_TEST(test_alloc_parse_pi_2) { + const char *text = "\n" + "" + "Hello, world" + "\n" + ""; + int i; + const int max_alloc_count = 15; + + for (i = 0; i < max_alloc_count; i++) { + g_allocation_count = i; + XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler); + if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) + != XML_STATUS_ERROR) + break; + /* See comment in test_alloc_parse_xdecl() */ + alloc_teardown(); + alloc_setup(); + } + if (i == 0) + fail("Parse succeeded despite failing allocator"); + if (i == max_alloc_count) + fail("Parse failed with max allocations"); +} +END_TEST + +START_TEST(test_alloc_parse_pi_3) { + const char *text + = ""; + int i; + const int max_alloc_count = 20; + + for (i = 0; i < max_alloc_count; i++) { + g_allocation_count = i; + XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler); + if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) + != XML_STATUS_ERROR) + break; + /* See comment in test_alloc_parse_xdecl() */ + alloc_teardown(); + alloc_setup(); + } + if (i == 0) + fail("Parse succeeded despite failing allocator"); + if (i == max_alloc_count) + fail("Parse failed with max allocations"); +} +END_TEST + +START_TEST(test_alloc_parse_comment) { + const char *text = "\n" + "" + "Hi"; + int i; + const int max_alloc_count = 15; + + for (i = 0; i < max_alloc_count; i++) { + g_allocation_count = i; + XML_SetCommentHandler(g_parser, dummy_comment_handler); + if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) + != XML_STATUS_ERROR) + break; + /* See comment in test_alloc_parse_xdecl() */ + alloc_teardown(); + alloc_setup(); + } + if (i == 0) + fail("Parse succeeded despite failing allocator"); + if (i == max_alloc_count) + fail("Parse failed with max allocations"); +} +END_TEST + +START_TEST(test_alloc_parse_comment_2) { + const char *text = "\n" + "" + "Hello, world" + "" + ""; + int i; + const int max_alloc_count = 15; + + for (i = 0; i < max_alloc_count; i++) { + g_allocation_count = i; + XML_SetCommentHandler(g_parser, dummy_comment_handler); + if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) + != XML_STATUS_ERROR) + break; + /* See comment in test_alloc_parse_xdecl() */ + alloc_teardown(); + alloc_setup(); + } + if (i == 0) + fail("Parse succeeded despite failing allocator"); + if (i == max_alloc_count) + fail("Parse failed with max allocations"); +} +END_TEST + TCase * make_alloc_test_case(Suite *s) { TCase *tc_alloc = tcase_create("allocation tests"); @@ -101,6 +292,12 @@ make_alloc_test_case(Suite *s) { tcase_add_checked_fixture(tc_alloc, alloc_setup, alloc_teardown); tcase_add_test(tc_alloc, test_alloc_parse_xdecl); + tcase_add_test(tc_alloc, test_alloc_parse_xdecl_2); + tcase_add_test(tc_alloc, test_alloc_parse_pi); + tcase_add_test(tc_alloc, test_alloc_parse_pi_2); + tcase_add_test(tc_alloc, test_alloc_parse_pi_3); + tcase_add_test(tc_alloc, test_alloc_parse_comment); + tcase_add_test(tc_alloc, test_alloc_parse_comment_2); return tc_alloc; /* TEMPORARY: this will become a void function */ } diff --git a/expat/tests/handlers.c b/expat/tests/handlers.c index 25785af3..43b2ac08 100644 --- a/expat/tests/handlers.c +++ b/expat/tests/handlers.c @@ -389,6 +389,21 @@ MiscEncodingHandler(void *data, const XML_Char *encoding, XML_Encoding *info) { return XML_STATUS_OK; } +int XMLCALL +long_encoding_handler(void *userData, const XML_Char *encoding, + XML_Encoding *info) { + int i; + + UNUSED_P(userData); + UNUSED_P(encoding); + for (i = 0; i < 256; i++) + info->map[i] = i; + info->data = NULL; + info->convert = NULL; + info->release = NULL; + return XML_STATUS_OK; +} + /* External Entity Handlers */ int XMLCALL diff --git a/expat/tests/handlers.h b/expat/tests/handlers.h index 71f8b4da..f87fa503 100644 --- a/expat/tests/handlers.h +++ b/expat/tests/handlers.h @@ -147,6 +147,10 @@ extern int XMLCALL unknown_released_encoding_handler(void *data, extern int XMLCALL MiscEncodingHandler(void *data, const XML_Char *encoding, XML_Encoding *info); +extern int XMLCALL long_encoding_handler(void *userData, + const XML_Char *encoding, + XML_Encoding *info); + /* External Entity Handlers */ typedef struct ExtOption { diff --git a/expat/tests/runtests.c b/expat/tests/runtests.c index a62df02f..a6f8fb5b 100644 --- a/expat/tests/runtests.c +++ b/expat/tests/runtests.c @@ -76,211 +76,6 @@ XML_Parser g_parser = NULL; -/* As above, but with an encoding big enough to cause storing the - * version information to expand the string pool being used. - */ -static int XMLCALL -long_encoding_handler(void *userData, const XML_Char *encoding, - XML_Encoding *info) { - int i; - - UNUSED_P(userData); - UNUSED_P(encoding); - for (i = 0; i < 256; i++) - info->map[i] = i; - info->data = NULL; - info->convert = NULL; - info->release = NULL; - return XML_STATUS_OK; -} - -START_TEST(test_alloc_parse_xdecl_2) { - const char *text - = "" - "Hello, world"; - int i; - const int max_alloc_count = 20; - - for (i = 0; i < max_alloc_count; i++) { - g_allocation_count = i; - XML_SetXmlDeclHandler(g_parser, dummy_xdecl_handler); - XML_SetUnknownEncodingHandler(g_parser, long_encoding_handler, NULL); - if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) - != XML_STATUS_ERROR) - break; - /* See comment in test_alloc_parse_xdecl() */ - alloc_teardown(); - alloc_setup(); - } - if (i == 0) - fail("Parse succeeded despite failing allocator"); - if (i == max_alloc_count) - fail("Parse failed with max allocations"); -} -END_TEST - -/* Test the effects of allocation failures on a straightforward parse */ -START_TEST(test_alloc_parse_pi) { - const char *text = "\n" - "\n" - "" - "Hello, world" - ""; - int i; - const int max_alloc_count = 15; - - for (i = 0; i < max_alloc_count; i++) { - g_allocation_count = i; - XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler); - if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) - != XML_STATUS_ERROR) - break; - /* See comment in test_alloc_parse_xdecl() */ - alloc_teardown(); - alloc_setup(); - } - if (i == 0) - fail("Parse succeeded despite failing allocator"); - if (i == max_alloc_count) - fail("Parse failed with max allocations"); -} -END_TEST - -START_TEST(test_alloc_parse_pi_2) { - const char *text = "\n" - "" - "Hello, world" - "\n" - ""; - int i; - const int max_alloc_count = 15; - - for (i = 0; i < max_alloc_count; i++) { - g_allocation_count = i; - XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler); - if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) - != XML_STATUS_ERROR) - break; - /* See comment in test_alloc_parse_xdecl() */ - alloc_teardown(); - alloc_setup(); - } - if (i == 0) - fail("Parse succeeded despite failing allocator"); - if (i == max_alloc_count) - fail("Parse failed with max allocations"); -} -END_TEST - -START_TEST(test_alloc_parse_pi_3) { - const char *text - = ""; - int i; - const int max_alloc_count = 20; - - for (i = 0; i < max_alloc_count; i++) { - g_allocation_count = i; - XML_SetProcessingInstructionHandler(g_parser, dummy_pi_handler); - if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) - != XML_STATUS_ERROR) - break; - /* See comment in test_alloc_parse_xdecl() */ - alloc_teardown(); - alloc_setup(); - } - if (i == 0) - fail("Parse succeeded despite failing allocator"); - if (i == max_alloc_count) - fail("Parse failed with max allocations"); -} -END_TEST - -START_TEST(test_alloc_parse_comment) { - const char *text = "\n" - "" - "Hi"; - int i; - const int max_alloc_count = 15; - - for (i = 0; i < max_alloc_count; i++) { - g_allocation_count = i; - XML_SetCommentHandler(g_parser, dummy_comment_handler); - if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) - != XML_STATUS_ERROR) - break; - /* See comment in test_alloc_parse_xdecl() */ - alloc_teardown(); - alloc_setup(); - } - if (i == 0) - fail("Parse succeeded despite failing allocator"); - if (i == max_alloc_count) - fail("Parse failed with max allocations"); -} -END_TEST - -START_TEST(test_alloc_parse_comment_2) { - const char *text = "\n" - "" - "Hello, world" - "" - ""; - int i; - const int max_alloc_count = 15; - - for (i = 0; i < max_alloc_count; i++) { - g_allocation_count = i; - XML_SetCommentHandler(g_parser, dummy_comment_handler); - if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) - != XML_STATUS_ERROR) - break; - /* See comment in test_alloc_parse_xdecl() */ - alloc_teardown(); - alloc_setup(); - } - if (i == 0) - fail("Parse succeeded despite failing allocator"); - if (i == max_alloc_count) - fail("Parse failed with max allocations"); -} -END_TEST - static int XMLCALL external_entity_duff_loader(XML_Parser parser, const XML_Char *context, const XML_Char *base, const XML_Char *systemId, @@ -4130,12 +3925,6 @@ make_suite(void) { TCase *tc_accounting = tcase_create("accounting tests"); #endif - tcase_add_test(tc_alloc, test_alloc_parse_xdecl_2); - tcase_add_test(tc_alloc, test_alloc_parse_pi); - tcase_add_test(tc_alloc, test_alloc_parse_pi_2); - tcase_add_test(tc_alloc, test_alloc_parse_pi_3); - tcase_add_test(tc_alloc, test_alloc_parse_comment); - tcase_add_test(tc_alloc, test_alloc_parse_comment_2); tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_create_external_parser); tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_run_external_parser); tcase_add_test__ifdef_xml_dtd(tc_alloc, test_alloc_dtd_copy_default_atts);