libxml / tests /tests_HTMLtree_htmlDtdDumpOutput.c
AryaWu's picture
Upload folder using huggingface_hub
6baed57 verified
#include "unity/unity.h"
#include <libxml/HTMLtree.h>
#include <libxml/tree.h>
#include <libxml/xmlIO.h>
#include <libxml/parser.h>
#include <string.h>
#include <stdlib.h>
/* Wrapper provided in the module for testing the static function */
extern void test_htmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, const char *encoding);
static char* run_dump_and_get_string(xmlDocPtr doc) {
xmlBufferPtr xbuf = xmlBufferCreate();
TEST_ASSERT_NOT_NULL_MESSAGE(xbuf, "Failed to create xmlBuffer");
xmlOutputBufferPtr out = xmlOutputBufferCreateBuffer(xbuf, NULL);
TEST_ASSERT_NOT_NULL_MESSAGE(out, "Failed to create xmlOutputBuffer");
test_htmlDtdDumpOutput(out, doc, "utf-8");
/* Ensure all data is written into the xmlBuffer */
xmlOutputBufferFlush(out);
const xmlChar *content = xmlBufferContent(xbuf);
int len = xmlBufferLength(xbuf);
if (len < 0) len = 0;
char *ret = (char *)malloc((size_t)len + 1);
TEST_ASSERT_NOT_NULL_MESSAGE(ret, "malloc failed");
if (len > 0 && content != NULL) {
memcpy(ret, content, (size_t)len);
}
ret[len] = '\0';
xmlOutputBufferClose(out);
xmlBufferFree(xbuf);
return ret;
}
void setUp(void) {
xmlInitParser();
}
void tearDown(void) {
xmlCleanupParser();
}
static xmlDocPtr make_doc(void) {
xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
TEST_ASSERT_NOT_NULL_MESSAGE(doc, "xmlNewDoc failed");
return doc;
}
void test_htmlDtdDumpOutput_no_subset_outputs_nothing(void) {
xmlDocPtr doc = make_doc();
char *out = run_dump_and_get_string(doc);
TEST_ASSERT_NOT_NULL(out);
TEST_ASSERT_EQUAL_STRING("", out);
free(out);
xmlFreeDoc(doc);
}
void test_htmlDtdDumpOutput_name_only(void) {
xmlDocPtr doc = make_doc();
xmlDtdPtr dtd = xmlCreateIntSubset(doc, BAD_CAST "html", NULL, NULL);
TEST_ASSERT_NOT_NULL_MESSAGE(dtd, "xmlCreateIntSubset failed");
char *out = run_dump_and_get_string(doc);
TEST_ASSERT_EQUAL_STRING("<!DOCTYPE html>\n", out);
free(out);
xmlFreeDoc(doc);
}
void test_htmlDtdDumpOutput_public_only(void) {
xmlDocPtr doc = make_doc();
xmlDtdPtr dtd = xmlCreateIntSubset(doc, BAD_CAST "html",
BAD_CAST "PUBLIC_ID",
NULL);
TEST_ASSERT_NOT_NULL_MESSAGE(dtd, "xmlCreateIntSubset failed");
char *out = run_dump_and_get_string(doc);
TEST_ASSERT_EQUAL_STRING("<!DOCTYPE html PUBLIC \"PUBLIC_ID\">\n", out);
free(out);
xmlFreeDoc(doc);
}
void test_htmlDtdDumpOutput_system_only_non_legacy(void) {
xmlDocPtr doc = make_doc();
xmlDtdPtr dtd = xmlCreateIntSubset(doc, BAD_CAST "html",
NULL,
BAD_CAST "http://example.com/sys.dtd");
TEST_ASSERT_NOT_NULL_MESSAGE(dtd, "xmlCreateIntSubset failed");
char *out = run_dump_and_get_string(doc);
TEST_ASSERT_EQUAL_STRING("<!DOCTYPE html SYSTEM \"http://example.com/sys.dtd\">\n", out);
free(out);
xmlFreeDoc(doc);
}
void test_htmlDtdDumpOutput_system_only_legacy_compat_suppressed(void) {
xmlDocPtr doc = make_doc();
xmlDtdPtr dtd = xmlCreateIntSubset(doc, BAD_CAST "html",
NULL,
BAD_CAST "about:legacy-compat");
TEST_ASSERT_NOT_NULL_MESSAGE(dtd, "xmlCreateIntSubset failed");
char *out = run_dump_and_get_string(doc);
/* No SYSTEM printed for about:legacy-compat */
TEST_ASSERT_EQUAL_STRING("<!DOCTYPE html>\n", out);
free(out);
xmlFreeDoc(doc);
}
void test_htmlDtdDumpOutput_public_and_system(void) {
xmlDocPtr doc = make_doc();
xmlDtdPtr dtd = xmlCreateIntSubset(doc, BAD_CAST "html",
BAD_CAST "-//W3C//DTD HTML 4.01//EN",
BAD_CAST "http://www.w3.org/TR/html4/strict.dtd");
TEST_ASSERT_NOT_NULL_MESSAGE(dtd, "xmlCreateIntSubset failed");
char *out = run_dump_and_get_string(doc);
TEST_ASSERT_EQUAL_STRING("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n", out);
free(out);
xmlFreeDoc(doc);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_htmlDtdDumpOutput_no_subset_outputs_nothing);
RUN_TEST(test_htmlDtdDumpOutput_name_only);
RUN_TEST(test_htmlDtdDumpOutput_public_only);
RUN_TEST(test_htmlDtdDumpOutput_system_only_non_legacy);
RUN_TEST(test_htmlDtdDumpOutput_system_only_legacy_compat_suppressed);
RUN_TEST(test_htmlDtdDumpOutput_public_and_system);
return UNITY_END();
}