Skip to content

Commit

Permalink
tests: Tweak fuzz tests (#719)
Browse files Browse the repository at this point in the history
  • Loading branch information
klauspost authored Dec 19, 2022
1 parent b2d7bff commit 272fbc7
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 10 deletions.
17 changes: 10 additions & 7 deletions flate/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,19 @@ func FuzzEncoding(f *testing.F) {
startFuzz = HuffmanOnly
endFuzz = BestCompression

// Also tests with dictionaries...
testDicts = true

// Max input size:
maxSize = 1 << 20
)
decoder := NewReader(nil)
buf := new(bytes.Buffer)
encs := make([]*Writer, endFuzz-startFuzz+1)
for i := range encs {
var err error
encs[i], err = NewWriter(nil, i+startFuzz)
if err != nil {
f.Fatal(err.Error())
}
}

f.Fuzz(func(t *testing.T, data []byte) {
if len(data) > maxSize {
Expand All @@ -38,10 +43,8 @@ func FuzzEncoding(f *testing.F) {
for level := startFuzz; level <= endFuzz; level++ {
msg := "level " + strconv.Itoa(level) + ":"
buf.Reset()
fw, err := NewWriter(buf, level)
if err != nil {
t.Fatal(msg + err.Error())
}
fw := encs[level-startFuzz]
fw.Reset(buf)
n, err := fw.Write(data)
if n != len(data) {
t.Fatal(msg + "short write")
Expand Down
Binary file modified flate/testdata/fuzz/FuzzEncoding.zip
Binary file not shown.
6 changes: 3 additions & 3 deletions zstd/fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,14 +146,14 @@ func FuzzEncoding(f *testing.F) {

initEnc := func() func() {
var err error
dec, err = NewReader(nil, WithDecoderConcurrency(2), WithDecoderDicts(dicts...), WithDecoderMaxWindow(128<<10), WithDecoderMaxMemory(maxSize))
dec, err = NewReader(nil, WithDecoderConcurrency(2), WithDecoderDicts(dicts...), WithDecoderMaxWindow(64<<10), WithDecoderMaxMemory(maxSize))
if err != nil {
panic(err)
}
for level := startFuzz; level <= endFuzz; level++ {
encs[level], err = NewWriter(nil, WithEncoderCRC(true), WithEncoderLevel(level), WithEncoderConcurrency(2), WithWindowSize(128<<10), WithZeroFrames(true), WithLowerEncoderMem(true))
encs[level], err = NewWriter(nil, WithEncoderCRC(true), WithEncoderLevel(level), WithEncoderConcurrency(2), WithWindowSize(64<<10), WithZeroFrames(true), WithLowerEncoderMem(true))
if testDicts {
encsD[level], err = NewWriter(nil, WithEncoderCRC(true), WithEncoderLevel(level), WithEncoderConcurrency(2), WithWindowSize(128<<10), WithZeroFrames(true), WithEncoderDict(dicts[0]), WithLowerEncoderMem(true), WithLowerEncoderMem(true))
encsD[level], err = NewWriter(nil, WithEncoderCRC(true), WithEncoderLevel(level), WithEncoderConcurrency(2), WithWindowSize(64<<10), WithZeroFrames(true), WithEncoderDict(dicts[level]), WithLowerEncoderMem(true), WithLowerEncoderMem(true))
}
}
return func() {
Expand Down

0 comments on commit 272fbc7

Please sign in to comment.