-
Notifications
You must be signed in to change notification settings - Fork 46
/
fuzz.go
49 lines (43 loc) · 972 Bytes
/
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
//go:build gofuzz
// +build gofuzz
//go:generate GO111MODULE=off go-fuzz-build github.com/rsocket/rsocket-go/
package rsocket
import (
"bytes"
"errors"
"github.com/rsocket/rsocket-go/core"
"github.com/rsocket/rsocket-go/core/framing"
"github.com/rsocket/rsocket-go/core/transport"
)
func Fuzz(data []byte) int {
buf := bytes.NewBuffer(data)
decoder := transport.NewLengthBasedFrameDecoder(buf)
raw, err := decoder.Read()
if err == nil {
err = handleRaw(raw)
}
if err == nil || isExpectedError(err) {
return 0
}
return 1
}
func isExpectedError(err error) bool {
return err == core.ErrInvalidFrame || err == transport.ErrIncompleteHeader
}
func handleRaw(raw []byte) (err error) {
var frame core.BufferedFrame
frame, err = framing.FromBytes(raw)
if err != nil {
return
}
defer frame.Release()
err = frame.Validate()
if err != nil {
return
}
if frame.Len() >= core.FrameHeaderLen {
return
}
err = errors.New("broken frame")
return
}