|
| 1 | +package zstd |
| 2 | + |
| 3 | +import ( |
| 4 | + "bytes" |
| 5 | + "testing" |
| 6 | +) |
| 7 | + |
| 8 | +// unknownSizeFrame returns a zstd frame that does not advertise its decompressed |
| 9 | +// size in the header. The streaming writer produces such frames, as do legacy |
| 10 | +// zstd v0.5 frames; ZSTD_getFrameContentSize reports "unknown" for both, so |
| 11 | +// decompressSizeHint falls back to its (large) upper bound. |
| 12 | +func unknownSizeFrame(t *testing.T, payload []byte) []byte { |
| 13 | + t.Helper() |
| 14 | + var b bytes.Buffer |
| 15 | + w := NewWriter(&b) |
| 16 | + if _, err := w.Write(payload); err != nil { |
| 17 | + t.Fatalf("write: %v", err) |
| 18 | + } |
| 19 | + if err := w.Close(); err != nil { |
| 20 | + t.Fatalf("close: %v", err) |
| 21 | + } |
| 22 | + return b.Bytes() |
| 23 | +} |
| 24 | + |
| 25 | +type namedDecompressor struct { |
| 26 | + name string |
| 27 | + fn func(dst, src []byte) ([]byte, error) |
| 28 | +} |
| 29 | + |
| 30 | +// decompressors are the entry points that share the caller-buffer-reuse logic. |
| 31 | +func decompressors() []namedDecompressor { |
| 32 | + return []namedDecompressor{ |
| 33 | + {"Decompress", Decompress}, |
| 34 | + {"ctx.Decompress", NewCtx().Decompress}, |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +// TestDecompressReusesCallerBufferUnknownSize verifies that when a frame does |
| 39 | +// not advertise its content size, decompression uses an adequately sized caller |
| 40 | +// buffer instead of discarding it and allocating decompressSizeBufferLimit. |
| 41 | +func TestDecompressReusesCallerBufferUnknownSize(t *testing.T) { |
| 42 | + payload := bytes.Repeat([]byte("datadog-"), 525) // 4200 bytes |
| 43 | + frame := unknownSizeFrame(t, payload) |
| 44 | + |
| 45 | + // Sanity: the frame has unknown content size, so the hint is the upper |
| 46 | + // bound (>= decompressSizeBufferLimit). Without reusing the caller buffer, |
| 47 | + // decompression would allocate that many bytes. |
| 48 | + if hint, found := decompressSizeHint(frame); found || hint < decompressSizeBufferLimit { |
| 49 | + t.Fatalf("expected unknown-size frame to hint the upper bound: found=%v hint=%d", found, hint) |
| 50 | + } |
| 51 | + |
| 52 | + for _, d := range decompressors() { |
| 53 | + t.Run(d.name, func(t *testing.T) { |
| 54 | + buf := make([]byte, 8192) // adequate for the payload, far below the bound |
| 55 | + out, err := d.fn(buf, frame) |
| 56 | + if err != nil { |
| 57 | + t.Fatalf("decompress: %v", err) |
| 58 | + } |
| 59 | + if !bytes.Equal(out, payload) { |
| 60 | + t.Fatalf("round-trip mismatch") |
| 61 | + } |
| 62 | + if cap(out) != cap(buf) { |
| 63 | + t.Fatalf("caller buffer (cap %d) should be reused, got cap %d", cap(buf), cap(out)) |
| 64 | + } |
| 65 | + }) |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +// TestDecompressUnknownSizeTooSmallBuffer verifies the fallback still works when |
| 70 | +// the caller buffer is too small for an unknown-size frame. |
| 71 | +func TestDecompressUnknownSizeTooSmallBuffer(t *testing.T) { |
| 72 | + payload := bytes.Repeat([]byte("datadog-"), 525) |
| 73 | + frame := unknownSizeFrame(t, payload) |
| 74 | + |
| 75 | + for _, d := range decompressors() { |
| 76 | + t.Run(d.name, func(t *testing.T) { |
| 77 | + out, err := d.fn(make([]byte, 8), frame) // too small; must fall back |
| 78 | + if err != nil { |
| 79 | + t.Fatalf("decompress: %v", err) |
| 80 | + } |
| 81 | + if !bytes.Equal(out, payload) { |
| 82 | + t.Fatalf("round-trip mismatch on fallback") |
| 83 | + } |
| 84 | + }) |
| 85 | + } |
| 86 | +} |
| 87 | + |
| 88 | +// TestDecompressUnknownSizeNilBuffer verifies nil dst still decompresses. |
| 89 | +func TestDecompressUnknownSizeNilBuffer(t *testing.T) { |
| 90 | + payload := bytes.Repeat([]byte("datadog-"), 525) |
| 91 | + frame := unknownSizeFrame(t, payload) |
| 92 | + |
| 93 | + for _, d := range decompressors() { |
| 94 | + t.Run(d.name, func(t *testing.T) { |
| 95 | + out, err := d.fn(nil, frame) |
| 96 | + if err != nil { |
| 97 | + t.Fatalf("decompress: %v", err) |
| 98 | + } |
| 99 | + if !bytes.Equal(out, payload) { |
| 100 | + t.Fatalf("round-trip mismatch on nil dst") |
| 101 | + } |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// TestDecompressKnownSizeReusesCallerBuffer verifies that for frames that do |
| 107 | +// advertise their size, an adequate caller buffer is still reused (the common |
| 108 | +// path is unchanged). |
| 109 | +func TestDecompressKnownSizeReusesCallerBuffer(t *testing.T) { |
| 110 | + payload := bytes.Repeat([]byte("datadog-"), 525) |
| 111 | + frame, err := Compress(nil, payload) |
| 112 | + if err != nil { |
| 113 | + t.Fatalf("Compress: %v", err) |
| 114 | + } |
| 115 | + if _, found := decompressSizeHint(frame); !found { |
| 116 | + t.Fatal("Compress frame should advertise its content size") |
| 117 | + } |
| 118 | + |
| 119 | + for _, d := range decompressors() { |
| 120 | + t.Run(d.name, func(t *testing.T) { |
| 121 | + buf := make([]byte, 8192) |
| 122 | + out, err := d.fn(buf, frame) |
| 123 | + if err != nil { |
| 124 | + t.Fatalf("decompress: %v", err) |
| 125 | + } |
| 126 | + if !bytes.Equal(out, payload) { |
| 127 | + t.Fatalf("round-trip mismatch") |
| 128 | + } |
| 129 | + if cap(out) != cap(buf) { |
| 130 | + t.Fatalf("caller buffer (cap %d) should be reused, got cap %d", cap(buf), cap(out)) |
| 131 | + } |
| 132 | + }) |
| 133 | + } |
| 134 | +} |
| 135 | + |
| 136 | +// TestDecompressSizeHintFound verifies foundHint distinguishes frames that |
| 137 | +// advertise their content size (produced by Compress) from those that do not |
| 138 | +// (streaming, as here, and legacy zstd v0.5 frames). |
| 139 | +func TestDecompressSizeHintFound(t *testing.T) { |
| 140 | + payload := bytes.Repeat([]byte("datadog-"), 525) |
| 141 | + |
| 142 | + known, err := Compress(nil, payload) |
| 143 | + if err != nil { |
| 144 | + t.Fatalf("Compress: %v", err) |
| 145 | + } |
| 146 | + if hint, found := decompressSizeHint(known); !found || hint != len(payload) { |
| 147 | + t.Fatalf("known-size frame: found=%v hint=%d, want found=true hint=%d", found, hint, len(payload)) |
| 148 | + } |
| 149 | + |
| 150 | + if _, found := decompressSizeHint(unknownSizeFrame(t, payload)); found { |
| 151 | + t.Fatalf("streaming frame should not advertise its content size") |
| 152 | + } |
| 153 | +} |
0 commit comments