From ad98b2fa5f26dfc77a08bb861eb425104a05951c Mon Sep 17 00:00:00 2001 From: Johannes Kohnen Date: Mon, 26 Apr 2021 12:56:17 +0200 Subject: [PATCH] Add fuzz target for FromString Uses the fuzzer of the Go tool chain in [branch dev.fuzz](https://github.com/golang/go/tree/dev.fuzz). See also https://github.com/golang/go/issues/44551 and https://go.googlesource.com/proposal/+/master/design/draft-fuzzing.md --- airac_fuzz_test.go | 30 +++++++++++++++++++ airac_test.go | 4 +++ go.mod | 2 +- ...96e30cb4f52cd0e65cd3c1843393c5a535f97c7a6e | 2 ++ ...0af05f0e1c4b74934119ed36156d64e32768cbcc4e | 2 ++ 5 files changed, 39 insertions(+), 1 deletion(-) create mode 100644 airac_fuzz_test.go create mode 100644 testdata/corpus/FuzzFromString/44ea5456f4caf7ee4b0cb896e30cb4f52cd0e65cd3c1843393c5a535f97c7a6e create mode 100644 testdata/corpus/FuzzFromString/cde16d0f1e64e24bd633e50af05f0e1c4b74934119ed36156d64e32768cbcc4e diff --git a/airac_fuzz_test.go b/airac_fuzz_test.go new file mode 100644 index 0000000..27eac48 --- /dev/null +++ b/airac_fuzz_test.go @@ -0,0 +1,30 @@ +//+build go.dev.fuzz + +package airac + +import ( + "strconv" + "strings" + "testing" +) + +//goland:noinspection GoUnusedExportedFunction +func FuzzFromString(f *testing.F) { + for i := -999; i <= 9999; i++ { + f.Add(strconv.Itoa(i)) + } + + f.Fuzz(func(t *testing.T, s string) { + t.Parallel() + + a, err := FromString(s) + if err != nil { + t.SkipNow() + } + + s2 := a.String() + if strings.TrimSpace(s) != s2 { + t.Errorf("%q != %q", s, s2) + } + }) +} diff --git a/airac_test.go b/airac_test.go index 9d9390d..5086ffa 100644 --- a/airac_test.go +++ b/airac_test.go @@ -238,6 +238,10 @@ func TestFromString(t *testing.T) { // found by fuzzer (case testdata/corpus/FuzzFromString/44ea5456f4caf7ee4b0cb896e30cb4f52cd0e65cd3c1843393c5a535f97c7a6e) {"+911", "", 0, 0, false}, + + + // found by fuzzer (case testdata/corpus/FuzzFromString/cde16d0f1e64e24bd633e50af05f0e1c4b74934119ed36156d64e32768cbcc4e) + {" 7807", "1978-07-13", 1978, 07, true}, } for i, tt := range testt { diff --git a/go.mod b/go.mod index bc9a832..dbac545 100644 --- a/go.mod +++ b/go.mod @@ -1,3 +1,3 @@ module github.com/jwkohnen/airac -go 1.12 +go 1.16 diff --git a/testdata/corpus/FuzzFromString/44ea5456f4caf7ee4b0cb896e30cb4f52cd0e65cd3c1843393c5a535f97c7a6e b/testdata/corpus/FuzzFromString/44ea5456f4caf7ee4b0cb896e30cb4f52cd0e65cd3c1843393c5a535f97c7a6e new file mode 100644 index 0000000..56301d7 --- /dev/null +++ b/testdata/corpus/FuzzFromString/44ea5456f4caf7ee4b0cb896e30cb4f52cd0e65cd3c1843393c5a535f97c7a6e @@ -0,0 +1,2 @@ +go test fuzz v1 +string("+911") \ No newline at end of file diff --git a/testdata/corpus/FuzzFromString/cde16d0f1e64e24bd633e50af05f0e1c4b74934119ed36156d64e32768cbcc4e b/testdata/corpus/FuzzFromString/cde16d0f1e64e24bd633e50af05f0e1c4b74934119ed36156d64e32768cbcc4e new file mode 100644 index 0000000..501899b --- /dev/null +++ b/testdata/corpus/FuzzFromString/cde16d0f1e64e24bd633e50af05f0e1c4b74934119ed36156d64e32768cbcc4e @@ -0,0 +1,2 @@ +go test fuzz v1 +string(" 7807") \ No newline at end of file