1
2
3
4
5 package zstd
6
7 import (
8 "bytes"
9 "crypto/sha256"
10 "fmt"
11 "internal/race"
12 "internal/testenv"
13 "io"
14 "os"
15 "os/exec"
16 "path/filepath"
17 "strings"
18 "sync"
19 "testing"
20 )
21
22
23 var tests = []struct {
24 name, uncompressed, compressed string
25 }{
26 {
27 "hello",
28 "hello, world\n",
29 "\x28\xb5\x2f\xfd\x24\x0d\x69\x00\x00\x68\x65\x6c\x6c\x6f\x2c\x20\x77\x6f\x72\x6c\x64\x0a\x4c\x1f\xf9\xf1",
30 },
31 {
32
33 "ranges",
34 "\xcc\x11\x00\x00\x00\x00\x00\x00\xd5\x13\x00\x00\x00\x00\x00\x00" +
35 "\x1c\x14\x00\x00\x00\x00\x00\x00\x72\x14\x00\x00\x00\x00\x00\x00" +
36 "\x9d\x14\x00\x00\x00\x00\x00\x00\xd5\x14\x00\x00\x00\x00\x00\x00" +
37 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
38 "\xfb\x12\x00\x00\x00\x00\x00\x00\x09\x13\x00\x00\x00\x00\x00\x00" +
39 "\x0c\x13\x00\x00\x00\x00\x00\x00\xcb\x13\x00\x00\x00\x00\x00\x00" +
40 "\x29\x14\x00\x00\x00\x00\x00\x00\x4e\x14\x00\x00\x00\x00\x00\x00" +
41 "\x9d\x14\x00\x00\x00\x00\x00\x00\xd5\x14\x00\x00\x00\x00\x00\x00" +
42 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
43 "\xfb\x12\x00\x00\x00\x00\x00\x00\x09\x13\x00\x00\x00\x00\x00\x00" +
44 "\x67\x13\x00\x00\x00\x00\x00\x00\xcb\x13\x00\x00\x00\x00\x00\x00" +
45 "\x9d\x14\x00\x00\x00\x00\x00\x00\xd5\x14\x00\x00\x00\x00\x00\x00" +
46 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
47 "\x5f\x0b\x00\x00\x00\x00\x00\x00\x6c\x0b\x00\x00\x00\x00\x00\x00" +
48 "\x7d\x0b\x00\x00\x00\x00\x00\x00\x7e\x0c\x00\x00\x00\x00\x00\x00" +
49 "\x38\x0f\x00\x00\x00\x00\x00\x00\x5c\x0f\x00\x00\x00\x00\x00\x00" +
50 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
51 "\x83\x0c\x00\x00\x00\x00\x00\x00\xfa\x0c\x00\x00\x00\x00\x00\x00" +
52 "\xfd\x0d\x00\x00\x00\x00\x00\x00\xef\x0e\x00\x00\x00\x00\x00\x00" +
53 "\x14\x0f\x00\x00\x00\x00\x00\x00\x38\x0f\x00\x00\x00\x00\x00\x00" +
54 "\x9f\x0f\x00\x00\x00\x00\x00\x00\xac\x0f\x00\x00\x00\x00\x00\x00" +
55 "\xdb\x0f\x00\x00\x00\x00\x00\x00\xff\x0f\x00\x00\x00\x00\x00\x00" +
56 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
57 "\xfd\x0d\x00\x00\x00\x00\x00\x00\xd8\x0e\x00\x00\x00\x00\x00\x00" +
58 "\x9f\x0f\x00\x00\x00\x00\x00\x00\xac\x0f\x00\x00\x00\x00\x00\x00" +
59 "\xdb\x0f\x00\x00\x00\x00\x00\x00\xff\x0f\x00\x00\x00\x00\x00\x00" +
60 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
61 "\xfa\x0c\x00\x00\x00\x00\x00\x00\xea\x0d\x00\x00\x00\x00\x00\x00" +
62 "\xef\x0e\x00\x00\x00\x00\x00\x00\x14\x0f\x00\x00\x00\x00\x00\x00" +
63 "\x5c\x0f\x00\x00\x00\x00\x00\x00\x9f\x0f\x00\x00\x00\x00\x00\x00" +
64 "\xac\x0f\x00\x00\x00\x00\x00\x00\xdb\x0f\x00\x00\x00\x00\x00\x00" +
65 "\xff\x0f\x00\x00\x00\x00\x00\x00\x2c\x10\x00\x00\x00\x00\x00\x00" +
66 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
67 "\x60\x11\x00\x00\x00\x00\x00\x00\xd1\x16\x00\x00\x00\x00\x00\x00" +
68 "\x40\x0b\x00\x00\x00\x00\x00\x00\x2c\x10\x00\x00\x00\x00\x00\x00" +
69 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
70 "\x7a\x00\x00\x00\x00\x00\x00\x00\xb6\x00\x00\x00\x00\x00\x00\x00" +
71 "\x9f\x01\x00\x00\x00\x00\x00\x00\xa7\x01\x00\x00\x00\x00\x00\x00" +
72 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
73 "\x7a\x00\x00\x00\x00\x00\x00\x00\xa9\x00\x00\x00\x00\x00\x00\x00" +
74 "\x9f\x01\x00\x00\x00\x00\x00\x00\xa7\x01\x00\x00\x00\x00\x00\x00" +
75 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
76
77 "\x28\xb5\x2f\xfd\x64\xa0\x01\x2d\x05\x00\xc4\x04\xcc\x11\x00\xd5" +
78 "\x13\x00\x1c\x14\x00\x72\x9d\xd5\xfb\x12\x00\x09\x0c\x13\xcb\x13" +
79 "\x29\x4e\x67\x5f\x0b\x6c\x0b\x7d\x0b\x7e\x0c\x38\x0f\x5c\x0f\x83" +
80 "\x0c\xfa\x0c\xfd\x0d\xef\x0e\x14\x38\x9f\x0f\xac\x0f\xdb\x0f\xff" +
81 "\x0f\xd8\x9f\xac\xdb\xff\xea\x5c\x2c\x10\x60\xd1\x16\x40\x0b\x7a" +
82 "\x00\xb6\x00\x9f\x01\xa7\x01\xa9\x36\x20\xa0\x83\x14\x34\x63\x4a" +
83 "\x21\x70\x8c\x07\x46\x03\x4e\x10\x62\x3c\x06\x4e\xc8\x8c\xb0\x32" +
84 "\x2a\x59\xad\xb2\xf1\x02\x82\x7c\x33\xcb\x92\x6f\x32\x4f\x9b\xb0" +
85 "\xa2\x30\xf0\xc0\x06\x1e\x98\x99\x2c\x06\x1e\xd8\xc0\x03\x56\xd8" +
86 "\xc0\x03\x0f\x6c\xe0\x01\xf1\xf0\xee\x9a\xc6\xc8\x97\x99\xd1\x6c" +
87 "\xb4\x21\x45\x3b\x10\xe4\x7b\x99\x4d\x8a\x36\x64\x5c\x77\x08\x02" +
88 "\xcb\xe0\xce",
89 },
90 {
91 "fuzz1",
92 "0\x00\x00\x00\x00\x000\x00\x00\x00\x00\x001\x00\x00\x00\x00\x000000",
93 "(\xb5/\xfd\x04X\x8d\x00\x00P0\x000\x001\x000000\x03T\x02\x00\x01\x01m\xf9\xb7G",
94 },
95 {
96 "empty block",
97 "",
98 "\x28\xb5\x2f\xfd\x00\x00\x15\x00\x00\x00\x00",
99 },
100 {
101 "single skippable frame",
102 "",
103 "\x50\x2a\x4d\x18\x00\x00\x00\x00",
104 },
105 {
106 "two skippable frames",
107 "",
108 "\x50\x2a\x4d\x18\x00\x00\x00\x00" +
109 "\x50\x2a\x4d\x18\x00\x00\x00\x00",
110 },
111 }
112
113 func TestSamples(t *testing.T) {
114 for _, test := range tests {
115 t.Run(test.name, func(t *testing.T) {
116 r := NewReader(strings.NewReader(test.compressed))
117 got, err := io.ReadAll(r)
118 if err != nil {
119 t.Fatal(err)
120 }
121 gotstr := string(got)
122 if gotstr != test.uncompressed {
123 t.Errorf("got %q want %q", gotstr, test.uncompressed)
124 }
125 })
126 }
127 }
128
129 func TestReset(t *testing.T) {
130 input := strings.NewReader("")
131 r := NewReader(input)
132 for _, test := range tests {
133 t.Run(test.name, func(t *testing.T) {
134 input.Reset(test.compressed)
135 r.Reset(input)
136 got, err := io.ReadAll(r)
137 if err != nil {
138 t.Fatal(err)
139 }
140 gotstr := string(got)
141 if gotstr != test.uncompressed {
142 t.Errorf("got %q want %q", gotstr, test.uncompressed)
143 }
144 })
145 }
146 }
147
148 var (
149 bigDataOnce sync.Once
150 bigDataBytes []byte
151 bigDataErr error
152 )
153
154
155 func bigData(t testing.TB) []byte {
156 bigDataOnce.Do(func() {
157 bigDataBytes, bigDataErr = os.ReadFile("../../testdata/Isaac.Newton-Opticks.txt")
158 if bigDataErr == nil {
159 bigDataBytes = bytes.Repeat(bigDataBytes, 20)
160 }
161 })
162 if bigDataErr != nil {
163 t.Fatal(bigDataErr)
164 }
165 return bigDataBytes
166 }
167
168 func findZstd(t testing.TB) string {
169 zstd, err := exec.LookPath("zstd")
170 if err != nil {
171 t.Skip("skipping because zstd not found")
172 }
173 return zstd
174 }
175
176 var (
177 zstdBigOnce sync.Once
178 zstdBigBytes []byte
179 zstdBigErr error
180 )
181
182
183
184
185 func zstdBigData(t testing.TB) []byte {
186 input := bigData(t)
187
188 zstd := findZstd(t)
189
190 zstdBigOnce.Do(func() {
191 cmd := exec.Command(zstd, "-z")
192 cmd.Stdin = bytes.NewReader(input)
193 var compressed bytes.Buffer
194 cmd.Stdout = &compressed
195 cmd.Stderr = os.Stderr
196 if err := cmd.Run(); err != nil {
197 zstdBigErr = fmt.Errorf("running zstd failed: %v", err)
198 return
199 }
200
201 zstdBigBytes = compressed.Bytes()
202 })
203 if zstdBigErr != nil {
204 t.Fatal(zstdBigErr)
205 }
206 return zstdBigBytes
207 }
208
209
210
211 func TestLarge(t *testing.T) {
212 if testing.Short() {
213 t.Skip("skipping expensive test in short mode")
214 }
215
216 data := bigData(t)
217 compressed := zstdBigData(t)
218
219 t.Logf("zstd compressed %d bytes to %d", len(data), len(compressed))
220
221 r := NewReader(bytes.NewReader(compressed))
222 got, err := io.ReadAll(r)
223 if err != nil {
224 t.Fatal(err)
225 }
226
227 if !bytes.Equal(got, data) {
228 showDiffs(t, got, data)
229 }
230 }
231
232
233 func showDiffs(t *testing.T, got, want []byte) {
234 t.Error("data mismatch")
235 if len(got) != len(want) {
236 t.Errorf("got data length %d, want %d", len(got), len(want))
237 }
238 diffs := 0
239 for i, b := range got {
240 if i >= len(want) {
241 break
242 }
243 if b != want[i] {
244 diffs++
245 if diffs > 20 {
246 break
247 }
248 t.Logf("%d: %#x != %#x", i, b, want[i])
249 }
250 }
251 }
252
253 func TestAlloc(t *testing.T) {
254 testenv.SkipIfOptimizationOff(t)
255 if race.Enabled {
256 t.Skip("skipping allocation test under race detector")
257 }
258
259 compressed := zstdBigData(t)
260 input := bytes.NewReader(compressed)
261 r := NewReader(input)
262 c := testing.AllocsPerRun(10, func() {
263 input.Reset(compressed)
264 r.Reset(input)
265 io.Copy(io.Discard, r)
266 })
267 if c != 0 {
268 t.Errorf("got %v allocs, want 0", c)
269 }
270 }
271
272 func TestFileSamples(t *testing.T) {
273 samples, err := os.ReadDir("testdata")
274 if err != nil {
275 t.Fatal(err)
276 }
277
278 for _, sample := range samples {
279 name := sample.Name()
280 if !strings.HasSuffix(name, ".zst") {
281 continue
282 }
283
284 t.Run(name, func(t *testing.T) {
285 f, err := os.Open(filepath.Join("testdata", name))
286 if err != nil {
287 t.Fatal(err)
288 }
289
290 r := NewReader(f)
291 h := sha256.New()
292 if _, err := io.Copy(h, r); err != nil {
293 t.Fatal(err)
294 }
295 got := fmt.Sprintf("%x", h.Sum(nil))[:8]
296
297 want, _, _ := strings.Cut(name, ".")
298 if got != want {
299 t.Errorf("Wrong uncompressed content hash: got %s, want %s", got, want)
300 }
301 })
302 }
303 }
304
305 func TestReaderBad(t *testing.T) {
306 for i, s := range badStrings {
307 t.Run(fmt.Sprintf("badStrings#%d", i), func(t *testing.T) {
308 _, err := io.Copy(io.Discard, NewReader(strings.NewReader(s)))
309 if err == nil {
310 t.Error("expected error")
311 }
312 })
313 }
314 }
315
316 func BenchmarkLarge(b *testing.B) {
317 b.StopTimer()
318 b.ReportAllocs()
319
320 compressed := zstdBigData(b)
321
322 b.SetBytes(int64(len(compressed)))
323
324 input := bytes.NewReader(compressed)
325 r := NewReader(input)
326
327 b.StartTimer()
328 for i := 0; i < b.N; i++ {
329 input.Reset(compressed)
330 r.Reset(input)
331 io.Copy(io.Discard, r)
332 }
333 }
334
View as plain text