From 9bece1957fc66f45af050f65a7dbb447af840e27 Mon Sep 17 00:00:00 2001 From: Rhodri James Date: Fri, 28 Oct 2022 20:45:35 +0100 Subject: [PATCH] Move (some) epilog tests out of runtests.c --- expat/tests/basic_tests.c | 45 +++++++++++++++++++++++++++++++ expat/tests/handlers.c | 11 ++++++++ expat/tests/handlers.h | 4 +++ expat/tests/runtests.c | 56 --------------------------------------- 4 files changed, 60 insertions(+), 56 deletions(-) diff --git a/expat/tests/basic_tests.c b/expat/tests/basic_tests.c index 1417b512..da2a6ca4 100644 --- a/expat/tests/basic_tests.c +++ b/expat/tests/basic_tests.c @@ -3256,6 +3256,48 @@ START_TEST(test_suspend_xdecl) { } END_TEST +/* Test aborting the parse in an epilog works */ +START_TEST(test_abort_epilog) { + const char *text = "\n\r\n"; + XML_Char match[] = XCS("\r"); + + XML_SetDefaultHandler(g_parser, selective_aborting_default_handler); + XML_SetUserData(g_parser, match); + g_resumable = XML_FALSE; + if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) + != XML_STATUS_ERROR) + fail("Abort not triggered"); + if (XML_GetErrorCode(g_parser) != XML_ERROR_ABORTED) + xml_failure(g_parser); +} +END_TEST + +/* Test a different code path for abort in the epilog */ +START_TEST(test_abort_epilog_2) { + const char *text = "\n"; + XML_Char match[] = XCS("\n"); + + XML_SetDefaultHandler(g_parser, selective_aborting_default_handler); + XML_SetUserData(g_parser, match); + g_resumable = XML_FALSE; + expect_failure(text, XML_ERROR_ABORTED, "Abort not triggered"); +} +END_TEST + +/* Test suspension from the epilog */ +START_TEST(test_suspend_epilog) { + const char *text = "\n"; + XML_Char match[] = XCS("\n"); + + XML_SetDefaultHandler(g_parser, selective_aborting_default_handler); + XML_SetUserData(g_parser, match); + g_resumable = XML_TRUE; + if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) + != XML_STATUS_SUSPENDED) + xml_failure(g_parser); +} +END_TEST + TCase * make_basic_test_case(Suite *s) { TCase *tc_basic = tcase_create("basic tests"); @@ -3402,6 +3444,9 @@ make_basic_test_case(Suite *s) { test_recursive_external_parameter_entity); tcase_add_test(tc_basic, test_undefined_ext_entity_in_external_dtd); tcase_add_test(tc_basic, test_suspend_xdecl); + tcase_add_test(tc_basic, test_abort_epilog); + tcase_add_test(tc_basic, test_abort_epilog_2); + tcase_add_test(tc_basic, test_suspend_epilog); return tc_basic; /* TEMPORARY: this will become a void function */ } diff --git a/expat/tests/handlers.c b/expat/tests/handlers.c index bd4965db..8e669d91 100644 --- a/expat/tests/handlers.c +++ b/expat/tests/handlers.c @@ -1145,3 +1145,14 @@ data_check_comment_handler(void *userData, const XML_Char *data) { fail("User data in parser not correctly set"); g_comment_count++; } + +void XMLCALL +selective_aborting_default_handler(void *userData, const XML_Char *s, int len) { + const XML_Char *match = (const XML_Char *)userData; + + if (match == NULL + || (xcstrlen(match) == (unsigned)len && ! xcstrncmp(match, s, len))) { + XML_StopParser(g_parser, g_resumable); + XML_SetDefaultHandler(g_parser, NULL); + } +} diff --git a/expat/tests/handlers.h b/expat/tests/handlers.h index e81d05b7..c54ad0fa 100644 --- a/expat/tests/handlers.h +++ b/expat/tests/handlers.h @@ -357,6 +357,10 @@ extern void XMLCALL param_check_skip_handler(void *userData, extern void XMLCALL data_check_comment_handler(void *userData, const XML_Char *data); +extern void XMLCALL selective_aborting_default_handler(void *userData, + const XML_Char *s, + int len); + #endif /* XML_HANDLERS_H */ #ifdef __cplusplus diff --git a/expat/tests/runtests.c b/expat/tests/runtests.c index d9f29fc6..e6eaef87 100644 --- a/expat/tests/runtests.c +++ b/expat/tests/runtests.c @@ -74,59 +74,6 @@ XML_Parser g_parser = NULL; -/* Test aborting the parse in an epilog works */ -static void XMLCALL -selective_aborting_default_handler(void *userData, const XML_Char *s, int len) { - const XML_Char *match = (const XML_Char *)userData; - - if (match == NULL - || (xcstrlen(match) == (unsigned)len && ! xcstrncmp(match, s, len))) { - XML_StopParser(g_parser, g_resumable); - XML_SetDefaultHandler(g_parser, NULL); - } -} - -START_TEST(test_abort_epilog) { - const char *text = "\n\r\n"; - XML_Char match[] = XCS("\r"); - - XML_SetDefaultHandler(g_parser, selective_aborting_default_handler); - XML_SetUserData(g_parser, match); - g_resumable = XML_FALSE; - if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) - != XML_STATUS_ERROR) - fail("Abort not triggered"); - if (XML_GetErrorCode(g_parser) != XML_ERROR_ABORTED) - xml_failure(g_parser); -} -END_TEST - -/* Test a different code path for abort in the epilog */ -START_TEST(test_abort_epilog_2) { - const char *text = "\n"; - XML_Char match[] = XCS("\n"); - - XML_SetDefaultHandler(g_parser, selective_aborting_default_handler); - XML_SetUserData(g_parser, match); - g_resumable = XML_FALSE; - expect_failure(text, XML_ERROR_ABORTED, "Abort not triggered"); -} -END_TEST - -/* Test suspension from the epilog */ -START_TEST(test_suspend_epilog) { - const char *text = "\n"; - XML_Char match[] = XCS("\n"); - - XML_SetDefaultHandler(g_parser, selective_aborting_default_handler); - XML_SetUserData(g_parser, match); - g_resumable = XML_TRUE; - if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) - != XML_STATUS_SUSPENDED) - xml_failure(g_parser); -} -END_TEST - static void XMLCALL suspending_end_handler(void *userData, const XML_Char *s) { UNUSED_P(s); @@ -7317,9 +7264,6 @@ make_suite(void) { TCase *tc_accounting = tcase_create("accounting tests"); #endif - tcase_add_test(tc_basic, test_abort_epilog); - tcase_add_test(tc_basic, test_abort_epilog_2); - tcase_add_test(tc_basic, test_suspend_epilog); tcase_add_test(tc_basic, test_suspend_in_sole_empty_tag); tcase_add_test(tc_basic, test_unfinished_epilog); tcase_add_test(tc_basic, test_partial_char_in_epilog);