-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathzipindex_test.go
183 lines (178 loc) · 4.55 KB
/
zipindex_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
/*
* zipindex, (C)2021 MinIO, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package zipindex
import (
"archive/zip"
"bytes"
"errors"
"io"
"os"
"path/filepath"
"reflect"
"testing"
"github.com/klauspost/compress/zstd"
)
func TestReadDir(t *testing.T) {
testSet := []string{
"big.zip",
"crc32-not-streamed.zip",
"dd.zip",
"go-no-datadesc-sig.zip",
"gophercolor16x16.png",
"go-with-datadesc-sig.zip",
"readme.notzip",
"readme.zip",
"symlink.zip",
"test.zip",
"test-trailing-junk.zip",
"time-22738.zip",
"time-7zip.zip",
"time-go.zip",
"time-infozip.zip",
"time-osx.zip",
"time-win7.zip",
"time-winrar.zip",
"time-winzip.zip",
"unix.zip",
"utf8-7zip.zip",
"utf8-infozip.zip",
"utf8-osx.zip",
"utf8-winrar.zip",
"utf8-winzip.zip",
"winxp.zip",
"zip64.zip",
"zip64-2.zip",
"smallish.zip",
"zstd-compressed.zip",
"test-badbase.zip",
"comment-truncated.zip",
"fuzz/FuzzDeserializeFiles.zip",
"fuzz/FuzzRoundtrip.zip",
}
for _, test := range testSet {
t.Run(test, func(t *testing.T) {
input, err := os.ReadFile(filepath.Join("testdata", test))
if err != nil {
t.Fatal(err)
}
zr, err := zip.NewReader(bytes.NewReader(input), int64(len(input)))
if err != nil {
// We got an error, we should also get one from ourself
_, err := ReadDir(input, int64(len(input)), DefaultFileFilter)
if err == nil {
t.Errorf("want error, like %v, got none", err)
}
return
}
zr.RegisterDecompressor(zstd.ZipMethodWinZip, zstd.ZipDecompressor(zstd.WithDecoderLowmem(true)))
sz := 8 << 10
if sz > len(input) {
// Truncate a bit from the start...
sz = len(input) - 10
}
var files Files
for {
files, err = ReadDir(input[len(input)-sz:], int64(len(input)),
func(dst *File, entry *ZipDirEntry) *File {
dst.Custom = map[string]string{
"modified": entry.Modified.UTC().String(),
}
return DefaultFileFilter(dst, entry)
})
if err == nil {
break
}
var more ErrNeedMoreData
if !errors.As(err, &more) {
t.Errorf("unexpected error: %v", err)
return
}
t.Logf("wanted more: %d bytes from end...", more.FromEnd)
sz = int(more.FromEnd)
}
files.OptimizeSize()
files.RemoveInsecurePaths()
ser, err := files.Serialize()
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
t.Log("Serialized size:", len(ser), "Files:", len(files))
files, err = DeserializeFiles(ser)
if err != nil {
t.Errorf("unexpected error: %v", err)
return
}
var nFiles int
for _, file := range zr.File {
if !file.Mode().IsRegular() {
if files.Find(file.Name) != nil {
t.Errorf("found non-regular file %v", file.Name)
}
continue
}
nFiles++
gotFile := files.Find(file.Name)
if gotFile == nil {
t.Errorf(" could not find regular file %v", file.Name)
continue
}
if f, err := FindSerialized(ser, file.Name); err != nil || f == nil {
t.Errorf("FindSerialized: could not find regular file %v, err: %v, file: %v", file.Name, err, f)
continue
} else {
if !reflect.DeepEqual(*f, *gotFile) {
t.Errorf("FindSerialized returned %+v\nfiles.Find returned: %+v", *f, *gotFile)
}
}
wantRC, wantErr := file.Open()
rc, err := gotFile.Open(bytes.NewReader(input[gotFile.Offset:]))
if err != nil {
if wantErr != nil {
continue
}
t.Error("got error:", err)
return
}
if wantErr != nil {
t.Errorf("want error, like %v, got none", wantErr)
}
defer func() {
wantErr := wantRC.Close()
gotErr := rc.Close()
if wantErr != gotErr {
t.Errorf("err mismatch: %v != %v", wantErr, gotErr)
}
}()
wantData, wantErr := io.ReadAll(wantRC)
gotData, err := io.ReadAll(rc)
if err != nil {
if err == wantErr {
continue
}
t.Error("got error:", err)
return
}
if !bytes.Equal(wantData, gotData) {
t.Error("data mismatch")
}
}
if nFiles > 0 {
t.Logf("%.02f bytes/file", float64(len(ser))/float64(nFiles))
}
})
}
}