...

Source file src/github.com/AdamKorcz/go-118-fuzz-build/testing/t.go

Documentation: github.com/AdamKorcz/go-118-fuzz-build/testing

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

View as plain text