From 36edcbaaa0c53e908e38347e62088252faed7c13 Mon Sep 17 00:00:00 2001 From: Evgenii Stratonikov Date: Thu, 2 May 2024 18:11:38 +0300 Subject: [PATCH] [#72] chain/test: Refactor fuzz tests Make it possible to execute fuzz tests with different backend, such as go-fuzz which supports coverage collection. Signed-off-by: Evgenii Stratonikov --- pkg/chain/marshal_binary_test.go | 25 ----------------------- pkg/chain/marshal_fuzz.go | 13 ++++++++++++ pkg/chain/marshal_fuzz_test.go | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 47 insertions(+), 25 deletions(-) create mode 100644 pkg/chain/marshal_fuzz.go create mode 100644 pkg/chain/marshal_fuzz_test.go diff --git a/pkg/chain/marshal_binary_test.go b/pkg/chain/marshal_binary_test.go index bc6045f..57176de 100644 --- a/pkg/chain/marshal_binary_test.go +++ b/pkg/chain/marshal_binary_test.go @@ -28,31 +28,6 @@ func TestInvalidChainData(t *testing.T) { require.Error(t, ch.UnmarshalBinary([]byte("\x00\x00:aws:iam::namespace:group/so\x82\x82\x82\x82\x82\x82u\x82"))) } -func FuzzUnmarshal(f *testing.F) { - for _, id := range generateTestIDs() { - for _, rules := range generateTestRules() { - for _, matchType := range generateTestMatchTypes() { - - chain := Chain{ - ID: id, - Rules: rules, - MatchType: matchType, - } - data, err := chain.MarshalBinary() - require.NoError(f, err) - f.Add(data) - } - } - } - - f.Fuzz(func(t *testing.T, data []byte) { - var ch Chain - require.NotPanics(t, func() { - _ = ch.UnmarshalBinary(data) - }) - }) -} - func performMarshalTest(t *testing.T, id ID, r []Rule, mt MatchType) { chain := Chain{ ID: id, diff --git a/pkg/chain/marshal_fuzz.go b/pkg/chain/marshal_fuzz.go new file mode 100644 index 0000000..b5cbec9 --- /dev/null +++ b/pkg/chain/marshal_fuzz.go @@ -0,0 +1,13 @@ +//go:build gofuzz +// +build gofuzz + +package chain + +func DoFuzzChainUnmarshalBinary(data []byte) int { + var ch Chain + err := ch.UnmarshalBinary(data) + if err != nil { + return 0 + } + return 1 +} diff --git a/pkg/chain/marshal_fuzz_test.go b/pkg/chain/marshal_fuzz_test.go new file mode 100644 index 0000000..f6956c5 --- /dev/null +++ b/pkg/chain/marshal_fuzz_test.go @@ -0,0 +1,34 @@ +//go:build gofuzz +// +build gofuzz + +package chain + +import ( + "testing" + + "github.com/stretchr/testify/require" +) + +func FuzzUnmarshal(f *testing.F) { + for _, id := range generateTestIDs() { + for _, rules := range generateTestRules() { + for _, matchType := range generateTestMatchTypes() { + + chain := Chain{ + ID: id, + Rules: rules, + MatchType: matchType, + } + data, err := chain.MarshalBinary() + require.NoError(f, err) + f.Add(data) + } + } + } + + f.Fuzz(func(t *testing.T, data []byte) { + require.NotPanics(t, func() { + DoFuzzChainUnmarshalBinary(data) + }) + }) +} -- 2.45.2