| | #include "unity/unity.h" |
| | #include <libxml/HTMLparser.h> |
| | #include <string.h> |
| | #include <stdlib.h> |
| |
|
| | |
| | int test_htmlParseCharData(htmlParserCtxtPtr ctxt, int partial); |
| |
|
| | |
| | static htmlParserCtxtPtr make_ctxt_buf(const char *buf, int len) { |
| | htmlParserCtxtPtr ctxt = htmlCreateMemoryParserCtxt(buf, len); |
| | TEST_ASSERT_NOT_NULL_MESSAGE(ctxt, "htmlCreateMemoryParserCtxt returned NULL"); |
| | TEST_ASSERT_NOT_NULL_MESSAGE(ctxt->input, "Parser context has no input"); |
| | |
| | ctxt->sax = NULL; |
| | ctxt->disableSAX = 1; |
| | |
| | ctxt->endCheckState = 0; |
| | |
| | ctxt->input->line = 1; |
| | ctxt->input->col = 1; |
| | return ctxt; |
| | } |
| |
|
| | |
| | static htmlParserCtxtPtr make_ctxt_str(const char *s) { |
| | return make_ctxt_buf(s, (int)strlen(s)); |
| | } |
| |
|
| | void setUp(void) { |
| | |
| | } |
| |
|
| | void tearDown(void) { |
| | |
| | } |
| |
|
| | |
| | static void test_htmlParseCharData_stops_at_lt_and_returns_complete(void) { |
| | const char *data = "hello<world"; |
| | htmlParserCtxtPtr ctxt = make_ctxt_str(data); |
| |
|
| | int ret = test_htmlParseCharData(ctxt, 0); |
| |
|
| | TEST_ASSERT_EQUAL_INT(1, ret); |
| | TEST_ASSERT_NOT_NULL(ctxt->input); |
| | TEST_ASSERT_TRUE(ctxt->input->cur < ctxt->input->end); |
| | TEST_ASSERT_EQUAL_CHAR('<', (char)*ctxt->input->cur); |
| | |
| | TEST_ASSERT_EQUAL_INT(0, ctxt->endCheckState); |
| |
|
| | htmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | |
| | static void test_htmlParseCharData_partial_incomplete_entity_not_consumed(void) { |
| | const char *data = "&"; |
| | htmlParserCtxtPtr ctxt = make_ctxt_str(data); |
| |
|
| | int ret = test_htmlParseCharData(ctxt, 1); |
| |
|
| | TEST_ASSERT_EQUAL_INT(0, ret); |
| | TEST_ASSERT_TRUE(ctxt->input->cur < ctxt->input->end); |
| | TEST_ASSERT_EQUAL_CHAR('&', (char)*ctxt->input->cur); |
| | |
| | TEST_ASSERT_EQUAL_INT(0, ctxt->endCheckState); |
| |
|
| | htmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | |
| | static void test_htmlParseCharData_consumes_nul_then_stops_at_lt(void) { |
| | const char raw[] = { '\0', '<', 'a', 'b', 'c' }; |
| | htmlParserCtxtPtr ctxt = make_ctxt_buf(raw, (int)sizeof(raw)); |
| |
|
| | int ret = test_htmlParseCharData(ctxt, 0); |
| |
|
| | TEST_ASSERT_EQUAL_INT(1, ret); |
| | TEST_ASSERT_TRUE(ctxt->input->cur < ctxt->input->end); |
| | TEST_ASSERT_EQUAL_CHAR('<', (char)*ctxt->input->cur); |
| | TEST_ASSERT_EQUAL_INT(0, ctxt->endCheckState); |
| |
|
| | htmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | |
| | static void test_htmlParseCharData_cr_translated_and_skipped_then_lt(void) { |
| | const char raw[] = { '\r', '<', 'x' }; |
| | htmlParserCtxtPtr ctxt = make_ctxt_buf(raw, (int)sizeof(raw)); |
| |
|
| | int ret = test_htmlParseCharData(ctxt, 0); |
| |
|
| | TEST_ASSERT_EQUAL_INT(1, ret); |
| | TEST_ASSERT_TRUE(ctxt->input->cur < ctxt->input->end); |
| | TEST_ASSERT_EQUAL_CHAR('<', (char)*ctxt->input->cur); |
| | TEST_ASSERT_EQUAL_INT(0, ctxt->endCheckState); |
| |
|
| | htmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | |
| | static void test_htmlParseCharData_consumes_numeric_char_ref(void) { |
| | const char *data = "ABC<"; |
| | htmlParserCtxtPtr ctxt = make_ctxt_str(data); |
| |
|
| | int ret = test_htmlParseCharData(ctxt, 0); |
| |
|
| | TEST_ASSERT_EQUAL_INT(1, ret); |
| | TEST_ASSERT_TRUE(ctxt->input->cur < ctxt->input->end); |
| | TEST_ASSERT_EQUAL_CHAR('<', (char)*ctxt->input->cur); |
| | TEST_ASSERT_EQUAL_INT(0, ctxt->endCheckState); |
| |
|
| | htmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | |
| | static void test_htmlParseCharData_nonzero_mode_stops_at_matching_closing_tag(void) { |
| | const char *data = "abc</p>xyz"; |
| | htmlParserCtxtPtr ctxt = make_ctxt_str(data); |
| |
|
| | |
| | ctxt->name = (xmlChar *)"p"; |
| | ctxt->endCheckState = 1; |
| |
|
| | int ret = test_htmlParseCharData(ctxt, 0); |
| |
|
| | TEST_ASSERT_EQUAL_INT(1, ret); |
| | TEST_ASSERT_TRUE(ctxt->input->cur < ctxt->input->end); |
| | TEST_ASSERT_EQUAL_CHAR('<', (char)*ctxt->input->cur); |
| | |
| | TEST_ASSERT_EQUAL_INT(0, ctxt->endCheckState); |
| |
|
| | htmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | |
| | static void test_htmlParseCharData_consumes_until_eof_when_no_lt(void) { |
| | const char *data = "abcdef"; |
| | htmlParserCtxtPtr ctxt = make_ctxt_str(data); |
| |
|
| | int ret = test_htmlParseCharData(ctxt, 0); |
| |
|
| | TEST_ASSERT_EQUAL_INT(0, ret); |
| | TEST_ASSERT_TRUE(ctxt->input->cur == ctxt->input->end); |
| | |
| | TEST_ASSERT_EQUAL_INT(0, ctxt->endCheckState); |
| |
|
| | htmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | int main(void) { |
| | |
| | xmlInitParser(); |
| |
|
| | UNITY_BEGIN(); |
| | RUN_TEST(test_htmlParseCharData_stops_at_lt_and_returns_complete); |
| | RUN_TEST(test_htmlParseCharData_partial_incomplete_entity_not_consumed); |
| | RUN_TEST(test_htmlParseCharData_consumes_nul_then_stops_at_lt); |
| | RUN_TEST(test_htmlParseCharData_cr_translated_and_skipped_then_lt); |
| | RUN_TEST(test_htmlParseCharData_consumes_numeric_char_ref); |
| | RUN_TEST(test_htmlParseCharData_nonzero_mode_stops_at_matching_closing_tag); |
| | RUN_TEST(test_htmlParseCharData_consumes_until_eof_when_no_lt); |
| | int res = UNITY_END(); |
| |
|
| | xmlCleanupParser(); |
| | return res; |
| | } |