]> granicus.if.org Git - libexpat/commitdiff
Extend test coverage of external entity suspension
authorRhodri James <rhodri@kynesim.co.uk>
Fri, 10 Feb 2017 12:52:55 +0000 (12:52 +0000)
committerSebastian Pipping <sebastian@pipping.org>
Sun, 16 Jul 2017 16:23:44 +0000 (18:23 +0200)
expat/tests/runtests.c

index 869307b60a541dff8f1077211a9ae8c885437a8e..553782e762a254161da67bb3c1bd0079010cef2e 100644 (file)
@@ -2207,6 +2207,78 @@ START_TEST(test_subordinate_xdecl_abort)
 }
 END_TEST
 
+/* Test external entity fault handling with suspension */
+static int XMLCALL
+external_entity_suspending_faulter(XML_Parser parser,
+                                   const XML_Char *context,
+                                   const XML_Char *UNUSED_P(base),
+                                   const XML_Char *UNUSED_P(systemId),
+                                   const XML_Char *UNUSED_P(publicId))
+{
+    XML_Parser ext_parser;
+    ExtFaults *fault = (ExtFaults *)XML_GetUserData(parser);
+    void *buffer;
+    int parse_len = strlen(fault->parse_text);
+
+    ext_parser = XML_ExternalEntityParserCreate(parser, context, NULL);
+    if (ext_parser == NULL)
+        fail("Could not create external entity parser");
+    XML_SetXmlDeclHandler(ext_parser, entity_suspending_xdecl_handler);
+    XML_SetUserData(ext_parser, ext_parser);
+    resumable = XML_TRUE;
+    buffer = XML_GetBuffer(ext_parser, parse_len);
+    if (buffer == NULL)
+        fail("Could not allocate parse buffer");
+    memcpy(buffer, fault->parse_text, parse_len);
+    if (XML_ParseBuffer(ext_parser, parse_len,
+                        XML_FALSE) != XML_STATUS_SUSPENDED)
+        fail("XML declaration did not suspend");
+    if (XML_ResumeParser(ext_parser) != XML_STATUS_OK)
+        xml_failure(ext_parser);
+    if (XML_ParseBuffer(ext_parser, 0, XML_TRUE) != XML_STATUS_ERROR)
+        fail(fault->fail_text);
+    if (XML_GetErrorCode(ext_parser) != fault->error)
+        xml_failure(ext_parser);
+    return XML_STATUS_ERROR;
+}
+
+START_TEST(test_ext_entity_invalid_suspended_parse)
+{
+    const char *text =
+        "<!DOCTYPE doc [\n"
+        "  <!ENTITY en SYSTEM 'http://xml.libexpat.org/dummy.ent'>\n"
+        "]>\n"
+        "<doc>&en;</doc>";
+    ExtFaults faults[] = {
+        {
+            "<?xml version='1.0' encoding='us-ascii'?><",
+            "Incomplete element declaration not faulted",
+            XML_ERROR_UNCLOSED_TOKEN
+        },
+        {
+            /* First two bytes of a three-byte char */
+            "<?xml version='1.0' encoding='utf-8'?>\xe2\x82",
+            "Incomplete character not faulted",
+            XML_ERROR_PARTIAL_CHAR
+        },
+        { NULL, NULL, XML_ERROR_NONE }
+    };
+    ExtFaults *fault;
+
+    for (fault = &faults[0]; fault->parse_text != NULL; fault++) {
+        XML_SetParamEntityParsing(parser, XML_PARAM_ENTITY_PARSING_ALWAYS);
+        XML_SetExternalEntityRefHandler(parser,
+                                        external_entity_suspending_faulter);
+        XML_SetUserData(parser, fault);
+        expect_failure(text,
+                       XML_ERROR_EXTERNAL_ENTITY_HANDLING,
+                       "Parser did not report external entity error");
+        XML_ParserReset(parser, NULL);
+    }
+}
+END_TEST
+
+
 
 /* Test setting an explicit encoding */
 START_TEST(test_explicit_encoding)
@@ -3829,6 +3901,7 @@ make_suite(void)
     tcase_add_test(tc_basic, test_ext_entity_set_bom);
     tcase_add_test(tc_basic, test_ext_entity_bad_encoding);
     tcase_add_test(tc_basic, test_ext_entity_invalid_parse);
+    tcase_add_test(tc_basic, test_ext_entity_invalid_suspended_parse);
     tcase_add_test(tc_basic, test_dtd_default_handling);
     tcase_add_test(tc_basic, test_empty_ns_without_namespaces);
     tcase_add_test(tc_basic, test_ns_in_attribute_default_without_namespaces);