You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

65 lines
1.3 KiB

  1. package server
  2. import (
  3. "bytes"
  4. "context"
  5. "encoding/json"
  6. "io"
  7. "net/http"
  8. "net/http/httptest"
  9. "github.com/tendermint/tendermint/libs/log"
  10. rs "github.com/tendermint/tendermint/rpc/jsonrpc/server"
  11. "github.com/tendermint/tendermint/rpc/jsonrpc/types"
  12. )
  13. var rpcFuncMap = map[string]*rs.RPCFunc{
  14. "c": rs.NewRPCFunc(func(ctx context.Context, s string, i int) (string, error) {
  15. return "foo", nil
  16. }, "s", "i"),
  17. }
  18. var mux *http.ServeMux
  19. func init() {
  20. mux = http.NewServeMux()
  21. rs.RegisterRPCFuncs(mux, rpcFuncMap, log.NewNopLogger())
  22. }
  23. func Fuzz(data []byte) int {
  24. if len(data) == 0 {
  25. return -1
  26. }
  27. req, _ := http.NewRequest("POST", "http://localhost/", bytes.NewReader(data))
  28. rec := httptest.NewRecorder()
  29. mux.ServeHTTP(rec, req)
  30. res := rec.Result()
  31. blob, err := io.ReadAll(res.Body)
  32. if err != nil {
  33. panic(err)
  34. }
  35. if err := res.Body.Close(); err != nil {
  36. panic(err)
  37. }
  38. if len(blob) == 0 {
  39. return 1
  40. }
  41. if outputJSONIsSlice(blob) {
  42. recv := []types.RPCResponse{}
  43. if err := json.Unmarshal(blob, &recv); err != nil {
  44. panic(err)
  45. }
  46. return 1
  47. }
  48. recv := &types.RPCResponse{}
  49. if err := json.Unmarshal(blob, recv); err != nil {
  50. panic(err)
  51. }
  52. return 1
  53. }
  54. func outputJSONIsSlice(input []byte) bool {
  55. slice := []interface{}{}
  56. return json.Unmarshal(input, &slice) == nil
  57. }