forked from lestrrat-go/libxml2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_test.go
359 lines (321 loc) · 9.32 KB
/
parser_test.go
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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
package libxml2
import (
"regexp"
"testing"
"github.com/lestrrat-go/libxml2/dom"
"github.com/lestrrat-go/libxml2/types"
"github.com/lestrrat-go/libxml2/clib"
"github.com/lestrrat-go/libxml2/parser"
"github.com/stretchr/testify/assert"
)
const stdXMLDecl = `<?xml version="1.0"?>` + "\n"
var (
goodWFNSStrings = []string{
stdXMLDecl + `<foobar xmlns:bar="xml://foo" bar:foo="bar"/>` + "\n",
stdXMLDecl + `<foobar xmlns="xml://foo" foo="bar"><foo/></foobar>` + "\n",
stdXMLDecl + `<bar:foobar xmlns:bar="xml://foo" foo="bar"><foo/></bar:foobar>` + "\n",
stdXMLDecl + `<bar:foobar xmlns:bar="xml://foo" foo="bar"><bar:foo/></bar:foobar>` + "\n",
stdXMLDecl + `<bar:foobar xmlns:bar="xml://foo" bar:foo="bar"><bar:foo/></bar:foobar>` + "\n",
}
goodWFStrings = []string{
`<foobar/>`,
`<foobar></foobar>`,
`<foobar></foobar>`,
`<?xml version="1.0" encoding="UTF-8"?>` + "\n" + `<foobar></foobar>`,
`<?xml version="1.0" encoding="ISO-8859-1"?>` + "\n" + `<foobar></foobar>`,
stdXMLDecl + `<foobar> </foobar>` + "\n",
stdXMLDecl + `<foobar><foo/></foobar> `,
stdXMLDecl + `<foobar> <foo/> </foobar> `,
stdXMLDecl + `<foobar><![CDATA[<>&"\` + "`" + `]]></foobar>`,
stdXMLDecl + `<foobar><>&"'</foobar>`,
stdXMLDecl + `<foobar>  </foobar>`,
stdXMLDecl + `<!--comment--><foobar>foo</foobar>`,
stdXMLDecl + `<foobar>foo</foobar><!--comment-->`,
stdXMLDecl + `<foobar>foo<!----></foobar>`,
stdXMLDecl + `<foobar foo="bar"/>`,
stdXMLDecl + `<foobar foo="\` + "`" + `bar>"/>`,
}
goodWFDTDStrings = []string{
stdXMLDecl + `<!DOCTYPE foobar [` + "\n" + `<!ENTITY foo " test ">` + "\n" + `]>` + "\n" + `<foobar>&foo;</foobar>`,
stdXMLDecl + `<!DOCTYPE foobar [<!ENTITY foo "bar">]><foobar>&foo;</foobar>`,
stdXMLDecl + `<!DOCTYPE foobar [<!ENTITY foo "bar">]><foobar>&foo;></foobar>`,
stdXMLDecl + `<!DOCTYPE foobar [<!ENTITY foo "bar="foo"">]><foobar>&foo;></foobar>`,
stdXMLDecl + `<!DOCTYPE foobar [<!ENTITY foo "bar">]><foobar>&foo;></foobar>`,
stdXMLDecl + `<!DOCTYPE foobar [<!ENTITY foo "bar">]><foobar foo="&foo;"/>`,
stdXMLDecl + `<!DOCTYPE foobar [<!ENTITY foo "bar">]><foobar foo=">&foo;"/>`,
}
badWFStrings = []string{
"", // totally empty document
stdXMLDecl, // only XML Declaration
"<!--ouch-->", // comment only is like an empty document
`<!DOCTYPE ouch [<!ENTITY foo "bar">]>`, // no good either ...
"<ouch>", // single tag (tag mismatch)
"<ouch/>foo", // trailing junk
"foo<ouch/>", // leading junk
"<ouch foo=bar/>", // bad attribute
`<ouch foo="bar/>`, // bad attribute
"<ouch>&</ouch>", // bad char
`<ouch>&//0x20;</ouch>`, // bad chart
"<foob<e4>r/>", // bad encoding
"<ouch>&foo;</ouch>", // undefind entity
"<ouch>></ouch>", // unterminated entity
stdXMLDecl + `<!DOCTYPE foobar [<!ENTITY foo "bar">]><foobar &foo;="ouch"/>`, // bad placed entity
stdXMLDecl + `<!DOCTYPE foobar [<!ENTITY foo "bar="foo"">]><foobar &foo;/>`, // even worse
"<ouch><!---></ouch>", // bad comment
"<ouch><!-----></ouch>", // bad either... (is this conform with the spec????)
}
)
func parseShouldSucceed(t *testing.T, opts parser.Option, inputs []string) {
t.Logf("Test parsing with parser %v", opts)
for _, s := range inputs {
d, err := ParseString(s, opts)
if !assert.NoError(t, err, "Parse should succeed") {
return
}
d.Free()
}
}
func parseShouldFail(t *testing.T, opts parser.Option, inputs []string) {
for _, s := range inputs {
d, err := ParseString(s, opts)
if err == nil {
d.Free()
t.Errorf("Expected failure to parse '%s'", s)
}
}
}
type ParseOptionToString struct {
v parser.Option
e string
}
func TestParseOptionStringer(t *testing.T) {
values := []ParseOptionToString{
ParseOptionToString{
v: parser.XMLParseRecover,
e: "Recover",
},
ParseOptionToString{
v: parser.XMLParseNoEnt,
e: "NoEnt",
},
ParseOptionToString{
v: parser.XMLParseDTDLoad,
e: "DTDLoad",
},
ParseOptionToString{
v: parser.XMLParseDTDAttr,
e: "DTDAttr",
},
ParseOptionToString{
v: parser.XMLParseDTDValid,
e: "DTDValid",
},
ParseOptionToString{
v: parser.XMLParseNoError,
e: "NoError",
},
ParseOptionToString{
v: parser.XMLParseNoWarning,
e: "NoWarning",
},
ParseOptionToString{
v: parser.XMLParsePedantic,
e: "Pedantic",
},
ParseOptionToString{
v: parser.XMLParseNoBlanks,
e: "NoBlanks",
},
ParseOptionToString{
v: parser.XMLParseSAX1,
e: "SAX1",
},
ParseOptionToString{
v: parser.XMLParseXInclude,
e: "XInclude",
},
ParseOptionToString{
v: parser.XMLParseNoNet,
e: "NoNet",
},
ParseOptionToString{
v: parser.XMLParseNoDict,
e: "NoDict",
},
ParseOptionToString{
v: parser.XMLParseNsclean,
e: "Nsclean",
},
ParseOptionToString{
v: parser.XMLParseNoCDATA,
e: "NoCDATA",
},
ParseOptionToString{
v: parser.XMLParseNoXIncNode,
e: "NoXIncNode",
},
ParseOptionToString{
v: parser.XMLParseCompact,
e: "Compact",
},
ParseOptionToString{
v: parser.XMLParseOld10,
e: "Old10",
},
ParseOptionToString{
v: parser.XMLParseNoBaseFix,
e: "NoBaseFix",
},
ParseOptionToString{
v: parser.XMLParseHuge,
e: "Huge",
},
ParseOptionToString{
v: parser.XMLParseOldSAX,
e: "OldSAX",
},
ParseOptionToString{
v: parser.XMLParseIgnoreEnc,
e: "IgnoreEnc",
},
ParseOptionToString{
v: parser.XMLParseBigLines,
e: "BigLines",
},
}
for _, d := range values {
if d.v.String() != "["+d.e+"]" {
t.Errorf("e '%s', got '%s'", d.e, d.v.String())
}
}
}
func TestParseEmpty(t *testing.T) {
doc, err := ParseString(``)
if err == nil {
t.Errorf("Parse of empty string should fail")
defer doc.Free()
}
}
func TestParse(t *testing.T) {
inputs := [][]string{
goodWFStrings,
goodWFNSStrings,
goodWFDTDStrings,
}
for _, input := range inputs {
parseShouldSucceed(t, 0, input)
}
}
func TestParseBad(t *testing.T) {
clib.ReportErrors(false)
defer clib.ReportErrors(true)
inputs := [][]string{
badWFStrings,
}
for _, input := range inputs {
parseShouldFail(t, 0, input)
}
}
func TestParseNoBlanks(t *testing.T) {
inputs := [][]string{
goodWFStrings,
goodWFNSStrings,
goodWFDTDStrings,
}
for _, input := range inputs {
parseShouldSucceed(t, parser.XMLParseNoBlanks, input)
}
}
func TestRoundtripNoBlanks(t *testing.T) {
doc, err := ParseString(`<a> <b/> </a>`, parser.XMLParseNoBlanks)
if err != nil {
t.Errorf("failed to parse string: %s", err)
return
}
if !assert.Regexp(t, regexp.MustCompile(`<a><b/></a>`), doc.Dump(false), "stringified xml should have no blanks") {
return
}
}
func TestOptionsShouldCombine(t *testing.T) {
opts := map[parser.Option][]parser.Option{
parser.Option(64): []parser.Option{parser.XMLParseNoWarning},
parser.Option(96): []parser.Option{parser.XMLParseNoWarning, parser.XMLParseNoError},
}
for expected, options := range opts {
p := parser.New(options...)
assert.Equal(t, expected, p.Options)
}
}
func TestGHIssue23(t *testing.T) {
const src = `<?xml version=1.0?>
<rootnode>
<greeting>Hello</greeting>
<goodbye>Goodbye!</goodbye>
</rootnode>`
doc, err := ParseString(src, parser.XMLParseRecover, parser.XMLParseNoWarning, parser.XMLParseNoError)
if !assert.NoError(t, err, "should pass") {
return
}
doc.Free()
}
func TestCommentWrapNodeIssue(t *testing.T) {
// should wrap comment node
const testHTML = "<p><!-- test --></p><!-- test --><p><!-- test --></p>"
doc, err := ParseHTMLString(testHTML, parser.HTMLParseRecover)
if err != nil {
t.Fatalf("Got error when parsing HTML: %v", err)
}
bodyRes, err := doc.Find("//body")
if err != nil {
t.Fatalf("Got error when grabbing body: %v", err)
}
bodyChildren, err := bodyRes.NodeList().First().ChildNodes()
if err != nil {
t.Fatalf("Got error when grabbing body's children: %v", err)
}
if str := bodyChildren.String(); str != testHTML {
t.Fatalf("HTML did not convert back correctly, expected: %v, got: %v.", testHTML, str)
}
}
func TestPiWrapNodeIssue(t *testing.T) {
// should wrap Pi node
const textXML = "<?xml version=\"1.0\" encoding=\"utf-8\"?>\n<a>test <?test?></a>\n"
doc, err := ParseString(textXML)
if err != nil {
t.Fatalf("Got error when parsing xml: %v", err)
}
nodes, err := doc.ChildNodes()
if err != nil {
t.Fatalf("Got error when getting childnodes: %v", err)
}
for _, node := range nodes {
if node.HasChildNodes() {
if _, err := node.ChildNodes(); err != nil {
t.Fatalf("Got error when getting childnodes of childnodes: %v", err)
}
}
}
if str := doc.String(); str != textXML {
t.Fatalf("XML did not convert back correctly, expected: %v, got: %v", textXML, str)
}
}
func TestGetNonexistentAttributeReturnsRecoverableError(t *testing.T) {
const src = `<?xml version="1.0"?><rootnode/>`
doc, err := ParseString(src)
if !assert.NoError(t, err, "Should parse") {
return
}
defer doc.Free()
rootNode, err := doc.DocumentElement()
if !assert.NoError(t, err, "Should find root element") {
return
}
el, ok := rootNode.(types.Element)
if !ok {
t.Fatalf("Root node was not an element")
}
_, err = el.GetAttribute("non-existant")
if err != dom.ErrAttributeNotFound {
t.Fatalf("GetAttribute() error not comparable to existing library")
}
}