1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57
|
// License: GPLv3 Copyright: 2023, Kovid Goyal, <kovid at kovidgoyal.net>
package utils
import (
"bytes"
"compress/zlib"
"crypto/rand"
"fmt"
"io"
"testing"
)
var _ = fmt.Print
func TestStreamDecompressor(t *testing.T) {
input := make([]byte, 9723)
io.ReadFull(rand.Reader, input)
b := bytes.Buffer{}
w := zlib.NewWriter(&b)
io.Copy(w, bytes.NewReader(input))
w.Close()
o := bytes.Buffer{}
sd := NewStreamDecompressor(zlib.NewReader, &o)
data := b.Bytes()
for len(data) > 0 {
chunk := data[:Min(117, len(data))]
data = data[len(chunk):]
if err := sd(chunk, len(data) == 0); err != nil {
t.Fatal(err)
}
}
if !bytes.Equal(o.Bytes(), input) {
t.Fatalf("Roundtripping via zlib failed output (%d) != input (%d)", len(o.Bytes()), len(input))
}
o.Reset()
sd = NewStreamDecompressor(zlib.NewReader, &o)
err := sd([]byte("abcd"), true)
if err == nil {
t.Fatalf("Did not get an invalid header error from zlib")
}
o.Reset()
sd = NewStreamDecompressor(zlib.NewReader, &o)
err = sd(b.Bytes(), false)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(o.Bytes(), input) {
t.Fatalf("Roundtripping via zlib failed output (%d) != input (%d)", len(o.Bytes()), len(input))
}
err = sd([]byte("extra trailing data"), true)
if err == nil {
t.Fatalf("Did not get an invalid header error from zlib")
}
}
|