File size: 9,329 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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 |
#include "unity/unity.h"
#include <libxml/HTMLparser.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
/* Wrapper provided in the module for calling the static function */
extern void test_htmlParseStartTag(htmlParserCtxtPtr ctxt);
/* Simple capture of SAX startElement events */
typedef struct {
xmlChar *name;
int att_count; /* number of attribute pairs */
xmlChar **atts; /* flattened name, value, name, value..., NULL terminated */
} StartEvent;
typedef struct {
int nevents;
StartEvent events[64];
} SAXCapture;
static void capture_init(SAXCapture *cap) {
memset(cap, 0, sizeof(*cap));
}
static void capture_free(SAXCapture *cap) {
for (int i = 0; i < cap->nevents; i++) {
if (cap->events[i].name) {
xmlFree(cap->events[i].name);
}
if (cap->events[i].atts) {
/* Free copied attribute names and values */
int j = 0;
while (cap->events[i].atts[j] != NULL) {
xmlFree(cap->events[i].atts[j]);
j++;
}
free(cap->events[i].atts);
}
}
memset(cap, 0, sizeof(*cap));
}
static void test_sax_startElement(void *ctx, const xmlChar *name, const xmlChar **atts) {
SAXCapture *cap = (SAXCapture *)ctx;
if (cap->nevents >= (int)(sizeof(cap->events)/sizeof(cap->events[0])))
return;
StartEvent *ev = &cap->events[cap->nevents++];
ev->name = xmlStrdup(name);
int count = 0;
if (atts != NULL) {
const xmlChar **p = atts;
while (*p != NULL) {
/* name */
p++;
/* value */
if (*p == NULL) break;
p++;
count++;
}
}
ev->att_count = count;
if (atts != NULL && count > 0) {
/* allocate space for 2*count + 1 NULL terminator */
ev->atts = (xmlChar **)calloc((size_t)(2 * count + 1), sizeof(xmlChar *));
int idx = 0;
for (int i = 0; i < count; i++) {
const xmlChar *aname = atts[2*i];
const xmlChar *aval = atts[2*i + 1];
ev->atts[idx++] = xmlStrdup(aname);
ev->atts[idx++] = (aval != NULL) ? xmlStrdup(aval) : NULL;
}
ev->atts[idx] = NULL;
} else {
ev->atts = (xmlChar **)calloc(1, sizeof(xmlChar *));
ev->atts[0] = NULL;
}
}
static const xmlChar* find_attr_value(const StartEvent *ev, const char *name) {
if (ev->atts == NULL) return NULL;
for (int i = 0; ev->atts[i] != NULL && ev->atts[i+1] != NULL; i += 2) {
if (xmlStrcasecmp(ev->atts[i], (const xmlChar *)name) == 0) {
return ev->atts[i+1];
}
}
return NULL;
}
/* Create a parser context from a memory buffer and attach our SAX handler */
static htmlParserCtxtPtr make_ctxt(const char *buf, int flags, SAXCapture *cap) {
htmlParserCtxtPtr ctxt = htmlCreateMemoryParserCtxt(buf, (int)strlen(buf));
TEST_ASSERT_NOT_NULL_MESSAGE(ctxt, "Failed to create HTML parser context");
static xmlSAXHandler sax; /* static to ensure it lives long enough */
memset(&sax, 0, sizeof(sax));
sax.startElement = test_sax_startElement;
ctxt->sax = &sax;
ctxt->userData = cap;
ctxt->options |= flags;
return ctxt;
}
void setUp(void) {
/* Initialize libxml2 for safety */
xmlInitParser();
}
void tearDown(void) {
/* No global cleanup here to avoid interfering between tests */
}
/* Test: simple start tag without implied element insertion (NOIMPLIED) */
void test_htmlParseStartTag_simple_div_noimplied(void) {
const char *src = "<div>";
SAXCapture cap; capture_init(&cap);
htmlParserCtxtPtr ctxt = make_ctxt(src, HTML_PARSE_NOIMPLIED, &cap);
test_htmlParseStartTag(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap.nevents);
TEST_ASSERT_NOT_NULL(cap.events[0].name);
TEST_ASSERT_EQUAL_STRING("div", (const char *)cap.events[0].name);
TEST_ASSERT_EQUAL_INT(0, cap.events[0].att_count);
capture_free(&cap);
htmlFreeParserCtxt(ctxt);
}
/* Test: uppercase tag and attribute names are lowercased; values preserved */
void test_htmlParseStartTag_uppercase_and_attr_lowercased(void) {
const char *src = "<DIV CLASS=AbC ID=42>";
SAXCapture cap; capture_init(&cap);
htmlParserCtxtPtr ctxt = make_ctxt(src, HTML_PARSE_NOIMPLIED, &cap);
test_htmlParseStartTag(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap.nevents);
TEST_ASSERT_EQUAL_STRING("div", (const char *)cap.events[0].name);
TEST_ASSERT_TRUE(cap.events[0].att_count >= 2);
/* Attribute names should be lowercased */
const xmlChar *vclass = find_attr_value(&cap.events[0], "class");
const xmlChar *vid = find_attr_value(&cap.events[0], "id");
TEST_ASSERT_NOT_NULL(vclass);
TEST_ASSERT_NOT_NULL(vid);
TEST_ASSERT_EQUAL_STRING("AbC", (const char *)vclass);
TEST_ASSERT_EQUAL_STRING("42", (const char *)vid);
capture_free(&cap);
htmlFreeParserCtxt(ctxt);
}
/* Test: duplicate attributes are de-duplicated with first value preserved */
void test_htmlParseStartTag_duplicate_attributes_dedup(void) {
const char *src = "<div class='a' CLASS=\"b\" class=c>";
SAXCapture cap; capture_init(&cap);
htmlParserCtxtPtr ctxt = make_ctxt(src, HTML_PARSE_NOIMPLIED, &cap);
test_htmlParseStartTag(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap.nevents);
/* Only one 'class' attribute should remain, with the first value 'a' */
const StartEvent *ev = &cap.events[0];
/* Count how many 'class' attributes remain */
int class_count = 0;
for (int i = 0; ev->atts[i] != NULL && ev->atts[i+1] != NULL; i += 2) {
if (xmlStrcasecmp(ev->atts[i], BAD_CAST "class") == 0)
class_count++;
}
TEST_ASSERT_EQUAL_INT(1, class_count);
const xmlChar *v = find_attr_value(ev, "class");
TEST_ASSERT_NOT_NULL(v);
TEST_ASSERT_EQUAL_STRING("a", (const char *)v);
capture_free(&cap);
htmlFreeParserCtxt(ctxt);
}
/* Test: unexpected solidus inside tag (not as '/>') is ignored */
void test_htmlParseStartTag_unexpected_solidus_ignored(void) {
const char *src = "<div / id='x'>";
SAXCapture cap; capture_init(&cap);
htmlParserCtxtPtr ctxt = make_ctxt(src, HTML_PARSE_NOIMPLIED, &cap);
test_htmlParseStartTag(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap.nevents);
TEST_ASSERT_EQUAL_STRING("div", (const char *)cap.events[0].name);
const xmlChar *vx = find_attr_value(&cap.events[0], "id");
TEST_ASSERT_NOT_NULL(vx);
TEST_ASSERT_EQUAL_STRING("x", (const char *)vx);
capture_free(&cap);
htmlFreeParserCtxt(ctxt);
}
/* Test: self-closing tags '/>' are handled and produce a startElement event */
void test_htmlParseStartTag_self_closing(void) {
const char *src = "<br/>";
SAXCapture cap; capture_init(&cap);
htmlParserCtxtPtr ctxt = make_ctxt(src, HTML_PARSE_NOIMPLIED, &cap);
test_htmlParseStartTag(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap.nevents);
TEST_ASSERT_EQUAL_STRING("br", (const char *)cap.events[0].name);
TEST_ASSERT_EQUAL_INT(0, cap.events[0].att_count);
capture_free(&cap);
htmlFreeParserCtxt(ctxt);
}
/* Test: incomplete tag without closing '>' is discarded (no SAX events) */
void test_htmlParseStartTag_incomplete_tag_discarded(void) {
const char *src = "<div id='x'";
SAXCapture cap; capture_init(&cap);
htmlParserCtxtPtr ctxt = make_ctxt(src, HTML_PARSE_NOIMPLIED, &cap);
test_htmlParseStartTag(ctxt);
TEST_ASSERT_EQUAL_INT(0, cap.nevents);
capture_free(&cap);
htmlFreeParserCtxt(ctxt);
}
/* Test: many attributes are all passed (exercises attribute storage growth) */
void test_htmlParseStartTag_many_attributes(void) {
/* Build a tag with many attributes */
char buf[4096];
strcpy(buf, "<span");
const int N = 20;
char tmp[64];
for (int i = 0; i < N; i++) {
snprintf(tmp, sizeof(tmp), " a%d='v%d'", i, i);
strcat(buf, tmp);
}
strcat(buf, ">");
SAXCapture cap; capture_init(&cap);
htmlParserCtxtPtr ctxt = make_ctxt(buf, HTML_PARSE_NOIMPLIED, &cap);
test_htmlParseStartTag(ctxt);
TEST_ASSERT_EQUAL_INT(1, cap.nevents);
TEST_ASSERT_EQUAL_STRING("span", (const char *)cap.events[0].name);
TEST_ASSERT_EQUAL_INT(N, cap.events[0].att_count);
/* Spot-check a few attributes */
const StartEvent *ev = &cap.events[0];
const xmlChar *v0 = find_attr_value(ev, "a0");
const xmlChar *v7 = find_attr_value(ev, "a7");
const xmlChar *v19 = find_attr_value(ev, "a19");
TEST_ASSERT_NOT_NULL(v0);
TEST_ASSERT_NOT_NULL(v7);
TEST_ASSERT_NOT_NULL(v19);
TEST_ASSERT_EQUAL_STRING("v0", (const char *)v0);
TEST_ASSERT_EQUAL_STRING("v7", (const char *)v7);
TEST_ASSERT_EQUAL_STRING("v19", (const char *)v19);
capture_free(&cap);
htmlFreeParserCtxt(ctxt);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_htmlParseStartTag_simple_div_noimplied);
RUN_TEST(test_htmlParseStartTag_uppercase_and_attr_lowercased);
RUN_TEST(test_htmlParseStartTag_duplicate_attributes_dedup);
RUN_TEST(test_htmlParseStartTag_unexpected_solidus_ignored);
RUN_TEST(test_htmlParseStartTag_self_closing);
RUN_TEST(test_htmlParseStartTag_incomplete_tag_discarded);
RUN_TEST(test_htmlParseStartTag_many_attributes);
return UNITY_END();
} |