Source file
src/archive/tar/fuzz_test.go
1
2
3
4
5 package tar
6
7 import (
8 "bytes"
9 "io"
10 "testing"
11 )
12
13 func FuzzReader(f *testing.F) {
14 b := bytes.NewBuffer(nil)
15 w := NewWriter(b)
16 inp := []byte("Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.")
17 err := w.WriteHeader(&Header{
18 Name: "lorem.txt",
19 Mode: 0600,
20 Size: int64(len(inp)),
21 })
22 if err != nil {
23 f.Fatalf("failed to create writer: %s", err)
24 }
25 _, err = w.Write(inp)
26 if err != nil {
27 f.Fatalf("failed to write file to archive: %s", err)
28 }
29 if err := w.Close(); err != nil {
30 f.Fatalf("failed to write archive: %s", err)
31 }
32 f.Add(b.Bytes())
33
34 f.Fuzz(func(t *testing.T, b []byte) {
35 r := NewReader(bytes.NewReader(b))
36 type file struct {
37 header *Header
38 content []byte
39 }
40 files := []file{}
41 for {
42 hdr, err := r.Next()
43 if err == io.EOF {
44 break
45 }
46 if err != nil {
47 return
48 }
49 buf := bytes.NewBuffer(nil)
50 if _, err := io.Copy(buf, r); err != nil {
51 continue
52 }
53 files = append(files, file{header: hdr, content: buf.Bytes()})
54 }
55
56
57
58 if len(files) == 0 {
59 return
60 }
61
62 out := bytes.NewBuffer(nil)
63 w := NewWriter(out)
64 for _, f := range files {
65 if err := w.WriteHeader(f.header); err != nil {
66 t.Fatalf("unable to write previously parsed header: %s", err)
67 }
68 if _, err := w.Write(f.content); err != nil {
69 t.Fatalf("unable to write previously parsed content: %s", err)
70 }
71 }
72 if err := w.Close(); err != nil {
73 t.Fatalf("Unable to write archive: %s", err)
74 }
75
76
77
78
79 })
80 }
81
View as plain text