|
|
#include "unity/unity.h" |
|
|
#include <libxml/HTMLparser.h> |
|
|
|
|
|
#include <stdlib.h> |
|
|
#include <string.h> |
|
|
#include <stdint.h> |
|
|
|
|
|
|
|
|
void test_htmlSkipBogusDoctype(htmlParserCtxtPtr ctxt); |
|
|
|
|
|
static htmlParserCtxtPtr make_ctxt_from_str(const char *s) { |
|
|
|
|
|
return htmlCreateMemoryParserCtxt(s, (int)strlen(s)); |
|
|
} |
|
|
|
|
|
void setUp(void) { |
|
|
|
|
|
} |
|
|
|
|
|
void tearDown(void) { |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
void test_htmlSkipBogusDoctype_basic(void) { |
|
|
const char *buf = "bogus>"; |
|
|
htmlParserCtxtPtr ctxt = make_ctxt_from_str(buf); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
TEST_ASSERT_NOT_NULL(ctxt->input); |
|
|
|
|
|
|
|
|
ctxt->input->line = 1; |
|
|
ctxt->input->col = 1; |
|
|
|
|
|
test_htmlSkipBogusDoctype(ctxt); |
|
|
|
|
|
|
|
|
int offset = (int)(ctxt->input->cur - ctxt->input->base); |
|
|
TEST_ASSERT_EQUAL_INT(6, offset); |
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(1, ctxt->input->line); |
|
|
TEST_ASSERT_EQUAL_INT(7, ctxt->input->col); |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
} |
|
|
|
|
|
|
|
|
void test_htmlSkipBogusDoctype_with_newlines(void) { |
|
|
const char *buf = "ab\ncd>"; |
|
|
htmlParserCtxtPtr ctxt = make_ctxt_from_str(buf); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
TEST_ASSERT_NOT_NULL(ctxt->input); |
|
|
|
|
|
ctxt->input->line = 1; |
|
|
ctxt->input->col = 1; |
|
|
|
|
|
test_htmlSkipBogusDoctype(ctxt); |
|
|
|
|
|
int offset = (int)(ctxt->input->cur - ctxt->input->base); |
|
|
TEST_ASSERT_EQUAL_INT(6, offset); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(2, ctxt->input->line); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(4, ctxt->input->col); |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
} |
|
|
|
|
|
|
|
|
void test_htmlSkipBogusDoctype_no_gt_reaches_eof(void) { |
|
|
const char *buf = "no gt present"; |
|
|
const int len = (int)strlen(buf); |
|
|
htmlParserCtxtPtr ctxt = make_ctxt_from_str(buf); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
TEST_ASSERT_NOT_NULL(ctxt->input); |
|
|
|
|
|
ctxt->input->line = 5; |
|
|
ctxt->input->col = 10; |
|
|
|
|
|
test_htmlSkipBogusDoctype(ctxt); |
|
|
|
|
|
|
|
|
int offset = (int)(ctxt->input->cur - ctxt->input->base); |
|
|
TEST_ASSERT_EQUAL_INT(len, offset); |
|
|
|
|
|
|
|
|
TEST_ASSERT_EQUAL_INT(5, ctxt->input->line); |
|
|
TEST_ASSERT_EQUAL_INT(10 + len, ctxt->input->col); |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
} |
|
|
|
|
|
|
|
|
void test_htmlSkipBogusDoctype_zero_avail_noop(void) { |
|
|
const char *buf = ">"; |
|
|
htmlParserCtxtPtr ctxt = make_ctxt_from_str(buf); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
TEST_ASSERT_NOT_NULL(ctxt->input); |
|
|
|
|
|
|
|
|
ctxt->input->cur = ctxt->input->end; |
|
|
ctxt->input->line = 3; |
|
|
ctxt->input->col = 7; |
|
|
|
|
|
test_htmlSkipBogusDoctype(ctxt); |
|
|
|
|
|
|
|
|
TEST_ASSERT_TRUE(ctxt->input->cur == ctxt->input->end); |
|
|
TEST_ASSERT_EQUAL_INT(3, ctxt->input->line); |
|
|
TEST_ASSERT_EQUAL_INT(7, ctxt->input->col); |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
} |
|
|
|
|
|
|
|
|
void test_htmlSkipBogusDoctype_long_input(void) { |
|
|
|
|
|
const int xcount = 200; |
|
|
const int total = xcount + 1; |
|
|
char *buf = (char *)malloc((size_t)total + 1); |
|
|
TEST_ASSERT_NOT_NULL(buf); |
|
|
memset(buf, 'x', (size_t)xcount); |
|
|
buf[xcount] = '>'; |
|
|
buf[total] = '\0'; |
|
|
|
|
|
htmlParserCtxtPtr ctxt = make_ctxt_from_str(buf); |
|
|
TEST_ASSERT_NOT_NULL(ctxt); |
|
|
TEST_ASSERT_NOT_NULL(ctxt->input); |
|
|
|
|
|
ctxt->input->line = 1; |
|
|
ctxt->input->col = 1; |
|
|
|
|
|
test_htmlSkipBogusDoctype(ctxt); |
|
|
|
|
|
int offset = (int)(ctxt->input->cur - ctxt->input->base); |
|
|
TEST_ASSERT_EQUAL_INT(total, offset); |
|
|
TEST_ASSERT_EQUAL_INT(1, ctxt->input->line); |
|
|
TEST_ASSERT_EQUAL_INT(1 + total, ctxt->input->col); |
|
|
|
|
|
htmlFreeParserCtxt(ctxt); |
|
|
free(buf); |
|
|
} |
|
|
|
|
|
int main(void) { |
|
|
UNITY_BEGIN(); |
|
|
|
|
|
RUN_TEST(test_htmlSkipBogusDoctype_basic); |
|
|
RUN_TEST(test_htmlSkipBogusDoctype_with_newlines); |
|
|
RUN_TEST(test_htmlSkipBogusDoctype_no_gt_reaches_eof); |
|
|
RUN_TEST(test_htmlSkipBogusDoctype_zero_avail_noop); |
|
|
RUN_TEST(test_htmlSkipBogusDoctype_long_input); |
|
|
|
|
|
return UNITY_END(); |
|
|
} |