1 // Copyright 2010 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
20 Error os
.Error
// the error that Opening this file should return
23 type ZipTestFile
struct {
25 Content
[]byte // if blank, will attempt to compare against File
26 File
string // name of file to compare to (relative to testdata/)
29 var tests
= []ZipTest
{
32 Comment
: "This is a zipfile comment.",
36 Content
: []byte("This is a test text file.\n"),
39 Name
: "gophercolor16x16.png",
40 File
: "gophercolor16x16.png",
54 {Name
: "readme.notzip", Error
: FormatError
},
57 func TestReader(t
*testing
.T
) {
58 for _
, zt
:= range tests
{
63 func readTestZip(t
*testing
.T
, zt ZipTest
) {
64 z
, err
:= OpenReader("testdata/" + zt
.Name
)
66 t
.Errorf("error=%v, want %v", err
, zt
.Error
)
70 // bail here if no Files expected to be tested
71 // (there may actually be files in the zip, but we don't care)
76 if z
.Comment
!= zt
.Comment
{
77 t
.Errorf("%s: comment=%q, want %q", zt
.Name
, z
.Comment
, zt
.Comment
)
79 if len(z
.File
) != len(zt
.File
) {
80 t
.Errorf("%s: file count=%d, want %d", zt
.Name
, len(z
.File
), len(zt
.File
))
83 // test read of each file
84 for i
, ft
:= range zt
.File
{
85 readTestFile(t
, ft
, z
.File
[i
])
88 // test simultaneous reads
90 done
:= make(chan bool)
91 for i
:= 0; i
< 5; i
++ {
92 for j
, ft
:= range zt
.File
{
94 readTestFile(t
, ft
, z
.File
[j
])
104 // test invalid checksum
105 z
.File
[0].CRC32
++ // invalidate
106 r
, err
:= z
.File
[0].Open()
112 _
, err
= io
.Copy(&b
, r
)
113 if err
!= ChecksumError
{
114 t
.Errorf("%s: copy error=%v, want %v", err
, ChecksumError
)
118 func readTestFile(t
*testing
.T
, ft ZipTestFile
, f
*File
) {
119 if f
.Name
!= ft
.Name
{
120 t
.Errorf("name=%q, want %q", f
.Name
, ft
.Name
)
128 _
, err
= io
.Copy(&b
, r
)
135 if len(ft
.Content
) != 0 {
137 } else if c
, err
= ioutil
.ReadFile("testdata/" + ft
.File
); err
!= nil {
141 if b
.Len() != len(c
) {
142 t
.Errorf("%s: len=%d, want %d", f
.Name
, b
.Len(), len(c
))
145 for i
, b
:= range b
.Bytes() {
147 t
.Errorf("%s: content[%d]=%q want %q", i
, b
, c
[i
])
153 func TestInvalidFiles(t
*testing
.T
) {
154 const size
= 1024 * 70 // 70kb
155 b
:= make([]byte, size
)
158 _
, err
:= NewReader(sliceReaderAt(b
), size
)
159 if err
!= FormatError
{
160 t
.Errorf("zeroes: error=%v, want %v", err
, FormatError
)
163 // repeated directoryEndSignatures
164 sig
:= make([]byte, 4)
165 binary
.LittleEndian
.PutUint32(sig
, directoryEndSignature
)
166 for i
:= 0; i
< size
-4; i
+= 4 {
169 _
, err
= NewReader(sliceReaderAt(b
), size
)
170 if err
!= FormatError
{
171 t
.Errorf("sigs: error=%v, want %v", err
, FormatError
)
175 type sliceReaderAt
[]byte
177 func (r sliceReaderAt
) ReadAt(b
[]byte, off
int64) (int, os
.Error
) {
178 copy(b
, r
[int(off
):int(off
)+len(b
)])