Skip to content

Commit bd3c172

Browse files
authored
tests: Add s2 fuzz tests (#662)
1 parent c474b64 commit bd3c172

File tree

4 files changed

+177
-28
lines changed

4 files changed

+177
-28
lines changed

s2/encode_test.go

Lines changed: 47 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -87,39 +87,58 @@ func TestEncoderRegression(t *testing.T) {
8787
if testing.Short() && len(data) > 10000 {
8888
t.SkipNow()
8989
}
90+
var blocksTested bool
9091
for name, opts := range testOptions(t) {
9192
t.Run(name, func(t *testing.T) {
9293
var buf bytes.Buffer
9394
dec := NewReader(nil)
9495
enc := NewWriter(&buf, opts...)
9596

96-
comp := Encode(make([]byte, MaxEncodedLen(len(data))), data)
97-
decoded, err := Decode(nil, comp)
98-
if err != nil {
99-
t.Error(err)
100-
return
101-
}
102-
if !bytes.Equal(data, decoded) {
103-
t.Error("block decoder mismatch")
104-
return
105-
}
106-
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
107-
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
108-
return
109-
}
110-
comp = EncodeBetter(make([]byte, MaxEncodedLen(len(data))), data)
111-
decoded, err = Decode(nil, comp)
112-
if err != nil {
113-
t.Error(err)
114-
return
115-
}
116-
if !bytes.Equal(data, decoded) {
117-
t.Error("block decoder mismatch")
118-
return
119-
}
120-
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
121-
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
122-
return
97+
if !blocksTested {
98+
comp := Encode(make([]byte, MaxEncodedLen(len(data))), data)
99+
decoded, err := Decode(nil, comp)
100+
if err != nil {
101+
t.Error(err)
102+
return
103+
}
104+
if !bytes.Equal(data, decoded) {
105+
t.Error("block decoder mismatch")
106+
return
107+
}
108+
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
109+
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
110+
return
111+
}
112+
comp = EncodeBetter(make([]byte, MaxEncodedLen(len(data))), data)
113+
decoded, err = Decode(nil, comp)
114+
if err != nil {
115+
t.Error(err)
116+
return
117+
}
118+
if !bytes.Equal(data, decoded) {
119+
t.Error("block decoder mismatch")
120+
return
121+
}
122+
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
123+
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
124+
return
125+
}
126+
127+
comp = EncodeBest(make([]byte, MaxEncodedLen(len(data))), data)
128+
decoded, err = Decode(nil, comp)
129+
if err != nil {
130+
t.Error(err)
131+
return
132+
}
133+
if !bytes.Equal(data, decoded) {
134+
t.Error("block decoder mismatch")
135+
return
136+
}
137+
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
138+
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
139+
return
140+
}
141+
blocksTested = true
123142
}
124143

125144
// Test writer.
@@ -143,7 +162,7 @@ func TestEncoderRegression(t *testing.T) {
143162
t.Error(err)
144163
return
145164
}
146-
comp = buf.Bytes()
165+
comp := buf.Bytes()
147166
if enc.pad > 0 && len(comp)%enc.pad != 0 {
148167
t.Error(fmt.Errorf("wanted size to be mutiple of %d, got size %d with remainder %d", enc.pad, len(comp), len(comp)%enc.pad))
149168
return

s2/fuzz_test.go

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
//go:build go1.18
2+
// +build go1.18
3+
4+
package s2
5+
6+
import (
7+
"bytes"
8+
"fmt"
9+
"testing"
10+
11+
"github.com/klauspost/compress/internal/fuzz"
12+
"github.com/klauspost/compress/internal/snapref"
13+
)
14+
15+
func FuzzEncodingBlocks(f *testing.F) {
16+
fuzz.AddFromZip(f, "testdata/enc_regressions.zip", true, false)
17+
fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-raw.zip", true, testing.Short())
18+
fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-enc.zip", false, testing.Short())
19+
20+
// Fuzzing tweaks:
21+
const (
22+
// Max input size:
23+
maxSize = 8 << 20
24+
)
25+
26+
f.Fuzz(func(t *testing.T, data []byte) {
27+
if len(data) > maxSize {
28+
return
29+
}
30+
31+
writeDst := make([]byte, MaxEncodedLen(len(data)), MaxEncodedLen(len(data))+4)
32+
writeDst = append(writeDst, 1, 2, 3, 4)
33+
defer func() {
34+
got := writeDst[MaxEncodedLen(len(data)):]
35+
want := []byte{1, 2, 3, 4}
36+
if !bytes.Equal(got, want) {
37+
t.Fatalf("want %v, got %v - dest modified outside cap", want, got)
38+
}
39+
}()
40+
compDst := writeDst[:MaxEncodedLen(len(data)):MaxEncodedLen(len(data))] // Hard cap
41+
decDst := make([]byte, len(data))
42+
comp := Encode(compDst, data)
43+
decoded, err := Decode(decDst, comp)
44+
if err != nil {
45+
t.Error(err)
46+
return
47+
}
48+
if !bytes.Equal(data, decoded) {
49+
t.Error("block decoder mismatch")
50+
return
51+
}
52+
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
53+
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
54+
return
55+
}
56+
comp = EncodeBetter(compDst, data)
57+
decoded, err = Decode(decDst, comp)
58+
if err != nil {
59+
t.Error(err)
60+
return
61+
}
62+
if !bytes.Equal(data, decoded) {
63+
t.Error("block decoder mismatch")
64+
return
65+
}
66+
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
67+
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
68+
return
69+
}
70+
71+
comp = EncodeBest(compDst, data)
72+
decoded, err = Decode(decDst, comp)
73+
if err != nil {
74+
t.Error(err)
75+
return
76+
}
77+
if !bytes.Equal(data, decoded) {
78+
t.Error("block decoder mismatch")
79+
return
80+
}
81+
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
82+
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
83+
return
84+
}
85+
86+
comp = EncodeSnappy(compDst, data)
87+
decoded, err = snapref.Decode(decDst, comp)
88+
if err != nil {
89+
t.Error(err)
90+
return
91+
}
92+
if !bytes.Equal(data, decoded) {
93+
t.Error("block decoder mismatch")
94+
return
95+
}
96+
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
97+
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
98+
return
99+
}
100+
comp = EncodeSnappyBetter(compDst, data)
101+
decoded, err = snapref.Decode(decDst, comp)
102+
if err != nil {
103+
t.Error(err)
104+
return
105+
}
106+
if !bytes.Equal(data, decoded) {
107+
t.Error("block decoder mismatch")
108+
return
109+
}
110+
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
111+
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
112+
return
113+
}
114+
115+
comp = EncodeSnappyBest(compDst, data)
116+
decoded, err = snapref.Decode(decDst, comp)
117+
if err != nil {
118+
t.Error(err)
119+
return
120+
}
121+
if !bytes.Equal(data, decoded) {
122+
t.Error("block decoder mismatch")
123+
return
124+
}
125+
if mel := MaxEncodedLen(len(data)); len(comp) > mel {
126+
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp)))
127+
return
128+
}
129+
})
130+
}
2.13 MB
Binary file not shown.
8.03 MB
Binary file not shown.

0 commit comments

Comments
 (0)