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
|
// Copyright 2010 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
package zip
import (
"bytes"
"encoding/binary"
"io"
"io/ioutil"
"os"
"testing"
"time"
)
type ZipTest struct {
Name string
Comment string
File []ZipTestFile
Error error // the error that Opening this file should return
}
type ZipTestFile struct {
Name string
Content []byte // if blank, will attempt to compare against File
File string // name of file to compare to (relative to testdata/)
Mtime string // modified time in format "mm-dd-yy hh:mm:ss"
Mode os.FileMode
}
// Caution: The Mtime values found for the test files should correspond to
// the values listed with unzip -l <zipfile>. However, the values
// listed by unzip appear to be off by some hours. When creating
// fresh test files and testing them, this issue is not present.
// The test files were created in Sydney, so there might be a time
// zone issue. The time zone information does have to be encoded
// somewhere, because otherwise unzip -l could not provide a different
// time from what the archive/zip package provides, but there appears
// to be no documentation about this.
var tests = []ZipTest{
{
Name: "test.zip",
Comment: "This is a zipfile comment.",
File: []ZipTestFile{
{
Name: "test.txt",
Content: []byte("This is a test text file.\n"),
Mtime: "09-05-10 12:12:02",
Mode: 0644,
},
{
Name: "gophercolor16x16.png",
File: "gophercolor16x16.png",
Mtime: "09-05-10 15:52:58",
Mode: 0644,
},
},
},
{
Name: "r.zip",
File: []ZipTestFile{
{
Name: "r/r.zip",
File: "r.zip",
Mtime: "03-04-10 00:24:16",
Mode: 0666,
},
},
},
{Name: "readme.zip"},
{Name: "readme.notzip", Error: FormatError},
{
Name: "dd.zip",
File: []ZipTestFile{
{
Name: "filename",
Content: []byte("This is a test textfile.\n"),
Mtime: "02-02-11 13:06:20",
Mode: 0666,
},
},
},
{
// created in windows XP file manager.
Name: "winxp.zip",
File: crossPlatform,
},
{
// created by Zip 3.0 under Linux
Name: "unix.zip",
File: crossPlatform,
},
}
var crossPlatform = []ZipTestFile{
{
Name: "hello",
Content: []byte("world \r\n"),
Mode: 0666,
},
{
Name: "dir/bar",
Content: []byte("foo \r\n"),
Mode: 0666,
},
{
Name: "dir/empty/",
Content: []byte{},
Mode: os.ModeDir | 0777,
},
{
Name: "readonly",
Content: []byte("important \r\n"),
Mode: 0444,
},
}
func TestReader(t *testing.T) {
for _, zt := range tests {
readTestZip(t, zt)
}
}
func readTestZip(t *testing.T, zt ZipTest) {
z, err := OpenReader("testdata/" + zt.Name)
if err != zt.Error {
t.Errorf("error=%v, want %v", err, zt.Error)
return
}
// bail if file is not zip
if err == FormatError {
return
}
defer func() {
if err := z.Close(); err != nil {
t.Errorf("error %q when closing zip file", err)
}
}()
// bail here if no Files expected to be tested
// (there may actually be files in the zip, but we don't care)
if zt.File == nil {
return
}
if z.Comment != zt.Comment {
t.Errorf("%s: comment=%q, want %q", zt.Name, z.Comment, zt.Comment)
}
if len(z.File) != len(zt.File) {
t.Errorf("%s: file count=%d, want %d", zt.Name, len(z.File), len(zt.File))
}
// test read of each file
for i, ft := range zt.File {
readTestFile(t, ft, z.File[i])
}
// test simultaneous reads
n := 0
done := make(chan bool)
for i := 0; i < 5; i++ {
for j, ft := range zt.File {
go func() {
readTestFile(t, ft, z.File[j])
done <- true
}()
n++
}
}
for ; n > 0; n-- {
<-done
}
// test invalid checksum
if !z.File[0].hasDataDescriptor() { // skip test when crc32 in dd
z.File[0].CRC32++ // invalidate
r, err := z.File[0].Open()
if err != nil {
t.Error(err)
return
}
var b bytes.Buffer
_, err = io.Copy(&b, r)
if err != ChecksumError {
t.Errorf("%s: copy error=%v, want %v", z.File[0].Name, err, ChecksumError)
}
}
}
func readTestFile(t *testing.T, ft ZipTestFile, f *File) {
if f.Name != ft.Name {
t.Errorf("name=%q, want %q", f.Name, ft.Name)
}
if ft.Mtime != "" {
mtime, err := time.Parse("01-02-06 15:04:05", ft.Mtime)
if err != nil {
t.Error(err)
return
}
if ft := f.ModTime(); !ft.Equal(mtime) {
t.Errorf("%s: mtime=%s, want %s", f.Name, ft, mtime)
}
}
testFileMode(t, f, ft.Mode)
size0 := f.UncompressedSize
var b bytes.Buffer
r, err := f.Open()
if err != nil {
t.Error(err)
return
}
if size1 := f.UncompressedSize; size0 != size1 {
t.Errorf("file %q changed f.UncompressedSize from %d to %d", f.Name, size0, size1)
}
_, err = io.Copy(&b, r)
if err != nil {
t.Error(err)
return
}
r.Close()
var c []byte
if ft.Content != nil {
c = ft.Content
} else if c, err = ioutil.ReadFile("testdata/" + ft.File); err != nil {
t.Error(err)
return
}
if b.Len() != len(c) {
t.Errorf("%s: len=%d, want %d", f.Name, b.Len(), len(c))
return
}
for i, b := range b.Bytes() {
if b != c[i] {
t.Errorf("%s: content[%d]=%q want %q", f.Name, i, b, c[i])
return
}
}
}
func testFileMode(t *testing.T, f *File, want os.FileMode) {
mode, err := f.Mode()
if want == 0 {
if err == nil {
t.Errorf("%s mode: got %v, want none", f.Name, mode)
}
} else if err != nil {
t.Errorf("%s mode: %s", f.Name, err)
} else if mode != want {
t.Errorf("%s mode: want %v, got %v", f.Name, want, mode)
}
}
func TestInvalidFiles(t *testing.T) {
const size = 1024 * 70 // 70kb
b := make([]byte, size)
// zeroes
_, err := NewReader(sliceReaderAt(b), size)
if err != FormatError {
t.Errorf("zeroes: error=%v, want %v", err, FormatError)
}
// repeated directoryEndSignatures
sig := make([]byte, 4)
binary.LittleEndian.PutUint32(sig, directoryEndSignature)
for i := 0; i < size-4; i += 4 {
copy(b[i:i+4], sig)
}
_, err = NewReader(sliceReaderAt(b), size)
if err != FormatError {
t.Errorf("sigs: error=%v, want %v", err, FormatError)
}
}
type sliceReaderAt []byte
func (r sliceReaderAt) ReadAt(b []byte, off int64) (int, error) {
copy(b, r[int(off):int(off)+len(b)])
return len(b), nil
}
|