Skip to content

Commit

Permalink
encoder: add fuzz test for compact encoder
Browse files Browse the repository at this point in the history
We have envountered issues with unhandled types slipping through the cracks in the compact
encoder. Let's add a fuzz test that should help catch these cases in the future.

Signed-off-by: William Findlay <will@isovalent.com>
  • Loading branch information
willfindlay authored and jrfastab committed Aug 16, 2024
1 parent eda5693 commit 2cee513
Showing 1 changed file with 49 additions and 0 deletions.
49 changes: 49 additions & 0 deletions pkg/encoder/encoder_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"google.golang.org/protobuf/types/known/timestamppb"

"github.com/cilium/tetragon/api/v1/tetragon"
"github.com/cilium/tetragon/api/v1/tetragon/codegen/helpers"
"github.com/sryoya/protorand"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -535,3 +536,51 @@ func FuzzProtojsonCompatibility(f *testing.F) {
assert.True(t, proto.Equal(msg, msgProtojson))
})
}

func FuzzCompactEncoder(f *testing.F) {
for _, n := range []int64{
1337,
78776406,
56343416,
68876713,
51156281,
45544244,
4011756,
} {
for _, cm := range []uint8{0, 1, 2} {
for _, ts := range []bool{true, false} {
for _, st := range []bool{true, false} {
f.Add(n, cm, ts, st)
}
}
}
}
f.Fuzz(func(t *testing.T, seed int64, colorMode uint8, timestamps bool, stackTraces bool) {
var cm ColorMode
switch colorMode % 3 {
case 0:
cm = "never"
case 1:
cm = "always"
case 2:
cm = "auto"
default:
panic("unreachable")
}

pr := protorand.New()
pr.Seed(seed)
ev := &tetragon.GetEventsResponse{}
msg, err := pr.Gen(ev)
require.NoError(t, err)

if helpers.ResponseGetProcess(msg.(*tetragon.GetEventsResponse)) == nil {
t.Skipf("Empty process")
}

var buf1 bytes.Buffer
compactEncoder := NewCompactEncoder(&buf1, cm, timestamps, stackTraces)
err = compactEncoder.Encode(msg)
require.NoError(t, err)
})
}

0 comments on commit 2cee513

Please sign in to comment.