aboutsummaryrefslogtreecommitdiffstats
path: root/vendor/github.com/ClickHouse/ch-go/compress/fuzz_test.go
blob: bc2f50c57eb6e354d587468ffae7a82fb98b7908 (plain) (blame)
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
package compress

import (
	"bytes"
	"encoding/binary"
	"io"
	"testing"

	"github.com/go-faster/city"
	"github.com/stretchr/testify/require"
)

func FuzzWriter_Compress(f *testing.F) {
	f.Add([]byte("Hello, world!"))
	f.Add([]byte{})
	f.Add([]byte{1, 2, 3, 4, 5})
	f.Fuzz(func(t *testing.T, data []byte) {
		w := NewWriter()
		require.NoError(t, w.Compress(LZ4, data))

		r := NewReader(bytes.NewReader(w.Data))
		out := make([]byte, len(data))
		_, err := io.ReadFull(r, out)
		require.NoError(t, err)
		require.Equal(t, data, out)
	})
}

func FuzzReader_Read(f *testing.F) {
	for _, data := range [][]byte{
		{},
		[]byte("Hello, world!"),
		{1, 2, 3, 4, 5},
	} {
		w := NewWriter()
		require.NoError(f, w.Compress(LZ4, data))
		f.Add(w.Data)
	}

	f.Fuzz(func(t *testing.T, data []byte) {
		if len(data) > headerSize {
			h := city.CH128(data[hMethod:])
			binary.LittleEndian.PutUint64(data[0:8], h.Low)
			binary.LittleEndian.PutUint64(data[8:16], h.High)
		}

		r := NewReader(bytes.NewReader(data))
		out := make([]byte, len(data))
		_, _ = io.ReadFull(r, out)
	})
}