...
Run Format

Source file src/archive/zip/zip_test.go

Documentation: archive/zip

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

View as plain text