| 1 | package utils |
| 2 | |
| 3 | import ( |
| 4 | "encoding/json" |
| 5 | "fmt" |
| 6 | "net/http" |
| 7 | "net/netip" |
| 8 | "net/url" |
| 9 | "strings" |
| 10 | "testing" |
| 11 | "time" |
| 12 | |
| 13 | "github.com/stretchr/testify/require" |
| 14 | ) |
| 15 | |
| 16 | // Differential edge-case suites for the helpers that promise byte-for-byte |
| 17 | // stdlib parity. Rather than hand-picking expectations, these enumerate |
| 18 | // structured input spaces — positional mutations, word-boundary offsets, |
| 19 | // malformed UTF-8, calendar boundaries — and require agreement with the |
| 20 | // stdlib reference on every input. |
| 21 | |
| 22 | func Test_ParseHTTPDate_AllMonthsAndWeekdays(t *testing.T) { |
| 23 | t.Parallel() |
| 24 | // Twelve months exercise every httpMonthNum case; seven consecutive |
| 25 | // days exercise every weekday name. |
| 26 | for month := time.January; month <= time.December; month++ { |
| 27 | tm := time.Date(2021, month, 15, 1, 2, 3, 0, time.UTC) |
| 28 | got, err := ParseHTTPDate(FormatHTTPDate(tm)) |
| 29 | require.NoError(t, err, "month %v", month) |
| 30 | require.True(t, got.Equal(tm), "month %v", month) |
| 31 | } |
| 32 | for day := 1; day <= 7; day++ { |
| 33 | tm := time.Date(2021, time.August, day, 23, 59, 59, 0, time.UTC) |
| 34 | got, err := ParseHTTPDate(FormatHTTPDate(tm)) |
| 35 | require.NoError(t, err, "day %d", day) |
| 36 | require.True(t, got.Equal(tm), "day %d", day) |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | func Test_ParseHTTPDate_PositionalMutations(t *testing.T) { |
| 41 | t.Parallel() |
| 42 | base := "Sun, 06 Nov 1994 08:49:37 GMT" |
| 43 | replacements := []byte{' ', ':', ',', '-', '.', '0', '5', '9', 'A', 'M', 'a', 'z', 0x00, 0x7F, 0xFF} |
| 44 | for pos := range len(base) { |
| 45 | for _, r := range replacements { |
| 46 | mutated := base[:pos] + string(r) + base[pos+1:] |
| 47 | assertHTTPDateParity(t, mutated) |
| 48 | } |
| 49 | } |
| 50 | // Length mutations: truncations and extensions. |
| 51 | for cut := range len(base) { |
| 52 | assertHTTPDateParity(t, base[:cut]) |
| 53 | } |
| 54 | assertHTTPDateParity(t, base+" ") |
| 55 | assertHTTPDateParity(t, base+"X") |
| 56 | assertHTTPDateParity(t, " "+base) |
| 57 | assertHTTPDateParity(t, "\t"+base+"\r\n") |
| 58 | } |
| 59 | |
| 60 | func Test_ParseHTTPDate_CalendarBoundaries(t *testing.T) { |
| 61 | t.Parallel() |
| 62 | months := []string{"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"} |
| 63 | years := []int{0, 4, 100, 400, 1900, 1996, 2000, 2020, 2023, 9999} |
| 64 | days := []int{0, 1, 28, 29, 30, 31, 32, 99} |
| 65 | for _, year := range years { |
| 66 | for _, month := range months { |
| 67 | for _, day := range days { |
| 68 | in := fmt.Sprintf("Fri, %02d %s %04d 12:00:00 GMT", day, month, year) |
| 69 | assertHTTPDateParity(t, in) |
| 70 | } |
| 71 | } |
| 72 | } |
| 73 | // Clock-field boundaries. |
| 74 | for _, clock := range []string{"00:00:00", "23:59:59", "24:00:00", "23:60:00", "23:59:60", "99:99:99"} { |
| 75 | assertHTTPDateParity(t, "Sat, 15 Jul 2023 "+clock+" GMT") |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | // assertHTTPDateParity requires ParseHTTPDate to agree with the |
| 80 | // net/http.ParseTime reference on outcome, instant, and location. |
| 81 | func assertHTTPDateParity(t *testing.T, in string) { |
| 82 | t.Helper() |
| 83 | want, wantErr := refParseHTTPDate(in) |
| 84 | got, err := ParseHTTPDate(in) |
| 85 | if wantErr != nil { |
| 86 | require.Error(t, err, "input %q", in) |
| 87 | return |
| 88 | } |
| 89 | require.NoError(t, err, "input %q", in) |
| 90 | require.True(t, got.Equal(want), "input %q: got %v, want %v", in, got, want) |
| 91 | require.Equal(t, want.Location().String(), got.Location().String(), "input %q", in) |
| 92 | } |
| 93 | |
| 94 | func Test_AppendUnescape_LongAndBoundaryInputs(t *testing.T) { |
| 95 | t.Parallel() |
| 96 | long := strings.Repeat("abcdefgh", 8) // 64B, engages the vectorized scans |
| 97 | inputs := []string{ |
| 98 | long, |
| 99 | long + "%", |
| 100 | long + "%4", |
| 101 | long + "%41", |
| 102 | long + "%zz", |
| 103 | "%41" + long, |
| 104 | long[:31] + "%" + long[:1], // '%' exactly at index 31 |
| 105 | strings.Repeat("+", 64), |
| 106 | strings.Repeat("%25", 32), |
| 107 | strings.Repeat("a+", 32), |
| 108 | "%", |
| 109 | "+", |
| 110 | "%%%", |
| 111 | "%+1", |
| 112 | "+%41+", |
| 113 | } |
| 114 | for _, in := range inputs { |
| 115 | assertUnescapeParity(t, in) |
| 116 | } |
| 117 | |
| 118 | // A late error must still return the original dst content untouched. |
| 119 | got, err := AppendQueryUnescape([]byte("keep"), long+"%zz") |
| 120 | require.Error(t, err) |
| 121 | require.Equal(t, "keep", string(got)) |
| 122 | gotPath, err := AppendPathUnescape([]byte("keep"), long+"%") |
| 123 | require.Error(t, err) |
| 124 | require.Equal(t, "keep", string(gotPath)) |
| 125 | } |
| 126 | |
| 127 | func Test_AppendUnescape_EveryEscapeByte(t *testing.T) { |
| 128 | t.Parallel() |
| 129 | // Every byte in both positions of an escape pair, plus that byte alone |
| 130 | // after '%', pinned to net/url's accept set and error values. |
| 131 | for i := range 256 { |
| 132 | c := string([]byte{byte(i)}) |
| 133 | assertUnescapeParity(t, "%"+c+"0ab") |
| 134 | assertUnescapeParity(t, "%4"+c+"ab") |
| 135 | assertUnescapeParity(t, "ab%"+c) |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | // assertUnescapeParity requires the two unescape helpers to agree with |
| 140 | // net/url on output and exact error values, for both instantiations. |
| 141 | func assertUnescapeParity(t *testing.T, in string) { |
| 142 | t.Helper() |
| 143 | wantQ, wantQErr := url.QueryUnescape(in) |
| 144 | gotQ, errQ := AppendQueryUnescape(nil, in) |
| 145 | gotQB, errQB := AppendQueryUnescape([]byte(nil), []byte(in)) |
| 146 | wantP, wantPErr := url.PathUnescape(in) |
| 147 | gotP, errP := AppendPathUnescape(nil, in) |
| 148 | if wantQErr != nil { |
| 149 | require.Equal(t, wantQErr, errQ, "query input %q", in) |
| 150 | require.Equal(t, wantQErr, errQB, "query input %q", in) |
| 151 | } else { |
| 152 | require.NoError(t, errQ, "query input %q", in) |
| 153 | require.Equal(t, wantQ, string(gotQ), "query input %q", in) |
| 154 | require.Equal(t, wantQ, string(gotQB), "query input %q", in) |
| 155 | } |
| 156 | if wantPErr != nil { |
| 157 | require.Equal(t, wantPErr, errP, "path input %q", in) |
| 158 | } else { |
| 159 | require.NoError(t, errP, "path input %q", in) |
| 160 | require.Equal(t, wantP, string(gotP), "path input %q", in) |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | func Test_AppendEscape_EdgeCases(t *testing.T) { |
| 165 | t.Parallel() |
| 166 | all := make([]byte, 256) |
| 167 | for i := range all { |
| 168 | all[i] = byte(i) |
| 169 | } |
| 170 | inputs := []string{ |
| 171 | string(all), |
| 172 | strings.Repeat(" ", 64), |
| 173 | strings.Repeat("%", 64), |
| 174 | strings.Repeat("a ", 32), |
| 175 | strings.Repeat("unreserved-only.~_", 16), |
| 176 | "ends with unsafe/", |
| 177 | "/starts with unsafe", |
| 178 | } |
| 179 | for _, in := range inputs { |
| 180 | require.Equal(t, url.QueryEscape(in), string(AppendQueryEscape(nil, in)), "input %q", in) |
| 181 | require.Equal(t, url.PathEscape(in), string(AppendPathEscape(nil, in)), "input %q", in) |
| 182 | } |
| 183 | } |
| 184 | |
| 185 | func Test_AppendJSONString_BoundaryOffsets(t *testing.T) { |
| 186 | t.Parallel() |
| 187 | // Each special byte or sequence at every offset around the SWAR word |
| 188 | // width, so escapes land in every lane and on both sides of the |
| 189 | // word-loop/tail split. |
| 190 | specials := []string{ |
| 191 | "\"", "\\", "\n", "\r", "\t", "\b", "\f", "\x00", "\x1f", "\x7f", |
| 192 | "<", ">", "&", "\xff", "\xc3\xa9", "\u2028", "\u2029", "\xf0\x9f\x98\x80", "\xc3", |
| 193 | } |
| 194 | for _, sp := range specials { |
| 195 | for pad := range 18 { |
| 196 | in := strings.Repeat("a", pad) + sp + strings.Repeat("b", 17-pad) |
| 197 | assertJSONStringParity(t, in) |
| 198 | // And with the special at the very end, where the tail paths run. |
| 199 | assertJSONStringParity(t, strings.Repeat("a", pad)+sp) |
| 200 | } |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | func Test_AppendJSONString_UTF8EdgeCases(t *testing.T) { |
| 205 | t.Parallel() |
| 206 | inputs := []string{ |
| 207 | "\xc0\x80", // overlong NUL |
| 208 | "\xc1\xbf", // overlong |
| 209 | "\xe0\x80\xaf", // overlong 3-byte |
| 210 | "\xed\xa0\x80", // UTF-16 surrogate half |
| 211 | "\xed\xbf\xbf", // UTF-16 surrogate half |
| 212 | "\xf4\x8f\xbf\xbf", // U+10FFFF, highest valid rune |
| 213 | "\xf4\x90\x80\x80", // just above U+10FFFF |
| 214 | "\xf8\x88\x80\x80\x80", // 5-byte form |
| 215 | "\x80", // lone continuation |
| 216 | "\xbf\xbf", // lone continuations |
| 217 | "\xc3", // truncated 2-byte |
| 218 | "\xe2\x80", // truncated 3-byte (prefix of U+2028) |
| 219 | "\xf0\x9f\x98", // truncated 4-byte |
| 220 | "\xef\xbb\xbfBOM", // BOM prefix |
| 221 | "ok\xc3\xa9\xc3", // valid then truncated |
| 222 | "\xe2\x80\xa8\xe2\x80\xa9\xe2\x80\xaa", // U+2028, U+2029, then U+202A (unescaped) |
| 223 | strings.Repeat("\xff", 32), |
| 224 | strings.Repeat("\xc3\xa9", 16), |
| 225 | } |
| 226 | for _, in := range inputs { |
| 227 | assertJSONStringParity(t, in) |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | // assertJSONStringParity requires AppendJSONString to agree with |
| 232 | // encoding/json.Marshal byte for byte, for both instantiations. |
| 233 | func assertJSONStringParity(t *testing.T, in string) { |
| 234 | t.Helper() |
| 235 | want, err := json.Marshal(in) |
| 236 | require.NoError(t, err) |
| 237 | require.Equal(t, string(want), string(AppendJSONString(nil, in)), "input %q", in) |
| 238 | require.Equal(t, string(want), string(AppendJSONString(nil, []byte(in))), "input %q", in) |
| 239 | } |
| 240 | |
| 241 | func Test_ParseIP_PositionalMutations(t *testing.T) { |
| 242 | t.Parallel() |
| 243 | bases := []string{ |
| 244 | "203.0.113.195", |
| 245 | "::1", |
| 246 | "2001:db8::8a2e:370:7334", |
| 247 | "1:2:3:4:5:6:7:8", |
| 248 | "::ffff:1.2.3.4", |
| 249 | "1:2:3:4:5:6:1.2.3.4", |
| 250 | "fe80::1%eth0", |
| 251 | } |
| 252 | replacements := []byte{'.', ':', '%', '0', '9', 'a', 'f', 'g', 'G', '-', ' ', 0x00, 0xFF} |
| 253 | for _, base := range bases { |
| 254 | for pos := range len(base) { |
| 255 | for _, r := range replacements { |
| 256 | assertParseIPParity(t, base[:pos]+string(r)+base[pos+1:]) |
| 257 | } |
| 258 | assertParseIPParity(t, base[:pos]) // every truncation |
| 259 | } |
| 260 | assertParseIPParity(t, base+":") |
| 261 | assertParseIPParity(t, base+".") |
| 262 | assertParseIPParity(t, base+"0") |
| 263 | assertParseIPParity(t, base+"%") |
| 264 | } |
| 265 | } |
| 266 | |
| 267 | func Test_ParseIP_MoreEdgeCases(t *testing.T) { |
| 268 | t.Parallel() |
| 269 | inputs := []string{ |
| 270 | "1;2", // field followed by a non-separator byte |
| 271 | "1:2;3", // same, after a valid group |
| 272 | "fe80::1%%", // zone containing '%' |
| 273 | "1::2%", // empty zone after valid address |
| 274 | "::%25eth0", |
| 275 | "1:2:3:4:5:6:7:8%eth0", // full address with zone |
| 276 | "0.0.0.256", |
| 277 | "0.0.0.00", |
| 278 | "255.255.255.2555", |
| 279 | "::0.0.0.0", |
| 280 | "0::00:0:00:0.0.0.0", |
| 281 | "1:2:3:4:5:1.2.3.4", // v4 tail too early without ellipsis |
| 282 | "1:2:3:4:5:6:7:1.2.3.4", // v4 tail too late |
| 283 | "::ffff:016.1.2.3", |
| 284 | "ABCD:EF01:2345:6789:abcd:ef01:2345:6789", |
| 285 | "0000:0000:0000:0000:0000:0000:0000:0000", |
| 286 | "00000::", |
| 287 | "::00000", |
| 288 | "2:2:2:2:2:2:2:2:", |
| 289 | "\x00::", |
| 290 | "1.2.3.4%eth0", // zones are IPv6-only |
| 291 | } |
| 292 | for _, in := range inputs { |
| 293 | assertParseIPParity(t, in) |
| 294 | } |
| 295 | } |
| 296 | |
| 297 | // assertParseIPParity requires ParseIPv4/ParseIPv6 to accept exactly the |
| 298 | // per-family netip.ParseAddr inputs and produce identical addresses, for |
| 299 | // both instantiations. |
| 300 | func assertParseIPParity(t *testing.T, in string) { |
| 301 | t.Helper() |
| 302 | want, wantErr := netip.ParseAddr(in) |
| 303 | hasColon := strings.ContainsRune(in, ':') |
| 304 | |
| 305 | got4, ok4 := ParseIPv4(in) |
| 306 | _, ok4B := ParseIPv4([]byte(in)) |
| 307 | require.Equal(t, wantErr == nil && !hasColon, ok4, "ParseIPv4 input %q (netip err: %v)", in, wantErr) |
| 308 | require.Equal(t, ok4, ok4B, "ParseIPv4 bytes input %q", in) |
| 309 | if ok4 { |
| 310 | require.Equal(t, want, got4, "ParseIPv4 input %q", in) |
| 311 | } else { |
| 312 | require.Equal(t, netip.Addr{}, got4, "ParseIPv4 input %q", in) |
| 313 | } |
| 314 | |
| 315 | got6, ok6 := ParseIPv6(in) |
| 316 | _, ok6B := ParseIPv6([]byte(in)) |
| 317 | require.Equal(t, wantErr == nil && hasColon, ok6, "ParseIPv6 input %q (netip err: %v)", in, wantErr) |
| 318 | require.Equal(t, ok6, ok6B, "ParseIPv6 bytes input %q", in) |
| 319 | if ok6 { |
| 320 | require.Equal(t, want, got6, "ParseIPv6 input %q", in) |
| 321 | } else { |
| 322 | require.Equal(t, netip.Addr{}, got6, "ParseIPv6 input %q", in) |
| 323 | } |
| 324 | } |
| 325 | |
| 326 | func Test_CanonicalHeaderKey_PositionalMutations(t *testing.T) { |
| 327 | t.Parallel() |
| 328 | bases := []string{"content-type", "X-Request-Id", "a", "-", "--", "a-", "-a", "A--b"} |
| 329 | for _, base := range bases { |
| 330 | for pos := range len(base) { |
| 331 | for c := range 256 { |
| 332 | in := base[:pos] + string(byte(c)) + base[pos+1:] |
| 333 | want := http.CanonicalHeaderKey(in) |
| 334 | require.Equal(t, want, CanonicalHeaderKey(in), "input %q", in) |
| 335 | require.Equal(t, want, string(CanonicalHeaderKey([]byte(in))), "input %q", in) |
| 336 | } |
| 337 | } |
| 338 | } |
| 339 | } |
| 340 | |
| 341 | func Test_CanonicalHeaderKey_InputImmutability(t *testing.T) { |
| 342 | t.Parallel() |
| 343 | // The rewrite path must copy, never mutate the caller's bytes. |
| 344 | in := []byte("content-TYPE") |
| 345 | out := CanonicalHeaderKey(in) |
| 346 | require.Equal(t, "content-TYPE", string(in)) |
| 347 | require.Equal(t, "Content-Type", string(out)) |
| 348 | } |