File size: 7,084 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 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
#include "unity/unity.h"
#include <libxml/HTMLparser.h>
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
/* Wrapper for the static function under test (provided in module) */
extern void test_htmlParseDocTypeDecl(htmlParserCtxtPtr ctxt);
/* Capture of SAX internalSubset callback parameters */
static int cap_called = 0;
static xmlChar *cap_name = NULL;
static xmlChar *cap_public = NULL;
static xmlChar *cap_system = NULL;
static void capture_reset(void) {
if (cap_name) { xmlFree(cap_name); cap_name = NULL; }
if (cap_public) { xmlFree(cap_public); cap_public = NULL; }
if (cap_system) { xmlFree(cap_system); cap_system = NULL; }
cap_called = 0;
}
static void capture_internalSubset(void *ctx,
const xmlChar *name,
const xmlChar *ExternalID,
const xmlChar *SystemID) {
(void)ctx;
cap_called++;
cap_name = (name != NULL) ? xmlStrdup(name) : NULL;
cap_public = (ExternalID != NULL) ? xmlStrdup(ExternalID) : NULL;
cap_system = (SystemID != NULL) ? xmlStrdup(SystemID) : NULL;
}
static void attach_sax(htmlParserCtxtPtr ctxt, xmlSAXHandler *sax) {
memset(sax, 0, sizeof(*sax));
sax->initialized = XML_SAX2_MAGIC;
sax->internalSubset = capture_internalSubset;
/* Install our handler */
ctxt->sax = sax;
ctxt->disableSAX = 0;
}
static htmlParserCtxtPtr make_ctxt(const char *input, int options, xmlSAXHandler *saxOut) {
htmlParserCtxtPtr ctxt = htmlCreateMemoryParserCtxt(input, (int)strlen(input));
TEST_ASSERT_NOT_NULL_MESSAGE(ctxt, "Failed to create HTML parser context");
if (options != 0) {
int rc = htmlCtxtUseOptions(ctxt, options);
(void)rc; /* options are stored in ctxt->options */
}
attach_sax(ctxt, saxOut);
return ctxt;
}
void setUp(void) {
/* Initialize libxml2, if needed */
xmlInitParser();
capture_reset();
}
void tearDown(void) {
capture_reset();
/* Optionally clean up per-test; global cleanup not strictly required per test */
}
/* Helpers for assertions */
static void assert_xml_equals(const xmlChar *actual, const char *expected) {
TEST_ASSERT_NOT_NULL_MESSAGE(actual, "Expected non-NULL xmlChar*");
TEST_ASSERT_TRUE_MESSAGE(xmlStrEqual(actual, BAD_CAST expected), "xmlChar string mismatch");
}
/* Test: basic DOCTYPE with name; verify callback is invoked and name is parsed */
void test_htmlParseDocTypeDecl_basic_invokes_callback(void) {
const char *buf = "<!DOCTYPE html>";
xmlSAXHandler sax;
htmlParserCtxtPtr ctxt = make_ctxt(buf, 0, &sax);
test_htmlParseDocTypeDecl(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap_called);
assert_xml_equals(cap_name, "html");
TEST_ASSERT_NULL(cap_public);
TEST_ASSERT_NULL(cap_system);
htmlFreeParserCtxt(ctxt);
}
/* Test: HTML5 option lowercases the name */
void test_htmlParseDocTypeDecl_html5_lowers_name(void) {
const char *buf = "<!DOCTYPE HTML>";
xmlSAXHandler sax;
htmlParserCtxtPtr ctxt = make_ctxt(buf, HTML_PARSE_HTML5, &sax);
test_htmlParseDocTypeDecl(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap_called);
assert_xml_equals(cap_name, "html");
TEST_ASSERT_NULL(cap_public);
TEST_ASSERT_NULL(cap_system);
htmlFreeParserCtxt(ctxt);
}
/* Test: without HTML5 option, case is preserved */
void test_htmlParseDocTypeDecl_no_html5_preserves_case(void) {
const char *buf = "<!DOCTYPE Html>";
xmlSAXHandler sax;
htmlParserCtxtPtr ctxt = make_ctxt(buf, 0, &sax);
test_htmlParseDocTypeDecl(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap_called);
assert_xml_equals(cap_name, "Html");
TEST_ASSERT_NULL(cap_public);
TEST_ASSERT_NULL(cap_system);
htmlFreeParserCtxt(ctxt);
}
/* Test: PUBLIC and SYSTEM identifiers are parsed */
void test_htmlParseDocTypeDecl_public_and_system_ids(void) {
const char *pub = "-//W3C//DTD HTML 4.01//EN";
const char *sys = "http://www.w3.org/TR/html4/strict.dtd";
char buf[512];
snprintf(buf, sizeof(buf), "<!DOCTYPE html PUBLIC \"%s\" \"%s\">", pub, sys);
xmlSAXHandler sax;
htmlParserCtxtPtr ctxt = make_ctxt(buf, 0, &sax);
test_htmlParseDocTypeDecl(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap_called);
assert_xml_equals(cap_name, "html");
assert_xml_equals(cap_public, pub);
assert_xml_equals(cap_system, sys);
htmlFreeParserCtxt(ctxt);
}
/* Test: SYSTEM identifier only is parsed */
void test_htmlParseDocTypeDecl_system_only(void) {
const char *sys = "about:legacy-compat";
char buf[256];
snprintf(buf, sizeof(buf), "<!DOCTYPE html SYSTEM \"%s\">", sys);
xmlSAXHandler sax;
htmlParserCtxtPtr ctxt = make_ctxt(buf, 0, &sax);
test_htmlParseDocTypeDecl(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap_called);
assert_xml_equals(cap_name, "html");
TEST_ASSERT_NULL(cap_public);
assert_xml_equals(cap_system, sys);
htmlFreeParserCtxt(ctxt);
}
/* Test: Missing name still triggers callback with NULL name */
void test_htmlParseDocTypeDecl_missing_name(void) {
const char *buf = "<!DOCTYPE>";
xmlSAXHandler sax;
htmlParserCtxtPtr ctxt = make_ctxt(buf, 0, &sax);
test_htmlParseDocTypeDecl(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap_called);
TEST_ASSERT_NULL(cap_name);
TEST_ASSERT_NULL(cap_public);
TEST_ASSERT_NULL(cap_system);
htmlFreeParserCtxt(ctxt);
}
/* Test: Missing closing '>' is handled (bogus doctype) and callback still occurs */
void test_htmlParseDocTypeDecl_bogus_missing_gt(void) {
const char *buf = "<!DOCTYPE html PUBLIC \"abc\" \"def\"";
xmlSAXHandler sax;
htmlParserCtxtPtr ctxt = make_ctxt(buf, 0, &sax);
test_htmlParseDocTypeDecl(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap_called);
assert_xml_equals(cap_name, "html");
assert_xml_equals(cap_public, "abc");
assert_xml_equals(cap_system, "def");
htmlFreeParserCtxt(ctxt);
}
/* Test: After parsing, input cursor advances past '>' */
void test_htmlParseDocTypeDecl_advances_past_gt(void) {
const char *buf = "<!DOCTYPE html><html>";
xmlSAXHandler sax;
htmlParserCtxtPtr ctxt = make_ctxt(buf, 0, &sax);
test_htmlParseDocTypeDecl(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap_called);
/* After skipping DOCTYPE and '>', the next char should be '<' of the html tag */
TEST_ASSERT_NOT_NULL(ctxt->input);
TEST_ASSERT_NOT_NULL(ctxt->input->cur);
TEST_ASSERT_TRUE_MESSAGE(*(ctxt->input->cur) == '<', "Parser cursor not advanced past '>' as expected");
htmlFreeParserCtxt(ctxt);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_htmlParseDocTypeDecl_basic_invokes_callback);
RUN_TEST(test_htmlParseDocTypeDecl_html5_lowers_name);
RUN_TEST(test_htmlParseDocTypeDecl_no_html5_preserves_case);
RUN_TEST(test_htmlParseDocTypeDecl_public_and_system_ids);
RUN_TEST(test_htmlParseDocTypeDecl_system_only);
RUN_TEST(test_htmlParseDocTypeDecl_missing_name);
RUN_TEST(test_htmlParseDocTypeDecl_bogus_missing_gt);
RUN_TEST(test_htmlParseDocTypeDecl_advances_past_gt);
return UNITY_END();
} |