-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfuzz_test.go
More file actions
258 lines (220 loc) Β· 7.15 KB
/
fuzz_test.go
File metadata and controls
258 lines (220 loc) Β· 7.15 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
package wrap_test
import (
"strings"
"testing"
"unicode/utf8"
"github.com/bbrks/wrap/v2"
)
func FuzzWrap(f *testing.F) {
// Seed corpus with interesting cases
f.Add("hello world", 10)
f.Add("hello-world-foo-bar", 8)
f.Add("", 5)
f.Add("a", 1)
f.Add("ζ₯ζ¬θͺγγΉγ", 3)
f.Add(" leading spaces", 10)
f.Add("trailing spaces ", 10)
f.Add("multiple spaces", 5)
f.Add("line1\nline2\nline3", 5)
f.Add("no-break-points-here", 5)
f.Add("ππππ", 2)
// Edge cases
f.Add("", 0)
f.Add("", -1)
f.Add("a", 0)
f.Add("a", -1)
f.Add("a", 1000000)
f.Add(strings.Repeat("a", 10000), 10)
f.Add(strings.Repeat("a ", 1000), 5)
f.Add(strings.Repeat("ζ₯ζ¬θͺ ", 500), 3)
// Pathological cases
f.Add("\n\n\n", 1)
f.Add(" \n \n ", 2)
f.Add("-", 1)
f.Add("---", 1)
f.Add("- - - -", 1)
f.Add("\t\t\t", 2)
f.Add("a\nb\nc\nd\ne", 1)
f.Add(strings.Repeat("\n", 100), 5)
f.Add(strings.Repeat(" ", 100), 5)
f.Add(strings.Repeat("-", 100), 5)
// Mixed multibyte
f.Add("aγbγcγdγeγ", 2)
f.Add("πΊπΈπ¬π§π―π΅", 2) // Flag emojis (multi-codepoint)
f.Add("π¨βπ©βπ§βπ¦", 1) // Family emoji (ZWJ sequence)
f.Add("e\u0301", 1) // e + combining acute accent
f.Add("νκΈν
μ€νΈ", 3) // Korean
f.Add("Ω
Ψ±ΨΨ¨Ψ§", 2) // Arabic (RTL)
f.Add("Χ©ΧΧΧ", 2) // Hebrew (RTL)
// Breakpoint edge cases
f.Add("a-b-c-d-e", 1)
f.Add("a - b - c", 2)
f.Add("----", 2)
f.Add(" ", 2)
f.Add("a--b", 2)
f.Add("a b", 2)
f.Add("-a-", 2)
f.Add(" a ", 2)
f.Fuzz(func(t *testing.T, input string, limit int) {
// Skip invalid UTF-8 inputs
if !utf8.ValidString(input) {
t.Skip()
}
w := wrap.NewWrapper()
result := w.Wrap(input, limit)
// Result must be valid UTF-8
if !utf8.ValidString(result) {
t.Errorf("result is not valid UTF-8: %q", result)
}
// Test with StripTrailingNewline
w.StripTrailingNewline = true
result2 := w.Wrap(input, limit)
if !utf8.ValidString(result2) {
t.Errorf("result with StripTrailingNewline is not valid UTF-8: %q", result2)
}
// Test with CutLongWords
w.CutLongWords = true
result3 := w.Wrap(input, limit)
if !utf8.ValidString(result3) {
t.Errorf("result with CutLongWords is not valid UTF-8: %q", result3)
}
// With CutLongWords and positive limit, lines should not exceed limit by more than 1
// (the +1 accounts for preserved hyphens at line breaks)
if limit > 0 {
for _, line := range strings.Split(strings.TrimSuffix(result3, "\n"), "\n") {
lineLen := utf8.RuneCountInString(line)
// Allow +1 for hyphen preservation at end of line
if lineLen > limit+1 {
t.Errorf("line exceeds limit %d by more than 1: %q (len=%d)", limit, line, lineLen)
}
}
}
// Test with MinimumRaggedness
w.CutLongWords = false
w.MinimumRaggedness = true
result4 := w.Wrap(input, limit)
if !utf8.ValidString(result4) {
t.Errorf("result with MinimumRaggedness is not valid UTF-8: %q", result4)
}
// Test MinimumRaggedness with CutLongWords
w.CutLongWords = true
result5 := w.Wrap(input, limit)
if !utf8.ValidString(result5) {
t.Errorf("result with MinimumRaggedness+CutLongWords is not valid UTF-8: %q", result5)
}
})
}
func FuzzWrapWithOptions(f *testing.F) {
// Seed with combinations of input, limit, prefix, suffix
f.Add("hello world", 10, "// ", "", " -")
f.Add("hello world", 15, "/* ", " */", " -")
f.Add("test input", 5, "> ", "", " ")
f.Add("ζ₯ζ¬θͺ", 4, "- ", "", " -")
f.Add("a-b-c", 3, "", "", "-")
f.Add("a|b|c", 3, "", "", "|")
f.Add("test", 10, ">>>", "<<<", " ")
f.Add("", 5, "prefix", "suffix", " -")
f.Add("no breaks here", 5, "", "", "")
f.Add(strings.Repeat("word ", 100), 20, "# ", "", " ")
// Extreme prefix/suffix
f.Add("x", 100, strings.Repeat("p", 50), strings.Repeat("s", 50), " ")
f.Add("x", 10, strings.Repeat("p", 100), "", " ")
// Custom breakpoints
f.Add("a,b,c,d,e", 3, "", "", ",")
f.Add("a::b::c", 4, "", "", ":")
f.Add("path/to/file", 5, "", "", "/")
f.Add("CamelCaseText", 5, "", "", "ABCDEFGHIJKLMNOPQRSTUVWXYZ")
f.Fuzz(func(t *testing.T, input string, limit int, prefix, suffix, breakpoints string) {
// Skip invalid UTF-8 inputs
if !utf8.ValidString(input) || !utf8.ValidString(prefix) || !utf8.ValidString(suffix) || !utf8.ValidString(breakpoints) {
t.Skip()
}
w := wrap.NewWrapper()
w.OutputLinePrefix = prefix
w.OutputLineSuffix = suffix
w.Breakpoints = breakpoints
result := w.Wrap(input, limit)
// Result must be valid UTF-8
if !utf8.ValidString(result) {
t.Errorf("result is not valid UTF-8: %q", result)
}
// Test all option combinations including MinimumRaggedness
for _, strip := range []bool{false, true} {
for _, cut := range []bool{false, true} {
for _, includeLimit := range []bool{false, true} {
for _, optimal := range []bool{false, true} {
w.StripTrailingNewline = strip
w.CutLongWords = cut
w.LimitIncludesPrefixSuffix = includeLimit
w.MinimumRaggedness = optimal
result := w.Wrap(input, limit)
if !utf8.ValidString(result) {
t.Errorf("result is not valid UTF-8 with strip=%v cut=%v includeLimit=%v optimal=%v: %q",
strip, cut, includeLimit, optimal, result)
}
}
}
}
}
})
}
func FuzzWrapCustomNewline(f *testing.F) {
f.Add("hello world", 5, "\n")
f.Add("hello world", 5, "\r\n")
f.Add("hello world", 5, "<br>")
f.Add("line1\nline2", 10, "\n")
f.Add("line1\r\nline2", 10, "\r\n")
f.Add("line1<br>line2", 10, "<br>")
f.Add(strings.Repeat("word ", 50), 10, "|||")
f.Add("ζ₯ζ¬θͺγγΉγ", 3, "β")
f.Fuzz(func(t *testing.T, input string, limit int, newline string) {
if !utf8.ValidString(input) || !utf8.ValidString(newline) {
t.Skip()
}
w := wrap.NewWrapper()
w.Newline = newline
result := w.Wrap(input, limit)
if !utf8.ValidString(result) {
t.Errorf("result is not valid UTF-8: %q", result)
}
// Test with various option combinations
for _, strip := range []bool{false, true} {
for _, cut := range []bool{false, true} {
w.StripTrailingNewline = strip
w.CutLongWords = cut
result := w.Wrap(input, limit)
if !utf8.ValidString(result) {
t.Errorf("result is not valid UTF-8 with strip=%v cut=%v: %q", strip, cut, result)
}
}
}
})
}
func FuzzWrapTrimOptions(f *testing.F) {
f.Add("// hello world", 10, "// ", "")
f.Add("/* comment */", 20, "/* ", " */")
f.Add("# heading", 15, "# ", "")
f.Add("> quote", 10, "> ", "")
f.Add(" indented ", 10, " ", " ")
f.Add("prefix text suffix", 10, "prefix ", " suffix")
f.Add("// line1\n// line2", 20, "// ", "")
f.Fuzz(func(t *testing.T, input string, limit int, trimPrefix, trimSuffix string) {
if !utf8.ValidString(input) || !utf8.ValidString(trimPrefix) || !utf8.ValidString(trimSuffix) {
t.Skip()
}
w := wrap.NewWrapper()
w.TrimInputPrefix = trimPrefix
w.TrimInputSuffix = trimSuffix
result := w.Wrap(input, limit)
if !utf8.ValidString(result) {
t.Errorf("result is not valid UTF-8: %q", result)
}
// Combined with output prefix/suffix
w.OutputLinePrefix = trimPrefix
w.OutputLineSuffix = trimSuffix
result2 := w.Wrap(input, limit)
if !utf8.ValidString(result2) {
t.Errorf("result with output prefix/suffix is not valid UTF-8: %q", result2)
}
})
}