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
7package zip
8
9import (
10	"bytes"
11	"fmt"
12	"hash"
13	"io"
14	"io/ioutil"
15	"sort"
16	"strings"
17	"testing"
18	"time"
19)
20
21func TestOver65kFiles(t *testing.T) {
22	if testing.Short() {
23		t.Skip("skipping in short mode")
24	}
25	buf := new(bytes.Buffer)
26	w := NewWriter(buf)
27	const nFiles = (1 << 16) + 42
28	for i := 0; i < nFiles; i++ {
29		_, err := w.CreateHeader(&FileHeader{
30			Name:   fmt.Sprintf("%d.dat", i),
31			Method: Store, // avoid Issue 6136 and Issue 6138
32		})
33		if err != nil {
34			t.Fatalf("creating file %d: %v", i, err)
35		}
36	}
37	if err := w.Close(); err != nil {
38		t.Fatalf("Writer.Close: %v", err)
39	}
40	s := buf.String()
41	zr, err := NewReader(strings.NewReader(s), int64(len(s)))
42	if err != nil {
43		t.Fatalf("NewReader: %v", err)
44	}
45	if got := len(zr.File); got != nFiles {
46		t.Fatalf("File contains %d files, want %d", got, nFiles)
47	}
48	for i := 0; i < nFiles; i++ {
49		want := fmt.Sprintf("%d.dat", i)
50		if zr.File[i].Name != want {
51			t.Fatalf("File(%d) = %q, want %q", i, zr.File[i].Name, want)
52		}
53	}
54}
55
56func TestModTime(t *testing.T) {
57	var testTime = time.Date(2009, time.November, 10, 23, 45, 58, 0, time.UTC)
58	fh := new(FileHeader)
59	fh.SetModTime(testTime)
60	outTime := fh.ModTime()
61	if !outTime.Equal(testTime) {
62		t.Errorf("times don't match: got %s, want %s", outTime, testTime)
63	}
64}
65
66func testHeaderRoundTrip(fh *FileHeader, wantUncompressedSize uint32, wantUncompressedSize64 uint64, t *testing.T) {
67	fi := fh.FileInfo()
68	fh2, err := FileInfoHeader(fi)
69	if err != nil {
70		t.Fatal(err)
71	}
72	if got, want := fh2.Name, fh.Name; got != want {
73		t.Errorf("Name: got %s, want %s\n", got, want)
74	}
75	if got, want := fh2.UncompressedSize, wantUncompressedSize; got != want {
76		t.Errorf("UncompressedSize: got %d, want %d\n", got, want)
77	}
78	if got, want := fh2.UncompressedSize64, wantUncompressedSize64; got != want {
79		t.Errorf("UncompressedSize64: got %d, want %d\n", got, want)
80	}
81	if got, want := fh2.ModifiedTime, fh.ModifiedTime; got != want {
82		t.Errorf("ModifiedTime: got %d, want %d\n", got, want)
83	}
84	if got, want := fh2.ModifiedDate, fh.ModifiedDate; got != want {
85		t.Errorf("ModifiedDate: got %d, want %d\n", got, want)
86	}
87
88	if sysfh, ok := fi.Sys().(*FileHeader); !ok && sysfh != fh {
89		t.Errorf("Sys didn't return original *FileHeader")
90	}
91}
92
93func TestFileHeaderRoundTrip(t *testing.T) {
94	fh := &FileHeader{
95		Name:             "foo.txt",
96		UncompressedSize: 987654321,
97		ModifiedTime:     1234,
98		ModifiedDate:     5678,
99	}
100	testHeaderRoundTrip(fh, fh.UncompressedSize, uint64(fh.UncompressedSize), t)
101}
102
103func TestFileHeaderRoundTrip64(t *testing.T) {
104	fh := &FileHeader{
105		Name:               "foo.txt",
106		UncompressedSize64: 9876543210,
107		ModifiedTime:       1234,
108		ModifiedDate:       5678,
109	}
110	testHeaderRoundTrip(fh, uint32max, fh.UncompressedSize64, t)
111}
112
113type repeatedByte struct {
114	off int64
115	b   byte
116	n   int64
117}
118
119// rleBuffer is a run-length-encoded byte buffer.
120// It's an io.Writer (like a bytes.Buffer) and also an io.ReaderAt,
121// allowing random-access reads.
122type rleBuffer struct {
123	buf []repeatedByte
124}
125
126func (r *rleBuffer) Size() int64 {
127	if len(r.buf) == 0 {
128		return 0
129	}
130	last := &r.buf[len(r.buf)-1]
131	return last.off + last.n
132}
133
134func (r *rleBuffer) Write(p []byte) (n int, err error) {
135	var rp *repeatedByte
136	if len(r.buf) > 0 {
137		rp = &r.buf[len(r.buf)-1]
138		// Fast path, if p is entirely the same byte repeated.
139		if lastByte := rp.b; len(p) > 0 && p[0] == lastByte {
140			all := true
141			for _, b := range p {
142				if b != lastByte {
143					all = false
144					break
145				}
146			}
147			if all {
148				rp.n += int64(len(p))
149				return len(p), nil
150			}
151		}
152	}
153
154	for _, b := range p {
155		if rp == nil || rp.b != b {
156			r.buf = append(r.buf, repeatedByte{r.Size(), b, 1})
157			rp = &r.buf[len(r.buf)-1]
158		} else {
159			rp.n++
160		}
161	}
162	return len(p), nil
163}
164
165func (r *rleBuffer) ReadAt(p []byte, off int64) (n int, err error) {
166	if len(p) == 0 {
167		return
168	}
169	skipParts := sort.Search(len(r.buf), func(i int) bool {
170		part := &r.buf[i]
171		return part.off+part.n > off
172	})
173	parts := r.buf[skipParts:]
174	if len(parts) > 0 {
175		skipBytes := off - parts[0].off
176		for len(parts) > 0 {
177			part := parts[0]
178			for i := skipBytes; i < part.n; i++ {
179				if n == len(p) {
180					return
181				}
182				p[n] = part.b
183				n++
184			}
185			parts = parts[1:]
186			skipBytes = 0
187		}
188	}
189	if n != len(p) {
190		err = io.ErrUnexpectedEOF
191	}
192	return
193}
194
195// Just testing the rleBuffer used in the Zip64 test above. Not used by the zip code.
196func TestRLEBuffer(t *testing.T) {
197	b := new(rleBuffer)
198	var all []byte
199	writes := []string{"abcdeee", "eeeeeee", "eeeefghaaiii"}
200	for _, w := range writes {
201		b.Write([]byte(w))
202		all = append(all, w...)
203	}
204	if len(b.buf) != 10 {
205		t.Fatalf("len(b.buf) = %d; want 10", len(b.buf))
206	}
207
208	for i := 0; i < len(all); i++ {
209		for j := 0; j < len(all)-i; j++ {
210			buf := make([]byte, j)
211			n, err := b.ReadAt(buf, int64(i))
212			if err != nil || n != len(buf) {
213				t.Errorf("ReadAt(%d, %d) = %d, %v; want %d, nil", i, j, n, err, len(buf))
214			}
215			if !bytes.Equal(buf, all[i:i+j]) {
216				t.Errorf("ReadAt(%d, %d) = %q; want %q", i, j, buf, all[i:i+j])
217			}
218		}
219	}
220}
221
222// fakeHash32 is a fake Hash32 that always returns 0.
223type fakeHash32 struct {
224	hash.Hash32
225}
226
227func (fakeHash32) Write(p []byte) (int, error) { return len(p), nil }
228func (fakeHash32) Sum32() uint32               { return 0 }
229
230func TestZip64(t *testing.T) {
231	if testing.Short() {
232		t.Skip("slow test; skipping")
233	}
234	const size = 1 << 32 // before the "END\n" part
235	buf := testZip64(t, size)
236	testZip64DirectoryRecordLength(buf, t)
237}
238
239func TestZip64EdgeCase(t *testing.T) {
240	if testing.Short() {
241		t.Skip("slow test; skipping")
242	}
243	// Test a zip file with uncompressed size 0xFFFFFFFF.
244	// That's the magic marker for a 64-bit file, so even though
245	// it fits in a 32-bit field we must use the 64-bit field.
246	// Go 1.5 and earlier got this wrong,
247	// writing an invalid zip file.
248	const size = 1<<32 - 1 - int64(len("END\n")) // before the "END\n" part
249	buf := testZip64(t, size)
250	testZip64DirectoryRecordLength(buf, t)
251}
252
253func testZip64(t testing.TB, size int64) *rleBuffer {
254	const chunkSize = 1024
255	chunks := int(size / chunkSize)
256	// write size bytes plus "END\n" to a zip file
257	buf := new(rleBuffer)
258	w := NewWriter(buf)
259	f, err := w.CreateHeader(&FileHeader{
260		Name:   "huge.txt",
261		Method: Store,
262	})
263	if err != nil {
264		t.Fatal(err)
265	}
266	f.(*fileWriter).crc32 = fakeHash32{}
267	chunk := make([]byte, chunkSize)
268	for i := range chunk {
269		chunk[i] = '.'
270	}
271	for i := 0; i < chunks; i++ {
272		_, err := f.Write(chunk)
273		if err != nil {
274			t.Fatal("write chunk:", err)
275		}
276	}
277	if frag := int(size % chunkSize); frag > 0 {
278		_, err := f.Write(chunk[:frag])
279		if err != nil {
280			t.Fatal("write chunk:", err)
281		}
282	}
283	end := []byte("END\n")
284	_, err = f.Write(end)
285	if err != nil {
286		t.Fatal("write end:", err)
287	}
288	if err := w.Close(); err != nil {
289		t.Fatal(err)
290	}
291
292	// read back zip file and check that we get to the end of it
293	r, err := NewReader(buf, int64(buf.Size()))
294	if err != nil {
295		t.Fatal("reader:", err)
296	}
297	f0 := r.File[0]
298	rc, err := f0.Open()
299	if err != nil {
300		t.Fatal("opening:", err)
301	}
302	rc.(*checksumReader).hash = fakeHash32{}
303	for i := 0; i < chunks; i++ {
304		_, err := io.ReadFull(rc, chunk)
305		if err != nil {
306			t.Fatal("read:", err)
307		}
308	}
309	if frag := int(size % chunkSize); frag > 0 {
310		_, err := io.ReadFull(rc, chunk[:frag])
311		if err != nil {
312			t.Fatal("read:", err)
313		}
314	}
315	gotEnd, err := ioutil.ReadAll(rc)
316	if err != nil {
317		t.Fatal("read end:", err)
318	}
319	if !bytes.Equal(gotEnd, end) {
320		t.Errorf("End of zip64 archive %q, want %q", gotEnd, end)
321	}
322	err = rc.Close()
323	if err != nil {
324		t.Fatal("closing:", err)
325	}
326	if size+int64(len("END\n")) >= 1<<32-1 {
327		if got, want := f0.UncompressedSize, uint32(uint32max); got != want {
328			t.Errorf("UncompressedSize %#x, want %#x", got, want)
329		}
330	}
331
332	if got, want := f0.UncompressedSize64, uint64(size)+uint64(len(end)); got != want {
333		t.Errorf("UncompressedSize64 %#x, want %#x", got, want)
334	}
335
336	return buf
337}
338
339// Issue 9857
340func testZip64DirectoryRecordLength(buf *rleBuffer, t *testing.T) {
341	d := make([]byte, 1024)
342	if _, err := buf.ReadAt(d, buf.Size()-int64(len(d))); err != nil {
343		t.Fatal("read:", err)
344	}
345
346	sigOff := findSignatureInBlock(d)
347	dirOff, err := findDirectory64End(buf, buf.Size()-int64(len(d))+int64(sigOff))
348	if err != nil {
349		t.Fatal("findDirectory64End:", err)
350	}
351
352	d = make([]byte, directory64EndLen)
353	if _, err := buf.ReadAt(d, dirOff); err != nil {
354		t.Fatal("read:", err)
355	}
356
357	b := readBuf(d)
358	if sig := b.uint32(); sig != directory64EndSignature {
359		t.Fatalf("Expected directory64EndSignature (%d), got %d", directory64EndSignature, sig)
360	}
361
362	size := b.uint64()
363	if size != directory64EndLen-12 {
364		t.Fatalf("Expected length of %d, got %d", directory64EndLen-12, size)
365	}
366}
367
368func testValidHeader(h *FileHeader, t *testing.T) {
369	var buf bytes.Buffer
370	z := NewWriter(&buf)
371
372	f, err := z.CreateHeader(h)
373	if err != nil {
374		t.Fatalf("error creating header: %v", err)
375	}
376	if _, err := f.Write([]byte("hi")); err != nil {
377		t.Fatalf("error writing content: %v", err)
378	}
379	if err := z.Close(); err != nil {
380		t.Fatalf("error closing zip writer: %v", err)
381	}
382
383	b := buf.Bytes()
384	zf, err := NewReader(bytes.NewReader(b), int64(len(b)))
385	if err != nil {
386		t.Fatalf("got %v, expected nil", err)
387	}
388	zh := zf.File[0].FileHeader
389	if zh.Name != h.Name || zh.Method != h.Method || zh.UncompressedSize64 != uint64(len("hi")) {
390		t.Fatalf("got %q/%d/%d expected %q/%d/%d", zh.Name, zh.Method, zh.UncompressedSize64, h.Name, h.Method, len("hi"))
391	}
392}
393
394// Issue 4302.
395func TestHeaderInvalidTagAndSize(t *testing.T) {
396	const timeFormat = "20060102T150405.000.txt"
397
398	ts := time.Now()
399	filename := ts.Format(timeFormat)
400
401	h := FileHeader{
402		Name:   filename,
403		Method: Deflate,
404		Extra:  []byte(ts.Format(time.RFC3339Nano)), // missing tag and len, but Extra is best-effort parsing
405	}
406	h.SetModTime(ts)
407
408	testValidHeader(&h, t)
409}
410
411func TestHeaderTooShort(t *testing.T) {
412	h := FileHeader{
413		Name:   "foo.txt",
414		Method: Deflate,
415		Extra:  []byte{zip64ExtraId}, // missing size and second half of tag, but Extra is best-effort parsing
416	}
417	testValidHeader(&h, t)
418}
419
420func TestHeaderIgnoredSize(t *testing.T) {
421	h := FileHeader{
422		Name:   "foo.txt",
423		Method: Deflate,
424		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
425	}
426	testValidHeader(&h, t)
427}
428
429// Issue 4393. It is valid to have an extra data header
430// which contains no body.
431func TestZeroLengthHeader(t *testing.T) {
432	h := FileHeader{
433		Name:   "extadata.txt",
434		Method: Deflate,
435		Extra: []byte{
436			85, 84, 5, 0, 3, 154, 144, 195, 77, // tag 21589 size 5
437			85, 120, 0, 0, // tag 30805 size 0
438		},
439	}
440	testValidHeader(&h, t)
441}
442
443// Just benchmarking how fast the Zip64 test above is. Not related to
444// our zip performance, since the test above disabled CRC32 and flate.
445func BenchmarkZip64Test(b *testing.B) {
446	for i := 0; i < b.N; i++ {
447		testZip64(b, 1<<26)
448	}
449}
450