| 1 |
package utils |
| 2 |
|
| 3 |
import ( |
| 4 |
"slices" |
| 5 |
"strings" |
| 6 |
"testing" |
| 7 |
"unicode" |
| 8 |
|
| 9 |
"github.com/stretchr/testify/require" |
| 10 |
) |
| 11 |
|
| 12 |
// refIndexControl is the scalar reference; exempt is noControlExemption or '\t'. |
| 13 |
func refIndexControl(s string, exempt byte) int { |
| 14 |
for i := range len(s) { |
| 15 |
c := s[i] |
| 16 |
if (c < 0x20 || c == 0x7f) && c != exempt { |
| 17 |
return i |
| 18 |
} |
| 19 |
} |
| 20 |
return -1 |
| 21 |
} |
| 22 |
|
| 23 |
// checkIndexControl compares both scanners on both input types with the reference. |
| 24 |
func checkIndexControl(tb testing.TB, s string) { |
| 25 |
tb.Helper() |
| 26 |
if want := refIndexControl(s, noControlExemption); IndexControl(s) != want || IndexControl([]byte(s)) != want { |
| 27 |
tb.Fatalf("IndexControl(%q) = %d/%d, want %d", s, IndexControl(s), IndexControl([]byte(s)), want) |
| 28 |
} |
| 29 |
if want := refIndexControl(s, '\t'); IndexControlExceptTab(s) != want || IndexControlExceptTab([]byte(s)) != want { |
| 30 |
tb.Fatalf("IndexControlExceptTab(%q) = %d/%d, want %d", s, IndexControlExceptTab(s), IndexControlExceptTab([]byte(s)), want) |
| 31 |
} |
| 32 |
} |
| 33 |
|
| 34 |
func Test_IndexControl(t *testing.T) { |
| 35 |
t.Parallel() |
| 36 |
// Every byte value at every position of clean prefixes up to 40 bytes, so each scan shape sees each byte. |
| 37 |
clean := strings.Repeat("abcdefghijklmnopqrstuvwxyz0123456789", 2) |
| 38 |
for n := 0; n <= 40; n++ { |
| 39 |
checkIndexControl(t, clean[:n]) |
| 40 |
for c := range 256 { |
| 41 |
for pos := 0; pos <= n; pos++ { |
| 42 |
checkIndexControl(t, string(slices.Insert([]byte(clean[:n]), pos, byte(c)))) |
| 43 |
} |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
// Bytes >= 0x80 never match, including the UTF-8 C1 controls unicode.IsControl flags. |
| 48 |
require.Equal(t, -1, IndexControl("caf\xc3\xa9 \xc2\x85 \xff\x80\x9f")) |
| 49 |
require.Equal(t, 6, strings.IndexFunc("caf\xc3\xa9 \xc2\x85", unicode.IsControl)) |
| 50 |
require.Equal(t, -1, IndexControlExceptTab("a\tb\tc")) |
| 51 |
require.Equal(t, 1, IndexControl("a\tb\tc")) |
| 52 |
require.Equal(t, 3, IndexControlExceptTab("a\tb\x7f")) |
| 53 |
require.Equal(t, -1, IndexControl("")) |
| 54 |
require.Equal(t, 0, IndexControl("\x00")) |
| 55 |
// Only HTAB is exempt: the neighboring controls still match. |
| 56 |
require.Equal(t, 0, IndexControlExceptTab("\x08")) |
| 57 |
require.Equal(t, 0, IndexControlExceptTab("\n")) |
| 58 |
} |
| 59 |
|
| 60 |
func FuzzIndexControl(f *testing.F) { |
| 61 |
f.Add("") |
| 62 |
f.Add("plain header value") |
| 63 |
f.Add("tab\tseparated\tvalue") |
| 64 |
f.Add("crlf\r\ninjection") |
| 65 |
f.Add("del\x7fbyte") |
| 66 |
f.Add("utf8 caf\xc3\xa9 and \xc2\x85") |
| 67 |
f.Add(strings.Repeat("x", 31) + "\x01") |
| 68 |
f.Fuzz(func(t *testing.T, s string) { |
| 69 |
checkIndexControl(t, s) |
| 70 |
}) |
| 71 |
} |
| 72 |
|
| 73 |
func Benchmark_IndexControl(b *testing.B) { |
| 74 |
inputs := []struct { |
| 75 |
name string |
| 76 |
value string |
| 77 |
}{ |
| 78 |
{"clean-16B", "text/html;charse"}, |
| 79 |
{"clean-64B", strings.Repeat("no-control-bytes", 4)}, |
| 80 |
{"ctl-at-end-64B", strings.Repeat("no-control-bytes", 4)[:63] + "\r"}, |
| 81 |
} |
| 82 |
var res int |
| 83 |
for _, input := range inputs { |
| 84 |
b.Run(input.name+"/fiber", func(b *testing.B) { |
| 85 |
b.ReportAllocs() |
| 86 |
for b.Loop() { |
| 87 |
res = IndexControl(input.value) |
| 88 |
} |
| 89 |
}) |
| 90 |
b.Run(input.name+"/fiber-except-tab", func(b *testing.B) { |
| 91 |
b.ReportAllocs() |
| 92 |
for b.Loop() { |
| 93 |
res = IndexControlExceptTab(input.value) |
| 94 |
} |
| 95 |
}) |
| 96 |
b.Run(input.name+"/default", func(b *testing.B) { |
| 97 |
b.ReportAllocs() |
| 98 |
for b.Loop() { |
| 99 |
res = strings.IndexFunc(input.value, unicode.IsControl) |
| 100 |
} |
| 101 |
}) |
| 102 |
} |
| 103 |
_ = res |
| 104 |
} |