t.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. package testing
  2. import (
  3. "fmt"
  4. "os"
  5. "strings"
  6. "time"
  7. )
  8. // T can be used to terminate the current fuzz iteration
  9. // without terminating the whole fuzz run. To do so, simply
  10. // panic with the text "GO-FUZZ-BUILD-PANIC" and the fuzzer
  11. // will recover.
  12. type T struct {
  13. TempDirs []string
  14. }
  15. func NewT() *T {
  16. tempDirs := make([]string, 0)
  17. return &T{TempDirs: tempDirs}
  18. }
  19. func unsupportedApi(name string) string {
  20. plsOpenIss := "Please open an issue https://github.com/AdamKorcz/go-118-fuzz-build if you need this feature."
  21. var b strings.Builder
  22. b.WriteString(fmt.Sprintf("%s is not supported when fuzzing in libFuzzer mode\n.", name))
  23. b.WriteString(plsOpenIss)
  24. return b.String()
  25. }
  26. func (t *T) Cleanup(f func()) {
  27. f()
  28. }
  29. func (t *T) Deadline() (deadline time.Time, ok bool) {
  30. panic(unsupportedApi("t.Deadline()"))
  31. }
  32. func (t *T) Error(args ...any) {
  33. fmt.Println(args...)
  34. panic("error")
  35. }
  36. func (t *T) Errorf(format string, args ...any) {
  37. fmt.Printf(format+"\n", args...)
  38. panic("errorf")
  39. }
  40. func (t *T) Fail() {
  41. panic("Called T.Fail()")
  42. }
  43. func (t *T) FailNow() {
  44. panic("Called T.Fail()")
  45. panic(unsupportedApi("t.FailNow()"))
  46. }
  47. func (t *T) Failed() bool {
  48. panic(unsupportedApi("t.Failed()"))
  49. }
  50. func (t *T) Fatal(args ...any) {
  51. fmt.Println(args...)
  52. panic("fatal")
  53. }
  54. func (t *T) Fatalf(format string, args ...any) {
  55. fmt.Printf(format+"\n", args...)
  56. panic("fatal")
  57. }
  58. func (t *T) Helper() {
  59. // We can't support it, but it also just impacts how failures are reported, so we can ignore it
  60. }
  61. func (t *T) Log(args ...any) {
  62. fmt.Println(args...)
  63. }
  64. func (t *T) Logf(format string, args ...any) {
  65. fmt.Println(format)
  66. fmt.Println(args...)
  67. }
  68. func (t *T) Name() string {
  69. return "libFuzzer"
  70. }
  71. func (t *T) Parallel() {
  72. panic(unsupportedApi("t.Parallel()"))
  73. }
  74. func (t *T) Run(name string, f func(t *T)) bool {
  75. panic(unsupportedApi("t.Run()"))
  76. }
  77. func (t *T) Setenv(key, value string) {
  78. }
  79. func (t *T) Skip(args ...any) {
  80. panic("GO-FUZZ-BUILD-PANIC")
  81. }
  82. func (t *T) SkipNow() {
  83. panic("GO-FUZZ-BUILD-PANIC")
  84. }
  85. // Is not really supported. We just skip instead
  86. // of printing any message. A log message can be
  87. // added if need be.
  88. func (t *T) Skipf(format string, args ...any) {
  89. panic("GO-FUZZ-BUILD-PANIC")
  90. }
  91. func (t *T) Skipped() bool {
  92. panic(unsupportedApi("t.Skipped()"))
  93. }
  94. func (t *T) TempDir() string {
  95. dir, err := os.MkdirTemp("", "fuzzdir-")
  96. if err != nil {
  97. panic(err)
  98. }
  99. t.TempDirs = append(t.TempDirs, dir)
  100. return dir
  101. }
  102. func (t *T) CleanupTempDirs() {
  103. if len(t.TempDirs) > 0 {
  104. for _, tempDir := range t.TempDirs {
  105. os.RemoveAll(tempDir)
  106. }
  107. }
  108. }