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

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

/* The module provides a global test wrapper for the static function. */
extern htmlParserCtxtPtr test_htmlCreateMemoryParserCtxtInternal(const char *url,
                                   const char *buffer, size_t size,
                                   const char *encoding);

void setUp(void) {
    /* Setup code here, or leave empty */
}

void tearDown(void) {
    /* Cleanup code here, or leave empty */
}

static void free_ctxt_if_not_null(htmlParserCtxtPtr ctxt) {
    if (ctxt != NULL) {
        htmlFreeParserCtxt(ctxt);
    }
}

void test_htmlCreateMemoryParserCtxtInternal_returns_NULL_on_null_buffer(void) {
    htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, NULL, 0, NULL);
    TEST_ASSERT_NULL(ctxt);
}

void test_htmlCreateMemoryParserCtxtInternal_creates_context_with_basic_html(void) {
    const char *buf = "<!DOCTYPE html><html><head><title>T</title></head>"
                      "<body>Hello</body></html>";
    size_t size = strlen(buf);

    htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, buf, size, NULL);
    TEST_ASSERT_NOT_NULL(ctxt);
    free_ctxt_if_not_null(ctxt);
}

void test_htmlCreateMemoryParserCtxtInternal_accepts_zero_length_buffer(void) {
    /* Non-NULL pointer but zero size should still create a context for empty input. */
    const char dummy = 0; /* Any addressable byte is fine; size is 0. */

    htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, &dummy, 0, NULL);
    TEST_ASSERT_NOT_NULL(ctxt);
    free_ctxt_if_not_null(ctxt);
}

void test_htmlCreateMemoryParserCtxtInternal_accepts_non_null_url(void) {
    const char *url = "http://example.com/index.html";
    const char *buf = "<html><body>URL Test</body></html>";
    size_t size = strlen(buf);

    htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(url, buf, size, NULL);
    TEST_ASSERT_NOT_NULL(ctxt);
    free_ctxt_if_not_null(ctxt);
}

void test_htmlCreateMemoryParserCtxtInternal_accepts_explicit_utf8_encoding(void) {
    const char *buf = "<html><body>UTF-8 ✓</body></html>";
    size_t size = strlen(buf);
    const char *enc = "UTF-8";

    htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, buf, size, enc);
    TEST_ASSERT_NOT_NULL(ctxt);
    free_ctxt_if_not_null(ctxt);
}

void test_htmlCreateMemoryParserCtxtInternal_accepts_iso_8859_1_encoding(void) {
    /* Buffer contains 0xE9 (é in ISO-8859-1). */
    const char latin1_html[] = "<html><body>caf\xE9</body></html>";
    size_t size = sizeof(latin1_html) - 1; /* exclude terminating NUL */
    const char *enc = "ISO-8859-1";

    htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, latin1_html, size, enc);
    TEST_ASSERT_NOT_NULL(ctxt);
    free_ctxt_if_not_null(ctxt);
}

void test_htmlCreateMemoryParserCtxtInternal_handles_embedded_nul_bytes(void) {
    /* Explicit size includes an embedded NUL in the buffer. */
    const char html_with_nul[] = "<html><body>abc\0def</body></html>";
    size_t size = sizeof(html_with_nul) - 1; /* include the embedded NUL, exclude trailing terminator */

    htmlParserCtxtPtr ctxt = test_htmlCreateMemoryParserCtxtInternal(NULL, html_with_nul, size, NULL);
    TEST_ASSERT_NOT_NULL(ctxt);
    free_ctxt_if_not_null(ctxt);
}

int main(void) {
    UNITY_BEGIN();

    RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_returns_NULL_on_null_buffer);
    RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_creates_context_with_basic_html);
    RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_accepts_zero_length_buffer);
    RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_accepts_non_null_url);
    RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_accepts_explicit_utf8_encoding);
    RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_accepts_iso_8859_1_encoding);
    RUN_TEST(test_htmlCreateMemoryParserCtxtInternal_handles_embedded_nul_bytes);

    return UNITY_END();
}