File size: 4,487 Bytes
6baed57 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 |
#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, "&"));
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, "&"));
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();
} |