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