Debian Codegraph @

libgo/go/internal/fuzz/encoding_test.go

raw
1 // Copyright 2021 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package fuzz
6
7 import (
8 "math"
9 "runtime"
10 "strconv"
11 "testing"
12 "unicode"
13 )
14
15 func TestUnmarshalMarshal(t *testing.T) {
16 var tests = []struct {
17 desc string
18 in string
19 reject bool
20 want string // if different from in
21 }{
22 {
23 desc: "missing version",
24 in: "int(1234)",
25 reject: true,
26 },
27 {
28 desc: "malformed string",
29 in: `go test fuzz v1
30 string("a"bcad")`,
31 reject: true,
32 },
33 {
34 desc: "empty value",
35 in: `go test fuzz v1
36 int()`,
37 reject: true,
38 },
39 {
40 desc: "negative uint",
41 in: `go test fuzz v1
42 uint(-32)`,
43 reject: true,
44 },
45 {
46 desc: "int8 too large",
47 in: `go test fuzz v1
48 int8(1234456)`,
49 reject: true,
50 },
51 {
52 desc: "multiplication in int value",
53 in: `go test fuzz v1
54 int(20*5)`,
55 reject: true,
56 },
57 {
58 desc: "double negation",
59 in: `go test fuzz v1
60 int(--5)`,
61 reject: true,
62 },
63 {
64 desc: "malformed bool",
65 in: `go test fuzz v1
66 bool(0)`,
67 reject: true,
68 },
69 {
70 desc: "malformed byte",
71 in: `go test fuzz v1
72 byte('aa)`,
73 reject: true,
74 },
75 {
76 desc: "byte out of range",
77 in: `go test fuzz v1
78 byte('☃')`,
79 reject: true,
80 },
81 {
82 desc: "extra newline",
83 in: `go test fuzz v1
84 string("has extra newline")
85 `,
86 want: `go test fuzz v1
87 string("has extra newline")`,
88 },
89 {
90 desc: "trailing spaces",
91 in: `go test fuzz v1
92 string("extra")
93 []byte("spacing")
94 `,
95 want: `go test fuzz v1
96 string("extra")
97 []byte("spacing")`,
98 },
99 {
100 desc: "float types",
101 in: `go test fuzz v1
102 float64(0)
103 float32(0)`,
104 },
105 {
106 desc: "various types",
107 in: `go test fuzz v1
108 int(-23)
109 int8(-2)
110 int64(2342425)
111 uint(1)
112 uint16(234)
113 uint32(352342)
114 uint64(123)
115 rune('œ')
116 byte('K')
117 byte('ÿ')
118 []byte("hello¿")
119 []byte("a")
120 bool(true)
121 string("hello\\xbd\\xb2=\\xbc ⌘")
122 float64(-12.5)
123 float32(2.5)`,
124 },
125 {
126 desc: "float edge cases",
127 // The two IEEE 754 bit patterns used for the math.Float{64,32}frombits
128 // encodings are non-math.NAN quiet-NaN values. Since they are not equal
129 // to math.NaN(), they should be re-encoded to their bit patterns. They
130 // are, respectively:
131 // * math.Float64bits(math.NaN())+1
132 // * math.Float32bits(float32(math.NaN()))+1
133 in: `go test fuzz v1
134 float32(-0)
135 float64(-0)
136 float32(+Inf)
137 float32(-Inf)
138 float32(NaN)
139 float64(+Inf)
140 float64(-Inf)
141 float64(NaN)
142 math.Float64frombits(0x7ff8000000000002)
143 math.Float32frombits(0x7fc00001)`,
144 },
145 {
146 desc: "int variations",
147 // Although we arbitrarily choose default integer bases (0 or 16), we may
148 // want to change those arbitrary choices in the future and should not
149 // break the parser. Verify that integers in the opposite bases still
150 // parse correctly.
151 in: `go test fuzz v1
152 int(0x0)
153 int32(0x41)
154 int64(0xfffffffff)
155 uint32(0xcafef00d)
156 uint64(0xffffffffffffffff)
157 uint8(0b0000000)
158 byte(0x0)
159 byte('\000')
160 byte('\u0000')
161 byte('\'')
162 math.Float64frombits(9221120237041090562)
163 math.Float32frombits(2143289345)`,
164 want: `go test fuzz v1
165 int(0)
166 rune('A')
167 int64(68719476735)
168 uint32(3405705229)
169 uint64(18446744073709551615)
170 byte('\x00')
171 byte('\x00')
172 byte('\x00')
173 byte('\x00')
174 byte('\'')
175 math.Float64frombits(0x7ff8000000000002)
176 math.Float32frombits(0x7fc00001)`,
177 },
178 {
179 desc: "rune validation",
180 in: `go test fuzz v1
181 rune(0)
182 rune(0x41)
183 rune(-1)
184 rune(0xfffd)
185 rune(0xd800)
186 rune(0x10ffff)
187 rune(0x110000)
188 `,
189 want: `go test fuzz v1
190 rune('\x00')
191 rune('A')
192 int32(-1)
193 rune('�')
194 int32(55296)
195 rune('\U0010ffff')
196 int32(1114112)`,
197 },
198 {
199 desc: "int overflow",
200 in: `go test fuzz v1
201 int(0x7fffffffffffffff)
202 uint(0xffffffffffffffff)`,
203 want: func() string {
204 switch strconv.IntSize {
205 case 32:
206 return `go test fuzz v1
207 int(-1)
208 uint(4294967295)`
209 case 64:
210 return `go test fuzz v1
211 int(9223372036854775807)
212 uint(18446744073709551615)`
213 default:
214 panic("unreachable")
215 }
216 }(),
217 },
218 }
219 for _, test := range tests {
220 t.Run(test.desc, func(t *testing.T) {
221 vals, err := unmarshalCorpusFile([]byte(test.in))
222 if test.reject {
223 if err == nil {
224 t.Fatalf("unmarshal unexpected success")
225 }
226 return
227 }
228 if err != nil {
229 t.Fatalf("unmarshal unexpected error: %v", err)
230 }
231 newB := marshalCorpusFile(vals...)
232 if err != nil {
233 t.Fatalf("marshal unexpected error: %v", err)
234 }
235 if newB[len(newB)-1] != '\n' {
236 t.Error("didn't write final newline to corpus file")
237 }
238
239 want := test.want
240 if want == "" {
241 want = test.in
242 }
243 want += "\n"
244 got := string(newB)
245 if got != want {
246 t.Errorf("unexpected marshaled value\ngot:\n%s\nwant:\n%s", got, want)
247 }
248 })
249 }
250 }
251
252 // BenchmarkMarshalCorpusFile measures the time it takes to serialize byte
253 // slices of various sizes to a corpus file. The slice contains a repeating
254 // sequence of bytes 0-255 to mix escaped and non-escaped characters.
255 func BenchmarkMarshalCorpusFile(b *testing.B) {
256 buf := make([]byte, 1024*1024)
257 for i := 0; i < len(buf); i++ {
258 buf[i] = byte(i)
259 }
260
261 for sz := 1; sz <= len(buf); sz <<= 1 {
262 sz := sz
263 b.Run(strconv.Itoa(sz), func(b *testing.B) {
264 for i := 0; i < b.N; i++ {
265 b.SetBytes(int64(sz))
266 marshalCorpusFile(buf[:sz])
267 }
268 })
269 }
270 }
271
272 // BenchmarkUnmarshalCorpusfile measures the time it takes to deserialize
273 // files encoding byte slices of various sizes. The slice contains a repeating
274 // sequence of bytes 0-255 to mix escaped and non-escaped characters.
275 func BenchmarkUnmarshalCorpusFile(b *testing.B) {
276 buf := make([]byte, 1024*1024)
277 for i := 0; i < len(buf); i++ {
278 buf[i] = byte(i)
279 }
280
281 for sz := 1; sz <= len(buf); sz <<= 1 {
282 sz := sz
283 data := marshalCorpusFile(buf[:sz])
284 b.Run(strconv.Itoa(sz), func(b *testing.B) {
285 for i := 0; i < b.N; i++ {
286 b.SetBytes(int64(sz))
287 unmarshalCorpusFile(data)
288 }
289 })
290 }
291 }
292
293 func TestByteRoundTrip(t *testing.T) {
294 for x := 0; x < 256; x++ {
295 b1 := byte(x)
296 buf := marshalCorpusFile(b1)
297 vs, err := unmarshalCorpusFile(buf)
298 if err != nil {
299 t.Fatal(err)
300 }
301 b2 := vs[0].(byte)
302 if b2 != b1 {
303 t.Fatalf("unmarshaled %v, want %v:\n%s", b2, b1, buf)
304 }
305 }
306 }
307
308 func TestInt8RoundTrip(t *testing.T) {
309 for x := -128; x < 128; x++ {
310 i1 := int8(x)
311 buf := marshalCorpusFile(i1)
312 vs, err := unmarshalCorpusFile(buf)
313 if err != nil {
314 t.Fatal(err)
315 }
316 i2 := vs[0].(int8)
317 if i2 != i1 {
318 t.Fatalf("unmarshaled %v, want %v:\n%s", i2, i1, buf)
319 }
320 }
321 }
322
323 func FuzzFloat64RoundTrip(f *testing.F) {
324 f.Add(math.Float64bits(0))
325 f.Add(math.Float64bits(math.Copysign(0, -1)))
326 f.Add(math.Float64bits(math.MaxFloat64))
327 f.Add(math.Float64bits(math.SmallestNonzeroFloat64))
328 f.Add(math.Float64bits(math.NaN()))
329 f.Add(uint64(0x7FF0000000000001)) // signaling NaN
330 f.Add(math.Float64bits(math.Inf(1)))
331 f.Add(math.Float64bits(math.Inf(-1)))
332
333 f.Fuzz(func(t *testing.T, u1 uint64) {
334 // The signaling NaN test fails on 32-bit x86 with gccgo,
335 // which uses the 387 floating-point stack by default.
336 // Converting a signaling NaN in and out of the stack
337 // changes the NaN to a quiet NaN.
338 if runtime.GOARCH == "386" && u1 == 0x7FF0000000000001 {
339 t.Skip("skipping signalling NaN test on 386 with gccgo")
340 }
341
342 x1 := math.Float64frombits(u1)
343
344 b := marshalCorpusFile(x1)
345 t.Logf("marshaled math.Float64frombits(0x%x):\n%s", u1, b)
346
347 xs, err := unmarshalCorpusFile(b)
348 if err != nil {
349 t.Fatal(err)
350 }
351 if len(xs) != 1 {
352 t.Fatalf("unmarshaled %d values", len(xs))
353 }
354 x2 := xs[0].(float64)
355 u2 := math.Float64bits(x2)
356 if u2 != u1 {
357 t.Errorf("unmarshaled %v (bits 0x%x)", x2, u2)
358 }
359 })
360 }
361
362 func FuzzRuneRoundTrip(f *testing.F) {
363 f.Add(rune(-1))
364 f.Add(rune(0xd800))
365 f.Add(rune(0xdfff))
366 f.Add(rune(unicode.ReplacementChar))
367 f.Add(rune(unicode.MaxASCII))
368 f.Add(rune(unicode.MaxLatin1))
369 f.Add(rune(unicode.MaxRune))
370 f.Add(rune(unicode.MaxRune + 1))
371 f.Add(rune(-0x80000000))
372 f.Add(rune(0x7fffffff))
373
374 f.Fuzz(func(t *testing.T, r1 rune) {
375 b := marshalCorpusFile(r1)
376 t.Logf("marshaled rune(0x%x):\n%s", r1, b)
377
378 rs, err := unmarshalCorpusFile(b)
379 if err != nil {
380 t.Fatal(err)
381 }
382 if len(rs) != 1 {
383 t.Fatalf("unmarshaled %d values", len(rs))
384 }
385 r2 := rs[0].(rune)
386 if r2 != r1 {
387 t.Errorf("unmarshaled rune(0x%x)", r2)
388 }
389 })
390 }
391
392 func FuzzStringRoundTrip(f *testing.F) {
393 f.Add("")
394 f.Add("\x00")
395 f.Add(string([]rune{unicode.ReplacementChar}))
396
397 f.Fuzz(func(t *testing.T, s1 string) {
398 b := marshalCorpusFile(s1)
399 t.Logf("marshaled %q:\n%s", s1, b)
400
401 rs, err := unmarshalCorpusFile(b)
402 if err != nil {
403 t.Fatal(err)
404 }
405 if len(rs) != 1 {
406 t.Fatalf("unmarshaled %d values", len(rs))
407 }
408 s2 := rs[0].(string)
409 if s2 != s1 {
410 t.Errorf("unmarshaled %q", s2)
411 }
412 })
413 }