#include "unity/unity.h" #include #include #include #include #include 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 = "" "T" "" "

a & b

" ""; /* 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, " 0); TEST_ASSERT_EQUAL_HEX8(0, ((const unsigned char*)mem)[size]); /* Same essential structure/content present */ TEST_ASSERT_NOT_NULL(strstr((const char*)mem, "