| 1 |
//go:build go1.18 |
| 2 |
|
| 3 |
package s2 |
| 4 |
|
| 5 |
import ( |
| 6 |
"bytes" |
| 7 |
"fmt" |
| 8 |
"io" |
| 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", fuzz.TypeRaw, false) |
| 17 |
fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-raw.zip", fuzz.TypeRaw, testing.Short()) |
| 18 |
fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-enc.zip", fuzz.TypeGoFuzz, 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 |
concat, err := ConcatBlocks(nil, data, []byte{0}) |
| 131 |
if err != nil || concat == nil { |
| 132 |
return |
| 133 |
} |
| 134 |
|
| 135 |
EstimateBlockSize(data) |
| 136 |
encoded := make([]byte, MaxEncodedLen(len(data))) |
| 137 |
if len(encoded) < MaxEncodedLen(len(data)) || minNonLiteralBlockSize > len(data) || len(data) > maxBlockSize { |
| 138 |
return |
| 139 |
} |
| 140 |
|
| 141 |
encodeBlockGo(encoded, data) |
| 142 |
encodeBlockBetterGo(encoded, data) |
| 143 |
encodeBlockSnappyGo(encoded, data) |
| 144 |
encodeBlockBetterSnappyGo(encoded, data) |
| 145 |
if len(data) <= 64<<10 { |
| 146 |
encodeBlockGo64K(encoded, data) |
| 147 |
encodeBlockSnappyGo64K(encoded, data) |
| 148 |
encodeBlockBetterGo64K(encoded, data) |
| 149 |
encodeBlockBetterSnappyGo64K(encoded, data) |
| 150 |
} |
| 151 |
dst := encodeGo(encoded, data) |
| 152 |
if dst == nil { |
| 153 |
return |
| 154 |
} |
| 155 |
}) |
| 156 |
} |
| 157 |
|
| 158 |
func FuzzStreamDecode(f *testing.F) { |
| 159 |
enc := NewWriter(nil, WriterBlockSize(8<<10)) |
| 160 |
addCompressed := func(b []byte) { |
| 161 |
var buf bytes.Buffer |
| 162 |
enc.Reset(&buf) |
| 163 |
enc.Write(b) |
| 164 |
enc.Close() |
| 165 |
f.Add(buf.Bytes()) |
| 166 |
} |
| 167 |
fuzz.ReturnFromZip(f, "testdata/enc_regressions.zip", fuzz.TypeRaw, addCompressed) |
| 168 |
fuzz.ReturnFromZip(f, "testdata/fuzz/block-corpus-raw.zip", fuzz.TypeRaw, addCompressed) |
| 169 |
fuzz.ReturnFromZip(f, "testdata/fuzz/block-corpus-enc.zip", fuzz.TypeGoFuzz, addCompressed) |
| 170 |
dec := NewReader(nil, ReaderIgnoreCRC()) |
| 171 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 172 |
// Using Read |
| 173 |
dec.Reset(bytes.NewReader(data)) |
| 174 |
io.Copy(io.Discard, dec) |
| 175 |
|
| 176 |
// Using DecodeConcurrent |
| 177 |
dec.Reset(bytes.NewReader(data)) |
| 178 |
dec.DecodeConcurrent(io.Discard, 2) |
| 179 |
|
| 180 |
// Use ByteReader. |
| 181 |
dec.Reset(bytes.NewReader(data)) |
| 182 |
for { |
| 183 |
_, err := dec.ReadByte() |
| 184 |
if err != nil { |
| 185 |
break |
| 186 |
} |
| 187 |
} |
| 188 |
}) |
| 189 |
} |
| 190 |
|
| 191 |
func FuzzDecodeBlock(f *testing.F) { |
| 192 |
addCompressed := func(b []byte) { |
| 193 |
b2 := Encode(nil, b) |
| 194 |
f.Add(b2) |
| 195 |
f.Add(EncodeBetter(nil, b)) |
| 196 |
} |
| 197 |
fuzz.ReturnFromZip(f, "testdata/enc_regressions.zip", fuzz.TypeRaw, addCompressed) |
| 198 |
fuzz.ReturnFromZip(f, "testdata/fuzz/block-corpus-raw.zip", fuzz.TypeRaw, addCompressed) |
| 199 |
fuzz.ReturnFromZip(f, "testdata/fuzz/block-corpus-enc.zip", fuzz.TypeGoFuzz, addCompressed) |
| 200 |
fuzz.AddFromZip(f, "testdata/dec-block-regressions.zip", fuzz.TypeRaw, false) |
| 201 |
|
| 202 |
f.Fuzz(func(t *testing.T, data []byte) { |
| 203 |
if t.Failed() { |
| 204 |
return |
| 205 |
} |
| 206 |
|
| 207 |
dCopy := append([]byte{}, data...) |
| 208 |
dlen, err := DecodedLen(data) |
| 209 |
if dlen > 8<<20 { |
| 210 |
return |
| 211 |
} |
| 212 |
base, baseErr := Decode(nil, data) |
| 213 |
if !bytes.Equal(data, dCopy) { |
| 214 |
t.Fatal("data was changed") |
| 215 |
} |
| 216 |
hasErr := baseErr != nil |
| 217 |
dataCapped := make([]byte, 0, len(data)+1024) |
| 218 |
dataCapped = append(dataCapped, data...) |
| 219 |
dataCapped = append(dataCapped, bytes.Repeat([]byte{0xff, 0xff, 0xff, 0xff}, 1024/4)...) |
| 220 |
dataCapped = dataCapped[:len(data):len(data)] |
| 221 |
if dlen > MaxBlockSize { |
| 222 |
dlen = MaxBlockSize |
| 223 |
} |
| 224 |
dst2 := bytes.Repeat([]byte{0xfe}, dlen+1024) |
| 225 |
got, err := Decode(dst2[:dlen:dlen], dataCapped[:len(data)]) |
| 226 |
if !bytes.Equal(dataCapped[:len(data)], dCopy) { |
| 227 |
t.Fatal("data was changed") |
| 228 |
} |
| 229 |
if err != nil && !hasErr { |
| 230 |
t.Fatalf("base err: %v, capped: %v", baseErr, err) |
| 231 |
} |
| 232 |
for i, v := range dst2[dlen:] { |
| 233 |
if v != 0xfe { |
| 234 |
t.Errorf("DST overwritten beyond cap! index %d: got 0x%02x, want 0x%02x, err:%v", i, v, 0xfe, err) |
| 235 |
break |
| 236 |
} |
| 237 |
} |
| 238 |
if baseErr == nil { |
| 239 |
if !bytes.Equal(got, base) { |
| 240 |
t.Fatal("data mismatch") |
| 241 |
} |
| 242 |
gotLen, err := DecodedLen(data) |
| 243 |
if err != nil { |
| 244 |
t.Errorf("DecodedLen returned error: %v", err) |
| 245 |
} else if gotLen != len(got) { |
| 246 |
t.Errorf("DecodedLen mismatch: got %d, want %d", gotLen, len(got)) |
| 247 |
} |
| 248 |
} |
| 249 |
}) |
| 250 |
} |