Golang / GoLang System Architecture and Testing Interview Questions
How does Go's built-in fuzzing work and when should you use property-based testing?
Go 1.18 added native fuzz testing via go test -fuzz. Fuzzing automatically generates inputs that exercise edge cases your hand-written tests miss — particularly effective for parsers, serialisers, and cryptographic code.
// Fuzz test â finds inputs that cause a panic or incorrect result func FuzzParseURL(f *testing.F) { // Seed corpus: known interesting inputs f.Add("https://example.com/path?q=1") f.Add("http://user:pass@host:8080/") f.Add("") f.Fuzz(func(t *testing.T, s string) { // Property: parsing should never panic u, err := url.Parse(s) if err != nil { return } // error is ok, panic is not // Property: round-trip should be stable // parsing the string representation should give the same URL u2, err := url.Parse(u.String()) if err != nil { t.Errorf("round-trip parse failed: %v", err) } if u.String() != u2.String() { t.Errorf("round-trip changed URL: %q â %q", u.String(), u2.String()) } }) } // Another fuzz example: JSON encode/decode round-trip func FuzzJSONRoundTrip(f *testing.F) { f.Add(`{"name":"Alice","age":30}`) f.Fuzz(func(t *testing.T, data []byte) { var v map[string]any if err := json.Unmarshal(data, &v); err != nil { return } encoded, err := json.Marshal(v) if err != nil { t.Errorf("marshal failed after successful unmarshal: %v", err) } var v2 map[string]any if err := json.Unmarshal(encoded, &v2); err != nil { t.Errorf("second unmarshal failed: %v", err) } }) } // Run fuzzing: // go test -fuzz=FuzzParseURL # fuzz until stopped // go test -fuzz=FuzzParseURL -fuzztime=60s # fuzz for 60 seconds // go test # replays corpus only (CI)
When to use fuzzing: parsers (JSON, YAML, protobuf), network protocol handlers, cryptographic code, regular expression engines, any function that accepts arbitrary byte/string input. Fuzzing found critical bugs in Go's own standard library.
More Related questions...