Created
September 13, 2022 19:50
-
-
Save prestonvasquez/8038c092b803edd2ed1a85ecffd8e979 to your computer and use it in GitHub Desktop.
Fuzz Testing Example
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
go test fuzz v1 | |
int(-42) | |
string("hello") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
func Foo(i int, s string) (string, error) { | |
if i%7 == 0 && s == "hello" { | |
return s, fmt.Errorf("error") | |
} | |
return fmt.Sprintf("%d: %s", i, s), nil | |
} | |
func FuzzFoo(f *testing.F) { | |
f.Add(5, "hello") // Seed corpus addition | |
f.Fuzz(func(t *testing.T, i int, s string) { | |
out, err := Foo(i, s) | |
if err != nil && out != "" { | |
t.Errorf("Foo(%v, %v) returned error %v", i, s, err) | |
} | |
}) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment