-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzz_test.go
212 lines (192 loc) · 3.73 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
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
// Copyright 2024 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// Package maps implements Go's builtin map type.
package maps_test
import (
"bytes"
"encoding/binary"
"fmt"
"internal/runtime/maps"
"reflect"
"testing"
"unsafe"
)
// The input to FuzzTable is a binary-encoded array of fuzzCommand structs.
//
// Each fuzz call begins with an empty Map[uint16, uint32].
//
// Each command is then executed on the map in sequence. Operations with
// output (e.g., Get) are verified against a reference map.
type fuzzCommand struct {
Op fuzzOp
// Used for Get, Put, Delete.
Key uint16
// Used for Put.
Elem uint32
}
// Encoded size of fuzzCommand.
var fuzzCommandSize = binary.Size(fuzzCommand{})
type fuzzOp uint8
const (
fuzzOpGet fuzzOp = iota
fuzzOpPut
fuzzOpDelete
)
func encode(fc []fuzzCommand) []byte {
var buf bytes.Buffer
if err := binary.Write(&buf, binary.LittleEndian, fc); err != nil {
panic(fmt.Sprintf("error writing %v: %v", fc, err))
}
return buf.Bytes()
}
func decode(b []byte) []fuzzCommand {
// Round b down to a multiple of fuzzCommand size. i.e., ignore extra
// bytes of input.
entries := len(b) / fuzzCommandSize
usefulSize := entries * fuzzCommandSize
b = b[:usefulSize]
fc := make([]fuzzCommand, entries)
buf := bytes.NewReader(b)
if err := binary.Read(buf, binary.LittleEndian, &fc); err != nil {
panic(fmt.Sprintf("error reading %v: %v", b, err))
}
return fc
}
func TestEncodeDecode(t *testing.T) {
fc := []fuzzCommand{
{
Op: fuzzOpPut,
Key: 123,
Elem: 456,
},
{
Op: fuzzOpGet,
Key: 123,
},
}
b := encode(fc)
got := decode(b)
if !reflect.DeepEqual(fc, got) {
t.Errorf("encode-decode roundtrip got %+v want %+v", got, fc)
}
// Extra trailing bytes ignored.
b = append(b, 42)
got = decode(b)
if !reflect.DeepEqual(fc, got) {
t.Errorf("encode-decode (extra byte) roundtrip got %+v want %+v", got, fc)
}
}
func FuzzTable(f *testing.F) {
// All of the ops.
f.Add(encode([]fuzzCommand{
{
Op: fuzzOpPut,
Key: 123,
Elem: 456,
},
{
Op: fuzzOpDelete,
Key: 123,
},
{
Op: fuzzOpGet,
Key: 123,
},
}))
// Add enough times to trigger grow.
f.Add(encode([]fuzzCommand{
{
Op: fuzzOpPut,
Key: 1,
Elem: 101,
},
{
Op: fuzzOpPut,
Key: 2,
Elem: 102,
},
{
Op: fuzzOpPut,
Key: 3,
Elem: 103,
},
{
Op: fuzzOpPut,
Key: 4,
Elem: 104,
},
{
Op: fuzzOpPut,
Key: 5,
Elem: 105,
},
{
Op: fuzzOpPut,
Key: 6,
Elem: 106,
},
{
Op: fuzzOpPut,
Key: 7,
Elem: 107,
},
{
Op: fuzzOpPut,
Key: 8,
Elem: 108,
},
{
Op: fuzzOpGet,
Key: 1,
},
{
Op: fuzzOpDelete,
Key: 2,
},
{
Op: fuzzOpPut,
Key: 2,
Elem: 42,
},
{
Op: fuzzOpGet,
Key: 2,
},
}))
f.Fuzz(func(t *testing.T, in []byte) {
fc := decode(in)
if len(fc) == 0 {
return
}
m, typ := maps.NewTestMap[uint16, uint32](8)
ref := make(map[uint16]uint32)
for _, c := range fc {
switch c.Op {
case fuzzOpGet:
elemPtr, ok := m.Get(typ, unsafe.Pointer(&c.Key))
refElem, refOK := ref[c.Key]
if ok != refOK {
t.Errorf("Get(%d) got ok %v want ok %v", c.Key, ok, refOK)
}
if !ok {
continue
}
gotElem := *(*uint32)(elemPtr)
if gotElem != refElem {
t.Errorf("Get(%d) got %d want %d", c.Key, gotElem, refElem)
}
case fuzzOpPut:
m.Put(typ, unsafe.Pointer(&c.Key), unsafe.Pointer(&c.Elem))
ref[c.Key] = c.Elem
case fuzzOpDelete:
m.Delete(typ, unsafe.Pointer(&c.Key))
delete(ref, c.Key)
default:
// Just skip this command to keep the fuzzer
// less constrained.
continue
}
}
})
}