OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [archive/] [zip/] [reader_test.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// 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.
4
 
5
package zip
6
 
7
import (
8
        "bytes"
9
        "encoding/binary"
10
        "io"
11
        "io/ioutil"
12
        "os"
13
        "testing"
14
        "time"
15
)
16
 
17
type ZipTest struct {
18
        Name    string
19
        Comment string
20
        File    []ZipTestFile
21
        Error   error // the error that Opening this file should return
22
}
23
 
24
type ZipTestFile struct {
25
        Name    string
26
        Content []byte // if blank, will attempt to compare against File
27
        File    string // name of file to compare to (relative to testdata/)
28
        Mtime   string // modified time in format "mm-dd-yy hh:mm:ss"
29
        Mode    os.FileMode
30
}
31
 
32
// Caution: The Mtime values found for the test files should correspond to
33
//          the values listed with unzip -l . However, the values
34
//          listed by unzip appear to be off by some hours. When creating
35
//          fresh test files and testing them, this issue is not present.
36
//          The test files were created in Sydney, so there might be a time
37
//          zone issue. The time zone information does have to be encoded
38
//          somewhere, because otherwise unzip -l could not provide a different
39
//          time from what the archive/zip package provides, but there appears
40
//          to be no documentation about this.
41
 
42
var tests = []ZipTest{
43
        {
44
                Name:    "test.zip",
45
                Comment: "This is a zipfile comment.",
46
                File: []ZipTestFile{
47
                        {
48
                                Name:    "test.txt",
49
                                Content: []byte("This is a test text file.\n"),
50
                                Mtime:   "09-05-10 12:12:02",
51
                                Mode:    0644,
52
                        },
53
                        {
54
                                Name:  "gophercolor16x16.png",
55
                                File:  "gophercolor16x16.png",
56
                                Mtime: "09-05-10 15:52:58",
57
                                Mode:  0644,
58
                        },
59
                },
60
        },
61
        {
62
                Name: "r.zip",
63
                File: []ZipTestFile{
64
                        {
65
                                Name:  "r/r.zip",
66
                                File:  "r.zip",
67
                                Mtime: "03-04-10 00:24:16",
68
                                Mode:  0666,
69
                        },
70
                },
71
        },
72
        {
73
                Name: "symlink.zip",
74
                File: []ZipTestFile{
75
                        {
76
                                Name:    "symlink",
77
                                Content: []byte("../target"),
78
                                Mode:    0777 | os.ModeSymlink,
79
                        },
80
                },
81
        },
82
        {
83
                Name: "readme.zip",
84
        },
85
        {
86
                Name:  "readme.notzip",
87
                Error: ErrFormat,
88
        },
89
        {
90
                Name: "dd.zip",
91
                File: []ZipTestFile{
92
                        {
93
                                Name:    "filename",
94
                                Content: []byte("This is a test textfile.\n"),
95
                                Mtime:   "02-02-11 13:06:20",
96
                                Mode:    0666,
97
                        },
98
                },
99
        },
100
        {
101
                // created in windows XP file manager.
102
                Name: "winxp.zip",
103
                File: crossPlatform,
104
        },
105
        {
106
                // created by Zip 3.0 under Linux
107
                Name: "unix.zip",
108
                File: crossPlatform,
109
        },
110
}
111
 
112
var crossPlatform = []ZipTestFile{
113
        {
114
                Name:    "hello",
115
                Content: []byte("world \r\n"),
116
                Mode:    0666,
117
        },
118
        {
119
                Name:    "dir/bar",
120
                Content: []byte("foo \r\n"),
121
                Mode:    0666,
122
        },
123
        {
124
                Name:    "dir/empty/",
125
                Content: []byte{},
126
                Mode:    os.ModeDir | 0777,
127
        },
128
        {
129
                Name:    "readonly",
130
                Content: []byte("important \r\n"),
131
                Mode:    0444,
132
        },
133
}
134
 
135
func TestReader(t *testing.T) {
136
        for _, zt := range tests {
137
                readTestZip(t, zt)
138
        }
139
}
140
 
141
func readTestZip(t *testing.T, zt ZipTest) {
142
        z, err := OpenReader("testdata/" + zt.Name)
143
        if err != zt.Error {
144
                t.Errorf("error=%v, want %v", err, zt.Error)
145
                return
146
        }
147
 
148
        // bail if file is not zip
149
        if err == ErrFormat {
150
                return
151
        }
152
        defer func() {
153
                if err := z.Close(); err != nil {
154
                        t.Errorf("error %q when closing zip file", err)
155
                }
156
        }()
157
 
158
        // bail here if no Files expected to be tested
159
        // (there may actually be files in the zip, but we don't care)
160
        if zt.File == nil {
161
                return
162
        }
163
 
164
        if z.Comment != zt.Comment {
165
                t.Errorf("%s: comment=%q, want %q", zt.Name, z.Comment, zt.Comment)
166
        }
167
        if len(z.File) != len(zt.File) {
168
                t.Errorf("%s: file count=%d, want %d", zt.Name, len(z.File), len(zt.File))
169
        }
170
 
171
        // test read of each file
172
        for i, ft := range zt.File {
173
                readTestFile(t, ft, z.File[i])
174
        }
175
 
176
        // test simultaneous reads
177
        n := 0
178
        done := make(chan bool)
179
        for i := 0; i < 5; i++ {
180
                for j, ft := range zt.File {
181
                        go func(j int, ft ZipTestFile) {
182
                                readTestFile(t, ft, z.File[j])
183
                                done <- true
184
                        }(j, ft)
185
                        n++
186
                }
187
        }
188
        for ; n > 0; n-- {
189
                <-done
190
        }
191
 
192
        // test invalid checksum
193
        if !z.File[0].hasDataDescriptor() { // skip test when crc32 in dd
194
                z.File[0].CRC32++ // invalidate
195
                r, err := z.File[0].Open()
196
                if err != nil {
197
                        t.Error(err)
198
                        return
199
                }
200
                var b bytes.Buffer
201
                _, err = io.Copy(&b, r)
202
                if err != ErrChecksum {
203
                        t.Errorf("%s: copy error=%v, want %v", z.File[0].Name, err, ErrChecksum)
204
                }
205
        }
206
}
207
 
208
func readTestFile(t *testing.T, ft ZipTestFile, f *File) {
209
        if f.Name != ft.Name {
210
                t.Errorf("name=%q, want %q", f.Name, ft.Name)
211
        }
212
 
213
        if ft.Mtime != "" {
214
                mtime, err := time.Parse("01-02-06 15:04:05", ft.Mtime)
215
                if err != nil {
216
                        t.Error(err)
217
                        return
218
                }
219
                if ft := f.ModTime(); !ft.Equal(mtime) {
220
                        t.Errorf("%s: mtime=%s, want %s", f.Name, ft, mtime)
221
                }
222
        }
223
 
224
        testFileMode(t, f, ft.Mode)
225
 
226
        size0 := f.UncompressedSize
227
 
228
        var b bytes.Buffer
229
        r, err := f.Open()
230
        if err != nil {
231
                t.Error(err)
232
                return
233
        }
234
 
235
        if size1 := f.UncompressedSize; size0 != size1 {
236
                t.Errorf("file %q changed f.UncompressedSize from %d to %d", f.Name, size0, size1)
237
        }
238
 
239
        _, err = io.Copy(&b, r)
240
        if err != nil {
241
                t.Error(err)
242
                return
243
        }
244
        r.Close()
245
 
246
        var c []byte
247
        if ft.Content != nil {
248
                c = ft.Content
249
        } else if c, err = ioutil.ReadFile("testdata/" + ft.File); err != nil {
250
                t.Error(err)
251
                return
252
        }
253
 
254
        if b.Len() != len(c) {
255
                t.Errorf("%s: len=%d, want %d", f.Name, b.Len(), len(c))
256
                return
257
        }
258
 
259
        for i, b := range b.Bytes() {
260
                if b != c[i] {
261
                        t.Errorf("%s: content[%d]=%q want %q", f.Name, i, b, c[i])
262
                        return
263
                }
264
        }
265
}
266
 
267
func testFileMode(t *testing.T, f *File, want os.FileMode) {
268
        mode := f.Mode()
269
        if want == 0 {
270
                t.Errorf("%s mode: got %v, want none", f.Name, mode)
271
        } else if mode != want {
272
                t.Errorf("%s mode: want %v, got %v", f.Name, want, mode)
273
        }
274
}
275
 
276
func TestInvalidFiles(t *testing.T) {
277
        const size = 1024 * 70 // 70kb
278
        b := make([]byte, size)
279
 
280
        // zeroes
281
        _, err := NewReader(sliceReaderAt(b), size)
282
        if err != ErrFormat {
283
                t.Errorf("zeroes: error=%v, want %v", err, ErrFormat)
284
        }
285
 
286
        // repeated directoryEndSignatures
287
        sig := make([]byte, 4)
288
        binary.LittleEndian.PutUint32(sig, directoryEndSignature)
289
        for i := 0; i < size-4; i += 4 {
290
                copy(b[i:i+4], sig)
291
        }
292
        _, err = NewReader(sliceReaderAt(b), size)
293
        if err != ErrFormat {
294
                t.Errorf("sigs: error=%v, want %v", err, ErrFormat)
295
        }
296
}
297
 
298
type sliceReaderAt []byte
299
 
300
func (r sliceReaderAt) ReadAt(b []byte, off int64) (int, error) {
301
        copy(b, r[int(off):int(off)+len(b)])
302
        return len(b), nil
303
}

powered by: WebSVN 2.1.0

© copyright 1999-2024 OpenCores.org, equivalent to Oliscience, all rights reserved. OpenCores®, registered trademark.