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.

50 lines
1.0 KiB

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