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.

49 lines
1.1 KiB

  1. package handler
  2. import (
  3. "bytes"
  4. "encoding/json"
  5. "io/ioutil"
  6. "net/http"
  7. "net/http/httptest"
  8. "github.com/tendermint/tendermint/libs/log"
  9. rs "github.com/tendermint/tendermint/rpc/jsonrpc/server"
  10. types "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 := ioutil.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. recv := new(types.RPCResponse)
  38. if err := json.Unmarshal(blob, recv); err != nil {
  39. panic(err)
  40. }
  41. }
  42. return 1
  43. }