Source file
src/internal/fuzz/minimize_test.go
1
2
3
4
5
6
7 package fuzz
8
9 import (
10 "bytes"
11 "context"
12 "errors"
13 "fmt"
14 "reflect"
15 "testing"
16 "time"
17 "unicode"
18 "unicode/utf8"
19 )
20
21 func TestMinimizeInput(t *testing.T) {
22 type testcase struct {
23 name string
24 fn func(CorpusEntry) error
25 input []any
26 expected []any
27 }
28 cases := []testcase{
29 {
30 name: "ones_byte",
31 fn: func(e CorpusEntry) error {
32 b := e.Values[0].([]byte)
33 ones := 0
34 for _, v := range b {
35 if v == 1 {
36 ones++
37 }
38 }
39 if ones == 3 {
40 return fmt.Errorf("bad %v", e.Values[0])
41 }
42 return nil
43 },
44 input: []any{[]byte{0, 0, 1, 0, 1, 0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}},
45 expected: []any{[]byte{1, 1, 1}},
46 },
47 {
48 name: "single_bytes",
49 fn: func(e CorpusEntry) error {
50 b := e.Values[0].([]byte)
51 if len(b) < 2 {
52 return nil
53 }
54 if len(b) == 2 && b[0] == 1 && b[1] == 2 {
55 return nil
56 }
57 return fmt.Errorf("bad %v", e.Values[0])
58 },
59 input: []any{[]byte{1, 2, 3, 4, 5}},
60 expected: []any{[]byte("00")},
61 },
62 {
63 name: "set_of_bytes",
64 fn: func(e CorpusEntry) error {
65 b := e.Values[0].([]byte)
66 if len(b) < 3 {
67 return nil
68 }
69 if bytes.Equal(b, []byte{0, 1, 2, 3, 4, 5}) || bytes.Equal(b, []byte{0, 4, 5}) {
70 return fmt.Errorf("bad %v", e.Values[0])
71 }
72 return nil
73 },
74 input: []any{[]byte{0, 1, 2, 3, 4, 5}},
75 expected: []any{[]byte{0, 4, 5}},
76 },
77 {
78 name: "non_ascii_bytes",
79 fn: func(e CorpusEntry) error {
80 b := e.Values[0].([]byte)
81 if len(b) == 3 {
82 return fmt.Errorf("bad %v", e.Values[0])
83 }
84 return nil
85 },
86 input: []any{[]byte("ท")},
87 expected: []any{[]byte("000")},
88 },
89 {
90 name: "ones_string",
91 fn: func(e CorpusEntry) error {
92 b := e.Values[0].(string)
93 ones := 0
94 for _, v := range b {
95 if v == '1' {
96 ones++
97 }
98 }
99 if ones == 3 {
100 return fmt.Errorf("bad %v", e.Values[0])
101 }
102 return nil
103 },
104 input: []any{"001010001000000000000000000"},
105 expected: []any{"111"},
106 },
107 {
108 name: "string_length",
109 fn: func(e CorpusEntry) error {
110 b := e.Values[0].(string)
111 if len(b) == 5 {
112 return fmt.Errorf("bad %v", e.Values[0])
113 }
114 return nil
115 },
116 input: []any{"zzzzz"},
117 expected: []any{"00000"},
118 },
119 {
120 name: "string_with_letter",
121 fn: func(e CorpusEntry) error {
122 b := e.Values[0].(string)
123 r, _ := utf8.DecodeRune([]byte(b))
124 if unicode.IsLetter(r) {
125 return fmt.Errorf("bad %v", e.Values[0])
126 }
127 return nil
128 },
129 input: []any{"ZZZZZ"},
130 expected: []any{"A"},
131 },
132 }
133
134 for _, tc := range cases {
135 t.Run(tc.name, func(t *testing.T) {
136 t.Parallel()
137 ws := &workerServer{
138 fuzzFn: func(e CorpusEntry) (time.Duration, error) {
139 return time.Second, tc.fn(e)
140 },
141 }
142 mem := &sharedMem{region: make([]byte, 100)}
143 vals := tc.input
144 success, err := ws.minimizeInput(context.Background(), vals, mem, minimizeArgs{})
145 if !success {
146 t.Errorf("minimizeInput did not succeed")
147 }
148 if err == nil {
149 t.Fatal("minimizeInput didn't provide an error")
150 }
151 if expected := fmt.Sprintf("bad %v", tc.expected[0]); err.Error() != expected {
152 t.Errorf("unexpected error: got %q, want %q", err, expected)
153 }
154 if !reflect.DeepEqual(vals, tc.expected) {
155 t.Errorf("unexpected results: got %v, want %v", vals, tc.expected)
156 }
157 })
158 }
159 }
160
161
162
163
164 func TestMinimizeFlaky(t *testing.T) {
165 ws := &workerServer{fuzzFn: func(e CorpusEntry) (time.Duration, error) {
166 return time.Second, errors.New("ohno")
167 }}
168 mem := &sharedMem{region: make([]byte, 100)}
169 vals := []any{[]byte(nil)}
170 args := minimizeArgs{KeepCoverage: make([]byte, len(coverageSnapshot))}
171 success, err := ws.minimizeInput(context.Background(), vals, mem, args)
172 if success {
173 t.Error("unexpected success")
174 }
175 if err != nil {
176 t.Errorf("unexpected error: %v", err)
177 }
178 if count := mem.header().count; count != 1 {
179 t.Errorf("count: got %d, want 1", count)
180 }
181 }
182
View as plain text