-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgockl_test.go
445 lines (396 loc) · 11.5 KB
/
gockl_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
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
package gockl
import (
"bytes"
"fmt"
"io"
"io/ioutil"
"path/filepath"
"reflect"
"strings"
"testing"
)
type DocumentInfo struct {
Data string
ElementNames []string
}
var documents map[string]DocumentInfo = map[string]DocumentInfo{
// taken from https://github.com/golang/go/issues/10158
"doctype subset": {
Data: `<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE doc [
<!ELEMENT doc ANY>
]>
<doc>
</doc>`,
ElementNames: []string{"doc", "doc"},
},
"empty start": {
Data: `<<a`,
ElementNames: []string{},
},
"empty end": {
Data: `</<a`,
ElementNames: []string{""},
},
"empty doctype": {
Data: `<!DOCTYPE[`,
ElementNames: []string{},
},
"simple-svg": {
Data: `<?xml version="1.0" encoding="UTF-8"?>
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" width="100%" height="100%" xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1920 1080">
<style>
/* This is a comment. */
.test {
fill: 'black';
}
</style>
<rect width="1920" height="1080" class="test" fill="red"></rect>
<defs>
<linearGradient id="grad">
<stop stop-color="white" offset="0"></stop>
<stop stop-opacity="0" stop-color="white" offset="1"></stop>
</linearGradient>
</defs>
</svg>`,
ElementNames: []string{
"svg", "style", "style", "rect", "rect", "defs", "linearGradient", "stop", "stop", "stop", "stop", "linearGradient", "defs", "svg",
},
},
}
func passthrough(data string) string {
buf := bytes.Buffer{}
z := New(data)
for {
t, err := z.Next()
if err != nil {
break
}
buf.WriteString(t.Raw())
}
return buf.String()
}
func elements(data string) []string {
r := []string{}
z := New(data)
for {
t, err := z.Next()
if err != nil {
break
}
if el, ok := t.(ElementToken); ok {
r = append(r, el.Name())
}
}
return r
}
func Test_NoChange(t *testing.T) {
for name, info := range documents {
if info.Data != passthrough(info.Data) {
t.Errorf("Error processing document '%s'", name)
}
}
}
func Test_ElementNames(t *testing.T) {
for name, info := range documents {
elements := elements(info.Data)
for pos, expected := range info.ElementNames {
if pos >= len(elements) {
t.Errorf("Element pos %d not existing for document %s", pos, name)
} else if actual := elements[pos]; actual != expected {
t.Errorf("Element name not matching at pos %d for document %s: %s (actual) != %s (expected)", pos, name, actual, expected)
}
}
}
}
func Test_BrokenStartElement(t *testing.T) {
input := "<elem"
decoder := New(input)
tok, err := decoder.Next()
if err != nil {
t.Error("Error while getting token.")
}
if _, ok := tok.(StartElementToken); !ok {
t.Errorf("Not a start element token: %s, %s", input, reflect.TypeOf(tok))
}
if tok.Raw() != input {
t.Errorf("Token text not matching: %s (expected) != %s (actual)", input, tok.Raw())
}
if next, err := decoder.Next(); next != nil || err != io.EOF {
t.Errorf("Wanted EOF, got: '%s'/%s", next, err)
}
}
func TestGreaterSymbol(t *testing.T) {
for input, content := range map[string]string{
"<style>h1 > i { color: red !important; }</style>": `h1 > i { color: red !important; }`,
"<button email='Someone <[email protected]>'>Contact</button>": "Contact",
} {
decoder := New(input)
tok, err := decoder.Next()
if err != nil {
t.Errorf("Error while getting token from %s: %s", input, err)
}
if _, ok := tok.(StartElementToken); !ok {
t.Errorf("Not a start element token: %s, %s", input, reflect.TypeOf(tok))
}
contentTok, err := decoder.Next()
if err != nil {
t.Errorf("Error while getting token from %s: %s", input, err)
}
if _, ok := contentTok.(TextToken); !ok {
t.Errorf("Not a text token: %s, %s", input, reflect.TypeOf(contentTok))
}
if contentTok.Raw() != content {
t.Errorf("Token text not matching: %s (expected) != %s (actual)", content, contentTok.Raw())
}
// skip end element
_, _ = decoder.Next()
if next, err := decoder.Next(); next != nil || err != io.EOF {
t.Errorf("Wanted EOF, got: '%s'/%s", next, err)
}
}
}
func TestNewlineInElements(t *testing.T) {
for input, typ := range map[string]rune{
"<a\nb>": '<',
"<a\nb\n>": '<',
"<\na\nb=c\n>": '<',
"</a>": '>',
"</\na>": '>',
"</a\n>": '>',
"<a\n name='b'\n content='c'\n/>": '/',
} {
decoder := New(input)
tok, err := decoder.Next()
if err != nil {
t.Errorf("Error while getting token from %s: %s", input, err)
}
if typ == '<' {
if _, ok := tok.(StartElementToken); !ok {
t.Errorf("Not a start element token: %s, %s", input, reflect.TypeOf(tok))
}
} else if typ == '>' {
if _, ok := tok.(EndElementToken); !ok {
t.Errorf("Not an end element token: %s, %s", input, reflect.TypeOf(tok))
}
} else if typ == '/' {
if _, ok := tok.(EmptyElementToken); !ok {
t.Errorf("Not an empty element token: %s, %s", input, reflect.TypeOf(tok))
}
}
if tok.Raw() != input {
t.Errorf("Token text not matching: %s (expected) != %s (actual)", input, tok.Raw())
}
if next, err := decoder.Next(); next != nil || err != io.EOF {
t.Errorf("Wanted EOF, got: '%s'/%s", next, err)
}
}
}
func Test_BrokenTextElement(t *testing.T) {
input := "/asdkjlh"
decoder := New(input)
tok, err := decoder.Next()
if err != nil {
t.Error("Error while getting token.")
}
if _, ok := tok.(TextToken); !ok {
t.Errorf("Not a text token: %s, %s", input, reflect.TypeOf(tok))
}
if tok.Raw() != input {
t.Errorf("Token text not matching: %s (expected) != %s (actual)", input, tok.Raw())
}
if next, err := decoder.Next(); next != nil || err != io.EOF {
t.Errorf("Wanted EOF, got: '%s'/%s", next, err)
}
}
// I have a directory of ~15.000 XML files created using: find ~ -name '*.xml' -exec cp {} ./XML \;
func Test_RealLifeFiles(t *testing.T) {
files, err := ioutil.ReadDir(filepath.Join("testdata", "xml"))
if err != nil {
return
}
for _, fi := range files {
raw, err := ioutil.ReadFile(filepath.Join("testdata", "xml", fi.Name()))
if err != nil {
t.Logf("Error reading %s: %s", fi.Name(), err)
continue
}
input := string(raw)
if output := passthrough(input); output != input {
outfile := fmt.Sprintf("%s.tmp", filepath.Join("testdata", "xml", fi.Name()))
t.Errorf("Error processing document '%s', writing output to %s for comparing to ", fi.Name(), outfile)
ioutil.WriteFile(outfile, []byte(output), 0644)
return
}
//t.Logf("%d/%d files checked.", i+1, len(files))
}
}
func Test_Attributes(t *testing.T) {
svg := StartElementToken(`<svg xmlns="http://www.w3.org/2000/svg" version=1.1 width='100%' height='a + b' xmlns:xlink="http://www.w3.org/1999/xlink" viewBox="0 0 1920 1080" bla=blub bla>`)
attrs := []Attribute{
{"xmlns", "http://www.w3.org/2000/svg"},
{"version", "1.1"},
{"width", "100%"},
{"height", "a + b"},
{"xmlns:xlink", "http://www.w3.org/1999/xlink"},
{"viewBox", "0 0 1920 1080"},
{"bla", "blub"},
{"bla", ""},
}
result := svg.Attributes()
if len(result) != len(attrs) {
t.Error("Attribute count not matching")
t.Errorf("Expected: %v", attrs)
t.Errorf("Got: %v", result)
}
for i, a := range result {
if i >= len(attrs) {
t.Errorf("Unexpected attribute: %s", a)
} else if attrs[i] != a {
t.Errorf("Attributes not matching. Expected %s, got %s", attrs[i], a)
}
}
}
func Test_AttributesInEmptyElements(t *testing.T) {
svg := EmptyElementToken(`<circle cx="50" cy="25" r="20" fill="yellow" />`)
attrs := []Attribute{
{"cx", "50"},
{"cy", "25"},
{"r", "20"},
{"fill", "yellow"},
}
if "circle" != svg.Name() {
t.Error("No a circle")
}
result := svg.Attributes()
if len(result) != len(attrs) {
t.Error("Attribute count not matching")
t.Errorf("Expected: %v", attrs)
t.Errorf("Got: %v", result)
}
for i, a := range result {
if i >= len(attrs) {
t.Errorf("Unexpected attribute: %s", a)
} else if attrs[i] != a {
t.Errorf("Attributes not matching. Expected %s, got %s", attrs[i], a)
}
}
}
func TestGettingAttributesByName(t *testing.T) {
type AttribTest struct {
Token Token
Attributes []Attribute
NonAttributes []string
}
testdata := []AttribTest{
{
EmptyElementToken(`<circle cx="50" cy="25" r="20" fill="yellow" />`),
[]Attribute{
{Name: "cx", Content: "50"},
{Name: "R", Content: "20"},
},
[]string{"bla"},
},
{
StartElementToken(`<group style="fill: none;" style="nope">`),
[]Attribute{
{Name: "style", Content: "fill: none;"},
},
[]string{"r"},
},
}
for _, i := range testdata {
tok, ok := i.Token.(StartOrEmptyElementToken)
if !ok {
t.FailNow()
}
for _, a := range i.Attributes {
for _, fn := range []func(string) string{
strings.ToLower,
strings.ToUpper,
func(x string) string { return x },
} {
name := fn(a.Name)
res, ok := tok.Attribute(name)
if !ok {
t.Errorf("Missing attribute %s in token %s", name, i.Token)
continue
}
if res != a.Content {
t.Errorf("Wrong attribute %s in token %s: Expected %s, got %s", name, i.Token, a.Content, res)
}
}
}
}
}
func Test_CDATA(t *testing.T) {
doc := New(`<p><![CDATA[</p>]]><!-- </p> --></p>`)
if tok, err := doc.Next(); err != nil {
t.Error(err)
} else if start, ok := tok.(StartElementToken); !ok {
t.Errorf("Expected start element token, got: %s", start)
}
if tok, err := doc.Next(); err != nil {
t.Error(err)
} else if cdata, ok := tok.(CDATAToken); !ok {
t.Errorf("Expected CDATA token, got: %s", cdata)
} else if raw := cdata.Raw(); raw != `<![CDATA[</p>]]>` {
t.Errorf("Wrong content for CDATA token: %s", raw)
}
if tok, err := doc.Next(); err != nil {
t.Error(err)
} else if comment, ok := tok.(CommentToken); !ok {
t.Errorf("Expected end element token, got: %s", comment)
} else if raw := comment.Raw(); raw != `<!-- </p> -->` {
t.Errorf("Wrong content for end token: %s", raw)
}
if tok, err := doc.Next(); err != nil {
t.Error(err)
} else if end, ok := tok.(EndElementToken); !ok {
t.Errorf("Expected end element token, got: %s", end)
} else if raw := end.Raw(); raw != `</p>` {
t.Errorf("Wrong content for end token: %s", raw)
}
}
func getAllTokens(data string) []Token {
r := []Token{}
z := New(data)
for {
t, err := z.Next()
if err != nil {
break
}
r = append(r, t)
}
return r
}
// Tests taken from https://mionskowski.pl/posts/unmasking-go-html-parser-bug/
// we should add differential fuzzing to our tests, too
func TestParsingFringeCases(t *testing.T) {
type docInfo struct {
Data string
Tokens []Token
}
var tests map[string]docInfo = map[string]docInfo{
"dangling =": {
Data: `<script =">alert(1)</script>`,
Tokens: []Token{StartElementToken(`<script =">`), TextToken(`alert(1)`), EndElementToken(`</script>`)},
},
"= after /": {
Data: `<A/=">`,
Tokens: []Token{StartElementToken(`<A/=">`)},
},
}
for name, info := range tests {
tokens := getAllTokens(info.Data)
for pos, expected := range info.Tokens {
if pos >= len(tokens) {
t.Errorf("Token pos %d not existing for document %s", pos, name)
} else if actual := tokens[pos]; reflect.TypeOf(actual) != reflect.TypeOf(expected) {
t.Errorf("Token type not matching at pos %d for input data `%s`: %s (actual) != %s (expected)", pos, name, reflect.TypeOf(actual), reflect.TypeOf(expected))
} else if actual := tokens[pos]; actual.Raw() != expected.Raw() {
t.Errorf("Token content not matching at pos %d for input data `%s`: %s (actual) != %s (expected)", pos, name, actual, expected)
}
}
}
}