blob: 2b54eb59f66e148cc78b5734db569962b918e2e8 [file] [log] [blame]
Damien Neil75f53c52019-12-20 10:33:37 -08001// 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.
6package fuzztest
7
8import (
9 "flag"
10 "io/ioutil"
11 "os"
12 "path/filepath"
13 "sort"
14 "testing"
15)
16
17var corpus = flag.String("corpus", "corpus", "directory containing the fuzzer corpus")
18
19// Test executes a fuzz function for every entry in the corpus.
20func Test(t *testing.T, fuzz func(b []byte) int) {
21 dir, err := os.Open(*corpus)
22 if err != nil {
23 t.Fatal(err)
24 }
25 infos, err := dir.Readdir(0)
26 if err != nil {
27 t.Fatal(err)
28
29 }
30 var names []string
31 for _, info := range infos {
32 names = append(names, info.Name())
33 }
34 sort.Strings(names)
35 for _, name := range names {
36 t.Run(name, func(t *testing.T) {
37 b, err := ioutil.ReadFile(filepath.Join(*corpus, name))
38 if err != nil {
39 t.Fatal(err)
40 }
Damien Neil4eeab362020-02-05 14:42:17 -080041 b = b[:len(b):len(b)] // set cap to len
Damien Neil75f53c52019-12-20 10:33:37 -080042 fuzz(b)
43 })
44 }
45}