...

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

Documentation: cmd/go/testdata/script

     1# Test that minimization doesn't use dirty coverage snapshots when it
     2# is unable to actually minimize the input. We do this by checking that
     3# an expected value appears in the cache. If a dirty coverage map is used
     4# (i.e. the coverage map generated during the last minimization step,
     5# rather than the map provided with the initial input) then this value
     6# is unlikely to appear in the cache, since the map generated during
     7# the last minimization step should not increase the coverage.
     8
     9[short] skip
    10[!fuzz-instrumented] skip
    11
    12env GOCACHE=$WORK/gocache
    13go test -fuzz=FuzzCovMin -fuzztime=500000x -test.fuzzcachedir=$GOCACHE/fuzz
    14go run check_file/main.go $GOCACHE/fuzz/FuzzCovMin ab
    15
    16-- go.mod --
    17module test
    18
    19-- covmin_test.go --
    20package covmin
    21
    22import "testing"
    23
    24func FuzzCovMin(f *testing.F) {
    25	f.Add([]byte("aa"))
    26	f.Fuzz(func(t *testing.T, data []byte) {
    27		if len(data) == 2 && data[0] == 'a' && data[1] == 'b' {
    28			return
    29		}
    30	})
    31}
    32
    33-- check_file/main.go --
    34package main
    35
    36import (
    37	"bytes"
    38	"fmt"
    39	"os"
    40	"path/filepath"
    41	"regexp"
    42	"strconv"
    43)
    44
    45func checkFile(name, expected string) (bool, error) {
    46	data, err := os.ReadFile(name)
    47	if err != nil {
    48		return false, err
    49	}
    50	for _, line := range bytes.Split(data, []byte("\n")) {
    51		m := valRe.FindSubmatch(line)
    52		if m == nil {
    53			continue
    54		}
    55		fmt.Println(strconv.Unquote(string(m[1])))
    56		if s, err := strconv.Unquote(string(m[1])); err != nil {
    57			return false, err
    58		} else if s == expected {
    59			return true, nil
    60		}
    61	}
    62	return false, nil
    63}
    64
    65var valRe = regexp.MustCompile(`^\[\]byte\(([^)]+)\)$`)
    66
    67func main() {
    68	dir, expected := os.Args[1], os.Args[2]
    69	ents, err := os.ReadDir(dir)
    70	if err != nil {
    71		fmt.Fprintln(os.Stderr, err)
    72		os.Exit(1)
    73	}
    74	for _, ent := range ents {
    75		name := filepath.Join(dir, ent.Name())
    76		if good, err := checkFile(name, expected); err != nil {
    77			fmt.Fprintln(os.Stderr, err)
    78			os.Exit(1)
    79		} else if good {
    80			os.Exit(0)
    81		}
    82	}
    83	fmt.Fprintln(os.Stderr, "input over minimized")
    84	os.Exit(1)
    85}

View as plain text