Skip to content

Commit

Permalink
feat(test): add a simple fuzzing example
Browse files Browse the repository at this point in the history
  • Loading branch information
dnwe committed Dec 1, 2021
1 parent 556ddf7 commit 2b76321
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 14 deletions.
64 changes: 64 additions & 0 deletions encoder_decoder_fuzz_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
//go:build go1.18
// +build go1.18

package sarama

import (
"bytes"
"testing"
)

func FuzzDecodeEncodeProduceRequest(f *testing.F) {
for _, seed := range [][]byte{
produceRequestEmpty,
produceRequestHeader,
produceRequestOneMessage,
produceRequestOneRecord,
} {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, in []byte) {
for i := int16(0); i < 8; i++ {
req := &ProduceRequest{}
err := versionedDecode(in, req, i)
if err != nil {
continue
}
out, err := encode(req, nil)
if err != nil {
t.Fatalf("%v: encode: %v", in, err)
}
if !bytes.Equal(in, out) {
t.Logf("%v: not equal after round trip: %v", in, out)
}
}
})
}

func FuzzDecodeEncodeFetchRequest(f *testing.F) {
for _, seed := range [][]byte{
fetchRequestNoBlocks,
fetchRequestWithProperties,
fetchRequestOneBlock,
fetchRequestOneBlockV4,
fetchRequestOneBlockV11,
} {
f.Add(seed)
}
f.Fuzz(func(t *testing.T, in []byte) {
for i := int16(0); i < 11; i++ {
req := &FetchRequest{}
err := versionedDecode(in, req, i)
if err != nil {
continue
}
out, err := encode(req, nil)
if err != nil {
t.Fatalf("%v: encode: %v", in, err)
}
if !bytes.Equal(in, out) {
t.Logf("%v: not equal after round trip: %v", in, out)
}
}
})
}
28 changes: 14 additions & 14 deletions request.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,21 +119,21 @@ func decodeRequest(r io.Reader) (*request, int, error) {
func allocateBody(key, version int16) protocolBody {
switch key {
case 0:
return &ProduceRequest{}
return &ProduceRequest{Version: version}
case 1:
return &FetchRequest{Version: version}
case 2:
return &OffsetRequest{Version: version}
case 3:
return &MetadataRequest{}
return &MetadataRequest{Version: version}
case 8:
return &OffsetCommitRequest{Version: version}
case 9:
return &OffsetFetchRequest{Version: version}
case 10:
return &FindCoordinatorRequest{}
return &FindCoordinatorRequest{Version: version}
case 11:
return &JoinGroupRequest{}
return &JoinGroupRequest{Version: version}
case 12:
return &HeartbeatRequest{}
case 13:
Expand All @@ -145,13 +145,13 @@ func allocateBody(key, version int16) protocolBody {
case 16:
return &ListGroupsRequest{}
case 17:
return &SaslHandshakeRequest{}
return &SaslHandshakeRequest{Version: version}
case 18:
return &ApiVersionsRequest{Version: version}
case 19:
return &CreateTopicsRequest{}
return &CreateTopicsRequest{Version: version}
case 20:
return &DeleteTopicsRequest{}
return &DeleteTopicsRequest{Version: version}
case 21:
return &DeleteRecordsRequest{}
case 22:
Expand All @@ -167,15 +167,15 @@ func allocateBody(key, version int16) protocolBody {
case 29:
return &DescribeAclsRequest{}
case 30:
return &CreateAclsRequest{}
return &CreateAclsRequest{Version: version}
case 31:
return &DeleteAclsRequest{}
case 32:
return &DescribeConfigsRequest{}
return &DescribeConfigsRequest{Version: version}
case 33:
return &AlterConfigsRequest{}
case 35:
return &DescribeLogDirsRequest{}
return &DescribeLogDirsRequest{Version: version}
case 36:
return &SaslAuthenticateRequest{}
case 37:
Expand All @@ -185,19 +185,19 @@ func allocateBody(key, version int16) protocolBody {
case 44:
return &IncrementalAlterConfigsRequest{}
case 45:
return &AlterPartitionReassignmentsRequest{}
return &AlterPartitionReassignmentsRequest{Version: version}
case 46:
return &ListPartitionReassignmentsRequest{}
return &ListPartitionReassignmentsRequest{Version: version}
case 47:
return &DeleteOffsetsRequest{}
case 48:
return &DescribeClientQuotasRequest{}
case 49:
return &AlterClientQuotasRequest{}
case 50:
return &DescribeUserScramCredentialsRequest{}
return &DescribeUserScramCredentialsRequest{Version: version}
case 51:
return &AlterUserScramCredentialsRequest{}
return &AlterUserScramCredentialsRequest{Version: version}
}
return nil
}

0 comments on commit 2b76321

Please sign in to comment.