| 1 |
// Copyright 2014 The Go Authors. All rights reserved. |
| 2 |
// Use of this source code is governed by a BSD-style |
| 3 |
// license that can be found in the LICENSE file. |
| 4 |
|
| 5 |
package flate |
| 6 |
|
| 7 |
import ( |
| 8 |
"bytes" |
| 9 |
"crypto/rand" |
| 10 |
"io" |
| 11 |
"os" |
| 12 |
"strconv" |
| 13 |
"strings" |
| 14 |
"testing" |
| 15 |
) |
| 16 |
|
| 17 |
func TestReset(t *testing.T) { |
| 18 |
ss := []string{ |
| 19 |
"lorem ipsum izzle fo rizzle", |
| 20 |
"the quick brown fox jumped over", |
| 21 |
} |
| 22 |
|
| 23 |
deflated := make([]bytes.Buffer, 2) |
| 24 |
for i, s := range ss { |
| 25 |
w, _ := NewWriter(&deflated[i], 1) |
| 26 |
w.Write([]byte(s)) |
| 27 |
w.Close() |
| 28 |
} |
| 29 |
|
| 30 |
inflated := make([]bytes.Buffer, 2) |
| 31 |
|
| 32 |
f := NewReader(&deflated[0]) |
| 33 |
io.Copy(&inflated[0], f) |
| 34 |
f.(Resetter).Reset(&deflated[1], nil) |
| 35 |
io.Copy(&inflated[1], f) |
| 36 |
f.Close() |
| 37 |
|
| 38 |
for i, s := range ss { |
| 39 |
if s != inflated[i].String() { |
| 40 |
t.Errorf("inflated[%d]:\ngot %q\nwant %q", i, inflated[i].String(), s) |
| 41 |
} |
| 42 |
} |
| 43 |
} |
| 44 |
|
| 45 |
func TestReaderTruncated(t *testing.T) { |
| 46 |
vectors := []struct{ input, output string }{ |
| 47 |
{"\x00", ""}, |
| 48 |
{"\x00\f", ""}, |
| 49 |
{"\x00\f\x00", ""}, |
| 50 |
{"\x00\f\x00\xf3\xff", ""}, |
| 51 |
{"\x00\f\x00\xf3\xffhello", "hello"}, |
| 52 |
{"\x00\f\x00\xf3\xffhello, world", "hello, world"}, |
| 53 |
{"\x02", ""}, |
| 54 |
{"\xf2H\xcd", "He"}, |
| 55 |
{"\xf2H͙0a\u0084\t", "Hel\x90\x90\x90\x90\x90"}, |
| 56 |
{"\xf2H͙0a\u0084\t\x00", "Hel\x90\x90\x90\x90\x90"}, |
| 57 |
} |
| 58 |
|
| 59 |
for i, v := range vectors { |
| 60 |
r := strings.NewReader(v.input) |
| 61 |
zr := NewReader(r) |
| 62 |
b, err := io.ReadAll(zr) |
| 63 |
if err != io.ErrUnexpectedEOF { |
| 64 |
t.Errorf("test %d, error mismatch: got %v, want io.ErrUnexpectedEOF", i, err) |
| 65 |
} |
| 66 |
if string(b) != v.output { |
| 67 |
t.Errorf("test %d, output mismatch: got %q, want %q", i, b, v.output) |
| 68 |
} |
| 69 |
} |
| 70 |
} |
| 71 |
|
| 72 |
func TestResetDict(t *testing.T) { |
| 73 |
dict := []byte("the lorem fox") |
| 74 |
ss := []string{ |
| 75 |
"lorem ipsum izzle fo rizzle", |
| 76 |
"the quick brown fox jumped over", |
| 77 |
} |
| 78 |
|
| 79 |
deflated := make([]bytes.Buffer, len(ss)) |
| 80 |
for i, s := range ss { |
| 81 |
w, _ := NewWriterDict(&deflated[i], DefaultCompression, dict) |
| 82 |
w.Write([]byte(s)) |
| 83 |
w.Close() |
| 84 |
} |
| 85 |
|
| 86 |
inflated := make([]bytes.Buffer, len(ss)) |
| 87 |
|
| 88 |
f := NewReader(nil) |
| 89 |
for i := range inflated { |
| 90 |
f.(Resetter).Reset(&deflated[i], dict) |
| 91 |
io.Copy(&inflated[i], f) |
| 92 |
} |
| 93 |
f.Close() |
| 94 |
|
| 95 |
for i, s := range ss { |
| 96 |
if s != inflated[i].String() { |
| 97 |
t.Errorf("inflated[%d]:\ngot %q\nwant %q", i, inflated[i].String(), s) |
| 98 |
} |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
// Tests ported from zlib/test/infcover.c |
| 103 |
type infTest struct { |
| 104 |
hex string |
| 105 |
id string |
| 106 |
n int |
| 107 |
} |
| 108 |
|
| 109 |
var infTests = []infTest{ |
| 110 |
{"0 0 0 0 0", "invalid stored block lengths", 1}, |
| 111 |
{"3 0", "fixed", 0}, |
| 112 |
{"6", "invalid block type", 1}, |
| 113 |
{"1 1 0 fe ff 0", "stored", 0}, |
| 114 |
{"fc 0 0", "too many length or distance symbols", 1}, |
| 115 |
{"4 0 fe ff", "invalid code lengths set", 1}, |
| 116 |
{"4 0 24 49 0", "invalid bit length repeat", 1}, |
| 117 |
{"4 0 24 e9 ff ff", "invalid bit length repeat", 1}, |
| 118 |
{"4 0 24 e9 ff 6d", "invalid code -- missing end-of-block", 1}, |
| 119 |
{"4 80 49 92 24 49 92 24 71 ff ff 93 11 0", "invalid literal/lengths set", 1}, |
| 120 |
{"4 80 49 92 24 49 92 24 f b4 ff ff c3 84", "invalid distances set", 1}, |
| 121 |
{"4 c0 81 8 0 0 0 0 20 7f eb b 0 0", "invalid literal/length code", 1}, |
| 122 |
{"2 7e ff ff", "invalid distance code", 1}, |
| 123 |
{"c c0 81 0 0 0 0 0 90 ff 6b 4 0", "invalid distance too far back", 1}, |
| 124 |
|
| 125 |
// also trailer mismatch just in inflate() |
| 126 |
{"1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 1", "incorrect data check", -1}, |
| 127 |
{"1f 8b 8 0 0 0 0 0 0 0 3 0 0 0 0 0 0 0 0 1", "incorrect length check", -1}, |
| 128 |
{"5 c0 21 d 0 0 0 80 b0 fe 6d 2f 91 6c", "pull 17", 0}, |
| 129 |
{"5 e0 81 91 24 cb b2 2c 49 e2 f 2e 8b 9a 47 56 9f fb fe ec d2 ff 1f", "long code", 0}, |
| 130 |
{"ed c0 1 1 0 0 0 40 20 ff 57 1b 42 2c 4f", "length extra", 0}, |
| 131 |
{"ed cf c1 b1 2c 47 10 c4 30 fa 6f 35 1d 1 82 59 3d fb be 2e 2a fc f c", "long distance and extra", 0}, |
| 132 |
{"ed c0 81 0 0 0 0 80 a0 fd a9 17 a9 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 6", "window end", 0}, |
| 133 |
} |
| 134 |
|
| 135 |
func TestInflate(t *testing.T) { |
| 136 |
for _, test := range infTests { |
| 137 |
hex := strings.Split(test.hex, " ") |
| 138 |
data := make([]byte, len(hex)) |
| 139 |
for i, h := range hex { |
| 140 |
b, _ := strconv.ParseInt(h, 16, 32) |
| 141 |
data[i] = byte(b) |
| 142 |
} |
| 143 |
buf := bytes.NewReader(data) |
| 144 |
r := NewReader(buf) |
| 145 |
|
| 146 |
_, err := io.Copy(io.Discard, r) |
| 147 |
if (test.n == 0 && err == nil) || (test.n != 0 && err != nil) { |
| 148 |
t.Logf("%q: OK:", test.id) |
| 149 |
t.Logf(" - got %v", err) |
| 150 |
continue |
| 151 |
} |
| 152 |
|
| 153 |
if test.n == 0 && err != nil { |
| 154 |
t.Errorf("%q: Expected no error, but got %v", test.id, err) |
| 155 |
continue |
| 156 |
} |
| 157 |
|
| 158 |
if test.n != 0 && err == nil { |
| 159 |
t.Errorf("%q:Expected an error, but got none", test.id) |
| 160 |
continue |
| 161 |
} |
| 162 |
t.Fatal(test.n, err) |
| 163 |
} |
| 164 |
|
| 165 |
for _, test := range infOutTests { |
| 166 |
hex := strings.Split(test.hex, " ") |
| 167 |
data := make([]byte, len(hex)) |
| 168 |
for i, h := range hex { |
| 169 |
b, _ := strconv.ParseInt(h, 16, 32) |
| 170 |
data[i] = byte(b) |
| 171 |
} |
| 172 |
buf := bytes.NewReader(data) |
| 173 |
r := NewReader(buf) |
| 174 |
|
| 175 |
_, err := io.Copy(io.Discard, r) |
| 176 |
if test.err == (err != nil) { |
| 177 |
t.Logf("%q: OK:", test.id) |
| 178 |
t.Logf(" - got %v", err) |
| 179 |
continue |
| 180 |
} |
| 181 |
|
| 182 |
if test.err == false && err != nil { |
| 183 |
t.Errorf("%q: Expected no error, but got %v", test.id, err) |
| 184 |
continue |
| 185 |
} |
| 186 |
|
| 187 |
if test.err && err == nil { |
| 188 |
t.Errorf("%q: Expected an error, but got none", test.id) |
| 189 |
continue |
| 190 |
} |
| 191 |
t.Fatal(test.err, err) |
| 192 |
} |
| 193 |
|
| 194 |
} |
| 195 |
|
| 196 |
// Tests ported from zlib/test/infcover.c |
| 197 |
// Since zlib inflate is push (writer) instead of pull (reader) |
| 198 |
// some of the window size tests have been removed, since they |
| 199 |
// are irrelevant. |
| 200 |
type infOutTest struct { |
| 201 |
hex string |
| 202 |
id string |
| 203 |
step int |
| 204 |
win int |
| 205 |
length int |
| 206 |
err bool |
| 207 |
} |
| 208 |
|
| 209 |
var infOutTests = []infOutTest{ |
| 210 |
{"2 8 20 80 0 3 0", "inflate_fast TYPE return", 0, -15, 258, false}, |
| 211 |
{"63 18 5 40 c 0", "window wrap", 3, -8, 300, false}, |
| 212 |
{"e5 e0 81 ad 6d cb b2 2c c9 01 1e 59 63 ae 7d ee fb 4d fd b5 35 41 68 ff 7f 0f 0 0 0", "fast length extra bits", 0, -8, 258, true}, |
| 213 |
{"25 fd 81 b5 6d 59 b6 6a 49 ea af 35 6 34 eb 8c b9 f6 b9 1e ef 67 49 50 fe ff ff 3f 0 0", "fast distance extra bits", 0, -8, 258, true}, |
| 214 |
{"3 7e 0 0 0 0 0", "fast invalid distance code", 0, -8, 258, true}, |
| 215 |
{"1b 7 0 0 0 0 0", "fast invalid literal/length code", 0, -8, 258, true}, |
| 216 |
{"d c7 1 ae eb 38 c 4 41 a0 87 72 de df fb 1f b8 36 b1 38 5d ff ff 0", "fast 2nd level codes and too far back", 0, -8, 258, true}, |
| 217 |
{"63 18 5 8c 10 8 0 0 0 0", "very common case", 0, -8, 259, false}, |
| 218 |
{"63 60 60 18 c9 0 8 18 18 18 26 c0 28 0 29 0 0 0", "contiguous and wrap around window", 6, -8, 259, false}, |
| 219 |
{"63 0 3 0 0 0 0 0", "copy direct from output", 0, -8, 259, false}, |
| 220 |
{"1f 8b 0 0", "bad gzip method", 0, 31, 0, true}, |
| 221 |
{"1f 8b 8 80", "bad gzip flags", 0, 31, 0, true}, |
| 222 |
{"77 85", "bad zlib method", 0, 15, 0, true}, |
| 223 |
{"78 9c", "bad zlib window size", 0, 8, 0, true}, |
| 224 |
{"1f 8b 8 1e 0 0 0 0 0 0 1 0 0 0 0 0 0", "bad header crc", 0, 47, 1, true}, |
| 225 |
{"1f 8b 8 2 0 0 0 0 0 0 1d 26 3 0 0 0 0 0 0 0 0 0", "check gzip length", 0, 47, 0, true}, |
| 226 |
{"78 90", "bad zlib header check", 0, 47, 0, true}, |
| 227 |
{"8 b8 0 0 0 1", "need dictionary", 0, 8, 0, true}, |
| 228 |
{"63 18 68 30 d0 0 0", "force split window update", 4, -8, 259, false}, |
| 229 |
{"3 0", "use fixed blocks", 0, -15, 1, false}, |
| 230 |
{"", "bad window size", 0, 1, 0, true}, |
| 231 |
} |
| 232 |
|
| 233 |
func TestWriteTo(t *testing.T) { |
| 234 |
input := make([]byte, 100000) |
| 235 |
n, err := rand.Read(input) |
| 236 |
if err != nil { |
| 237 |
t.Fatal(err) |
| 238 |
} |
| 239 |
if n != len(input) { |
| 240 |
t.Fatal("did not fill buffer") |
| 241 |
} |
| 242 |
compressed := &bytes.Buffer{} |
| 243 |
w, err := NewWriter(compressed, -2) |
| 244 |
if err != nil { |
| 245 |
t.Fatal(err) |
| 246 |
} |
| 247 |
n, err = w.Write(input) |
| 248 |
if err != nil { |
| 249 |
t.Fatal(err) |
| 250 |
} |
| 251 |
if n != len(input) { |
| 252 |
t.Fatal("did not fill buffer") |
| 253 |
} |
| 254 |
w.Close() |
| 255 |
buf := compressed.Bytes() |
| 256 |
|
| 257 |
dec := NewReader(bytes.NewBuffer(buf)) |
| 258 |
// ReadAll does not use WriteTo, but we wrap it in a NopCloser to be sure. |
| 259 |
readall, err := io.ReadAll(io.NopCloser(dec)) |
| 260 |
if err != nil { |
| 261 |
t.Fatal(err) |
| 262 |
} |
| 263 |
if len(readall) != len(input) { |
| 264 |
t.Fatal("did not decompress everything") |
| 265 |
} |
| 266 |
|
| 267 |
dec = NewReader(bytes.NewBuffer(buf)) |
| 268 |
wtbuf := &bytes.Buffer{} |
| 269 |
written, err := dec.(io.WriterTo).WriteTo(wtbuf) |
| 270 |
if err != nil { |
| 271 |
t.Fatal(err) |
| 272 |
} |
| 273 |
if written != int64(len(input)) { |
| 274 |
t.Error("Returned length did not match, expected", len(input), "got", written) |
| 275 |
} |
| 276 |
if wtbuf.Len() != len(input) { |
| 277 |
t.Error("Actual Length did not match, expected", len(input), "got", wtbuf.Len()) |
| 278 |
} |
| 279 |
if !bytes.Equal(wtbuf.Bytes(), input) { |
| 280 |
t.Fatal("output did not match input") |
| 281 |
} |
| 282 |
} |
| 283 |
|
| 284 |
func TestInflateCheckpointResume(t *testing.T) { |
| 285 |
// Compress a moderately large, varied input so the deflate writer emits |
| 286 |
// multiple blocks. Most boundaries land on non-byte-aligned bit positions, |
| 287 |
// which exercises BitOffset on resume. |
| 288 |
raw := make([]byte, 0, 256<<10) |
| 289 |
for range 4000 { |
| 290 |
raw = append(raw, "the quick brown fox jumps over the lazy dog "...) |
| 291 |
raw = append(raw, "Lorem ipsum dolor sit amet, consectetur "...) |
| 292 |
} |
| 293 |
|
| 294 |
var compressed bytes.Buffer |
| 295 |
w, err := NewWriter(&compressed, DefaultCompression) |
| 296 |
if err != nil { |
| 297 |
t.Fatal(err) |
| 298 |
} |
| 299 |
if _, err := w.Write(raw); err != nil { |
| 300 |
t.Fatal(err) |
| 301 |
} |
| 302 |
if err := w.Close(); err != nil { |
| 303 |
t.Fatal(err) |
| 304 |
} |
| 305 |
|
| 306 |
type cpRec struct { |
| 307 |
cp InflateCheckpoint |
| 308 |
window []byte |
| 309 |
} |
| 310 |
var cps []cpRec |
| 311 |
var seenFinal bool |
| 312 |
cb := func(cp InflateCheckpoint) { |
| 313 |
if cp.Final { |
| 314 |
if seenFinal { |
| 315 |
t.Fatalf("final callback called twice") |
| 316 |
} |
| 317 |
seenFinal = true |
| 318 |
return |
| 319 |
} |
| 320 |
// Window is reused between callbacks, copy it. |
| 321 |
cps = append(cps, cpRec{cp: cp, window: append([]byte(nil), cp.Window...)}) |
| 322 |
} |
| 323 |
|
| 324 |
r := NewReaderOpts(bytes.NewReader(compressed.Bytes()), WithEobCallback(cb)) |
| 325 |
decoded, err := io.ReadAll(r) |
| 326 |
if err != nil { |
| 327 |
t.Fatalf("decode: %v", err) |
| 328 |
} |
| 329 |
if !bytes.Equal(decoded, raw) { |
| 330 |
t.Fatalf("decoded mismatch: %d vs %d", len(decoded), len(raw)) |
| 331 |
} |
| 332 |
if len(cps) < 2 { |
| 333 |
t.Fatalf("expected multiple non-final block callbacks, got %d", len(cps)) |
| 334 |
} |
| 335 |
t.Logf("got %d block(s)", len(cps)) |
| 336 |
|
| 337 |
// Offsets must be monotonically non-decreasing. |
| 338 |
for i := 1; i < len(cps); i++ { |
| 339 |
prev, cur := cps[i-1].cp, cps[i].cp |
| 340 |
if cur.UncompressedOffset < prev.UncompressedOffset { |
| 341 |
t.Errorf("UncompressedOffset regressed at %d: %d -> %d", i, prev.UncompressedOffset, cur.UncompressedOffset) |
| 342 |
} |
| 343 |
pbits := prev.CompressedOffset*8 + int64(prev.BitOffset) |
| 344 |
cbits := cur.CompressedOffset*8 + int64(cur.BitOffset) |
| 345 |
if cbits < pbits { |
| 346 |
t.Errorf("CompressedOffset regressed at %d: %d -> %d", i, pbits, cbits) |
| 347 |
} |
| 348 |
} |
| 349 |
|
| 350 |
// Resume from every checkpoint (the final block no longer triggers cb). |
| 351 |
sawBitOffset := false |
| 352 |
for i, rec := range cps { |
| 353 |
cp := rec.cp |
| 354 |
cp.Window = rec.window |
| 355 |
if cp.BitOffset != 0 { |
| 356 |
sawBitOffset = true |
| 357 |
} |
| 358 |
src := compressed.Bytes()[cp.CompressedOffset:] |
| 359 |
rr := NewReaderOpts(bytes.NewReader(src), WithResumeFrom(cp)) |
| 360 |
got, err := io.ReadAll(rr) |
| 361 |
if err != nil { |
| 362 |
t.Errorf("resume %d (CompOff=%d BitOff=%d): %v", i, cp.CompressedOffset, cp.BitOffset, err) |
| 363 |
continue |
| 364 |
} |
| 365 |
want := raw[cp.UncompressedOffset:] |
| 366 |
if !bytes.Equal(got, want) { |
| 367 |
t.Errorf("resume %d: output mismatch (%d vs %d bytes)", i, len(got), len(want)) |
| 368 |
} |
| 369 |
} |
| 370 |
if !sawBitOffset { |
| 371 |
t.Errorf("no non-zero BitOffset checkpoints — BitOffset resume path not exercised") |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
func TestInflateCheckpointResetCP(t *testing.T) { |
| 376 |
// Verify ResetCP applies a checkpoint to an existing reader. |
| 377 |
var raw []byte |
| 378 |
for range 4000 { |
| 379 |
raw = append(raw, "the quick brown fox jumps over the lazy dog "...) |
| 380 |
raw = append(raw, "Lorem ipsum dolor sit amet, consectetur "...) |
| 381 |
} |
| 382 |
|
| 383 |
var compressed bytes.Buffer |
| 384 |
w, err := NewWriter(&compressed, DefaultCompression) |
| 385 |
if err != nil { |
| 386 |
t.Fatal(err) |
| 387 |
} |
| 388 |
w.Write(raw) |
| 389 |
w.Close() |
| 390 |
|
| 391 |
var cps []InflateCheckpoint |
| 392 |
var seenFinal bool |
| 393 |
cb := func(cp InflateCheckpoint) { |
| 394 |
if cp.Final { |
| 395 |
if seenFinal { |
| 396 |
t.Fatalf("final callback called twice") |
| 397 |
} |
| 398 |
seenFinal = true |
| 399 |
return |
| 400 |
} |
| 401 |
cp.Window = append([]byte(nil), cp.Window...) |
| 402 |
cps = append(cps, cp) |
| 403 |
} |
| 404 |
r := NewReaderOpts(bytes.NewReader(compressed.Bytes()), WithEobCallback(cb)) |
| 405 |
if _, err := io.ReadAll(r); err != nil { |
| 406 |
t.Fatal(err) |
| 407 |
} |
| 408 |
if len(cps) < 2 { |
| 409 |
t.Skipf("only %d block(s); need at least 2 for ResetCP test", len(cps)) |
| 410 |
} |
| 411 |
t.Logf("ResetCP test with %d block(s)", len(cps)) |
| 412 |
|
| 413 |
cp := cps[len(cps)-1] |
| 414 |
dec, ok := NewReader(nil).(interface { |
| 415 |
ResetCP(io.Reader, InflateCheckpoint) error |
| 416 |
}) |
| 417 |
if !ok { |
| 418 |
t.Fatal("reader does not implement ResetCP") |
| 419 |
} |
| 420 |
if err := dec.ResetCP(bytes.NewReader(compressed.Bytes()[cp.CompressedOffset:]), cp); err != nil { |
| 421 |
t.Fatalf("ResetCP: %v", err) |
| 422 |
} |
| 423 |
got, err := io.ReadAll(dec.(io.Reader)) |
| 424 |
if err != nil { |
| 425 |
t.Fatalf("read after ResetCP: %v", err) |
| 426 |
} |
| 427 |
if want := raw[cp.UncompressedOffset:]; !bytes.Equal(got, want) { |
| 428 |
t.Fatalf("ResetCP output mismatch (%d vs %d bytes)", len(got), len(want)) |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
func TestReaderPartialBlock(t *testing.T) { |
| 433 |
data, err := os.ReadFile("testdata/partial-block") |
| 434 |
if err != nil { |
| 435 |
t.Error(err) |
| 436 |
} |
| 437 |
|
| 438 |
r := NewReaderOpts(bytes.NewReader(data), WithPartialBlock()) |
| 439 |
rb := make([]byte, 32) |
| 440 |
n, err := r.Read(rb) |
| 441 |
if err != nil { |
| 442 |
t.Fatalf("Read: %v", err) |
| 443 |
} |
| 444 |
|
| 445 |
expected := "hello, world" |
| 446 |
actual := string(rb[:n]) |
| 447 |
if expected != actual { |
| 448 |
t.Fatalf("expected: %v, got: %v", expected, actual) |
| 449 |
} |
| 450 |
} |