This repository has been archived by the owner on Nov 26, 2020. It is now read-only.
forked from documize/html-diff
-
Notifications
You must be signed in to change notification settings - Fork 0
/
fuzz.go
212 lines (191 loc) · 4.33 KB
/
fuzz.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
// +build gofuzz
package htmldiff
import (
"strings"
"unicode/utf8"
"golang.org/x/net/html"
)
type fuzzCfg struct {
cfg *Config
cmp []string
}
func baseFuzzCfg() *fuzzCfg {
return &fuzzCfg{
cfg: &Config{
Granularity: 0,
InsertedSpan: []html.Attribute{{Key: "style", Val: "background-color: palegreen; text-decoration: underline;"}},
DeletedSpan: []html.Attribute{{Key: "style", Val: "background-color: lightpink; text-decoration: line-through;"}},
ReplacedSpan: []html.Attribute{{Key: "style", Val: "background-color: lightskyblue; text-decoration: overline;"}},
CleanTags: nil,
},
}
}
type fuzzer func(*fuzzCfg, []byte, uint)
var fuzzers = []fuzzer{fuz0, fuz1, fuz2, fuz3, fuz4, fuz5, fuz6 /*, fuz7 */}
// Fuzz is the test function for https://github.com/dvyukov/go-fuzz
func Fuzz(data []byte) int {
fcfg := baseFuzzCfg()
seed := uint1(&data)
fuzzers[int(seed)%len(fuzzers)](fcfg, data, seed/uint(len(fuzzers)))
if _, err := fcfg.cfg.HTMLdiff(fcfg.cmp); err != nil {
return 0
}
return 1
}
func fuz0(fcfg *fuzzCfg, data []byte, seed uint) {
raw := string(data)
var x0, x1 string
for i, r := range raw {
if r != utf8.RuneError {
x0 += string(r)
switch i % 4 {
case 0:
case 1:
x1 += string(r)
x1 += string(r)
default:
x1 += string(r)
}
}
}
fcfg.cmp = []string{x0, x1}
}
func fuz1(fcfg *fuzzCfg, data []byte, seed uint) {
fcfg.cfg.Granularity = int(seed)
fcfg.cmp = []string{string(data), strings.ToUpper(string(data))}
}
func fuz2(fcfg *fuzzCfg, data []byte, seed uint) {
fcfg.cfg.Granularity = int(seed)
for ct := int(seed) / 2; ct > 0; ct-- {
fcfg.cfg.CleanTags = append(fcfg.cfg.CleanTags, str5(&data))
}
n := int(seed) + 1
h := make([]string, 0, n)
xx := ""
l := len(data) / int(n)
for n--; n >= 0; n-- {
x := strMax(&data, l)
h = append(h, x)
if len(x) > 0 {
xx = x
}
}
fcfg.cmp = append([]string{xx}, h...)
}
func fuz3(fcfg *fuzzCfg, data []byte, seed uint) {
fcfg.cfg.Granularity = int(seed)
raw := string(data)
var x0, x1 string
for i, r := range raw {
if r != utf8.RuneError {
x0 += string(r)
switch i % 2 {
case 0:
x0 += string(r)
case 1:
x1 += string(r)
}
}
}
fcfg.cmp = []string{x0, x1}
}
func fuz4(fcfg *fuzzCfg, data []byte, seed uint) {
raw := string(data)
x, y := "", ""
for _, r := range raw {
if r != utf8.RuneError {
x += string(r)
y = string(r) + y
}
}
fcfg.cfg.Granularity = len(x) % int(seed+1)
fcfg.cmp = []string{x, y}
}
func fuz5(fcfg *fuzzCfg, data []byte, seed uint) {
raw := string(data)
x := ""
for _, r := range raw {
if r != utf8.RuneError {
x += string(r)
}
}
fcfg.cfg.Granularity = len(x) % int(seed+1)
fcfg.cmp = []string{x, strings.ToLower(x)}
}
func fuz6(fcfg *fuzzCfg, data []byte, seed uint) {
raw := string(data)
x := ""
for _, r := range raw {
if r != utf8.RuneError {
x += string(r)
}
}
fcfg.cmp = []string{x, strings.ToTitle(x)}
}
/* file decode time is too long
func fuz7(fcfg *fuzzCfg, data []byte, seed uint) {
dir := ".." + string(os.PathSeparator) + "testin" // should be running in the fuzz directory
files, err := ioutil.ReadDir(dir)
if err != nil {
return
}
tryAgain:
file := files[int(seed)%len(files)]
baseHTML := ""
fn := file.Name()
var dat []byte
if !strings.HasSuffix(fn, ".html") || fn == "google.html" {
seed++
goto tryAgain
}
ffn := dir + string(os.PathSeparator) + fn
dat, err = ioutil.ReadFile(ffn)
if err != nil {
return
}
baseHTML = string(dat)
ptr := (int(seed) + len(dat)/2) % len(dat)
dat[ptr] = byte(seed) & 0x7f
for x, xx := range dat {
ptr = (ptr + x + int(xx)) % len(dat)
dat[ptr] = xx & 0x7f
}
fcfg.cmp = []string{baseHTML, string(dat)}
}
*/
func uint1(data *[]byte) uint {
if len(*data) < 1 {
return 0
}
r := (*data)[0]
*data = (*data)[1:]
return uint(r)
}
func str5(data *[]byte) string {
if len(*data) < 1 {
return ""
}
l := int((*data)[0])%5 + 1
if len(*data) <= l {
return ""
}
raw := string((*data)[1 : l+1])
*data = (*data)[l:]
s := ""
for _, r := range raw {
if r != utf8.RuneError {
s += string(r)
}
}
return s
}
var tags = []string{"u", "p", "i", "b", "div"}
func strMax(data *[]byte, limit int) string {
s := ""
for max := limit; len(s) < limit && max > 0; max-- {
txt := str5(data)
tag := tags[uint1(data)%uint(len(tags))]
s += "<" + tag + ">" + txt + "</" + tag + ">"
}
return s
}