github.com/rafaeltorres324/go/src@v0.0.0-20210519164414-9fdf653a9838/archive/zip/reader_test.go (about) 1 // Copyright 2010 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 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 "regexp" 17 "strings" 18 "testing" 19 "testing/fstest" 20 "time" 21 ) 22 23 type ZipTest struct { 24 Name string 25 Source func() (r io.ReaderAt, size int64) // if non-nil, used instead of testdata/<Name> file 26 Comment string 27 File []ZipTestFile 28 Obscured bool // needed for Apple notarization (golang.org/issue/34986) 29 Error error // the error that Opening this file should return 30 } 31 32 type ZipTestFile struct { 33 Name string 34 Mode fs.FileMode 35 NonUTF8 bool 36 ModTime time.Time 37 Modified time.Time 38 39 // Information describing expected zip file content. 40 // First, reading the entire content should produce the error ContentErr. 41 // Second, if ContentErr==nil, the content should match Content. 42 // If content is large, an alternative to setting Content is to set File, 43 // which names a file in the testdata/ directory containing the 44 // uncompressed expected content. 45 // If content is very large, an alternative to setting Content or File 46 // is to set Size, which will then be checked against the header-reported size 47 // but will bypass the decompressing of the actual data. 48 // This last option is used for testing very large (multi-GB) compressed files. 49 ContentErr error 50 Content []byte 51 File string 52 Size uint64 53 } 54 55 var tests = []ZipTest{ 56 { 57 Name: "test.zip", 58 Comment: "This is a zipfile comment.", 59 File: []ZipTestFile{ 60 { 61 Name: "test.txt", 62 Content: []byte("This is a test text file.\n"), 63 Modified: time.Date(2010, 9, 5, 12, 12, 1, 0, timeZone(+10*time.Hour)), 64 Mode: 0644, 65 }, 66 { 67 Name: "gophercolor16x16.png", 68 File: "gophercolor16x16.png", 69 Modified: time.Date(2010, 9, 5, 15, 52, 58, 0, timeZone(+10*time.Hour)), 70 Mode: 0644, 71 }, 72 }, 73 }, 74 { 75 Name: "test-trailing-junk.zip", 76 Comment: "This is a zipfile comment.", 77 File: []ZipTestFile{ 78 { 79 Name: "test.txt", 80 Content: []byte("This is a test text file.\n"), 81 Modified: time.Date(2010, 9, 5, 12, 12, 1, 0, timeZone(+10*time.Hour)), 82 Mode: 0644, 83 }, 84 { 85 Name: "gophercolor16x16.png", 86 File: "gophercolor16x16.png", 87 Modified: time.Date(2010, 9, 5, 15, 52, 58, 0, timeZone(+10*time.Hour)), 88 Mode: 0644, 89 }, 90 }, 91 }, 92 { 93 Name: "r.zip", 94 Source: returnRecursiveZip, 95 File: []ZipTestFile{ 96 { 97 Name: "r/r.zip", 98 Content: rZipBytes(), 99 Modified: time.Date(2010, 3, 4, 0, 24, 16, 0, time.UTC), 100 Mode: 0666, 101 }, 102 }, 103 }, 104 { 105 Name: "symlink.zip", 106 File: []ZipTestFile{ 107 { 108 Name: "symlink", 109 Content: []byte("../target"), 110 Modified: time.Date(2012, 2, 3, 19, 56, 48, 0, timeZone(-2*time.Hour)), 111 Mode: 0777 | fs.ModeSymlink, 112 }, 113 }, 114 }, 115 { 116 Name: "readme.zip", 117 }, 118 { 119 Name: "readme.notzip", 120 Error: ErrFormat, 121 }, 122 { 123 Name: "dd.zip", 124 File: []ZipTestFile{ 125 { 126 Name: "filename", 127 Content: []byte("This is a test textfile.\n"), 128 Modified: time.Date(2011, 2, 2, 13, 6, 20, 0, time.UTC), 129 Mode: 0666, 130 }, 131 }, 132 }, 133 { 134 // created in windows XP file manager. 135 Name: "winxp.zip", 136 File: []ZipTestFile{ 137 { 138 Name: "hello", 139 Content: []byte("world \r\n"), 140 Modified: time.Date(2011, 12, 8, 10, 4, 24, 0, time.UTC), 141 Mode: 0666, 142 }, 143 { 144 Name: "dir/bar", 145 Content: []byte("foo \r\n"), 146 Modified: time.Date(2011, 12, 8, 10, 4, 50, 0, time.UTC), 147 Mode: 0666, 148 }, 149 { 150 Name: "dir/empty/", 151 Content: []byte{}, 152 Modified: time.Date(2011, 12, 8, 10, 8, 6, 0, time.UTC), 153 Mode: fs.ModeDir | 0777, 154 }, 155 { 156 Name: "readonly", 157 Content: []byte("important \r\n"), 158 Modified: time.Date(2011, 12, 8, 10, 6, 8, 0, time.UTC), 159 Mode: 0444, 160 }, 161 }, 162 }, 163 { 164 // created by Zip 3.0 under Linux 165 Name: "unix.zip", 166 File: []ZipTestFile{ 167 { 168 Name: "hello", 169 Content: []byte("world \r\n"), 170 Modified: time.Date(2011, 12, 8, 10, 4, 24, 0, timeZone(0)), 171 Mode: 0666, 172 }, 173 { 174 Name: "dir/bar", 175 Content: []byte("foo \r\n"), 176 Modified: time.Date(2011, 12, 8, 10, 4, 50, 0, timeZone(0)), 177 Mode: 0666, 178 }, 179 { 180 Name: "dir/empty/", 181 Content: []byte{}, 182 Modified: time.Date(2011, 12, 8, 10, 8, 6, 0, timeZone(0)), 183 Mode: fs.ModeDir | 0777, 184 }, 185 { 186 Name: "readonly", 187 Content: []byte("important \r\n"), 188 Modified: time.Date(2011, 12, 8, 10, 6, 8, 0, timeZone(0)), 189 Mode: 0444, 190 }, 191 }, 192 }, 193 { 194 // created by Go, before we wrote the "optional" data 195 // descriptor signatures (which are required by macOS). 196 // Use obscured file to avoid Apple’s notarization service 197 // rejecting the toolchain due to an inability to unzip this archive. 198 // See golang.org/issue/34986 199 Name: "go-no-datadesc-sig.zip.base64", 200 Obscured: true, 201 File: []ZipTestFile{ 202 { 203 Name: "foo.txt", 204 Content: []byte("foo\n"), 205 Modified: time.Date(2012, 3, 8, 16, 59, 10, 0, timeZone(-8*time.Hour)), 206 Mode: 0644, 207 }, 208 { 209 Name: "bar.txt", 210 Content: []byte("bar\n"), 211 Modified: time.Date(2012, 3, 8, 16, 59, 12, 0, timeZone(-8*time.Hour)), 212 Mode: 0644, 213 }, 214 }, 215 }, 216 { 217 // created by Go, after we wrote the "optional" data 218 // descriptor signatures (which are required by macOS) 219 Name: "go-with-datadesc-sig.zip", 220 File: []ZipTestFile{ 221 { 222 Name: "foo.txt", 223 Content: []byte("foo\n"), 224 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC), 225 Mode: 0666, 226 }, 227 { 228 Name: "bar.txt", 229 Content: []byte("bar\n"), 230 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC), 231 Mode: 0666, 232 }, 233 }, 234 }, 235 { 236 Name: "Bad-CRC32-in-data-descriptor", 237 Source: returnCorruptCRC32Zip, 238 File: []ZipTestFile{ 239 { 240 Name: "foo.txt", 241 Content: []byte("foo\n"), 242 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC), 243 Mode: 0666, 244 ContentErr: ErrChecksum, 245 }, 246 { 247 Name: "bar.txt", 248 Content: []byte("bar\n"), 249 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC), 250 Mode: 0666, 251 }, 252 }, 253 }, 254 // Tests that we verify (and accept valid) crc32s on files 255 // with crc32s in their file header (not in data descriptors) 256 { 257 Name: "crc32-not-streamed.zip", 258 File: []ZipTestFile{ 259 { 260 Name: "foo.txt", 261 Content: []byte("foo\n"), 262 Modified: time.Date(2012, 3, 8, 16, 59, 10, 0, timeZone(-8*time.Hour)), 263 Mode: 0644, 264 }, 265 { 266 Name: "bar.txt", 267 Content: []byte("bar\n"), 268 Modified: time.Date(2012, 3, 8, 16, 59, 12, 0, timeZone(-8*time.Hour)), 269 Mode: 0644, 270 }, 271 }, 272 }, 273 // Tests that we verify (and reject invalid) crc32s on files 274 // with crc32s in their file header (not in data descriptors) 275 { 276 Name: "crc32-not-streamed.zip", 277 Source: returnCorruptNotStreamedZip, 278 File: []ZipTestFile{ 279 { 280 Name: "foo.txt", 281 Content: []byte("foo\n"), 282 Modified: time.Date(2012, 3, 8, 16, 59, 10, 0, timeZone(-8*time.Hour)), 283 Mode: 0644, 284 ContentErr: ErrChecksum, 285 }, 286 { 287 Name: "bar.txt", 288 Content: []byte("bar\n"), 289 Modified: time.Date(2012, 3, 8, 16, 59, 12, 0, timeZone(-8*time.Hour)), 290 Mode: 0644, 291 }, 292 }, 293 }, 294 { 295 Name: "zip64.zip", 296 File: []ZipTestFile{ 297 { 298 Name: "README", 299 Content: []byte("This small file is in ZIP64 format.\n"), 300 Modified: time.Date(2012, 8, 10, 14, 33, 32, 0, time.UTC), 301 Mode: 0644, 302 }, 303 }, 304 }, 305 // Another zip64 file with different Extras fields. (golang.org/issue/7069) 306 { 307 Name: "zip64-2.zip", 308 File: []ZipTestFile{ 309 { 310 Name: "README", 311 Content: []byte("This small file is in ZIP64 format.\n"), 312 Modified: time.Date(2012, 8, 10, 14, 33, 32, 0, timeZone(-4*time.Hour)), 313 Mode: 0644, 314 }, 315 }, 316 }, 317 // Largest possible non-zip64 file, with no zip64 header. 318 { 319 Name: "big.zip", 320 Source: returnBigZipBytes, 321 File: []ZipTestFile{ 322 { 323 Name: "big.file", 324 Content: nil, 325 Size: 1<<32 - 1, 326 Modified: time.Date(1979, 11, 30, 0, 0, 0, 0, time.UTC), 327 Mode: 0666, 328 }, 329 }, 330 }, 331 { 332 Name: "utf8-7zip.zip", 333 File: []ZipTestFile{ 334 { 335 Name: "世界", 336 Content: []byte{}, 337 Mode: 0666, 338 Modified: time.Date(2017, 11, 6, 13, 9, 27, 867862500, timeZone(-8*time.Hour)), 339 }, 340 }, 341 }, 342 { 343 Name: "utf8-infozip.zip", 344 File: []ZipTestFile{ 345 { 346 Name: "世界", 347 Content: []byte{}, 348 Mode: 0644, 349 // Name is valid UTF-8, but format does not have UTF-8 flag set. 350 // We don't do UTF-8 detection for multi-byte runes due to 351 // false-positives with other encodings (e.g., Shift-JIS). 352 // Format says encoding is not UTF-8, so we trust it. 353 NonUTF8: true, 354 Modified: time.Date(2017, 11, 6, 13, 9, 27, 0, timeZone(-8*time.Hour)), 355 }, 356 }, 357 }, 358 { 359 Name: "utf8-osx.zip", 360 File: []ZipTestFile{ 361 { 362 Name: "世界", 363 Content: []byte{}, 364 Mode: 0644, 365 // Name is valid UTF-8, but format does not have UTF-8 set. 366 NonUTF8: true, 367 Modified: time.Date(2017, 11, 6, 13, 9, 27, 0, timeZone(-8*time.Hour)), 368 }, 369 }, 370 }, 371 { 372 Name: "utf8-winrar.zip", 373 File: []ZipTestFile{ 374 { 375 Name: "世界", 376 Content: []byte{}, 377 Mode: 0666, 378 Modified: time.Date(2017, 11, 6, 13, 9, 27, 867862500, timeZone(-8*time.Hour)), 379 }, 380 }, 381 }, 382 { 383 Name: "utf8-winzip.zip", 384 File: []ZipTestFile{ 385 { 386 Name: "世界", 387 Content: []byte{}, 388 Mode: 0666, 389 Modified: time.Date(2017, 11, 6, 13, 9, 27, 867000000, timeZone(-8*time.Hour)), 390 }, 391 }, 392 }, 393 { 394 Name: "time-7zip.zip", 395 File: []ZipTestFile{ 396 { 397 Name: "test.txt", 398 Content: []byte{}, 399 Size: 1<<32 - 1, 400 Modified: time.Date(2017, 10, 31, 21, 11, 57, 244817900, timeZone(-7*time.Hour)), 401 Mode: 0666, 402 }, 403 }, 404 }, 405 { 406 Name: "time-infozip.zip", 407 File: []ZipTestFile{ 408 { 409 Name: "test.txt", 410 Content: []byte{}, 411 Size: 1<<32 - 1, 412 Modified: time.Date(2017, 10, 31, 21, 11, 57, 0, timeZone(-7*time.Hour)), 413 Mode: 0644, 414 }, 415 }, 416 }, 417 { 418 Name: "time-osx.zip", 419 File: []ZipTestFile{ 420 { 421 Name: "test.txt", 422 Content: []byte{}, 423 Size: 1<<32 - 1, 424 Modified: time.Date(2017, 10, 31, 21, 11, 57, 0, timeZone(-7*time.Hour)), 425 Mode: 0644, 426 }, 427 }, 428 }, 429 { 430 Name: "time-win7.zip", 431 File: []ZipTestFile{ 432 { 433 Name: "test.txt", 434 Content: []byte{}, 435 Size: 1<<32 - 1, 436 Modified: time.Date(2017, 10, 31, 21, 11, 58, 0, time.UTC), 437 Mode: 0666, 438 }, 439 }, 440 }, 441 { 442 Name: "time-winrar.zip", 443 File: []ZipTestFile{ 444 { 445 Name: "test.txt", 446 Content: []byte{}, 447 Size: 1<<32 - 1, 448 Modified: time.Date(2017, 10, 31, 21, 11, 57, 244817900, timeZone(-7*time.Hour)), 449 Mode: 0666, 450 }, 451 }, 452 }, 453 { 454 Name: "time-winzip.zip", 455 File: []ZipTestFile{ 456 { 457 Name: "test.txt", 458 Content: []byte{}, 459 Size: 1<<32 - 1, 460 Modified: time.Date(2017, 10, 31, 21, 11, 57, 244000000, timeZone(-7*time.Hour)), 461 Mode: 0666, 462 }, 463 }, 464 }, 465 { 466 Name: "time-go.zip", 467 File: []ZipTestFile{ 468 { 469 Name: "test.txt", 470 Content: []byte{}, 471 Size: 1<<32 - 1, 472 Modified: time.Date(2017, 10, 31, 21, 11, 57, 0, timeZone(-7*time.Hour)), 473 Mode: 0666, 474 }, 475 }, 476 }, 477 { 478 Name: "time-22738.zip", 479 File: []ZipTestFile{ 480 { 481 Name: "file", 482 Content: []byte{}, 483 Mode: 0666, 484 Modified: time.Date(1999, 12, 31, 19, 0, 0, 0, timeZone(-5*time.Hour)), 485 ModTime: time.Date(1999, 12, 31, 19, 0, 0, 0, time.UTC), 486 }, 487 }, 488 }, 489 } 490 491 func TestReader(t *testing.T) { 492 for _, zt := range tests { 493 t.Run(zt.Name, func(t *testing.T) { 494 readTestZip(t, zt) 495 }) 496 } 497 } 498 499 func readTestZip(t *testing.T, zt ZipTest) { 500 var z *Reader 501 var err error 502 if zt.Source != nil { 503 rat, size := zt.Source() 504 z, err = NewReader(rat, size) 505 } else { 506 path := filepath.Join("testdata", zt.Name) 507 if zt.Obscured { 508 tf, err := obscuretestdata.DecodeToTempFile(path) 509 if err != nil { 510 t.Errorf("obscuretestdata.DecodeToTempFile(%s): %v", path, err) 511 return 512 } 513 defer os.Remove(tf) 514 path = tf 515 } 516 var rc *ReadCloser 517 rc, err = OpenReader(path) 518 if err == nil { 519 defer rc.Close() 520 z = &rc.Reader 521 } 522 } 523 if err != zt.Error { 524 t.Errorf("error=%v, want %v", err, zt.Error) 525 return 526 } 527 528 // bail if file is not zip 529 if err == ErrFormat { 530 return 531 } 532 533 // bail here if no Files expected to be tested 534 // (there may actually be files in the zip, but we don't care) 535 if zt.File == nil { 536 return 537 } 538 539 if z.Comment != zt.Comment { 540 t.Errorf("comment=%q, want %q", z.Comment, zt.Comment) 541 } 542 if len(z.File) != len(zt.File) { 543 t.Fatalf("file count=%d, want %d", len(z.File), len(zt.File)) 544 } 545 546 // test read of each file 547 for i, ft := range zt.File { 548 readTestFile(t, zt, ft, z.File[i]) 549 } 550 if t.Failed() { 551 return 552 } 553 554 // test simultaneous reads 555 n := 0 556 done := make(chan bool) 557 for i := 0; i < 5; i++ { 558 for j, ft := range zt.File { 559 go func(j int, ft ZipTestFile) { 560 readTestFile(t, zt, ft, z.File[j]) 561 done <- true 562 }(j, ft) 563 n++ 564 } 565 } 566 for ; n > 0; n-- { 567 <-done 568 } 569 } 570 571 func equalTimeAndZone(t1, t2 time.Time) bool { 572 name1, offset1 := t1.Zone() 573 name2, offset2 := t2.Zone() 574 return t1.Equal(t2) && name1 == name2 && offset1 == offset2 575 } 576 577 func readTestFile(t *testing.T, zt ZipTest, ft ZipTestFile, f *File) { 578 if f.Name != ft.Name { 579 t.Errorf("name=%q, want %q", f.Name, ft.Name) 580 } 581 if !ft.Modified.IsZero() && !equalTimeAndZone(f.Modified, ft.Modified) { 582 t.Errorf("%s: Modified=%s, want %s", f.Name, f.Modified, ft.Modified) 583 } 584 if !ft.ModTime.IsZero() && !equalTimeAndZone(f.ModTime(), ft.ModTime) { 585 t.Errorf("%s: ModTime=%s, want %s", f.Name, f.ModTime(), ft.ModTime) 586 } 587 588 testFileMode(t, f, ft.Mode) 589 590 size := uint64(f.UncompressedSize) 591 if size == uint32max { 592 size = f.UncompressedSize64 593 } else if size != f.UncompressedSize64 { 594 t.Errorf("%v: UncompressedSize=%#x does not match UncompressedSize64=%#x", f.Name, size, f.UncompressedSize64) 595 } 596 597 r, err := f.Open() 598 if err != nil { 599 t.Errorf("%v", err) 600 return 601 } 602 603 // For very large files, just check that the size is correct. 604 // The content is expected to be all zeros. 605 // Don't bother uncompressing: too big. 606 if ft.Content == nil && ft.File == "" && ft.Size > 0 { 607 if size != ft.Size { 608 t.Errorf("%v: uncompressed size %#x, want %#x", ft.Name, size, ft.Size) 609 } 610 r.Close() 611 return 612 } 613 614 var b bytes.Buffer 615 _, err = io.Copy(&b, r) 616 if err != ft.ContentErr { 617 t.Errorf("copying contents: %v (want %v)", err, ft.ContentErr) 618 } 619 if err != nil { 620 return 621 } 622 r.Close() 623 624 if g := uint64(b.Len()); g != size { 625 t.Errorf("%v: read %v bytes but f.UncompressedSize == %v", f.Name, g, size) 626 } 627 628 var c []byte 629 if ft.Content != nil { 630 c = ft.Content 631 } else if c, err = os.ReadFile("testdata/" + ft.File); err != nil { 632 t.Error(err) 633 return 634 } 635 636 if b.Len() != len(c) { 637 t.Errorf("%s: len=%d, want %d", f.Name, b.Len(), len(c)) 638 return 639 } 640 641 for i, b := range b.Bytes() { 642 if b != c[i] { 643 t.Errorf("%s: content[%d]=%q want %q", f.Name, i, b, c[i]) 644 return 645 } 646 } 647 } 648 649 func testFileMode(t *testing.T, f *File, want fs.FileMode) { 650 mode := f.Mode() 651 if want == 0 { 652 t.Errorf("%s mode: got %v, want none", f.Name, mode) 653 } else if mode != want { 654 t.Errorf("%s mode: want %v, got %v", f.Name, want, mode) 655 } 656 } 657 658 func TestInvalidFiles(t *testing.T) { 659 const size = 1024 * 70 // 70kb 660 b := make([]byte, size) 661 662 // zeroes 663 _, err := NewReader(bytes.NewReader(b), size) 664 if err != ErrFormat { 665 t.Errorf("zeroes: error=%v, want %v", err, ErrFormat) 666 } 667 668 // repeated directoryEndSignatures 669 sig := make([]byte, 4) 670 binary.LittleEndian.PutUint32(sig, directoryEndSignature) 671 for i := 0; i < size-4; i += 4 { 672 copy(b[i:i+4], sig) 673 } 674 _, err = NewReader(bytes.NewReader(b), size) 675 if err != ErrFormat { 676 t.Errorf("sigs: error=%v, want %v", err, ErrFormat) 677 } 678 679 // negative size 680 _, err = NewReader(bytes.NewReader([]byte("foobar")), -1) 681 if err == nil { 682 t.Errorf("archive/zip.NewReader: expected error when negative size is passed") 683 } 684 } 685 686 func messWith(fileName string, corrupter func(b []byte)) (r io.ReaderAt, size int64) { 687 data, err := os.ReadFile(filepath.Join("testdata", fileName)) 688 if err != nil { 689 panic("Error reading " + fileName + ": " + err.Error()) 690 } 691 corrupter(data) 692 return bytes.NewReader(data), int64(len(data)) 693 } 694 695 func returnCorruptCRC32Zip() (r io.ReaderAt, size int64) { 696 return messWith("go-with-datadesc-sig.zip", func(b []byte) { 697 // Corrupt one of the CRC32s in the data descriptor: 698 b[0x2d]++ 699 }) 700 } 701 702 func returnCorruptNotStreamedZip() (r io.ReaderAt, size int64) { 703 return messWith("crc32-not-streamed.zip", func(b []byte) { 704 // Corrupt foo.txt's final crc32 byte, in both 705 // the file header and TOC. (0x7e -> 0x7f) 706 b[0x11]++ 707 b[0x9d]++ 708 709 // TODO(bradfitz): add a new test that only corrupts 710 // one of these values, and verify that that's also an 711 // error. Currently, the reader code doesn't verify the 712 // fileheader and TOC's crc32 match if they're both 713 // non-zero and only the second line above, the TOC, 714 // is what matters. 715 }) 716 } 717 718 // rZipBytes returns the bytes of a recursive zip file, without 719 // putting it on disk and triggering certain virus scanners. 720 func rZipBytes() []byte { 721 s := ` 722 0000000 50 4b 03 04 14 00 00 00 08 00 08 03 64 3c f9 f4 723 0000010 89 64 48 01 00 00 b8 01 00 00 07 00 00 00 72 2f 724 0000020 72 2e 7a 69 70 00 25 00 da ff 50 4b 03 04 14 00 725 0000030 00 00 08 00 08 03 64 3c f9 f4 89 64 48 01 00 00 726 0000040 b8 01 00 00 07 00 00 00 72 2f 72 2e 7a 69 70 00 727 0000050 2f 00 d0 ff 00 25 00 da ff 50 4b 03 04 14 00 00 728 0000060 00 08 00 08 03 64 3c f9 f4 89 64 48 01 00 00 b8 729 0000070 01 00 00 07 00 00 00 72 2f 72 2e 7a 69 70 00 2f 730 0000080 00 d0 ff c2 54 8e 57 39 00 05 00 fa ff c2 54 8e 731 0000090 57 39 00 05 00 fa ff 00 05 00 fa ff 00 14 00 eb 732 00000a0 ff c2 54 8e 57 39 00 05 00 fa ff 00 05 00 fa ff 733 00000b0 00 14 00 eb ff 42 88 21 c4 00 00 14 00 eb ff 42 734 00000c0 88 21 c4 00 00 14 00 eb ff 42 88 21 c4 00 00 14 735 00000d0 00 eb ff 42 88 21 c4 00 00 14 00 eb ff 42 88 21 736 00000e0 c4 00 00 00 00 ff ff 00 00 00 ff ff 00 34 00 cb 737 00000f0 ff 42 88 21 c4 00 00 00 00 ff ff 00 00 00 ff ff 738 0000100 00 34 00 cb ff 42 e8 21 5e 0f 00 00 00 ff ff 0a 739 0000110 f0 66 64 12 61 c0 15 dc e8 a0 48 bf 48 af 2a b3 740 0000120 20 c0 9b 95 0d c4 67 04 42 53 06 06 06 40 00 06 741 0000130 00 f9 ff 6d 01 00 00 00 00 42 e8 21 5e 0f 00 00 742 0000140 00 ff ff 0a f0 66 64 12 61 c0 15 dc e8 a0 48 bf 743 0000150 48 af 2a b3 20 c0 9b 95 0d c4 67 04 42 53 06 06 744 0000160 06 40 00 06 00 f9 ff 6d 01 00 00 00 00 50 4b 01 745 0000170 02 14 00 14 00 00 00 08 00 08 03 64 3c f9 f4 89 746 0000180 64 48 01 00 00 b8 01 00 00 07 00 00 00 00 00 00 747 0000190 00 00 00 00 00 00 00 00 00 00 00 72 2f 72 2e 7a 748 00001a0 69 70 50 4b 05 06 00 00 00 00 01 00 01 00 35 00 749 00001b0 00 00 6d 01 00 00 00 00` 750 s = regexp.MustCompile(`[0-9a-f]{7}`).ReplaceAllString(s, "") 751 s = regexp.MustCompile(`\s+`).ReplaceAllString(s, "") 752 b, err := hex.DecodeString(s) 753 if err != nil { 754 panic(err) 755 } 756 return b 757 } 758 759 func returnRecursiveZip() (r io.ReaderAt, size int64) { 760 b := rZipBytes() 761 return bytes.NewReader(b), int64(len(b)) 762 } 763 764 // biggestZipBytes returns the bytes of a zip file biggest.zip 765 // that contains a zip file bigger.zip that contains a zip file 766 // big.zip that contains big.file, which contains 2³²-1 zeros. 767 // The big.zip file is interesting because it has no zip64 header, 768 // much like the innermost zip files in the well-known 42.zip. 769 // 770 // biggest.zip was generated by changing isZip64 to use > uint32max 771 // instead of >= uint32max and then running this program: 772 // 773 // package main 774 // 775 // import ( 776 // "archive/zip" 777 // "bytes" 778 // "io" 779 // "io/ioutil" 780 // "log" 781 // ) 782 // 783 // type zeros struct{} 784 // 785 // func (zeros) Read(b []byte) (int, error) { 786 // for i := range b { 787 // b[i] = 0 788 // } 789 // return len(b), nil 790 // } 791 // 792 // func main() { 793 // bigZip := makeZip("big.file", io.LimitReader(zeros{}, 1<<32-1)) 794 // if err := os.WriteFile("/tmp/big.zip", bigZip, 0666); err != nil { 795 // log.Fatal(err) 796 // } 797 // 798 // biggerZip := makeZip("big.zip", bytes.NewReader(bigZip)) 799 // if err := os.WriteFile("/tmp/bigger.zip", biggerZip, 0666); err != nil { 800 // log.Fatal(err) 801 // } 802 // 803 // biggestZip := makeZip("bigger.zip", bytes.NewReader(biggerZip)) 804 // if err := os.WriteFile("/tmp/biggest.zip", biggestZip, 0666); err != nil { 805 // log.Fatal(err) 806 // } 807 // } 808 // 809 // func makeZip(name string, r io.Reader) []byte { 810 // var buf bytes.Buffer 811 // w := zip.NewWriter(&buf) 812 // wf, err := w.Create(name) 813 // if err != nil { 814 // log.Fatal(err) 815 // } 816 // if _, err = io.Copy(wf, r); err != nil { 817 // log.Fatal(err) 818 // } 819 // if err := w.Close(); err != nil { 820 // log.Fatal(err) 821 // } 822 // return buf.Bytes() 823 // } 824 // 825 // The 4 GB of zeros compresses to 4 MB, which compresses to 20 kB, 826 // which compresses to 1252 bytes (in the hex dump below). 827 // 828 // It's here in hex for the same reason as rZipBytes above: to avoid 829 // problems with on-disk virus scanners or other zip processors. 830 // 831 func biggestZipBytes() []byte { 832 s := ` 833 0000000 50 4b 03 04 14 00 08 00 08 00 00 00 00 00 00 00 834 0000010 00 00 00 00 00 00 00 00 00 00 0a 00 00 00 62 69 835 0000020 67 67 65 72 2e 7a 69 70 ec dc 6b 4c 53 67 18 07 836 0000030 f0 16 c5 ca 65 2e cb b8 94 20 61 1f 44 33 c7 cd 837 0000040 c0 86 4a b5 c0 62 8a 61 05 c6 cd 91 b2 54 8c 1b 838 0000050 63 8b 03 9c 1b 95 52 5a e3 a0 19 6c b2 05 59 44 839 0000060 64 9d 73 83 71 11 46 61 14 b9 1d 14 09 4a c3 60 840 0000070 2e 4c 6e a5 60 45 02 62 81 95 b6 94 9e 9e 77 e7 841 0000080 d0 43 b6 f8 71 df 96 3c e7 a4 69 ce bf cf e9 79 842 0000090 ce ef 79 3f bf f1 31 db b6 bb 31 76 92 e7 f3 07 843 00000a0 8b fc 9c ca cc 08 cc cb cc 5e d2 1c 88 d9 7e bb 844 00000b0 4f bb 3a 3f 75 f1 5d 7f 8f c2 68 67 77 8f 25 ff 845 00000c0 84 e2 93 2d ef a4 95 3d 71 4e 2c b9 b0 87 c3 be 846 00000d0 3d f8 a7 60 24 61 c5 ef ae 9e c8 6c 6d 4e 69 c8 847 00000e0 67 65 34 f8 37 76 2d 76 5c 54 f3 95 65 49 c7 0f 848 00000f0 18 71 4b 7e 5b 6a d1 79 47 61 41 b0 4e 2a 74 45 849 0000100 43 58 12 b2 5a a5 c6 7d 68 55 88 d4 98 75 18 6d 850 0000110 08 d1 1f 8f 5a 9e 96 ee 45 cf a4 84 4e 4b e8 50 851 0000120 a7 13 d9 06 de 52 81 97 36 b2 d7 b8 fc 2b 5f 55 852 0000130 23 1f 32 59 cf 30 27 fb e2 8a b9 de 45 dd 63 9c 853 0000140 4b b5 8b 96 4c 7a 62 62 cc a1 a7 cf fa f1 fe dd 854 0000150 54 62 11 bf 36 78 b3 c7 b1 b5 f2 61 4d 4e dd 66 855 0000160 32 2e e6 70 34 5f f4 c9 e6 6c 43 6f da 6b c6 c3 856 0000170 09 2c ce 09 57 7f d2 7e b4 23 ba 7c 1b 99 bc 22 857 0000180 3e f1 de 91 2f e3 9c 1b 82 cc c2 84 39 aa e6 de 858 0000190 b4 69 fc cc cb 72 a6 61 45 f0 d3 1d 26 19 7c 8d 859 00001a0 29 c8 66 02 be 77 6a f9 3d 34 79 17 19 c8 96 24 860 00001b0 a3 ac e4 dd 3b 1a 8e c6 fe 96 38 6b bf 67 5a 23 861 00001c0 f4 16 f4 e6 8a b4 fc c2 cd bf 95 66 1d bb 35 aa 862 00001d0 92 7d 66 d8 08 8d a5 1f 54 2a af 09 cf 61 ff d2 863 00001e0 85 9d 8f b6 d7 88 07 4a 86 03 db 64 f3 d9 92 73 864 00001f0 df ec a7 fc 23 4c 8d 83 79 63 2a d9 fd 8d b3 c8 865 0000200 8f 7e d4 19 85 e6 8d 1c 76 f0 8b 58 32 fd 9a d6 866 0000210 85 e2 48 ad c3 d5 60 6f 7e 22 dd ef 09 49 7c 7f 867 0000220 3a 45 c3 71 b7 df f3 4c 63 fb b5 d9 31 5f 6e d6 868 0000230 24 1d a4 4a fe 32 a7 5c 16 48 5c 3e 08 6b 8a d3 869 0000240 25 1d a2 12 a5 59 24 ea 20 5f 52 6d ad 94 db 6b 870 0000250 94 b9 5d eb 4b a7 5c 44 bb 1e f2 3c 6b cf 52 c9 871 0000260 e9 e5 ba 06 b9 c4 e5 0a d0 00 0d d0 00 0d d0 00 872 0000270 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d 873 0000280 d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0 874 0000290 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00 0d d0 00 875 00002a0 0d d0 00 cd ff 9e 46 86 fa a7 7d 3a 43 d7 8e 10 876 00002b0 52 e9 be e6 6e cf eb 9e 85 4d 65 ce cc 30 c1 44 877 00002c0 c0 4e af bc 9c 6c 4b a0 d7 54 ff 1d d5 5c 89 fb 878 00002d0 b5 34 7e c4 c2 9e f5 a0 f6 5b 7e 6e ca 73 c7 ef 879 00002e0 5d be de f9 e8 81 eb a5 0a a5 63 54 2c d7 1c d1 880 00002f0 89 17 85 f8 16 94 f2 8a b2 a3 f5 b6 6d df 75 cd 881 0000300 90 dd 64 bd 5d 55 4e f2 55 19 1b b7 cc ef 1b ea 882 0000310 2e 05 9c f4 aa 1e a8 cd a6 82 c7 59 0f 5e 9d e0 883 0000320 bb fc 6c d6 99 23 eb 36 ad c6 c5 e1 d8 e1 e2 3e 884 0000330 d9 90 5a f7 91 5d 6f bc 33 6d 98 47 d2 7c 2e 2f 885 0000340 99 a4 25 72 85 49 2c be 0b 5b af 8f e5 6e 81 a6 886 0000350 a3 5a 6f 39 53 3a ab 7a 8b 1e 26 f7 46 6c 7d 26 887 0000360 53 b3 22 31 94 d3 83 f2 18 4d f5 92 33 27 53 97 888 0000370 0f d3 e6 55 9c a6 c5 31 87 6f d3 f3 ae 39 6f 56 889 0000380 10 7b ab 7e d0 b4 ca f2 b8 05 be 3f 0e 6e 5a 75 890 0000390 ab 0c f5 37 0e ba 8e 75 71 7a aa ed 7a dd 6a 63 891 00003a0 be 9b a0 97 27 6a 6f e7 d3 8b c4 7c ec d3 91 56 892 00003b0 d9 ac 5e bf 16 42 2f 00 1f 93 a2 23 87 bd e2 59 893 00003c0 a0 de 1a 66 c8 62 eb 55 8f 91 17 b4 61 42 7a 50 894 00003d0 40 03 34 40 03 34 40 03 34 40 03 34 40 03 34 40 895 00003e0 03 34 40 03 34 40 03 34 40 03 34 40 03 34 40 03 896 00003f0 34 40 03 34 40 03 34 ff 85 86 90 8b ea 67 90 0d 897 0000400 e1 42 1b d2 61 d6 79 ec fd 3e 44 28 a4 51 6c 5c 898 0000410 fc d2 72 ca ba 82 18 46 16 61 cd 93 a9 0f d1 24 899 0000420 17 99 e2 2c 71 16 84 0c c8 7a 13 0f 9a 5e c5 f0 900 0000430 79 64 e2 12 4d c8 82 a1 81 19 2d aa 44 6d 87 54 901 0000440 84 71 c1 f6 d4 ca 25 8c 77 b9 08 c7 c8 5e 10 8a 902 0000450 8f 61 ed 8c ba 30 1f 79 9a c7 60 34 2b b9 8c f8 903 0000460 18 a6 83 1b e3 9f ad 79 fe fd 1b 8b f1 fc 41 6f 904 0000470 d4 13 1f e3 b8 83 ba 64 92 e7 eb e4 77 05 8f ba 905 0000480 fa 3b 00 00 ff ff 50 4b 07 08 a6 18 b1 91 5e 04 906 0000490 00 00 e4 47 00 00 50 4b 01 02 14 00 14 00 08 00 907 00004a0 08 00 00 00 00 00 a6 18 b1 91 5e 04 00 00 e4 47 908 00004b0 00 00 0a 00 00 00 00 00 00 00 00 00 00 00 00 00 909 00004c0 00 00 00 00 62 69 67 67 65 72 2e 7a 69 70 50 4b 910 00004d0 05 06 00 00 00 00 01 00 01 00 38 00 00 00 96 04 911 00004e0 00 00 00 00` 912 s = regexp.MustCompile(`[0-9a-f]{7}`).ReplaceAllString(s, "") 913 s = regexp.MustCompile(`\s+`).ReplaceAllString(s, "") 914 b, err := hex.DecodeString(s) 915 if err != nil { 916 panic(err) 917 } 918 return b 919 } 920 921 func returnBigZipBytes() (r io.ReaderAt, size int64) { 922 b := biggestZipBytes() 923 for i := 0; i < 2; i++ { 924 r, err := NewReader(bytes.NewReader(b), int64(len(b))) 925 if err != nil { 926 panic(err) 927 } 928 f, err := r.File[0].Open() 929 if err != nil { 930 panic(err) 931 } 932 b, err = io.ReadAll(f) 933 if err != nil { 934 panic(err) 935 } 936 } 937 return bytes.NewReader(b), int64(len(b)) 938 } 939 940 func TestIssue8186(t *testing.T) { 941 // Directory headers & data found in the TOC of a JAR file. 942 dirEnts := []string{ 943 "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", 944 "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", 945 "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", 946 "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", 947 "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", 948 "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", 949 "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", 950 "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", 951 "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", 952 } 953 for i, s := range dirEnts { 954 var f File 955 err := readDirectoryHeader(&f, strings.NewReader(s)) 956 if err != nil { 957 t.Errorf("error reading #%d: %v", i, err) 958 } 959 } 960 } 961 962 // Verify we return ErrUnexpectedEOF when length is short. 963 func TestIssue10957(t *testing.T) { 964 data := []byte("PK\x03\x040000000PK\x01\x0200000" + 965 "0000000000000000000\x00" + 966 "\x00\x00\x00\x00\x00000000000000PK\x01" + 967 "\x020000000000000000000" + 968 "00000\v\x00\x00\x00\x00\x00000000000" + 969 "00000000000000PK\x01\x0200" + 970 "00000000000000000000" + 971 "00\v\x00\x00\x00\x00\x00000000000000" + 972 "00000000000PK\x01\x020000<" + 973 "0\x00\x0000000000000000\v\x00\v" + 974 "\x00\x00\x00\x00\x0000000000\x00\x00\x00\x00000" + 975 "00000000PK\x01\x0200000000" + 976 "0000000000000000\v\x00\x00\x00" + 977 "\x00\x0000PK\x05\x06000000\x05\x000000" + 978 "\v\x00\x00\x00\x00\x00") 979 z, err := NewReader(bytes.NewReader(data), int64(len(data))) 980 if err != nil { 981 t.Fatal(err) 982 } 983 for i, f := range z.File { 984 r, err := f.Open() 985 if err != nil { 986 continue 987 } 988 if f.UncompressedSize64 < 1e6 { 989 n, err := io.Copy(io.Discard, r) 990 if i == 3 && err != io.ErrUnexpectedEOF { 991 t.Errorf("File[3] error = %v; want io.ErrUnexpectedEOF", err) 992 } 993 if err == nil && uint64(n) != f.UncompressedSize64 { 994 t.Errorf("file %d: bad size: copied=%d; want=%d", i, n, f.UncompressedSize64) 995 } 996 } 997 r.Close() 998 } 999 } 1000 1001 // Verify that this particular malformed zip file is rejected. 1002 func TestIssue10956(t *testing.T) { 1003 data := []byte("PK\x06\x06PK\x06\a0000\x00\x00\x00\x00\x00\x00\x00\x00" + 1004 "0000PK\x05\x06000000000000" + 1005 "0000\v\x00000\x00\x00\x00\x00\x00\x00\x000") 1006 r, err := NewReader(bytes.NewReader(data), int64(len(data))) 1007 if err == nil { 1008 t.Errorf("got nil error, want ErrFormat") 1009 } 1010 if r != nil { 1011 t.Errorf("got non-nil Reader, want nil") 1012 } 1013 } 1014 1015 // Verify we return ErrUnexpectedEOF when reading truncated data descriptor. 1016 func TestIssue11146(t *testing.T) { 1017 data := []byte("PK\x03\x040000000000000000" + 1018 "000000\x01\x00\x00\x000\x01\x00\x00\xff\xff0000" + 1019 "0000000000000000PK\x01\x02" + 1020 "0000\b0\b\x00000000000000" + 1021 "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x000000PK\x05\x06\x00\x00" + 1022 "\x00\x0000\x01\x0000008\x00\x00\x00\x00\x00") 1023 z, err := NewReader(bytes.NewReader(data), int64(len(data))) 1024 if err != nil { 1025 t.Fatal(err) 1026 } 1027 r, err := z.File[0].Open() 1028 if err != nil { 1029 t.Fatal(err) 1030 } 1031 _, err = io.ReadAll(r) 1032 if err != io.ErrUnexpectedEOF { 1033 t.Errorf("File[0] error = %v; want io.ErrUnexpectedEOF", err) 1034 } 1035 r.Close() 1036 } 1037 1038 // Verify we do not treat non-zip64 archives as zip64 1039 func TestIssue12449(t *testing.T) { 1040 data := []byte{ 1041 0x50, 0x4b, 0x03, 0x04, 0x14, 0x00, 0x08, 0x00, 1042 0x00, 0x00, 0x6b, 0xb4, 0xba, 0x46, 0x00, 0x00, 1043 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1044 0x00, 0x00, 0x03, 0x00, 0x18, 0x00, 0xca, 0x64, 1045 0x55, 0x75, 0x78, 0x0b, 0x00, 0x50, 0x4b, 0x05, 1046 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 1047 0x00, 0x49, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 1048 0x00, 0x31, 0x31, 0x31, 0x32, 0x32, 0x32, 0x0a, 1049 0x50, 0x4b, 0x07, 0x08, 0x1d, 0x88, 0x77, 0xb0, 1050 0x07, 0x00, 0x00, 0x00, 0x07, 0x00, 0x00, 0x00, 1051 0x50, 0x4b, 0x01, 0x02, 0x14, 0x03, 0x14, 0x00, 1052 0x08, 0x00, 0x00, 0x00, 0x6b, 0xb4, 0xba, 0x46, 1053 0x1d, 0x88, 0x77, 0xb0, 0x07, 0x00, 0x00, 0x00, 1054 0x07, 0x00, 0x00, 0x00, 0x03, 0x00, 0x18, 0x00, 1055 0x20, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 1056 0xa0, 0x81, 0x00, 0x00, 0x00, 0x00, 0xca, 0x64, 1057 0x55, 0x75, 0x78, 0x0b, 0x00, 0x50, 0x4b, 0x05, 1058 0x06, 0x00, 0x00, 0x00, 0x00, 0x01, 0x00, 0x01, 1059 0x00, 0x49, 0x00, 0x00, 0x00, 0x44, 0x00, 0x00, 1060 0x00, 0x97, 0x2b, 0x49, 0x23, 0x05, 0xc5, 0x0b, 1061 0xa7, 0xd1, 0x52, 0xa2, 0x9c, 0x50, 0x4b, 0x06, 1062 0x07, 0xc8, 0x19, 0xc1, 0xaf, 0x94, 0x9c, 0x61, 1063 0x44, 0xbe, 0x94, 0x19, 0x42, 0x58, 0x12, 0xc6, 1064 0x5b, 0x50, 0x4b, 0x05, 0x06, 0x00, 0x00, 0x00, 1065 0x00, 0x01, 0x00, 0x01, 0x00, 0x69, 0x00, 0x00, 1066 0x00, 0x50, 0x00, 0x00, 0x00, 0x00, 0x00, 1067 } 1068 // Read in the archive. 1069 _, err := NewReader(bytes.NewReader([]byte(data)), int64(len(data))) 1070 if err != nil { 1071 t.Errorf("Error reading the archive: %v", err) 1072 } 1073 } 1074 1075 func TestFS(t *testing.T) { 1076 z, err := OpenReader("testdata/unix.zip") 1077 if err != nil { 1078 t.Fatal(err) 1079 } 1080 if err := fstest.TestFS(z, "hello", "dir/bar", "dir/empty", "readonly"); err != nil { 1081 t.Fatal(err) 1082 } 1083 }