zip_test.go 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828
  1. // Copyright 2011 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. // Tests that involve both reading and writing.
  5. package zip
  6. import (
  7. "bytes"
  8. "errors"
  9. "fmt"
  10. "hash"
  11. "io"
  12. "io/ioutil"
  13. "runtime"
  14. "sort"
  15. "strings"
  16. "testing"
  17. "time"
  18. )
  19. func TestOver65kFiles(t *testing.T) {
  20. if testing.Short() {
  21. t.Skip("skipping in short mode")
  22. }
  23. buf := new(bytes.Buffer)
  24. w := NewWriter(buf)
  25. const nFiles = (1 << 16) + 42
  26. for i := 0; i < nFiles; i++ {
  27. _, err := w.CreateHeader(&FileHeader{
  28. Name: fmt.Sprintf("%d.dat", i),
  29. Method: Store, // avoid Issue 6136 and Issue 6138
  30. })
  31. if err != nil {
  32. t.Fatalf("creating file %d: %v", i, err)
  33. }
  34. }
  35. if err := w.Close(); err != nil {
  36. t.Fatalf("Writer.Close: %v", err)
  37. }
  38. s := buf.String()
  39. zr, err := NewReader(strings.NewReader(s), int64(len(s)))
  40. if err != nil {
  41. t.Fatalf("NewReader: %v", err)
  42. }
  43. if got := len(zr.File); got != nFiles {
  44. t.Fatalf("File contains %d files, want %d", got, nFiles)
  45. }
  46. for i := 0; i < nFiles; i++ {
  47. want := fmt.Sprintf("%d.dat", i)
  48. if zr.File[i].Name != want {
  49. t.Fatalf("File(%d) = %q, want %q", i, zr.File[i].Name, want)
  50. }
  51. }
  52. }
  53. func TestModTime(t *testing.T) {
  54. var testTime = time.Date(2009, time.November, 10, 23, 45, 58, 0, time.UTC)
  55. fh := new(FileHeader)
  56. fh.SetModTime(testTime)
  57. outTime := fh.ModTime()
  58. if !outTime.Equal(testTime) {
  59. t.Errorf("times don't match: got %s, want %s", outTime, testTime)
  60. }
  61. }
  62. func testHeaderRoundTrip(fh *FileHeader, wantUncompressedSize uint32, wantUncompressedSize64 uint64, t *testing.T) {
  63. fi := fh.FileInfo()
  64. fh2, err := FileInfoHeader(fi)
  65. if err != nil {
  66. t.Fatal(err)
  67. }
  68. if got, want := fh2.Name, fh.Name; got != want {
  69. t.Errorf("Name: got %s, want %s\n", got, want)
  70. }
  71. if got, want := fh2.UncompressedSize, wantUncompressedSize; got != want {
  72. t.Errorf("UncompressedSize: got %d, want %d\n", got, want)
  73. }
  74. if got, want := fh2.UncompressedSize64, wantUncompressedSize64; got != want {
  75. t.Errorf("UncompressedSize64: got %d, want %d\n", got, want)
  76. }
  77. if got, want := fh2.ModifiedTime, fh.ModifiedTime; got != want {
  78. t.Errorf("ModifiedTime: got %d, want %d\n", got, want)
  79. }
  80. if got, want := fh2.ModifiedDate, fh.ModifiedDate; got != want {
  81. t.Errorf("ModifiedDate: got %d, want %d\n", got, want)
  82. }
  83. if sysfh, ok := fi.Sys().(*FileHeader); !ok && sysfh != fh {
  84. t.Errorf("Sys didn't return original *FileHeader")
  85. }
  86. }
  87. func TestFileHeaderRoundTrip(t *testing.T) {
  88. fh := &FileHeader{
  89. Name: "foo.txt",
  90. UncompressedSize: 987654321,
  91. ModifiedTime: 1234,
  92. ModifiedDate: 5678,
  93. }
  94. testHeaderRoundTrip(fh, fh.UncompressedSize, uint64(fh.UncompressedSize), t)
  95. }
  96. func TestFileHeaderRoundTrip64(t *testing.T) {
  97. fh := &FileHeader{
  98. Name: "foo.txt",
  99. UncompressedSize64: 9876543210,
  100. ModifiedTime: 1234,
  101. ModifiedDate: 5678,
  102. }
  103. testHeaderRoundTrip(fh, uint32max, fh.UncompressedSize64, t)
  104. }
  105. func TestFileHeaderRoundTripModified(t *testing.T) {
  106. fh := &FileHeader{
  107. Name: "foo.txt",
  108. UncompressedSize: 987654321,
  109. Modified: time.Now().Local(),
  110. ModifiedTime: 1234,
  111. ModifiedDate: 5678,
  112. }
  113. fi := fh.FileInfo()
  114. fh2, err := FileInfoHeader(fi)
  115. if err != nil {
  116. t.Fatal(err)
  117. }
  118. if got, want := fh2.Modified, fh.Modified.UTC(); got != want {
  119. t.Errorf("Modified: got %s, want %s\n", got, want)
  120. }
  121. if got, want := fi.ModTime(), fh.Modified.UTC(); got != want {
  122. t.Errorf("Modified: got %s, want %s\n", got, want)
  123. }
  124. }
  125. func TestFileHeaderRoundTripWithoutModified(t *testing.T) {
  126. fh := &FileHeader{
  127. Name: "foo.txt",
  128. UncompressedSize: 987654321,
  129. ModifiedTime: 1234,
  130. ModifiedDate: 5678,
  131. }
  132. fi := fh.FileInfo()
  133. fh2, err := FileInfoHeader(fi)
  134. if err != nil {
  135. t.Fatal(err)
  136. }
  137. if got, want := fh2.ModTime(), fh.ModTime(); got != want {
  138. t.Errorf("Modified: got %s, want %s\n", got, want)
  139. }
  140. if got, want := fi.ModTime(), fh.ModTime(); got != want {
  141. t.Errorf("Modified: got %s, want %s\n", got, want)
  142. }
  143. }
  144. type repeatedByte struct {
  145. off int64
  146. b byte
  147. n int64
  148. }
  149. // rleBuffer is a run-length-encoded byte buffer.
  150. // It's an io.Writer (like a bytes.Buffer) and also an io.ReaderAt,
  151. // allowing random-access reads.
  152. type rleBuffer struct {
  153. buf []repeatedByte
  154. }
  155. func (r *rleBuffer) Size() int64 {
  156. if len(r.buf) == 0 {
  157. return 0
  158. }
  159. last := &r.buf[len(r.buf)-1]
  160. return last.off + last.n
  161. }
  162. func (r *rleBuffer) Write(p []byte) (n int, err error) {
  163. var rp *repeatedByte
  164. if len(r.buf) > 0 {
  165. rp = &r.buf[len(r.buf)-1]
  166. // Fast path, if p is entirely the same byte repeated.
  167. if lastByte := rp.b; len(p) > 0 && p[0] == lastByte {
  168. if bytes.Count(p, []byte{lastByte}) == len(p) {
  169. rp.n += int64(len(p))
  170. return len(p), nil
  171. }
  172. }
  173. }
  174. for _, b := range p {
  175. if rp == nil || rp.b != b {
  176. r.buf = append(r.buf, repeatedByte{r.Size(), b, 1})
  177. rp = &r.buf[len(r.buf)-1]
  178. } else {
  179. rp.n++
  180. }
  181. }
  182. return len(p), nil
  183. }
  184. func min(x, y int64) int64 {
  185. if x < y {
  186. return x
  187. }
  188. return y
  189. }
  190. func memset(a []byte, b byte) {
  191. if len(a) == 0 {
  192. return
  193. }
  194. // Double, until we reach power of 2 >= len(a), same as bytes.Repeat,
  195. // but without allocation.
  196. a[0] = b
  197. for i, l := 1, len(a); i < l; i *= 2 {
  198. copy(a[i:], a[:i])
  199. }
  200. }
  201. func (r *rleBuffer) ReadAt(p []byte, off int64) (n int, err error) {
  202. if len(p) == 0 {
  203. return
  204. }
  205. skipParts := sort.Search(len(r.buf), func(i int) bool {
  206. part := &r.buf[i]
  207. return part.off+part.n > off
  208. })
  209. parts := r.buf[skipParts:]
  210. if len(parts) > 0 {
  211. skipBytes := off - parts[0].off
  212. for _, part := range parts {
  213. repeat := int(min(part.n-skipBytes, int64(len(p)-n)))
  214. memset(p[n:n+repeat], part.b)
  215. n += repeat
  216. if n == len(p) {
  217. return
  218. }
  219. skipBytes = 0
  220. }
  221. }
  222. if n != len(p) {
  223. err = io.ErrUnexpectedEOF
  224. }
  225. return
  226. }
  227. // Just testing the rleBuffer used in the Zip64 test above. Not used by the zip code.
  228. func TestRLEBuffer(t *testing.T) {
  229. b := new(rleBuffer)
  230. var all []byte
  231. writes := []string{"abcdeee", "eeeeeee", "eeeefghaaiii"}
  232. for _, w := range writes {
  233. b.Write([]byte(w))
  234. all = append(all, w...)
  235. }
  236. if len(b.buf) != 10 {
  237. t.Fatalf("len(b.buf) = %d; want 10", len(b.buf))
  238. }
  239. for i := 0; i < len(all); i++ {
  240. for j := 0; j < len(all)-i; j++ {
  241. buf := make([]byte, j)
  242. n, err := b.ReadAt(buf, int64(i))
  243. if err != nil || n != len(buf) {
  244. t.Errorf("ReadAt(%d, %d) = %d, %v; want %d, nil", i, j, n, err, len(buf))
  245. }
  246. if !bytes.Equal(buf, all[i:i+j]) {
  247. t.Errorf("ReadAt(%d, %d) = %q; want %q", i, j, buf, all[i:i+j])
  248. }
  249. }
  250. }
  251. }
  252. // fakeHash32 is a dummy Hash32 that always returns 0.
  253. type fakeHash32 struct {
  254. hash.Hash32
  255. }
  256. func (fakeHash32) Write(p []byte) (int, error) { return len(p), nil }
  257. func (fakeHash32) Sum32() uint32 { return 0 }
  258. func TestZip64(t *testing.T) {
  259. if testing.Short() {
  260. t.Skip("slow test; skipping")
  261. }
  262. t.Parallel()
  263. const size = 1 << 32 // before the "END\n" part
  264. buf := testZip64(t, size)
  265. testZip64DirectoryRecordLength(buf, t)
  266. }
  267. func TestZip64EdgeCase(t *testing.T) {
  268. if testing.Short() {
  269. t.Skip("slow test; skipping")
  270. }
  271. t.Parallel()
  272. // Test a zip file with uncompressed size 0xFFFFFFFF.
  273. // That's the magic marker for a 64-bit file, so even though
  274. // it fits in a 32-bit field we must use the 64-bit field.
  275. // Go 1.5 and earlier got this wrong,
  276. // writing an invalid zip file.
  277. const size = 1<<32 - 1 - int64(len("END\n")) // before the "END\n" part
  278. buf := testZip64(t, size)
  279. testZip64DirectoryRecordLength(buf, t)
  280. }
  281. // Tests that we generate a zip64 file if the directory at offset
  282. // 0xFFFFFFFF, but not before.
  283. func TestZip64DirectoryOffset(t *testing.T) {
  284. if testing.Short() {
  285. t.Skip("skipping in short mode")
  286. }
  287. t.Parallel()
  288. const filename = "huge.txt"
  289. gen := func(wantOff uint64) func(*Writer) {
  290. return func(w *Writer) {
  291. w.testHookCloseSizeOffset = func(size, off uint64) {
  292. if off != wantOff {
  293. t.Errorf("central directory offset = %d (%x); want %d", off, off, wantOff)
  294. }
  295. }
  296. f, err := w.CreateHeader(&FileHeader{
  297. Name: filename,
  298. Method: Store,
  299. })
  300. if err != nil {
  301. t.Fatal(err)
  302. }
  303. f.(*fileWriter).crc32 = fakeHash32{}
  304. size := wantOff - fileHeaderLen - uint64(len(filename)) - dataDescriptorLen
  305. if _, err := io.CopyN(f, zeros{}, int64(size)); err != nil {
  306. t.Fatal(err)
  307. }
  308. if err := w.Close(); err != nil {
  309. t.Fatal(err)
  310. }
  311. }
  312. }
  313. t.Run("uint32max-2_NoZip64", func(t *testing.T) {
  314. t.Parallel()
  315. if generatesZip64(t, gen(0xfffffffe)) {
  316. t.Error("unexpected zip64")
  317. }
  318. })
  319. t.Run("uint32max-1_Zip64", func(t *testing.T) {
  320. t.Parallel()
  321. if !generatesZip64(t, gen(0xffffffff)) {
  322. t.Error("expected zip64")
  323. }
  324. })
  325. }
  326. // At 16k records, we need to generate a zip64 file.
  327. func TestZip64ManyRecords(t *testing.T) {
  328. if testing.Short() {
  329. t.Skip("skipping in short mode")
  330. }
  331. t.Parallel()
  332. gen := func(numRec int) func(*Writer) {
  333. return func(w *Writer) {
  334. for i := 0; i < numRec; i++ {
  335. _, err := w.CreateHeader(&FileHeader{
  336. Name: "a.txt",
  337. Method: Store,
  338. })
  339. if err != nil {
  340. t.Fatal(err)
  341. }
  342. }
  343. if err := w.Close(); err != nil {
  344. t.Fatal(err)
  345. }
  346. }
  347. }
  348. // 16k-1 records shouldn't make a zip64:
  349. t.Run("uint16max-1_NoZip64", func(t *testing.T) {
  350. t.Parallel()
  351. if generatesZip64(t, gen(0xfffe)) {
  352. t.Error("unexpected zip64")
  353. }
  354. })
  355. // 16k records should make a zip64:
  356. t.Run("uint16max_Zip64", func(t *testing.T) {
  357. t.Parallel()
  358. if !generatesZip64(t, gen(0xffff)) {
  359. t.Error("expected zip64")
  360. }
  361. })
  362. }
  363. // suffixSaver is an io.Writer & io.ReaderAt that remembers the last 0
  364. // to 'keep' bytes of data written to it. Call Suffix to get the
  365. // suffix bytes.
  366. type suffixSaver struct {
  367. keep int
  368. buf []byte
  369. start int
  370. size int64
  371. }
  372. func (ss *suffixSaver) Size() int64 { return ss.size }
  373. var errDiscardedBytes = errors.New("ReadAt of discarded bytes")
  374. func (ss *suffixSaver) ReadAt(p []byte, off int64) (n int, err error) {
  375. back := ss.size - off
  376. if back > int64(ss.keep) {
  377. return 0, errDiscardedBytes
  378. }
  379. suf := ss.Suffix()
  380. n = copy(p, suf[len(suf)-int(back):])
  381. if n != len(p) {
  382. err = io.EOF
  383. }
  384. return
  385. }
  386. func (ss *suffixSaver) Suffix() []byte {
  387. if len(ss.buf) < ss.keep {
  388. return ss.buf
  389. }
  390. buf := make([]byte, ss.keep)
  391. n := copy(buf, ss.buf[ss.start:])
  392. copy(buf[n:], ss.buf[:])
  393. return buf
  394. }
  395. func (ss *suffixSaver) Write(p []byte) (n int, err error) {
  396. n = len(p)
  397. ss.size += int64(len(p))
  398. if len(ss.buf) < ss.keep {
  399. space := ss.keep - len(ss.buf)
  400. add := len(p)
  401. if add > space {
  402. add = space
  403. }
  404. ss.buf = append(ss.buf, p[:add]...)
  405. p = p[add:]
  406. }
  407. for len(p) > 0 {
  408. n := copy(ss.buf[ss.start:], p)
  409. p = p[n:]
  410. ss.start += n
  411. if ss.start == ss.keep {
  412. ss.start = 0
  413. }
  414. }
  415. return
  416. }
  417. // generatesZip64 reports whether f wrote a zip64 file.
  418. // f is also responsible for closing w.
  419. func generatesZip64(t *testing.T, f func(w *Writer)) bool {
  420. ss := &suffixSaver{keep: 10 << 20}
  421. w := NewWriter(ss)
  422. f(w)
  423. return suffixIsZip64(t, ss)
  424. }
  425. type sizedReaderAt interface {
  426. io.ReaderAt
  427. Size() int64
  428. }
  429. func suffixIsZip64(t *testing.T, zip sizedReaderAt) bool {
  430. d := make([]byte, 1024)
  431. if _, err := zip.ReadAt(d, zip.Size()-int64(len(d))); err != nil {
  432. t.Fatalf("ReadAt: %v", err)
  433. }
  434. sigOff := findSignatureInBlock(d)
  435. if sigOff == -1 {
  436. t.Errorf("failed to find signature in block")
  437. return false
  438. }
  439. dirOff, err := findDirectory64End(zip, zip.Size()-int64(len(d))+int64(sigOff))
  440. if err != nil {
  441. t.Fatalf("findDirectory64End: %v", err)
  442. }
  443. if dirOff == -1 {
  444. return false
  445. }
  446. d = make([]byte, directory64EndLen)
  447. if _, err := zip.ReadAt(d, dirOff); err != nil {
  448. t.Fatalf("ReadAt(off=%d): %v", dirOff, err)
  449. }
  450. b := readBuf(d)
  451. if sig := b.uint32(); sig != directory64EndSignature {
  452. return false
  453. }
  454. size := b.uint64()
  455. if size != directory64EndLen-12 {
  456. t.Errorf("expected length of %d, got %d", directory64EndLen-12, size)
  457. }
  458. return true
  459. }
  460. // Zip64 is required if the total size of the records is uint32max.
  461. func TestZip64LargeDirectory(t *testing.T) {
  462. if runtime.GOARCH == "wasm" {
  463. t.Skip("too slow on wasm")
  464. }
  465. if testing.Short() {
  466. t.Skip("skipping in short mode")
  467. }
  468. t.Parallel()
  469. // gen returns a func that writes a zip with a wantLen bytes
  470. // of central directory.
  471. gen := func(wantLen int64) func(*Writer) {
  472. return func(w *Writer) {
  473. w.testHookCloseSizeOffset = func(size, off uint64) {
  474. if size != uint64(wantLen) {
  475. t.Errorf("Close central directory size = %d; want %d", size, wantLen)
  476. }
  477. }
  478. uint16string := strings.Repeat(".", uint16max)
  479. remain := wantLen
  480. for remain > 0 {
  481. commentLen := int(uint16max) - directoryHeaderLen - 1
  482. thisRecLen := directoryHeaderLen + int(uint16max) + commentLen
  483. if int64(thisRecLen) > remain {
  484. remove := thisRecLen - int(remain)
  485. commentLen -= remove
  486. thisRecLen -= remove
  487. }
  488. remain -= int64(thisRecLen)
  489. f, err := w.CreateHeader(&FileHeader{
  490. Name: uint16string,
  491. Comment: uint16string[:commentLen],
  492. })
  493. if err != nil {
  494. t.Fatalf("CreateHeader: %v", err)
  495. }
  496. f.(*fileWriter).crc32 = fakeHash32{}
  497. }
  498. if err := w.Close(); err != nil {
  499. t.Fatalf("Close: %v", err)
  500. }
  501. }
  502. }
  503. t.Run("uint32max-1_NoZip64", func(t *testing.T) {
  504. t.Parallel()
  505. if generatesZip64(t, gen(uint32max-1)) {
  506. t.Error("unexpected zip64")
  507. }
  508. })
  509. t.Run("uint32max_HasZip64", func(t *testing.T) {
  510. t.Parallel()
  511. if !generatesZip64(t, gen(uint32max)) {
  512. t.Error("expected zip64")
  513. }
  514. })
  515. }
  516. func testZip64(t testing.TB, size int64) *rleBuffer {
  517. const chunkSize = 1024
  518. chunks := int(size / chunkSize)
  519. // write size bytes plus "END\n" to a zip file
  520. buf := new(rleBuffer)
  521. w := NewWriter(buf)
  522. f, err := w.CreateHeader(&FileHeader{
  523. Name: "huge.txt",
  524. Method: Store,
  525. })
  526. if err != nil {
  527. t.Fatal(err)
  528. }
  529. f.(*fileWriter).crc32 = fakeHash32{}
  530. chunk := make([]byte, chunkSize)
  531. for i := range chunk {
  532. chunk[i] = '.'
  533. }
  534. for i := 0; i < chunks; i++ {
  535. _, err := f.Write(chunk)
  536. if err != nil {
  537. t.Fatal("write chunk:", err)
  538. }
  539. }
  540. if frag := int(size % chunkSize); frag > 0 {
  541. _, err := f.Write(chunk[:frag])
  542. if err != nil {
  543. t.Fatal("write chunk:", err)
  544. }
  545. }
  546. end := []byte("END\n")
  547. _, err = f.Write(end)
  548. if err != nil {
  549. t.Fatal("write end:", err)
  550. }
  551. if err := w.Close(); err != nil {
  552. t.Fatal(err)
  553. }
  554. // read back zip file and check that we get to the end of it
  555. r, err := NewReader(buf, int64(buf.Size()))
  556. if err != nil {
  557. t.Fatal("reader:", err)
  558. }
  559. f0 := r.File[0]
  560. rc, err := f0.Open()
  561. if err != nil {
  562. t.Fatal("opening:", err)
  563. }
  564. rc.(*checksumReader).hash = fakeHash32{}
  565. for i := 0; i < chunks; i++ {
  566. _, err := io.ReadFull(rc, chunk)
  567. if err != nil {
  568. t.Fatal("read:", err)
  569. }
  570. }
  571. if frag := int(size % chunkSize); frag > 0 {
  572. _, err := io.ReadFull(rc, chunk[:frag])
  573. if err != nil {
  574. t.Fatal("read:", err)
  575. }
  576. }
  577. gotEnd, err := ioutil.ReadAll(rc)
  578. if err != nil {
  579. t.Fatal("read end:", err)
  580. }
  581. if !bytes.Equal(gotEnd, end) {
  582. t.Errorf("End of zip64 archive %q, want %q", gotEnd, end)
  583. }
  584. err = rc.Close()
  585. if err != nil {
  586. t.Fatal("closing:", err)
  587. }
  588. if size+int64(len("END\n")) >= 1<<32-1 {
  589. if got, want := f0.UncompressedSize, uint32(uint32max); got != want {
  590. t.Errorf("UncompressedSize %#x, want %#x", got, want)
  591. }
  592. }
  593. if got, want := f0.UncompressedSize64, uint64(size)+uint64(len(end)); got != want {
  594. t.Errorf("UncompressedSize64 %#x, want %#x", got, want)
  595. }
  596. return buf
  597. }
  598. // Issue 9857
  599. func testZip64DirectoryRecordLength(buf *rleBuffer, t *testing.T) {
  600. if !suffixIsZip64(t, buf) {
  601. t.Fatal("not a zip64")
  602. }
  603. }
  604. func testValidHeader(h *FileHeader, t *testing.T) {
  605. var buf bytes.Buffer
  606. z := NewWriter(&buf)
  607. f, err := z.CreateHeader(h)
  608. if err != nil {
  609. t.Fatalf("error creating header: %v", err)
  610. }
  611. if _, err := f.Write([]byte("hi")); err != nil {
  612. t.Fatalf("error writing content: %v", err)
  613. }
  614. if err := z.Close(); err != nil {
  615. t.Fatalf("error closing zip writer: %v", err)
  616. }
  617. b := buf.Bytes()
  618. zf, err := NewReader(bytes.NewReader(b), int64(len(b)))
  619. if err != nil {
  620. t.Fatalf("got %v, expected nil", err)
  621. }
  622. zh := zf.File[0].FileHeader
  623. if zh.Name != h.Name || zh.Method != h.Method || zh.UncompressedSize64 != uint64(len("hi")) {
  624. t.Fatalf("got %q/%d/%d expected %q/%d/%d", zh.Name, zh.Method, zh.UncompressedSize64, h.Name, h.Method, len("hi"))
  625. }
  626. }
  627. // Issue 4302.
  628. func TestHeaderInvalidTagAndSize(t *testing.T) {
  629. const timeFormat = "20060102T150405.000.txt"
  630. ts := time.Now()
  631. filename := ts.Format(timeFormat)
  632. h := FileHeader{
  633. Name: filename,
  634. Method: Deflate,
  635. Extra: []byte(ts.Format(time.RFC3339Nano)), // missing tag and len, but Extra is best-effort parsing
  636. }
  637. h.SetModTime(ts)
  638. testValidHeader(&h, t)
  639. }
  640. func TestHeaderTooShort(t *testing.T) {
  641. h := FileHeader{
  642. Name: "foo.txt",
  643. Method: Deflate,
  644. Extra: []byte{zip64ExtraID}, // missing size and second half of tag, but Extra is best-effort parsing
  645. }
  646. testValidHeader(&h, t)
  647. }
  648. func TestHeaderTooLongErr(t *testing.T) {
  649. var headerTests = []struct {
  650. name string
  651. extra []byte
  652. wanterr error
  653. }{
  654. {
  655. name: strings.Repeat("x", 1<<16),
  656. extra: []byte{},
  657. wanterr: errLongName,
  658. },
  659. {
  660. name: "long_extra",
  661. extra: bytes.Repeat([]byte{0xff}, 1<<16),
  662. wanterr: errLongExtra,
  663. },
  664. }
  665. // write a zip file
  666. buf := new(bytes.Buffer)
  667. w := NewWriter(buf)
  668. for _, test := range headerTests {
  669. h := &FileHeader{
  670. Name: test.name,
  671. Extra: test.extra,
  672. }
  673. _, err := w.CreateHeader(h)
  674. if err != test.wanterr {
  675. t.Errorf("error=%v, want %v", err, test.wanterr)
  676. }
  677. }
  678. if err := w.Close(); err != nil {
  679. t.Fatal(err)
  680. }
  681. }
  682. func TestHeaderIgnoredSize(t *testing.T) {
  683. h := FileHeader{
  684. Name: "foo.txt",
  685. Method: Deflate,
  686. Extra: []byte{zip64ExtraID & 0xFF, zip64ExtraID >> 8, 24, 0, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8, 1, 2, 3, 4, 5, 6, 7, 8}, // bad size but shouldn't be consulted
  687. }
  688. testValidHeader(&h, t)
  689. }
  690. // Issue 4393. It is valid to have an extra data header
  691. // which contains no body.
  692. func TestZeroLengthHeader(t *testing.T) {
  693. h := FileHeader{
  694. Name: "extadata.txt",
  695. Method: Deflate,
  696. Extra: []byte{
  697. 85, 84, 5, 0, 3, 154, 144, 195, 77, // tag 21589 size 5
  698. 85, 120, 0, 0, // tag 30805 size 0
  699. },
  700. }
  701. testValidHeader(&h, t)
  702. }
  703. // Just benchmarking how fast the Zip64 test above is. Not related to
  704. // our zip performance, since the test above disabled CRC32 and flate.
  705. func BenchmarkZip64Test(b *testing.B) {
  706. for i := 0; i < b.N; i++ {
  707. testZip64(b, 1<<26)
  708. }
  709. }
  710. func BenchmarkZip64TestSizes(b *testing.B) {
  711. for _, size := range []int64{1 << 12, 1 << 20, 1 << 26} {
  712. b.Run(fmt.Sprint(size), func(b *testing.B) {
  713. b.RunParallel(func(pb *testing.PB) {
  714. for pb.Next() {
  715. testZip64(b, size)
  716. }
  717. })
  718. })
  719. }
  720. }
  721. func TestSuffixSaver(t *testing.T) {
  722. const keep = 10
  723. ss := &suffixSaver{keep: keep}
  724. ss.Write([]byte("abc"))
  725. if got := string(ss.Suffix()); got != "abc" {
  726. t.Errorf("got = %q; want abc", got)
  727. }
  728. ss.Write([]byte("defghijklmno"))
  729. if got := string(ss.Suffix()); got != "fghijklmno" {
  730. t.Errorf("got = %q; want fghijklmno", got)
  731. }
  732. if got, want := ss.Size(), int64(len("abc")+len("defghijklmno")); got != want {
  733. t.Errorf("Size = %d; want %d", got, want)
  734. }
  735. buf := make([]byte, ss.Size())
  736. for off := int64(0); off < ss.Size(); off++ {
  737. for size := 1; size <= int(ss.Size()-off); size++ {
  738. readBuf := buf[:size]
  739. n, err := ss.ReadAt(readBuf, off)
  740. if off < ss.Size()-keep {
  741. if err != errDiscardedBytes {
  742. t.Errorf("off %d, size %d = %v, %v (%q); want errDiscardedBytes", off, size, n, err, readBuf[:n])
  743. }
  744. continue
  745. }
  746. want := "abcdefghijklmno"[off : off+int64(size)]
  747. got := string(readBuf[:n])
  748. if err != nil || got != want {
  749. t.Errorf("off %d, size %d = %v, %v (%q); want %q", off, size, n, err, got, want)
  750. }
  751. }
  752. }
  753. }
  754. type zeros struct{}
  755. func (zeros) Read(p []byte) (int, error) {
  756. for i := range p {
  757. p[i] = 0
  758. }
  759. return len(p), nil
  760. }