From e3b3d725f5cd5e5dfcd20b6df7716d80d731755e Mon Sep 17 00:00:00 2001 From: William Wilson Date: Wed, 16 Mar 2022 13:15:13 -0500 Subject: [PATCH] Use new Go fuzzing in 1.18+ builds --- base/strings_test.go | 2 ++ base/strings_test_go118.go | 54 ++++++++++++++++++++++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 base/strings_test_go118.go diff --git a/base/strings_test.go b/base/strings_test.go index c37629f..651e597 100644 --- a/base/strings_test.go +++ b/base/strings_test.go @@ -1,3 +1,5 @@ +// +build !go1.18 + package base import ( diff --git a/base/strings_test_go118.go b/base/strings_test_go118.go new file mode 100644 index 0000000..eb1e04d --- /dev/null +++ b/base/strings_test_go118.go @@ -0,0 +1,54 @@ +// +build go1.18 + +package base + +import ( + "fmt" + "testing" +) + +func TestEncodeDecode(t *testing.T) { + // crashes identified by go-fuzz + origs := []string{ + "&\x020000", + "&\x020000\x9c", + "&\x020\x9c0", + "&\x0230j", + "&\x02\x98000", + "&\x02\x983\xc8j00", + "00\x000", + "00\x0000", + "00\x0000000000000", + "\x11\x030", + } + + for _, orig := range origs { + escaped := escape(orig) + unescaped, err := unescape(escaped) + if err != nil { + t.Errorf("%s: orig: %#v, escaped: %#v, unescaped: %#v\n", err.Error(), orig, escaped, unescaped) + continue + } + + if unescaped != orig { + t.Errorf("expected: %#v, got: %#v", orig, unescaped) + } + } +} + +// hook for go-fuzz: https://github.com/dvyukov/go-fuzz +func Fuzz(f *testing.F) { + f.Fuzz(func(t *testing.T, data []byte) { + orig := string(data) + escaped := escape(orig) + + unescaped, err := unescape(escaped) + if err != nil { + t.SkipNow() + } + + if unescaped != orig { + panic(fmt.Sprintf("unescaped: \"%#v\", != orig: \"%#v\"", unescaped, orig)) + } + }) +}