]> Cypherpunks.ru repositories - gostls13.git/blob - src/internal/zstd/zstd_test.go
internal/zstd: allow empty compressed blocks
[gostls13.git] / src / internal / zstd / zstd_test.go
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.
4
5 package zstd
6
7 import (
8         "bytes"
9         "fmt"
10         "internal/race"
11         "internal/testenv"
12         "io"
13         "os"
14         "os/exec"
15         "strings"
16         "sync"
17         "testing"
18 )
19
20 // tests holds some simple test cases, including some found by fuzzing.
21 var tests = []struct {
22         name, uncompressed, compressed string
23 }{
24         {
25                 "hello",
26                 "hello, world\n",
27                 "\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",
28         },
29         {
30                 // a small compressed .debug_ranges section.
31                 "ranges",
32                 "\xcc\x11\x00\x00\x00\x00\x00\x00\xd5\x13\x00\x00\x00\x00\x00\x00" +
33                         "\x1c\x14\x00\x00\x00\x00\x00\x00\x72\x14\x00\x00\x00\x00\x00\x00" +
34                         "\x9d\x14\x00\x00\x00\x00\x00\x00\xd5\x14\x00\x00\x00\x00\x00\x00" +
35                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
36                         "\xfb\x12\x00\x00\x00\x00\x00\x00\x09\x13\x00\x00\x00\x00\x00\x00" +
37                         "\x0c\x13\x00\x00\x00\x00\x00\x00\xcb\x13\x00\x00\x00\x00\x00\x00" +
38                         "\x29\x14\x00\x00\x00\x00\x00\x00\x4e\x14\x00\x00\x00\x00\x00\x00" +
39                         "\x9d\x14\x00\x00\x00\x00\x00\x00\xd5\x14\x00\x00\x00\x00\x00\x00" +
40                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
41                         "\xfb\x12\x00\x00\x00\x00\x00\x00\x09\x13\x00\x00\x00\x00\x00\x00" +
42                         "\x67\x13\x00\x00\x00\x00\x00\x00\xcb\x13\x00\x00\x00\x00\x00\x00" +
43                         "\x9d\x14\x00\x00\x00\x00\x00\x00\xd5\x14\x00\x00\x00\x00\x00\x00" +
44                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
45                         "\x5f\x0b\x00\x00\x00\x00\x00\x00\x6c\x0b\x00\x00\x00\x00\x00\x00" +
46                         "\x7d\x0b\x00\x00\x00\x00\x00\x00\x7e\x0c\x00\x00\x00\x00\x00\x00" +
47                         "\x38\x0f\x00\x00\x00\x00\x00\x00\x5c\x0f\x00\x00\x00\x00\x00\x00" +
48                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
49                         "\x83\x0c\x00\x00\x00\x00\x00\x00\xfa\x0c\x00\x00\x00\x00\x00\x00" +
50                         "\xfd\x0d\x00\x00\x00\x00\x00\x00\xef\x0e\x00\x00\x00\x00\x00\x00" +
51                         "\x14\x0f\x00\x00\x00\x00\x00\x00\x38\x0f\x00\x00\x00\x00\x00\x00" +
52                         "\x9f\x0f\x00\x00\x00\x00\x00\x00\xac\x0f\x00\x00\x00\x00\x00\x00" +
53                         "\xdb\x0f\x00\x00\x00\x00\x00\x00\xff\x0f\x00\x00\x00\x00\x00\x00" +
54                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
55                         "\xfd\x0d\x00\x00\x00\x00\x00\x00\xd8\x0e\x00\x00\x00\x00\x00\x00" +
56                         "\x9f\x0f\x00\x00\x00\x00\x00\x00\xac\x0f\x00\x00\x00\x00\x00\x00" +
57                         "\xdb\x0f\x00\x00\x00\x00\x00\x00\xff\x0f\x00\x00\x00\x00\x00\x00" +
58                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
59                         "\xfa\x0c\x00\x00\x00\x00\x00\x00\xea\x0d\x00\x00\x00\x00\x00\x00" +
60                         "\xef\x0e\x00\x00\x00\x00\x00\x00\x14\x0f\x00\x00\x00\x00\x00\x00" +
61                         "\x5c\x0f\x00\x00\x00\x00\x00\x00\x9f\x0f\x00\x00\x00\x00\x00\x00" +
62                         "\xac\x0f\x00\x00\x00\x00\x00\x00\xdb\x0f\x00\x00\x00\x00\x00\x00" +
63                         "\xff\x0f\x00\x00\x00\x00\x00\x00\x2c\x10\x00\x00\x00\x00\x00\x00" +
64                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
65                         "\x60\x11\x00\x00\x00\x00\x00\x00\xd1\x16\x00\x00\x00\x00\x00\x00" +
66                         "\x40\x0b\x00\x00\x00\x00\x00\x00\x2c\x10\x00\x00\x00\x00\x00\x00" +
67                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
68                         "\x7a\x00\x00\x00\x00\x00\x00\x00\xb6\x00\x00\x00\x00\x00\x00\x00" +
69                         "\x9f\x01\x00\x00\x00\x00\x00\x00\xa7\x01\x00\x00\x00\x00\x00\x00" +
70                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" +
71                         "\x7a\x00\x00\x00\x00\x00\x00\x00\xa9\x00\x00\x00\x00\x00\x00\x00" +
72                         "\x9f\x01\x00\x00\x00\x00\x00\x00\xa7\x01\x00\x00\x00\x00\x00\x00" +
73                         "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00",
74
75                 "\x28\xb5\x2f\xfd\x64\xa0\x01\x2d\x05\x00\xc4\x04\xcc\x11\x00\xd5" +
76                         "\x13\x00\x1c\x14\x00\x72\x9d\xd5\xfb\x12\x00\x09\x0c\x13\xcb\x13" +
77                         "\x29\x4e\x67\x5f\x0b\x6c\x0b\x7d\x0b\x7e\x0c\x38\x0f\x5c\x0f\x83" +
78                         "\x0c\xfa\x0c\xfd\x0d\xef\x0e\x14\x38\x9f\x0f\xac\x0f\xdb\x0f\xff" +
79                         "\x0f\xd8\x9f\xac\xdb\xff\xea\x5c\x2c\x10\x60\xd1\x16\x40\x0b\x7a" +
80                         "\x00\xb6\x00\x9f\x01\xa7\x01\xa9\x36\x20\xa0\x83\x14\x34\x63\x4a" +
81                         "\x21\x70\x8c\x07\x46\x03\x4e\x10\x62\x3c\x06\x4e\xc8\x8c\xb0\x32" +
82                         "\x2a\x59\xad\xb2\xf1\x02\x82\x7c\x33\xcb\x92\x6f\x32\x4f\x9b\xb0" +
83                         "\xa2\x30\xf0\xc0\x06\x1e\x98\x99\x2c\x06\x1e\xd8\xc0\x03\x56\xd8" +
84                         "\xc0\x03\x0f\x6c\xe0\x01\xf1\xf0\xee\x9a\xc6\xc8\x97\x99\xd1\x6c" +
85                         "\xb4\x21\x45\x3b\x10\xe4\x7b\x99\x4d\x8a\x36\x64\x5c\x77\x08\x02" +
86                         "\xcb\xe0\xce",
87         },
88         {
89                 "fuzz1",
90                 "0\x00\x00\x00\x00\x000\x00\x00\x00\x00\x001\x00\x00\x00\x00\x000000",
91                 "(\xb5/\xfd\x04X\x8d\x00\x00P0\x000\x001\x000000\x03T\x02\x00\x01\x01m\xf9\xb7G",
92         },
93         {
94                 "empty block",
95                 "",
96                 "\x28\xb5\x2f\xfd\x00\x00\x15\x00\x00\x00\x00",
97         },
98 }
99
100 func TestSamples(t *testing.T) {
101         for _, test := range tests {
102                 test := test
103                 t.Run(test.name, func(t *testing.T) {
104                         r := NewReader(strings.NewReader(test.compressed))
105                         got, err := io.ReadAll(r)
106                         if err != nil {
107                                 t.Fatal(err)
108                         }
109                         gotstr := string(got)
110                         if gotstr != test.uncompressed {
111                                 t.Errorf("got %q want %q", gotstr, test.uncompressed)
112                         }
113                 })
114         }
115 }
116
117 var (
118         bigDataOnce  sync.Once
119         bigDataBytes []byte
120         bigDataErr   error
121 )
122
123 // bigData returns the contents of our large test file repeated multiple times.
124 func bigData(t testing.TB) []byte {
125         bigDataOnce.Do(func() {
126                 bigDataBytes, bigDataErr = os.ReadFile("../../testdata/Isaac.Newton-Opticks.txt")
127                 if bigDataErr == nil {
128                         bigDataBytes = bytes.Repeat(bigDataBytes, 20)
129                 }
130         })
131         if bigDataErr != nil {
132                 t.Fatal(bigDataErr)
133         }
134         return bigDataBytes
135 }
136
137 var (
138         zstdBigOnce  sync.Once
139         zstdBigBytes []byte
140         zstdBigSkip  bool
141         zstdBigErr   error
142 )
143
144 // zstdBigData returns the compressed contents of our large test file.
145 // This will only run on Unix systems with zstd installed.
146 // That's OK as the package is GOOS-independent.
147 func zstdBigData(t testing.TB) []byte {
148         input := bigData(t)
149
150         zstdBigOnce.Do(func() {
151                 if _, err := os.Stat("/usr/bin/zstd"); err != nil {
152                         zstdBigSkip = true
153                         return
154                 }
155
156                 cmd := exec.Command("/usr/bin/zstd", "-z")
157                 cmd.Stdin = bytes.NewReader(input)
158                 var compressed bytes.Buffer
159                 cmd.Stdout = &compressed
160                 cmd.Stderr = os.Stderr
161                 if err := cmd.Run(); err != nil {
162                         zstdBigErr = fmt.Errorf("running zstd failed: %v", err)
163                         return
164                 }
165
166                 zstdBigBytes = compressed.Bytes()
167         })
168         if zstdBigSkip {
169                 t.Skip("skipping because /usr/bin/zstd does not exist")
170         }
171         if zstdBigErr != nil {
172                 t.Fatal(zstdBigErr)
173         }
174         return zstdBigBytes
175 }
176
177 // Test decompressing a large file. We don't have a compressor,
178 // so this test only runs on systems with zstd installed.
179 func TestLarge(t *testing.T) {
180         if testing.Short() {
181                 t.Skip("skipping expensive test in short mode")
182         }
183
184         data := bigData(t)
185         compressed := zstdBigData(t)
186
187         t.Logf("/usr/bin/zstd compressed %d bytes to %d", len(data), len(compressed))
188
189         r := NewReader(bytes.NewReader(compressed))
190         got, err := io.ReadAll(r)
191         if err != nil {
192                 t.Fatal(err)
193         }
194
195         if !bytes.Equal(got, data) {
196                 showDiffs(t, got, data)
197         }
198 }
199
200 // showDiffs reports the first few differences in two []byte.
201 func showDiffs(t *testing.T, got, want []byte) {
202         t.Error("data mismatch")
203         if len(got) != len(want) {
204                 t.Errorf("got data length %d, want %d", len(got), len(want))
205         }
206         diffs := 0
207         for i, b := range got {
208                 if i >= len(want) {
209                         break
210                 }
211                 if b != want[i] {
212                         diffs++
213                         if diffs > 20 {
214                                 break
215                         }
216                         t.Logf("%d: %#x != %#x", i, b, want[i])
217                 }
218         }
219 }
220
221 func TestAlloc(t *testing.T) {
222         testenv.SkipIfOptimizationOff(t)
223         if race.Enabled {
224                 t.Skip("skipping allocation test under race detector")
225         }
226
227         compressed := zstdBigData(t)
228         input := bytes.NewReader(compressed)
229         r := NewReader(input)
230         c := testing.AllocsPerRun(10, func() {
231                 input.Reset(compressed)
232                 r.Reset(input)
233                 io.Copy(io.Discard, r)
234         })
235         if c != 0 {
236                 t.Errorf("got %v allocs, want 0", c)
237         }
238 }
239
240 func BenchmarkLarge(b *testing.B) {
241         b.StopTimer()
242         b.ReportAllocs()
243
244         compressed := zstdBigData(b)
245
246         b.SetBytes(int64(len(compressed)))
247
248         input := bytes.NewReader(compressed)
249         r := NewReader(input)
250
251         b.StartTimer()
252         for i := 0; i < b.N; i++ {
253                 input.Reset(compressed)
254                 r.Reset(input)
255                 io.Copy(io.Discard, r)
256         }
257 }