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.

66 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. lgr := log.MustNewDefaultLogger(log.LogFormatPlain, log.LogLevelInfo)
  22. rs.RegisterRPCFuncs(mux, rpcFuncMap, lgr)
  23. }
  24. func Fuzz(data []byte) int {
  25. if len(data) == 0 {
  26. return -1
  27. }
  28. req, _ := http.NewRequest("POST", "http://localhost/", bytes.NewReader(data))
  29. rec := httptest.NewRecorder()
  30. mux.ServeHTTP(rec, req)
  31. res := rec.Result()
  32. blob, err := io.ReadAll(res.Body)
  33. if err != nil {
  34. panic(err)
  35. }
  36. if err := res.Body.Close(); err != nil {
  37. panic(err)
  38. }
  39. if len(blob) == 0 {
  40. return 1
  41. }
  42. if outputJSONIsSlice(blob) {
  43. recv := []types.RPCResponse{}
  44. if err := json.Unmarshal(blob, &recv); err != nil {
  45. panic(err)
  46. }
  47. return 1
  48. }
  49. recv := &types.RPCResponse{}
  50. if err := json.Unmarshal(blob, recv); err != nil {
  51. panic(err)
  52. }
  53. return 1
  54. }
  55. func outputJSONIsSlice(input []byte) bool {
  56. slice := []interface{}{}
  57. return json.Unmarshal(input, &slice) == nil
  58. }