Skip to Content
Skip to content
Go Advanced: Performance, Concurrency, and Production
Advanced
Courses
Go Advanced: Performance, Concurrency, and Production

Fuzz Testing Parsers and Decoders With go test -fuzz

Write FuzzX(f *testing.F) targets and run go test -fuzz to uncover edge-case panics in parsers and decoders. After this, fuzz-discovered inputs stay in testdata/fuzz as regression tests.

Course content

Sign in to save your progress.

Course Home

Hand-written table-driven unit tests only test the edge cases you already imagined. In real backend services, parsers, query-string splitters, config loaders, and binary protocol decoders rarely crash on normal inputs—they panic on weird edge cases nobody thought to type by hand: empty keys, invalid UTF-8 bytes, zero-length slices, or repeated delimiters.

Go builds coverage-guided fuzzing right into go test -fuzz. Instead of generating blind random noise, the fuzzer watches which code branches each input hits, mutates inputs to explore uncovered branches at tens of thousands of runs per second, and permanently saves any crashing input under testdata/fuzz/ so regular go test runs replay it forever.

Skip ahead

If you already write FuzzX(f *testing.F) targets with f.Add seeds, run bounded -fuzztime campaigns, and commit testdata/fuzz/ regressions, move on to "Escape Analysis and GC Tuning."

Experiment 1: Writing and running a coverage-guided fuzz target

Suppose you wrote a key-value parser ParseKV for header or config pairs like port=8080. Seed the fuzzer with representative inputs using f.Add(...), then assert invariants that must hold for every possible string in the universe:

package fuzzdemo

import (
	"strings"
	"testing"
)

func ParseKV(s string) (string, string, bool) {
	k, v, ok := strings.Cut(s, "=")
	if !ok || k == "" {
		return "", "", false
	}
	return k, v, true
}

func FuzzParseKV(f *testing.F) {
	f.Add("port=8080")
	f.Add("=bad")
	f.Add("noequals")
	f.Fuzz(func(t *testing.T, input string) {
		k, v, ok := ParseKV(input)
		if ok && k == "" {
			t.Fatalf("empty key accepted for %q", input)
		}
		if ok && !strings.Contains(input, "=") {
			t.Fatalf("accepted input without =: %q -> %q=%q", input, k, v)
		}
	})
}
$ go test -fuzz=FuzzParseKV -fuzztime=2s .
fuzz: elapsed: 0s, gathering baseline coverage: 0/3 completed
fuzz: elapsed: 0s, gathering baseline coverage: 3/3 completed, now fuzzing with 2 workers
fuzz: elapsed: 2s, execs: 186385 (88746/sec), new interesting: 0 (total: 3)
PASS
ok  	example.com/fuzzdemo	2.102s

In just 2 seconds on 2 cores, Go ran 186,385 mutated inputs (~88,746/sec) across every branch of ParseKV!

Experiment 2: What happens when fuzzing actually finds a bug

Imagine an earlier version of ParseKV accidentally sliced s[:idx] without checking idx >= 0, or allowed an empty key on "=". The instant a fuzz worker triggers a panic or t.Fatalf:

  1. Go automatically minimizes the crashing input to the shortest string that still triggers the bug (for example shrinking a 400-byte crash down to "=").
  2. Go writes that exact failing case into testdata/fuzz/FuzzParseKV/<hash>.
  3. Even when you run plain go test ./... (without -fuzz!) in every future PR, Go replays all f.Add seeds plus every saved file in testdata/fuzz/ in milliseconds—turning every fuzzer discovery into a permanent regression test.

Experiment 3: Writing strong fuzz properties (round-trip invariants)

Since you do not know the exact expected output for a random fuzzed string, good fuzz targets assert properties instead of hardcoded answers:

  • No-panic property: Feeding arbitrary bytes into a parser or decoder must return an error cleanly, never panic.
  • Round-trip property: Decode(Encode(x)) == x for any valid structured input.
  • Differential property: Your fast optimized parser returns the exact same output as a simple reference implementation.

Common mistakes

  • Running go test -fuzz=. in CI without -fuzztime: Without -fuzztime=30s, Go fuzzes indefinitely until a crash or CI job timeout.
  • Doing disk or network I/O inside f.Fuzz: Drops fuzzing throughput from ~80,000 execs/sec to ~50 execs/sec. Keep fuzz targets pure in-memory functions.
  • Deleting or gitignoring testdata/fuzz/: Throws away the minimized regression corpus that keeps fixed bugs from coming back.

💡 Tip

Run plain go test ./... on every commit (fast, deterministic), and schedule a nightly CI job running go test -fuzz=FuzzParseKV -fuzztime=60s on every parser, validator, and encoder that touches untrusted external input.

What happens if you skip this step

Happy-path unit tests miss malformed UTF-8, empty slices, and boundary delimiters—leaving slice-bounds panics to be discovered by external traffic or security researchers.

After go test -fuzz=FuzzParseKV finds a panic and saves a file under testdata/fuzz/FuzzParseKV/, do future developers need to pass -fuzz for go test ./... to replay that failing input?

About

Write FuzzX(f *testing.F) targets and run go test -fuzz to uncover edge-case panics in parsers and decoders. After this, fuzz-discovered inputs stay in testdata/fuzz as regression tests.

Duration
5 min
Last update
11/27/2026
Was this helpful?
​ 0 ​ 0

Comments

Sign in to comment

No comments yet.