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.

63 lines
1.3 KiB

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