Files
tailscale/util/zstdframe/synctest_test.go
James Tucker 6608b9a387 tsweb/compserve, client/web: add zstd, remove brotli for precompressed assets
Adds tsweb/compserve: content negotiation for precompressed static
variants, with a transcode-to-identity fallback for clients that do not
accept an encoding (including when the raw file is absent), and
CompressWriter, which live-compresses dynamic responses with zstd in its
fastest mode, streamed incrementally with no buffering. Negotiation is
q-value and wildcard aware (gzip;q=0 previously matched gzip).

client/web serves its prebuilt embedded assets through compserve,
replacing brotli with zstd; the embedded FS is wrapped in
tsweb/vcstime for conditional-request mod times. tsweb/compserve/gzip.go
keeps transitional serving of gzip variants from pre-zstd file systems
(such as the currently published web-client-prebuilt module):
passthrough to gzip-accepting clients, transcoded to identity otherwise;
it becomes inert once a zstd-only module is published.

util/zstdframe gains pooled GetDecoder and GetStreamingEncoder
(concurrency=1). util/precompress is now a build-time tool, generating
zstd variants only. cmd/tsconnect and cmd/build-webclient consume the
new precompress/compserve split. tsweb.AcceptsEncoding and
tsweb/tswebutil are removed; negotiation lives in compserve and the
deprecated shim had no callers. go.mod bumps web-client-prebuilt.

Also fixes a transcoding bug where http.ServeContent's size probe via
the promoted zstd.Decoder.WriteTo could report a zero length, serving
empty bodies.

Updates tailscale/corp#20099

Signed-off-by: James Tucker <[email protected]>
2026-09-17 15:23:49 -07:00

116 lines
3.3 KiB
Go

// Copyright (c) Tailscale Inc & contributors
// SPDX-License-Identifier: BSD-3-Clause
package zstdframe
import (
"bytes"
"io"
"testing"
"testing/synctest"
"time"
)
// TestSynctestBubbleIsolation verifies that coders used within a
// testing/synctest bubble do not crash the process when zstdframe is
// subsequently used outside the bubble (or in another bubble).
//
// The zstd Encoder and Decoder types use channels internally, so a pooled
// coder that was created within a synctest bubble must never be reused
// outside of it: the runtime kills the process with "fatal error: receive
// on synctest channel from outside bubble".
func TestSynctestBubbleIsolation(t *testing.T) {
src := []byte("hello, hello, hello, world, world, world")
// Use the coder pools within a bubble several times to make it very
// likely that a bubble-created coder would land in the package-level
// pools if pooling were (incorrectly) enabled here.
var frame []byte
synctest.Test(t, func(t *testing.T) {
for range 10 {
frame = AppendEncode(nil, src)
out, err := AppendDecode(nil, frame)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(out, src) {
t.Fatalf("roundtrip inside bubble = %q, want %q", out, src)
}
}
// Bubble detection must not depend on the fake clock still being
// near its 2000-01-01 start, so advance it past the real
// process start time and use the coders again.
time.Sleep(100 * 365 * 24 * time.Hour)
frame = AppendEncode(nil, src)
if _, err := AppendDecode(nil, frame); err != nil {
t.Fatal(err)
}
})
// Prior to pooling being disabled in tests, this crashed the process
// by reusing a pooled coder whose channels were created in the
// now-exited bubble above.
for range 10 {
got := AppendEncode(nil, src)
out, err := AppendDecode(nil, got)
if err != nil {
t.Fatal(err)
}
if !bytes.Equal(out, src) {
t.Fatalf("roundtrip outside bubble = %q, want %q", out, src)
}
}
}
// TestSynctestBubbleIsolationStreaming verifies the same isolation property
// as TestSynctestBubbleIsolation for the streaming GetDecoder and
// GetStreamingEncoder APIs, whose pools are separate from the stateless
// coders'.
func TestSynctestBubbleIsolationStreaming(t *testing.T) {
src := []byte("hello, hello, hello, world, world, world")
synctest.Test(t, func(t *testing.T) {
for range 10 {
enc, putEnc := GetStreamingEncoder(FastestCompression)
enc.Reset(&bytes.Buffer{})
enc.Close()
putEnc()
dec, putDec := GetDecoder()
if err := dec.Reset(bytes.NewReader(src)); err != nil {
t.Fatal(err)
}
putDec()
}
})
// A bubble-created streaming coder must never be reused outside the
// bubble; prior to the nil-pool guard in GetStreamingEncoder's put
// function, put panicked inside the bubble instead.
for range 10 {
var buf bytes.Buffer
enc, putEnc := GetStreamingEncoder(FastestCompression)
enc.Reset(&buf)
if _, err := enc.Write(src); err != nil {
t.Fatal(err)
}
if err := enc.Close(); err != nil {
t.Fatal(err)
}
putEnc()
dec, putDec := GetDecoder()
if err := dec.Reset(bytes.NewReader(buf.Bytes())); err != nil {
t.Fatal(err)
}
out, err := io.ReadAll(dec)
if err != nil {
t.Fatal(err)
}
putDec()
if !bytes.Equal(out, src) {
t.Fatalf("roundtrip outside bubble = %q, want %q", out, src)
}
}
}