File size: 5,022 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
#include "unity/unity.h"
#include <libxml/HTMLparser.h>
#include <libxml/parserInternals.h>

#include <stdlib.h>
#include <string.h>

/* Wrapper provided in the module for the static function */
extern int test_htmlParseLookupCommentEnd(htmlParserCtxtPtr ctxt);

static htmlParserCtxtPtr makeCtxtFromString(const char *s) {
    htmlParserCtxtPtr ctxt = htmlNewParserCtxt();
    TEST_ASSERT_NOT_NULL_MESSAGE(ctxt, "htmlNewParserCtxt failed");

    xmlParserInputPtr in = xmlNewStringInputStream((xmlParserCtxtPtr)ctxt, (const xmlChar *)s);
    TEST_ASSERT_NOT_NULL_MESSAGE(in, "xmlNewStringInputStream failed");

    int pushed = inputPush((xmlParserCtxtPtr)ctxt, in);
    TEST_ASSERT_TRUE_MESSAGE(pushed >= 1, "inputPush failed");

    /* Start fresh for each test */
    ctxt->checkIndex = 0;

    return ctxt;
}

void setUp(void) {
    /* No global init required here; contexts are created per-test */
}

void tearDown(void) {
    /* No global teardown needed */
}

/* Minimal valid comment end: <!--> */
void test_htmlParseLookupCommentEnd_simple_end_arrow(void) {
    const char *s = "<!-->";
    htmlParserCtxtPtr ctxt = makeCtxtFromString(s);

    int ret = test_htmlParseLookupCommentEnd(ctxt);
    TEST_ASSERT_EQUAL_INT(2, ret);
    TEST_ASSERT_EQUAL_INT(0, ctxt->checkIndex);

    htmlFreeParserCtxt(ctxt);
}

/* Invalid: <!--!> should not terminate */
void test_htmlParseLookupCommentEnd_bang_invalid(void) {
    const char *s = "<!--!>";
    htmlParserCtxtPtr ctxt = makeCtxtFromString(s);

    int ret = test_htmlParseLookupCommentEnd(ctxt);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    /* The function will have searched and not found a valid end */
    htmlFreeParserCtxt(ctxt);
}

/* Invalid: <!---!> should not terminate */
void test_htmlParseLookupCommentEnd_dash_bang_invalid(void) {
    const char *s = "<!---!>";
    htmlParserCtxtPtr ctxt = makeCtxtFromString(s);

    int ret = test_htmlParseLookupCommentEnd(ctxt);
    TEST_ASSERT_EQUAL_INT(-1, ret);

    htmlFreeParserCtxt(ctxt);
}

/* Valid: <!----!> should terminate at the --!> (offset 4) */
void test_htmlParseLookupCommentEnd_dash_dash_bang_valid(void) {
    const char *s = "<!----!>";
    htmlParserCtxtPtr ctxt = makeCtxtFromString(s);

    int ret = test_htmlParseLookupCommentEnd(ctxt);
    TEST_ASSERT_EQUAL_INT(4, ret);
    TEST_ASSERT_EQUAL_INT(0, ctxt->checkIndex);

    htmlFreeParserCtxt(ctxt);
}

/* Incomplete buffer: "<!--" requires more data, should return -1 and set checkIndex to 2 */
void test_htmlParseLookupCommentEnd_incomplete_needs_more_offset2(void) {
    const char *s = "<!--";
    htmlParserCtxtPtr ctxt = makeCtxtFromString(s);

    int ret = test_htmlParseLookupCommentEnd(ctxt);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    TEST_ASSERT_EQUAL_INT(2, ctxt->checkIndex);

    htmlFreeParserCtxt(ctxt);
}

/* Incomplete buffer after '!': "<!--!" requires more data, should return -1 and set checkIndex to 2 */
void test_htmlParseLookupCommentEnd_incomplete_after_bang(void) {
    const char *s = "<!--!";
    htmlParserCtxtPtr ctxt = makeCtxtFromString(s);

    int ret = test_htmlParseLookupCommentEnd(ctxt);
    TEST_ASSERT_EQUAL_INT(-1, ret);
    TEST_ASSERT_EQUAL_INT(2, ctxt->checkIndex);

    htmlFreeParserCtxt(ctxt);
}

/* Skip non-terminators until a valid "-->" is found: "<!---a-->" should return 6 */
void test_htmlParseLookupCommentEnd_skip_non_terminators_then_close(void) {
    const char *s = "<!---a-->";
    htmlParserCtxtPtr ctxt = makeCtxtFromString(s);

    int ret = test_htmlParseLookupCommentEnd(ctxt);
    TEST_ASSERT_EQUAL_INT(6, ret);
    TEST_ASSERT_EQUAL_INT(0, ctxt->checkIndex);

    htmlFreeParserCtxt(ctxt);
}

/* Skip past a false '!'-sequence, then close: "<!--!x-->" should return 6 */
void test_htmlParseLookupCommentEnd_skip_false_bang_then_close(void) {
    const char *s = "<!--!x-->";
    htmlParserCtxtPtr ctxt = makeCtxtFromString(s);

    int ret = test_htmlParseLookupCommentEnd(ctxt);
    TEST_ASSERT_EQUAL_INT(6, ret);
    TEST_ASSERT_EQUAL_INT(0, ctxt->checkIndex);

    htmlFreeParserCtxt(ctxt);
}

/* No "--" present at all: "<!>" should return -1 */
void test_htmlParseLookupCommentEnd_no_hyphens(void) {
    const char *s = "<!>";
    htmlParserCtxtPtr ctxt = makeCtxtFromString(s);

    int ret = test_htmlParseLookupCommentEnd(ctxt);
    TEST_ASSERT_EQUAL_INT(-1, ret);

    htmlFreeParserCtxt(ctxt);
}

int main(void) {
    UNITY_BEGIN();

    RUN_TEST(test_htmlParseLookupCommentEnd_simple_end_arrow);
    RUN_TEST(test_htmlParseLookupCommentEnd_bang_invalid);
    RUN_TEST(test_htmlParseLookupCommentEnd_dash_bang_invalid);
    RUN_TEST(test_htmlParseLookupCommentEnd_dash_dash_bang_valid);
    RUN_TEST(test_htmlParseLookupCommentEnd_incomplete_needs_more_offset2);
    RUN_TEST(test_htmlParseLookupCommentEnd_incomplete_after_bang);
    RUN_TEST(test_htmlParseLookupCommentEnd_skip_non_terminators_then_close);
    RUN_TEST(test_htmlParseLookupCommentEnd_skip_false_bang_then_close);
    RUN_TEST(test_htmlParseLookupCommentEnd_no_hyphens);

    return UNITY_END();
}