! stdout FAIL
! stdout 'fatal here'
+# Test fails with file containing wrong type
+! go test -run FuzzWrongType corpustesting/fuzz_testdata_corpus_test.go
+! stdout ^ok
+stdout FAIL
+
-- noop_fuzz_test.go --
package noop_fuzz
f.Fuzz(func(t *testing.T, b []byte) {})
}
+func FuzzWrongType(f *testing.F) {
+ f.Fuzz(func(t *testing.T, b []byte) {})
+}
+
-- corpustesting/testdata/corpus/FuzzFail/1 --
go test fuzz v1
[]byte("12345")
malformed
-- corpustesting/testdata/corpus/FuzzInNestedDir/anotherdir/1 --
go test fuzz v1
-[]byte("12345")
\ No newline at end of file
+[]byte("12345")
+-- corpustesting/testdata/corpus/FuzzWrongType/1 --
+go test fuzz v1
+int("00000")
\ No newline at end of file
if err != nil {
return nil, fmt.Errorf("failed to read corpus file: %v", err)
}
- vals, err := unmarshalCorpusFile(data)
+ var vals []interface{}
+ vals, err = readCorpusData(data, types)
if err != nil {
- errs = append(errs, fmt.Errorf("failed to unmarshal %q: %v", filename, err))
- continue
- }
- if len(vals) != len(types) {
- errs = append(errs, fmt.Errorf("wrong number of values in corpus file %q: %d, want %d", filename, len(vals), len(types)))
+ errs = append(errs, fmt.Errorf("%q: %v", filename, err))
continue
}
- for i := range types {
- if reflect.TypeOf(vals[i]) != types[i] {
- errs = append(errs, fmt.Errorf("mismatched types in corpus file %q: %v, want %v", filename, vals, types))
- continue
- }
- }
corpus = append(corpus, CorpusEntry{Name: file.Name(), Data: data, Values: vals})
}
if len(errs) > 0 {
return corpus, nil
}
+func readCorpusData(data []byte, types []reflect.Type) ([]interface{}, error) {
+ vals, err := unmarshalCorpusFile(data)
+ if err != nil {
+ return nil, fmt.Errorf("unmarshal: %v", err)
+ }
+ if len(vals) != len(types) {
+ return nil, fmt.Errorf("wrong number of values in corpus file: %d, want %d", len(vals), len(types))
+ }
+ for i := range types {
+ if reflect.TypeOf(vals[i]) != types[i] {
+ return nil, fmt.Errorf("mismatched types in corpus file: %v, want %v", vals, types)
+ }
+ }
+ return vals, nil
+}
+
// writeToCorpus atomically writes the given bytes to a new file in testdata.
// If the directory does not exist, it will create one. If the file already
// exists, writeToCorpus will not rewrite it. writeToCorpus returns the