Damien Neil | 75f53c5 | 2019-12-20 10:33:37 -0800 | [diff] [blame^] | 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 | "io/ioutil" |
| 11 | "os" |
| 12 | "path/filepath" |
| 13 | "sort" |
| 14 | "testing" |
| 15 | ) |
| 16 | |
| 17 | var corpus = flag.String("corpus", "corpus", "directory containing the fuzzer corpus") |
| 18 | |
| 19 | // Test executes a fuzz function for every entry in the corpus. |
| 20 | func 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 | } |
| 41 | fuzz(b) |
| 42 | }) |
| 43 | } |
| 44 | } |