reader_test.go 31 KB

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