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
|
// Copyright (c) 2019 Klaus Post. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package s2
import (
"bytes"
"fmt"
"io"
"os"
"runtime"
"strings"
"testing"
"github.com/klauspost/compress/zip"
)
func TestDecodeRegression(t *testing.T) {
data, err := os.ReadFile("testdata/dec-block-regressions.zip")
if err != nil {
t.Fatal(err)
}
zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
t.Fatal(err)
}
for _, tt := range zr.File {
if !strings.HasSuffix(t.Name(), "") {
continue
}
t.Run(tt.Name, func(t *testing.T) {
r, err := tt.Open()
if err != nil {
t.Error(err)
return
}
in, err := io.ReadAll(r)
if err != nil {
t.Error(err)
}
got, err := Decode(nil, in)
t.Log("Received:", len(got), err)
})
}
}
func TestDecoderMaxBlockSize(t *testing.T) {
data, err := os.ReadFile("testdata/enc_regressions.zip")
if err != nil {
t.Fatal(err)
}
zr, err := zip.NewReader(bytes.NewReader(data), int64(len(data)))
if err != nil {
t.Fatal(err)
}
sizes := []int{4 << 10, 10 << 10, 1 << 20, 4 << 20}
test := func(t *testing.T, data []byte) {
for _, size := range sizes {
t.Run(fmt.Sprintf("%d", size), func(t *testing.T) {
var buf bytes.Buffer
dec := NewReader(nil, ReaderMaxBlockSize(size), ReaderAllocBlock(size/2))
enc := NewWriter(&buf, WriterBlockSize(size), WriterPadding(16<<10), WriterPaddingSrc(zeroReader{}))
// Test writer.
n, err := enc.Write(data)
if err != nil {
t.Error(err)
return
}
if n != len(data) {
t.Error(fmt.Errorf("Write: Short write, want %d, got %d", len(data), n))
return
}
err = enc.Close()
if err != nil {
t.Error(err)
return
}
// Calling close twice should not affect anything.
err = enc.Close()
if err != nil {
t.Error(err)
return
}
dec.Reset(&buf)
got, err := io.ReadAll(dec)
if err != nil {
t.Error(err)
return
}
if !bytes.Equal(data, got) {
t.Error("block (reset) decoder mismatch")
return
}
// Test Reset on both and use ReadFrom instead.
buf.Reset()
enc.Reset(&buf)
n2, err := enc.ReadFrom(bytes.NewBuffer(data))
if err != nil {
t.Error(err)
return
}
if n2 != int64(len(data)) {
t.Error(fmt.Errorf("ReadFrom: Short read, want %d, got %d", len(data), n2))
return
}
// Encode twice...
n2, err = enc.ReadFrom(bytes.NewBuffer(data))
if err != nil {
t.Error(err)
return
}
if n2 != int64(len(data)) {
t.Error(fmt.Errorf("ReadFrom: Short read, want %d, got %d", len(data), n2))
return
}
err = enc.Close()
if err != nil {
t.Error(err)
return
}
if enc.pad > 0 && buf.Len()%enc.pad != 0 {
t.Error(fmt.Errorf("wanted size to be mutiple of %d, got size %d with remainder %d", enc.pad, buf.Len(), buf.Len()%enc.pad))
return
}
encoded := buf.Bytes()
dec.Reset(&buf)
// Skip first...
dec.Skip(int64(len(data)))
got, err = io.ReadAll(dec)
if err != nil {
t.Error(err)
return
}
if !bytes.Equal(data, got) {
t.Error("frame (reset) decoder mismatch")
return
}
// Re-add data, Read concurrent.
buf.Write(encoded)
dec.Reset(&buf)
var doubleB bytes.Buffer
nb, err := dec.DecodeConcurrent(&doubleB, runtime.GOMAXPROCS(0))
if err != nil {
t.Error(err)
return
}
if nb != int64(len(data)*2) {
t.Errorf("want %d, got %d, err: %v", len(data)*2, nb, err)
return
}
got = doubleB.Bytes()[:len(data)]
if !bytes.Equal(data, got) {
t.Error("frame (DecodeConcurrent) decoder mismatch")
return
}
got = doubleB.Bytes()[len(data):]
if !bytes.Equal(data, got) {
t.Error("frame (DecodeConcurrent) decoder mismatch")
return
}
})
}
}
for _, tt := range zr.File {
if !strings.HasSuffix(t.Name(), "") {
continue
}
t.Run(tt.Name, func(t *testing.T) {
r, err := tt.Open()
if err != nil {
t.Error(err)
return
}
b, err := io.ReadAll(r)
if err != nil {
t.Error(err)
return
}
test(t, b[:len(b):len(b)])
})
}
}
|