tests: Handle entities in SAX tests

This commit is contained in:
Nick Wellnhofer 2023-10-06 10:44:38 +02:00
parent 713ded60ad
commit 6337a14a6b
9 changed files with 91 additions and 28 deletions

View File

@ -10,7 +10,6 @@ SAX.startElement(EXAMPLE)
SAX.characters( SAX.characters(
, 3) , 3)
SAX.getEntity(title) SAX.getEntity(title)
SAX.error: Entity 'title' not defined
SAX.reference(title) SAX.reference(title)
SAX.characters( SAX.characters(
This text is about XML, the, 31) This text is about XML, the, 31)

View File

@ -10,7 +10,6 @@ SAX.startElementNs(EXAMPLE, NULL, NULL, 0, 0, 0)
SAX.characters( SAX.characters(
, 3) , 3)
SAX.getEntity(title) SAX.getEntity(title)
SAX.error: Entity 'title' not defined
SAX.reference(title) SAX.reference(title)
SAX.characters( SAX.characters(
This text is about XML, the, 31) This text is about XML, the, 31)

View File

@ -7,11 +7,16 @@ SAX.getParameterEntity(sampleEnt)
SAX.entityDecl(sampleEnt, 1, (null), (null), the hyacinth girl) SAX.entityDecl(sampleEnt, 1, (null), (null), the hyacinth girl)
SAX.getEntity(sampleEnt) SAX.getEntity(sampleEnt)
SAX.getParameterEntity(sampleEnt) SAX.getParameterEntity(sampleEnt)
SAX.error: PEReference: %sampleEnt; not found SAX.elementDecl(item, 4, ...)
SAX.elementDecl(para, 3, ...)
SAX.externalSubset(item, , )
SAX.startElement(item)
SAX.startElement(para)
SAX.characters('they called me , 16) SAX.characters('they called me , 16)
SAX.getEntity(sampleEnt) SAX.getEntity(sampleEnt)
SAX.error: Entity 'sampleEnt' not defined SAX.characters(the hyacinth girl, 17)
SAX.reference(sampleEnt) SAX.reference(sampleEnt)
SAX.characters(', 1) SAX.characters(', 1)
SAX.endElement(para)
SAX.endElement(item)
SAX.endDocument() SAX.endDocument()
xmlSAXUserParseFile returned error 27

View File

@ -7,11 +7,16 @@ SAX.getParameterEntity(sampleEnt)
SAX.entityDecl(sampleEnt, 1, (null), (null), the hyacinth girl) SAX.entityDecl(sampleEnt, 1, (null), (null), the hyacinth girl)
SAX.getEntity(sampleEnt) SAX.getEntity(sampleEnt)
SAX.getParameterEntity(sampleEnt) SAX.getParameterEntity(sampleEnt)
SAX.error: PEReference: %sampleEnt; not found SAX.elementDecl(item, 4, ...)
SAX.elementDecl(para, 3, ...)
SAX.externalSubset(item, , )
SAX.startElementNs(item, NULL, NULL, 0, 0, 0)
SAX.startElementNs(para, NULL, NULL, 0, 0, 0)
SAX.characters('they called me , 16) SAX.characters('they called me , 16)
SAX.getEntity(sampleEnt) SAX.getEntity(sampleEnt)
SAX.error: Entity 'sampleEnt' not defined SAX.characters(the hyacinth girl, 17)
SAX.reference(sampleEnt) SAX.reference(sampleEnt)
SAX.characters(', 1) SAX.characters(', 1)
SAX.endElementNs(para, NULL, NULL)
SAX.endElementNs(item, NULL, NULL)
SAX.endDocument() SAX.endDocument()
xmlSAXUserParseFile returned error 27

View File

@ -7,11 +7,15 @@ SAX.getParameterEntity(sampleEnt)
SAX.entityDecl(sampleEnt, 1, (null), (null), the hyacinth girl) SAX.entityDecl(sampleEnt, 1, (null), (null), the hyacinth girl)
SAX.getEntity(sampleEnt) SAX.getEntity(sampleEnt)
SAX.getParameterEntity(sampleEnt) SAX.getParameterEntity(sampleEnt)
SAX.error: PEReference: %sampleEnt; not found SAX.elementDecl(item, 4, ...)
SAX.elementDecl(para, 3, ...)
SAX.externalSubset(item, , )
SAX.startElementNs(item, NULL, NULL, 0, 0, 0)
SAX.startElementNs(para, NULL, NULL, 0, 0, 0)
SAX.characters('they called me , 16) SAX.characters('they called me , 16)
SAX.getEntity(sampleEnt) SAX.getEntity(sampleEnt)
SAX.error: Entity 'sampleEnt' not defined SAX.characters(the hyacinth girl, 17)
SAX.reference(sampleEnt)
SAX.characters(', 1) SAX.characters(', 1)
SAX.endElementNs(para, NULL, NULL)
SAX.endElementNs(item, NULL, NULL)
SAX.endDocument() SAX.endDocument()
xmlSAXUserParseFile returned error 27

View File

@ -8,11 +8,14 @@ SAX.getParameterEntity(xx)
SAX.entityDecl(zz, 4, (null), (null), <!ENTITY tricky "error-prone" >) SAX.entityDecl(zz, 4, (null), (null), <!ENTITY tricky "error-prone" >)
SAX.getParameterEntity(zz) SAX.getParameterEntity(zz)
SAX.getParameterEntity(xx) SAX.getParameterEntity(xx)
SAX.error: PEReference: %xx; not found SAX.getParameterEntity(zz)
SAX.entityDecl(tricky, 1, (null), (null), error-prone)
SAX.getEntity(tricky)
SAX.externalSubset(test, , )
SAX.startElementNs(test, NULL, NULL, 0, 0, 0)
SAX.characters(This sample shows a , 20) SAX.characters(This sample shows a , 20)
SAX.getEntity(tricky) SAX.getEntity(tricky)
SAX.error: Entity 'tricky' not defined SAX.characters(error-prone, 11)
SAX.reference(tricky)
SAX.characters( method., 8) SAX.characters( method., 8)
SAX.endElementNs(test, NULL, NULL)
SAX.endDocument() SAX.endDocument()
xmlSAXUserParseFile returned error 27

View File

@ -8,11 +8,15 @@ SAX.getParameterEntity(xx)
SAX.entityDecl(zz, 4, (null), (null), <!ENTITY tricky "error-prone" >) SAX.entityDecl(zz, 4, (null), (null), <!ENTITY tricky "error-prone" >)
SAX.getParameterEntity(zz) SAX.getParameterEntity(zz)
SAX.getParameterEntity(xx) SAX.getParameterEntity(xx)
SAX.error: PEReference: %xx; not found SAX.getParameterEntity(zz)
SAX.entityDecl(tricky, 1, (null), (null), error-prone)
SAX.getEntity(tricky)
SAX.externalSubset(test, , )
SAX.startElement(test)
SAX.characters(This sample shows a , 20) SAX.characters(This sample shows a , 20)
SAX.getEntity(tricky) SAX.getEntity(tricky)
SAX.error: Entity 'tricky' not defined SAX.characters(error-prone, 11)
SAX.reference(tricky) SAX.reference(tricky)
SAX.characters( method., 8) SAX.characters( method., 8)
SAX.endElement(test)
SAX.endDocument() SAX.endDocument()
xmlSAXUserParseFile returned error 27

View File

@ -8,11 +8,15 @@ SAX.getParameterEntity(xx)
SAX.entityDecl(zz, 4, (null), (null), <!ENTITY tricky "error-prone" >) SAX.entityDecl(zz, 4, (null), (null), <!ENTITY tricky "error-prone" >)
SAX.getParameterEntity(zz) SAX.getParameterEntity(zz)
SAX.getParameterEntity(xx) SAX.getParameterEntity(xx)
SAX.error: PEReference: %xx; not found SAX.getParameterEntity(zz)
SAX.entityDecl(tricky, 1, (null), (null), error-prone)
SAX.getEntity(tricky)
SAX.externalSubset(test, , )
SAX.startElementNs(test, NULL, NULL, 0, 0, 0)
SAX.characters(This sample shows a , 20) SAX.characters(This sample shows a , 20)
SAX.getEntity(tricky) SAX.getEntity(tricky)
SAX.error: Entity 'tricky' not defined SAX.characters(error-prone, 11)
SAX.reference(tricky) SAX.reference(tricky)
SAX.characters( method., 8) SAX.characters( method., 8)
SAX.endElementNs(test, NULL, NULL)
SAX.endDocument() SAX.endDocument()
xmlSAXUserParseFile returned error 27

View File

@ -839,6 +839,12 @@ static xmlSAXHandler emptySAXHandlerStruct = {
NULL /* xmlStructuredErrorFunc */ NULL /* xmlStructuredErrorFunc */
}; };
typedef struct {
const char *filename;
xmlHashTablePtr generalEntities;
xmlHashTablePtr parameterEntities;
} debugContext;
static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct; static xmlSAXHandlerPtr emptySAXHandler = &emptySAXHandlerStruct;
static int callbacks = 0; static int callbacks = 0;
static int quiet = 0; static int quiet = 0;
@ -997,13 +1003,16 @@ resolveEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *publicId, const xm
* Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour. * Returns the xmlParserInputPtr if inlined or NULL for DOM behaviour.
*/ */
static xmlEntityPtr static xmlEntityPtr
getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name) getEntityDebug(void *ctx, const xmlChar *name)
{ {
debugContext *ctxt = ctx;
callbacks++; callbacks++;
if (quiet) if (quiet)
return(NULL); return(NULL);
fprintf(SAXdebug, "SAX.getEntity(%s)\n", name); fprintf(SAXdebug, "SAX.getEntity(%s)\n", name);
return(NULL);
return(xmlHashLookup(ctxt->generalEntities, name));
} }
/** /**
@ -1016,13 +1025,16 @@ getEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
* Returns the xmlParserInputPtr * Returns the xmlParserInputPtr
*/ */
static xmlEntityPtr static xmlEntityPtr
getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name) getParameterEntityDebug(void *ctx, const xmlChar *name)
{ {
debugContext *ctxt = ctx;
callbacks++; callbacks++;
if (quiet) if (quiet)
return(NULL); return(NULL);
fprintf(SAXdebug, "SAX.getParameterEntity(%s)\n", name); fprintf(SAXdebug, "SAX.getParameterEntity(%s)\n", name);
return(NULL);
return(xmlHashLookup(ctxt->parameterEntities, name));
} }
@ -1038,10 +1050,13 @@ getParameterEntityDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name)
* An entity definition has been parsed * An entity definition has been parsed
*/ */
static void static void
entityDeclDebug(void *ctx ATTRIBUTE_UNUSED, const xmlChar *name, int type, entityDeclDebug(void *ctx, const xmlChar *name, int type,
const xmlChar *publicId, const xmlChar *systemId, xmlChar *content) const xmlChar *publicId, const xmlChar *systemId, xmlChar *content)
{ {
const xmlChar *nullstr = BAD_CAST "(null)"; debugContext *ctxt = ctx;
xmlEntityPtr ent;
const xmlChar *nullstr = BAD_CAST "(null)";
/* not all libraries handle printing null pointers nicely */ /* not all libraries handle printing null pointers nicely */
if (publicId == NULL) if (publicId == NULL)
publicId = nullstr; publicId = nullstr;
@ -1054,6 +1069,16 @@ const xmlChar *nullstr = BAD_CAST "(null)";
return; return;
fprintf(SAXdebug, "SAX.entityDecl(%s, %d, %s, %s, %s)\n", fprintf(SAXdebug, "SAX.entityDecl(%s, %d, %s, %s, %s)\n",
name, type, publicId, systemId, content); name, type, publicId, systemId, content);
ent = xmlNewEntity(NULL, name, type, publicId, systemId, content);
if (systemId != NULL)
ent->URI = xmlBuildURI(systemId, (const xmlChar *) ctxt->filename);
if ((type == XML_INTERNAL_PARAMETER_ENTITY) ||
(type == XML_EXTERNAL_PARAMETER_ENTITY))
xmlHashAddEntry(ctxt->parameterEntities, name, ent);
else
xmlHashAddEntry(ctxt->generalEntities, name, ent);
} }
/** /**
@ -1711,6 +1736,13 @@ static xmlSAXHandler debugHTMLSAXHandlerStruct = {
static xmlSAXHandlerPtr debugHTMLSAXHandler = &debugHTMLSAXHandlerStruct; static xmlSAXHandlerPtr debugHTMLSAXHandler = &debugHTMLSAXHandlerStruct;
#endif /* LIBXML_HTML_ENABLED */ #endif /* LIBXML_HTML_ENABLED */
static void
hashFreeEntity(void *payload, const xmlChar *name ATTRIBUTE_UNUSED) {
xmlEntityPtr ent = payload;
xmlFreeEntity(ent);
}
/** /**
* saxParseTest: * saxParseTest:
* @filename: the file to parse * @filename: the file to parse
@ -1784,16 +1816,24 @@ saxParseTest(const char *filename, const char *result,
} else } else
#endif #endif
{ {
debugContext userData;
xmlParserCtxtPtr ctxt = xmlCreateFileParserCtxt(filename); xmlParserCtxtPtr ctxt = xmlCreateFileParserCtxt(filename);
if (options & XML_PARSE_SAX1) { if (options & XML_PARSE_SAX1) {
memcpy(ctxt->sax, debugSAXHandler, sizeof(xmlSAXHandler)); memcpy(ctxt->sax, debugSAXHandler, sizeof(xmlSAXHandler));
options -= XML_PARSE_SAX1; options -= XML_PARSE_SAX1;
} else { } else {
memcpy(ctxt->sax, debugSAX2Handler, sizeof(xmlSAXHandler)); memcpy(ctxt->sax, debugSAX2Handler, sizeof(xmlSAXHandler));
} }
userData.filename = filename;
userData.generalEntities = xmlHashCreate(0);
userData.parameterEntities = xmlHashCreate(0);
ctxt->userData = &userData;
xmlCtxtUseOptions(ctxt, options); xmlCtxtUseOptions(ctxt, options);
xmlParseDocument(ctxt); xmlParseDocument(ctxt);
ret = ctxt->wellFormed ? 0 : ctxt->errNo; ret = ctxt->wellFormed ? 0 : ctxt->errNo;
xmlHashFree(userData.generalEntities, hashFreeEntity);
xmlHashFree(userData.parameterEntities, hashFreeEntity);
xmlFreeDoc(ctxt->myDoc); xmlFreeDoc(ctxt->myDoc);
xmlFreeParserCtxt(ctxt); xmlFreeParserCtxt(ctxt);
} }