Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
177 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,130 @@ | ||
//go:build go1.18 | ||
// +build go1.18 | ||
|
||
package s2 | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/klauspost/compress/internal/fuzz" | ||
"github.com/klauspost/compress/internal/snapref" | ||
) | ||
|
||
func FuzzEncodingBlocks(f *testing.F) { | ||
fuzz.AddFromZip(f, "testdata/enc_regressions.zip", true, false) | ||
fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-raw.zip", true, testing.Short()) | ||
fuzz.AddFromZip(f, "testdata/fuzz/block-corpus-enc.zip", false, testing.Short()) | ||
|
||
// Fuzzing tweaks: | ||
const ( | ||
// Max input size: | ||
maxSize = 8 << 20 | ||
) | ||
|
||
f.Fuzz(func(t *testing.T, data []byte) { | ||
if len(data) > maxSize { | ||
return | ||
} | ||
|
||
writeDst := make([]byte, MaxEncodedLen(len(data)), MaxEncodedLen(len(data))+4) | ||
writeDst = append(writeDst, 1, 2, 3, 4) | ||
defer func() { | ||
got := writeDst[MaxEncodedLen(len(data)):] | ||
want := []byte{1, 2, 3, 4} | ||
if !bytes.Equal(got, want) { | ||
t.Fatalf("want %v, got %v - dest modified outside cap", want, got) | ||
} | ||
}() | ||
compDst := writeDst[:MaxEncodedLen(len(data)):MaxEncodedLen(len(data))] // Hard cap | ||
decDst := make([]byte, len(data)) | ||
comp := Encode(compDst, data) | ||
decoded, err := Decode(decDst, comp) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if !bytes.Equal(data, decoded) { | ||
t.Error("block decoder mismatch") | ||
return | ||
} | ||
if mel := MaxEncodedLen(len(data)); len(comp) > mel { | ||
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) | ||
return | ||
} | ||
comp = EncodeBetter(compDst, data) | ||
decoded, err = Decode(decDst, comp) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if !bytes.Equal(data, decoded) { | ||
t.Error("block decoder mismatch") | ||
return | ||
} | ||
if mel := MaxEncodedLen(len(data)); len(comp) > mel { | ||
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) | ||
return | ||
} | ||
|
||
comp = EncodeBest(compDst, data) | ||
decoded, err = Decode(decDst, comp) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if !bytes.Equal(data, decoded) { | ||
t.Error("block decoder mismatch") | ||
return | ||
} | ||
if mel := MaxEncodedLen(len(data)); len(comp) > mel { | ||
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) | ||
return | ||
} | ||
|
||
comp = EncodeSnappy(compDst, data) | ||
decoded, err = snapref.Decode(decDst, comp) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if !bytes.Equal(data, decoded) { | ||
t.Error("block decoder mismatch") | ||
return | ||
} | ||
if mel := MaxEncodedLen(len(data)); len(comp) > mel { | ||
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) | ||
return | ||
} | ||
comp = EncodeSnappyBetter(compDst, data) | ||
decoded, err = snapref.Decode(decDst, comp) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if !bytes.Equal(data, decoded) { | ||
t.Error("block decoder mismatch") | ||
return | ||
} | ||
if mel := MaxEncodedLen(len(data)); len(comp) > mel { | ||
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) | ||
return | ||
} | ||
|
||
comp = EncodeSnappyBest(compDst, data) | ||
decoded, err = snapref.Decode(decDst, comp) | ||
if err != nil { | ||
t.Error(err) | ||
return | ||
} | ||
if !bytes.Equal(data, decoded) { | ||
t.Error("block decoder mismatch") | ||
return | ||
} | ||
if mel := MaxEncodedLen(len(data)); len(comp) > mel { | ||
t.Error(fmt.Errorf("MaxEncodedLen Exceed: input: %d, mel: %d, got %d", len(data), mel, len(comp))) | ||
return | ||
} | ||
}) | ||
} |
Binary file not shown.
Binary file not shown.