Fuzzing generates or modifies function inputs to expose unexpected edge cases that manually designed tests may miss. It is especially useful when input may be malformed or malicious. section 6.3.2
A practical fuzz test:
- Seeds the fuzz target with ordinary cases, including text, punctuation, Unicode, or emoji. section 6.3.2
- Runs the target with generated inputs rather than requiring a predicted output for every case. section 6.3.2
- Checks an invariant such as avoiding errors, negative values, overflow, or unexpected return formats. section 6.3.2
Finding overflow in a rune-summing function
A function converts each character in a string to a numeric value and returns an int16 sum. Hand-written tests cover normal text and Unicode, while fuzzing supplies additional strings and reports a failure when the result becomes negative, revealing the algorithm's brittle range assumption. section 6.3.2
Listing 6.17 Letting fuzz testing find errors for us
func FuzzSummedRuneCodes(f *testing.F) {
tests := []string{"i am trying things", "doing my best to find a way to
break this!", "even adding emojis or unicode doesn't break it, I'm sure
I'm fine
"}
for t := range tests {
f.Add(tests[t])
}
f.Fuzz(func(t *testing.T, seed string) {
got := summedRuneCodes(seed)
if got < 0 {
t.Errorf("how did this happen? somehow we got %d from string
%s", got, seed)4 }
})
}
Inspect the complete Go fuzz target, including seed strings, the fuzz callback, and its invariant check. section 6.3.2






