forked from go-asn1-ber/asn1-ber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuzz_test.go
50 lines (41 loc) · 1.31 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
//go:build go1.18
// +build go1.18
// Fuzzing test(s) are gated behind build tags in order to avoid generating errors
// when running on versions of Go that do not support fuzzing. This was added in
// Go 1.18.
package ber
import (
"os"
"testing"
"time"
)
func FuzzDecodePacket(f *testing.F) {
// Seed the fuzz corpus with the test cases defined in suite_test.go
for _, tc := range testCases {
file := tc.File
dataIn, err := os.ReadFile(file)
if err != nil {
f.Fatalf("failed to load file %s into fuzz corpus: %v", file, err)
continue
}
f.Add(dataIn)
}
// Seed the fuzz corpus with data known to cause panics in the past
f.Add([]byte{0x09, 0x02, 0x85, 0x30})
f.Add([]byte{0x09, 0x01, 0xcf})
// Set a limit on the length decoded in readPacket() since the call to
// make([]byte, length) can allocate up to MaxPacketLengthBytes which is
// currently 2 GB. This can cause memory related crashes when fuzzing in
// parallel or on memory constrained devices.
MaxPacketLengthBytes = 65536
f.Fuzz(func(t *testing.T, data []byte) {
stime := time.Now()
p, err := DecodePacketErr(data)
if e := time.Since(stime); e > (time.Millisecond * 500) {
t.Fatalf("DecodePacketErr took too long: %s", e)
}
if p == nil && err == nil {
t.Fatalf("DecodePacketErr returned a nil packet and no error")
}
})
}