liveBook · Manning

Fuzzing

As explained in Go in Practice, Second Edition

Fuzz testing is software testing that exercises a program with varied, automatically generated inputs to reveal unexpected behavior.

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 figure"}
    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

How Go in Practice, Second Edition covers it

How deep it goes
Defines fuzzing and demonstrates Go seed cases plus a fuzz callback, but does not detail fuzzing internals.
Examples
Uses a rune-summing function, hand-written strings with Unicode and emojis, and a negative-result failure condition.
What it assumes
Assumes familiarity with function tests and expected outputs, then shifts to cases without one predictable answer.
What you can do after
Apply it via Go's testing tools by seeding inputs and checking invariants for errors or unexpected formats.
How it connects
Connects fuzz testing to unit tests, security testing, malicious input, SQL injection, and benchmarking.

Also explained in

Compare how 7 books explain fuzzing →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Go in Practice, Second Edition. The section links point to the parts of the book it draws on.