-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathbzip2_testsuite_test.go
121 lines (110 loc) · 3.2 KB
/
bzip2_testsuite_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
// Copyright 2021 Cosmos Nicolaou. All rights reserved.
// Use of this source code is governed by the Apache-2.0
// license that can be found in the LICENSE file.
package pbzip2_test
import (
"context"
"crypto/md5" //nolint:gosec
"fmt"
"io"
"io/fs"
"os"
"os/exec"
"path/filepath"
"runtime"
"strings"
"testing"
"github.com/cosnicolaou/pbzip2"
)
func gitcloneTestsuite(tmpdir string) error {
opts := []string{"clone"}
if runtime.GOOS == "windows" {
opts = append(opts, "--config", "core.autocrlf=input")
}
opts = append(opts, "https://sourceware.org/git/bzip2-tests.git")
cmd := exec.Command("git", opts...)
cmd.Dir = tmpdir
out, err := cmd.CombinedOutput()
if err != nil {
return fmt.Errorf("%s: %s: %v", strings.Join(cmd.Args, " "), out, err)
}
return nil
}
type testfile struct {
filename string
md5 string
err string
}
func getBzip2Files(tmpdir string) ([]testfile, error) {
// exceptions represent input files that we expect to fail with for
// the reasons given below.
var exceptions = map[string]string{
// The error message from bzcat differs.
filepath.Join("lbzip2", "gap.bz2"): "mismatched stream CRCs: calculated=0x4818d9f8 != stored=0x35ebf960",
// The error message from bzcat differs.
filepath.Join("lbzip2", "trash.bz2"): "failed to find trailer",
// bzcat supports the legacy randomized mode whereas the go bzip2
// package does not.
filepath.Join("lbzip2", "rand.bz2"): "bzip2 data invalid: deprecated randomized files",
}
files := map[string]bool{}
sums := map[string]string{}
err := filepath.Walk(tmpdir,
func(path string, info fs.FileInfo, err error) error {
if err != nil {
return err
}
if base := strings.TrimSuffix(info.Name(), ".bz2"); base != info.Name() {
files[path] = true
}
if base := strings.TrimSuffix(info.Name(), ".md5"); base != info.Name() {
buf, err := os.ReadFile(path)
if err != nil {
return err
}
dirname := filepath.Dir(path)
sums[filepath.Join(dirname, base+".bz2")] = strings.TrimSuffix(string(buf), " -\n")
}
return nil
})
tldir := filepath.Join(tmpdir, "bzip2-tests") + string(filepath.Separator)
pairs := make([]testfile, 0, len(files))
for file := range files {
err := exceptions[strings.TrimPrefix(file, tldir)]
pairs = append(pairs, testfile{filename: file, err: err, md5: sums[file]})
}
return pairs, err
}
func TestBzip2Tests(t *testing.T) {
ctx := context.Background()
tmpdir := t.TempDir()
if err := gitcloneTestsuite(tmpdir); err != nil {
t.Fatal(err)
}
testcases, err := getBzip2Files(tmpdir)
if err != nil {
t.Fatal(err)
}
for _, tc := range testcases {
t.Log(tc.filename)
bzfile, err := os.Open(tc.filename)
if err != nil {
t.Errorf("%v: %v", tc.filename, err)
}
defer bzfile.Close()
rd := pbzip2.NewReader(ctx, bzfile)
h := md5.New() //nolint:gosec
_, err = io.Copy(h, rd)
if len(tc.err) > 0 {
if err == nil || err.Error() != tc.err {
t.Errorf("%v: missing or wrong error: got %v: want: %v", tc.filename, err, tc.err)
}
continue
} else if err != nil {
t.Errorf("%v: %v", tc.filename, err)
}
if got, want := fmt.Sprintf("%02x", h.Sum(nil)), tc.md5; got != want {
t.Errorf("%v: got %v, want %v", tc.filename, got, want)
}
}
}