libxml / tests /tests_HTMLtree_htmlDocDumpMemoryFormat.c
AryaWu's picture
Upload folder using huggingface_hub
6baed57 verified
#include "unity/unity.h"
#include <libxml/HTMLtree.h>
#include <libxml/HTMLparser.h>
#include <libxml/xmlstring.h>
#include <string.h>
#include <stdlib.h>
void setUp(void) {
/* Setup code here, or leave empty */
}
void tearDown(void) {
/* Cleanup code here, or leave empty */
}
static xmlDocPtr make_simple_html_doc(void) {
const char *html =
"<!DOCTYPE html>"
"<html><head><title>T</title></head>"
"<body>"
"<div><p>a & b</p></div>"
"</body></html>";
/* Parse a tiny HTML doc in-memory */
xmlDocPtr doc = htmlReadMemory(html, (int)strlen(html), "test.html",
NULL, HTML_PARSE_NOERROR | HTML_PARSE_NOWARNING);
return doc;
}
void test_htmlDocDumpMemoryFormat_null_doc_sets_outputs(void) {
xmlChar *mem = (xmlChar*)1; /* sentinel */
int size = -123;
htmlDocDumpMemoryFormat(NULL, &mem, &size, 0);
TEST_ASSERT_NULL(mem);
TEST_ASSERT_EQUAL_INT(0, size);
}
void test_htmlDocDumpMemoryFormat_mem_null_does_not_touch_size(void) {
xmlDocPtr doc = make_simple_html_doc();
TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to create HTML doc");
int size = 777;
/* Should early return and leave size unchanged */
htmlDocDumpMemoryFormat(doc, NULL, &size, 0);
TEST_ASSERT_EQUAL_INT(777, size);
xmlFreeDoc(doc);
}
void test_htmlDocDumpMemoryFormat_size_null_does_not_touch_mem(void) {
xmlDocPtr doc = make_simple_html_doc();
TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to create HTML doc");
xmlChar *mem = (xmlChar*)0xDEADBEEF; /* sentinel */
/* Should early return and leave mem pointer unchanged */
htmlDocDumpMemoryFormat(doc, &mem, NULL, 0);
TEST_ASSERT_EQUAL_PTR((void*)0xDEADBEEF, mem);
xmlFreeDoc(doc);
}
void test_htmlDocDumpMemoryFormat_invalid_encoding_returns_null(void) {
xmlDocPtr doc = make_simple_html_doc();
TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to create HTML doc");
/* Force an invalid/unavailable output encoding */
if (doc->encoding != NULL) {
xmlFree((void*)doc->encoding);
doc->encoding = NULL;
}
doc->encoding = xmlCharStrdup("x-invalid-encoding-123");
xmlChar *mem = (xmlChar*)1; /* sentinel to ensure it's overwritten to NULL */
int size = -42;
htmlDocDumpMemoryFormat(doc, &mem, &size, 0);
TEST_ASSERT_NULL(mem);
TEST_ASSERT_EQUAL_INT(0, size);
xmlFreeDoc(doc);
}
void test_htmlDocDumpMemoryFormat_basic_html_output_nonformatted(void) {
xmlDocPtr doc = make_simple_html_doc();
TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to create HTML doc");
xmlChar *mem = NULL;
int size = 0;
htmlDocDumpMemoryFormat(doc, &mem, &size, 0);
TEST_ASSERT_NOT_NULL(mem);
TEST_ASSERT_TRUE(size > 0);
/* Ensure NUL-termination at mem[size] */
TEST_ASSERT_EQUAL_HEX8(0, ((const unsigned char*)mem)[size]);
/* Basic content checks */
TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<html"));
TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<body"));
TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<p"));
/* Ensure HTML escaping of '&' occurred */
TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "&amp;"));
xmlFree(mem);
xmlFreeDoc(doc);
}
void test_htmlDocDumpMemoryFormat_basic_html_output_formatted(void) {
xmlDocPtr doc = make_simple_html_doc();
TEST_ASSERT_NOT_NULL_MESSAGE(doc, "Failed to create HTML doc");
xmlChar *mem = NULL;
int size = 0;
htmlDocDumpMemoryFormat(doc, &mem, &size, 1);
TEST_ASSERT_NOT_NULL(mem);
TEST_ASSERT_TRUE(size > 0);
TEST_ASSERT_EQUAL_HEX8(0, ((const unsigned char*)mem)[size]);
/* Same essential structure/content present */
TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<html"));
TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<body"));
TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "<p"));
TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "&amp;"));
xmlFree(mem);
xmlFreeDoc(doc);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_htmlDocDumpMemoryFormat_null_doc_sets_outputs);
RUN_TEST(test_htmlDocDumpMemoryFormat_mem_null_does_not_touch_size);
RUN_TEST(test_htmlDocDumpMemoryFormat_size_null_does_not_touch_mem);
RUN_TEST(test_htmlDocDumpMemoryFormat_invalid_encoding_returns_null);
RUN_TEST(test_htmlDocDumpMemoryFormat_basic_html_output_nonformatted);
RUN_TEST(test_htmlDocDumpMemoryFormat_basic_html_output_formatted);
return UNITY_END();
}