Move test_external_entity_values out of runtests.c

This commit is contained in:
Rhodri James 2022-10-28 20:14:11 +01:00
parent 5509b7df8a
commit 07b7a9f4fc
4 changed files with 97 additions and 91 deletions

View file

@ -2948,6 +2948,50 @@ START_TEST(test_bad_ignore_section) {
}
END_TEST
/* Test recursive parsing */
START_TEST(test_external_entity_values) {
const char *text = "<!DOCTYPE doc SYSTEM '004-1.ent'>\n"
"<doc></doc>\n";
ExtFaults data_004_2[] = {
{"<!ATTLIST doc a1 CDATA 'value'>", NULL, NULL, XML_ERROR_NONE},
{"<!ATTLIST $doc a1 CDATA 'value'>", "Invalid token not faulted", NULL,
XML_ERROR_INVALID_TOKEN},
{"'wombat", "Unterminated string not faulted", NULL,
XML_ERROR_UNCLOSED_TOKEN},
{"\xe2\x82", "Partial UTF-8 character not faulted", NULL,
XML_ERROR_PARTIAL_CHAR},
{"<?xml version='1.0' encoding='utf-8'?>\n", NULL, NULL, XML_ERROR_NONE},
{"<?xml?>", "Malformed XML declaration not faulted", NULL,
XML_ERROR_XML_DECL},
{/* UTF-8 BOM */
"\xEF\xBB\xBF<!ATTLIST doc a1 CDATA 'value'>", NULL, NULL,
XML_ERROR_NONE},
{"<?xml version='1.0' encoding='utf-8'?>\n$",
"Invalid token after text declaration not faulted", NULL,
XML_ERROR_INVALID_TOKEN},
{"<?xml version='1.0' encoding='utf-8'?>\n'wombat",
"Unterminated string after text decl not faulted", NULL,
XML_ERROR_UNCLOSED_TOKEN},
{"<?xml version='1.0' encoding='utf-8'?>\n\xe2\x82",
"Partial UTF-8 character after text decl not faulted", NULL,
XML_ERROR_PARTIAL_CHAR},
{"%e1;", "Recursive parameter entity not faulted", NULL,
XML_ERROR_RECURSIVE_ENTITY_REF},
{NULL, NULL, NULL, XML_ERROR_NONE}};
int i;
for (i = 0; data_004_2[i].parse_text != NULL; i++) {
XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
XML_SetExternalEntityRefHandler(g_parser, external_entity_valuer);
XML_SetUserData(g_parser, &data_004_2[i]);
if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
== XML_STATUS_ERROR)
xml_failure(g_parser);
XML_ParserReset(g_parser, NULL);
}
}
END_TEST
TCase *
make_basic_test_case(Suite *s) {
TCase *tc_basic = tcase_create("basic tests");
@ -3078,6 +3122,7 @@ make_basic_test_case(Suite *s) {
tcase_add_test__ifdef_xml_dtd(tc_basic, test_ignore_section_utf16);
tcase_add_test__ifdef_xml_dtd(tc_basic, test_ignore_section_utf16_be);
tcase_add_test__ifdef_xml_dtd(tc_basic, test_bad_ignore_section);
tcase_add_test__ifdef_xml_dtd(tc_basic, test_external_entity_values);
return tc_basic; /* TEMPORARY: this will become a void function */
}

View file

@ -727,6 +727,52 @@ external_entity_load_ignore_utf16_be(XML_Parser parser, const XML_Char *context,
return XML_STATUS_OK;
}
int XMLCALL
external_entity_valuer(XML_Parser parser, const XML_Char *context,
const XML_Char *base, const XML_Char *systemId,
const XML_Char *publicId) {
const char *text1 = "<!ELEMENT doc EMPTY>\n"
"<!ENTITY % e1 SYSTEM '004-2.ent'>\n"
"<!ENTITY % e2 '%e1;'>\n"
"%e1;\n";
XML_Parser ext_parser;
UNUSED_P(base);
UNUSED_P(publicId);
if (systemId == NULL)
return XML_STATUS_OK;
ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
if (ext_parser == NULL)
fail("Could not create external entity parser");
if (! xcstrcmp(systemId, XCS("004-1.ent"))) {
if (_XML_Parse_SINGLE_BYTES(ext_parser, text1, (int)strlen(text1), XML_TRUE)
== XML_STATUS_ERROR)
xml_failure(ext_parser);
} else if (! xcstrcmp(systemId, XCS("004-2.ent"))) {
ExtFaults *fault = (ExtFaults *)XML_GetUserData(parser);
enum XML_Status status;
enum XML_Error error;
status = _XML_Parse_SINGLE_BYTES(ext_parser, fault->parse_text,
(int)strlen(fault->parse_text), XML_TRUE);
if (fault->error == XML_ERROR_NONE) {
if (status == XML_STATUS_ERROR)
xml_failure(ext_parser);
} else {
if (status != XML_STATUS_ERROR)
fail(fault->fail_text);
error = XML_GetErrorCode(ext_parser);
if (error != fault->error
&& (fault->error != XML_ERROR_XML_DECL
|| error != XML_ERROR_TEXT_DECL))
xml_failure(ext_parser);
}
}
XML_ParserFree(ext_parser);
return XML_STATUS_OK;
}
/* NotStandalone handlers */
int XMLCALL

View file

@ -236,6 +236,12 @@ extern int XMLCALL external_entity_load_ignore_utf16_be(
XML_Parser parser, const XML_Char *context, const XML_Char *base,
const XML_Char *systemId, const XML_Char *publicId);
extern int XMLCALL external_entity_valuer(XML_Parser parser,
const XML_Char *context,
const XML_Char *base,
const XML_Char *systemId,
const XML_Char *publicId);
/* NotStandalone handlers */
extern int XMLCALL reject_not_standalone_handler(void *userData);

View file

@ -74,96 +74,6 @@
XML_Parser g_parser = NULL;
/* Test recursive parsing */
static int XMLCALL
external_entity_valuer(XML_Parser parser, const XML_Char *context,
const XML_Char *base, const XML_Char *systemId,
const XML_Char *publicId) {
const char *text1 = "<!ELEMENT doc EMPTY>\n"
"<!ENTITY % e1 SYSTEM '004-2.ent'>\n"
"<!ENTITY % e2 '%e1;'>\n"
"%e1;\n";
XML_Parser ext_parser;
UNUSED_P(base);
UNUSED_P(publicId);
if (systemId == NULL)
return XML_STATUS_OK;
ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
if (ext_parser == NULL)
fail("Could not create external entity parser");
if (! xcstrcmp(systemId, XCS("004-1.ent"))) {
if (_XML_Parse_SINGLE_BYTES(ext_parser, text1, (int)strlen(text1), XML_TRUE)
== XML_STATUS_ERROR)
xml_failure(ext_parser);
} else if (! xcstrcmp(systemId, XCS("004-2.ent"))) {
ExtFaults *fault = (ExtFaults *)XML_GetUserData(parser);
enum XML_Status status;
enum XML_Error error;
status = _XML_Parse_SINGLE_BYTES(ext_parser, fault->parse_text,
(int)strlen(fault->parse_text), XML_TRUE);
if (fault->error == XML_ERROR_NONE) {
if (status == XML_STATUS_ERROR)
xml_failure(ext_parser);
} else {
if (status != XML_STATUS_ERROR)
fail(fault->fail_text);
error = XML_GetErrorCode(ext_parser);
if (error != fault->error
&& (fault->error != XML_ERROR_XML_DECL
|| error != XML_ERROR_TEXT_DECL))
xml_failure(ext_parser);
}
}
XML_ParserFree(ext_parser);
return XML_STATUS_OK;
}
START_TEST(test_external_entity_values) {
const char *text = "<!DOCTYPE doc SYSTEM '004-1.ent'>\n"
"<doc></doc>\n";
ExtFaults data_004_2[] = {
{"<!ATTLIST doc a1 CDATA 'value'>", NULL, NULL, XML_ERROR_NONE},
{"<!ATTLIST $doc a1 CDATA 'value'>", "Invalid token not faulted", NULL,
XML_ERROR_INVALID_TOKEN},
{"'wombat", "Unterminated string not faulted", NULL,
XML_ERROR_UNCLOSED_TOKEN},
{"\xe2\x82", "Partial UTF-8 character not faulted", NULL,
XML_ERROR_PARTIAL_CHAR},
{"<?xml version='1.0' encoding='utf-8'?>\n", NULL, NULL, XML_ERROR_NONE},
{"<?xml?>", "Malformed XML declaration not faulted", NULL,
XML_ERROR_XML_DECL},
{/* UTF-8 BOM */
"\xEF\xBB\xBF<!ATTLIST doc a1 CDATA 'value'>", NULL, NULL,
XML_ERROR_NONE},
{"<?xml version='1.0' encoding='utf-8'?>\n$",
"Invalid token after text declaration not faulted", NULL,
XML_ERROR_INVALID_TOKEN},
{"<?xml version='1.0' encoding='utf-8'?>\n'wombat",
"Unterminated string after text decl not faulted", NULL,
XML_ERROR_UNCLOSED_TOKEN},
{"<?xml version='1.0' encoding='utf-8'?>\n\xe2\x82",
"Partial UTF-8 character after text decl not faulted", NULL,
XML_ERROR_PARTIAL_CHAR},
{"%e1;", "Recursive parameter entity not faulted", NULL,
XML_ERROR_RECURSIVE_ENTITY_REF},
{NULL, NULL, NULL, XML_ERROR_NONE}};
int i;
for (i = 0; data_004_2[i].parse_text != NULL; i++) {
XML_SetParamEntityParsing(g_parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
XML_SetExternalEntityRefHandler(g_parser, external_entity_valuer);
XML_SetUserData(g_parser, &data_004_2[i]);
if (_XML_Parse_SINGLE_BYTES(g_parser, text, (int)strlen(text), XML_TRUE)
== XML_STATUS_ERROR)
xml_failure(g_parser);
XML_ParserReset(g_parser, NULL);
}
}
END_TEST
/* Test the recursive parse interacts with a not standalone handler */
static int XMLCALL
external_entity_not_standalone(XML_Parser parser, const XML_Char *context,
@ -7818,7 +7728,6 @@ make_suite(void) {
TCase *tc_accounting = tcase_create("accounting tests");
#endif
tcase_add_test__ifdef_xml_dtd(tc_basic, test_external_entity_values);
tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_not_standalone);
tcase_add_test__ifdef_xml_dtd(tc_basic, test_ext_entity_value_abort);
tcase_add_test(tc_basic, test_bad_public_doctype);