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