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

     1  # TODO(jayconrod): support shared memory on more platforms.
     2  [!darwin] [!linux] [!windows] skip
     3  
     4  # Verify that the fuzzing engine records the actual crashing input, even when
     5  # a worker process terminates without communicating the crashing input back
     6  # to the coordinator.
     7  
     8  [short] skip
     9  
    10  # Start fuzzing. The worker crashes after 100 iterations.
    11  # The fuzz function writes the crashing input to "want" before exiting.
    12  # The fuzzing engine reconstructs the crashing input and saves it to testdata.
    13  ! exists want
    14  ! go test -fuzz=. -parallel=1 -fuzztime=110x -fuzzminimizetime=10x -v
    15  stdout '^\s+fuzzing process hung or terminated unexpectedly: exit status'
    16  stdout 'Failing input written to testdata'
    17  
    18  # Run the fuzz target without fuzzing. The fuzz function is called with the
    19  # crashing input in testdata. The test passes if that input is identical to
    20  # the one saved in "want".
    21  exists want
    22  go test -want=want
    23  
    24  -- go.mod --
    25  module fuzz
    26  
    27  go 1.17
    28  -- fuzz_test.go --
    29  package fuzz
    30  
    31  import (
    32  	"bytes"
    33  	"flag"
    34  	"os"
    35  	"testing"
    36  )
    37  
    38  var wantFlag = flag.String("want", "", "file containing previous crashing input")
    39  
    40  func FuzzRepeat(f *testing.F) {
    41  	i := 0
    42  	f.Fuzz(func(t *testing.T, b []byte) {
    43  		i++
    44  		if i == 100 {
    45  			f, err := os.OpenFile("want", os.O_WRONLY|os.O_CREATE|os.O_EXCL, 0666)
    46  			if err != nil {
    47  				// Couldn't create the file. Return without crashing, and try
    48  				// again.
    49  				i--
    50  				t.Skip(err)
    51  			}
    52  			if _, err := f.Write(b); err != nil {
    53  				// We already created the file, so if we failed to write it
    54  				// there's not much we can do. The test will fail anyway, but
    55  				// at least make sure the error is logged to stdout.
    56  				t.Fatal(err)
    57  			}
    58  			if err := f.Close(); err != nil {
    59  				t.Fatal(err)
    60  			}
    61  			os.Exit(1) // crash without communicating
    62  		}
    63  
    64  		if *wantFlag != "" {
    65  			want, err := os.ReadFile(*wantFlag)
    66  			if err != nil {
    67  				t.Fatal(err)
    68  			}
    69  			if !bytes.Equal(want, b) {
    70  				t.Fatalf("inputs are not equal!\n got: %q\nwant:%q", b, want)
    71  			}
    72  		}
    73  	})
    74  }
    75  

View as plain text