-
Notifications
You must be signed in to change notification settings - Fork 38
/
decompressors_test.go
89 lines (77 loc) · 1.83 KB
/
decompressors_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
package fakemachine
import (
"bufio"
"bytes"
"errors"
"io"
"os"
"path"
"testing"
"github.com/go-debos/fakemachine/cpio"
)
func checkStreamsMatch(t *testing.T, output, check io.Reader) error {
i := 0
oreader := bufio.NewReader(output)
creader := bufio.NewReader(check)
for {
ochar, oerr := oreader.ReadByte()
cchar, cerr := creader.ReadByte()
if oerr != nil || cerr != nil {
if oerr == io.EOF && cerr == io.EOF {
return nil
}
if oerr != nil && oerr != io.EOF {
t.Errorf("Error reading output stream: %s", oerr)
return oerr
}
if cerr != nil && cerr != io.EOF {
t.Errorf("Error reading check stream: %s", cerr)
return cerr
}
return nil
}
if ochar != cchar {
t.Errorf("Mismatch at byte %d, values %d (output) and %d (check)",
i, ochar, cchar)
return errors.New("Data mismatch")
}
i += 1
}
}
func decompressorTest(t *testing.T, file, suffix string, d writerhelper.Transformer) {
f, err := os.Open(path.Join("testdata", file+suffix))
if err != nil {
t.Errorf("Unable to open test data: %s", err)
return
}
defer f.Close()
output := new(bytes.Buffer)
err = d(output, f)
if err != nil {
t.Errorf("Error whilst decompressing test file: %s", err)
return
}
checkFile, err := os.Open(path.Join("testdata", file))
if err != nil {
t.Errorf("Unable to open check data: %s", err)
return
}
defer checkFile.Close()
err = checkStreamsMatch(t, output, checkFile)
if err != nil {
t.Errorf("Failed to compare streams: %s", err)
return
}
}
func TestZstd(t *testing.T) {
decompressorTest(t, "test", ".zst", ZstdDecompressor)
}
func TestXz(t *testing.T) {
decompressorTest(t, "test", ".xz", XzDecompressor)
}
func TestGzip(t *testing.T) {
decompressorTest(t, "test", ".gz", GzipDecompressor)
}
func TestNull(t *testing.T) {
decompressorTest(t, "test", "", NullDecompressor)
}