Debian Codegraph @

internal/caseconv/caseconv_test.go

raw
1 package caseconv
2
3 import (
4 "math/rand"
5 "testing"
6
7 "github.com/stretchr/testify/require"
8 )
9
10 // refLower and refUpper are the scalar ASCII folds every test measures against;
11 // bytes >= 0x80 pass through, as both the tables and the kernels promise.
12 func refLower(b []byte) []byte {
13 out := make([]byte, len(b))
14 for i, c := range b {
15 if c >= 'A' && c <= 'Z' {
16 c += 'a' - 'A'
17 }
18 out[i] = c
19 }
20 return out
21 }
22
23 func refUpper(b []byte) []byte {
24 out := make([]byte, len(b))
25 for i, c := range b {
26 if c >= 'a' && c <= 'z' {
27 c -= 'a' - 'A'
28 }
29 out[i] = c
30 }
31 return out
32 }
33
34 func refFirstIndex(b []byte, lo, hi byte) int {
35 for i, c := range b {
36 if c >= lo && c <= hi {
37 return i
38 }
39 }
40 return -1
41 }
42
43 // assertCaseParity drives the whole interface over one input; the randomized and
44 // fuzz tests share it.
45 //
46 // Expectations are computed before each call and src is compared against an
47 // untouched copy after: the converters promise to read src only, and an illegal
48 // write there is itself a case-fold, so a reference read afterwards would match
49 // the corruption rather than catch it.
50 func assertCaseParity(t *testing.T, src []byte) {
51 t.Helper()
52
53 untouched := append([]byte(nil), src...)
54 wantLower, wantUpper := refLower(src), refUpper(src)
55 wantUp, wantLow := refFirstIndex(src, 'A', 'Z'), refFirstIndex(src, 'a', 'z')
56
57 iUp, iLow := FirstUpperIndex(src), FirstLowerIndex(src)
58 require.Equal(t, wantUp, iUp, "FirstUpperIndex(%q)", src)
59 require.Equal(t, wantLow, iLow, "FirstLowerIndex(%q)", src)
60
61 // The From converters require a word of input and the scanner's own index.
62 if len(src) >= WordLen {
63 if iUp >= 0 {
64 require.Equal(t, string(wantLower), string(ToLowerFrom(src, iUp)), "ToLowerFrom(%q, %d)", src, iUp)
65 require.Equal(t, string(untouched), string(src), "ToLowerFrom wrote through src")
66 }
67 if iLow >= 0 {
68 require.Equal(t, string(wantUpper), string(ToUpperFrom(src, iLow)), "ToUpperFrom(%q, %d)", src, iLow)
69 require.Equal(t, string(untouched), string(src), "ToUpperFrom wrote through src")
70 }
71 }
72
73 lowered := append([]byte(nil), src...)
74 ToLowerInPlace(lowered)
75 require.Equal(t, string(wantLower), string(lowered), "ToLowerInPlace(%q)", untouched)
76
77 uppered := append([]byte(nil), src...)
78 ToUpperInPlace(uppered)
79 require.Equal(t, string(wantUpper), string(uppered), "ToUpperInPlace(%q)", untouched)
80 }
81
82 // Test_CaseConv_Randomized covers sub-word inputs, every %WordLen tail, the
83 // block loop and its overlapping tail, with non-ASCII bytes mixed in.
84 func Test_CaseConv_Randomized(t *testing.T) {
85 t.Parallel()
86 rng := rand.New(rand.NewSource(42)) //nolint:gosec // deterministic test data
87
88 for range 4000 {
89 src := make([]byte, rng.Intn(101))
90 for i := range src {
91 src[i] = byte(rng.Intn(256))
92 }
93 assertCaseParity(t, src)
94 }
95 }
96
97 // Test_CaseConv_Lengths walks every length with the changing byte at every
98 // position, so each word and block offset is the first change once and the
99 // overlapping tail store meets every residue. The letter-free filler carries a
100 // byte >= 0x80 every fourth offset: without MatchRangeMask's high-bit clear,
101 // 0xE9 falsely matches 'a'..'z' and this fails.
102 func Test_CaseConv_Lengths(t *testing.T) {
103 t.Parallel()
104
105 for n := range 71 {
106 base := make([]byte, n)
107 for i := range base {
108 base[i] = "40-\xe9/~:9"[i%8] // no letters at all, incl. >= 0x80
109 }
110 // The untouched filler drives both scanners to -1.
111 assertCaseParity(t, base)
112
113 for pos := range n {
114 lo := append([]byte(nil), base...)
115 lo[pos] = 'A' + byte(pos%26)
116 if pos+1 < n {
117 lo[pos+1] = 0xE9
118 }
119 assertCaseParity(t, lo)
120
121 up := append([]byte(nil), base...)
122 up[pos] = 'a' + byte(pos%26)
123 if pos+1 < n {
124 up[pos+1] = 0xE9
125 }
126 assertCaseParity(t, up)
127 }
128 }
129 }
130
131 // Test_ConvertFrom_PrefixIsTakenOnTrust pins the prefix skip, which no
132 // in-contract equality oracle can see: there the skipped bytes are unchanged
133 // either way. So these pass a first above the true first change -- the case the
134 // doc warns mis-converts -- and fail if the alignment or prefix copy is dropped.
135 // The results are not a clean unconverted prefix, because the overlapping tail
136 // store folds src back over part of it.
137 func Test_ConvertFrom_PrefixIsTakenOnTrust(t *testing.T) {
138 t.Parallel()
139
140 // The tail store folds src[n-WordLen:n] over dst, so only byte 0 survives.
141 require.Equal(t, "Aaaaaaaab", string(ToLowerFrom([]byte("AAAAAAAAB"), WordLen)))
142 require.Equal(t, "aAAAAAAAB", string(ToUpperFrom([]byte("aaaaaaaab"), WordLen)))
143
144 // An unaligned first aligns down, so its whole word folds after all.
145 require.Equal(t, "aaaaaaaab", string(ToLowerFrom([]byte("AAAAAAAAB"), WordLen-1)))
146 require.Equal(t, "AAAAAAAAB", string(ToUpperFrom([]byte("aaaaaaaab"), WordLen-1)))
147 }
148
149 // FuzzCaseConv is the property test AGENTS.md asks of bit-twiddling code; the
150 // seeds pin the word and block boundaries and carry non-ASCII at word lengths
151 // and above, where the kernels rather than the tables fold.
152 func FuzzCaseConv(f *testing.F) {
153 f.Add([]byte(""))
154 f.Add([]byte("a"))
155 f.Add([]byte("Content-Type"))
156 f.Add([]byte("0123456A"))
157 f.Add([]byte("0123456789abcdefghijklmnopqrstuvW"))
158 f.Add([]byte("\xe9A\xe9a"))
159 f.Add([]byte("0123456789\xe9\xc3\xa9ABCDEFGHIJKLMNOPQRST"))
160 f.Add([]byte("\xffA\xff\xffBCDEFGH\x80ijklmnop\x7f"))
161 f.Add([]byte("AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA"))
162 f.Fuzz(func(t *testing.T, src []byte) {
163 assertCaseParity(t, src)
164 })
165 }