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

#include <libxml/tree.h>
#include <libxml/xmlIO.h>
#include <libxml/parser.h>
#include <string.h>
#include <stdlib.h>

/* Wrapper provided in the module for testing the static function */
extern void test_htmlDtdDumpOutput(xmlOutputBufferPtr buf, xmlDocPtr doc, const char *encoding);

static char* run_dump_and_get_string(xmlDocPtr doc) {
    xmlBufferPtr xbuf = xmlBufferCreate();
    TEST_ASSERT_NOT_NULL_MESSAGE(xbuf, "Failed to create xmlBuffer");

    xmlOutputBufferPtr out = xmlOutputBufferCreateBuffer(xbuf, NULL);
    TEST_ASSERT_NOT_NULL_MESSAGE(out, "Failed to create xmlOutputBuffer");

    test_htmlDtdDumpOutput(out, doc, "utf-8");

    /* Ensure all data is written into the xmlBuffer */
    xmlOutputBufferFlush(out);

    const xmlChar *content = xmlBufferContent(xbuf);
    int len = xmlBufferLength(xbuf);
    if (len < 0) len = 0;

    char *ret = (char *)malloc((size_t)len + 1);
    TEST_ASSERT_NOT_NULL_MESSAGE(ret, "malloc failed");
    if (len > 0 && content != NULL) {
        memcpy(ret, content, (size_t)len);
    }
    ret[len] = '\0';

    xmlOutputBufferClose(out);
    xmlBufferFree(xbuf);

    return ret;
}

void setUp(void) {
    xmlInitParser();
}

void tearDown(void) {
    xmlCleanupParser();
}

static xmlDocPtr make_doc(void) {
    xmlDocPtr doc = xmlNewDoc(BAD_CAST "1.0");
    TEST_ASSERT_NOT_NULL_MESSAGE(doc, "xmlNewDoc failed");
    return doc;
}

void test_htmlDtdDumpOutput_no_subset_outputs_nothing(void) {
    xmlDocPtr doc = make_doc();

    char *out = run_dump_and_get_string(doc);
    TEST_ASSERT_NOT_NULL(out);
    TEST_ASSERT_EQUAL_STRING("", out);

    free(out);
    xmlFreeDoc(doc);
}

void test_htmlDtdDumpOutput_name_only(void) {
    xmlDocPtr doc = make_doc();
    xmlDtdPtr dtd = xmlCreateIntSubset(doc, BAD_CAST "html", NULL, NULL);
    TEST_ASSERT_NOT_NULL_MESSAGE(dtd, "xmlCreateIntSubset failed");

    char *out = run_dump_and_get_string(doc);
    TEST_ASSERT_EQUAL_STRING("<!DOCTYPE html>\n", out);

    free(out);
    xmlFreeDoc(doc);
}

void test_htmlDtdDumpOutput_public_only(void) {
    xmlDocPtr doc = make_doc();
    xmlDtdPtr dtd = xmlCreateIntSubset(doc, BAD_CAST "html",
                                       BAD_CAST "PUBLIC_ID",
                                       NULL);
    TEST_ASSERT_NOT_NULL_MESSAGE(dtd, "xmlCreateIntSubset failed");

    char *out = run_dump_and_get_string(doc);
    TEST_ASSERT_EQUAL_STRING("<!DOCTYPE html PUBLIC \"PUBLIC_ID\">\n", out);

    free(out);
    xmlFreeDoc(doc);
}

void test_htmlDtdDumpOutput_system_only_non_legacy(void) {
    xmlDocPtr doc = make_doc();
    xmlDtdPtr dtd = xmlCreateIntSubset(doc, BAD_CAST "html",
                                       NULL,
                                       BAD_CAST "http://example.com/sys.dtd");
    TEST_ASSERT_NOT_NULL_MESSAGE(dtd, "xmlCreateIntSubset failed");

    char *out = run_dump_and_get_string(doc);
    TEST_ASSERT_EQUAL_STRING("<!DOCTYPE html SYSTEM \"http://example.com/sys.dtd\">\n", out);

    free(out);
    xmlFreeDoc(doc);
}

void test_htmlDtdDumpOutput_system_only_legacy_compat_suppressed(void) {
    xmlDocPtr doc = make_doc();
    xmlDtdPtr dtd = xmlCreateIntSubset(doc, BAD_CAST "html",
                                       NULL,
                                       BAD_CAST "about:legacy-compat");
    TEST_ASSERT_NOT_NULL_MESSAGE(dtd, "xmlCreateIntSubset failed");

    char *out = run_dump_and_get_string(doc);
    /* No SYSTEM printed for about:legacy-compat */
    TEST_ASSERT_EQUAL_STRING("<!DOCTYPE html>\n", out);

    free(out);
    xmlFreeDoc(doc);
}

void test_htmlDtdDumpOutput_public_and_system(void) {
    xmlDocPtr doc = make_doc();
    xmlDtdPtr dtd = xmlCreateIntSubset(doc, BAD_CAST "html",
                                       BAD_CAST "-//W3C//DTD HTML 4.01//EN",
                                       BAD_CAST "http://www.w3.org/TR/html4/strict.dtd");
    TEST_ASSERT_NOT_NULL_MESSAGE(dtd, "xmlCreateIntSubset failed");

    char *out = run_dump_and_get_string(doc);
    TEST_ASSERT_EQUAL_STRING("<!DOCTYPE html PUBLIC \"-//W3C//DTD HTML 4.01//EN\" \"http://www.w3.org/TR/html4/strict.dtd\">\n", out);

    free(out);
    xmlFreeDoc(doc);
}

int main(void) {
    UNITY_BEGIN();

    RUN_TEST(test_htmlDtdDumpOutput_no_subset_outputs_nothing);
    RUN_TEST(test_htmlDtdDumpOutput_name_only);
    RUN_TEST(test_htmlDtdDumpOutput_public_only);
    RUN_TEST(test_htmlDtdDumpOutput_system_only_non_legacy);
    RUN_TEST(test_htmlDtdDumpOutput_system_only_legacy_compat_suppressed);
    RUN_TEST(test_htmlDtdDumpOutput_public_and_system);

    return UNITY_END();
}