fuzz.go 939 B

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. //go:build gofuzz
  2. // +build gofuzz
  3. package rtcp
  4. import (
  5. "bytes"
  6. "io"
  7. )
  8. // Fuzz implements a randomized fuzz test of the rtcp
  9. // parser using go-fuzz.
  10. //
  11. // To run the fuzzer, first download go-fuzz:
  12. // `go get github.com/dvyukov/go-fuzz/...`
  13. //
  14. // Then build the testing package:
  15. // `go-fuzz-build github.com/pion/webrtc`
  16. //
  17. // And run the fuzzer on the corpus:
  18. // ```
  19. // mkdir workdir
  20. //
  21. // # optionally add a starter corpus of valid rtcp packets.
  22. // # the corpus should be as compact and diverse as possible.
  23. // cp -r ~/my-rtcp-packets workdir/corpus
  24. //
  25. // go-fuzz -bin=ase-fuzz.zip -workdir=workdir
  26. // ````
  27. func Fuzz(data []byte) int {
  28. r := NewReader(bytes.NewReader(data))
  29. for {
  30. _, data, err := r.ReadPacket()
  31. if err == io.EOF {
  32. break
  33. }
  34. if err != nil {
  35. return 0
  36. }
  37. packet, err := Unmarshal(data)
  38. if err != nil {
  39. return 0
  40. }
  41. if _, err := packet.Marshal(); err != nil {
  42. return 0
  43. }
  44. }
  45. return 1
  46. }