| | #include "unity/unity.h" |
| | #include <libxml/HTMLparser.h> |
| |
|
| | #include <libxml/parser.h> |
| | #include <libxml/parserInternals.h> |
| | #include <string.h> |
| | #include <stdlib.h> |
| |
|
| | |
| | extern int test_htmlParseLookupString(xmlParserCtxtPtr ctxt, size_t startDelta, |
| | const char *str, size_t strLen, size_t extraLen); |
| |
|
| | static xmlParserCtxtPtr make_ctxt(const char *buf) { |
| | |
| | size_t len = strlen(buf); |
| | xmlParserCtxtPtr ctxt = xmlCreateMemoryParserCtxt(buf, (int)len); |
| | return ctxt; |
| | } |
| |
|
| | void setUp(void) { |
| | xmlInitParser(); |
| | } |
| |
|
| | void tearDown(void) { |
| | |
| | xmlCleanupParser(); |
| | } |
| |
|
| | |
| | void test_htmlParseLookupString_basic_found(void) { |
| | const char *buf = "zzabcYYY"; |
| | xmlParserCtxtPtr ctxt = make_ctxt(buf); |
| | TEST_ASSERT_NOT_NULL(ctxt); |
| | TEST_ASSERT_NOT_NULL(ctxt->input); |
| |
|
| | int ret = test_htmlParseLookupString(ctxt, 2, "abc", 3, 0); |
| | TEST_ASSERT_EQUAL_INT(2, ret); |
| | TEST_ASSERT_EQUAL_INT(0, ctxt->checkIndex); |
| |
|
| | xmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | |
| | void test_htmlParseLookupString_not_found_sets_checkIndex_rescan(void) { |
| | const char *buf = "abcdef"; |
| | xmlParserCtxtPtr ctxt = make_ctxt(buf); |
| | TEST_ASSERT_NOT_NULL(ctxt); |
| | TEST_ASSERT_NOT_NULL(ctxt->input); |
| |
|
| | |
| | int ret = test_htmlParseLookupString(ctxt, 0, "xyz", 3, 2); |
| | TEST_ASSERT_EQUAL_INT(-1, ret); |
| | TEST_ASSERT_EQUAL_INT(2, ctxt->checkIndex); |
| |
|
| | xmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | |
| | void test_htmlParseLookupString_found_but_too_close_to_end_treated_as_not_found(void) { |
| | const char *buf = "abXY"; |
| | xmlParserCtxtPtr ctxt = make_ctxt(buf); |
| | TEST_ASSERT_NOT_NULL(ctxt); |
| | TEST_ASSERT_NOT_NULL(ctxt->input); |
| |
|
| | |
| | |
| | int ret = test_htmlParseLookupString(ctxt, 0, "XY", 2, 2); |
| | TEST_ASSERT_EQUAL_INT(-1, ret); |
| | TEST_ASSERT_EQUAL_INT(1, ctxt->checkIndex); |
| |
|
| | xmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | |
| | void test_htmlParseLookupString_uses_checkIndex_over_startDelta_and_resets_on_success(void) { |
| | const char *buf = "1234567abc"; |
| | xmlParserCtxtPtr ctxt = make_ctxt(buf); |
| | TEST_ASSERT_NOT_NULL(ctxt); |
| | TEST_ASSERT_NOT_NULL(ctxt->input); |
| |
|
| | |
| | |
| | int ret1 = test_htmlParseLookupString(ctxt, 0, "abc", 3, 4); |
| | TEST_ASSERT_EQUAL_INT(-1, ret1); |
| | TEST_ASSERT_EQUAL_INT(4, ctxt->checkIndex); |
| |
|
| | |
| | |
| | int ret2 = test_htmlParseLookupString(ctxt, 8, "abc", 3, 0); |
| | TEST_ASSERT_EQUAL_INT(7, ret2); |
| | TEST_ASSERT_EQUAL_INT(0, ctxt->checkIndex); |
| |
|
| | xmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | |
| | void test_htmlParseLookupString_rescan_ge_remaining_forces_zero_index(void) { |
| | const char *buf = "abcde"; |
| | xmlParserCtxtPtr ctxt = make_ctxt(buf); |
| | TEST_ASSERT_NOT_NULL(ctxt); |
| | TEST_ASSERT_NOT_NULL(ctxt->input); |
| |
|
| | |
| | int ret = test_htmlParseLookupString(ctxt, 0, "nop", 3, 3); |
| | TEST_ASSERT_EQUAL_INT(-1, ret); |
| | TEST_ASSERT_EQUAL_INT(0, ctxt->checkIndex); |
| |
|
| | xmlFreeParserCtxt(ctxt); |
| | } |
| |
|
| | int main(void) { |
| | UNITY_BEGIN(); |
| | RUN_TEST(test_htmlParseLookupString_basic_found); |
| | RUN_TEST(test_htmlParseLookupString_not_found_sets_checkIndex_rescan); |
| | RUN_TEST(test_htmlParseLookupString_found_but_too_close_to_end_treated_as_not_found); |
| | RUN_TEST(test_htmlParseLookupString_uses_checkIndex_over_startDelta_and_resets_on_success); |
| | RUN_TEST(test_htmlParseLookupString_rescan_ge_remaining_forces_zero_index); |
| | return UNITY_END(); |
| | } |