diff options
| author | Xin LI <delphij@FreeBSD.org> | 2024-12-08 19:17:41 +0000 |
|---|---|---|
| committer | Xin LI <delphij@FreeBSD.org> | 2024-12-08 19:17:41 +0000 |
| commit | 7277ee24655bd9190ee1131666aebf6ed9d12390 (patch) | |
| tree | c20bba1607052f2bdb5a626c78e8c5dcf757e778 /tests | |
| parent | 8e1eae2319cd3a651941c88b46d95e8ee8507c6c (diff) | |
Diffstat (limited to 'tests')
| -rw-r--r-- | tests/basic_tests.c | 17 | ||||
| -rw-r--r-- | tests/common.c | 33 | ||||
| -rw-r--r-- | tests/common.h | 11 | ||||
| -rw-r--r-- | tests/handlers.c | 42 | ||||
| -rw-r--r-- | tests/handlers.h | 17 | ||||
| -rw-r--r-- | tests/misc_tests.c | 39 |
6 files changed, 95 insertions, 64 deletions
diff --git a/tests/basic_tests.c b/tests/basic_tests.c index 0d97b1090c7f..d38b8fd18416 100644 --- a/tests/basic_tests.c +++ b/tests/basic_tests.c @@ -2357,11 +2357,20 @@ START_TEST(test_attributes) { info[0].attributes = doc_info; info[1].attributes = tag_info; - XML_SetStartElementHandler(g_parser, counting_start_element_handler); - XML_SetUserData(g_parser, info); - if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE) + XML_Parser parser = XML_ParserCreate(NULL); + assert_true(parser != NULL); + ParserAndElementInfo parserAndElementInfos = { + parser, + info, + }; + + XML_SetStartElementHandler(parser, counting_start_element_handler); + XML_SetUserData(parser, &parserAndElementInfos); + if (_XML_Parse_SINGLE_BYTES(parser, text, (int)strlen(text), XML_TRUE) == XML_STATUS_ERROR) - xml_failure(g_parser); + xml_failure(parser); + + XML_ParserFree(parser); } END_TEST diff --git a/tests/common.c b/tests/common.c index 26d0c5473a63..3aea8d74d1ee 100644 --- a/tests/common.c +++ b/tests/common.c @@ -10,7 +10,7 @@ Copyright (c) 2003 Greg Stein <gstein@users.sourceforge.net> Copyright (c) 2005-2007 Steven Solie <steven@solie.ca> Copyright (c) 2005-2012 Karl Waclawek <karl@waclawek.net> - Copyright (c) 2016-2023 Sebastian Pipping <sebastian@pipping.org> + Copyright (c) 2016-2024 Sebastian Pipping <sebastian@pipping.org> Copyright (c) 2017-2022 Rhodri James <rhodri@wildebeest.org.uk> Copyright (c) 2017 Joe Orton <jorton@redhat.com> Copyright (c) 2017 José Gutiérrez de la Concha <jose@zeroc.com> @@ -51,6 +51,7 @@ #include "chardata.h" #include "minicheck.h" #include "common.h" +#include "handlers.h" /* Common test data */ @@ -221,30 +222,6 @@ _expect_failure(const char *text, enum XML_Error errorCode, _xml_failure(g_parser, file, lineno); } -/* Character data support for handlers, built on top of the code in - * chardata.c - */ -void XMLCALL -accumulate_characters(void *userData, const XML_Char *s, int len) { - CharData_AppendXMLChars((CharData *)userData, s, len); -} - -void XMLCALL -accumulate_attribute(void *userData, const XML_Char *name, - const XML_Char **atts) { - CharData *storage = (CharData *)userData; - UNUSED_P(name); - /* Check there are attributes to deal with */ - if (atts == NULL) - return; - - while (storage->count < 0 && atts[0] != NULL) { - /* "accumulate" the value of the first attribute we see */ - CharData_AppendXMLChars(storage, atts[1], -1); - atts += 2; - } -} - void _run_character_check(const char *text, const XML_Char *expected, const char *file, int line) { @@ -273,12 +250,6 @@ _run_attribute_check(const char *text, const XML_Char *expected, CharData_CheckXMLChars(&storage, expected); } -void XMLCALL -ext_accumulate_characters(void *userData, const XML_Char *s, int len) { - ExtTest *test_data = (ExtTest *)userData; - accumulate_characters(test_data->storage, s, len); -} - void _run_ext_character_check(const char *text, ExtTest *test_data, const XML_Char *expected, const char *file, int line) { diff --git a/tests/common.h b/tests/common.h index 52f00cc0eeb0..bc4c7da68071 100644 --- a/tests/common.h +++ b/tests/common.h @@ -10,7 +10,7 @@ Copyright (c) 2003 Greg Stein <gstein@users.sourceforge.net> Copyright (c) 2005-2007 Steven Solie <steven@solie.ca> Copyright (c) 2005-2012 Karl Waclawek <karl@waclawek.net> - Copyright (c) 2016-2023 Sebastian Pipping <sebastian@pipping.org> + Copyright (c) 2016-2024 Sebastian Pipping <sebastian@pipping.org> Copyright (c) 2017-2022 Rhodri James <rhodri@wildebeest.org.uk> Copyright (c) 2017 Joe Orton <jorton@redhat.com> Copyright (c) 2017 José Gutiérrez de la Concha <jose@zeroc.com> @@ -111,12 +111,6 @@ extern void _expect_failure(const char *text, enum XML_Error errorCode, /* Support functions for handlers to collect up character and attribute data. */ -extern void XMLCALL accumulate_characters(void *userData, const XML_Char *s, - int len); - -extern void XMLCALL accumulate_attribute(void *userData, const XML_Char *name, - const XML_Char **atts); - extern void _run_character_check(const char *text, const XML_Char *expected, const char *file, int line); @@ -135,9 +129,6 @@ typedef struct ExtTest { CharData *storage; } ExtTest; -extern void XMLCALL ext_accumulate_characters(void *userData, const XML_Char *s, - int len); - extern void _run_ext_character_check(const char *text, ExtTest *test_data, const XML_Char *expected, const char *file, int line); diff --git a/tests/handlers.c b/tests/handlers.c index 449ada70f9a2..0211985fe95c 100644 --- a/tests/handlers.c +++ b/tests/handlers.c @@ -103,7 +103,9 @@ end_element_event_handler2(void *userData, const XML_Char *name) { void XMLCALL counting_start_element_handler(void *userData, const XML_Char *name, const XML_Char **atts) { - ElementInfo *info = (ElementInfo *)userData; + ParserAndElementInfo *const parserAndElementInfos + = (ParserAndElementInfo *)userData; + ElementInfo *info = parserAndElementInfos->info; AttrInfo *attr; int count, id, i; @@ -120,12 +122,12 @@ counting_start_element_handler(void *userData, const XML_Char *name, * is possibly a little unexpected, but it is what the * documentation in expat.h tells us to expect. */ - count = XML_GetSpecifiedAttributeCount(g_parser); + count = XML_GetSpecifiedAttributeCount(parserAndElementInfos->parser); if (info->attr_count * 2 != count) { fail("Not got expected attribute count"); return; } - id = XML_GetIdAttributeIndex(g_parser); + id = XML_GetIdAttributeIndex(parserAndElementInfos->parser); if (id == -1 && info->id_name != NULL) { fail("ID not present"); return; @@ -1881,12 +1883,6 @@ accumulate_entity_decl(void *userData, const XML_Char *entityName, } void XMLCALL -accumulate_char_data(void *userData, const XML_Char *s, int len) { - CharData *const storage = (CharData *)userData; - CharData_AppendXMLChars(storage, s, len); -} - -void XMLCALL accumulate_start_element(void *userData, const XML_Char *name, const XML_Char **atts) { CharData *const storage = (CharData *)userData; @@ -1911,6 +1907,34 @@ accumulate_start_element(void *userData, const XML_Char *name, } void XMLCALL +accumulate_characters(void *userData, const XML_Char *s, int len) { + CharData *const storage = (CharData *)userData; + CharData_AppendXMLChars(storage, s, len); +} + +void XMLCALL +accumulate_attribute(void *userData, const XML_Char *name, + const XML_Char **atts) { + CharData *const storage = (CharData *)userData; + UNUSED_P(name); + /* Check there are attributes to deal with */ + if (atts == NULL) + return; + + while (storage->count < 0 && atts[0] != NULL) { + /* "accumulate" the value of the first attribute we see */ + CharData_AppendXMLChars(storage, atts[1], -1); + atts += 2; + } +} + +void XMLCALL +ext_accumulate_characters(void *userData, const XML_Char *s, int len) { + ExtTest *const test_data = (ExtTest *)userData; + accumulate_characters(test_data->storage, s, len); +} + +void XMLCALL checking_default_handler(void *userData, const XML_Char *s, int len) { DefaultCheck *data = (DefaultCheck *)userData; int i; diff --git a/tests/handlers.h b/tests/handlers.h index e1f0995f79e6..8850bb948da3 100644 --- a/tests/handlers.h +++ b/tests/handlers.h @@ -92,6 +92,11 @@ typedef struct elementInfo { AttrInfo *attributes; } ElementInfo; +typedef struct StructParserAndElementInfo { + XML_Parser parser; + ElementInfo *info; +} ParserAndElementInfo; + extern void XMLCALL counting_start_element_handler(void *userData, const XML_Char *name, const XML_Char **atts); @@ -564,13 +569,19 @@ extern void XMLCALL accumulate_entity_decl( const XML_Char *systemId, const XML_Char *publicId, const XML_Char *notationName); -extern void XMLCALL accumulate_char_data(void *userData, const XML_Char *s, - int len); - extern void XMLCALL accumulate_start_element(void *userData, const XML_Char *name, const XML_Char **atts); +extern void XMLCALL accumulate_characters(void *userData, const XML_Char *s, + int len); + +extern void XMLCALL accumulate_attribute(void *userData, const XML_Char *name, + const XML_Char **atts); + +extern void XMLCALL ext_accumulate_characters(void *userData, const XML_Char *s, + int len); + typedef struct default_check { const XML_Char *expected; const int expectedLen; diff --git a/tests/misc_tests.c b/tests/misc_tests.c index 2ee9320b1392..9afe0922d6b2 100644 --- a/tests/misc_tests.c +++ b/tests/misc_tests.c @@ -208,7 +208,7 @@ START_TEST(test_misc_version) { if (! versions_equal(&read_version, &parsed_version)) fail("Version mismatch"); - if (xcstrcmp(version_text, XCS("expat_2.6.3"))) /* needs bump on releases */ + if (xcstrcmp(version_text, XCS("expat_2.6.4"))) /* needs bump on releases */ fail("XML_*_VERSION in expat.h out of sync?\n"); } END_TEST @@ -332,14 +332,15 @@ START_TEST(test_misc_deny_internal_entity_closing_doctype_issue_317) { "<!ENTITY % e ']><d/>'>\n" "\n" "%e;"; - const char *const inputTwo = "<!DOCTYPE d [\n" - "<!ENTITY % e1 ']><d/>'><!ENTITY % e2 '&e1;'>\n" - "\n" - "%e2;"; + const char *const inputTwo + = "<!DOCTYPE d [\n" + "<!ENTITY % e1 ']><d/>'><!ENTITY % e2 '%e1;'>\n" + "\n" + "%e2;"; const char *const inputThree = "<!DOCTYPE d [\n" "<!ENTITY % e ']><d'>\n" "\n" - "%e;"; + "%e;/>"; const char *const inputIssue317 = "<!DOCTYPE doc [\n" "<!ENTITY % foo ']>\n" "<doc>Hell<oc (#PCDATA)*>'>\n" @@ -447,7 +448,7 @@ START_TEST(test_misc_general_entities_support) { XML_SetExternalEntityRefHandler(parser, external_entity_failer__if_not_xml_ge); XML_SetEntityDeclHandler(parser, accumulate_entity_decl); - XML_SetCharacterDataHandler(parser, accumulate_char_data); + XML_SetCharacterDataHandler(parser, accumulate_characters); if (_XML_Parse_SINGLE_BYTES(parser, doc, (int)strlen(doc), XML_TRUE) != XML_STATUS_OK) { @@ -496,6 +497,28 @@ START_TEST(test_misc_char_handler_stop_without_leak) { } END_TEST +START_TEST(test_misc_resumeparser_not_crashing) { + XML_Parser parser = XML_ParserCreate(NULL); + XML_GetBuffer(parser, 1); + XML_StopParser(parser, /*resumable=*/XML_TRUE); + XML_ResumeParser(parser); // could crash here, previously + XML_ParserFree(parser); +} +END_TEST + +START_TEST(test_misc_stopparser_rejects_unstarted_parser) { + const XML_Bool cases[] = {XML_TRUE, XML_FALSE}; + for (size_t i = 0; i < sizeof(cases) / sizeof(cases[0]); i++) { + const XML_Bool resumable = cases[i]; + XML_Parser parser = XML_ParserCreate(NULL); + assert_true(XML_GetErrorCode(parser) == XML_ERROR_NONE); + assert_true(XML_StopParser(parser, resumable) == XML_STATUS_ERROR); + assert_true(XML_GetErrorCode(parser) == XML_ERROR_NOT_STARTED); + XML_ParserFree(parser); + } +} +END_TEST + void make_miscellaneous_test_case(Suite *s) { TCase *tc_misc = tcase_create("miscellaneous tests"); @@ -520,4 +543,6 @@ make_miscellaneous_test_case(Suite *s) { test_misc_create_external_entity_parser_with_null_context); tcase_add_test(tc_misc, test_misc_general_entities_support); tcase_add_test(tc_misc, test_misc_char_handler_stop_without_leak); + tcase_add_test(tc_misc, test_misc_resumeparser_not_crashing); + tcase_add_test(tc_misc, test_misc_stopparser_rejects_unstarted_parser); } |
