Skip to content

Commit

Permalink
Merge pull request #97 from skidder/sk-tests-for-xerial-snappy
Browse files Browse the repository at this point in the history
Add tests for Xerial Snappy decode
  • Loading branch information
twmb authored Oct 24, 2021
2 parents d12308a + 39b5bb2 commit 020dab4
Showing 1 changed file with 53 additions and 0 deletions.
53 changes: 53 additions & 0 deletions pkg/kgo/compression_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package kgo

import (
"bytes"
"encoding/base64"
"reflect"
"sync"
"testing"
)
Expand Down Expand Up @@ -101,3 +103,54 @@ func BenchmarkCompress(b *testing.B) {
sliceWriters.Put(w)
}
}

func Test_xerialDecode(t *testing.T) {
tests := []struct {
name string
compressedInput string
want []byte
wantErr bool
}{
{
"Compressed data ok",
"glNOQVBQWQAAAAABAAAAAQAAAA8NMEhlbGxvLCBXb3JsZCE=",
[]byte("Hello, World!"),
false,
},
{
"Compressed data without header",
"/wYAAHNOYVBwWQERAACChVPDSGVsbG8sIFdvcmxkIQ==",
nil,
true,
},
{
"Compressed data less than minimum length, malformed",
"glNOQVBQWQAAAAABAAAAAQAAAA==",
nil,
true,
},
{
"Compressed data not the advertised length",
"glNOQVBQWQAAAAABAAAAAQAAAA8NMEhlbGxvLCBXb3Js",
nil,
true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
data, err := base64.StdEncoding.DecodeString(test.compressedInput)
if err != nil {
t.Errorf("base64 decode error = %v", err)
return
}
got, err := xerialDecode(data)
if (err != nil) != test.wantErr {
t.Errorf("xerialDecode() error = %v, wantErr %v", err, test.wantErr)
return
}
if !reflect.DeepEqual(got, test.want) {
t.Errorf("got decompress %s != exp compress in %s", got, test.want)
}
})
}
}

0 comments on commit 020dab4

Please sign in to comment.