reader_test.go 26 KB

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