This repository has been archived by the owner on Nov 19, 2024. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 391
/
formats_test.go
459 lines (419 loc) · 12.6 KB
/
formats_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
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
package archiver
import (
"bytes"
"context"
"errors"
"io"
"io/fs"
"math/rand"
"os"
"strings"
"testing"
"time"
)
func TestRewindReader(t *testing.T) {
data := "the header\nthe body\n"
r, err := newRewindReader(strings.NewReader(data))
if err != nil {
t.Errorf("creating rewindReader: %v", err)
}
buf := make([]byte, 10) // enough for 'the header'
// test rewinding reads
for i := 0; i < 10; i++ {
r.rewind()
n, err := r.Read(buf)
if err != nil {
t.Errorf("Read failed: %s", err)
}
if string(buf[:n]) != "the header" {
t.Errorf("iteration %d: expected 'the header' but got '%s' (n=%d)", i, string(buf[:n]), n)
}
}
// get the reader from header reader and make sure we can read all of the data out
r.rewind()
finalReader := r.reader()
buf = make([]byte, len(data))
n, err := io.ReadFull(finalReader, buf)
if err != nil {
t.Errorf("ReadFull failed: %s (n=%d)", err, n)
}
if string(buf) != data {
t.Errorf("expected '%s' but got '%s'", string(data), string(buf))
}
}
func TestCompression(t *testing.T) {
seed := time.Now().UnixNano()
t.Logf("seed: %d", seed)
r := rand.New(rand.NewSource(seed))
contents := make([]byte, 1024)
r.Read(contents)
compressed := new(bytes.Buffer)
testOK := func(t *testing.T, comp Compression, testFilename string) {
// compress into buffer
compressed.Reset()
wc, err := comp.OpenWriter(compressed)
checkErr(t, err, "opening writer")
_, err = wc.Write(contents)
checkErr(t, err, "writing contents")
checkErr(t, wc.Close(), "closing writer")
// make sure Identify correctly chooses this compression method
format, stream, err := Identify(context.Background(), testFilename, compressed)
checkErr(t, err, "identifying")
if format.Extension() != comp.Extension() {
t.Errorf("expected format %s but got %s", comp.Extension(), format.Extension())
}
// read the contents back out and compare
decompReader, err := format.(Decompressor).OpenReader(stream)
checkErr(t, err, "opening with decompressor '%s'", format.Extension())
data, err := io.ReadAll(decompReader)
checkErr(t, err, "reading decompressed data")
checkErr(t, decompReader.Close(), "closing decompressor")
if !bytes.Equal(data, contents) {
t.Errorf("not equal to original")
}
}
var cannotIdentifyFromStream = map[string]bool{Brotli{}.Extension(): true}
for _, f := range formats {
// only test compressors
comp, ok := f.(Compression)
if !ok {
continue
}
t.Run(f.Extension()+"_with_extension", func(t *testing.T) {
testOK(t, comp, "file"+f.Extension())
})
if !cannotIdentifyFromStream[f.Extension()] {
t.Run(f.Extension()+"_without_extension", func(t *testing.T) {
testOK(t, comp, "")
})
}
}
}
func checkErr(t *testing.T, err error, msgFmt string, args ...any) {
t.Helper()
if err == nil {
return
}
args = append(args, err)
t.Fatalf(msgFmt+": %s", args...)
}
func TestIdentifyDoesNotMatchContentFromTrimmedKnownHeaderHaving0Suffix(t *testing.T) {
// Using the outcome of `n, err := io.ReadFull(stream, buf)` without minding n
// may lead to a mis-characterization for cases with known header ending with 0x0
// because the default byte value in a declared array is 0.
// This test guards against those cases.
tests := []struct {
name string
header []byte
}{
{
name: "rar_v5.0",
header: rarHeaderV5_0,
},
{
name: "rar_v1.5",
header: rarHeaderV1_5,
},
{
name: "xz",
header: xzHeader,
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
headerLen := len(tt.header)
if headerLen == 0 || tt.header[headerLen-1] != 0 {
t.Errorf("header expected to end with 0: header=%v", tt.header)
return
}
headerTrimmed := tt.header[:headerLen-1]
stream := bytes.NewReader(headerTrimmed)
got, _, err := Identify(context.Background(), "", stream)
if got != nil {
t.Errorf("no Format expected for trimmed know %s header: found Format= %v", tt.name, got.Extension())
return
}
if !errors.Is(err, NoMatch) {
t.Errorf("NoMatch expected for for trimmed know %s header: err :=%#v", tt.name, err)
return
}
})
}
}
func TestIdentifyCanAssessSmallOrNoContent(t *testing.T) {
type args struct {
stream io.ReadSeeker
}
tests := []struct {
name string
args args
}{
{
name: "should return nomatch for an empty stream",
args: args{
stream: bytes.NewReader([]byte{}),
},
},
{
name: "should return nomatch for a stream with content size less than known header",
args: args{
stream: bytes.NewReader([]byte{'a'}),
},
},
{
name: "should return nomatch for a stream with content size greater then known header size and not supported format",
args: args{
stream: bytes.NewReader([]byte(strings.Repeat("this is a txt content", 2))),
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
got, _, err := Identify(context.Background(), "", tt.args.stream)
if got != nil {
t.Errorf("no Format expected for non archive and not compressed stream: found Format=%#v", got)
return
}
if !errors.Is(err, NoMatch) {
t.Errorf("NoMatch expected for non archive and not compressed stream: %#v", err)
return
}
})
}
}
func compress(
t *testing.T, compName string, content []byte,
openwriter func(w io.Writer) (io.WriteCloser, error),
) []byte {
buf := bytes.NewBuffer(make([]byte, 0, 128))
cwriter, err := openwriter(buf)
if err != nil {
t.Errorf("fail to open compression writer: compression-name=%s, err=%#v", compName, err)
return nil
}
_, err = cwriter.Write(content)
if err != nil {
cerr := cwriter.Close()
t.Errorf(
"fail to write using compression writer: compression-name=%s, err=%#v, close-err=%#v",
compName, err, cerr)
return nil
}
err = cwriter.Close()
if err != nil {
t.Errorf("fail to close compression writer: compression-name=%s, err=%#v", compName, err)
return nil
}
return buf.Bytes()
}
func archive(t *testing.T, arch Archiver, fname string, fileInfo fs.FileInfo) []byte {
files := []FileInfo{
{FileInfo: fileInfo, NameInArchive: "tmp.txt",
Open: func() (fs.File, error) {
return os.Open(fname)
}},
}
buf := bytes.NewBuffer(make([]byte, 0, 128))
err := arch.Archive(context.TODO(), buf, files)
if err != nil {
t.Errorf("fail to create archive: err=%#v", err)
return nil
}
return buf.Bytes()
}
type writeNopCloser struct{ io.Writer }
func (wnc writeNopCloser) Close() error { return nil }
func newWriteNopCloser(w io.Writer) (io.WriteCloser, error) {
return writeNopCloser{w}, nil
}
func newTmpTextFile(t *testing.T, content string) (string, fs.FileInfo) {
tmpTxtFile, err := os.CreateTemp("", "TestIdentifyFindFormatByStreamContent-tmp-*.txt")
if err != nil {
t.Errorf("fail to create tmp test file for archive tests: err=%v", err)
return "", nil
}
fname := tmpTxtFile.Name()
if _, err = tmpTxtFile.Write([]byte(content)); err != nil {
t.Errorf("fail to write content to tmp-txt-file: err=%#v", err)
return "", nil
}
if err = tmpTxtFile.Close(); err != nil {
t.Errorf("fail to close tmp-txt-file: err=%#v", err)
return "", nil
}
fi, err := os.Stat(fname)
if err != nil {
t.Errorf("fail to get tmp-txt-file stats: err=%v", err)
return "", nil
}
return fname, fi
}
func TestIdentifyFindFormatByStreamContent(t *testing.T) {
tmpTxtFileName, tmpTxtFileInfo := newTmpTextFile(t, "this is text that has to be long enough for brotli to match")
t.Cleanup(func() {
os.RemoveAll(tmpTxtFileName)
})
tests := []struct {
name string
content []byte
openCompressionWriter func(w io.Writer) (io.WriteCloser, error)
compressorName string
wantFormatName string
}{
{
name: "should recognize brotli",
openCompressionWriter: Brotli{}.OpenWriter,
content: []byte("this is text, but it has to be long enough to match brotli which doesn't have a magic number"),
compressorName: ".br",
wantFormatName: ".br",
},
{
name: "should recognize bz2",
openCompressionWriter: Bz2{}.OpenWriter,
content: []byte("this is text"),
compressorName: ".bz2",
wantFormatName: ".bz2",
},
{
name: "should recognize gz",
openCompressionWriter: Gz{}.OpenWriter,
content: []byte("this is text"),
compressorName: ".gz",
wantFormatName: ".gz",
},
{
name: "should recognize lz4",
openCompressionWriter: Lz4{}.OpenWriter,
content: []byte("this is text"),
compressorName: ".lz4",
wantFormatName: ".lz4",
},
{
name: "should recognize lz",
openCompressionWriter: Lzip{}.OpenWriter,
content: []byte("this is text"),
compressorName: ".lz",
wantFormatName: ".lz",
},
{
name: "should recognize sz",
openCompressionWriter: Sz{}.OpenWriter,
content: []byte("this is text"),
compressorName: ".sz",
wantFormatName: ".sz",
},
{
name: "should recognize xz",
openCompressionWriter: Xz{}.OpenWriter,
content: []byte("this is text"),
compressorName: ".xz",
wantFormatName: ".xz",
},
{
name: "should recognize zst",
openCompressionWriter: Zstd{}.OpenWriter,
content: []byte("this is text"),
compressorName: ".zst",
wantFormatName: ".zst",
},
{
name: "should recognize tar",
openCompressionWriter: newWriteNopCloser,
content: archive(t, Tar{}, tmpTxtFileName, tmpTxtFileInfo),
compressorName: "",
wantFormatName: ".tar",
},
{
name: "should recognize tar.gz",
openCompressionWriter: Gz{}.OpenWriter,
content: archive(t, Tar{}, tmpTxtFileName, tmpTxtFileInfo),
compressorName: ".gz",
wantFormatName: ".tar.gz",
},
{
name: "should recognize zip",
openCompressionWriter: newWriteNopCloser,
content: archive(t, Zip{}, tmpTxtFileName, tmpTxtFileInfo),
compressorName: "",
wantFormatName: ".zip",
},
{
name: "should recognize rar by v5.0 header",
openCompressionWriter: newWriteNopCloser,
content: rarHeaderV5_0[:],
compressorName: "",
wantFormatName: ".rar",
},
{
name: "should recognize rar by v1.5 header",
openCompressionWriter: newWriteNopCloser,
content: rarHeaderV1_5[:],
compressorName: "",
wantFormatName: ".rar",
},
{
name: "should recognize zz",
openCompressionWriter: Zlib{}.OpenWriter,
content: []byte("this is text"),
compressorName: ".zz",
wantFormatName: ".zz",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
stream := bytes.NewReader(compress(t, tt.compressorName, tt.content, tt.openCompressionWriter))
got, _, err := Identify(context.Background(), "", stream)
if err != nil {
t.Errorf("should have found a corresponding Format, but got err=%+v", err)
return
}
if tt.wantFormatName != got.Extension() {
t.Errorf("unexpected format found: expected=%s actual=%s", tt.wantFormatName, got.Extension())
return
}
})
}
}
func TestIdentifyAndOpenZip(t *testing.T) {
f, err := os.Open("testdata/test.zip")
checkErr(t, err, "opening zip")
defer f.Close()
format, reader, err := Identify(context.Background(), "test.zip", f)
checkErr(t, err, "identifying zip")
if format.Extension() != ".zip" {
t.Errorf("unexpected format found: expected=.zip actual=%s", format.Extension())
}
err = format.(Extractor).Extract(context.Background(), reader, func(ctx context.Context, f FileInfo) error {
rc, err := f.Open()
if err != nil {
return err
}
defer rc.Close()
_, err = io.ReadAll(rc)
return err
})
checkErr(t, err, "extracting zip")
}
func TestIdentifyASCIIFileStartingWithX(t *testing.T) {
// Create a temporary file starting with the letter 'x'
tmpFile, err := os.CreateTemp("", "TestIdentifyASCIIFileStartingWithX-tmp-*.txt")
if err != nil {
t.Errorf("fail to create tmp test file for archive tests: err=%v", err)
}
defer os.Remove(tmpFile.Name())
_, err = tmpFile.Write([]byte("xThis is a test file"))
if err != nil {
t.Errorf("Failed to write to temp file: %v", err)
}
tmpFile.Close()
// Open the file and use the Identify function
file, err := os.Open(tmpFile.Name())
if err != nil {
t.Errorf("Failed to open temp file: %v", err)
}
defer file.Close()
_, _, err = Identify(context.Background(), tmpFile.Name(), file)
if !errors.Is(err, NoMatch) {
t.Errorf("Identify failed: %v", err)
}
}