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