forked from DataDog/zstd
-
Notifications
You must be signed in to change notification settings - Fork 1
/
zstd_bullk_test.go
277 lines (243 loc) · 7.18 KB
/
zstd_bullk_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
package zstd
import (
"bytes"
"encoding/base64"
"math/rand"
"regexp"
"strings"
"testing"
)
var dictBase64 string = `
N6Qw7IsuFDIdENCSQjr//////4+QlekuNkmXbUBIkIDiVRX7H4AzAFCgQCFCO9oHAAAEQEuSikaK
Dg51OYghBYgBAAAAAAAAAAAAAAAAAAAAANQVpmRQGQAAAAAAAAAAAAAAAAABAAAABAAAAAgAAABo
ZWxwIEpvaW4gZW5naW5lZXJzIGVuZ2luZWVycyBmdXR1cmUgbG92ZSB0aGF0IGFyZWlsZGluZyB1
c2UgaGVscCBoZWxwIHVzaGVyIEpvaW4gdXNlIGxvdmUgdXMgSm9pbiB1bmQgaW4gdXNoZXIgdXNo
ZXIgYSBwbGF0Zm9ybSB1c2UgYW5kIGZ1dHVyZQ==`
var dict []byte
var compressedPayload []byte
func init() {
var err error
dict, err = base64.StdEncoding.DecodeString(regexp.MustCompile(`\s+`).ReplaceAllString(dictBase64, ""))
if err != nil {
panic("failed to create dictionary")
}
p, err := NewBulkProcessor(dict, BestSpeed)
if err != nil {
panic("failed to create bulk processor")
}
compressedPayload, err = p.Compress(nil, []byte("We're building a platform that engineers love to use. Join us, and help usher in the future."))
if err != nil {
panic("failed to compress payload")
}
}
func newBulkProcessor(t testing.TB, dict []byte, level int) *BulkProcessor {
p, err := NewBulkProcessor(dict, level)
if err != nil {
t.Fatal("failed to create a BulkProcessor")
}
return p
}
func getRandomText() string {
words := []string{"We", "are", "building", "a platform", "that", "engineers", "love", "to", "use", "Join", "us", "and", "help", "usher", "in", "the", "future"}
wordCount := 10 + rand.Intn(100) // 10 - 109
result := []string{}
for i := 0; i < wordCount; i++ {
result = append(result, words[rand.Intn(len(words))])
}
return strings.Join(result, " ")
}
func TestBulkDictionary(t *testing.T) {
if len(dict) < 1 {
t.Error("dictionary is empty")
}
}
func TestBulkCompressAndDecompress(t *testing.T) {
p := newBulkProcessor(t, dict, BestSpeed)
for i := 0; i < 100; i++ {
payload := []byte(getRandomText())
compressed, err := p.Compress(nil, payload)
if err != nil {
t.Error("failed to compress")
}
uncompressed, err := p.Decompress(nil, compressed)
if err != nil {
t.Error("failed to decompress")
}
if bytes.Compare(payload, uncompressed) != 0 {
t.Error("uncompressed payload didn't match")
}
}
}
func TestBulkEmptyOrNilDictionary(t *testing.T) {
p, err := NewBulkProcessor(nil, BestSpeed)
if p != nil {
t.Error("nil is expected")
}
if err != ErrEmptyDictionary {
t.Error("ErrEmptyDictionary is expected")
}
p, err = NewBulkProcessor([]byte{}, BestSpeed)
if p != nil {
t.Error("nil is expected")
}
if err != ErrEmptyDictionary {
t.Error("ErrEmptyDictionary is expected")
}
}
func TestBulkCompressDecompressEmptyOrNilContent(t *testing.T) {
p := newBulkProcessor(t, dict, BestSpeed)
compressed, err := p.Compress(nil, nil)
if err != nil {
t.Error("failed to compress")
}
if len(compressed) < 4 {
t.Error("magic number doesn't exist")
}
compressed, err = p.Compress(nil, []byte{})
if err != nil {
t.Error("failed to compress")
}
if len(compressed) < 4 {
t.Error("magic number doesn't exist")
}
decompressed, err := p.Decompress(nil, compressed)
if err != nil {
t.Error("failed to decompress")
}
if len(decompressed) != 0 {
t.Error("content was not decompressed correctly")
}
}
func TestBulkCompressIntoGivenDestination(t *testing.T) {
p := newBulkProcessor(t, dict, BestSpeed)
dst := make([]byte, 100000)
compressed, err := p.Compress(dst, []byte(getRandomText()))
if err != nil {
t.Error("failed to compress")
}
if len(compressed) < 4 {
t.Error("magic number doesn't exist")
}
if &dst[0] != &compressed[0] {
t.Error("'dst' and 'compressed' are not the same object")
}
}
func TestBulkCompressNotEnoughDestination(t *testing.T) {
p := newBulkProcessor(t, dict, BestSpeed)
dst := make([]byte, 1)
compressed, err := p.Compress(dst, []byte(getRandomText()))
if err != nil {
t.Error("failed to compress")
}
if len(compressed) < 4 {
t.Error("magic number doesn't exist")
}
if &dst[0] == &compressed[0] {
t.Error("'dst' and 'compressed' are the same object")
}
}
func TestBulkDecompressIntoGivenDestination(t *testing.T) {
p := newBulkProcessor(t, dict, BestSpeed)
dst := make([]byte, 100000)
decompressed, err := p.Decompress(dst, compressedPayload)
if err != nil {
t.Error("failed to decompress")
}
if &dst[0] != &decompressed[0] {
t.Error("'dst' and 'decompressed' are not the same object")
}
}
func TestBulkDecompressNotEnoughDestination(t *testing.T) {
p := newBulkProcessor(t, dict, BestSpeed)
dst := make([]byte, 1)
decompressed, err := p.Decompress(dst, compressedPayload)
if err != nil {
t.Error("failed to decompress")
}
if &dst[0] == &decompressed[0] {
t.Error("'dst' and 'decompressed' are the same object")
}
}
func TestBulkDecompressEmptyOrNilContent(t *testing.T) {
p := newBulkProcessor(t, dict, BestSpeed)
decompressed, err := p.Decompress(nil, nil)
if err != ErrEmptySlice {
t.Error("ErrEmptySlice is expected")
}
if decompressed != nil {
t.Error("nil is expected")
}
decompressed, err = p.Decompress(nil, []byte{})
if err != ErrEmptySlice {
t.Error("ErrEmptySlice is expected")
}
if decompressed != nil {
t.Error("nil is expected")
}
}
func TestBulkCompressAndDecompressInReverseOrder(t *testing.T) {
p := newBulkProcessor(t, dict, BestSpeed)
payloads := [][]byte{}
compressedPayloads := [][]byte{}
for i := 0; i < 100; i++ {
payloads = append(payloads, []byte(getRandomText()))
compressed, err := p.Compress(nil, payloads[i])
if err != nil {
t.Error("failed to compress")
}
compressedPayloads = append(compressedPayloads, compressed)
}
for i := 99; i >= 0; i-- {
uncompressed, err := p.Decompress(nil, compressedPayloads[i])
if err != nil {
t.Error("failed to decompress")
}
if bytes.Compare(payloads[i], uncompressed) != 0 {
t.Error("uncompressed payload didn't match")
}
}
}
func TestBulkDecompressHighlyCompressable(t *testing.T) {
p := newBulkProcessor(t, dict, BestSpeed)
// Generate a big payload
msgSize := 10 * 1000 * 1000 // 10 MiB
msg := make([]byte, msgSize)
compressed, err := Compress(nil, msg)
if err != nil {
t.Error("failed to compress")
}
// Regular decompression would trigger zipbomb prevention
_, err = p.Decompress(nil, compressed)
if !IsDstSizeTooSmallError(err) {
t.Error("expected too small error")
}
// Passing an output should suceed the decompression
dst := make([]byte, 10*msgSize)
_, err = p.Decompress(dst, compressed)
if err != nil {
t.Errorf("failed to decompress: %s", err)
}
}
// BenchmarkBulkCompress-8 780148 1505 ns/op 61.14 MB/s 208 B/op 5 allocs/op
func BenchmarkBulkCompress(b *testing.B) {
p := newBulkProcessor(b, dict, BestSpeed)
payload := []byte("We're building a platform that engineers love to use. Join us, and help usher in the future.")
b.SetBytes(int64(len(payload)))
for n := 0; n < b.N; n++ {
_, err := p.Compress(nil, payload)
if err != nil {
b.Error("failed to compress")
}
}
}
// BenchmarkBulkDecompress-8 817425 1412 ns/op 40.37 MB/s 192 B/op 7 allocs/op
func BenchmarkBulkDecompress(b *testing.B) {
p := newBulkProcessor(b, dict, BestSpeed)
b.SetBytes(int64(len(compressedPayload)))
for n := 0; n < b.N; n++ {
_, err := p.Decompress(nil, compressedPayload)
if err != nil {
b.Error("failed to decompress")
}
}
}