...

Source file src/google.golang.org/protobuf/internal/fuzztest/fuzztest.go

Documentation: google.golang.org/protobuf/internal/fuzztest

     1  // Copyright 2019 The Go Authors. All rights reserved.
     2  // Use of this source code is governed by a BSD-style
     3  // license that can be found in the LICENSE file.
     4  
     5  // Package fuzztest contains a common fuzzer test.
     6  package fuzztest
     7  
     8  import (
     9  	"flag"
    10  	"os"
    11  	"path/filepath"
    12  	"sort"
    13  	"testing"
    14  )
    15  
    16  var corpus = flag.String("corpus", "corpus", "directory containing the fuzzer corpus")
    17  
    18  // Test executes a fuzz function for every entry in the corpus.
    19  func Test(t *testing.T, fuzz func(b []byte) int) {
    20  	dir, err := os.Open(*corpus)
    21  	if err != nil {
    22  		t.Fatal(err)
    23  	}
    24  	infos, err := dir.Readdir(0)
    25  	if err != nil {
    26  		t.Fatal(err)
    27  
    28  	}
    29  	var names []string
    30  	for _, info := range infos {
    31  		names = append(names, info.Name())
    32  	}
    33  	sort.Strings(names)
    34  	for _, name := range names {
    35  		t.Run(name, func(t *testing.T) {
    36  			b, err := os.ReadFile(filepath.Join(*corpus, name))
    37  			if err != nil {
    38  				t.Fatal(err)
    39  			}
    40  			b = b[:len(b):len(b)] // set cap to len
    41  			fuzz(b)
    42  		})
    43  	}
    44  }
    45  

View as plain text