-
Notifications
You must be signed in to change notification settings - Fork 34
/
Copy pathfuzz_test.go
140 lines (129 loc) · 2.72 KB
/
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
//go:build go1.18
// +build go1.18
package fixedwidth_test
import (
"bytes"
"testing"
"github.com/ianlopshire/go-fixedwidth"
)
func FuzzUnmarshal(f *testing.F) {
unmarshal := func(data []byte, v interface{}, useCodepointIndices bool) error {
if useCodepointIndices {
dec := fixedwidth.NewDecoder(bytes.NewReader(data))
dec.SetUseCodepointIndices(useCodepointIndices)
return dec.Decode(v)
}
return fixedwidth.Unmarshal(data, v)
}
marshal := func(v interface{}, useCodepointIndices bool) ([]byte, error) {
buff := bytes.NewBuffer(nil)
enc := fixedwidth.NewEncoder(buff)
enc.SetUseCodepointIndices(useCodepointIndices)
if err := enc.Encode(v); err != nil {
return nil, err
}
return buff.Bytes(), nil
}
typs := []func() interface{}{
func() interface{} {
return new([]struct {
F string `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F string `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F int `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F int64 `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F int32 `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F int16 `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F int8 `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F uint `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F uint64 `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F uint32 `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F uint16 `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F uint8 `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F float32 `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F float64 `fixed:"1,10"`
})
},
func() interface{} {
return new(struct {
F bool `fixed:"1,10"`
})
},
}
f.Add([]byte(`\n`))
f.Add([]byte(`foo `))
f.Add([]byte(`foo ` + "\n" + `foo `))
f.Add([]byte(`føø `))
f.Add([]byte(`true `))
f.Add([]byte(`123 `))
f.Add([]byte(`123.456 `))
f.Add([]byte(`-123 `))
f.Fuzz(func(t *testing.T, b []byte) {
for _, typ := range typs {
for _, useCodepointIndices := range []bool{true, false} {
i := typ()
if err := unmarshal(b, i, useCodepointIndices); err != nil {
continue
}
encoded, err := marshal(i, useCodepointIndices)
if err != nil {
t.Fatalf("failed to marshal: %s", err)
}
if err := unmarshal(encoded, i, useCodepointIndices); err != nil {
t.Fatalf("failed to roundtrip: %s", err)
}
}
}
})
}