-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzzing_test.go
165 lines (150 loc) · 3.49 KB
/
fuzzing_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
package gosaxml_test
import (
"bytes"
"github.com/HBTGmbH/gosaxml"
"github.com/stretchr/testify/assert"
"io"
"math/rand"
"testing"
)
var startNameRunes = []rune(":-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var restNameRunes = []rune("0123456789-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var stringRunes = []rune("/:+*#.!§$%&/[]=?`´'0123456789-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var textRunes = []rune("\"/:+*#'.!§$%&[]=?`´'0123456789-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
var everythingRunes = []rune("<> \t\n\r\"/:+*#'.!§$%&[]=?`´'0123456789-_abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ")
func randGarbage(r *rand.Rand) string {
c := r.Intn(8000)
b := make([]rune, c)
for i := 0; i < c; i++ {
b[i] = everythingRunes[r.Intn(len(everythingRunes))]
}
return string(b)
}
func randName(r *rand.Rand) string {
c := 1 + r.Intn(10)
b := make([]rune, c)
b[0] = startNameRunes[r.Intn(len(startNameRunes))]
for i := 1; i < c; i++ {
b[i] = restNameRunes[r.Intn(len(restNameRunes))]
}
return string(b)
}
func randText(r *rand.Rand) string {
c := 1 + r.Intn(255)
b := make([]rune, c)
for i := 0; i < c; i++ {
b[i] = textRunes[r.Intn(len(textRunes))]
}
return string(b)
}
func randString(r *rand.Rand) string {
c := r.Intn(30)
b := make([]rune, c)
for i := 0; i < c; i++ {
b[i] = stringRunes[r.Intn(len(stringRunes))]
}
return string(b)
}
func buildElement(i int, b *bytes.Buffer, r *rand.Rand, lastOpen bool) bool {
switch i {
case 0:
if lastOpen {
b.WriteString(">")
}
name := randName(r)
b.WriteString("<")
b.WriteString(name)
numAttrs := r.Intn(10)
for j := 0; j < numAttrs; j++ {
b.WriteString(" ")
buildAttribute(b, r)
}
ended := buildElement(r.Intn(2), b, r, true)
if !ended {
b.WriteString("</")
b.WriteString(name)
b.WriteString(">")
}
return false
case 1:
if lastOpen {
b.WriteString(">")
}
b.WriteString(randText(r))
return false
default:
b.WriteString("/>")
return true
}
}
func buildAttribute(b *bytes.Buffer, r *rand.Rand) {
name := randName(r)
value := randString(r)
randName(r)
b.WriteString(name)
b.WriteString("=\"")
b.WriteString(value)
b.WriteString("\"")
}
func TestFuzz(t *testing.T) {
// given
s1 := rand.NewSource(123456789)
r := rand.New(s1)
n := 100000
for i := 0; i < n; i++ {
b := &bytes.Buffer{}
buildElement(0, b, r, false)
xml := b.String()
reader := bytes.NewReader(b.Bytes())
dec := gosaxml.NewDecoder(reader)
w := &bytes.Buffer{}
modifier := gosaxml.NewNamespaceModifier()
enc := gosaxml.NewEncoder(w, modifier)
var tk gosaxml.Token
// when
for {
err := dec.NextToken(&tk)
if err == io.EOF {
break
}
assert.Nil(t, err)
modifier.NamespaceOfToken(&tk)
err = enc.EncodeToken(&tk)
assert.Nil(t, err)
}
assert.Nil(t, enc.Flush())
// then
assert.Equal(t, xml, w.String())
}
}
func TestFuzzNoPanic(t *testing.T) {
// given
s1 := rand.NewSource(123456789)
r := rand.New(s1)
n := 100000
for i := 0; i < n; i++ {
xml := randGarbage(r)
reader := bytes.NewReader([]byte(xml))
dec := gosaxml.NewDecoder(reader)
w := &bytes.Buffer{}
modifier := gosaxml.NewNamespaceModifier()
enc := gosaxml.NewEncoder(w, modifier)
var tk gosaxml.Token
// when
for {
err := dec.NextToken(&tk)
if err == io.EOF {
break
}
if err != nil {
break
}
modifier.NamespaceOfToken(&tk)
err = enc.EncodeToken(&tk)
if err != nil {
break
}
}
assert.Nil(t, enc.Flush())
}
}