...

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

Documentation: cmd/go/testdata/script

     1[!fuzz] skip
     2[short] skip
     3env 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
    12stdout 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
    18stdout 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
    23stdout 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
    28stdout FAIL
    29
    30# Test that successful test exits cleanly.
    31go test -vet=off success_fuzz_test.go
    32stdout ^ok
    33! stdout FAIL
    34
    35# Test that successful fuzzing exits cleanly.
    36go test -vet=off -fuzz=Fuzz -fuzztime=1x success_fuzz_test.go
    37stdout 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
    43stdout 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
    48stdout FAIL
    49stdout '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
    56stdout FAIL
    57
    58# Test that skipped test exits cleanly.
    59go test -vet=off skipped_fuzz_test.go
    60stdout 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'
    67stdout FAIL
    68stdout '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'
    74stdout FAIL
    75stdout '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
    80stdout FAIL
    81stdout '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'
    87stdout FAIL
    88stdout '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'
    94stdout FAIL
    95stdout 'fuzz target'
    96stdout '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
   101stdout 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
   106stdout 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
   111stdout 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
   116stdout FAIL
   117stdout 'failed some precondition'
   118
   119# Test success with seed corpus in f.Fuzz
   120go test -vet=off -run Fuzz_pass -vet=off fuzz_add_test.go
   121stdout 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
   128stdout FAIL
   129stdout '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
   134stdout FAIL
   135stdout '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
   140stdout 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
   145stdout 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
   150stdout 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
   155stdout 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
   160stdout '\[string int\], want \[\[\]uint8 int8\]'
   161stdout 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
   166stdout FAIL
   167stdout 'fatal here'
   168
   169# Test pass with testdata seed corpus
   170go test -vet=off -run Fuzz_pass corpustesting/fuzz_testdata_corpus_test.go
   171stdout ok
   172! stdout FAIL
   173! stdout 'fatal here'
   174
   175# Test pass with testdata and f.Add seed corpus
   176go test -vet=off -run Fuzz_passString corpustesting/fuzz_testdata_corpus_test.go
   177stdout ok
   178! stdout FAIL
   179
   180# Fuzzing pass with testdata and f.Add seed corpus (skip running tests first)
   181go test -vet=off -run=None -fuzz=Fuzz_passString corpustesting/fuzz_testdata_corpus_test.go -fuzztime=10x
   182stdout ok
   183! stdout FAIL
   184
   185# Fuzzing pass with testdata and f.Add seed corpus
   186go test -vet=off -run=Fuzz_passString -fuzz=Fuzz_passString corpustesting/fuzz_testdata_corpus_test.go -fuzztime=10x
   187stdout 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
   193stdout FAIL
   194
   195# Test pass with file in other nested testdata directory
   196go test -vet=off -run Fuzz_inNestedDir corpustesting/fuzz_testdata_corpus_test.go
   197stdout 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
   204stdout FAIL
   205
   206-- noop_fuzz_test.go --
   207package noop_fuzz
   208
   209import "testing"
   210
   211func Fuzz(f *testing.F) {}
   212
   213-- error_fuzz_test.go --
   214package error_fuzz
   215
   216import "testing"
   217
   218func Fuzz(f *testing.F) {
   219    f.Error("error in target")
   220}
   221
   222-- fatal_fuzz_test.go --
   223package fatal_fuzz
   224
   225import "testing"
   226
   227func Fuzz(f *testing.F) {
   228    f.Fatal("fatal in target")
   229}
   230
   231-- panic_fuzz_test.go --
   232package panic_fuzz
   233
   234import "testing"
   235
   236func Fuzz_panic(f *testing.F) {
   237    panic(nil)
   238}
   239
   240-- success_fuzz_test.go --
   241package success_fuzz
   242
   243import "testing"
   244
   245func Fuzz(f *testing.F) {
   246    f.Fuzz(func (*testing.T, []byte) {})
   247}
   248
   249-- skipped_fuzz_test.go --
   250package skipped_fuzz
   251
   252import "testing"
   253
   254func Fuzz(f *testing.F) {
   255    f.Skip()
   256}
   257
   258-- fatal_fuzz_fn_fuzz_test.go --
   259package fatal_fuzz_fn_fuzz
   260
   261import "testing"
   262
   263func 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 --
   271package error_fuzz_fn_fuzz
   272
   273import "testing"
   274
   275func 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 --
   283package skip_fuzz_fn_fuzz
   284
   285import "testing"
   286
   287func 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 --
   295package skip_fuzz_fn_fuzz
   296
   297import "testing"
   298
   299func 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 --
   307package skipped_fuzz_fn_fuzz
   308
   309import "testing"
   310
   311func 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 --
   320package goexit_fuzz_fn_fuzz
   321
   322import "testing"
   323
   324func 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 --
   332package fatal_after_fuzz_func_fuzz
   333
   334import "testing"
   335
   336func 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 --
   344package incomplete_fuzz_call_fuzz
   345
   346import "testing"
   347
   348func 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 --
   355package cleanup_fuzz_test
   356
   357import "testing"
   358
   359func 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 --
   369package fuzz_add
   370
   371import "testing"
   372
   373func add(f *testing.F) {
   374    f.Helper()
   375    f.Add([]byte("123"))
   376    f.Add([]byte("12345"))
   377    f.Add([]byte(""))
   378}
   379
   380func 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
   389func 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
   398func 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
   407func 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
   416func 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
   425func Fuzz_unsupported(f *testing.F) {
   426    m := make(map[string]bool)
   427    f.Add(m)
   428    f.Fuzz(func(*testing.T, []byte) {})
   429}
   430
   431func 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
   437func Fuzz_addDifferentType(f *testing.F) {
   438    f.Add(false)
   439    f.Add(1234)
   440    f.Fuzz(func(*testing.T, []byte) {})
   441}
   442
   443func 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 --
   449package fuzz_testdata_corpus
   450
   451import "testing"
   452
   453func 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
   462func Fuzz_fail(f *testing.F) {
   463    fuzzFn(f)
   464}
   465
   466func Fuzz_pass(f *testing.F) {
   467    fuzzFn(f)
   468}
   469
   470func Fuzz_passString(f *testing.F) {
   471    f.Add("some seed corpus")
   472    f.Fuzz(func(*testing.T, string) {})
   473}
   474
   475func Fuzz_panic(f *testing.F) {
   476    f.Fuzz(func(t *testing.T, b []byte) {})
   477}
   478
   479func Fuzz_inNestedDir(f *testing.F) {
   480    f.Fuzz(func(t *testing.T, b []byte) {})
   481}
   482
   483func Fuzz_wrongType(f *testing.F) {
   484    f.Fuzz(func(t *testing.T, b []byte) {})
   485}
   486
   487-- corpustesting/testdata/fuzz/Fuzz_fail/1 --
   488go test fuzz v1
   489[]byte("12345")
   490-- corpustesting/testdata/fuzz/Fuzz_pass/1 --
   491go test fuzz v1
   492[]byte("00000")
   493-- corpustesting/testdata/fuzz/Fuzz_passString/1 --
   494go test fuzz v1
   495string("hello")
   496-- corpustesting/testdata/fuzz/Fuzz_panic/1 --
   497malformed
   498-- corpustesting/testdata/fuzz/Fuzz_inNestedDir/anotherdir/1 --
   499go test fuzz v1
   500[]byte("12345")
   501-- corpustesting/testdata/fuzz/Fuzz_wrongType/1 --
   502go test fuzz v1
   503int("00000")

View as plain text