summaryrefslogtreecommitdiffstats
path: root/src/archive/zip/fuzz_test.go
blob: 7dffde69bf5d5f8934298bf69887cb64a525a41c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// Copyright 2021 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package zip

import (
	"bytes"
	"io"
	"os"
	"path/filepath"
	"testing"
)

func FuzzReader(f *testing.F) {
	testdata, err := os.ReadDir("testdata")
	if err != nil {
		f.Fatalf("failed to read testdata directory: %s", err)
	}
	for _, de := range testdata {
		if de.IsDir() {
			continue
		}
		b, err := os.ReadFile(filepath.Join("testdata", de.Name()))
		if err != nil {
			f.Fatalf("failed to read testdata: %s", err)
		}
		f.Add(b)
	}

	f.Fuzz(func(t *testing.T, b []byte) {
		r, err := NewReader(bytes.NewReader(b), int64(len(b)))
		if err != nil {
			return
		}

		type file struct {
			header  *FileHeader
			content []byte
		}
		files := []file{}

		for _, f := range r.File {
			fr, err := f.Open()
			if err != nil {
				continue
			}
			content, err := io.ReadAll(fr)
			if err != nil {
				continue
			}
			files = append(files, file{header: &f.FileHeader, content: content})
			if _, err := r.Open(f.Name); err != nil {
				continue
			}
		}

		// If we were unable to read anything out of the archive don't
		// bother trying to roundtrip it.
		if len(files) == 0 {
			return
		}

		w := NewWriter(io.Discard)
		for _, f := range files {
			ww, err := w.CreateHeader(f.header)
			if err != nil {
				t.Fatalf("unable to write previously parsed header: %s", err)
			}
			if _, err := ww.Write(f.content); err != nil {
				t.Fatalf("unable to write previously parsed content: %s", err)
			}
		}

		if err := w.Close(); err != nil {
			t.Fatalf("Unable to write archive: %s", err)
		}

		// TODO: We may want to check if the archive roundtrips.
	})
}