| 1 |
package zstd |
| 2 |
|
| 3 |
import ( |
| 4 |
"bytes" |
| 5 |
"fmt" |
| 6 |
"io" |
| 7 |
"math/rand" |
| 8 |
"os" |
| 9 |
"strings" |
| 10 |
"sync" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/klauspost/compress/zip" |
| 14 |
) |
| 15 |
|
| 16 |
func TestDecoder_SmallDict(t *testing.T) { |
| 17 |
// All files have CRC |
| 18 |
zr := testCreateZipReader("testdata/dict-tests-small.zip", t) |
| 19 |
dicts := readDicts(t, zr) |
| 20 |
dec, err := NewReader(nil, WithDecoderConcurrency(1), WithDecoderDicts(dicts...)) |
| 21 |
if err != nil { |
| 22 |
t.Fatal(err) |
| 23 |
return |
| 24 |
} |
| 25 |
defer dec.Close() |
| 26 |
for _, tt := range zr.File { |
| 27 |
if !strings.HasSuffix(tt.Name, ".zst") { |
| 28 |
continue |
| 29 |
} |
| 30 |
t.Run("decodeall-"+tt.Name, func(t *testing.T) { |
| 31 |
r, err := tt.Open() |
| 32 |
if err != nil { |
| 33 |
t.Fatal(err) |
| 34 |
} |
| 35 |
defer r.Close() |
| 36 |
in, err := io.ReadAll(r) |
| 37 |
if err != nil { |
| 38 |
t.Fatal(err) |
| 39 |
} |
| 40 |
got, err := dec.DecodeAll(in, nil) |
| 41 |
if err != nil { |
| 42 |
t.Fatal(err) |
| 43 |
} |
| 44 |
_, err = dec.DecodeAll(in, got[:0]) |
| 45 |
if err != nil { |
| 46 |
t.Fatal(err) |
| 47 |
} |
| 48 |
}) |
| 49 |
} |
| 50 |
} |
| 51 |
|
| 52 |
func buildDictLevelPathFixture() (samples [][]byte, history, src []byte) { |
| 53 |
history = bytes.Repeat([]byte("build dict common phrase alpha beta gamma delta "), 16) |
| 54 |
sample := func(suffix string) []byte { |
| 55 |
b := append([]byte(nil), history...) |
| 56 |
return append(b, suffix...) |
| 57 |
} |
| 58 |
samples = [][]byte{ |
| 59 |
sample("city=honolulu [email protected] status=active"), |
| 60 |
sample("city=seattle [email protected] status=active"), |
| 61 |
sample("city=london [email protected] status=paused"), |
| 62 |
sample("city=honolulu [email protected] status=active"), |
| 63 |
} |
| 64 |
src = sample("city=honolulu [email protected] status=active") |
| 65 |
return samples, history, src |
| 66 |
} |
| 67 |
|
| 68 |
func TestBuildDictLevelPathsRoundTrip(t *testing.T) { |
| 69 |
samples, history, src := buildDictLevelPathFixture() |
| 70 |
for _, tt := range []struct { |
| 71 |
name string |
| 72 |
level EncoderLevel |
| 73 |
}{ |
| 74 |
{name: "default"}, |
| 75 |
{name: "explicit", level: SpeedFastest}, |
| 76 |
} { |
| 77 |
t.Run(tt.name, func(t *testing.T) { |
| 78 |
dict, err := BuildDict(BuildDictOptions{ |
| 79 |
ID: 1, |
| 80 |
Contents: samples, |
| 81 |
History: history, |
| 82 |
Offsets: [3]int{1, 4, 8}, |
| 83 |
Level: tt.level, |
| 84 |
}) |
| 85 |
if err != nil { |
| 86 |
t.Fatalf("BuildDict failed: %v", err) |
| 87 |
} |
| 88 |
|
| 89 |
enc, err := NewWriter(nil, WithEncoderDict(dict), WithEncoderLevel(SpeedFastest), WithEncoderConcurrency(1)) |
| 90 |
if err != nil { |
| 91 |
t.Fatalf("NewWriter failed: %v", err) |
| 92 |
} |
| 93 |
defer enc.Close() |
| 94 |
encoded := enc.EncodeAll(src, nil) |
| 95 |
|
| 96 |
dec, err := NewReader(nil, WithDecoderDicts(dict), WithDecoderConcurrency(1)) |
| 97 |
if err != nil { |
| 98 |
t.Fatalf("NewReader failed: %v", err) |
| 99 |
} |
| 100 |
defer dec.Close() |
| 101 |
decoded, err := dec.DecodeAll(encoded, nil) |
| 102 |
if err != nil { |
| 103 |
t.Fatalf("DecodeAll failed: %v", err) |
| 104 |
} |
| 105 |
if !bytes.Equal(decoded, src) { |
| 106 |
t.Fatalf("decoded output mismatch: got %q want %q", decoded, src) |
| 107 |
} |
| 108 |
}) |
| 109 |
} |
| 110 |
} |
| 111 |
|
| 112 |
// Regression for https://github.com/klauspost/compress/issues/1179: |
| 113 |
// a homogeneous corpus with default (zero) Offsets used to emit a dictionary |
| 114 |
// whose recent-offsets table contained 0, which WithEncoderDict rejects. |
| 115 |
func TestBuildDictHomogeneousCorpusValidOffsets(t *testing.T) { |
| 116 |
sample := func(seed int64) []byte { |
| 117 |
const boiler = `<div class="card-grid" data-template="responsive"><style>.card-grid{display:grid;gap:6px;background:#0a0a0a;padding:4px}.card-item{color:#333;display:grid}</style><script>(function(){var n=document.querySelectorAll('[data-src]');for(var i=0;i<n.length;i++){var u=n[i].getAttribute('data-src');}})();</script>` |
| 118 |
rng := rand.New(rand.NewSource(seed)) |
| 119 |
var out strings.Builder |
| 120 |
out.WriteString(boiler) |
| 121 |
for i := range 8 { |
| 122 |
p := make([]byte, 32) |
| 123 |
rng.Read(p) |
| 124 |
out.WriteString(fmt.Sprintf(`{"url":"https://example.com/t?id=%d&p=%x"}`, i, p)) |
| 125 |
} |
| 126 |
return []byte(out.String()) |
| 127 |
} |
| 128 |
const sampleCount, historyLimit = 16, 8 << 10 |
| 129 |
samples := make([][]byte, sampleCount) |
| 130 |
for i := range samples { |
| 131 |
samples[i] = sample(12000 + int64(i)) |
| 132 |
} |
| 133 |
var hist []byte |
| 134 |
for _, s := range samples { |
| 135 |
if len(hist)+len(s) > historyLimit { |
| 136 |
break |
| 137 |
} |
| 138 |
hist = append(hist, s...) |
| 139 |
} |
| 140 |
|
| 141 |
dict, err := BuildDict(BuildDictOptions{ |
| 142 |
ID: 1, |
| 143 |
Contents: samples, |
| 144 |
History: hist, |
| 145 |
Level: SpeedBetterCompression, |
| 146 |
}) |
| 147 |
if err != nil { |
| 148 |
t.Fatalf("BuildDict failed: %v", err) |
| 149 |
} |
| 150 |
enc, err := NewWriter(nil, WithEncoderDict(dict)) |
| 151 |
if err != nil { |
| 152 |
t.Fatalf("WithEncoderDict rejected dictionary: %v", err) |
| 153 |
} |
| 154 |
enc.Close() |
| 155 |
info, err := InspectDictionary(dict) |
| 156 |
if err != nil { |
| 157 |
t.Fatalf("InspectDictionary: %v", err) |
| 158 |
} |
| 159 |
for i, off := range info.Offsets() { |
| 160 |
if off <= 0 { |
| 161 |
t.Fatalf("offset[%d]=%d; want positive", i, off) |
| 162 |
} |
| 163 |
if off > info.ContentSize() { |
| 164 |
t.Fatalf("offset[%d]=%d > content size %d", i, off, info.ContentSize()) |
| 165 |
} |
| 166 |
} |
| 167 |
} |
| 168 |
|
| 169 |
func TestBuildDictFullyMatchableCorpusReturnsError(t *testing.T) { |
| 170 |
block := bytes.Repeat([]byte("ABCDABCDABCDABCD"), 64) |
| 171 |
var contents [][]byte |
| 172 |
var history []byte |
| 173 |
for range 32 { |
| 174 |
contents = append(contents, block) |
| 175 |
history = append(history, block...) |
| 176 |
} |
| 177 |
|
| 178 |
defer func() { |
| 179 |
if r := recover(); r != nil { |
| 180 |
t.Fatalf("BuildDict panicked for fully matchable corpus: %v", r) |
| 181 |
} |
| 182 |
}() |
| 183 |
_, err := BuildDict(BuildDictOptions{ |
| 184 |
ID: 1, |
| 185 |
Contents: contents, |
| 186 |
History: history, |
| 187 |
Level: SpeedDefault, |
| 188 |
}) |
| 189 |
if err == nil { |
| 190 |
t.Fatal("BuildDict succeeded for corpus with no literals; want error") |
| 191 |
} |
| 192 |
if !strings.Contains(err.Error(), "0 literals") { |
| 193 |
t.Fatalf("BuildDict error = %v, want mention 0 literals", err) |
| 194 |
} |
| 195 |
} |
| 196 |
|
| 197 |
func BenchmarkBuildDictLevelPaths(b *testing.B) { |
| 198 |
samples, history, _ := buildDictLevelPathFixture() |
| 199 |
for _, tt := range []struct { |
| 200 |
name string |
| 201 |
level EncoderLevel |
| 202 |
}{ |
| 203 |
{name: "default"}, |
| 204 |
{name: "explicit-fastest", level: SpeedFastest}, |
| 205 |
} { |
| 206 |
b.Run(tt.name, func(b *testing.B) { |
| 207 |
_, err := BuildDict(BuildDictOptions{ |
| 208 |
ID: 1, |
| 209 |
Contents: samples, |
| 210 |
History: history, |
| 211 |
Offsets: [3]int{1, 4, 8}, |
| 212 |
Level: tt.level, |
| 213 |
}) |
| 214 |
if err != nil { |
| 215 |
b.Fatal(err) |
| 216 |
} |
| 217 |
b.ReportAllocs() |
| 218 |
b.ResetTimer() |
| 219 |
for i := 0; i < b.N; i++ { |
| 220 |
_, err := BuildDict(BuildDictOptions{ |
| 221 |
ID: 1, |
| 222 |
Contents: samples, |
| 223 |
History: history, |
| 224 |
Offsets: [3]int{1, 4, 8}, |
| 225 |
Level: tt.level, |
| 226 |
}) |
| 227 |
if err != nil { |
| 228 |
b.Fatal(err) |
| 229 |
} |
| 230 |
} |
| 231 |
}) |
| 232 |
} |
| 233 |
} |
| 234 |
|
| 235 |
func TestEncoder_SmallDict(t *testing.T) { |
| 236 |
// All files have CRC |
| 237 |
zr := testCreateZipReader("testdata/dict-tests-small.zip", t) |
| 238 |
var dicts [][]byte |
| 239 |
var encs []*Encoder |
| 240 |
var noDictEncs []*Encoder |
| 241 |
var encNames []string |
| 242 |
|
| 243 |
for _, tt := range zr.File { |
| 244 |
if !strings.HasSuffix(tt.Name, ".dict") { |
| 245 |
continue |
| 246 |
} |
| 247 |
func() { |
| 248 |
r, err := tt.Open() |
| 249 |
if err != nil { |
| 250 |
t.Fatal(err) |
| 251 |
} |
| 252 |
defer r.Close() |
| 253 |
in, err := io.ReadAll(r) |
| 254 |
if err != nil { |
| 255 |
t.Fatal(err) |
| 256 |
} |
| 257 |
dicts = append(dicts, in) |
| 258 |
for level := SpeedFastest; level < speedLast; level++ { |
| 259 |
if isRaceTest && level >= SpeedBestCompression { |
| 260 |
break |
| 261 |
} |
| 262 |
enc, err := NewWriter(nil, WithEncoderConcurrency(1), WithEncoderDict(in), WithEncoderLevel(level), WithWindowSize(1<<17)) |
| 263 |
if err != nil { |
| 264 |
t.Fatal(err) |
| 265 |
} |
| 266 |
encs = append(encs, enc) |
| 267 |
encNames = append(encNames, fmt.Sprint("level-", level.String(), "-dict-", len(dicts))) |
| 268 |
|
| 269 |
enc, err = NewWriter(nil, WithEncoderConcurrency(1), WithEncoderLevel(level), WithWindowSize(1<<17)) |
| 270 |
if err != nil { |
| 271 |
t.Fatal(err) |
| 272 |
} |
| 273 |
noDictEncs = append(noDictEncs, enc) |
| 274 |
} |
| 275 |
}() |
| 276 |
} |
| 277 |
dec, err := NewReader(nil, WithDecoderConcurrency(1), WithDecoderDicts(dicts...)) |
| 278 |
if err != nil { |
| 279 |
t.Fatal(err) |
| 280 |
return |
| 281 |
} |
| 282 |
defer dec.Close() |
| 283 |
for i, tt := range zr.File { |
| 284 |
if testing.Short() && i > 100 { |
| 285 |
break |
| 286 |
} |
| 287 |
if !strings.HasSuffix(tt.Name, ".zst") { |
| 288 |
continue |
| 289 |
} |
| 290 |
r, err := tt.Open() |
| 291 |
if err != nil { |
| 292 |
t.Fatal(err) |
| 293 |
} |
| 294 |
defer r.Close() |
| 295 |
in, err := io.ReadAll(r) |
| 296 |
if err != nil { |
| 297 |
t.Fatal(err) |
| 298 |
} |
| 299 |
decoded, err := dec.DecodeAll(in, nil) |
| 300 |
if err != nil { |
| 301 |
t.Fatal(err) |
| 302 |
} |
| 303 |
if testing.Short() && len(decoded) > 1000 { |
| 304 |
continue |
| 305 |
} |
| 306 |
|
| 307 |
t.Run("encodeall-"+tt.Name, func(t *testing.T) { |
| 308 |
// Attempt to compress with all dicts |
| 309 |
var b []byte |
| 310 |
var tmp []byte |
| 311 |
for i := range encs { |
| 312 |
t.Run(encNames[i], func(t *testing.T) { |
| 313 |
b = encs[i].EncodeAll(decoded, b[:0]) |
| 314 |
tmp, err = dec.DecodeAll(in, tmp[:0]) |
| 315 |
if err != nil { |
| 316 |
t.Fatal(err) |
| 317 |
} |
| 318 |
if !bytes.Equal(tmp, decoded) { |
| 319 |
t.Fatal("output mismatch") |
| 320 |
} |
| 321 |
|
| 322 |
tmp = noDictEncs[i].EncodeAll(decoded, tmp[:0]) |
| 323 |
|
| 324 |
if strings.Contains(t.Name(), "dictplain") && strings.Contains(t.Name(), "dict-1") { |
| 325 |
t.Log("reference:", len(in), "no dict:", len(tmp), "with dict:", len(b), "SAVED:", len(tmp)-len(b)) |
| 326 |
// Check that we reduced this significantly |
| 327 |
if len(b) > 250 { |
| 328 |
t.Error("output was bigger than expected") |
| 329 |
} |
| 330 |
} |
| 331 |
}) |
| 332 |
} |
| 333 |
}) |
| 334 |
t.Run("stream-"+tt.Name, func(t *testing.T) { |
| 335 |
// Attempt to compress with all dicts |
| 336 |
var tmp []byte |
| 337 |
for i := range encs { |
| 338 |
enc := encs[i] |
| 339 |
t.Run(encNames[i], func(t *testing.T) { |
| 340 |
var buf bytes.Buffer |
| 341 |
enc.ResetContentSize(&buf, int64(len(decoded))) |
| 342 |
_, err := enc.Write(decoded) |
| 343 |
if err != nil { |
| 344 |
t.Fatal(err) |
| 345 |
} |
| 346 |
err = enc.Close() |
| 347 |
if err != nil { |
| 348 |
t.Fatal(err) |
| 349 |
} |
| 350 |
tmp, err = dec.DecodeAll(buf.Bytes(), tmp[:0]) |
| 351 |
if err != nil { |
| 352 |
t.Fatal(err) |
| 353 |
} |
| 354 |
if !bytes.Equal(tmp, decoded) { |
| 355 |
t.Fatal("output mismatch") |
| 356 |
} |
| 357 |
var buf2 bytes.Buffer |
| 358 |
noDictEncs[i].Reset(&buf2) |
| 359 |
noDictEncs[i].Write(decoded) |
| 360 |
noDictEncs[i].Close() |
| 361 |
|
| 362 |
if strings.Contains(t.Name(), "dictplain") && strings.Contains(t.Name(), "dict-1") { |
| 363 |
t.Log("reference:", len(in), "no dict:", buf2.Len(), "with dict:", buf.Len(), "SAVED:", buf2.Len()-buf.Len()) |
| 364 |
// Check that we reduced this significantly |
| 365 |
if buf.Len() > 250 { |
| 366 |
t.Error("output was bigger than expected") |
| 367 |
} |
| 368 |
} |
| 369 |
}) |
| 370 |
} |
| 371 |
}) |
| 372 |
} |
| 373 |
} |
| 374 |
|
| 375 |
func TestEncoder_SmallDictFresh(t *testing.T) { |
| 376 |
// All files have CRC |
| 377 |
zr := testCreateZipReader("testdata/dict-tests-small.zip", t) |
| 378 |
var dicts [][]byte |
| 379 |
var encs []func() *Encoder |
| 380 |
var noDictEncs []*Encoder |
| 381 |
var encNames []string |
| 382 |
|
| 383 |
for _, tt := range zr.File { |
| 384 |
if !strings.HasSuffix(tt.Name, ".dict") { |
| 385 |
continue |
| 386 |
} |
| 387 |
func() { |
| 388 |
r, err := tt.Open() |
| 389 |
if err != nil { |
| 390 |
t.Fatal(err) |
| 391 |
} |
| 392 |
defer r.Close() |
| 393 |
in, err := io.ReadAll(r) |
| 394 |
if err != nil { |
| 395 |
t.Fatal(err) |
| 396 |
} |
| 397 |
dicts = append(dicts, in) |
| 398 |
for level := SpeedFastest; level < speedLast; level++ { |
| 399 |
if isRaceTest && level >= SpeedBestCompression { |
| 400 |
break |
| 401 |
} |
| 402 |
level := level |
| 403 |
encs = append(encs, func() *Encoder { |
| 404 |
enc, err := NewWriter(nil, WithEncoderConcurrency(1), WithEncoderDict(in), WithEncoderLevel(level), WithWindowSize(1<<17)) |
| 405 |
if err != nil { |
| 406 |
t.Fatal(err) |
| 407 |
} |
| 408 |
return enc |
| 409 |
}) |
| 410 |
encNames = append(encNames, fmt.Sprint("level-", level.String(), "-dict-", len(dicts))) |
| 411 |
|
| 412 |
enc, err := NewWriter(nil, WithEncoderConcurrency(1), WithEncoderLevel(level), WithWindowSize(1<<17)) |
| 413 |
if err != nil { |
| 414 |
t.Fatal(err) |
| 415 |
} |
| 416 |
noDictEncs = append(noDictEncs, enc) |
| 417 |
} |
| 418 |
}() |
| 419 |
} |
| 420 |
dec, err := NewReader(nil, WithDecoderConcurrency(1), WithDecoderDicts(dicts...)) |
| 421 |
if err != nil { |
| 422 |
t.Fatal(err) |
| 423 |
return |
| 424 |
} |
| 425 |
defer dec.Close() |
| 426 |
for i, tt := range zr.File { |
| 427 |
if testing.Short() && i > 100 { |
| 428 |
break |
| 429 |
} |
| 430 |
if !strings.HasSuffix(tt.Name, ".zst") { |
| 431 |
continue |
| 432 |
} |
| 433 |
r, err := tt.Open() |
| 434 |
if err != nil { |
| 435 |
t.Fatal(err) |
| 436 |
} |
| 437 |
defer r.Close() |
| 438 |
in, err := io.ReadAll(r) |
| 439 |
if err != nil { |
| 440 |
t.Fatal(err) |
| 441 |
} |
| 442 |
decoded, err := dec.DecodeAll(in, nil) |
| 443 |
if err != nil { |
| 444 |
t.Fatal(err) |
| 445 |
} |
| 446 |
if testing.Short() && len(decoded) > 1000 { |
| 447 |
continue |
| 448 |
} |
| 449 |
|
| 450 |
t.Run("encodeall-"+tt.Name, func(t *testing.T) { |
| 451 |
// Attempt to compress with all dicts |
| 452 |
var b []byte |
| 453 |
var tmp []byte |
| 454 |
for i := range encs { |
| 455 |
t.Run(encNames[i], func(t *testing.T) { |
| 456 |
enc := encs[i]() |
| 457 |
defer enc.Close() |
| 458 |
b = enc.EncodeAll(decoded, b[:0]) |
| 459 |
tmp, err = dec.DecodeAll(in, tmp[:0]) |
| 460 |
if err != nil { |
| 461 |
t.Fatal(err) |
| 462 |
} |
| 463 |
if !bytes.Equal(tmp, decoded) { |
| 464 |
t.Fatal("output mismatch") |
| 465 |
} |
| 466 |
|
| 467 |
tmp = noDictEncs[i].EncodeAll(decoded, tmp[:0]) |
| 468 |
|
| 469 |
if strings.Contains(t.Name(), "dictplain") && strings.Contains(t.Name(), "dict-1") { |
| 470 |
t.Log("reference:", len(in), "no dict:", len(tmp), "with dict:", len(b), "SAVED:", len(tmp)-len(b)) |
| 471 |
// Check that we reduced this significantly |
| 472 |
if len(b) > 250 { |
| 473 |
t.Error("output was bigger than expected") |
| 474 |
} |
| 475 |
} |
| 476 |
}) |
| 477 |
} |
| 478 |
}) |
| 479 |
t.Run("stream-"+tt.Name, func(t *testing.T) { |
| 480 |
// Attempt to compress with all dicts |
| 481 |
var tmp []byte |
| 482 |
for i := range encs { |
| 483 |
t.Run(encNames[i], func(t *testing.T) { |
| 484 |
enc := encs[i]() |
| 485 |
defer enc.Close() |
| 486 |
var buf bytes.Buffer |
| 487 |
enc.ResetContentSize(&buf, int64(len(decoded))) |
| 488 |
_, err := enc.Write(decoded) |
| 489 |
if err != nil { |
| 490 |
t.Fatal(err) |
| 491 |
} |
| 492 |
err = enc.Close() |
| 493 |
if err != nil { |
| 494 |
t.Fatal(err) |
| 495 |
} |
| 496 |
tmp, err = dec.DecodeAll(buf.Bytes(), tmp[:0]) |
| 497 |
if err != nil { |
| 498 |
t.Fatal(err) |
| 499 |
} |
| 500 |
if !bytes.Equal(tmp, decoded) { |
| 501 |
t.Fatal("output mismatch") |
| 502 |
} |
| 503 |
var buf2 bytes.Buffer |
| 504 |
noDictEncs[i].Reset(&buf2) |
| 505 |
noDictEncs[i].Write(decoded) |
| 506 |
noDictEncs[i].Close() |
| 507 |
|
| 508 |
if strings.Contains(t.Name(), "dictplain") && strings.Contains(t.Name(), "dict-1") { |
| 509 |
t.Log("reference:", len(in), "no dict:", buf2.Len(), "with dict:", buf.Len(), "SAVED:", buf2.Len()-buf.Len()) |
| 510 |
// Check that we reduced this significantly |
| 511 |
if buf.Len() > 250 { |
| 512 |
t.Error("output was bigger than expected") |
| 513 |
} |
| 514 |
} |
| 515 |
}) |
| 516 |
} |
| 517 |
}) |
| 518 |
} |
| 519 |
} |
| 520 |
|
| 521 |
func benchmarkEncodeAllLimitedBySize(b *testing.B, lowerLimit int, upperLimit int) { |
| 522 |
zr := testCreateZipReader("testdata/dict-tests-small.zip", b) |
| 523 |
t := testing.TB(b) |
| 524 |
|
| 525 |
var dicts [][]byte |
| 526 |
var encs []*Encoder |
| 527 |
var encNames []string |
| 528 |
|
| 529 |
for _, tt := range zr.File { |
| 530 |
if !strings.HasSuffix(tt.Name, ".dict") { |
| 531 |
continue |
| 532 |
} |
| 533 |
func() { |
| 534 |
r, err := tt.Open() |
| 535 |
if err != nil { |
| 536 |
t.Fatal(err) |
| 537 |
} |
| 538 |
defer r.Close() |
| 539 |
in, err := io.ReadAll(r) |
| 540 |
if err != nil { |
| 541 |
t.Fatal(err) |
| 542 |
} |
| 543 |
dicts = append(dicts, in) |
| 544 |
for level := SpeedFastest; level < speedLast; level++ { |
| 545 |
enc, err := NewWriter(nil, WithEncoderDict(in), WithEncoderLevel(level)) |
| 546 |
if err != nil { |
| 547 |
t.Fatal(err) |
| 548 |
} |
| 549 |
encs = append(encs, enc) |
| 550 |
encNames = append(encNames, fmt.Sprint("level-", level.String(), "-dict-", len(dicts))) |
| 551 |
} |
| 552 |
}() |
| 553 |
} |
| 554 |
const nPer = int(speedLast - SpeedFastest) |
| 555 |
dec, err := NewReader(nil, WithDecoderConcurrency(1), WithDecoderDicts(dicts...)) |
| 556 |
if err != nil { |
| 557 |
t.Fatal(err) |
| 558 |
return |
| 559 |
} |
| 560 |
defer dec.Close() |
| 561 |
|
| 562 |
tested := make(map[int]struct{}) |
| 563 |
for j, tt := range zr.File { |
| 564 |
if !strings.HasSuffix(tt.Name, ".zst") { |
| 565 |
continue |
| 566 |
} |
| 567 |
r, err := tt.Open() |
| 568 |
if err != nil { |
| 569 |
t.Fatal(err) |
| 570 |
} |
| 571 |
defer r.Close() |
| 572 |
in, err := io.ReadAll(r) |
| 573 |
if err != nil { |
| 574 |
t.Fatal(err) |
| 575 |
} |
| 576 |
decoded, err := dec.DecodeAll(in, nil) |
| 577 |
if err != nil { |
| 578 |
t.Fatal(err) |
| 579 |
} |
| 580 |
|
| 581 |
// Only test each size once |
| 582 |
if _, ok := tested[len(decoded)]; ok { |
| 583 |
continue |
| 584 |
} |
| 585 |
tested[len(decoded)] = struct{}{} |
| 586 |
|
| 587 |
if len(decoded) < lowerLimit { |
| 588 |
continue |
| 589 |
} |
| 590 |
|
| 591 |
if upperLimit > 0 && len(decoded) > upperLimit { |
| 592 |
continue |
| 593 |
} |
| 594 |
|
| 595 |
for i := range encs { |
| 596 |
// Only do 1 dict (4 encoders) for now. |
| 597 |
if i == nPer-1 { |
| 598 |
break |
| 599 |
} |
| 600 |
// Attempt to compress with all dicts |
| 601 |
encIdx := (i + j*nPer) % len(encs) |
| 602 |
enc := encs[encIdx] |
| 603 |
b.Run(fmt.Sprintf("length-%d-%s", len(decoded), encNames[encIdx]), func(b *testing.B) { |
| 604 |
b.RunParallel(func(pb *testing.PB) { |
| 605 |
dst := make([]byte, 0, len(decoded)+10) |
| 606 |
b.SetBytes(int64(len(decoded))) |
| 607 |
b.ResetTimer() |
| 608 |
b.ReportAllocs() |
| 609 |
for pb.Next() { |
| 610 |
dst = enc.EncodeAll(decoded, dst[:0]) |
| 611 |
} |
| 612 |
}) |
| 613 |
}) |
| 614 |
} |
| 615 |
} |
| 616 |
} |
| 617 |
|
| 618 |
func BenchmarkEncodeAllDict0_1024(b *testing.B) { |
| 619 |
benchmarkEncodeAllLimitedBySize(b, 0, 1024) |
| 620 |
} |
| 621 |
|
| 622 |
func BenchmarkEncodeAllDict1024_8192(b *testing.B) { |
| 623 |
benchmarkEncodeAllLimitedBySize(b, 1024, 8192) |
| 624 |
} |
| 625 |
|
| 626 |
func BenchmarkEncodeAllDict8192_16384(b *testing.B) { |
| 627 |
benchmarkEncodeAllLimitedBySize(b, 8192, 16384) |
| 628 |
} |
| 629 |
|
| 630 |
func BenchmarkEncodeAllDict16384_65536(b *testing.B) { |
| 631 |
benchmarkEncodeAllLimitedBySize(b, 16384, 65536) |
| 632 |
} |
| 633 |
|
| 634 |
func BenchmarkEncodeAllDict65536_0(b *testing.B) { |
| 635 |
benchmarkEncodeAllLimitedBySize(b, 65536, 0) |
| 636 |
} |
| 637 |
|
| 638 |
func TestDecoder_MoreDicts(t *testing.T) { |
| 639 |
// All files have CRC |
| 640 |
// https://files.klauspost.com/compress/zstd-dict-tests.zip |
| 641 |
fn := "testdata/zstd-dict-tests.zip" |
| 642 |
data, err := os.ReadFile(fn) |
| 643 |
if err != nil { |
| 644 |
t.Skip("extended dict test not found.") |
| 645 |
} |
| 646 |
zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) |
| 647 |
if err != nil { |
| 648 |
t.Fatal(err) |
| 649 |
} |
| 650 |
|
| 651 |
var dicts [][]byte |
| 652 |
for _, tt := range zr.File { |
| 653 |
if !strings.HasSuffix(tt.Name, ".dict") { |
| 654 |
continue |
| 655 |
} |
| 656 |
func() { |
| 657 |
r, err := tt.Open() |
| 658 |
if err != nil { |
| 659 |
t.Fatal(err) |
| 660 |
} |
| 661 |
defer r.Close() |
| 662 |
in, err := io.ReadAll(r) |
| 663 |
if err != nil { |
| 664 |
t.Fatal(err) |
| 665 |
} |
| 666 |
dicts = append(dicts, in) |
| 667 |
}() |
| 668 |
} |
| 669 |
dec, err := NewReader(nil, WithDecoderConcurrency(1), WithDecoderDicts(dicts...)) |
| 670 |
if err != nil { |
| 671 |
t.Fatal(err) |
| 672 |
return |
| 673 |
} |
| 674 |
defer dec.Close() |
| 675 |
for i, tt := range zr.File { |
| 676 |
if !strings.HasSuffix(tt.Name, ".zst") { |
| 677 |
continue |
| 678 |
} |
| 679 |
if testing.Short() && i > 50 { |
| 680 |
continue |
| 681 |
} |
| 682 |
t.Run("decodeall-"+tt.Name, func(t *testing.T) { |
| 683 |
r, err := tt.Open() |
| 684 |
if err != nil { |
| 685 |
t.Fatal(err) |
| 686 |
} |
| 687 |
defer r.Close() |
| 688 |
in, err := io.ReadAll(r) |
| 689 |
if err != nil { |
| 690 |
t.Fatal(err) |
| 691 |
} |
| 692 |
got, err := dec.DecodeAll(in, nil) |
| 693 |
if err != nil { |
| 694 |
t.Fatal(err) |
| 695 |
} |
| 696 |
_, err = dec.DecodeAll(in, got[:0]) |
| 697 |
if err != nil { |
| 698 |
t.Fatal(err) |
| 699 |
} |
| 700 |
}) |
| 701 |
} |
| 702 |
} |
| 703 |
|
| 704 |
func TestDecoder_MoreDicts2(t *testing.T) { |
| 705 |
// All files have CRC |
| 706 |
// https://files.klauspost.com/compress/zstd-dict-tests.zip |
| 707 |
fn := "testdata/zstd-dict-tests.zip" |
| 708 |
data, err := os.ReadFile(fn) |
| 709 |
if err != nil { |
| 710 |
t.Skip("extended dict test not found.") |
| 711 |
} |
| 712 |
zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data))) |
| 713 |
if err != nil { |
| 714 |
t.Fatal(err) |
| 715 |
} |
| 716 |
|
| 717 |
var dicts [][]byte |
| 718 |
for _, tt := range zr.File { |
| 719 |
if !strings.HasSuffix(tt.Name, ".dict") { |
| 720 |
continue |
| 721 |
} |
| 722 |
func() { |
| 723 |
r, err := tt.Open() |
| 724 |
if err != nil { |
| 725 |
t.Fatal(err) |
| 726 |
} |
| 727 |
defer r.Close() |
| 728 |
in, err := io.ReadAll(r) |
| 729 |
if err != nil { |
| 730 |
t.Fatal(err) |
| 731 |
} |
| 732 |
dicts = append(dicts, in) |
| 733 |
}() |
| 734 |
} |
| 735 |
dec, err := NewReader(nil, WithDecoderConcurrency(2), WithDecoderDicts(dicts...)) |
| 736 |
if err != nil { |
| 737 |
t.Fatal(err) |
| 738 |
return |
| 739 |
} |
| 740 |
defer dec.Close() |
| 741 |
for i, tt := range zr.File { |
| 742 |
if !strings.HasSuffix(tt.Name, ".zst") { |
| 743 |
continue |
| 744 |
} |
| 745 |
if testing.Short() && i > 50 { |
| 746 |
continue |
| 747 |
} |
| 748 |
t.Run("decodeall-"+tt.Name, func(t *testing.T) { |
| 749 |
r, err := tt.Open() |
| 750 |
if err != nil { |
| 751 |
t.Fatal(err) |
| 752 |
} |
| 753 |
defer r.Close() |
| 754 |
in, err := io.ReadAll(r) |
| 755 |
if err != nil { |
| 756 |
t.Fatal(err) |
| 757 |
} |
| 758 |
got, err := dec.DecodeAll(in, nil) |
| 759 |
if err != nil { |
| 760 |
t.Fatal(err) |
| 761 |
} |
| 762 |
_, err = dec.DecodeAll(in, got[:0]) |
| 763 |
if err != nil { |
| 764 |
t.Fatal(err) |
| 765 |
} |
| 766 |
}) |
| 767 |
} |
| 768 |
} |
| 769 |
|
| 770 |
func readDicts(tb testing.TB, zr *zip.Reader) [][]byte { |
| 771 |
var dicts [][]byte |
| 772 |
for _, tt := range zr.File { |
| 773 |
if !strings.HasSuffix(tt.Name, ".dict") { |
| 774 |
continue |
| 775 |
} |
| 776 |
func() { |
| 777 |
r, err := tt.Open() |
| 778 |
if err != nil { |
| 779 |
tb.Fatal(err) |
| 780 |
} |
| 781 |
defer r.Close() |
| 782 |
in, err := io.ReadAll(r) |
| 783 |
if err != nil { |
| 784 |
tb.Fatal(err) |
| 785 |
} |
| 786 |
dicts = append(dicts, in) |
| 787 |
}() |
| 788 |
} |
| 789 |
return dicts |
| 790 |
} |
| 791 |
|
| 792 |
// Test decoding of zstd --patch-from output. |
| 793 |
func TestDecoderRawDict(t *testing.T) { |
| 794 |
t.Parallel() |
| 795 |
|
| 796 |
dict, err := os.ReadFile("testdata/delta/source.txt") |
| 797 |
if err != nil { |
| 798 |
t.Fatal(err) |
| 799 |
} |
| 800 |
|
| 801 |
delta, err := os.Open("testdata/delta/target.txt.zst") |
| 802 |
if err != nil { |
| 803 |
t.Fatal(err) |
| 804 |
} |
| 805 |
defer delta.Close() |
| 806 |
|
| 807 |
dec, err := NewReader(delta, WithDecoderDictRaw(0, dict)) |
| 808 |
if err != nil { |
| 809 |
t.Fatal(err) |
| 810 |
} |
| 811 |
|
| 812 |
out, err := io.ReadAll(dec) |
| 813 |
if err != nil { |
| 814 |
t.Fatal(err) |
| 815 |
} |
| 816 |
|
| 817 |
ref, err := os.ReadFile("testdata/delta/target.txt") |
| 818 |
if err != nil { |
| 819 |
t.Fatal(err) |
| 820 |
} |
| 821 |
|
| 822 |
if !bytes.Equal(out, ref) { |
| 823 |
t.Errorf("mismatch: got %q, wanted %q", out, ref) |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
// TestEncoderDictResetDifferentContent verifies that ResetWithOptions correctly |
| 828 |
// handles switching between raw dicts that share the same ID but have different |
| 829 |
// content lengths. Previously, the encoder cached dict tables by ID only, so a |
| 830 |
// shorter dict reusing the same ID would leave stale table entries pointing |
| 831 |
// beyond the new (shorter) history, causing an out-of-bounds panic in matchlen. |
| 832 |
func TestEncoderDictResetDifferentContent(t *testing.T) { |
| 833 |
// Two raw dicts: same ID, different content lengths. |
| 834 |
longDict := make([]byte, 700) |
| 835 |
for i := range longDict { |
| 836 |
longDict[i] = byte(i * 3) |
| 837 |
} |
| 838 |
shortDict := make([]byte, 120) |
| 839 |
for i := range shortDict { |
| 840 |
shortDict[i] = byte(i * 7) |
| 841 |
} |
| 842 |
|
| 843 |
const dictID = 42 |
| 844 |
// Payload reuses bytes from the tail of longDict (beyond shortDict's length). |
| 845 |
// This makes stale dict table entries match during encoding, triggering the |
| 846 |
// out-of-bounds access when the encoder uses the stale offset. |
| 847 |
payload := make([]byte, 200) |
| 848 |
copy(payload, longDict[500:]) |
| 849 |
|
| 850 |
for level := SpeedFastest; level < speedLast; level++ { |
| 851 |
t.Run(level.String(), func(t *testing.T) { |
| 852 |
enc, err := NewWriter(nil, WithEncoderConcurrency(1), WithEncoderLevel(level), WithEncoderDictRaw(dictID, longDict)) |
| 853 |
if err != nil { |
| 854 |
t.Fatal(err) |
| 855 |
} |
| 856 |
|
| 857 |
// Encode with long dict first to populate table entries at high offsets. |
| 858 |
enc.EncodeAll(payload, nil) |
| 859 |
|
| 860 |
// Switch to shorter dict with same ID. This must rebuild the tables. |
| 861 |
if err := enc.ResetWithOptions(nil, WithEncoderDictRaw(dictID, shortDict)); err != nil { |
| 862 |
t.Fatal(err) |
| 863 |
} |
| 864 |
compressed := enc.EncodeAll(payload, nil) |
| 865 |
|
| 866 |
// Verify round-trip with matching dict. |
| 867 |
dec, err := NewReader(nil, WithDecoderConcurrency(1), WithDecoderDictRaw(dictID, shortDict)) |
| 868 |
if err != nil { |
| 869 |
t.Fatal(err) |
| 870 |
} |
| 871 |
defer dec.Close() |
| 872 |
got, err := dec.DecodeAll(compressed, nil) |
| 873 |
if err != nil { |
| 874 |
t.Fatal(err) |
| 875 |
} |
| 876 |
if !bytes.Equal(got, payload) { |
| 877 |
t.Errorf("round-trip mismatch: got %q, want %q", got, payload) |
| 878 |
} |
| 879 |
}) |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
// TestEncoderDictAddViaReset verifies that adding/removing a dict via |
| 884 |
// ResetWithOptions works (requires recreating the encoder type). |
| 885 |
func TestEncoderDictAddViaReset(t *testing.T) { |
| 886 |
dict := make([]byte, 120) |
| 887 |
for i := range dict { |
| 888 |
dict[i] = byte(i) |
| 889 |
} |
| 890 |
payload := []byte("hello world, this is a test payload!!") |
| 891 |
|
| 892 |
for level := SpeedFastest; level < speedLast; level++ { |
| 893 |
t.Run("nil-to-dict/"+level.String(), func(t *testing.T) { |
| 894 |
enc, err := NewWriter(nil, WithEncoderConcurrency(1), WithEncoderLevel(level)) |
| 895 |
if err != nil { |
| 896 |
t.Fatal(err) |
| 897 |
} |
| 898 |
if err := enc.ResetWithOptions(nil, WithEncoderDictRaw(42, dict)); err != nil { |
| 899 |
t.Fatal(err) |
| 900 |
} |
| 901 |
compressed := enc.EncodeAll(payload, nil) |
| 902 |
|
| 903 |
dec, err := NewReader(nil, WithDecoderConcurrency(1), WithDecoderDictRaw(42, dict)) |
| 904 |
if err != nil { |
| 905 |
t.Fatal(err) |
| 906 |
} |
| 907 |
defer dec.Close() |
| 908 |
got, err := dec.DecodeAll(compressed, nil) |
| 909 |
if err != nil { |
| 910 |
t.Fatal(err) |
| 911 |
} |
| 912 |
if !bytes.Equal(got, payload) { |
| 913 |
t.Errorf("round-trip mismatch: got %q, want %q", got, payload) |
| 914 |
} |
| 915 |
}) |
| 916 |
|
| 917 |
t.Run("dict-to-nil/"+level.String(), func(t *testing.T) { |
| 918 |
enc, err := NewWriter(nil, WithEncoderConcurrency(1), WithEncoderLevel(level), WithEncoderDictRaw(42, dict)) |
| 919 |
if err != nil { |
| 920 |
t.Fatal(err) |
| 921 |
} |
| 922 |
if err := enc.ResetWithOptions(nil, WithEncoderDictDelete()); err != nil { |
| 923 |
t.Fatal(err) |
| 924 |
} |
| 925 |
compressed := enc.EncodeAll(payload, nil) |
| 926 |
|
| 927 |
dec, err := NewReader(nil, WithDecoderConcurrency(1)) |
| 928 |
if err != nil { |
| 929 |
t.Fatal(err) |
| 930 |
} |
| 931 |
defer dec.Close() |
| 932 |
got, err := dec.DecodeAll(compressed, nil) |
| 933 |
if err != nil { |
| 934 |
t.Fatal(err) |
| 935 |
} |
| 936 |
if !bytes.Equal(got, payload) { |
| 937 |
t.Errorf("round-trip mismatch: got %q, want %q", got, payload) |
| 938 |
} |
| 939 |
}) |
| 940 |
|
| 941 |
t.Run("streaming-dict-to-nil/"+level.String(), func(t *testing.T) { |
| 942 |
var buf bytes.Buffer |
| 943 |
enc, err := NewWriter(&buf, WithEncoderConcurrency(2), WithEncoderLevel(level), WithEncoderDictRaw(42, dict)) |
| 944 |
if err != nil { |
| 945 |
t.Fatal(err) |
| 946 |
} |
| 947 |
enc.Close() |
| 948 |
|
| 949 |
buf.Reset() |
| 950 |
if err := enc.ResetWithOptions(&buf, WithEncoderDictDelete()); err != nil { |
| 951 |
t.Fatal(err) |
| 952 |
} |
| 953 |
_, err = enc.Write(payload) |
| 954 |
if err != nil { |
| 955 |
t.Fatal(err) |
| 956 |
} |
| 957 |
if err := enc.Close(); err != nil { |
| 958 |
t.Fatal(err) |
| 959 |
} |
| 960 |
|
| 961 |
dec, err := NewReader(nil, WithDecoderConcurrency(1)) |
| 962 |
if err != nil { |
| 963 |
t.Fatal(err) |
| 964 |
} |
| 965 |
defer dec.Close() |
| 966 |
got, err := dec.DecodeAll(buf.Bytes(), nil) |
| 967 |
if err != nil { |
| 968 |
t.Fatal(err) |
| 969 |
} |
| 970 |
if !bytes.Equal(got, payload) { |
| 971 |
t.Errorf("round-trip mismatch: got %q, want %q", got, payload) |
| 972 |
} |
| 973 |
}) |
| 974 |
} |
| 975 |
} |
| 976 |
|
| 977 |
// TestSharedDecoderTrainedDictRace verifies concurrent DecodeAll on one Decoder |
| 978 |
// that holds a trained dictionary does not race on dict.litEnc (gh-1180). |
| 979 |
func TestSharedDecoderTrainedDictRace(t *testing.T) { |
| 980 |
weights := map[byte]int{'e': 40, 't': 20, 'a': 12, 'o': 8, 'i': 6, 'n': 5, 's': 4, 'h': 3, 'r': 2} |
| 981 |
var alphabet []byte |
| 982 |
for c, n := range weights { |
| 983 |
for range n { |
| 984 |
alphabet = append(alphabet, c) |
| 985 |
} |
| 986 |
} |
| 987 |
sample := func(rng *rand.Rand, n int) []byte { |
| 988 |
b := make([]byte, n) |
| 989 |
for i := range b { |
| 990 |
b[i] = alphabet[rng.Intn(len(alphabet))] |
| 991 |
} |
| 992 |
return b |
| 993 |
} |
| 994 |
|
| 995 |
rng := rand.New(rand.NewSource(2000)) |
| 996 |
samples := make([][]byte, 2000) |
| 997 |
for i := range samples { |
| 998 |
samples[i] = sample(rng, 32+rng.Intn(200)) |
| 999 |
} |
| 1000 |
dict, err := BuildDict(BuildDictOptions{ |
| 1001 |
ID: 12345, |
| 1002 |
Contents: samples, |
| 1003 |
History: sample(rng, 12<<10), |
| 1004 |
Offsets: [3]int{1, 4, 8}, |
| 1005 |
}) |
| 1006 |
if err != nil { |
| 1007 |
t.Fatal(err) |
| 1008 |
} |
| 1009 |
|
| 1010 |
enc, err := NewWriter(nil, WithEncoderDict(dict), WithEncoderConcurrency(1)) |
| 1011 |
if err != nil { |
| 1012 |
t.Fatal(err) |
| 1013 |
} |
| 1014 |
nFrames := 400 |
| 1015 |
iters := 3000 |
| 1016 |
goroutines := 48 |
| 1017 |
if testing.Short() { |
| 1018 |
nFrames = 80 |
| 1019 |
iters = 200 |
| 1020 |
goroutines = 8 |
| 1021 |
} |
| 1022 |
frames := make([][]byte, nFrames) |
| 1023 |
plains := make([][]byte, nFrames) |
| 1024 |
for i := 0; i < nFrames; i++ { |
| 1025 |
pt := sample(rng, 200+rng.Intn(800)) |
| 1026 |
frames[i] = enc.EncodeAll(pt, nil) |
| 1027 |
plains[i] = pt |
| 1028 |
} |
| 1029 |
enc.Close() |
| 1030 |
|
| 1031 |
dec, err := NewReader(nil, WithDecoderDicts(dict)) |
| 1032 |
if err != nil { |
| 1033 |
t.Fatal(err) |
| 1034 |
} |
| 1035 |
defer dec.Close() |
| 1036 |
|
| 1037 |
var wg sync.WaitGroup |
| 1038 |
for g := 0; g < goroutines; g++ { |
| 1039 |
wg.Add(1) |
| 1040 |
go func(seed int) { |
| 1041 |
defer wg.Done() |
| 1042 |
r := rand.New(rand.NewSource(int64(seed))) |
| 1043 |
for i := 0; i < iters; i++ { |
| 1044 |
idx := r.Intn(len(frames)) |
| 1045 |
out, err := dec.DecodeAll(frames[idx], nil) |
| 1046 |
if err != nil { |
| 1047 |
t.Errorf("decode error: %v", err) |
| 1048 |
return |
| 1049 |
} |
| 1050 |
if !bytes.Equal(out, plains[idx]) { |
| 1051 |
t.Errorf("output mismatch") |
| 1052 |
return |
| 1053 |
} |
| 1054 |
} |
| 1055 |
}(g + 1) |
| 1056 |
} |
| 1057 |
wg.Wait() |
| 1058 |
} |
| 1059 |
|
| 1060 |
// hideLen wraps a reader so the decoder cannot detect a *bytes.Reader and take |
| 1061 |
// the small-buffer sync path; this forces the concurrent streaming decode path. |
| 1062 |
type hideLen struct{ r io.Reader } |
| 1063 |
|
| 1064 |
func (h hideLen) Read(p []byte) (int, error) { return h.r.Read(p) } |
| 1065 |
|
| 1066 |
// TestDecoderDictReuseAfterLargeStream verifies that streaming-decoding a |
| 1067 |
// dictionary-compressed frame whose output exceeds the window does not corrupt |
| 1068 |
// the registered dictionary for later decodes on the same decoder. |
| 1069 |
func TestDecoderDictReuseAfterLargeStream(t *testing.T) { |
| 1070 |
rng := rand.New(rand.NewSource(1)) |
| 1071 |
dictContent := make([]byte, 8<<10) |
| 1072 |
rng.Read(dictContent) |
| 1073 |
const dictID = 12345 |
| 1074 |
const windowSize = 256 << 10 |
| 1075 |
|
| 1076 |
// The payload starts with the dictionary content (so the encoder emits a |
| 1077 |
// match into the dictionary) and is long enough that decoded history |
| 1078 |
// exceeds the window while compressed blocks are still being executed. |
| 1079 |
payload := bytes.Repeat(dictContent, 2*windowSize/len(dictContent)) |
| 1080 |
|
| 1081 |
enc, err := NewWriter(nil, WithEncoderDictRaw(dictID, dictContent), WithWindowSize(windowSize)) |
| 1082 |
if err != nil { |
| 1083 |
t.Fatal(err) |
| 1084 |
} |
| 1085 |
frame := enc.EncodeAll(payload, nil) |
| 1086 |
enc.Close() |
| 1087 |
|
| 1088 |
dec, err := NewReader(nil, WithDecoderDictRaw(dictID, dictContent)) |
| 1089 |
if err != nil { |
| 1090 |
t.Fatal(err) |
| 1091 |
} |
| 1092 |
defer dec.Close() |
| 1093 |
|
| 1094 |
for i := range 3 { |
| 1095 |
if err := dec.Reset(hideLen{bytes.NewReader(frame)}); err != nil { |
| 1096 |
t.Fatalf("reset %d: %v", i, err) |
| 1097 |
} |
| 1098 |
got, err := io.ReadAll(dec) |
| 1099 |
if err != nil { |
| 1100 |
t.Fatalf("decode %d of a valid dictionary frame failed: %v", i, err) |
| 1101 |
} |
| 1102 |
if !bytes.Equal(got, payload) { |
| 1103 |
t.Fatalf("decode %d: output mismatch: got %d bytes, want %d", i, len(got), len(payload)) |
| 1104 |
} |
| 1105 |
} |
| 1106 |
} |