Fuzz and property tests in Go
Scoring a parser against a corpus checks the cases the corpus holds. A fuzz target and a property test go past
them. The fuzzer mutates the corpus documents, and yamlgen draws new documents for
rapid. In both, the oracle gives the verdict on any document,
so a test can check documents nobody wrote down.
The examples below test go-openapi/go-yaml. Replace yaml.Unmarshal
with your parser’s entry point.
Install
Each is a module of its own, so a test requires only what it uses.
Seed a fuzz target
Add every corpus document as a seed. The corpus already covers the grammar, so the fuzzer starts there and spends its time on combinations nobody generated.
The check runs in one direction only. A parser that accepts a document the grammar rejects has a defect, unless
you declare that leniency. A parser that refuses a document the grammar accepts may be right: the specification
sets rules beyond the grammar, such as unique keys, and your parser takes positions on the questions it leaves open.
To check that direction too, give each document a verdict with your stance.Table, as
Score a parser does for the corpus cases.
go test without -fuzz runs each seed once: 25,175 distinct documents, about 6 seconds with go-yaml.
With -fuzz, Go first measures the coverage of every seed, which takes about a minute before the fuzzer starts
mutating. Seed a subset, such as the refused cases, to start sooner.
go-yaml’s own fuzz_test.go adds the YAML Test
Suite and the inputs reduced from past bug reports to the same seeds.
JSON
RFC 8259 leaves little to the implementation, so a JSON fuzz target can compare verdicts in both directions:
The JSON corpus is embedded in the root module, so this needs no other module.
Draw documents with rapid
yamlgen.Values draws what a document means and yamlgen.Styles draws how to write it. yamlgen.Emit writes a
value in a style, and every document it writes is valid YAML 1.2. Two properties follow, and neither needs an
expected value.
Every style reads the same
Write one value in two styles. A parser must read both documents as the same value.
Three choices keep this property free of false failures:
- A style that declares
%YAML 1.1is skipped. Under YAML 1.1,<<is a merge key and some plain spellings resolve to other types, so the two documents can mean different things. - A value that both styles fail to read is skipped. go-yaml, for example, refuses a mapping as a key when it reads
into
any. That is a position the parser takes, and it does not depend on the style. - The two readings are compared through
yaml.Marshal. Comparing Go values directly fails on NaN, and printing them shows the addresses of pointers such as*big.Float.
No broken document is accepted
yamlgen.Mutate breaks a valid document. Many mutations leave a document that is still valid YAML, so ask the
oracle first, and keep only the documents it rejects.
how names the mutation, such as a-document-marker-inside, so a failure tells you what kind of break the parser
missed.
When a property fails
rapid shrinks the drawn value and style to a small failing case, and prints a -rapid.failfile flag that replays
it. Raise the number of draws with -rapid.checks: the default is 100, and the rarer shapes appear only after
thousands of draws.
go-yaml’s tests go further. They compare each document with the value the generator meant (yamlgen.Write
returns it as Written.Means), and they keep a ledger of known divergences (yamlgen.Divergence), so that a known
defect does not stop the run and a fixed one is noticed.