File: encoder_decoder_fuzz_test.go

package info (click to toggle)
golang-github-ibm-sarama 1.45.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,964 kB
  • sloc: makefile: 35; sh: 19
file content (65 lines) | stat: -rw-r--r-- 1,309 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//go:build go1.18 && !functional

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, nil)
			if err != nil {
				continue
			}
			out, err := encode(req, nil)
			if err != nil {
				t.Logf("%v: encode: %v", in, err)
				continue
			}
			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, nil)
			if err != nil {
				continue
			}
			out, err := encode(req, nil)
			if err != nil {
				t.Logf("%v: encode: %v", in, err)
				continue
			}
			if !bytes.Equal(in, out) {
				t.Logf("%v: not equal after round trip: %v", in, out)
			}
		}
	})
}