1 // Copyright 2023 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.
22 // tests holds some simple test cases, including some found by fuzzing.
23 var tests = []struct {
24 name, uncompressed, compressed string
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",
32 // a small compressed .debug_ranges section.
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",
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" +
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",
98 "\x28\xb5\x2f\xfd\x00\x00\x15\x00\x00\x00\x00",
101 "single skippable frame",
103 "\x50\x2a\x4d\x18\x00\x00\x00\x00",
106 "two skippable frames",
108 "\x50\x2a\x4d\x18\x00\x00\x00\x00" +
109 "\x50\x2a\x4d\x18\x00\x00\x00\x00",
113 func TestSamples(t *testing.T) {
114 for _, test := range tests {
116 t.Run(test.name, func(t *testing.T) {
117 r := NewReader(strings.NewReader(test.compressed))
118 got, err := io.ReadAll(r)
122 gotstr := string(got)
123 if gotstr != test.uncompressed {
124 t.Errorf("got %q want %q", gotstr, test.uncompressed)
130 func TestReset(t *testing.T) {
131 input := strings.NewReader("")
132 r := NewReader(input)
133 for _, test := range tests {
135 t.Run(test.name, func(t *testing.T) {
136 input.Reset(test.compressed)
138 got, err := io.ReadAll(r)
142 gotstr := string(got)
143 if gotstr != test.uncompressed {
144 t.Errorf("got %q want %q", gotstr, test.uncompressed)
151 bigDataOnce sync.Once
156 // bigData returns the contents of our large test file repeated multiple times.
157 func bigData(t testing.TB) []byte {
158 bigDataOnce.Do(func() {
159 bigDataBytes, bigDataErr = os.ReadFile("../../testdata/Isaac.Newton-Opticks.txt")
160 if bigDataErr == nil {
161 bigDataBytes = bytes.Repeat(bigDataBytes, 20)
164 if bigDataErr != nil {
170 func findZstd(t testing.TB) string {
171 zstd, err := exec.LookPath("zstd")
173 t.Skip("skipping because zstd not found")
179 zstdBigOnce sync.Once
184 // zstdBigData returns the compressed contents of our large test file.
185 // This will only run on Unix systems with zstd installed.
186 // That's OK as the package is GOOS-independent.
187 func zstdBigData(t testing.TB) []byte {
192 zstdBigOnce.Do(func() {
193 cmd := exec.Command(zstd, "-z")
194 cmd.Stdin = bytes.NewReader(input)
195 var compressed bytes.Buffer
196 cmd.Stdout = &compressed
197 cmd.Stderr = os.Stderr
198 if err := cmd.Run(); err != nil {
199 zstdBigErr = fmt.Errorf("running zstd failed: %v", err)
203 zstdBigBytes = compressed.Bytes()
205 if zstdBigErr != nil {
211 // Test decompressing a large file. We don't have a compressor,
212 // so this test only runs on systems with zstd installed.
213 func TestLarge(t *testing.T) {
215 t.Skip("skipping expensive test in short mode")
219 compressed := zstdBigData(t)
221 t.Logf("zstd compressed %d bytes to %d", len(data), len(compressed))
223 r := NewReader(bytes.NewReader(compressed))
224 got, err := io.ReadAll(r)
229 if !bytes.Equal(got, data) {
230 showDiffs(t, got, data)
234 // showDiffs reports the first few differences in two []byte.
235 func showDiffs(t *testing.T, got, want []byte) {
236 t.Error("data mismatch")
237 if len(got) != len(want) {
238 t.Errorf("got data length %d, want %d", len(got), len(want))
241 for i, b := range got {
250 t.Logf("%d: %#x != %#x", i, b, want[i])
255 func TestAlloc(t *testing.T) {
256 testenv.SkipIfOptimizationOff(t)
258 t.Skip("skipping allocation test under race detector")
261 compressed := zstdBigData(t)
262 input := bytes.NewReader(compressed)
263 r := NewReader(input)
264 c := testing.AllocsPerRun(10, func() {
265 input.Reset(compressed)
267 io.Copy(io.Discard, r)
270 t.Errorf("got %v allocs, want 0", c)
274 func TestFileSamples(t *testing.T) {
275 samples, err := os.ReadDir("testdata")
280 for _, sample := range samples {
281 name := sample.Name()
282 if !strings.HasSuffix(name, ".zst") {
286 t.Run(name, func(t *testing.T) {
287 f, err := os.Open(filepath.Join("testdata", name))
294 if _, err := io.Copy(h, r); err != nil {
297 got := fmt.Sprintf("%x", h.Sum(nil))[:8]
299 want, _, _ := strings.Cut(name, ".")
301 t.Errorf("Wrong uncompressed content hash: got %s, want %s", got, want)
307 func BenchmarkLarge(b *testing.B) {
311 compressed := zstdBigData(b)
313 b.SetBytes(int64(len(compressed)))
315 input := bytes.NewReader(compressed)
316 r := NewReader(input)
319 for i := 0; i < b.N; i++ {
320 input.Reset(compressed)
322 io.Copy(io.Discard, r)