Source file
src/archive/zip/reader_test.go
1
2
3
4
5 package zip
6
7 import (
8 "bytes"
9 "encoding/binary"
10 "encoding/hex"
11 "internal/obscuretestdata"
12 "io"
13 "io/fs"
14 "os"
15 "path/filepath"
16 "reflect"
17 "regexp"
18 "strings"
19 "testing"
20 "testing/fstest"
21 "time"
22 )
23
24 type ZipTest struct {
25 Name string
26 Source func() (r io.ReaderAt, size int64)
27 Comment string
28 File []ZipTestFile
29 Obscured bool
30 Error error
31 }
32
33 type ZipTestFile struct {
34 Name string
35 Mode fs.FileMode
36 NonUTF8 bool
37 ModTime time.Time
38 Modified time.Time
39
40
41
42
43
44
45
46
47
48
49
50 ContentErr error
51 Content []byte
52 File string
53 Size uint64
54 }
55
56 var tests = []ZipTest{
57 {
58 Name: "test.zip",
59 Comment: "This is a zipfile comment.",
60 File: []ZipTestFile{
61 {
62 Name: "test.txt",
63 Content: []byte("This is a test text file.\n"),
64 Modified: time.Date(2010, 9, 5, 12, 12, 1, 0, timeZone(+10*time.Hour)),
65 Mode: 0644,
66 },
67 {
68 Name: "gophercolor16x16.png",
69 File: "gophercolor16x16.png",
70 Modified: time.Date(2010, 9, 5, 15, 52, 58, 0, timeZone(+10*time.Hour)),
71 Mode: 0644,
72 },
73 },
74 },
75 {
76 Name: "test-trailing-junk.zip",
77 Comment: "This is a zipfile comment.",
78 File: []ZipTestFile{
79 {
80 Name: "test.txt",
81 Content: []byte("This is a test text file.\n"),
82 Modified: time.Date(2010, 9, 5, 12, 12, 1, 0, timeZone(+10*time.Hour)),
83 Mode: 0644,
84 },
85 {
86 Name: "gophercolor16x16.png",
87 File: "gophercolor16x16.png",
88 Modified: time.Date(2010, 9, 5, 15, 52, 58, 0, timeZone(+10*time.Hour)),
89 Mode: 0644,
90 },
91 },
92 },
93 {
94 Name: "test-prefix.zip",
95 Comment: "This is a zipfile comment.",
96 File: []ZipTestFile{
97 {
98 Name: "test.txt",
99 Content: []byte("This is a test text file.\n"),
100 Modified: time.Date(2010, 9, 5, 12, 12, 1, 0, timeZone(+10*time.Hour)),
101 Mode: 0644,
102 },
103 {
104 Name: "gophercolor16x16.png",
105 File: "gophercolor16x16.png",
106 Modified: time.Date(2010, 9, 5, 15, 52, 58, 0, timeZone(+10*time.Hour)),
107 Mode: 0644,
108 },
109 },
110 },
111 {
112 Name: "test-baddirsz.zip",
113 Comment: "This is a zipfile comment.",
114 File: []ZipTestFile{
115 {
116 Name: "test.txt",
117 Content: []byte("This is a test text file.\n"),
118 Modified: time.Date(2010, 9, 5, 12, 12, 1, 0, timeZone(+10*time.Hour)),
119 Mode: 0644,
120 },
121 {
122 Name: "gophercolor16x16.png",
123 File: "gophercolor16x16.png",
124 Modified: time.Date(2010, 9, 5, 15, 52, 58, 0, timeZone(+10*time.Hour)),
125 Mode: 0644,
126 },
127 },
128 },
129 {
130 Name: "test-badbase.zip",
131 Comment: "This is a zipfile comment.",
132 File: []ZipTestFile{
133 {
134 Name: "test.txt",
135 Content: []byte("This is a test text file.\n"),
136 Modified: time.Date(2010, 9, 5, 12, 12, 1, 0, timeZone(+10*time.Hour)),
137 Mode: 0644,
138 },
139 {
140 Name: "gophercolor16x16.png",
141 File: "gophercolor16x16.png",
142 Modified: time.Date(2010, 9, 5, 15, 52, 58, 0, timeZone(+10*time.Hour)),
143 Mode: 0644,
144 },
145 },
146 },
147 {
148 Name: "r.zip",
149 Source: returnRecursiveZip,
150 File: []ZipTestFile{
151 {
152 Name: "r/r.zip",
153 Content: rZipBytes(),
154 Modified: time.Date(2010, 3, 4, 0, 24, 16, 0, time.UTC),
155 Mode: 0666,
156 },
157 },
158 },
159 {
160 Name: "symlink.zip",
161 File: []ZipTestFile{
162 {
163 Name: "symlink",
164 Content: []byte("../target"),
165 Modified: time.Date(2012, 2, 3, 19, 56, 48, 0, timeZone(-2*time.Hour)),
166 Mode: 0777 | fs.ModeSymlink,
167 },
168 },
169 },
170 {
171 Name: "readme.zip",
172 },
173 {
174 Name: "readme.notzip",
175 Error: ErrFormat,
176 },
177 {
178 Name: "dd.zip",
179 File: []ZipTestFile{
180 {
181 Name: "filename",
182 Content: []byte("This is a test textfile.\n"),
183 Modified: time.Date(2011, 2, 2, 13, 6, 20, 0, time.UTC),
184 Mode: 0666,
185 },
186 },
187 },
188 {
189
190 Name: "winxp.zip",
191 File: []ZipTestFile{
192 {
193 Name: "hello",
194 Content: []byte("world \r\n"),
195 Modified: time.Date(2011, 12, 8, 10, 4, 24, 0, time.UTC),
196 Mode: 0666,
197 },
198 {
199 Name: "dir/bar",
200 Content: []byte("foo \r\n"),
201 Modified: time.Date(2011, 12, 8, 10, 4, 50, 0, time.UTC),
202 Mode: 0666,
203 },
204 {
205 Name: "dir/empty/",
206 Content: []byte{},
207 Modified: time.Date(2011, 12, 8, 10, 8, 6, 0, time.UTC),
208 Mode: fs.ModeDir | 0777,
209 },
210 {
211 Name: "readonly",
212 Content: []byte("important \r\n"),
213 Modified: time.Date(2011, 12, 8, 10, 6, 8, 0, time.UTC),
214 Mode: 0444,
215 },
216 },
217 },
218 {
219
220 Name: "unix.zip",
221 File: []ZipTestFile{
222 {
223 Name: "hello",
224 Content: []byte("world \r\n"),
225 Modified: time.Date(2011, 12, 8, 10, 4, 24, 0, timeZone(0)),
226 Mode: 0666,
227 },
228 {
229 Name: "dir/bar",
230 Content: []byte("foo \r\n"),
231 Modified: time.Date(2011, 12, 8, 10, 4, 50, 0, timeZone(0)),
232 Mode: 0666,
233 },
234 {
235 Name: "dir/empty/",
236 Content: []byte{},
237 Modified: time.Date(2011, 12, 8, 10, 8, 6, 0, timeZone(0)),
238 Mode: fs.ModeDir | 0777,
239 },
240 {
241 Name: "readonly",
242 Content: []byte("important \r\n"),
243 Modified: time.Date(2011, 12, 8, 10, 6, 8, 0, timeZone(0)),
244 Mode: 0444,
245 },
246 },
247 },
248 {
249
250
251
252
253
254 Name: "go-no-datadesc-sig.zip.base64",
255 Obscured: true,
256 File: []ZipTestFile{
257 {
258 Name: "foo.txt",
259 Content: []byte("foo\n"),
260 Modified: time.Date(2012, 3, 8, 16, 59, 10, 0, timeZone(-8*time.Hour)),
261 Mode: 0644,
262 },
263 {
264 Name: "bar.txt",
265 Content: []byte("bar\n"),
266 Modified: time.Date(2012, 3, 8, 16, 59, 12, 0, timeZone(-8*time.Hour)),
267 Mode: 0644,
268 },
269 },
270 },
271 {
272
273
274 Name: "go-with-datadesc-sig.zip",
275 File: []ZipTestFile{
276 {
277 Name: "foo.txt",
278 Content: []byte("foo\n"),
279 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC),
280 Mode: 0666,
281 },
282 {
283 Name: "bar.txt",
284 Content: []byte("bar\n"),
285 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC),
286 Mode: 0666,
287 },
288 },
289 },
290 {
291 Name: "Bad-CRC32-in-data-descriptor",
292 Source: returnCorruptCRC32Zip,
293 File: []ZipTestFile{
294 {
295 Name: "foo.txt",
296 Content: []byte("foo\n"),
297 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC),
298 Mode: 0666,
299 ContentErr: ErrChecksum,
300 },
301 {
302 Name: "bar.txt",
303 Content: []byte("bar\n"),
304 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC),
305 Mode: 0666,
306 },
307 },
308 },
309
310
311 {
312 Name: "crc32-not-streamed.zip",
313 File: []ZipTestFile{
314 {
315 Name: "foo.txt",
316 Content: []byte("foo\n"),
317 Modified: time.Date(2012, 3, 8, 16, 59, 10, 0, timeZone(-8*time.Hour)),
318 Mode: 0644,
319 },
320 {
321 Name: "bar.txt",
322 Content: []byte("bar\n"),
323 Modified: time.Date(2012, 3, 8, 16, 59, 12, 0, timeZone(-8*time.Hour)),
324 Mode: 0644,
325 },
326 },
327 },
328
329
330 {
331 Name: "crc32-not-streamed.zip",
332 Source: returnCorruptNotStreamedZip,
333 File: []ZipTestFile{
334 {
335 Name: "foo.txt",
336 Content: []byte("foo\n"),
337 Modified: time.Date(2012, 3, 8, 16, 59, 10, 0, timeZone(-8*time.Hour)),
338 Mode: 0644,
339 ContentErr: ErrChecksum,
340 },
341 {
342 Name: "bar.txt",
343 Content: []byte("bar\n"),
344 Modified: time.Date(2012, 3, 8, 16, 59, 12, 0, timeZone(-8*time.Hour)),
345 Mode: 0644,
346 },
347 },
348 },
349 {
350 Name: "zip64.zip",
351 File: []ZipTestFile{
352 {
353 Name: "README",
354 Content: []byte("This small file is in ZIP64 format.\n"),
355 Modified: time.Date(2012, 8, 10, 14, 33, 32, 0, time.UTC),
356 Mode: 0644,
357 },
358 },
359 },
360
361 {
362 Name: "zip64-2.zip",
363 File: []ZipTestFile{
364 {
365 Name: "README",
366 Content: []byte("This small file is in ZIP64 format.\n"),
367 Modified: time.Date(2012, 8, 10, 14, 33, 32, 0, timeZone(-4*time.Hour)),
368 Mode: 0644,
369 },
370 },
371 },
372
373 {
374 Name: "big.zip",
375 Source: returnBigZipBytes,
376 File: []ZipTestFile{
377 {
378 Name: "big.file",
379 Content: nil,
380 Size: 1<<32 - 1,
381 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC),
382 Mode: 0666,
383 },
384 },
385 },
386 {
387 Name: "utf8-7zip.zip",
388 File: []ZipTestFile{
389 {
390 Name: "世界",
391 Content: []byte{},
392 Mode: 0666,
393 Modified: time.Date(2017, 11, 6, 13, 9, 27, 867862500, timeZone(-8*time.Hour)),
394 },
395 },
396 },
397 {
398 Name: "utf8-infozip.zip",
399 File: []ZipTestFile{
400 {
401 Name: "世界",
402 Content: []byte{},
403 Mode: 0644,
404
405
406
407
408 NonUTF8: true,
409 Modified: time.Date(2017, 11, 6, 13, 9, 27, 0, timeZone(-8*time.Hour)),
410 },
411 },
412 },
413 {
414 Name: "utf8-osx.zip",
415 File: []ZipTestFile{
416 {
417 Name: "世界",
418 Content: []byte{},
419 Mode: 0644,
420
421 NonUTF8: true,
422 Modified: time.Date(2017, 11, 6, 13, 9, 27, 0, timeZone(-8*time.Hour)),
423 },
424 },
425 },
426 {
427 Name: "utf8-winrar.zip",
428 File: []ZipTestFile{
429 {
430 Name: "世界",
431 Content: []byte{},
432 Mode: 0666,
433 Modified: time.Date(2017, 11, 6, 13, 9, 27, 867862500, timeZone(-8*time.Hour)),
434 },
435 },
436 },
437 {
438 Name: "utf8-winzip.zip",
439 File: []ZipTestFile{
440 {
441 Name: "世界",
442 Content: []byte{},
443 Mode: 0666,
444 Modified: time.Date(2017, 11, 6, 13, 9, 27, 867000000, timeZone(-8*time.Hour)),
445 },
446 },
447 },
448 {
449 Name: "time-7zip.zip",
450 File: []ZipTestFile{
451 {
452 Name: "test.txt",
453 Content: []byte{},
454 Size: 1<<32 - 1,
455 Modified: time.Date(2017, 10, 31, 21, 11, 57, 244817900, timeZone(-7*time.Hour)),
456 Mode: 0666,
457 },
458 },
459 },
460 {
461 Name: "time-infozip.zip",
462 File: []ZipTestFile{
463 {
464 Name: "test.txt",
465 Content: []byte{},
466 Size: 1<<32 - 1,
467 Modified: time.Date(2017, 10, 31, 21, 11, 57, 0, timeZone(-7*time.Hour)),
468 Mode: 0644,
469 },
470 },
471 },
472 {
473 Name: "time-osx.zip",
474 File: []ZipTestFile{
475 {
476 Name: "test.txt",
477 Content: []byte{},
478 Size: 1<<32 - 1,
479 Modified: time.Date(2017, 10, 31, 21, 11, 57, 0, timeZone(-7*time.Hour)),
480 Mode: 0644,
481 },
482 },
483 },
484 {
485 Name: "time-win7.zip",
486 File: []ZipTestFile{
487 {
488 Name: "test.txt",
489 Content: []byte{},
490 Size: 1<<32 - 1,
491 Modified: time.Date(2017, 10, 31, 21, 11, 58, 0, time.UTC),
492 Mode: 0666,
493 },
494 },
495 },
496 {
497 Name: "time-winrar.zip",
498 File: []ZipTestFile{
499 {
500 Name: "test.txt",
501 Content: []byte{},
502 Size: 1<<32 - 1,
503 Modified: time.Date(2017, 10, 31, 21, 11, 57, 244817900, timeZone(-7*time.Hour)),
504 Mode: 0666,
505 },
506 },
507 },
508 {
509 Name: "time-winzip.zip",
510 File: []ZipTestFile{
511 {
512 Name: "test.txt",
513 Content: []byte{},
514 Size: 1<<32 - 1,
515 Modified: time.Date(2017, 10, 31, 21, 11, 57, 244000000, timeZone(-7*time.Hour)),
516 Mode: 0666,
517 },
518 },
519 },
520 {
521 Name: "time-go.zip",
522 File: []ZipTestFile{
523 {
524 Name: "test.txt",
525 Content: []byte{},
526 Size: 1<<32 - 1,
527 Modified: time.Date(2017, 10, 31, 21, 11, 57, 0, timeZone(-7*time.Hour)),
528 Mode: 0666,
529 },
530 },
531 },
532 {
533 Name: "time-22738.zip",
534 File: []ZipTestFile{
535 {
536 Name: "file",
537 Content: []byte{},
538 Mode: 0666,
539 Modified: time.Date(1999, 12, 31, 19, 0, 0, 0, timeZone(-5*time.Hour)),
540 ModTime: time.Date(1999, 12, 31, 19, 0, 0, 0, time.UTC),
541 },
542 },
543 },
544 {
545 Name: "dupdir.zip",
546 File: []ZipTestFile{
547 {
548 Name: "a/",
549 Content: []byte{},
550 Mode: fs.ModeDir | 0666,
551 Modified: time.Date(2021, 12, 29, 0, 0, 0, 0, timeZone(0)),
552 },
553 {
554 Name: "a/b",
555 Content: []byte{},
556 Mode: 0666,
557 Modified: time.Date(2021, 12, 29, 0, 0, 0, 0, timeZone(0)),
558 },
559 {
560 Name: "a/b/",
561 Content: []byte{},
562 Mode: fs.ModeDir | 0666,
563 Modified: time.Date(2021, 12, 29, 0, 0, 0, 0, timeZone(0)),
564 },
565 {
566 Name: "a/b/c",
567 Content: []byte{},
568 Mode: 0666,
569 Modified: time.Date(2021, 12, 29, 0, 0, 0, 0, timeZone(0)),
570 },
571 },
572 },
573 }
574
575 func TestReader(t *testing.T) {
576 for _, zt := range tests {
577 t.Run(zt.Name, func(t *testing.T) {
578 readTestZip(t, zt)
579 })
580 }
581 }
582
583 func readTestZip(t *testing.T, zt ZipTest) {
584 var z *Reader
585 var err error
586 var raw []byte
587 if zt.Source != nil {
588 rat, size := zt.Source()
589 z, err = NewReader(rat, size)
590 raw = make([]byte, size)
591 if _, err := rat.ReadAt(raw, 0); err != nil {
592 t.Errorf("ReadAt error=%v", err)
593 return
594 }
595 } else {
596 path := filepath.Join("testdata", zt.Name)
597 if zt.Obscured {
598 tf, err := obscuretestdata.DecodeToTempFile(path)
599 if err != nil {
600 t.Errorf("obscuretestdata.DecodeToTempFile(%s): %v", path, err)
601 return
602 }
603 defer os.Remove(tf)
604 path = tf
605 }
606 var rc *ReadCloser
607 rc, err = OpenReader(path)
608 if err == nil {
609 defer rc.Close()
610 z = &rc.Reader
611 }
612 var err2 error
613 raw, err2 = os.ReadFile(path)
614 if err2 != nil {
615 t.Errorf("ReadFile(%s) error=%v", path, err2)
616 return
617 }
618 }
619 if err != zt.Error {
620 t.Errorf("error=%v, want %v", err, zt.Error)
621 return
622 }
623
624
625 if err == ErrFormat {
626 return
627 }
628
629
630
631 if zt.File == nil {
632 return
633 }
634
635 if z.Comment != zt.Comment {
636 t.Errorf("comment=%q, want %q", z.Comment, zt.Comment)
637 }
638 if len(z.File) != len(zt.File) {
639 t.Fatalf("file count=%d, want %d", len(z.File), len(zt.File))
640 }
641
642
643 for i, ft := range zt.File {
644 readTestFile(t, zt, ft, z.File[i], raw)
645 }
646 if t.Failed() {
647 return
648 }
649
650
651 n := 0
652 done := make(chan bool)
653 for i := 0; i < 5; i++ {
654 for j, ft := range zt.File {
655 go func(j int, ft ZipTestFile) {
656 readTestFile(t, zt, ft, z.File[j], raw)
657 done <- true
658 }(j, ft)
659 n++
660 }
661 }
662 for ; n > 0; n-- {
663 <-done
664 }
665 }
666
667 func equalTimeAndZone(t1, t2 time.Time) bool {
668 name1, offset1 := t1.Zone()
669 name2, offset2 := t2.Zone()
670 return t1.Equal(t2) && name1 == name2 && offset1 == offset2
671 }
672
673 func readTestFile(t *testing.T, zt ZipTest, ft ZipTestFile, f *File, raw []byte) {
674 if f.Name != ft.Name {
675 t.Errorf("name=%q, want %q", f.Name, ft.Name)
676 }
677 if !ft.Modified.IsZero() && !equalTimeAndZone(f.Modified, ft.Modified) {
678 t.Errorf("%s: Modified=%s, want %s", f.Name, f.Modified, ft.Modified)
679 }
680 if !ft.ModTime.IsZero() && !equalTimeAndZone(f.ModTime(), ft.ModTime) {
681 t.Errorf("%s: ModTime=%s, want %s", f.Name, f.ModTime(), ft.ModTime)
682 }
683
684 testFileMode(t, f, ft.Mode)
685
686 size := uint64(f.UncompressedSize)
687 if size == uint32max {
688 size = f.UncompressedSize64
689 } else if size != f.UncompressedSize64 {
690 t.Errorf("%v: UncompressedSize=%#x does not match UncompressedSize64=%#x", f.Name, size, f.UncompressedSize64)
691 }
692
693
694 rw, err := f.OpenRaw()
695 if err != nil {
696 t.Errorf("%v: OpenRaw error=%v", f.Name, err)
697 return
698 }
699 start, err := f.DataOffset()
700 if err != nil {
701 t.Errorf("%v: DataOffset error=%v", f.Name, err)
702 return
703 }
704 got, err := io.ReadAll(rw)
705 if err != nil {
706 t.Errorf("%v: OpenRaw ReadAll error=%v", f.Name, err)
707 return
708 }
709 end := uint64(start) + f.CompressedSize64
710 want := raw[start:end]
711 if !bytes.Equal(got, want) {
712 t.Logf("got %q", got)
713 t.Logf("want %q", want)
714 t.Errorf("%v: OpenRaw returned unexpected bytes", f.Name)
715 return
716 }
717
718 r, err := f.Open()
719 if err != nil {
720 t.Errorf("%v", err)
721 return
722 }
723
724
725
726
727 if ft.Content == nil && ft.File == "" && ft.Size > 0 {
728 if size != ft.Size {
729 t.Errorf("%v: uncompressed size %#x, want %#x", ft.Name, size, ft.Size)
730 }
731 r.Close()
732 return
733 }
734
735 var b bytes.Buffer
736 _, err = io.Copy(&b, r)
737 if err != ft.ContentErr {
738 t.Errorf("copying contents: %v (want %v)", err, ft.ContentErr)
739 }
740 if err != nil {
741 return
742 }
743 r.Close()
744
745 if g := uint64(b.Len()); g != size {
746 t.Errorf("%v: read %v bytes but f.UncompressedSize == %v", f.Name, g, size)
747 }
748
749 var c []byte
750 if ft.Content != nil {
751 c = ft.Content
752 } else if c, err = os.ReadFile("testdata/" + ft.File); err != nil {
753 t.Error(err)
754 return
755 }
756
757 if b.Len() != len(c) {
758 t.Errorf("%s: len=%d, want %d", f.Name, b.Len(), len(c))
759 return
760 }
761
762 for i, b := range b.Bytes() {
763 if b != c[i] {
764 t.Errorf("%s: content[%d]=%q want %q", f.Name, i, b, c[i])
765 return
766 }
767 }
768 }
769
770 func testFileMode(t *testing.T, f *File, want fs.FileMode) {
771 mode := f.Mode()
772 if want == 0 {
773 t.Errorf("%s mode: got %v, want none", f.Name, mode)
774 } else if mode != want {
775 t.Errorf("%s mode: want %v, got %v", f.Name, want, mode)
776 }
777 }
778
779 func TestInvalidFiles(t *testing.T) {
780 const size = 1024 * 70
781 b := make([]byte, size)
782
783
784 _, err := NewReader(bytes.NewReader(b), size)
785 if err != ErrFormat {
786 t.Errorf("zeroes: error=%v, want %v", err, ErrFormat)
787 }
788
789
790 sig := make([]byte, 4)
791 binary.LittleEndian.PutUint32(sig, directoryEndSignature)
792 for i := 0; i < size-4; i += 4 {
793 copy(b[i:i+4], sig)
794 }
795 _, err = NewReader(bytes.NewReader(b), size)
796 if err != ErrFormat {
797 t.Errorf("sigs: error=%v, want %v", err, ErrFormat)
798 }
799
800
801 _, err = NewReader(bytes.NewReader([]byte("foobar")), -1)
802 if err == nil {
803 t.Errorf("archive/zip.NewReader: expected error when negative size is passed")
804 }
805 }
806
807 func messWith(fileName string, corrupter func(b []byte)) (r io.ReaderAt, size int64) {
808 data, err := os.ReadFile(filepath.Join("testdata", fileName))
809 if err != nil {
810 panic("Error reading " + fileName + ": " + err.Error())
811 }
812 corrupter(data)
813 return bytes.NewReader(data), int64(len(data))
814 }
815
816 func returnCorruptCRC32Zip() (r io.ReaderAt, size int64) {
817 return messWith("go-with-datadesc-sig.zip", func(b []byte) {
818
819 b[0x2d]++
820 })
821 }
822
823 func returnCorruptNotStreamedZip() (r io.ReaderAt, size int64) {
824 return messWith("crc32-not-streamed.zip", func(b []byte) {
825
826
827 b[0x11]++
828 b[0x9d]++
829
830
831
832
833
834
835
836 })
837 }
838
839
840
841 func rZipBytes() []byte {
842 s := `
843 0000000 50 4b 03 04 14 00 00 00 08 00 08 03 64 3c f9 f4
844 0000010 89 64 48 01 00 00 b8 01 00 00 07 00 00 00 72 2f
845 0000020 72 2e 7a 69 70 00 25 00 da ff 50 4b 03 04 14 00
846 0000030 00 00 08 00 08 03 64 3c f9 f4 89 64 48 01 00 00
847 0000040 b8 01 00 00 07 00 00 00 72 2f 72 2e 7a 69 70 00
848 0000050 2f 00 d0 ff 00 25 00 da ff 50 4b 03 04 14 00 00
849 0000060 00 08 00 08 03 64 3c f9 f4 89 64 48 01 00 00 b8
850 0000070 01 00 00 07 00 00 00 72 2f 72 2e 7a 69 70 00 2f
851 0000080 00 d0 ff c2 54 8e 57 39 00 05 00 fa ff c2 54 8e
852 0000090 57 39 00 05 00 fa ff 00 05 00 fa ff 00 14 00 eb
853 00000a0 ff c2 54 8e 57 39 00 05 00 fa ff 00 05 00 fa ff
854 00000b0 00 14 00 eb ff 42 88 21 c4 00 00 14 00 eb ff 42
855 00000c0 88 21 c4 00 00 14 00 eb ff 42 88 21 c4 00 00 14
856 00000d0 00 eb ff 42 88 21 c4 00 00 14 00 eb ff 42 88 21
857 00000e0 c4 00 00 00 00 ff ff 00 00 00 ff ff 00 34 00 cb
858 00000f0 ff 42 88 21 c4 00 00 00 00 ff ff 00 00 00 ff ff
859 0000100 00 34 00 cb ff 42 e8 21 5e 0f 00 00 00 ff ff 0a
860 0000110 f0 66 64 12 61 c0 15 dc e8 a0 48 bf 48 af 2a b3
861 0000120 20 c0 9b 95 0d c4 67 04 42 53 06 06 06 40 00 06
862 0000130 00 f9 ff 6d 01 00 00 00 00 42 e8 21 5e 0f 00 00
863 0000140 00 ff ff 0a f0 66 64 12 61 c0 15 dc e8 a0 48 bf
864 0000150 48 af 2a b3 20 c0 9b 95 0d c4 67 04 42 53 06 06
865 0000160 06 40 00 06 00 f9 ff 6d 01 00 00 00 00 50 4b 01
866 0000170 02 14 00 14 00 00 00 08 00 08 03 64 3c f9 f4 89
867 0000180 64 48 01 00 00 b8 01 00 00 07 00 00 00 00 00 00
868 0000190 00 00 00 00 00 00 00 00 00 00 00 72 2f 72 2e 7a
869 00001a0 69 70 50 4b 05 06 00 00 00 00 01 00 01 00 35 00
870 00001b0 00 00 6d 01 00 00 00 00`
871 s = regexp.MustCompile(`[0-9a-f]{7}`).ReplaceAllString(s, "")
872 s = regexp.MustCompile(`\s+`).ReplaceAllString(s, "")
873 b, err := hex.DecodeString(s)
874 if err != nil {
875 panic(err)
876 }
877 return b
878 }
879
880 func returnRecursiveZip() (r io.ReaderAt, size int64) {
881 b := rZipBytes()
882 return bytes.NewReader(b), int64(len(b))
883 }
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949 func biggestZipBytes() []byte {
950 s := `
951 0000000 50 4b 03 04 14 00 08 00 08 00 00 00 00 00 00 00
952 0000010 00 00 00 00 00 00 00 00 00 00 0a 00 00 00 62 69
953 0000020 67 67 65 72 2e 7a 69 70 ec dc 6b 4c 53 67 18 07
954 0000030 f0 16 c5 ca 65 2e cb b8 94 20 61 1f 44 33 c7 cd
955 0000040 c0 86 4a b5 c0 62 8a 61 05 c6 cd 91 b2 54 8c 1b
956 0000050 63 8b 03 9c 1b 95 52 5a e3 a0 19 6c b2 05 59 44
957 0000060 64 9d 73 83 71 11 46 61 14 b9 1d 14 09 4a c3 60
958 0000070 2e 4c 6e a5 60 45 02 62 81 95 b6 94 9e 9e 77 e7
959 0000080 d0 43 b6 f8 71 df 96 3c e7 a4 69 ce bf cf e9 79
960 0000090 ce ef 79 3f bf f1 31 db b6 bb 31 76 92 e7 f3 07
961 00000a0 8b fc 9c ca cc 08 cc cb cc 5e d2 1c 88 d9 7e bb
962 00000b0 4f bb 3a 3f 75 f1 5d 7f 8f c2 68 67 77 8f 25 ff
963 00000c0 84 e2 93 2d ef a4 95 3d 71 4e 2c b9 b0 87 c3 be
964 00000d0 3d f8 a7 60 24 61 c5 ef ae 9e c8 6c 6d 4e 69 c8
965 00000e0 67 65 34 f8 37 76 2d 76 5c 54 f3 95 65 49 c7 0f
966 00000f0 18 71 4b 7e 5b 6a d1 79 47 61 41 b0 4e 2a 74 45
967 0000100 43 58 12 b2 5a a5 c6 7d 68 55 88 d4 98 75 18 6d
968 0000110 08 d1 1f 8f 5a 9e 96 ee 45 cf a4 84 4e 4b e8 50
969 0000120 a7 13 d9 06 de 52 81 97 36 b2 d7 b8 fc 2b 5f 55
970 0000130 23 1f 32 59 cf 30 27 fb e2 8a b9 de 45 dd 63 9c
971 0000140 4b b5 8b 96 4c 7a 62 62 cc a1 a7 cf fa f1 fe dd
972 0000150 54 62 11 bf 36 78 b3 c7 b1 b5 f2 61 4d 4e dd 66
973 0000160 32 2e e6 70 34 5f f4 c9 e6 6c 43 6f da 6b c6 c3
974 0000170 09 2c ce 09 57 7f d2 7e b4 23 ba 7c 1b 99 bc 22
975 0000180 3e f1 de 91 2f e3 9c 1b 82 cc c2 84 39 aa e6 de
976 0000190 b4 69 fc cc cb 72 a6 61 45 f0 d3 1d 26 19 7c 8d
977 00001a0 29 c8 66 02 be 77 6a f9 3d 34 79 17 19 c8 96 24
978 00001b0 a3 ac e4 dd 3b 1a 8e c6 fe 96 38 6b bf 67 5a 23
979 00001c0 f4 16 f4 e6 8a b4 fc c2 cd bf 95 66 1d bb 35 aa
980 00001d0 92 7d 66 d8 08 8d a5 1f 54 2a af 09 cf 61 ff d2
981 00001e0 85 9d 8f b6 d7 88 07 4a 86 03 db 64 f3 d9 92 73
982 00001f0 df ec a7 fc 23 4c 8d 83 79 63 2a d9 fd 8d b3 c8
983 0000200 8f 7e d4 19 85 e6 8d 1c 76 f0 8b 58 32 fd 9a d6
984 0000210 85 e2 48 ad c3 d5 60 6f 7e 22 dd ef 09 49 7c 7f
985 0000220 3a 45 c3 71 b7 df f3 4c 63 fb b5 d9 31 5f 6e d6
986 0000230 24 1d a4 4a fe 32 a7 5c 16 48 5c 3e 08 6b 8a d3
987 0000240 25 1d a2 12 a5 59 24 ea 20 5f 52 6d ad 94 db 6b
988 0000250 94 b9 5d eb 4b a7 5c 44 bb 1e f2 3c 6b cf 52 c9
989 0000260 e9 e5 ba 06 b9 c4 e5 0a d0 00 0d d0 00 0d d0 00
990 0000270 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d
991 0000280 d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0
992 0000290 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00
993 00002a0 0d d0 00 cd ff 9e 46 86 fa a7 7d 3a 43 d7 8e 10
994 00002b0 52 e9 be e6 6e cf eb 9e 85 4d 65 ce cc 30 c1 44
995 00002c0 c0 4e af bc 9c 6c 4b a0 d7 54 ff 1d d5 5c 89 fb
996 00002d0 b5 34 7e c4 c2 9e f5 a0 f6 5b 7e 6e ca 73 c7 ef
997 00002e0 5d be de f9 e8 81 eb a5 0a a5 63 54 2c d7 1c d1
998 00002f0 89 17 85 f8 16 94 f2 8a b2 a3 f5 b6 6d df 75 cd
999 0000300 90 dd 64 bd 5d 55 4e f2 55 19 1b b7 cc ef 1b ea
1000 0000310 2e 05 9c f4 aa 1e a8 cd a6 82 c7 59 0f 5e 9d e0
1001 0000320 bb fc 6c d6 99 23 eb 36 ad c6 c5 e1 d8 e1 e2 3e
1002 0000330 d9 90 5a f7 91 5d 6f bc 33 6d 98 47 d2 7c 2e 2f
1003 0000340 99 a4 25 72 85 49 2c be 0b 5b af 8f e5 6e 81 a6
1004 0000350 a3 5a 6f 39 53 3a ab 7a 8b 1e 26 f7 46 6c 7d 26
1005 0000360 53 b3 22 31 94 d3 83 f2 18 4d f5 92 33 27 53 97
1006 0000370 0f d3 e6 55 9c a6 c5 31 87 6f d3 f3 ae 39 6f 56
1007 0000380 10 7b ab 7e d0 b4 ca f2 b8 05 be 3f 0e 6e 5a 75
1008 0000390 ab 0c f5 37 0e ba 8e 75 71 7a aa ed 7a dd 6a 63
1009 00003a0 be 9b a0 97 27 6a 6f e7 d3 8b c4 7c ec d3 91 56
1010 00003b0 d9 ac 5e bf 16 42 2f 00 1f 93 a2 23 87 bd e2 59
1011 00003c0 a0 de 1a 66 c8 62 eb 55 8f 91 17 b4 61 42 7a 50
1012 00003d0 40 03 34 40 03 34 40 03 34 40 03 34 40 03 34 40
1013 00003e0 03 34 40 03 34 40 03 34 40 03 34 40 03 34 40 03
1014 00003f0 34 40 03 34 40 03 34 ff 85 86 90 8b ea 67 90 0d
1015 0000400 e1 42 1b d2 61 d6 79 ec fd 3e 44 28 a4 51 6c 5c
1016 0000410 fc d2 72 ca ba 82 18 46 16 61 cd 93 a9 0f d1 24
1017 0000420 17 99 e2 2c 71 16 84 0c c8 7a 13 0f 9a 5e c5 f0
1018 0000430 79 64 e2 12 4d c8 82 a1 81 19 2d aa 44 6d 87 54
1019 0000440 84 71 c1 f6 d4 ca 25 8c 77 b9 08 c7 c8 5e 10 8a
1020 0000450 8f 61 ed 8c ba 30 1f 79 9a c7 60 34 2b b9 8c f8
1021 0000460 18 a6 83 1b e3 9f ad 79 fe fd 1b 8b f1 fc 41 6f
1022 0000470 d4 13 1f e3 b8 83 ba 64 92 e7 eb e4 77 05 8f ba
1023 0000480 fa 3b 00 00 ff ff 50 4b 07 08 a6 18 b1 91 5e 04
1024 0000490 00 00 e4 47 00 00 50 4b 01 02 14 00 14 00 08 00
1025 00004a0 08 00 00 00 00 00 a6 18 b1 91 5e 04 00 00 e4 47
1026 00004b0 00 00 0a 00 00 00 00 00 00 00 00 00 00 00 00 00
1027 00004c0 00 00 00 00 62 69 67 67 65 72 2e 7a 69 70 50 4b
1028 00004d0 05 06 00 00 00 00 01 00 01 00 38 00 00 00 96 04
1029 00004e0 00 00 00 00`
1030 s = regexp.MustCompile(`[0-9a-f]{7}`).ReplaceAllString(s, "")
1031 s = regexp.MustCompile(`\s+`).ReplaceAllString(s, "")
1032 b, err := hex.DecodeString(s)
1033 if err != nil {
1034 panic(err)
1035 }
1036 return b
1037 }
1038
1039 func returnBigZipBytes() (r io.ReaderAt, size int64) {
1040 b := biggestZipBytes()
1041 for i := 0; i < 2; i++ {
1042 r, err := NewReader(bytes.NewReader(b), int64(len(b)))
1043 if err != nil {
1044 panic(err)
1045 }
1046 f, err := r.File[0].Open()
1047 if err != nil {
1048 panic(err)
1049 }
1050 b, err = io.ReadAll(f)
1051 if err != nil {
1052 panic(err)
1053 }
1054 }
1055 return bytes.NewReader(b), int64(len(b))
1056 }
1057
1058 func TestIssue8186(t *testing.T) {
1059
1060 dirEnts := []string{
1061 "PK\x01\x02\n\x00\n\x00\x00\b\x00\x004\x9d3?\xaa\x1b\x06\xf0\x81\x02\x00\x00\x81\x02\x00\x00-\x00\x05\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00res/drawable-xhdpi-v4/ic_actionbar_accept.png\xfe\xca\x00\x00\x00",
1062 "PK\x01\x02\n\x00\n\x00\x00\b\x00\x004\x9d3?\x90K\x89\xc7t\n\x00\x00t\n\x00\x00\x0e\x00\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd1\x02\x00\x00resources.arsc\x00\x00\x00",
1063 "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?\xff$\x18\xed3\x03\x00\x00\xb4\b\x00\x00\x13\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00t\r\x00\x00AndroidManifest.xml",
1064 "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?\x14\xc5K\xab\x192\x02\x00\xc8\xcd\x04\x00\v\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xe8\x10\x00\x00classes.dex",
1065 "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?E\x96\nD\xac\x01\x00\x00P\x03\x00\x00&\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00:C\x02\x00res/layout/actionbar_set_wallpaper.xml",
1066 "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?Ļ\x14\xe3\xd8\x01\x00\x00\xd8\x03\x00\x00 \x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00:E\x02\x00res/layout/wallpaper_cropper.xml",
1067 "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?}\xc1\x15\x9eZ\x01\x00\x00!\x02\x00\x00\x14\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00`G\x02\x00META-INF/MANIFEST.MF",
1068 "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?\xe6\x98Ьo\x01\x00\x00\x84\x02\x00\x00\x10\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xfcH\x02\x00META-INF/CERT.SF",
1069 "PK\x01\x02\x14\x00\x14\x00\b\b\b\x004\x9d3?\xbfP\x96b\x86\x04\x00\x00\xb2\x06\x00\x00\x11\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xa9J\x02\x00META-INF/CERT.RSA",
1070 }
1071 for i, s := range dirEnts {
1072 var f File
1073 err := readDirectoryHeader(&f, strings.NewReader(s))
1074 if err != nil {
1075 t.Errorf("error reading #%d: %v", i, err)
1076 }
1077 }
1078 }
1079
1080
1081 func TestIssue10957(t *testing.T) {
1082 data := []byte("PK\x03\x040000000PK\x01\x0200000" +
1083 "0000000000000000000\x00" +
1084 "\x00\x00\x00\x00\x00000000000000PK\x01" +
1085 "\x020000000000000000000" +
1086 "00000\v\x00\x00\x00\x00\x00000000000" +
1087 "00000000000000PK\x01\x0200" +
1088 "00000000000000000000" +
1089 "00\v\x00\x00\x00\x00\x00000000000000" +
1090 "00000000000PK\x01\x020000<" +
1091 "0\x00\x0000000000000000\v\x00\v" +
1092 "\x00\x00\x00\x00\x0000000000\x00\x00\x00\x00000" +
1093 "00000000PK\x01\x0200000000" +
1094 "0000000000000000\v\x00\x00\x00" +
1095 "\x00\x0000PK\x05\x06000000\x05\x00\xfd\x00\x00\x00" +
1096 "\v\x00\x00\x00\x00\x00")
1097 z, err := NewReader(bytes.NewReader(data), int64(len(data)))
1098 if err != nil {
1099 t.Fatal(err)
1100 }
1101 for i, f := range z.File {
1102 r, err := f.Open()
1103 if err != nil {
1104 continue
1105 }
1106 if f.UncompressedSize64 < 1e6 {
1107 n, err := io.Copy(io.Discard, r)
1108 if i == 3 && err != io.ErrUnexpectedEOF {
1109 t.Errorf("File[3] error = %v; want io.ErrUnexpectedEOF", err)
1110 }
1111 if err == nil && uint64(n) != f.UncompressedSize64 {
1112 t.Errorf("file %d: bad size: copied=%d; want=%d", i, n, f.UncompressedSize64)
1113 }
1114 }
1115 r.Close()
1116 }
1117 }
1118
1119
1120 func TestIssue10956(t *testing.T) {
1121 data := []byte("PK\x06\x06PK\x06\a0000\x00\x00\x00\x00\x00\x00\x00\x00" +
1122 "0000PK\x05\x06000000000000" +
1123 "0000\v\x00000\x00\x00\x00\x00\x00\x00\x000")
1124 r, err := NewReader(bytes.NewReader(data), int64(len(data)))
1125 if err == nil {
1126 t.Errorf("got nil error, want ErrFormat")
1127 }
1128 if r != nil {
1129 t.Errorf("got non-nil Reader, want nil")
1130 }
1131 }
1132
1133
1134 func TestIssue11146(t *testing.T) {
1135 data := []byte("PK\x03\x040000000000000000" +
1136 "000000\x01\x00\x00\x000\x01\x00\x00\xff\xff0000" +
1137 "0000000000000000PK\x01\x02" +
1138 "0000\b0\b\x00000000000000" +
1139 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x000000PK\x05\x06\x00\x00" +
1140 "\x00\x0000\x01\x00\x26\x00\x00\x008\x00\x00\x00\x00\x00")
1141 z, err := NewReader(bytes.NewReader(data), int64(len(data)))
1142 if err != nil {
1143 t.Fatal(err)
1144 }
1145 r, err := z.File[0].Open()
1146 if err != nil {
1147 t.Fatal(err)
1148 }
1149 _, err = io.ReadAll(r)
1150 if err != io.ErrUnexpectedEOF {
1151 t.Errorf("File[0] error = %v; want io.ErrUnexpectedEOF", err)
1152 }
1153 r.Close()
1154 }
1155
1156
1157 func TestIssue12449(t *testing.T) {
1158 data := []byte{
1159 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00,
1160 0x00, 0x00, 0x6b, 0xb4, 0xba, 0x46, 0x00, 0x00,
1161 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1162 0x00, 0x00, 0x03, 0x00, 0x18, 0x00, 0xca, 0x64,
1163 0x55, 0x75, 0x78, 0x0b, 0x00, 0x50, 0x4b, 0x05,
1164 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
1165 0x00, 0x49, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00,
1166 0x00, 0x31, 0x31, 0x31, 0x32, 0x32, 0x32, 0x0a,
1167 0x50, 0x4b, 0x07, 0x08, 0x1d, 0x88, 0x77, 0xb0,
1168 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
1169 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00,
1170 0x08, 0x00, 0x00, 0x00, 0x6b, 0xb4, 0xba, 0x46,
1171 0x1d, 0x88, 0x77, 0xb0, 0x07, 0x00, 0x00, 0x00,
1172 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x18, 0x00,
1173 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1174 0xa0, 0x81, 0x00, 0x00, 0x00, 0x00, 0xca, 0x64,
1175 0x55, 0x75, 0x78, 0x0b, 0x00, 0x50, 0x4b, 0x05,
1176 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
1177 0x00, 0x49, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00,
1178 0x00, 0x97, 0x2b, 0x49, 0x23, 0x05, 0xc5, 0x0b,
1179 0xa7, 0xd1, 0x52, 0xa2, 0x9c, 0x50, 0x4b, 0x06,
1180 0x07, 0xc8, 0x19, 0xc1, 0xaf, 0x94, 0x9c, 0x61,
1181 0x44, 0xbe, 0x94, 0x19, 0x42, 0x58, 0x12, 0xc6,
1182 0x5b, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00,
1183 0x00, 0x01, 0x00, 0x01, 0x00, 0x69, 0x00, 0x00,
1184 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00,
1185 }
1186
1187 _, err := NewReader(bytes.NewReader(data), int64(len(data)))
1188 if err != nil {
1189 t.Errorf("Error reading the archive: %v", err)
1190 }
1191 }
1192
1193 func TestFS(t *testing.T) {
1194 for _, test := range []struct {
1195 file string
1196 want []string
1197 }{
1198 {
1199 "testdata/unix.zip",
1200 []string{"hello", "dir/bar", "readonly"},
1201 },
1202 {
1203 "testdata/subdir.zip",
1204 []string{"a/b/c"},
1205 },
1206 } {
1207 test := test
1208 t.Run(test.file, func(t *testing.T) {
1209 t.Parallel()
1210 z, err := OpenReader(test.file)
1211 if err != nil {
1212 t.Fatal(err)
1213 }
1214 defer z.Close()
1215 if err := fstest.TestFS(z, test.want...); err != nil {
1216 t.Error(err)
1217 }
1218 })
1219 }
1220 }
1221
1222 func TestFSWalk(t *testing.T) {
1223 for _, test := range []struct {
1224 file string
1225 want []string
1226 wantErr bool
1227 }{
1228 {
1229 file: "testdata/unix.zip",
1230 want: []string{".", "dir", "dir/bar", "dir/empty", "hello", "readonly"},
1231 },
1232 {
1233 file: "testdata/subdir.zip",
1234 want: []string{".", "a", "a/b", "a/b/c"},
1235 },
1236 {
1237 file: "testdata/dupdir.zip",
1238 wantErr: true,
1239 },
1240 } {
1241 test := test
1242 t.Run(test.file, func(t *testing.T) {
1243 t.Parallel()
1244 z, err := OpenReader(test.file)
1245 if err != nil {
1246 t.Fatal(err)
1247 }
1248 var files []string
1249 sawErr := false
1250 err = fs.WalkDir(z, ".", func(path string, d fs.DirEntry, err error) error {
1251 if err != nil {
1252 if !test.wantErr {
1253 t.Errorf("%s: %v", path, err)
1254 }
1255 sawErr = true
1256 return nil
1257 }
1258 files = append(files, path)
1259 return nil
1260 })
1261 if err != nil {
1262 t.Errorf("fs.WalkDir error: %v", err)
1263 }
1264 if test.wantErr && !sawErr {
1265 t.Error("succeeded but want error")
1266 } else if !test.wantErr && sawErr {
1267 t.Error("unexpected error")
1268 }
1269 if test.want != nil && !reflect.DeepEqual(files, test.want) {
1270 t.Errorf("got %v want %v", files, test.want)
1271 }
1272 })
1273 }
1274 }
1275
1276 func TestFSModTime(t *testing.T) {
1277 t.Parallel()
1278 z, err := OpenReader("testdata/subdir.zip")
1279 if err != nil {
1280 t.Fatal(err)
1281 }
1282 defer z.Close()
1283
1284 for _, test := range []struct {
1285 name string
1286 want time.Time
1287 }{
1288 {
1289 "a",
1290 time.Date(2021, 4, 19, 12, 29, 56, 0, timeZone(-7*time.Hour)).UTC(),
1291 },
1292 {
1293 "a/b/c",
1294 time.Date(2021, 4, 19, 12, 29, 59, 0, timeZone(-7*time.Hour)).UTC(),
1295 },
1296 } {
1297 fi, err := fs.Stat(z, test.name)
1298 if err != nil {
1299 t.Errorf("%s: %v", test.name, err)
1300 continue
1301 }
1302 if got := fi.ModTime(); !got.Equal(test.want) {
1303 t.Errorf("%s: got modtime %v, want %v", test.name, got, test.want)
1304 }
1305 }
1306 }
1307
1308 func TestCVE202127919(t *testing.T) {
1309 t.Setenv("GODEBUG", "zipinsecurepath=0")
1310
1311 data := []byte{
1312 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00,
1313 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1314 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1315 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x2e, 0x2e,
1316 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
1317 0x74, 0x0a, 0xc9, 0xc8, 0x2c, 0x56, 0xc8, 0x2c,
1318 0x56, 0x48, 0x54, 0x28, 0x49, 0x2d, 0x2e, 0x51,
1319 0x28, 0x49, 0xad, 0x28, 0x51, 0x48, 0xcb, 0xcc,
1320 0x49, 0xd5, 0xe3, 0x02, 0x04, 0x00, 0x00, 0xff,
1321 0xff, 0x50, 0x4b, 0x07, 0x08, 0xc0, 0xd7, 0xed,
1322 0xc3, 0x20, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00,
1323 0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x00, 0x14,
1324 0x00, 0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
1325 0x00, 0xc0, 0xd7, 0xed, 0xc3, 0x20, 0x00, 0x00,
1326 0x00, 0x1a, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00,
1327 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1328 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2e,
1329 0x2e, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
1330 0x78, 0x74, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00,
1331 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x39, 0x00,
1332 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00,
1333 }
1334 r, err := NewReader(bytes.NewReader(data), int64(len(data)))
1335 if err != ErrInsecurePath {
1336 t.Fatalf("Error reading the archive: %v", err)
1337 }
1338 _, err = r.Open("test.txt")
1339 if err != nil {
1340 t.Errorf("Error reading file: %v", err)
1341 }
1342 if len(r.File) != 1 {
1343 t.Fatalf("No entries in the file list")
1344 }
1345 if r.File[0].Name != "../test.txt" {
1346 t.Errorf("Unexpected entry name: %s", r.File[0].Name)
1347 }
1348 if _, err := r.File[0].Open(); err != nil {
1349 t.Errorf("Error opening file: %v", err)
1350 }
1351 }
1352
1353 func TestOpenReaderInsecurePath(t *testing.T) {
1354 t.Setenv("GODEBUG", "zipinsecurepath=0")
1355
1356 data := []byte{
1357 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00,
1358 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1359 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1360 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x2e, 0x2e,
1361 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
1362 0x74, 0x0a, 0xc9, 0xc8, 0x2c, 0x56, 0xc8, 0x2c,
1363 0x56, 0x48, 0x54, 0x28, 0x49, 0x2d, 0x2e, 0x51,
1364 0x28, 0x49, 0xad, 0x28, 0x51, 0x48, 0xcb, 0xcc,
1365 0x49, 0xd5, 0xe3, 0x02, 0x04, 0x00, 0x00, 0xff,
1366 0xff, 0x50, 0x4b, 0x07, 0x08, 0xc0, 0xd7, 0xed,
1367 0xc3, 0x20, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00,
1368 0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x00, 0x14,
1369 0x00, 0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
1370 0x00, 0xc0, 0xd7, 0xed, 0xc3, 0x20, 0x00, 0x00,
1371 0x00, 0x1a, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00,
1372 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1373 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2e,
1374 0x2e, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
1375 0x78, 0x74, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00,
1376 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x39, 0x00,
1377 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00,
1378 }
1379
1380
1381 name := filepath.Join(t.TempDir(), "test.zip")
1382 err := os.WriteFile(name, data, 0644)
1383 if err != nil {
1384 t.Fatalf("Unable to write out the bugos zip entry")
1385 }
1386 r, err := OpenReader(name)
1387 if r != nil {
1388 defer r.Close()
1389 }
1390
1391 if err != ErrInsecurePath {
1392 t.Fatalf("Error reading the archive, we expected ErrInsecurePath but got: %v", err)
1393 }
1394 _, err = r.Open("test.txt")
1395 if err != nil {
1396 t.Errorf("Error reading file: %v", err)
1397 }
1398 if len(r.File) != 1 {
1399 t.Fatalf("No entries in the file list")
1400 }
1401 if r.File[0].Name != "../test.txt" {
1402 t.Errorf("Unexpected entry name: %s", r.File[0].Name)
1403 }
1404 if _, err := r.File[0].Open(); err != nil {
1405 t.Errorf("Error opening file: %v", err)
1406 }
1407 }
1408
1409 func TestCVE202133196(t *testing.T) {
1410
1411
1412
1413 data := []byte{
1414 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x08,
1415 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1416 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1417 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x02,
1418 0x03, 0x62, 0x61, 0x65, 0x03, 0x04, 0x00, 0x00,
1419 0xff, 0xff, 0x50, 0x4b, 0x07, 0x08, 0xbe, 0x20,
1420 0x5c, 0x6c, 0x09, 0x00, 0x00, 0x00, 0x03, 0x00,
1421 0x00, 0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x00,
1422 0x14, 0x00, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00,
1423 0x00, 0x00, 0xbe, 0x20, 0x5c, 0x6c, 0x09, 0x00,
1424 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00,
1425 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1426 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1427 0x01, 0x02, 0x03, 0x50, 0x4b, 0x06, 0x06, 0x2c,
1428 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d,
1429 0x00, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1430 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
1431 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
1432 0xff, 0xff, 0xff, 0x31, 0x00, 0x00, 0x00, 0x00,
1433 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00, 0x00,
1434 0x00, 0x00, 0x00, 0x50, 0x4b, 0x06, 0x07, 0x00,
1435 0x00, 0x00, 0x00, 0x6b, 0x00, 0x00, 0x00, 0x00,
1436 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x50,
1437 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0xff,
1438 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1439 0xff, 0xff, 0xff, 0x00, 0x00,
1440 }
1441 _, err := NewReader(bytes.NewReader(data), int64(len(data)))
1442 if err != ErrFormat {
1443 t.Fatalf("unexpected error, got: %v, want: %v", err, ErrFormat)
1444 }
1445
1446
1447
1448 b := bytes.NewBuffer(nil)
1449 w := NewWriter(b)
1450 for i := 0; i < 5; i++ {
1451 _, err := w.Create("")
1452 if err != nil {
1453 t.Fatalf("Writer.Create failed: %s", err)
1454 }
1455 }
1456 if err := w.Close(); err != nil {
1457 t.Fatalf("Writer.Close failed: %s", err)
1458 }
1459 r, err := NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))
1460 if err != nil {
1461 t.Fatalf("NewReader failed: %s", err)
1462 }
1463 if len(r.File) != 5 {
1464 t.Errorf("Archive has unexpected number of files, got %d, want 5", len(r.File))
1465 }
1466 }
1467
1468 func TestCVE202139293(t *testing.T) {
1469
1470
1471
1472 data := []byte{
1473 0x50, 0x4b, 0x06, 0x06, 0x05, 0x06, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b,
1474 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1475 0x00, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b,
1476 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1477 0x00, 0x00, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x31, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
1478 0xff, 0x50, 0xfe, 0x00, 0xff, 0x00, 0x3a, 0x00, 0x00, 0x00, 0xff,
1479 }
1480 _, err := NewReader(bytes.NewReader(data), int64(len(data)))
1481 if err != ErrFormat {
1482 t.Fatalf("unexpected error, got: %v, want: %v", err, ErrFormat)
1483 }
1484 }
1485
1486 func TestCVE202141772(t *testing.T) {
1487 t.Setenv("GODEBUG", "zipinsecurepath=0")
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499 data := []byte{
1500 0x50, 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x08,
1501 0x00, 0x00, 0x06, 0x94, 0x05, 0x53, 0x00, 0x00,
1502 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1503 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x2f, 0x50,
1504 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00,
1505 0x00, 0x78, 0x67, 0x2e, 0x53, 0x00, 0x00, 0x00,
1506 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1507 0x00, 0x02, 0x00, 0x00, 0x00, 0x2f, 0x2f, 0x50,
1508 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00,
1509 0x00, 0x78, 0x67, 0x2e, 0x53, 0x00, 0x00, 0x00,
1510 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1511 0x00, 0x01, 0x00, 0x00, 0x00, 0x5c, 0x50, 0x4b,
1512 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
1513 0x91, 0x68, 0x2e, 0x53, 0x85, 0x11, 0x4a, 0x0d,
1514 0x0b, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
1515 0x09, 0x00, 0x00, 0x00, 0x2f, 0x74, 0x65, 0x73,
1516 0x74, 0x2e, 0x74, 0x78, 0x74, 0x68, 0x65, 0x6c,
1517 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64,
1518 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x0a, 0x00,
1519 0x00, 0x08, 0x00, 0x00, 0x06, 0x94, 0x05, 0x53,
1520 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1521 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1522 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
1523 0xed, 0x41, 0x00, 0x00, 0x00, 0x00, 0x2f, 0x50,
1524 0x4b, 0x01, 0x02, 0x3f, 0x00, 0x0a, 0x00, 0x00,
1525 0x00, 0x00, 0x00, 0x78, 0x67, 0x2e, 0x53, 0x00,
1526 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1527 0x00, 0x00, 0x00, 0x02, 0x00, 0x24, 0x00, 0x00,
1528 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
1529 0x00, 0x1f, 0x00, 0x00, 0x00, 0x2f, 0x2f, 0x0a,
1530 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1531 0x00, 0x18, 0x00, 0x93, 0x98, 0x25, 0x57, 0x25,
1532 0xa9, 0xd7, 0x01, 0x93, 0x98, 0x25, 0x57, 0x25,
1533 0xa9, 0xd7, 0x01, 0x93, 0x98, 0x25, 0x57, 0x25,
1534 0xa9, 0xd7, 0x01, 0x50, 0x4b, 0x01, 0x02, 0x3f,
1535 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78,
1536 0x67, 0x2e, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00,
1537 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1538 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1539 0x00, 0x20, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00,
1540 0x00, 0x5c, 0x0a, 0x00, 0x20, 0x00, 0x00, 0x00,
1541 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x93, 0x98,
1542 0x25, 0x57, 0x25, 0xa9, 0xd7, 0x01, 0x93, 0x98,
1543 0x25, 0x57, 0x25, 0xa9, 0xd7, 0x01, 0x93, 0x98,
1544 0x25, 0x57, 0x25, 0xa9, 0xd7, 0x01, 0x50, 0x4b,
1545 0x01, 0x02, 0x3f, 0x00, 0x0a, 0x00, 0x00, 0x00,
1546 0x00, 0x00, 0x91, 0x68, 0x2e, 0x53, 0x85, 0x11,
1547 0x4a, 0x0d, 0x0b, 0x00, 0x00, 0x00, 0x0b, 0x00,
1548 0x00, 0x00, 0x09, 0x00, 0x24, 0x00, 0x00, 0x00,
1549 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
1550 0x5e, 0x00, 0x00, 0x00, 0x2f, 0x74, 0x65, 0x73,
1551 0x74, 0x2e, 0x74, 0x78, 0x74, 0x0a, 0x00, 0x20,
1552 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18,
1553 0x00, 0xa9, 0x80, 0x51, 0x01, 0x26, 0xa9, 0xd7,
1554 0x01, 0x31, 0xd1, 0x57, 0x01, 0x26, 0xa9, 0xd7,
1555 0x01, 0xdf, 0x48, 0x85, 0xf9, 0x25, 0xa9, 0xd7,
1556 0x01, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00,
1557 0x00, 0x04, 0x00, 0x04, 0x00, 0x31, 0x01, 0x00,
1558 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00,
1559 }
1560 r, err := NewReader(bytes.NewReader(data), int64(len(data)))
1561 if err != ErrInsecurePath {
1562 t.Fatalf("Error reading the archive: %v", err)
1563 }
1564 entryNames := []string{`/`, `//`, `\`, `/test.txt`}
1565 var names []string
1566 for _, f := range r.File {
1567 names = append(names, f.Name)
1568 if _, err := f.Open(); err != nil {
1569 t.Errorf("Error opening %q: %v", f.Name, err)
1570 }
1571 if _, err := r.Open(f.Name); err == nil {
1572 t.Errorf("Opening %q with fs.FS API succeeded", f.Name)
1573 }
1574 }
1575 if !reflect.DeepEqual(names, entryNames) {
1576 t.Errorf("Unexpected file entries: %q", names)
1577 }
1578 if _, err := r.Open(""); err == nil {
1579 t.Errorf("Opening %q with fs.FS API succeeded", "")
1580 }
1581 if _, err := r.Open("test.txt"); err != nil {
1582 t.Errorf("Error opening %q with fs.FS API: %v", "test.txt", err)
1583 }
1584 dirEntries, err := fs.ReadDir(r, ".")
1585 if err != nil {
1586 t.Fatalf("Error reading the root directory: %v", err)
1587 }
1588 if len(dirEntries) != 1 || dirEntries[0].Name() != "test.txt" {
1589 t.Errorf("Unexpected directory entries")
1590 for _, dirEntry := range dirEntries {
1591 _, err := r.Open(dirEntry.Name())
1592 t.Logf("%q (Open error: %v)", dirEntry.Name(), err)
1593 }
1594 t.FailNow()
1595 }
1596 info, err := dirEntries[0].Info()
1597 if err != nil {
1598 t.Fatalf("Error reading info entry: %v", err)
1599 }
1600 if name := info.Name(); name != "test.txt" {
1601 t.Errorf("Inconsistent name in info entry: %v", name)
1602 }
1603 }
1604
1605 func TestUnderSize(t *testing.T) {
1606 z, err := OpenReader("testdata/readme.zip")
1607 if err != nil {
1608 t.Fatal(err)
1609 }
1610 defer z.Close()
1611
1612 for _, f := range z.File {
1613 f.UncompressedSize64 = 1
1614 }
1615
1616 for _, f := range z.File {
1617 t.Run(f.Name, func(t *testing.T) {
1618 rd, err := f.Open()
1619 if err != nil {
1620 t.Fatal(err)
1621 }
1622 defer rd.Close()
1623
1624 _, err = io.Copy(io.Discard, rd)
1625 if err != ErrFormat {
1626 t.Fatalf("Error mismatch\n\tGot: %v\n\tWant: %v", err, ErrFormat)
1627 }
1628 })
1629 }
1630 }
1631
1632 func TestIssue54801(t *testing.T) {
1633 for _, input := range []string{"testdata/readme.zip", "testdata/dd.zip"} {
1634 z, err := OpenReader(input)
1635 if err != nil {
1636 t.Fatal(err)
1637 }
1638 defer z.Close()
1639
1640 for _, f := range z.File {
1641
1642 f.Name += "/"
1643
1644 t.Run(f.Name, func(t *testing.T) {
1645 t.Logf("CompressedSize64: %d, Flags: %#x", f.CompressedSize64, f.Flags)
1646
1647 rd, err := f.Open()
1648 if err != nil {
1649 t.Fatal(err)
1650 }
1651 defer rd.Close()
1652
1653 n, got := io.Copy(io.Discard, rd)
1654 if n != 0 || got != ErrFormat {
1655 t.Fatalf("Error mismatch, got: %d, %v, want: %v", n, got, ErrFormat)
1656 }
1657 })
1658 }
1659 }
1660 }
1661
1662 func TestInsecurePaths(t *testing.T) {
1663 t.Setenv("GODEBUG", "zipinsecurepath=0")
1664 for _, path := range []string{
1665 "../foo",
1666 "/foo",
1667 "a/b/../../../c",
1668 `a\b`,
1669 } {
1670 var buf bytes.Buffer
1671 zw := NewWriter(&buf)
1672 _, err := zw.Create(path)
1673 if err != nil {
1674 t.Errorf("zw.Create(%q) = %v", path, err)
1675 continue
1676 }
1677 zw.Close()
1678
1679 zr, err := NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
1680 if err != ErrInsecurePath {
1681 t.Errorf("NewReader for archive with file %q: got err %v, want ErrInsecurePath", path, err)
1682 continue
1683 }
1684 var gotPaths []string
1685 for _, f := range zr.File {
1686 gotPaths = append(gotPaths, f.Name)
1687 }
1688 if !reflect.DeepEqual(gotPaths, []string{path}) {
1689 t.Errorf("NewReader for archive with file %q: got files %q", path, gotPaths)
1690 continue
1691 }
1692 }
1693 }
1694
1695 func TestDisableInsecurePathCheck(t *testing.T) {
1696 t.Setenv("GODEBUG", "zipinsecurepath=1")
1697 var buf bytes.Buffer
1698 zw := NewWriter(&buf)
1699 const name = "/foo"
1700 _, err := zw.Create(name)
1701 if err != nil {
1702 t.Fatalf("zw.Create(%q) = %v", name, err)
1703 }
1704 zw.Close()
1705 zr, err := NewReader(bytes.NewReader(buf.Bytes()), int64(buf.Len()))
1706 if err != nil {
1707 t.Fatalf("NewReader with zipinsecurepath=1: got err %v, want nil", err)
1708 }
1709 var gotPaths []string
1710 for _, f := range zr.File {
1711 gotPaths = append(gotPaths, f.Name)
1712 }
1713 if want := []string{name}; !reflect.DeepEqual(gotPaths, want) {
1714 t.Errorf("NewReader with zipinsecurepath=1: got files %q, want %q", gotPaths, want)
1715 }
1716 }
1717
1718 func TestCompressedDirectory(t *testing.T) {
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728 data := []byte{
1729 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x08,
1730 0x08, 0x00, 0x49, 0x86, 0x81, 0x55, 0x00, 0x00,
1731 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1732 0x00, 0x00, 0x09, 0x00, 0x04, 0x00, 0x4d, 0x45,
1733 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46, 0x2f, 0xfe,
1734 0xca, 0x00, 0x00, 0x03, 0x00, 0x50, 0x4b, 0x07,
1735 0x08, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00, 0x00,
1736 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b, 0x03,
1737 0x04, 0x14, 0x00, 0x08, 0x08, 0x08, 0x00, 0x49,
1738 0x86, 0x81, 0x55, 0x00, 0x00, 0x00, 0x00, 0x00,
1739 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x14,
1740 0x00, 0x00, 0x00, 0x4d, 0x45, 0x54, 0x41, 0x2d,
1741 0x49, 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49,
1742 0x46, 0x45, 0x53, 0x54, 0x2e, 0x4d, 0x46, 0xf3,
1743 0x4d, 0xcc, 0xcb, 0x4c, 0x4b, 0x2d, 0x2e, 0xd1,
1744 0x0d, 0x4b, 0x2d, 0x2a, 0xce, 0xcc, 0xcf, 0xb3,
1745 0x52, 0x30, 0xd4, 0x33, 0xe0, 0xe5, 0x72, 0x2e,
1746 0x4a, 0x4d, 0x2c, 0x49, 0x4d, 0xd1, 0x75, 0xaa,
1747 0x04, 0x0a, 0x00, 0x45, 0xf4, 0x0c, 0x8d, 0x15,
1748 0x34, 0xdc, 0xf3, 0xf3, 0xd3, 0x73, 0x52, 0x15,
1749 0x3c, 0xf3, 0x92, 0xf5, 0x34, 0x79, 0xb9, 0x78,
1750 0xb9, 0x00, 0x50, 0x4b, 0x07, 0x08, 0x93, 0x7e,
1751 0x93, 0xaf, 0x3b, 0x00, 0x00, 0x00, 0x3c, 0x00,
1752 0x00, 0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x00,
1753 0x14, 0x00, 0x08, 0x08, 0x08, 0x00, 0x49, 0x86,
1754 0x81, 0x55, 0x00, 0x00, 0x00, 0x00, 0x02, 0x00,
1755 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x09, 0x00,
1756 0x04, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1757 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1758 0x4d, 0x45, 0x54, 0x41, 0x2d, 0x49, 0x4e, 0x46,
1759 0x2f, 0xfe, 0xca, 0x00, 0x00, 0x50, 0x4b, 0x01,
1760 0x02, 0x14, 0x00, 0x14, 0x00, 0x08, 0x08, 0x08,
1761 0x00, 0x49, 0x86, 0x81, 0x55, 0x93, 0x7e, 0x93,
1762 0xaf, 0x3b, 0x00, 0x00, 0x00, 0x3c, 0x00, 0x00,
1763 0x00, 0x14, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1764 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x3d,
1765 0x00, 0x00, 0x00, 0x4d, 0x45, 0x54, 0x41, 0x2d,
1766 0x49, 0x4e, 0x46, 0x2f, 0x4d, 0x41, 0x4e, 0x49,
1767 0x46, 0x45, 0x53, 0x54, 0x2e, 0x4d, 0x46, 0x50,
1768 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0x02,
1769 0x00, 0x02, 0x00, 0x7d, 0x00, 0x00, 0x00, 0xba,
1770 0x00, 0x00, 0x00, 0x00, 0x00,
1771 }
1772 r, err := NewReader(bytes.NewReader(data), int64(len(data)))
1773 if err != nil {
1774 t.Fatalf("unexpected error: %v", err)
1775 }
1776 for _, f := range r.File {
1777 r, err := f.Open()
1778 if err != nil {
1779 t.Fatalf("unexpected error: %v", err)
1780 }
1781 if _, err := io.Copy(io.Discard, r); err != nil {
1782 t.Fatalf("unexpected error: %v", err)
1783 }
1784 }
1785 }
1786
1787 func TestBaseOffsetPlusOverflow(t *testing.T) {
1788
1789 data := []byte{
1790 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1791 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1792 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1793 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1794 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1795 0xff, 0xff, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1796 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1797 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1798 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1799 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1800 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1801 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1802 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1803 0x20, 0x20, 0x20, 0x50, 0x4b, 0x06, 0x06, 0x20,
1804 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1805 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1806 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1807 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
1808 0x20, 0xff, 0xff, 0x20, 0x00, 0x00, 0x00, 0x00,
1809 0x00, 0x00, 0x00, 0x20, 0x08, 0x00, 0x00, 0x00,
1810 0x00, 0x00, 0x80, 0x50, 0x4b, 0x06, 0x07, 0x00,
1811 0x00, 0x00, 0x00, 0x6b, 0x00, 0x00, 0x00, 0x00,
1812 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x50,
1813 0x4b, 0x05, 0x06, 0x20, 0x20, 0x20, 0x20, 0xff,
1814 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1815 0xff, 0xff, 0xff, 0x20, 0x00,
1816 }
1817 defer func() {
1818 if r := recover(); r != nil {
1819 t.Fatalf("NewReader panicked: %s", r)
1820 }
1821 }()
1822
1823
1824
1825 NewReader(bytes.NewReader(data), int64(len(data))+1875)
1826 }
1827
View as plain text