test/fuzz: add test to reproduce found fuzz errors (#6757)

This change does two things:
1. It fixes the json fuzzer to account for receiving array results. Arrays are returned by the rpc server when the input data is an array.
2. Adds a `fuzz_test.go` file and corresponding `testdata` directory containing the failing test case.

This seems like a reasonable way to add and track previous crash issues in our fuzz test cases. The upcoming stdlib go fuzz tool does effectively this automatically.
This commit is contained in:
William Banfield
2021-07-26 14:58:51 +00:00
committed by GitHub
parent 93f462ef86
commit c5dc3b267f
4 changed files with 54 additions and 4 deletions
+19 -4
View File
@@ -1,4 +1,4 @@
package handler
package server
import (
"bytes"
@@ -39,11 +39,26 @@ func Fuzz(data []byte) int {
if err := res.Body.Close(); err != nil {
panic(err)
}
if len(blob) > 0 {
recv := new(types.RPCResponse)
if err := json.Unmarshal(blob, recv); err != nil {
if len(blob) == 0 {
return 1
}
if inputJSONIsMultiElementSlice(data) {
recv := []types.RPCResponse{}
if err := json.Unmarshal(blob, &recv); err != nil {
panic(err)
}
return 1
}
recv := &types.RPCResponse{}
if err := json.Unmarshal(blob, recv); err != nil {
panic(err)
}
return 1
}
func inputJSONIsMultiElementSlice(input []byte) bool {
slice := []interface{}{}
err := json.Unmarshal(input, &slice)
return err == nil && len(slice) > 1
}