File size: 8,938 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 |
#include "unity/unity.h"
#include <libxml/HTMLparser.h>
#include <stdlib.h>
#include <string.h>
/* External wrapper for the static function under test */
extern void test_htmlCharDataSAXCallback(htmlParserCtxtPtr ctxt, const xmlChar *buf, int size, int mode);
typedef struct {
int chars_calls;
int ign_calls;
int cdata_calls;
char chars_text[512];
char ign_text[512];
char cdata_text[512];
} Capture;
static void reset_capture(Capture *cap) {
if (!cap) return;
memset(cap, 0, sizeof(*cap));
}
static void cb_characters(void *userData, const xmlChar *ch, int len) {
Capture *cap = (Capture *)userData;
if (!cap || !ch || len <= 0) return;
int copy = len;
if (copy > (int)(sizeof(cap->chars_text) - 1 - (int)strlen(cap->chars_text)))
copy = (int)(sizeof(cap->chars_text) - 1 - strlen(cap->chars_text));
strncat(cap->chars_text, (const char *)ch, copy);
cap->chars_calls++;
}
static void cb_ignorableWhitespace(void *userData, const xmlChar *ch, int len) {
Capture *cap = (Capture *)userData;
if (!cap || !ch || len <= 0) return;
int copy = len;
if (copy > (int)(sizeof(cap->ign_text) - 1 - (int)strlen(cap->ign_text)))
copy = (int)(sizeof(cap->ign_text) - 1 - strlen(cap->ign_text));
strncat(cap->ign_text, (const char *)ch, copy);
cap->ign_calls++;
}
static void cb_cdataBlock(void *userData, const xmlChar *ch, int len) {
Capture *cap = (Capture *)userData;
if (!cap || !ch || len <= 0) return;
int copy = len;
if (copy > (int)(sizeof(cap->cdata_text) - 1 - (int)strlen(cap->cdata_text)))
copy = (int)(sizeof(cap->cdata_text) - 1 - strlen(cap->cdata_text));
strncat(cap->cdata_text, (const char *)ch, copy);
cap->cdata_calls++;
}
static void prepare_sax(xmlSAXHandler *sax,
xmlCharactersSAXFunc chars,
xmlIgnorableWhitespaceSAXFunc ign,
xmlCDataBlockSAXFunc cdata) {
memset(sax, 0, sizeof(*sax));
sax->characters = chars;
sax->ignorableWhitespace = ign;
sax->cdataBlock = cdata;
}
void setUp(void) {
/* Setup code here, or leave empty */
}
void tearDown(void) {
/* Cleanup code here, or leave empty */
}
/* Test: No SAX handler => no output */
void test_htmlCharDataSAXCallback_null_sax(void) {
htmlParserCtxtPtr ctxt = htmlNewParserCtxt();
TEST_ASSERT_NOT_NULL(ctxt);
Capture cap;
reset_capture(&cap);
ctxt->sax = NULL; /* Critical for this test */
ctxt->userData = ∩
ctxt->disableSAX = 0;
ctxt->keepBlanks = 0;
ctxt->name = (const xmlChar *)"div";
const char *data = "abc";
test_htmlCharDataSAXCallback(ctxt, (const xmlChar *)data, (int)strlen(data), 0);
TEST_ASSERT_EQUAL_INT(0, cap.chars_calls);
TEST_ASSERT_EQUAL_INT(0, cap.ign_calls);
TEST_ASSERT_EQUAL_INT(0, cap.cdata_calls);
htmlFreeParserCtxt(ctxt);
}
/* Test: disableSAX => no output even with handlers */
void test_htmlCharDataSAXCallback_disabled_sax(void) {
htmlParserCtxtPtr ctxt = htmlNewParserCtxt();
TEST_ASSERT_NOT_NULL(ctxt);
xmlSAXHandler sax;
Capture cap;
reset_capture(&cap);
prepare_sax(&sax, cb_characters, cb_ignorableWhitespace, cb_cdataBlock);
ctxt->sax = &sax;
ctxt->userData = ∩
ctxt->disableSAX = 1;
ctxt->keepBlanks = 0;
ctxt->name = (const xmlChar *)"div";
const char *data = "abc";
test_htmlCharDataSAXCallback(ctxt, (const xmlChar *)data, (int)strlen(data), 0);
TEST_ASSERT_EQUAL_INT(0, cap.chars_calls);
TEST_ASSERT_EQUAL_INT(0, cap.ign_calls);
TEST_ASSERT_EQUAL_INT(0, cap.cdata_calls);
htmlFreeParserCtxt(ctxt);
}
/* Test: In <html>, leading whitespace goes to ignorableWhitespace when keepBlanks == 0, rest to characters */
void test_htmlCharDataSAXCallback_html_leading_ws_keepBlanks_false(void) {
htmlParserCtxtPtr ctxt = htmlNewParserCtxt();
TEST_ASSERT_NOT_NULL(ctxt);
xmlSAXHandler sax;
Capture cap;
reset_capture(&cap);
prepare_sax(&sax, cb_characters, cb_ignorableWhitespace, cb_cdataBlock);
ctxt->sax = &sax;
ctxt->userData = ∩
ctxt->disableSAX = 0;
ctxt->keepBlanks = 0;
ctxt->name = (const xmlChar *)"html";
const char *data = " \t\nabc";
test_htmlCharDataSAXCallback(ctxt, (const xmlChar *)data, (int)strlen(data), 0);
TEST_ASSERT_EQUAL_INT(1, cap.ign_calls);
TEST_ASSERT_EQUAL_STRING(" \t\n", cap.ign_text);
TEST_ASSERT_EQUAL_INT(1, cap.chars_calls);
TEST_ASSERT_EQUAL_STRING("abc", cap.chars_text);
TEST_ASSERT_EQUAL_INT(0, cap.cdata_calls);
htmlFreeParserCtxt(ctxt);
}
/* Test: In <head>, leading whitespace is also delivered as characters when keepBlanks == 1, then remaining as characters */
void test_htmlCharDataSAXCallback_head_leading_ws_keepBlanks_true(void) {
htmlParserCtxtPtr ctxt = htmlNewParserCtxt();
TEST_ASSERT_NOT_NULL(ctxt);
xmlSAXHandler sax;
Capture cap;
reset_capture(&cap);
prepare_sax(&sax, cb_characters, cb_ignorableWhitespace, cb_cdataBlock);
ctxt->sax = &sax;
ctxt->userData = ∩
ctxt->disableSAX = 0;
ctxt->keepBlanks = 1;
ctxt->name = (const xmlChar *)"head";
const char *data = " abc";
test_htmlCharDataSAXCallback(ctxt, (const xmlChar *)data, (int)strlen(data), 0);
TEST_ASSERT_EQUAL_INT(2, cap.chars_calls);
TEST_ASSERT_EQUAL_STRING(" abc", cap.chars_text);
TEST_ASSERT_EQUAL_INT(0, cap.ign_calls);
TEST_ASSERT_EQUAL_INT(0, cap.cdata_calls);
htmlFreeParserCtxt(ctxt);
}
/* Test: Only whitespace in <html> with keepBlanks == 0 results only in ignorableWhitespace, then early return */
void test_htmlCharDataSAXCallback_html_only_ws_keepBlanks_false(void) {
htmlParserCtxtPtr ctxt = htmlNewParserCtxt();
TEST_ASSERT_NOT_NULL(ctxt);
xmlSAXHandler sax;
Capture cap;
reset_capture(&cap);
prepare_sax(&sax, cb_characters, cb_ignorableWhitespace, cb_cdataBlock);
ctxt->sax = &sax;
ctxt->userData = ∩
ctxt->disableSAX = 0;
ctxt->keepBlanks = 0;
ctxt->name = (const xmlChar *)"html";
const char *data = " ";
test_htmlCharDataSAXCallback(ctxt, (const xmlChar *)data, (int)strlen(data), 0);
TEST_ASSERT_EQUAL_INT(1, cap.ign_calls);
TEST_ASSERT_EQUAL_STRING(" ", cap.ign_text);
TEST_ASSERT_EQUAL_INT(0, cap.chars_calls);
TEST_ASSERT_EQUAL_INT(0, cap.cdata_calls);
htmlFreeParserCtxt(ctxt);
}
/* Test: Nonzero non-RCDATA mode with cdataBlock present uses cdataBlock only */
void test_htmlCharDataSAXCallback_cdata_mode_nonzero(void) {
htmlParserCtxtPtr ctxt = htmlNewParserCtxt();
TEST_ASSERT_NOT_NULL(ctxt);
xmlSAXHandler sax;
Capture cap;
reset_capture(&cap);
prepare_sax(&sax, cb_characters, cb_ignorableWhitespace, cb_cdataBlock);
ctxt->sax = &sax;
ctxt->userData = ∩
ctxt->disableSAX = 0;
ctxt->keepBlanks = 0;
ctxt->name = (const xmlChar *)"div";
const char *data = " CDATA <kept> ";
/* Pick a mode value very unlikely to equal DATA_RCDATA */
int mode = 9999;
test_htmlCharDataSAXCallback(ctxt, (const xmlChar *)data, (int)strlen(data), mode);
TEST_ASSERT_EQUAL_INT(0, cap.ign_calls);
TEST_ASSERT_EQUAL_INT(0, cap.chars_calls);
TEST_ASSERT_EQUAL_INT(1, cap.cdata_calls);
TEST_ASSERT_EQUAL_STRING(" CDATA <kept> ", cap.cdata_text);
htmlFreeParserCtxt(ctxt);
}
/* Test: cdataBlock is NULL with nonzero mode falls back to characters path */
void test_htmlCharDataSAXCallback_nonzero_mode_no_cdataBlock_falls_back(void) {
htmlParserCtxtPtr ctxt = htmlNewParserCtxt();
TEST_ASSERT_NOT_NULL(ctxt);
xmlSAXHandler sax;
Capture cap;
reset_capture(&cap);
/* Set cdataBlock to NULL to force the non-CDATA branch */
prepare_sax(&sax, cb_characters, cb_ignorableWhitespace, NULL);
ctxt->sax = &sax;
ctxt->userData = ∩
ctxt->disableSAX = 0;
ctxt->keepBlanks = 1; /* Avoid areBlanks checks */
ctxt->name = (const xmlChar *)"div";
const char *data = "abc";
int mode = 9999;
test_htmlCharDataSAXCallback(ctxt, (const xmlChar *)data, (int)strlen(data), mode);
TEST_ASSERT_EQUAL_INT(1, cap.chars_calls);
TEST_ASSERT_EQUAL_STRING("abc", cap.chars_text);
TEST_ASSERT_EQUAL_INT(0, cap.ign_calls);
TEST_ASSERT_EQUAL_INT(0, cap.cdata_calls);
htmlFreeParserCtxt(ctxt);
}
int main(void) {
UNITY_BEGIN();
RUN_TEST(test_htmlCharDataSAXCallback_null_sax);
RUN_TEST(test_htmlCharDataSAXCallback_disabled_sax);
RUN_TEST(test_htmlCharDataSAXCallback_html_leading_ws_keepBlanks_false);
RUN_TEST(test_htmlCharDataSAXCallback_head_leading_ws_keepBlanks_true);
RUN_TEST(test_htmlCharDataSAXCallback_html_only_ws_keepBlanks_false);
RUN_TEST(test_htmlCharDataSAXCallback_cdata_mode_nonzero);
RUN_TEST(test_htmlCharDataSAXCallback_nonzero_mode_no_cdataBlock_falls_back);
return UNITY_END();
} |