Skip to content

Commit

Permalink
include fp.RunFuzz in fuzzing
Browse files Browse the repository at this point in the history
  • Loading branch information
WillAbides committed Aug 21, 2021
1 parent 509eb11 commit 97385f4
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 9 deletions.
9 changes: 7 additions & 2 deletions fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,18 @@ package rjson

import (
"fmt"

"github.com/willabides/rjson/internal/fp"
)

// Fuzz is for running go-fuzz tests
func Fuzz(data []byte) int {
score := 0

for _, fd := range fuzzers {
allFuzzers := append(fuzzers, fuzzer{
name: "fp.RunFuzz",
fn: fp.RunFuzz,
})
for _, fd := range allFuzzers {
d := make([]byte, len(data))
copy(d, data)
s, err := fd.fn(d)
Expand Down
23 changes: 16 additions & 7 deletions internal/fp/fuzz.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,34 @@ func fuzzOracle(data []byte) (val float64, p int, err error) {
return val, int(decoder.InputOffset()), nil
}

// Fuzz is for running go-fuzz tests
func Fuzz(data []byte) int {
// RunFuzz runs a fuzz test but returns an error instead of panicking
func RunFuzz(data []byte) (int, error) {
want, wantOffset, err := fuzzOracle(data)
wantErr := err != nil
got, gotOffset, gotErr := ParseJSONFloatPrefix(data)
if wantErr {
if gotErr == nil {
panic("missed an error")
return 0, fmt.Errorf("missed an error")
}
} else {
if gotErr != nil {
panic("unexpected error")
return 0, fmt.Errorf("unexpected error")
}
}
if want != got {
panic(fmt.Sprintf("wanted %v but got %v\n", want, got))
return 0, fmt.Errorf("wanted %v but got %v", want, got)
}
if wantOffset != gotOffset {
panic(fmt.Sprintf("wanted offset %v but got %v\n", wantOffset, gotOffset))
return 0, fmt.Errorf("wanted offset %v but got %v", wantOffset, gotOffset)
}
return 0, nil
}

// Fuzz is for running go-fuzz tests
func Fuzz(data []byte) int {
n, err := RunFuzz(data)
if err != nil {
panic(err.Error())
}
return 0
return n
}

0 comments on commit 97385f4

Please sign in to comment.