Fuzz parsers with a seed corpus
Fuzz parsers with a seed corpus
Fuzzing supplies generated inputs to a test to find unexpected behavior. Seed inputs identify useful starting points such as empty, valid and malformed data. Go runs seeds during normal go test; active fuzzing needs go test -run=^$ -fuzz=^FuzzLesson$ -fuzztime=2s -parallel=2. Use a fresh module, one fuzz target, bounded input size and a meaningful invariant rather than treating every parse error as a defect.
Create the files shown below in a fresh directory, then run go test -count=1 -v ./... there. The separator lines are file labels, not source code. Go 1.23 is used for these local examples.
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "strconv" "testing")
func FuzzLesson(f *testing.F) { for _, s := range []string{"", "0", "-1", "127", "128", "bad"} { f.Add(s) } f.Fuzz(func(t *testing.T, s string) { if len(s) > 128 { t.Skip() } n, err := strconv.ParseInt(s, 10, 8) if err != nil { return } if n < -128 || n > 127 { t.Fatal(n) } again, err := strconv.ParseInt(strconv.FormatInt(n, 10), 10, 8) if err != nil || again != n { t.Fatal(again, err) } })}The target accepts strings; invalid syntax and overflow are legitimate errors. Successful parses must fit signed eight-bit range and survive a canonical decimal round trip. A panic or failed assertion is a bug candidate. Go saves reproducible failing inputs under testdata/fuzz/<target> when possible. Keep reviewed regression inputs and rerun them with ordinary go test. Fuzzing time bounds exploration, not proof.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: FuzzLessonCheck a parser against an independent grammar rule
--- go.mod ---module example.test/lesson
go 1.23
--- lesson_test.go ---package lesson
import ( "encoding/hex" "strings" "testing")
func FuzzLesson(f *testing.F) { for _, s := range []string{"", "00", "FF", "a", "xx"} { f.Add(s) } f.Fuzz(func(t *testing.T, s string) { if len(s) > 1024 { t.Skip() } b, err := hex.DecodeString(s) if err != nil { return } if len(s) != 2*len(b) || hex.EncodeToString(b) != strings.ToLower(s) { t.Fatal("hex invariant") } })}Each successful hex parse consumes two ASCII hexadecimal digits per byte. Canonical encoding is lowercase. On error DecodeString can return partial bytes, which this property intentionally ignores. Do not feed unbounded data to a resource-intensive parser. Keep fuzz targets free from shared mutable state and real external side effects.
Expected result (timings and formatting may vary; essential text shown):
--- PASS: FuzzLessonTry it locally
Fuzz strconv.ParseUint with base 10 and bitSize 8. Seed 0, 255, 256, -1 and an empty string. Check range and round trip of successful parses. Run seeds and a two-second fuzz session. Use a fresh module with the same go.mod shown above; run go test -count=1 -v ./...
Check Your Understanding
Solidify your learning with the interactive assessment directly below this lesson.
Concept Quiz & Questions
Advanced testing 04: Fuzz parsers with a seed corpus
Check the lesson objective before moving on.
What does normal go test do to fuzz targets?
What should a parser fuzz target do with expected invalid syntax?