...

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

View as plain text