]> Cypherpunks.ru repositories - gostls13.git/blob - src/internal/zstd/fuzz_test.go
internal/zstd: avoid panic when the regenerated size is too small
[gostls13.git] / src / internal / zstd / fuzz_test.go
1 // Copyright 2023 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 package zstd
6
7 import (
8         "bytes"
9         "io"
10         "os"
11         "os/exec"
12         "testing"
13 )
14
15 // badStrings is some inputs that FuzzReader failed on earlier.
16 var badStrings = []string{
17         "(\xb5/\xfdd00,\x05\x00\xc4\x0400000000000000000000000000000000000000000000000000000000000000000000000000000 \xa07100000000000000000000000000000000000000000000000000000000000000000000000000aM\x8a2y0B\b",
18         "(\xb5/\xfd00$\x05\x0020 00X70000a70000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
19         "(\xb5/\xfd00$\x05\x0020 00B00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
20         "(\xb5/\xfd00}\x00\x0020\x00\x9000000000000",
21         "(\xb5/\xfd00}\x00\x00&0\x02\x830!000000000",
22         "(\xb5/\xfd\x1002000$\x05\x0010\xcc0\xa8100000000100000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
23         "(\xb5/\xfd\x1002000$\x05\x0000\xcc0\xa8100d\x0000001000000000000000000000000000000000000000000000000000000000000000000000000\x000000000000000000000000000000000000000000000000000000000000000000000000000000",
24         "(\xb5/\xfd001\x00\x0000000000000000000",
25         "(\xb5/\xfd00\xec\x00\x00&@\x05\x05A7002\x02\x00\x02\x00\x02\x0000000000000000",
26         "(\xb5/\xfd00\xec\x00\x00V@\x05\x0517002\x02\x00\x02\x00\x02\x0000000000000000",
27 }
28
29 // This is a simple fuzzer to see if the decompressor panics.
30 func FuzzReader(f *testing.F) {
31         for _, test := range tests {
32                 f.Add([]byte(test.compressed))
33         }
34         for _, s := range badStrings {
35                 f.Add([]byte(s))
36         }
37         f.Fuzz(func(t *testing.T, b []byte) {
38                 r := NewReader(bytes.NewReader(b))
39                 io.Copy(io.Discard, r)
40         })
41 }
42
43 // Fuzz test to verify that what we decompress is what we compress.
44 // This isn't a great fuzz test because the fuzzer can't efficiently
45 // explore the space of decompressor behavior, since it can't see
46 // what the compressor is doing. But it's better than nothing.
47 func FuzzDecompressor(f *testing.F) {
48         zstd := findZstd(f)
49
50         for _, test := range tests {
51                 f.Add([]byte(test.uncompressed))
52         }
53
54         // Add some larger data, as that has more interesting compression.
55         f.Add(bytes.Repeat([]byte("abcdefghijklmnop"), 256))
56         var buf bytes.Buffer
57         for i := 0; i < 256; i++ {
58                 buf.WriteByte(byte(i))
59         }
60         f.Add(bytes.Repeat(buf.Bytes(), 64))
61         f.Add(bigData(f))
62
63         f.Fuzz(func(t *testing.T, b []byte) {
64                 cmd := exec.Command(zstd, "-z")
65                 cmd.Stdin = bytes.NewReader(b)
66                 var compressed bytes.Buffer
67                 cmd.Stdout = &compressed
68                 cmd.Stderr = os.Stderr
69                 if err := cmd.Run(); err != nil {
70                         t.Errorf("running zstd failed: %v", err)
71                 }
72
73                 r := NewReader(bytes.NewReader(compressed.Bytes()))
74                 got, err := io.ReadAll(r)
75                 if err != nil {
76                         t.Fatal(err)
77                 }
78                 if !bytes.Equal(got, b) {
79                         showDiffs(t, got, b)
80                 }
81         })
82 }
83
84 // Fuzz test to check that if we can decompress some data,
85 // so can zstd, and that we get the same result.
86 func FuzzReverse(f *testing.F) {
87         zstd := findZstd(f)
88
89         for _, test := range tests {
90                 f.Add([]byte(test.compressed))
91         }
92
93         // Set a hook to reject some cases where we don't match zstd.
94         fuzzing = true
95         defer func() { fuzzing = false }()
96
97         f.Fuzz(func(t *testing.T, b []byte) {
98                 r := NewReader(bytes.NewReader(b))
99                 goExp, goErr := io.ReadAll(r)
100
101                 cmd := exec.Command(zstd, "-d")
102                 cmd.Stdin = bytes.NewReader(b)
103                 var uncompressed bytes.Buffer
104                 cmd.Stdout = &uncompressed
105                 cmd.Stderr = os.Stderr
106                 zstdErr := cmd.Run()
107                 zstdExp := uncompressed.Bytes()
108
109                 if goErr == nil && zstdErr == nil {
110                         if !bytes.Equal(zstdExp, goExp) {
111                                 showDiffs(t, zstdExp, goExp)
112                         }
113                 } else {
114                         // Ideally we should check that this package and
115                         // the zstd program both fail or both succeed,
116                         // and that if they both fail one byte sequence
117                         // is an exact prefix of the other.
118                         // Actually trying this proved to be frustrating,
119                         // as the zstd program appears to accept invalid
120                         // byte sequences using rules that are difficult
121                         // to determine.
122                         // So we just check the prefix.
123
124                         c := len(goExp)
125                         if c > len(zstdExp) {
126                                 c = len(zstdExp)
127                         }
128                         goExp = goExp[:c]
129                         zstdExp = zstdExp[:c]
130                         if !bytes.Equal(goExp, zstdExp) {
131                                 t.Error("byte mismatch after error")
132                                 t.Logf("Go error: %v\n", goErr)
133                                 t.Logf("zstd error: %v\n", zstdErr)
134                                 showDiffs(t, zstdExp, goExp)
135                         }
136                 }
137         })
138 }