Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

huff0: Return compression error. #172

Merged
merged 2 commits into from
Oct 24, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions huff0/compress.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ func compress(in []byte, s *Scratch, compressor func(src []byte) ([]byte, error)
keepTable := s.cTable
s.cTable = s.prevTable
s.Out, err = compressor(in)
if err != nil {
return nil, false, err
}
s.cTable = keepTable
if len(s.Out) >= len(in) {
return nil, false, ErrIncompressible
Expand Down
114 changes: 114 additions & 0 deletions huff0/compress_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,120 @@ func init() {
}
}

func TestCompressRegression(t *testing.T) {
// Match the fuzz function
var testInput = func(data []byte) int {
var sc Scratch
comp, _, err := Compress1X(data, &sc)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
s, remain, err := ReadTable(comp, nil)
if err != nil {
panic(err)
}
out, err := s.Decompress1X(remain)
if err != nil {
panic(err)
}
if !bytes.Equal(out, data) {
panic("decompression 1x mismatch")
}
// Reuse as 4X
sc.Reuse = ReusePolicyAllow
comp, reUsed, err := Compress4X(data, &sc)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
remain = comp
if !reUsed {
s, remain, err = ReadTable(comp, s)
if err != nil {
panic(err)
}
}
out, err = s.Decompress4X(remain, len(data))
if err != nil {
panic(err)
}
if !bytes.Equal(out, data) {
panic("decompression 4x with reuse mismatch")
}

s.Reuse = ReusePolicyNone
comp, reUsed, err = Compress4X(data, s)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
if reUsed {
panic("reused when asked not to")
}
s, remain, err = ReadTable(comp, nil)
if err != nil {
panic(err)
}
out, err = s.Decompress4X(remain, len(data))
if err != nil {
panic(err)
}
if !bytes.Equal(out, data) {
panic("decompression 4x mismatch")
}

// Reuse as 1X
s.Reuse = ReusePolicyAllow
comp, reUsed, err = Compress1X(data, &sc)
if err == ErrIncompressible || err == ErrUseRLE || err == ErrTooBig {
return 0
}
if err != nil {
panic(err)
}
remain = comp
if !reUsed {
s, remain, err = ReadTable(comp, s)
if err != nil {
panic(err)
}
}
out, err = s.Decompress1X(remain)
if err != nil {
panic(err)
}
if !bytes.Equal(out, data) {
panic("decompression 1x with reuse mismatch")
}
return 1
}
for _, test := range testfiles {
t.Run(test.name, func(t *testing.T) {
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
}
testInput(buf0)
})
}
for _, test := range testfilesExtended {
t.Run(test.name, func(t *testing.T) {
buf0, err := test.fn()
if err != nil {
t.Fatal(err)
}
testInput(buf0)
})
}
}

func TestCompress1X(t *testing.T) {
for _, test := range testfiles {
t.Run(test.name, func(t *testing.T) {
Expand Down
Binary file modified huff0/testdata/regression.zip
Binary file not shown.