1
0
mirror of https://gitlab.gnome.org/GNOME/libxml2.git synced 2025-03-25 10:50:08 +03:00

test: Add push parser tests for split UTF-8 sequences

This commit is contained in:
Nick Wellnhofer 2023-05-18 19:23:58 +02:00
parent e0f3016f71
commit 981093abd1

View File

@ -713,6 +713,84 @@ error:
return ret;
}
static int
testUTF8Chunks(void) {
#if defined(LIBXML_PUSH_ENABLED) && defined(LIBXML_OUTPUT_ENABLED)
xmlParserCtxtPtr ctxt;
xmlChar *out;
int outSize;
char *buf;
int i;
int ret = 0;
ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
xmlParseChunk(ctxt, "<d>", 3, 0);
xmlParseChunk(ctxt, "\xF0", 1, 0);
xmlParseChunk(ctxt, "\x9F", 1, 0);
xmlParseChunk(ctxt, "\x98", 1, 0);
xmlParseChunk(ctxt, "\x8A", 1, 0);
xmlParseChunk(ctxt, "</d>", 4, 1);
xmlDocDumpMemory(ctxt->myDoc, &out, &outSize);
if (strcmp((char *) out,
"<?xml version=\"1.0\"?>\n<d>&#x1F60A;</d>\n") != 0) {
fprintf(stderr, "failed UTF-8 chunk test 1\n");
ret += 1;
}
xmlFree(out);
xmlFreeDoc(ctxt->myDoc);
xmlFreeParserCtxt(ctxt);
ctxt = xmlCreatePushParserCtxt(NULL, NULL, NULL, 0, NULL);
xmlParseChunk(ctxt, "<d>", 3, 0);
/*
* Create a chunk longer than XML_PARSER_BIG_BUFFER_SIZE (300) ending
* with an incomplete UTF-8 sequence.
*/
buf = xmlMalloc(1000 * 2 + 1);
for (i = 0; i < 2000; i += 2)
memcpy(buf + i, "\xCE\xB1", 2);
buf[i] = '\xCE';
xmlParseChunk(ctxt, buf, 2001, 0);
xmlFree(buf);
xmlParseChunk(ctxt, "\xB1</d>", 4, 0);
xmlParseChunk(ctxt, NULL, 0, 0);
xmlDocDumpMemory(ctxt->myDoc, &out, &outSize);
if (strncmp((char *) out, "<?xml version=\"1.0\"?>\n<d>", 25) != 0) {
fprintf(stderr, "failed UTF-8 chunk test 2-1\n");
ret += 1;
goto error;
}
for (i = 25; i < 25 + 1001 * 7; i += 7) {
if (memcmp(out + i, "&#x3B1;", 7) != 0) {
fprintf(stderr, "failed UTF-8 chunk test 2-2 %d\n", i);
ret += 1;
goto error;
}
}
if (strcmp((char *) out + i, "</d>\n") != 0) {
fprintf(stderr, "failed UTF-8 chunk test 2-3\n");
ret += 1;
goto error;
}
error:
xmlFree(out);
xmlFreeDoc(ctxt->myDoc);
xmlFreeParserCtxt(ctxt);
return(ret);
#else
return(0);
#endif
}
int main(void) {
int ret = 0;
@ -736,6 +814,7 @@ int main(void) {
ret += testCharRanges();
ret += testDocumentRanges();
ret += testUserEncoding();
ret += testUTF8Chunks();
/*
* Cleanup function for the XML library.