Text file src/cmd/go/testdata/script/test_fuzz.txt

     1  [!fuzz] skip
     2  [short] skip
     3  env GOCACHE=$WORK/cache
     4  
     5  # This test uses -vet=off to suppress vet, as vet's "tests" analyzer would
     6  # otherwise statically report the problems we are trying to observe dynamically.
     7  
     8  # Test that running a fuzz target that returns without failing or calling
     9  # f.Fuzz fails and causes a non-zero exit status.
    10  ! go test -vet=off noop_fuzz_test.go
    11  ! stdout ^ok
    12  stdout FAIL
    13  
    14  # Test that fuzzing a fuzz target that returns without failing or calling
    15  # f.Fuzz fails and causes a non-zero exit status.
    16  ! go test -vet=off -fuzz=Fuzz -fuzztime=1x noop_fuzz_test.go
    17  ! stdout ^ok
    18  stdout FAIL
    19  
    20  # Test that calling f.Error in a fuzz target causes a non-zero exit status.
    21  ! go test -vet=off -fuzz=Fuzz -fuzztime=1x error_fuzz_test.go
    22  ! stdout ^ok
    23  stdout FAIL
    24  
    25  # Test that calling f.Fatal in a fuzz target causes a non-zero exit status.
    26  ! go test -vet=off fatal_fuzz_test.go
    27  ! stdout ^ok
    28  stdout FAIL
    29  
    30  # Test that successful test exits cleanly.
    31  go test -vet=off success_fuzz_test.go
    32  stdout ^ok
    33  ! stdout FAIL
    34  
    35  # Test that successful fuzzing exits cleanly.
    36  go test -vet=off -fuzz=Fuzz -fuzztime=1x success_fuzz_test.go
    37  stdout ok
    38  ! stdout FAIL
    39  
    40  # Test that calling f.Fatal while fuzzing causes a non-zero exit status.
    41  ! go test -vet=off -fuzz=Fuzz -fuzztime=1x fatal_fuzz_test.go
    42  ! stdout ^ok
    43  stdout FAIL
    44  
    45  # Test error with seed corpus in f.Fuzz
    46  ! go test -vet=off -run Fuzz_error -vet=off fuzz_add_test.go
    47  ! stdout ^ok
    48  stdout FAIL
    49  stdout 'error here'
    50  
    51  [short] stop
    52  
    53  # Test that calling panic(nil) in a fuzz target causes a non-zero exit status.
    54  ! go test -vet=off panic_fuzz_test.go
    55  ! stdout ^ok
    56  stdout FAIL
    57  
    58  # Test that skipped test exits cleanly.
    59  go test -vet=off skipped_fuzz_test.go
    60  stdout ok
    61  ! stdout FAIL
    62  
    63  # Test that f.Fatal within f.Fuzz panics
    64  ! go test -vet=off fatal_fuzz_fn_fuzz_test.go
    65  ! stdout ^ok
    66  ! stdout 'fatal here'
    67  stdout FAIL
    68  stdout 'fuzz target'
    69  
    70  # Test that f.Error within f.Fuzz panics
    71  ! go test -vet=off error_fuzz_fn_fuzz_test.go
    72  ! stdout ^ok
    73  ! stdout 'error here'
    74  stdout FAIL
    75  stdout 'fuzz target'
    76  
    77  # Test that f.Fail within f.Fuzz panics
    78  ! go test -vet=off fail_fuzz_fn_fuzz_test.go
    79  ! stdout ^ok
    80  stdout FAIL
    81  stdout 'fuzz target'
    82  
    83  # Test that f.Skip within f.Fuzz panics
    84  ! go test -vet=off skip_fuzz_fn_fuzz_test.go
    85  ! stdout ^ok
    86  ! stdout 'skip here'
    87  stdout FAIL
    88  stdout 'fuzz target'
    89  
    90  # Test that f.Skipped within f.Fuzz panics
    91  ! go test -vet=off skipped_fuzz_fn_fuzz_test.go
    92  ! stdout ^ok
    93  ! stdout 'f.Skipped is'
    94  stdout FAIL
    95  stdout 'fuzz target'
    96  stdout 't.Skipped is false'
    97  
    98  # Test that runtime.Goexit within the fuzz function is an error.
    99  ! go test -vet=off goexit_fuzz_fn_fuzz_test.go
   100  ! stdout ^ok
   101  stdout FAIL
   102  
   103  # Test that a call to f.Fatal after the Fuzz func is executed.
   104  ! go test -vet=off fatal_after_fuzz_func_fuzz_test.go
   105  ! stdout ok
   106  stdout FAIL
   107  
   108  # Test that missing *T in f.Fuzz causes a non-zero exit status.
   109  ! go test -vet=off incomplete_fuzz_call_fuzz_test.go
   110  ! stdout ^ok
   111  stdout FAIL
   112  
   113  # Test that a panic in the Cleanup func is executed.
   114  ! go test -vet=off cleanup_fuzz_test.go
   115  ! stdout ^ok
   116  stdout FAIL
   117  stdout 'failed some precondition'
   118  
   119  # Test success with seed corpus in f.Fuzz
   120  go test -vet=off -run Fuzz_pass -vet=off fuzz_add_test.go
   121  stdout ok
   122  ! stdout FAIL
   123  ! stdout 'off by one error'
   124  
   125  # Test fatal with seed corpus in f.Fuzz
   126  ! go test -vet=off -run Fuzz_fatal -vet=off fuzz_add_test.go
   127  ! stdout ^ok
   128  stdout FAIL
   129  stdout 'fatal here'
   130  
   131  # Test panic with seed corpus in f.Fuzz
   132  ! go test -vet=off -run Fuzz_panic -vet=off fuzz_add_test.go
   133  ! stdout ^ok
   134  stdout FAIL
   135  stdout 'off by one error'
   136  
   137  # Test panic(nil) with seed corpus in f.Fuzz
   138  ! go test -vet=off -run Fuzz_nilPanic -vet=off fuzz_add_test.go
   139  ! stdout ^ok
   140  stdout FAIL
   141  
   142  # Test panic with unsupported seed corpus
   143  ! go test -vet=off -run Fuzz_unsupported -vet=off fuzz_add_test.go
   144  ! stdout ^ok
   145  stdout FAIL
   146  
   147  # Test panic with different number of args to f.Add
   148  ! go test -vet=off -run Fuzz_addDifferentNumber -vet=off fuzz_add_test.go
   149  ! stdout ^ok
   150  stdout FAIL
   151  
   152  # Test panic with different type of args to f.Add
   153  ! go test -vet=off -run Fuzz_addDifferentType -vet=off fuzz_add_test.go
   154  ! stdout ^ok
   155  stdout FAIL
   156  
   157  # Test that the wrong type given with f.Add will fail.
   158  ! go test -vet=off -run Fuzz_wrongType -vet=off fuzz_add_test.go
   159  ! stdout ^ok
   160  stdout '\[string int\], want \[\[\]uint8 int8\]'
   161  stdout FAIL
   162  
   163  # Test fatal with testdata seed corpus
   164  ! go test -vet=off -run Fuzz_fail corpustesting/fuzz_testdata_corpus_test.go
   165  ! stdout ^ok
   166  stdout FAIL
   167  stdout 'fatal here'
   168  
   169  # Test pass with testdata seed corpus
   170  go test -vet=off -run Fuzz_pass corpustesting/fuzz_testdata_corpus_test.go
   171  stdout ok
   172  ! stdout FAIL
   173  ! stdout 'fatal here'
   174  
   175  # Test pass with testdata and f.Add seed corpus
   176  go test -vet=off -run Fuzz_passString corpustesting/fuzz_testdata_corpus_test.go
   177  stdout ok
   178  ! stdout FAIL
   179  
   180  # Fuzzing pass with testdata and f.Add seed corpus (skip running tests first)
   181  go test -vet=off -run=None -fuzz=Fuzz_passString corpustesting/fuzz_testdata_corpus_test.go -fuzztime=10x
   182  stdout ok
   183  ! stdout FAIL
   184  
   185  # Fuzzing pass with testdata and f.Add seed corpus
   186  go test -vet=off -run=Fuzz_passString -fuzz=Fuzz_passString corpustesting/fuzz_testdata_corpus_test.go -fuzztime=10x
   187  stdout ok
   188  ! stdout FAIL
   189  
   190  # Test panic with malformed seed corpus
   191  ! go test -vet=off -run Fuzz_fail corpustesting/fuzz_testdata_corpus_test.go
   192  ! stdout ^ok
   193  stdout FAIL
   194  
   195  # Test pass with file in other nested testdata directory
   196  go test -vet=off -run Fuzz_inNestedDir corpustesting/fuzz_testdata_corpus_test.go
   197  stdout ok
   198  ! stdout FAIL
   199  ! stdout 'fatal here'
   200  
   201  # Test fails with file containing wrong type
   202  ! go test -vet=off -run Fuzz_wrongType corpustesting/fuzz_testdata_corpus_test.go
   203  ! stdout ^ok
   204  stdout FAIL
   205  
   206  -- noop_fuzz_test.go --
   207  package noop_fuzz
   208  
   209  import "testing"
   210  
   211  func Fuzz(f *testing.F) {}
   212  
   213  -- error_fuzz_test.go --
   214  package error_fuzz
   215  
   216  import "testing"
   217  
   218  func Fuzz(f *testing.F) {
   219      f.Error("error in target")
   220  }
   221  
   222  -- fatal_fuzz_test.go --
   223  package fatal_fuzz
   224  
   225  import "testing"
   226  
   227  func Fuzz(f *testing.F) {
   228      f.Fatal("fatal in target")
   229  }
   230  
   231  -- panic_fuzz_test.go --
   232  package panic_fuzz
   233  
   234  import "testing"
   235  
   236  func Fuzz_panic(f *testing.F) {
   237      panic(nil)
   238  }
   239  
   240  -- success_fuzz_test.go --
   241  package success_fuzz
   242  
   243  import "testing"
   244  
   245  func Fuzz(f *testing.F) {
   246      f.Fuzz(func (*testing.T, []byte) {})
   247  }
   248  
   249  -- skipped_fuzz_test.go --
   250  package skipped_fuzz
   251  
   252  import "testing"
   253  
   254  func Fuzz(f *testing.F) {
   255      f.Skip()
   256  }
   257  
   258  -- fatal_fuzz_fn_fuzz_test.go --
   259  package fatal_fuzz_fn_fuzz
   260  
   261  import "testing"
   262  
   263  func Fuzz(f *testing.F) {
   264      f.Add([]byte("aa"))
   265      f.Fuzz(func(t *testing.T, b []byte) {
   266          f.Fatal("fatal here")
   267      })
   268  }
   269  
   270  -- error_fuzz_fn_fuzz_test.go --
   271  package error_fuzz_fn_fuzz
   272  
   273  import "testing"
   274  
   275  func Fuzz(f *testing.F) {
   276      f.Add([]byte("aa"))
   277      f.Fuzz(func(t *testing.T, b []byte) {
   278          f.Error("error here")
   279      })
   280  }
   281  
   282  -- fail_fuzz_fn_fuzz_test.go --
   283  package skip_fuzz_fn_fuzz
   284  
   285  import "testing"
   286  
   287  func Fuzz(f *testing.F) {
   288      f.Add([]byte("aa"))
   289      f.Fuzz(func(t *testing.T, b []byte) {
   290          f.Fail()
   291      })
   292  }
   293  
   294  -- skip_fuzz_fn_fuzz_test.go --
   295  package skip_fuzz_fn_fuzz
   296  
   297  import "testing"
   298  
   299  func Fuzz(f *testing.F) {
   300      f.Add([]byte("aa"))
   301      f.Fuzz(func(t *testing.T, b []byte) {
   302          f.Skip("skip here")
   303      })
   304  }
   305  
   306  -- skipped_fuzz_fn_fuzz_test.go --
   307  package skipped_fuzz_fn_fuzz
   308  
   309  import "testing"
   310  
   311  func Fuzz(f *testing.F) {
   312      f.Add([]byte("aa"))
   313      f.Fuzz(func(t *testing.T, b []byte) {
   314          t.Logf("t.Skipped is %t\n", t.Skipped())
   315          t.Logf("f.Skipped is %t\n", f.Skipped())
   316      })
   317  }
   318  
   319  -- goexit_fuzz_fn_fuzz_test.go --
   320  package goexit_fuzz_fn_fuzz
   321  
   322  import "testing"
   323  
   324  func Fuzz(f *testing.F) {
   325      f.Add([]byte("aa"))
   326      f.Fuzz(func(t *testing.T, b []byte) {
   327          runtime.Goexit()
   328      })
   329  }
   330  
   331  -- fatal_after_fuzz_func_fuzz_test.go --
   332  package fatal_after_fuzz_func_fuzz
   333  
   334  import "testing"
   335  
   336  func Fuzz(f *testing.F) {
   337      f.Fuzz(func(t *testing.T, b []byte) {
   338          // no-op
   339      })
   340      f.Fatal("this shouldn't be called")
   341  }
   342  
   343  -- incomplete_fuzz_call_fuzz_test.go --
   344  package incomplete_fuzz_call_fuzz
   345  
   346  import "testing"
   347  
   348  func Fuzz(f *testing.F) {
   349      f.Fuzz(func(b []byte) {
   350          // this is missing *testing.T as the first param, so should panic
   351      })
   352  }
   353  
   354  -- cleanup_fuzz_test.go --
   355  package cleanup_fuzz_test
   356  
   357  import "testing"
   358  
   359  func Fuzz(f *testing.F) {
   360      f.Cleanup(func() {
   361          panic("failed some precondition")
   362      })
   363      f.Fuzz(func(t *testing.T, b []byte) {
   364          // no-op
   365      })
   366  }
   367  
   368  -- fuzz_add_test.go --
   369  package fuzz_add
   370  
   371  import "testing"
   372  
   373  func add(f *testing.F) {
   374      f.Helper()
   375      f.Add([]byte("123"))
   376      f.Add([]byte("12345"))
   377      f.Add([]byte(""))
   378  }
   379  
   380  func Fuzz_pass(f *testing.F) {
   381      add(f)
   382      f.Fuzz(func(t *testing.T, b []byte) {
   383          if len(b) == -1 {
   384              t.Fatal("fatal here") // will not be executed
   385          }
   386      })
   387  }
   388  
   389  func Fuzz_error(f *testing.F) {
   390      add(f)
   391      f.Fuzz(func(t *testing.T, b []byte) {
   392          if len(b) == 3 {
   393              t.Error("error here")
   394          }
   395      })
   396  }
   397  
   398  func Fuzz_fatal(f *testing.F) {
   399      add(f)
   400      f.Fuzz(func(t *testing.T, b []byte) {
   401          if len(b) == 0 {
   402              t.Fatal("fatal here")
   403          }
   404      })
   405  }
   406  
   407  func Fuzz_panic(f *testing.F) {
   408      add(f)
   409      f.Fuzz(func(t *testing.T, b []byte) {
   410          if len(b) == 5 {
   411              panic("off by one error")
   412          }
   413      })
   414  }
   415  
   416  func Fuzz_nilPanic(f *testing.F) {
   417      add(f)
   418      f.Fuzz(func(t *testing.T, b []byte) {
   419          if len(b) == 3 {
   420              panic(nil)
   421          }
   422      })
   423  }
   424  
   425  func Fuzz_unsupported(f *testing.F) {
   426      m := make(map[string]bool)
   427      f.Add(m)
   428      f.Fuzz(func(*testing.T, []byte) {})
   429  }
   430  
   431  func Fuzz_addDifferentNumber(f *testing.F) {
   432      f.Add([]byte("a"))
   433      f.Add([]byte("a"), []byte("b"))
   434      f.Fuzz(func(*testing.T, []byte) {})
   435  }
   436  
   437  func Fuzz_addDifferentType(f *testing.F) {
   438      f.Add(false)
   439      f.Add(1234)
   440      f.Fuzz(func(*testing.T, []byte) {})
   441  }
   442  
   443  func Fuzz_wrongType(f *testing.F) {
   444      f.Add("hello", 50)
   445      f.Fuzz(func(*testing.T, []byte, int8) {})
   446  }
   447  
   448  -- corpustesting/fuzz_testdata_corpus_test.go --
   449  package fuzz_testdata_corpus
   450  
   451  import "testing"
   452  
   453  func fuzzFn(f *testing.F) {
   454      f.Helper()
   455      f.Fuzz(func(t *testing.T, b []byte) {
   456          if string(b) == "12345" {
   457              t.Fatal("fatal here")
   458          }
   459      })
   460  }
   461  
   462  func Fuzz_fail(f *testing.F) {
   463      fuzzFn(f)
   464  }
   465  
   466  func Fuzz_pass(f *testing.F) {
   467      fuzzFn(f)
   468  }
   469  
   470  func Fuzz_passString(f *testing.F) {
   471      f.Add("some seed corpus")
   472      f.Fuzz(func(*testing.T, string) {})
   473  }
   474  
   475  func Fuzz_panic(f *testing.F) {
   476      f.Fuzz(func(t *testing.T, b []byte) {})
   477  }
   478  
   479  func Fuzz_inNestedDir(f *testing.F) {
   480      f.Fuzz(func(t *testing.T, b []byte) {})
   481  }
   482  
   483  func Fuzz_wrongType(f *testing.F) {
   484      f.Fuzz(func(t *testing.T, b []byte) {})
   485  }
   486  
   487  -- corpustesting/testdata/fuzz/Fuzz_fail/1 --
   488  go test fuzz v1
   489  []byte("12345")
   490  -- corpustesting/testdata/fuzz/Fuzz_pass/1 --
   491  go test fuzz v1
   492  []byte("00000")
   493  -- corpustesting/testdata/fuzz/Fuzz_passString/1 --
   494  go test fuzz v1
   495  string("hello")
   496  -- corpustesting/testdata/fuzz/Fuzz_panic/1 --
   497  malformed
   498  -- corpustesting/testdata/fuzz/Fuzz_inNestedDir/anotherdir/1 --
   499  go test fuzz v1
   500  []byte("12345")
   501  -- corpustesting/testdata/fuzz/Fuzz_wrongType/1 --
   502  go test fuzz v1
   503  int("00000")
   504  

View as plain text