-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgockl_fuzz_test.go
163 lines (138 loc) · 3.12 KB
/
gockl_fuzz_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
//go:build go1.18
// +build go1.18
package gockl
import (
"fmt"
"io"
"io/ioutil"
"path/filepath"
"reflect"
"strings"
"testing"
)
var seed = []string{
"<doc></doc>",
`<?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>`,
}
func uniqueAttributes(tok StartOrEmptyElementToken) []Attribute {
seen := map[string]struct{}{}
r := []Attribute{}
for _, i := range tok.Attributes() {
low := strings.ToLower(i.Name)
if _, ok := seen[low]; ok {
continue
}
r = append(r, i)
seen[low] = struct{}{}
}
return r
}
func process(str string) error {
z := New(str)
for {
t, err := z.Next()
if err != nil {
if err == io.EOF {
return nil
}
return err
}
if tok, ok := t.(ElementToken); ok {
_ = tok.Name()
}
if tok, ok := t.(StartOrEmptyElementToken); ok {
for _, i := range uniqueAttributes(tok) {
c, ok := tok.Attribute(i.Name)
if !ok {
return fmt.Errorf("unable to find previously found attribute %s", i.Name)
}
if c != i.Content {
return fmt.Errorf("attribute content does not match for %s: %s != %s", i.Name, i.Content, c)
}
}
}
}
}
func decode(data string) ([]Token, error) {
r := []Token{}
z := New(data)
for {
t, err := z.Next()
if err != nil {
if err == io.EOF {
err = nil
}
return r, nil
}
r = append(r, t)
}
}
func encode(tokens []Token) (string, error) {
b := strings.Builder{}
for _, t := range tokens {
if _, err := b.WriteString(t.Raw()); err != nil {
return "", err
}
}
return b.String(), nil
}
func addFixtures(f *testing.F) {
files, err := ioutil.ReadDir(filepath.Join("testdata", "xml"))
if err != nil {
f.Logf("Error opening %s: %s", filepath.Join("testdata", "xml"), err)
return
}
for _, fi := range files {
raw, err := ioutil.ReadFile(filepath.Join("testdata", "xml", fi.Name()))
if err != nil {
f.Logf("Error reading %s: %s", fi.Name(), err)
continue
}
f.Add(string(raw))
}
}
func FuzzParsing(f *testing.F) {
for _, i := range seed {
f.Add(i)
}
addFixtures(f)
f.Fuzz(func(t *testing.T, documentContent string) {
if err := process(documentContent); err != nil {
t.Fatalf("Unable to decode doc %s: %v", documentContent, err)
}
})
}
func FuzzRoundtrip(f *testing.F) {
for _, i := range seed {
f.Add(i)
}
addFixtures(f)
f.Fuzz(func(t *testing.T, document1 string) {
tokens1, err := decode(document1)
if err != nil {
t.Fatalf("Unable to decode doc %s: %v", document1, err)
}
document2, err := encode(tokens1)
if err != nil {
t.Fatalf("Unable to encode document %s we decoded before: %v", document1, err)
}
if !reflect.DeepEqual(document1, document2) {
t.Errorf("%s != %s", document1, document2)
}
})
}