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: "r.zip",
95 Source: returnRecursiveZip,
96 File: []ZipTestFile{
97 {
98 Name: "r/r.zip",
99 Content: rZipBytes(),
100 Modified: time.Date(2010, 3, 4, 0, 24, 16, 0, time.UTC),
101 Mode: 0666,
102 },
103 },
104 },
105 {
106 Name: "symlink.zip",
107 File: []ZipTestFile{
108 {
109 Name: "symlink",
110 Content: []byte("../target"),
111 Modified: time.Date(2012, 2, 3, 19, 56, 48, 0, timeZone(-2*time.Hour)),
112 Mode: 0777 | fs.ModeSymlink,
113 },
114 },
115 },
116 {
117 Name: "readme.zip",
118 },
119 {
120 Name: "readme.notzip",
121 Error: ErrFormat,
122 },
123 {
124 Name: "dd.zip",
125 File: []ZipTestFile{
126 {
127 Name: "filename",
128 Content: []byte("This is a test textfile.\n"),
129 Modified: time.Date(2011, 2, 2, 13, 6, 20, 0, time.UTC),
130 Mode: 0666,
131 },
132 },
133 },
134 {
135
136 Name: "winxp.zip",
137 File: []ZipTestFile{
138 {
139 Name: "hello",
140 Content: []byte("world \r\n"),
141 Modified: time.Date(2011, 12, 8, 10, 4, 24, 0, time.UTC),
142 Mode: 0666,
143 },
144 {
145 Name: "dir/bar",
146 Content: []byte("foo \r\n"),
147 Modified: time.Date(2011, 12, 8, 10, 4, 50, 0, time.UTC),
148 Mode: 0666,
149 },
150 {
151 Name: "dir/empty/",
152 Content: []byte{},
153 Modified: time.Date(2011, 12, 8, 10, 8, 6, 0, time.UTC),
154 Mode: fs.ModeDir | 0777,
155 },
156 {
157 Name: "readonly",
158 Content: []byte("important \r\n"),
159 Modified: time.Date(2011, 12, 8, 10, 6, 8, 0, time.UTC),
160 Mode: 0444,
161 },
162 },
163 },
164 {
165
166 Name: "unix.zip",
167 File: []ZipTestFile{
168 {
169 Name: "hello",
170 Content: []byte("world \r\n"),
171 Modified: time.Date(2011, 12, 8, 10, 4, 24, 0, timeZone(0)),
172 Mode: 0666,
173 },
174 {
175 Name: "dir/bar",
176 Content: []byte("foo \r\n"),
177 Modified: time.Date(2011, 12, 8, 10, 4, 50, 0, timeZone(0)),
178 Mode: 0666,
179 },
180 {
181 Name: "dir/empty/",
182 Content: []byte{},
183 Modified: time.Date(2011, 12, 8, 10, 8, 6, 0, timeZone(0)),
184 Mode: fs.ModeDir | 0777,
185 },
186 {
187 Name: "readonly",
188 Content: []byte("important \r\n"),
189 Modified: time.Date(2011, 12, 8, 10, 6, 8, 0, timeZone(0)),
190 Mode: 0444,
191 },
192 },
193 },
194 {
195
196
197
198
199
200 Name: "go-no-datadesc-sig.zip.base64",
201 Obscured: true,
202 File: []ZipTestFile{
203 {
204 Name: "foo.txt",
205 Content: []byte("foo\n"),
206 Modified: time.Date(2012, 3, 8, 16, 59, 10, 0, timeZone(-8*time.Hour)),
207 Mode: 0644,
208 },
209 {
210 Name: "bar.txt",
211 Content: []byte("bar\n"),
212 Modified: time.Date(2012, 3, 8, 16, 59, 12, 0, timeZone(-8*time.Hour)),
213 Mode: 0644,
214 },
215 },
216 },
217 {
218
219
220 Name: "go-with-datadesc-sig.zip",
221 File: []ZipTestFile{
222 {
223 Name: "foo.txt",
224 Content: []byte("foo\n"),
225 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC),
226 Mode: 0666,
227 },
228 {
229 Name: "bar.txt",
230 Content: []byte("bar\n"),
231 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC),
232 Mode: 0666,
233 },
234 },
235 },
236 {
237 Name: "Bad-CRC32-in-data-descriptor",
238 Source: returnCorruptCRC32Zip,
239 File: []ZipTestFile{
240 {
241 Name: "foo.txt",
242 Content: []byte("foo\n"),
243 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC),
244 Mode: 0666,
245 ContentErr: ErrChecksum,
246 },
247 {
248 Name: "bar.txt",
249 Content: []byte("bar\n"),
250 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC),
251 Mode: 0666,
252 },
253 },
254 },
255
256
257 {
258 Name: "crc32-not-streamed.zip",
259 File: []ZipTestFile{
260 {
261 Name: "foo.txt",
262 Content: []byte("foo\n"),
263 Modified: time.Date(2012, 3, 8, 16, 59, 10, 0, timeZone(-8*time.Hour)),
264 Mode: 0644,
265 },
266 {
267 Name: "bar.txt",
268 Content: []byte("bar\n"),
269 Modified: time.Date(2012, 3, 8, 16, 59, 12, 0, timeZone(-8*time.Hour)),
270 Mode: 0644,
271 },
272 },
273 },
274
275
276 {
277 Name: "crc32-not-streamed.zip",
278 Source: returnCorruptNotStreamedZip,
279 File: []ZipTestFile{
280 {
281 Name: "foo.txt",
282 Content: []byte("foo\n"),
283 Modified: time.Date(2012, 3, 8, 16, 59, 10, 0, timeZone(-8*time.Hour)),
284 Mode: 0644,
285 ContentErr: ErrChecksum,
286 },
287 {
288 Name: "bar.txt",
289 Content: []byte("bar\n"),
290 Modified: time.Date(2012, 3, 8, 16, 59, 12, 0, timeZone(-8*time.Hour)),
291 Mode: 0644,
292 },
293 },
294 },
295 {
296 Name: "zip64.zip",
297 File: []ZipTestFile{
298 {
299 Name: "README",
300 Content: []byte("This small file is in ZIP64 format.\n"),
301 Modified: time.Date(2012, 8, 10, 14, 33, 32, 0, time.UTC),
302 Mode: 0644,
303 },
304 },
305 },
306
307 {
308 Name: "zip64-2.zip",
309 File: []ZipTestFile{
310 {
311 Name: "README",
312 Content: []byte("This small file is in ZIP64 format.\n"),
313 Modified: time.Date(2012, 8, 10, 14, 33, 32, 0, timeZone(-4*time.Hour)),
314 Mode: 0644,
315 },
316 },
317 },
318
319 {
320 Name: "big.zip",
321 Source: returnBigZipBytes,
322 File: []ZipTestFile{
323 {
324 Name: "big.file",
325 Content: nil,
326 Size: 1<<32 - 1,
327 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC),
328 Mode: 0666,
329 },
330 },
331 },
332 {
333 Name: "utf8-7zip.zip",
334 File: []ZipTestFile{
335 {
336 Name: "世界",
337 Content: []byte{},
338 Mode: 0666,
339 Modified: time.Date(2017, 11, 6, 13, 9, 27, 867862500, timeZone(-8*time.Hour)),
340 },
341 },
342 },
343 {
344 Name: "utf8-infozip.zip",
345 File: []ZipTestFile{
346 {
347 Name: "世界",
348 Content: []byte{},
349 Mode: 0644,
350
351
352
353
354 NonUTF8: true,
355 Modified: time.Date(2017, 11, 6, 13, 9, 27, 0, timeZone(-8*time.Hour)),
356 },
357 },
358 },
359 {
360 Name: "utf8-osx.zip",
361 File: []ZipTestFile{
362 {
363 Name: "世界",
364 Content: []byte{},
365 Mode: 0644,
366
367 NonUTF8: true,
368 Modified: time.Date(2017, 11, 6, 13, 9, 27, 0, timeZone(-8*time.Hour)),
369 },
370 },
371 },
372 {
373 Name: "utf8-winrar.zip",
374 File: []ZipTestFile{
375 {
376 Name: "世界",
377 Content: []byte{},
378 Mode: 0666,
379 Modified: time.Date(2017, 11, 6, 13, 9, 27, 867862500, timeZone(-8*time.Hour)),
380 },
381 },
382 },
383 {
384 Name: "utf8-winzip.zip",
385 File: []ZipTestFile{
386 {
387 Name: "世界",
388 Content: []byte{},
389 Mode: 0666,
390 Modified: time.Date(2017, 11, 6, 13, 9, 27, 867000000, timeZone(-8*time.Hour)),
391 },
392 },
393 },
394 {
395 Name: "time-7zip.zip",
396 File: []ZipTestFile{
397 {
398 Name: "test.txt",
399 Content: []byte{},
400 Size: 1<<32 - 1,
401 Modified: time.Date(2017, 10, 31, 21, 11, 57, 244817900, timeZone(-7*time.Hour)),
402 Mode: 0666,
403 },
404 },
405 },
406 {
407 Name: "time-infozip.zip",
408 File: []ZipTestFile{
409 {
410 Name: "test.txt",
411 Content: []byte{},
412 Size: 1<<32 - 1,
413 Modified: time.Date(2017, 10, 31, 21, 11, 57, 0, timeZone(-7*time.Hour)),
414 Mode: 0644,
415 },
416 },
417 },
418 {
419 Name: "time-osx.zip",
420 File: []ZipTestFile{
421 {
422 Name: "test.txt",
423 Content: []byte{},
424 Size: 1<<32 - 1,
425 Modified: time.Date(2017, 10, 31, 21, 11, 57, 0, timeZone(-7*time.Hour)),
426 Mode: 0644,
427 },
428 },
429 },
430 {
431 Name: "time-win7.zip",
432 File: []ZipTestFile{
433 {
434 Name: "test.txt",
435 Content: []byte{},
436 Size: 1<<32 - 1,
437 Modified: time.Date(2017, 10, 31, 21, 11, 58, 0, time.UTC),
438 Mode: 0666,
439 },
440 },
441 },
442 {
443 Name: "time-winrar.zip",
444 File: []ZipTestFile{
445 {
446 Name: "test.txt",
447 Content: []byte{},
448 Size: 1<<32 - 1,
449 Modified: time.Date(2017, 10, 31, 21, 11, 57, 244817900, timeZone(-7*time.Hour)),
450 Mode: 0666,
451 },
452 },
453 },
454 {
455 Name: "time-winzip.zip",
456 File: []ZipTestFile{
457 {
458 Name: "test.txt",
459 Content: []byte{},
460 Size: 1<<32 - 1,
461 Modified: time.Date(2017, 10, 31, 21, 11, 57, 244000000, timeZone(-7*time.Hour)),
462 Mode: 0666,
463 },
464 },
465 },
466 {
467 Name: "time-go.zip",
468 File: []ZipTestFile{
469 {
470 Name: "test.txt",
471 Content: []byte{},
472 Size: 1<<32 - 1,
473 Modified: time.Date(2017, 10, 31, 21, 11, 57, 0, timeZone(-7*time.Hour)),
474 Mode: 0666,
475 },
476 },
477 },
478 {
479 Name: "time-22738.zip",
480 File: []ZipTestFile{
481 {
482 Name: "file",
483 Content: []byte{},
484 Mode: 0666,
485 Modified: time.Date(1999, 12, 31, 19, 0, 0, 0, timeZone(-5*time.Hour)),
486 ModTime: time.Date(1999, 12, 31, 19, 0, 0, 0, time.UTC),
487 },
488 },
489 },
490 }
491
492 func TestReader(t *testing.T) {
493 for _, zt := range tests {
494 t.Run(zt.Name, func(t *testing.T) {
495 readTestZip(t, zt)
496 })
497 }
498 }
499
500 func readTestZip(t *testing.T, zt ZipTest) {
501 var z *Reader
502 var err error
503 var raw []byte
504 if zt.Source != nil {
505 rat, size := zt.Source()
506 z, err = NewReader(rat, size)
507 raw = make([]byte, size)
508 if _, err := rat.ReadAt(raw, 0); err != nil {
509 t.Errorf("ReadAt error=%v", err)
510 return
511 }
512 } else {
513 path := filepath.Join("testdata", zt.Name)
514 if zt.Obscured {
515 tf, err := obscuretestdata.DecodeToTempFile(path)
516 if err != nil {
517 t.Errorf("obscuretestdata.DecodeToTempFile(%s): %v", path, err)
518 return
519 }
520 defer os.Remove(tf)
521 path = tf
522 }
523 var rc *ReadCloser
524 rc, err = OpenReader(path)
525 if err == nil {
526 defer rc.Close()
527 z = &rc.Reader
528 }
529 var err2 error
530 raw, err2 = os.ReadFile(path)
531 if err2 != nil {
532 t.Errorf("ReadFile(%s) error=%v", path, err2)
533 return
534 }
535 }
536 if err != zt.Error {
537 t.Errorf("error=%v, want %v", err, zt.Error)
538 return
539 }
540
541
542 if err == ErrFormat {
543 return
544 }
545
546
547
548 if zt.File == nil {
549 return
550 }
551
552 if z.Comment != zt.Comment {
553 t.Errorf("comment=%q, want %q", z.Comment, zt.Comment)
554 }
555 if len(z.File) != len(zt.File) {
556 t.Fatalf("file count=%d, want %d", len(z.File), len(zt.File))
557 }
558
559
560 for i, ft := range zt.File {
561 readTestFile(t, zt, ft, z.File[i], raw)
562 }
563 if t.Failed() {
564 return
565 }
566
567
568 n := 0
569 done := make(chan bool)
570 for i := 0; i < 5; i++ {
571 for j, ft := range zt.File {
572 go func(j int, ft ZipTestFile) {
573 readTestFile(t, zt, ft, z.File[j], raw)
574 done <- true
575 }(j, ft)
576 n++
577 }
578 }
579 for ; n > 0; n-- {
580 <-done
581 }
582 }
583
584 func equalTimeAndZone(t1, t2 time.Time) bool {
585 name1, offset1 := t1.Zone()
586 name2, offset2 := t2.Zone()
587 return t1.Equal(t2) && name1 == name2 && offset1 == offset2
588 }
589
590 func readTestFile(t *testing.T, zt ZipTest, ft ZipTestFile, f *File, raw []byte) {
591 if f.Name != ft.Name {
592 t.Errorf("name=%q, want %q", f.Name, ft.Name)
593 }
594 if !ft.Modified.IsZero() && !equalTimeAndZone(f.Modified, ft.Modified) {
595 t.Errorf("%s: Modified=%s, want %s", f.Name, f.Modified, ft.Modified)
596 }
597 if !ft.ModTime.IsZero() && !equalTimeAndZone(f.ModTime(), ft.ModTime) {
598 t.Errorf("%s: ModTime=%s, want %s", f.Name, f.ModTime(), ft.ModTime)
599 }
600
601 testFileMode(t, f, ft.Mode)
602
603 size := uint64(f.UncompressedSize)
604 if size == uint32max {
605 size = f.UncompressedSize64
606 } else if size != f.UncompressedSize64 {
607 t.Errorf("%v: UncompressedSize=%#x does not match UncompressedSize64=%#x", f.Name, size, f.UncompressedSize64)
608 }
609
610
611 rw, err := f.OpenRaw()
612 if err != nil {
613 t.Errorf("%v: OpenRaw error=%v", f.Name, err)
614 return
615 }
616 start, err := f.DataOffset()
617 if err != nil {
618 t.Errorf("%v: DataOffset error=%v", f.Name, err)
619 return
620 }
621 got, err := io.ReadAll(rw)
622 if err != nil {
623 t.Errorf("%v: OpenRaw ReadAll error=%v", f.Name, err)
624 return
625 }
626 end := uint64(start) + f.CompressedSize64
627 want := raw[start:end]
628 if !bytes.Equal(got, want) {
629 t.Logf("got %q", got)
630 t.Logf("want %q", want)
631 t.Errorf("%v: OpenRaw returned unexpected bytes", f.Name)
632 return
633 }
634
635 r, err := f.Open()
636 if err != nil {
637 t.Errorf("%v", err)
638 return
639 }
640
641
642
643
644 if ft.Content == nil && ft.File == "" && ft.Size > 0 {
645 if size != ft.Size {
646 t.Errorf("%v: uncompressed size %#x, want %#x", ft.Name, size, ft.Size)
647 }
648 r.Close()
649 return
650 }
651
652 var b bytes.Buffer
653 _, err = io.Copy(&b, r)
654 if err != ft.ContentErr {
655 t.Errorf("copying contents: %v (want %v)", err, ft.ContentErr)
656 }
657 if err != nil {
658 return
659 }
660 r.Close()
661
662 if g := uint64(b.Len()); g != size {
663 t.Errorf("%v: read %v bytes but f.UncompressedSize == %v", f.Name, g, size)
664 }
665
666 var c []byte
667 if ft.Content != nil {
668 c = ft.Content
669 } else if c, err = os.ReadFile("testdata/" + ft.File); err != nil {
670 t.Error(err)
671 return
672 }
673
674 if b.Len() != len(c) {
675 t.Errorf("%s: len=%d, want %d", f.Name, b.Len(), len(c))
676 return
677 }
678
679 for i, b := range b.Bytes() {
680 if b != c[i] {
681 t.Errorf("%s: content[%d]=%q want %q", f.Name, i, b, c[i])
682 return
683 }
684 }
685 }
686
687 func testFileMode(t *testing.T, f *File, want fs.FileMode) {
688 mode := f.Mode()
689 if want == 0 {
690 t.Errorf("%s mode: got %v, want none", f.Name, mode)
691 } else if mode != want {
692 t.Errorf("%s mode: want %v, got %v", f.Name, want, mode)
693 }
694 }
695
696 func TestInvalidFiles(t *testing.T) {
697 const size = 1024 * 70
698 b := make([]byte, size)
699
700
701 _, err := NewReader(bytes.NewReader(b), size)
702 if err != ErrFormat {
703 t.Errorf("zeroes: error=%v, want %v", err, ErrFormat)
704 }
705
706
707 sig := make([]byte, 4)
708 binary.LittleEndian.PutUint32(sig, directoryEndSignature)
709 for i := 0; i < size-4; i += 4 {
710 copy(b[i:i+4], sig)
711 }
712 _, err = NewReader(bytes.NewReader(b), size)
713 if err != ErrFormat {
714 t.Errorf("sigs: error=%v, want %v", err, ErrFormat)
715 }
716
717
718 _, err = NewReader(bytes.NewReader([]byte("foobar")), -1)
719 if err == nil {
720 t.Errorf("archive/zip.NewReader: expected error when negative size is passed")
721 }
722 }
723
724 func messWith(fileName string, corrupter func(b []byte)) (r io.ReaderAt, size int64) {
725 data, err := os.ReadFile(filepath.Join("testdata", fileName))
726 if err != nil {
727 panic("Error reading " + fileName + ": " + err.Error())
728 }
729 corrupter(data)
730 return bytes.NewReader(data), int64(len(data))
731 }
732
733 func returnCorruptCRC32Zip() (r io.ReaderAt, size int64) {
734 return messWith("go-with-datadesc-sig.zip", func(b []byte) {
735
736 b[0x2d]++
737 })
738 }
739
740 func returnCorruptNotStreamedZip() (r io.ReaderAt, size int64) {
741 return messWith("crc32-not-streamed.zip", func(b []byte) {
742
743
744 b[0x11]++
745 b[0x9d]++
746
747
748
749
750
751
752
753 })
754 }
755
756
757
758 func rZipBytes() []byte {
759 s := `
760 0000000 50 4b 03 04 14 00 00 00 08 00 08 03 64 3c f9 f4
761 0000010 89 64 48 01 00 00 b8 01 00 00 07 00 00 00 72 2f
762 0000020 72 2e 7a 69 70 00 25 00 da ff 50 4b 03 04 14 00
763 0000030 00 00 08 00 08 03 64 3c f9 f4 89 64 48 01 00 00
764 0000040 b8 01 00 00 07 00 00 00 72 2f 72 2e 7a 69 70 00
765 0000050 2f 00 d0 ff 00 25 00 da ff 50 4b 03 04 14 00 00
766 0000060 00 08 00 08 03 64 3c f9 f4 89 64 48 01 00 00 b8
767 0000070 01 00 00 07 00 00 00 72 2f 72 2e 7a 69 70 00 2f
768 0000080 00 d0 ff c2 54 8e 57 39 00 05 00 fa ff c2 54 8e
769 0000090 57 39 00 05 00 fa ff 00 05 00 fa ff 00 14 00 eb
770 00000a0 ff c2 54 8e 57 39 00 05 00 fa ff 00 05 00 fa ff
771 00000b0 00 14 00 eb ff 42 88 21 c4 00 00 14 00 eb ff 42
772 00000c0 88 21 c4 00 00 14 00 eb ff 42 88 21 c4 00 00 14
773 00000d0 00 eb ff 42 88 21 c4 00 00 14 00 eb ff 42 88 21
774 00000e0 c4 00 00 00 00 ff ff 00 00 00 ff ff 00 34 00 cb
775 00000f0 ff 42 88 21 c4 00 00 00 00 ff ff 00 00 00 ff ff
776 0000100 00 34 00 cb ff 42 e8 21 5e 0f 00 00 00 ff ff 0a
777 0000110 f0 66 64 12 61 c0 15 dc e8 a0 48 bf 48 af 2a b3
778 0000120 20 c0 9b 95 0d c4 67 04 42 53 06 06 06 40 00 06
779 0000130 00 f9 ff 6d 01 00 00 00 00 42 e8 21 5e 0f 00 00
780 0000140 00 ff ff 0a f0 66 64 12 61 c0 15 dc e8 a0 48 bf
781 0000150 48 af 2a b3 20 c0 9b 95 0d c4 67 04 42 53 06 06
782 0000160 06 40 00 06 00 f9 ff 6d 01 00 00 00 00 50 4b 01
783 0000170 02 14 00 14 00 00 00 08 00 08 03 64 3c f9 f4 89
784 0000180 64 48 01 00 00 b8 01 00 00 07 00 00 00 00 00 00
785 0000190 00 00 00 00 00 00 00 00 00 00 00 72 2f 72 2e 7a
786 00001a0 69 70 50 4b 05 06 00 00 00 00 01 00 01 00 35 00
787 00001b0 00 00 6d 01 00 00 00 00`
788 s = regexp.MustCompile(`[0-9a-f]{7}`).ReplaceAllString(s, "")
789 s = regexp.MustCompile(`\s+`).ReplaceAllString(s, "")
790 b, err := hex.DecodeString(s)
791 if err != nil {
792 panic(err)
793 }
794 return b
795 }
796
797 func returnRecursiveZip() (r io.ReaderAt, size int64) {
798 b := rZipBytes()
799 return bytes.NewReader(b), int64(len(b))
800 }
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869 func biggestZipBytes() []byte {
870 s := `
871 0000000 50 4b 03 04 14 00 08 00 08 00 00 00 00 00 00 00
872 0000010 00 00 00 00 00 00 00 00 00 00 0a 00 00 00 62 69
873 0000020 67 67 65 72 2e 7a 69 70 ec dc 6b 4c 53 67 18 07
874 0000030 f0 16 c5 ca 65 2e cb b8 94 20 61 1f 44 33 c7 cd
875 0000040 c0 86 4a b5 c0 62 8a 61 05 c6 cd 91 b2 54 8c 1b
876 0000050 63 8b 03 9c 1b 95 52 5a e3 a0 19 6c b2 05 59 44
877 0000060 64 9d 73 83 71 11 46 61 14 b9 1d 14 09 4a c3 60
878 0000070 2e 4c 6e a5 60 45 02 62 81 95 b6 94 9e 9e 77 e7
879 0000080 d0 43 b6 f8 71 df 96 3c e7 a4 69 ce bf cf e9 79
880 0000090 ce ef 79 3f bf f1 31 db b6 bb 31 76 92 e7 f3 07
881 00000a0 8b fc 9c ca cc 08 cc cb cc 5e d2 1c 88 d9 7e bb
882 00000b0 4f bb 3a 3f 75 f1 5d 7f 8f c2 68 67 77 8f 25 ff
883 00000c0 84 e2 93 2d ef a4 95 3d 71 4e 2c b9 b0 87 c3 be
884 00000d0 3d f8 a7 60 24 61 c5 ef ae 9e c8 6c 6d 4e 69 c8
885 00000e0 67 65 34 f8 37 76 2d 76 5c 54 f3 95 65 49 c7 0f
886 00000f0 18 71 4b 7e 5b 6a d1 79 47 61 41 b0 4e 2a 74 45
887 0000100 43 58 12 b2 5a a5 c6 7d 68 55 88 d4 98 75 18 6d
888 0000110 08 d1 1f 8f 5a 9e 96 ee 45 cf a4 84 4e 4b e8 50
889 0000120 a7 13 d9 06 de 52 81 97 36 b2 d7 b8 fc 2b 5f 55
890 0000130 23 1f 32 59 cf 30 27 fb e2 8a b9 de 45 dd 63 9c
891 0000140 4b b5 8b 96 4c 7a 62 62 cc a1 a7 cf fa f1 fe dd
892 0000150 54 62 11 bf 36 78 b3 c7 b1 b5 f2 61 4d 4e dd 66
893 0000160 32 2e e6 70 34 5f f4 c9 e6 6c 43 6f da 6b c6 c3
894 0000170 09 2c ce 09 57 7f d2 7e b4 23 ba 7c 1b 99 bc 22
895 0000180 3e f1 de 91 2f e3 9c 1b 82 cc c2 84 39 aa e6 de
896 0000190 b4 69 fc cc cb 72 a6 61 45 f0 d3 1d 26 19 7c 8d
897 00001a0 29 c8 66 02 be 77 6a f9 3d 34 79 17 19 c8 96 24
898 00001b0 a3 ac e4 dd 3b 1a 8e c6 fe 96 38 6b bf 67 5a 23
899 00001c0 f4 16 f4 e6 8a b4 fc c2 cd bf 95 66 1d bb 35 aa
900 00001d0 92 7d 66 d8 08 8d a5 1f 54 2a af 09 cf 61 ff d2
901 00001e0 85 9d 8f b6 d7 88 07 4a 86 03 db 64 f3 d9 92 73
902 00001f0 df ec a7 fc 23 4c 8d 83 79 63 2a d9 fd 8d b3 c8
903 0000200 8f 7e d4 19 85 e6 8d 1c 76 f0 8b 58 32 fd 9a d6
904 0000210 85 e2 48 ad c3 d5 60 6f 7e 22 dd ef 09 49 7c 7f
905 0000220 3a 45 c3 71 b7 df f3 4c 63 fb b5 d9 31 5f 6e d6
906 0000230 24 1d a4 4a fe 32 a7 5c 16 48 5c 3e 08 6b 8a d3
907 0000240 25 1d a2 12 a5 59 24 ea 20 5f 52 6d ad 94 db 6b
908 0000250 94 b9 5d eb 4b a7 5c 44 bb 1e f2 3c 6b cf 52 c9
909 0000260 e9 e5 ba 06 b9 c4 e5 0a d0 00 0d d0 00 0d d0 00
910 0000270 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d
911 0000280 d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0
912 0000290 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00
913 00002a0 0d d0 00 cd ff 9e 46 86 fa a7 7d 3a 43 d7 8e 10
914 00002b0 52 e9 be e6 6e cf eb 9e 85 4d 65 ce cc 30 c1 44
915 00002c0 c0 4e af bc 9c 6c 4b a0 d7 54 ff 1d d5 5c 89 fb
916 00002d0 b5 34 7e c4 c2 9e f5 a0 f6 5b 7e 6e ca 73 c7 ef
917 00002e0 5d be de f9 e8 81 eb a5 0a a5 63 54 2c d7 1c d1
918 00002f0 89 17 85 f8 16 94 f2 8a b2 a3 f5 b6 6d df 75 cd
919 0000300 90 dd 64 bd 5d 55 4e f2 55 19 1b b7 cc ef 1b ea
920 0000310 2e 05 9c f4 aa 1e a8 cd a6 82 c7 59 0f 5e 9d e0
921 0000320 bb fc 6c d6 99 23 eb 36 ad c6 c5 e1 d8 e1 e2 3e
922 0000330 d9 90 5a f7 91 5d 6f bc 33 6d 98 47 d2 7c 2e 2f
923 0000340 99 a4 25 72 85 49 2c be 0b 5b af 8f e5 6e 81 a6
924 0000350 a3 5a 6f 39 53 3a ab 7a 8b 1e 26 f7 46 6c 7d 26
925 0000360 53 b3 22 31 94 d3 83 f2 18 4d f5 92 33 27 53 97
926 0000370 0f d3 e6 55 9c a6 c5 31 87 6f d3 f3 ae 39 6f 56
927 0000380 10 7b ab 7e d0 b4 ca f2 b8 05 be 3f 0e 6e 5a 75
928 0000390 ab 0c f5 37 0e ba 8e 75 71 7a aa ed 7a dd 6a 63
929 00003a0 be 9b a0 97 27 6a 6f e7 d3 8b c4 7c ec d3 91 56
930 00003b0 d9 ac 5e bf 16 42 2f 00 1f 93 a2 23 87 bd e2 59
931 00003c0 a0 de 1a 66 c8 62 eb 55 8f 91 17 b4 61 42 7a 50
932 00003d0 40 03 34 40 03 34 40 03 34 40 03 34 40 03 34 40
933 00003e0 03 34 40 03 34 40 03 34 40 03 34 40 03 34 40 03
934 00003f0 34 40 03 34 40 03 34 ff 85 86 90 8b ea 67 90 0d
935 0000400 e1 42 1b d2 61 d6 79 ec fd 3e 44 28 a4 51 6c 5c
936 0000410 fc d2 72 ca ba 82 18 46 16 61 cd 93 a9 0f d1 24
937 0000420 17 99 e2 2c 71 16 84 0c c8 7a 13 0f 9a 5e c5 f0
938 0000430 79 64 e2 12 4d c8 82 a1 81 19 2d aa 44 6d 87 54
939 0000440 84 71 c1 f6 d4 ca 25 8c 77 b9 08 c7 c8 5e 10 8a
940 0000450 8f 61 ed 8c ba 30 1f 79 9a c7 60 34 2b b9 8c f8
941 0000460 18 a6 83 1b e3 9f ad 79 fe fd 1b 8b f1 fc 41 6f
942 0000470 d4 13 1f e3 b8 83 ba 64 92 e7 eb e4 77 05 8f ba
943 0000480 fa 3b 00 00 ff ff 50 4b 07 08 a6 18 b1 91 5e 04
944 0000490 00 00 e4 47 00 00 50 4b 01 02 14 00 14 00 08 00
945 00004a0 08 00 00 00 00 00 a6 18 b1 91 5e 04 00 00 e4 47
946 00004b0 00 00 0a 00 00 00 00 00 00 00 00 00 00 00 00 00
947 00004c0 00 00 00 00 62 69 67 67 65 72 2e 7a 69 70 50 4b
948 00004d0 05 06 00 00 00 00 01 00 01 00 38 00 00 00 96 04
949 00004e0 00 00 00 00`
950 s = regexp.MustCompile(`[0-9a-f]{7}`).ReplaceAllString(s, "")
951 s = regexp.MustCompile(`\s+`).ReplaceAllString(s, "")
952 b, err := hex.DecodeString(s)
953 if err != nil {
954 panic(err)
955 }
956 return b
957 }
958
959 func returnBigZipBytes() (r io.ReaderAt, size int64) {
960 b := biggestZipBytes()
961 for i := 0; i < 2; i++ {
962 r, err := NewReader(bytes.NewReader(b), int64(len(b)))
963 if err != nil {
964 panic(err)
965 }
966 f, err := r.File[0].Open()
967 if err != nil {
968 panic(err)
969 }
970 b, err = io.ReadAll(f)
971 if err != nil {
972 panic(err)
973 }
974 }
975 return bytes.NewReader(b), int64(len(b))
976 }
977
978 func TestIssue8186(t *testing.T) {
979
980 dirEnts := []string{
981 "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",
982 "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",
983 "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",
984 "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",
985 "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",
986 "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",
987 "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",
988 "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",
989 "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",
990 }
991 for i, s := range dirEnts {
992 var f File
993 err := readDirectoryHeader(&f, strings.NewReader(s))
994 if err != nil {
995 t.Errorf("error reading #%d: %v", i, err)
996 }
997 }
998 }
999
1000
1001 func TestIssue10957(t *testing.T) {
1002 data := []byte("PK\x03\x040000000PK\x01\x0200000" +
1003 "0000000000000000000\x00" +
1004 "\x00\x00\x00\x00\x00000000000000PK\x01" +
1005 "\x020000000000000000000" +
1006 "00000\v\x00\x00\x00\x00\x00000000000" +
1007 "00000000000000PK\x01\x0200" +
1008 "00000000000000000000" +
1009 "00\v\x00\x00\x00\x00\x00000000000000" +
1010 "00000000000PK\x01\x020000<" +
1011 "0\x00\x0000000000000000\v\x00\v" +
1012 "\x00\x00\x00\x00\x0000000000\x00\x00\x00\x00000" +
1013 "00000000PK\x01\x0200000000" +
1014 "0000000000000000\v\x00\x00\x00" +
1015 "\x00\x0000PK\x05\x06000000\x05\x000000" +
1016 "\v\x00\x00\x00\x00\x00")
1017 z, err := NewReader(bytes.NewReader(data), int64(len(data)))
1018 if err != nil {
1019 t.Fatal(err)
1020 }
1021 for i, f := range z.File {
1022 r, err := f.Open()
1023 if err != nil {
1024 continue
1025 }
1026 if f.UncompressedSize64 < 1e6 {
1027 n, err := io.Copy(io.Discard, r)
1028 if i == 3 && err != io.ErrUnexpectedEOF {
1029 t.Errorf("File[3] error = %v; want io.ErrUnexpectedEOF", err)
1030 }
1031 if err == nil && uint64(n) != f.UncompressedSize64 {
1032 t.Errorf("file %d: bad size: copied=%d; want=%d", i, n, f.UncompressedSize64)
1033 }
1034 }
1035 r.Close()
1036 }
1037 }
1038
1039
1040 func TestIssue10956(t *testing.T) {
1041 data := []byte("PK\x06\x06PK\x06\a0000\x00\x00\x00\x00\x00\x00\x00\x00" +
1042 "0000PK\x05\x06000000000000" +
1043 "0000\v\x00000\x00\x00\x00\x00\x00\x00\x000")
1044 r, err := NewReader(bytes.NewReader(data), int64(len(data)))
1045 if err == nil {
1046 t.Errorf("got nil error, want ErrFormat")
1047 }
1048 if r != nil {
1049 t.Errorf("got non-nil Reader, want nil")
1050 }
1051 }
1052
1053
1054 func TestIssue11146(t *testing.T) {
1055 data := []byte("PK\x03\x040000000000000000" +
1056 "000000\x01\x00\x00\x000\x01\x00\x00\xff\xff0000" +
1057 "0000000000000000PK\x01\x02" +
1058 "0000\b0\b\x00000000000000" +
1059 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x000000PK\x05\x06\x00\x00" +
1060 "\x00\x0000\x01\x0000008\x00\x00\x00\x00\x00")
1061 z, err := NewReader(bytes.NewReader(data), int64(len(data)))
1062 if err != nil {
1063 t.Fatal(err)
1064 }
1065 r, err := z.File[0].Open()
1066 if err != nil {
1067 t.Fatal(err)
1068 }
1069 _, err = io.ReadAll(r)
1070 if err != io.ErrUnexpectedEOF {
1071 t.Errorf("File[0] error = %v; want io.ErrUnexpectedEOF", err)
1072 }
1073 r.Close()
1074 }
1075
1076
1077 func TestIssue12449(t *testing.T) {
1078 data := []byte{
1079 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00,
1080 0x00, 0x00, 0x6b, 0xb4, 0xba, 0x46, 0x00, 0x00,
1081 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1082 0x00, 0x00, 0x03, 0x00, 0x18, 0x00, 0xca, 0x64,
1083 0x55, 0x75, 0x78, 0x0b, 0x00, 0x50, 0x4b, 0x05,
1084 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
1085 0x00, 0x49, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00,
1086 0x00, 0x31, 0x31, 0x31, 0x32, 0x32, 0x32, 0x0a,
1087 0x50, 0x4b, 0x07, 0x08, 0x1d, 0x88, 0x77, 0xb0,
1088 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00,
1089 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00,
1090 0x08, 0x00, 0x00, 0x00, 0x6b, 0xb4, 0xba, 0x46,
1091 0x1d, 0x88, 0x77, 0xb0, 0x07, 0x00, 0x00, 0x00,
1092 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x18, 0x00,
1093 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1094 0xa0, 0x81, 0x00, 0x00, 0x00, 0x00, 0xca, 0x64,
1095 0x55, 0x75, 0x78, 0x0b, 0x00, 0x50, 0x4b, 0x05,
1096 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01,
1097 0x00, 0x49, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00,
1098 0x00, 0x97, 0x2b, 0x49, 0x23, 0x05, 0xc5, 0x0b,
1099 0xa7, 0xd1, 0x52, 0xa2, 0x9c, 0x50, 0x4b, 0x06,
1100 0x07, 0xc8, 0x19, 0xc1, 0xaf, 0x94, 0x9c, 0x61,
1101 0x44, 0xbe, 0x94, 0x19, 0x42, 0x58, 0x12, 0xc6,
1102 0x5b, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00,
1103 0x00, 0x01, 0x00, 0x01, 0x00, 0x69, 0x00, 0x00,
1104 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00,
1105 }
1106
1107 _, err := NewReader(bytes.NewReader([]byte(data)), int64(len(data)))
1108 if err != nil {
1109 t.Errorf("Error reading the archive: %v", err)
1110 }
1111 }
1112
1113 func TestFS(t *testing.T) {
1114 for _, test := range []struct {
1115 file string
1116 want []string
1117 }{
1118 {
1119 "testdata/unix.zip",
1120 []string{"hello", "dir/bar", "readonly"},
1121 },
1122 {
1123 "testdata/subdir.zip",
1124 []string{"a/b/c"},
1125 },
1126 } {
1127 t.Run(test.file, func(t *testing.T) {
1128 t.Parallel()
1129 z, err := OpenReader(test.file)
1130 if err != nil {
1131 t.Fatal(err)
1132 }
1133 defer z.Close()
1134 if err := fstest.TestFS(z, test.want...); err != nil {
1135 t.Error(err)
1136 }
1137 })
1138 }
1139 }
1140
1141 func TestFSModTime(t *testing.T) {
1142 t.Parallel()
1143 z, err := OpenReader("testdata/subdir.zip")
1144 if err != nil {
1145 t.Fatal(err)
1146 }
1147 defer z.Close()
1148
1149 for _, test := range []struct {
1150 name string
1151 want time.Time
1152 }{
1153 {
1154 "a",
1155 time.Date(2021, 4, 19, 12, 29, 56, 0, timeZone(-7*time.Hour)).UTC(),
1156 },
1157 {
1158 "a/b/c",
1159 time.Date(2021, 4, 19, 12, 29, 59, 0, timeZone(-7*time.Hour)).UTC(),
1160 },
1161 } {
1162 fi, err := fs.Stat(z, test.name)
1163 if err != nil {
1164 t.Errorf("%s: %v", test.name, err)
1165 continue
1166 }
1167 if got := fi.ModTime(); !got.Equal(test.want) {
1168 t.Errorf("%s: got modtime %v, want %v", test.name, got, test.want)
1169 }
1170 }
1171 }
1172
1173 func TestCVE202127919(t *testing.T) {
1174
1175 data := []byte{
1176 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00,
1177 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1178 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1179 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00, 0x2e, 0x2e,
1180 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74, 0x78,
1181 0x74, 0x0a, 0xc9, 0xc8, 0x2c, 0x56, 0xc8, 0x2c,
1182 0x56, 0x48, 0x54, 0x28, 0x49, 0x2d, 0x2e, 0x51,
1183 0x28, 0x49, 0xad, 0x28, 0x51, 0x48, 0xcb, 0xcc,
1184 0x49, 0xd5, 0xe3, 0x02, 0x04, 0x00, 0x00, 0xff,
1185 0xff, 0x50, 0x4b, 0x07, 0x08, 0xc0, 0xd7, 0xed,
1186 0xc3, 0x20, 0x00, 0x00, 0x00, 0x1a, 0x00, 0x00,
1187 0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x00, 0x14,
1188 0x00, 0x08, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00,
1189 0x00, 0xc0, 0xd7, 0xed, 0xc3, 0x20, 0x00, 0x00,
1190 0x00, 0x1a, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00,
1191 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1192 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2e,
1193 0x2e, 0x2f, 0x74, 0x65, 0x73, 0x74, 0x2e, 0x74,
1194 0x78, 0x74, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00,
1195 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x39, 0x00,
1196 0x00, 0x00, 0x59, 0x00, 0x00, 0x00, 0x00, 0x00,
1197 }
1198 r, err := NewReader(bytes.NewReader([]byte(data)), int64(len(data)))
1199 if err != nil {
1200 t.Fatalf("Error reading the archive: %v", err)
1201 }
1202 _, err = r.Open("test.txt")
1203 if err != nil {
1204 t.Errorf("Error reading file: %v", err)
1205 }
1206 if len(r.File) != 1 {
1207 t.Fatalf("No entries in the file list")
1208 }
1209 if r.File[0].Name != "../test.txt" {
1210 t.Errorf("Unexpected entry name: %s", r.File[0].Name)
1211 }
1212 if _, err := r.File[0].Open(); err != nil {
1213 t.Errorf("Error opening file: %v", err)
1214 }
1215 }
1216
1217 func TestCVE202133196(t *testing.T) {
1218
1219
1220
1221 data := []byte{
1222 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x08,
1223 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1224 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1225 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x01, 0x02,
1226 0x03, 0x62, 0x61, 0x65, 0x03, 0x04, 0x00, 0x00,
1227 0xff, 0xff, 0x50, 0x4b, 0x07, 0x08, 0xbe, 0x20,
1228 0x5c, 0x6c, 0x09, 0x00, 0x00, 0x00, 0x03, 0x00,
1229 0x00, 0x00, 0x50, 0x4b, 0x01, 0x02, 0x14, 0x00,
1230 0x14, 0x00, 0x08, 0x08, 0x08, 0x00, 0x00, 0x00,
1231 0x00, 0x00, 0xbe, 0x20, 0x5c, 0x6c, 0x09, 0x00,
1232 0x00, 0x00, 0x03, 0x00, 0x00, 0x00, 0x03, 0x00,
1233 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1234 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1235 0x01, 0x02, 0x03, 0x50, 0x4b, 0x06, 0x06, 0x2c,
1236 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2d,
1237 0x00, 0x2d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1238 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
1239 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0xff,
1240 0xff, 0xff, 0xff, 0x31, 0x00, 0x00, 0x00, 0x00,
1241 0x00, 0x00, 0x00, 0x3a, 0x00, 0x00, 0x00, 0x00,
1242 0x00, 0x00, 0x00, 0x50, 0x4b, 0x06, 0x07, 0x00,
1243 0x00, 0x00, 0x00, 0x6b, 0x00, 0x00, 0x00, 0x00,
1244 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x50,
1245 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 0x00, 0xff,
1246 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
1247 0xff, 0xff, 0xff, 0x00, 0x00,
1248 }
1249 _, err := NewReader(bytes.NewReader(data), int64(len(data)))
1250 if err != ErrFormat {
1251 t.Fatalf("unexpected error, got: %v, want: %v", err, ErrFormat)
1252 }
1253
1254
1255
1256 b := bytes.NewBuffer(nil)
1257 w := NewWriter(b)
1258 for i := 0; i < 5; i++ {
1259 _, err := w.Create("")
1260 if err != nil {
1261 t.Fatalf("Writer.Create failed: %s", err)
1262 }
1263 }
1264 if err := w.Close(); err != nil {
1265 t.Fatalf("Writer.Close failed: %s", err)
1266 }
1267 r, err := NewReader(bytes.NewReader(b.Bytes()), int64(b.Len()))
1268 if err != nil {
1269 t.Fatalf("NewReader failed: %s", err)
1270 }
1271 if len(r.File) != 5 {
1272 t.Errorf("Archive has unexpected number of files, got %d, want 5", len(r.File))
1273 }
1274 }
1275
1276 func TestCVE202139293(t *testing.T) {
1277
1278
1279
1280 data := []byte{
1281 0x50, 0x4b, 0x06, 0x06, 0x05, 0x06, 0x31, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b,
1282 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1283 0x00, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x50, 0x4b,
1284 0x06, 0x07, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1285 0x00, 0x00, 0x00, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x31, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff,
1286 0xff, 0x50, 0xfe, 0x00, 0xff, 0x00, 0x3a, 0x00, 0x00, 0x00, 0xff,
1287 }
1288 _, err := NewReader(bytes.NewReader(data), int64(len(data)))
1289 if err != ErrFormat {
1290 t.Fatalf("unexpected error, got: %v, want: %v", err, ErrFormat)
1291 }
1292 }
1293
1294 func TestCVE202141772(t *testing.T) {
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306 data := []byte{
1307 0x50, 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x08,
1308 0x00, 0x00, 0x06, 0x94, 0x05, 0x53, 0x00, 0x00,
1309 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1310 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x2f, 0x50,
1311 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00,
1312 0x00, 0x78, 0x67, 0x2e, 0x53, 0x00, 0x00, 0x00,
1313 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1314 0x00, 0x02, 0x00, 0x00, 0x00, 0x2f, 0x2f, 0x50,
1315 0x4b, 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00,
1316 0x00, 0x78, 0x67, 0x2e, 0x53, 0x00, 0x00, 0x00,
1317 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1318 0x00, 0x01, 0x00, 0x00, 0x00, 0x5c, 0x50, 0x4b,
1319 0x03, 0x04, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00,
1320 0x91, 0x68, 0x2e, 0x53, 0x85, 0x11, 0x4a, 0x0d,
1321 0x0b, 0x00, 0x00, 0x00, 0x0b, 0x00, 0x00, 0x00,
1322 0x09, 0x00, 0x00, 0x00, 0x2f, 0x74, 0x65, 0x73,
1323 0x74, 0x2e, 0x74, 0x78, 0x74, 0x68, 0x65, 0x6c,
1324 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64,
1325 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x0a, 0x00,
1326 0x00, 0x08, 0x00, 0x00, 0x06, 0x94, 0x05, 0x53,
1327 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1328 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
1329 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00,
1330 0xed, 0x41, 0x00, 0x00, 0x00, 0x00, 0x2f, 0x50,
1331 0x4b, 0x01, 0x02, 0x3f, 0x00, 0x0a, 0x00, 0x00,
1332 0x00, 0x00, 0x00, 0x78, 0x67, 0x2e, 0x53, 0x00,
1333 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1334 0x00, 0x00, 0x00, 0x02, 0x00, 0x24, 0x00, 0x00,
1335 0x00, 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00,
1336 0x00, 0x1f, 0x00, 0x00, 0x00, 0x2f, 0x2f, 0x0a,
1337 0x00, 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1338 0x00, 0x18, 0x00, 0x93, 0x98, 0x25, 0x57, 0x25,
1339 0xa9, 0xd7, 0x01, 0x93, 0x98, 0x25, 0x57, 0x25,
1340 0xa9, 0xd7, 0x01, 0x93, 0x98, 0x25, 0x57, 0x25,
1341 0xa9, 0xd7, 0x01, 0x50, 0x4b, 0x01, 0x02, 0x3f,
1342 0x00, 0x0a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x78,
1343 0x67, 0x2e, 0x53, 0x00, 0x00, 0x00, 0x00, 0x00,
1344 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01,
1345 0x00, 0x24, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
1346 0x00, 0x20, 0x00, 0x00, 0x00, 0x3f, 0x00, 0x00,
1347 0x00, 0x5c, 0x0a, 0x00, 0x20, 0x00, 0x00, 0x00,
1348 0x00, 0x00, 0x01, 0x00, 0x18, 0x00, 0x93, 0x98,
1349 0x25, 0x57, 0x25, 0xa9, 0xd7, 0x01, 0x93, 0x98,
1350 0x25, 0x57, 0x25, 0xa9, 0xd7, 0x01, 0x93, 0x98,
1351 0x25, 0x57, 0x25, 0xa9, 0xd7, 0x01, 0x50, 0x4b,
1352 0x01, 0x02, 0x3f, 0x00, 0x0a, 0x00, 0x00, 0x00,
1353 0x00, 0x00, 0x91, 0x68, 0x2e, 0x53, 0x85, 0x11,
1354 0x4a, 0x0d, 0x0b, 0x00, 0x00, 0x00, 0x0b, 0x00,
1355 0x00, 0x00, 0x09, 0x00, 0x24, 0x00, 0x00, 0x00,
1356 0x00, 0x00, 0x00, 0x00, 0x20, 0x00, 0x00, 0x00,
1357 0x5e, 0x00, 0x00, 0x00, 0x2f, 0x74, 0x65, 0x73,
1358 0x74, 0x2e, 0x74, 0x78, 0x74, 0x0a, 0x00, 0x20,
1359 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x18,
1360 0x00, 0xa9, 0x80, 0x51, 0x01, 0x26, 0xa9, 0xd7,
1361 0x01, 0x31, 0xd1, 0x57, 0x01, 0x26, 0xa9, 0xd7,
1362 0x01, 0xdf, 0x48, 0x85, 0xf9, 0x25, 0xa9, 0xd7,
1363 0x01, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00,
1364 0x00, 0x04, 0x00, 0x04, 0x00, 0x31, 0x01, 0x00,
1365 0x00, 0x90, 0x00, 0x00, 0x00, 0x00, 0x00,
1366 }
1367 r, err := NewReader(bytes.NewReader([]byte(data)), int64(len(data)))
1368 if err != nil {
1369 t.Fatalf("Error reading the archive: %v", err)
1370 }
1371 entryNames := []string{`/`, `//`, `\`, `/test.txt`}
1372 var names []string
1373 for _, f := range r.File {
1374 names = append(names, f.Name)
1375 if _, err := f.Open(); err != nil {
1376 t.Errorf("Error opening %q: %v", f.Name, err)
1377 }
1378 if _, err := r.Open(f.Name); err == nil {
1379 t.Errorf("Opening %q with fs.FS API succeeded", f.Name)
1380 }
1381 }
1382 if !reflect.DeepEqual(names, entryNames) {
1383 t.Errorf("Unexpected file entries: %q", names)
1384 }
1385 if _, err := r.Open(""); err == nil {
1386 t.Errorf("Opening %q with fs.FS API succeeded", "")
1387 }
1388 if _, err := r.Open("test.txt"); err != nil {
1389 t.Errorf("Error opening %q with fs.FS API: %v", "test.txt", err)
1390 }
1391 dirEntries, err := fs.ReadDir(r, ".")
1392 if err != nil {
1393 t.Fatalf("Error reading the root directory: %v", err)
1394 }
1395 if len(dirEntries) != 1 || dirEntries[0].Name() != "test.txt" {
1396 t.Errorf("Unexpected directory entries")
1397 for _, dirEntry := range dirEntries {
1398 _, err := r.Open(dirEntry.Name())
1399 t.Logf("%q (Open error: %v)", dirEntry.Name(), err)
1400 }
1401 t.FailNow()
1402 }
1403 info, err := dirEntries[0].Info()
1404 if err != nil {
1405 t.Fatalf("Error reading info entry: %v", err)
1406 }
1407 if name := info.Name(); name != "test.txt" {
1408 t.Errorf("Inconsistent name in info entry: %v", name)
1409 }
1410 }
1411
View as plain text